blob: 5f7dcd5d46a9b9d4221f16b5df0619cc1b26535c [file] [log] [blame]
Ted Kremenek21475702008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
Ted Kremenek21475702008-09-05 17:16:31 +000013#include "clang/AST/DeclCXX.h"
14#include "clang/AST/ASTContext.h"
Faisal Vali2b391ab2013-09-26 19:54:12 +000015#include "clang/AST/ASTLambda.h"
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000016#include "clang/AST/ASTMutationListener.h"
Douglas Gregor8fb95122010-09-29 00:15:42 +000017#include "clang/AST/CXXInheritance.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/AST/DeclTemplate.h"
Anders Carlsson5bbe1d72009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregord73f3dd2011-11-01 01:16:03 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +000021#include "clang/AST/TypeLoc.h"
Douglas Gregorb6acda02008-11-12 23:21:09 +000022#include "clang/Basic/IdentifierTable.h"
Douglas Gregor74a34442008-12-23 21:31:30 +000023#include "llvm/ADT/STLExtras.h"
Fariborz Jahaniana9540492009-09-12 19:52:10 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek21475702008-09-05 17:16:31 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Decl Allocation/Deallocation Method Implementations
29//===----------------------------------------------------------------------===//
Douglas Gregor5101c242008-12-05 18:15:24 +000030
David Blaikie68e081d2011-12-20 02:48:34 +000031void AccessSpecDecl::anchor() { }
32
Douglas Gregor72172e92012-01-05 21:55:30 +000033AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +000034 return new (C, ID) AccessSpecDecl(EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +000035}
36
Richard Smitha4ba74c2013-08-30 04:46:40 +000037void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
38 ExternalASTSource *Source = C.getExternalSource();
39 assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
40 assert(Source && "getFromExternalSource with no external source");
41
42 for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
43 I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
44 reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
45 Impl.Decls.setLazy(false);
46}
47
John McCall67da35c2010-02-04 22:26:26 +000048CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Richard Smith328aae52012-11-30 05:11:39 +000049 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
Eli Friedmanc80ca682009-08-15 22:23:00 +000050 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruth583edf82011-04-30 10:07:30 +000051 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carruthb1963742011-04-30 09:17:45 +000052 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Richard Smithab44d5b2013-12-10 08:25:00 +000053 HasMutableFields(false), HasVariantMembers(false), HasOnlyCMembers(true),
Richard Smith593f9932012-12-08 02:01:17 +000054 HasInClassInitializer(false), HasUninitializedReferenceMember(false),
Richard Smith6b02d462012-12-08 08:32:28 +000055 NeedOverloadResolutionForMoveConstructor(false),
56 NeedOverloadResolutionForMoveAssignment(false),
57 NeedOverloadResolutionForDestructor(false),
58 DefaultedMoveConstructorIsDeleted(false),
59 DefaultedMoveAssignmentIsDeleted(false),
60 DefaultedDestructorIsDeleted(false),
Richard Smith328aae52012-11-30 05:11:39 +000061 HasTrivialSpecialMembers(SMF_All),
Richard Smith92f241f2012-12-08 02:53:02 +000062 DeclaredNonTrivialSpecialMembers(0),
Richard Smith328aae52012-11-30 05:11:39 +000063 HasIrrelevantDestructor(true),
Richard Smithcc36f692011-12-22 02:22:31 +000064 HasConstexprNonCopyMoveConstructor(false),
65 DefaultedDefaultConstructorIsConstexpr(true),
Richard Smith328aae52012-11-30 05:11:39 +000066 HasConstexprDefaultConstructor(false),
Alexis Huntf479f1b2011-05-09 18:22:59 +000067 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Richard Smith328aae52012-11-30 05:11:39 +000068 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smith1c33fe82012-11-28 06:23:12 +000069 ImplicitCopyConstructorHasConstParam(true),
70 ImplicitCopyAssignmentHasConstParam(true),
71 HasDeclaredCopyConstructorWithConstParam(false),
72 HasDeclaredCopyAssignmentWithConstParam(false),
Richard Smith1c33fe82012-11-28 06:23:12 +000073 IsLambda(false), NumBases(0), NumVBases(0), Bases(), VBases(),
Richard Smith68ad0e72013-06-26 02:41:25 +000074 Definition(D), FirstFriend() {
John McCall67da35c2010-02-04 22:26:26 +000075}
76
Benjamin Kramer300c0632012-07-04 17:03:33 +000077CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
78 return Bases.get(Definition->getASTContext().getExternalSource());
79}
80
81CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
82 return VBases.get(Definition->getASTContext().getExternalSource());
83}
84
John McCall67da35c2010-02-04 22:26:26 +000085CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000086 SourceLocation StartLoc, SourceLocation IdLoc,
87 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
88 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall67da35c2010-02-04 22:26:26 +000089 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +000090 TemplateOrInstantiation() { }
Douglas Gregorb6acda02008-11-12 23:21:09 +000091
Jay Foad39c79802011-01-12 09:06:06 +000092CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000093 DeclContext *DC, SourceLocation StartLoc,
94 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000095 CXXRecordDecl* PrevDecl,
96 bool DelayTypeCreation) {
Richard Smithf7981722013-11-22 09:01:48 +000097 CXXRecordDecl *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, DC, StartLoc,
98 IdLoc, Id, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +000099 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump11289f42009-09-09 15:08:12 +0000100
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000101 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000102 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000103 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000104 return R;
105}
106
Douglas Gregorc8a73492012-02-13 15:44:47 +0000107CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Eli Friedmand564afb2012-09-19 01:18:11 +0000108 TypeSourceInfo *Info, SourceLocation Loc,
Faisal Valic1a6dc42013-10-23 16:10:50 +0000109 bool Dependent, bool IsGeneric,
110 LambdaCaptureDefault CaptureDefault) {
Richard Smithf7981722013-11-22 09:01:48 +0000111 CXXRecordDecl *R =
112 new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc, 0, 0);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000113 R->IsBeingDefined = true;
Faisal Valic1a6dc42013-10-23 16:10:50 +0000114 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info,
115 Dependent,
116 IsGeneric,
117 CaptureDefault);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000118 R->MayHaveOutOfDateDef = false;
James Dennett8f60cdd2013-09-05 17:46:21 +0000119 R->setImplicit(true);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000120 C.getTypeDeclType(R, /*PrevDecl=*/0);
121 return R;
122}
123
Douglas Gregor72172e92012-01-05 21:55:30 +0000124CXXRecordDecl *
125CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000126 CXXRecordDecl *R = new (C, ID) CXXRecordDecl(
127 CXXRecord, TTK_Struct, 0, SourceLocation(), SourceLocation(), 0, 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000128 R->MayHaveOutOfDateDef = false;
129 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000130}
131
Mike Stump11289f42009-09-09 15:08:12 +0000132void
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000133CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000134 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000135 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000136
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000137 if (!data().Bases.isOffset() && data().NumBases > 0)
138 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000139
Richard Smithaed32b42011-10-18 20:08:55 +0000140 if (NumBases) {
141 // C++ [dcl.init.aggr]p1:
142 // An aggregate is [...] a class with [...] no base classes [...].
143 data().Aggregate = false;
144
145 // C++ [class]p4:
146 // A POD-struct is an aggregate class...
147 data().PlainOldData = false;
148 }
149
Anders Carlssonabb20e62010-03-29 05:13:12 +0000150 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000151 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000152
153 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000154 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000155
John McCall67da35c2010-02-04 22:26:26 +0000156 data().Bases = new(C) CXXBaseSpecifier [NumBases];
157 data().NumBases = NumBases;
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000158 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000159 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000160 // Keep track of inherited vbases for this base class.
161 const CXXBaseSpecifier *Base = Bases[i];
162 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000163 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000164 if (BaseType->isDependentType())
165 continue;
166 CXXRecordDecl *BaseClassDecl
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000167 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000168
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000169 // A class with a non-empty base class is not empty.
170 // FIXME: Standard ref?
Chandler Carruthb1963742011-04-30 09:17:45 +0000171 if (!BaseClassDecl->isEmpty()) {
172 if (!data().Empty) {
173 // C++0x [class]p7:
174 // A standard-layout class is a class that:
175 // [...]
176 // -- either has no non-static data members in the most derived
177 // class and at most one base class with non-static data members,
178 // or has no base classes with non-static data members, and
179 // If this is the second non-empty base, then neither of these two
180 // clauses can be true.
Chandler Carruth583edf82011-04-30 10:07:30 +0000181 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000182 }
183
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000184 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000185 data().HasNoNonEmptyBases = false;
186 }
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000187
Douglas Gregor11c024b2010-09-28 20:50:54 +0000188 // C++ [class.virtual]p1:
189 // A class that declares or inherits a virtual function is called a
190 // polymorphic class.
191 if (BaseClassDecl->isPolymorphic())
192 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000193
Chandler Carruthb1963742011-04-30 09:17:45 +0000194 // C++0x [class]p7:
195 // A standard-layout class is a class that: [...]
196 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000197 if (!BaseClassDecl->isStandardLayout())
198 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000199
Chandler Carruthe71d0622011-04-24 02:49:34 +0000200 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000201 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000202 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000203
Anders Carlssonabb20e62010-03-29 05:13:12 +0000204 // Now go through all virtual bases of this base and add them.
Mike Stump11289f42009-09-09 15:08:12 +0000205 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000206 BaseClassDecl->vbases_begin(),
207 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000208 // Add this base if it's not already in the list.
Richard Smith1c33fe82012-11-28 06:23:12 +0000209 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000210 VBases.push_back(VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000211
212 // C++11 [class.copy]p8:
213 // The implicitly-declared copy constructor for a class X will have
214 // the form 'X::X(const X&)' if each [...] virtual base class B of X
215 // has a copy constructor whose first parameter is of type
216 // 'const B&' or 'const volatile B&' [...]
217 if (CXXRecordDecl *VBaseDecl = VBase->getType()->getAsCXXRecordDecl())
218 if (!VBaseDecl->hasCopyConstructorWithConstParam())
219 data().ImplicitCopyConstructorHasConstParam = false;
220 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000221 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000222
223 if (Base->isVirtual()) {
224 // Add this base if it's not already in the list.
Anders Carlssone47380f2010-03-29 19:49:09 +0000225 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Richard Smith1c33fe82012-11-28 06:23:12 +0000226 VBases.push_back(Base);
227
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000228 // C++0x [meta.unary.prop] is_empty:
229 // T is a class type, but not a union type, with ... no virtual base
230 // classes
231 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000232
Richard Smith328aae52012-11-30 05:11:39 +0000233 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
234 // A [default constructor, copy/move constructor, or copy/move assignment
235 // operator for a class X] is trivial [...] if:
236 // -- class X has [...] no virtual base classes
237 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000238
239 // C++0x [class]p7:
240 // A standard-layout class is a class that: [...]
241 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000242 data().IsStandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000243
244 // C++11 [dcl.constexpr]p4:
245 // In the definition of a constexpr constructor [...]
246 // -- the class shall not have any virtual base classes
247 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000248 } else {
249 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000250 // A default constructor is trivial [...] if:
251 // -- all the direct base classes of its class have trivial default
252 // constructors.
253 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000254 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
255
Chandler Carruthad7d4042011-04-23 23:10:33 +0000256 // C++0x [class.copy]p13:
257 // A copy/move constructor for class X is trivial if [...]
258 // [...]
259 // -- the constructor selected to copy/move each direct base class
260 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000261 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000262 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000263 // If the base class doesn't have a simple move constructor, we'll eagerly
264 // declare it and perform overload resolution to determine which function
265 // it actually calls. If it does have a simple move constructor, this
266 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000267 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000268 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000269
270 // C++0x [class.copy]p27:
271 // A copy/move assignment operator for class X is trivial if [...]
272 // [...]
273 // -- the assignment operator selected to copy/move each direct base
274 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000275 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000276 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000277 // If the base class doesn't have a simple move assignment, we'll eagerly
278 // declare it and perform overload resolution to determine which function
279 // it actually calls. If it does have a simple move assignment, this
280 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000281 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000282 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000283
284 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000285 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000286 // requirements of a constexpr constructor, the implicitly-defined
287 // default constructor is constexpr.
288 if (!BaseClassDecl->hasConstexprDefaultConstructor())
289 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000290 }
Richard Smith92f241f2012-12-08 02:53:02 +0000291
Douglas Gregor11c024b2010-09-28 20:50:54 +0000292 // C++ [class.ctor]p3:
293 // A destructor is trivial if all the direct base classes of its class
294 // have trivial destructors.
295 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000296 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000297
298 if (!BaseClassDecl->hasIrrelevantDestructor())
299 data().HasIrrelevantDestructor = false;
300
Richard Smith1c33fe82012-11-28 06:23:12 +0000301 // C++11 [class.copy]p18:
302 // The implicitly-declared copy assignment oeprator for a class X will
303 // have the form 'X& X::operator=(const X&)' if each direct base class B
304 // of X has a copy assignment operator whose parameter is of type 'const
305 // B&', 'const volatile B&', or 'B' [...]
306 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
307 data().ImplicitCopyAssignmentHasConstParam = false;
308
309 // C++11 [class.copy]p8:
310 // The implicitly-declared copy constructor for a class X will have
311 // the form 'X::X(const X&)' if each direct [...] base class B of X
312 // has a copy constructor whose first parameter is of type
313 // 'const B&' or 'const volatile B&' [...]
314 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
315 data().ImplicitCopyConstructorHasConstParam = false;
316
John McCall31168b02011-06-15 23:02:42 +0000317 // A class has an Objective-C object member if... or any of its bases
318 // has an Objective-C object member.
319 if (BaseClassDecl->hasObjectMember())
320 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000321
322 if (BaseClassDecl->hasVolatileMember())
323 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000324
Douglas Gregor61226d32011-05-13 01:05:07 +0000325 // Keep track of the presence of mutable fields.
326 if (BaseClassDecl->hasMutableFields())
327 data().HasMutableFields = true;
Richard Smith593f9932012-12-08 02:01:17 +0000328
329 if (BaseClassDecl->hasUninitializedReferenceMember())
330 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000331
332 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000333 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000334
335 if (VBases.empty())
336 return;
337
338 // Create base specifier for any direct or indirect virtual bases.
339 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
340 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000341 for (int I = 0, E = VBases.size(); I != E; ++I) {
342 QualType Type = VBases[I]->getType();
343 if (!Type->isDependentType())
344 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000345 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000346 }
347}
348
349void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
350 // C++11 [class.copy]p11:
351 // A defaulted copy/move constructor for a class X is defined as
352 // deleted if X has:
353 // -- a direct or virtual base class B that cannot be copied/moved [...]
354 // -- a non-static data member of class type M (or array thereof)
355 // that cannot be copied or moved [...]
356 if (!Subobj->hasSimpleMoveConstructor())
357 data().NeedOverloadResolutionForMoveConstructor = true;
358
359 // C++11 [class.copy]p23:
360 // A defaulted copy/move assignment operator for a class X is defined as
361 // deleted if X has:
362 // -- a direct or virtual base class B that cannot be copied/moved [...]
363 // -- a non-static data member of class type M (or array thereof)
364 // that cannot be copied or moved [...]
365 if (!Subobj->hasSimpleMoveAssignment())
366 data().NeedOverloadResolutionForMoveAssignment = true;
367
368 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
369 // A defaulted [ctor or dtor] for a class X is defined as
370 // deleted if X has:
371 // -- any direct or virtual base class [...] has a type with a destructor
372 // that is deleted or inaccessible from the defaulted [ctor or dtor].
373 // -- any non-static data member has a type with a destructor
374 // that is deleted or inaccessible from the defaulted [ctor or dtor].
375 if (!Subobj->hasSimpleDestructor()) {
376 data().NeedOverloadResolutionForMoveConstructor = true;
377 data().NeedOverloadResolutionForDestructor = true;
378 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000379}
380
Douglas Gregord2e6a452010-01-14 17:47:39 +0000381/// Callback function for CXXRecordDecl::forallBases that acknowledges
382/// that it saw a base class.
383static bool SawBase(const CXXRecordDecl *, void *) {
384 return true;
385}
386
387bool CXXRecordDecl::hasAnyDependentBases() const {
388 if (!isDependentContext())
389 return false;
390
391 return !forallBases(SawBase, 0);
392}
393
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000394bool CXXRecordDecl::isTriviallyCopyable() const {
395 // C++0x [class]p5:
396 // A trivially copyable class is a class that:
397 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000398 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000399 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000400 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000401 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000402 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000403 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000404 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000405 // -- has a trivial destructor.
406 if (!hasTrivialDestructor()) return false;
407
408 return true;
409}
410
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000411void CXXRecordDecl::markedVirtualFunctionPure() {
412 // C++ [class.abstract]p2:
413 // A class is abstract if it has at least one pure virtual function.
414 data().Abstract = true;
415}
416
417void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000418 if (!D->isImplicit() &&
419 !isa<FieldDecl>(D) &&
420 !isa<IndirectFieldDecl>(D) &&
421 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
422 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
423 data().HasOnlyCMembers = false;
424
425 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000426 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000427 return;
428
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000429 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
430 if (FunTmpl)
431 D = FunTmpl->getTemplatedDecl();
432
Douglas Gregora832d3e2010-09-28 19:45:33 +0000433 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
434 if (Method->isVirtual()) {
435 // C++ [dcl.init.aggr]p1:
436 // An aggregate is an array or a class with [...] no virtual functions.
437 data().Aggregate = false;
438
439 // C++ [class]p4:
440 // A POD-struct is an aggregate class...
441 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000442
443 // Virtual functions make the class non-empty.
444 // FIXME: Standard ref?
445 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000446
447 // C++ [class.virtual]p1:
448 // A class that declares or inherits a virtual function is called a
449 // polymorphic class.
450 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000451
Richard Smith328aae52012-11-30 05:11:39 +0000452 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
453 // A [default constructor, copy/move constructor, or copy/move
454 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000455 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000456 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000457
Chandler Carruthb1963742011-04-30 09:17:45 +0000458 // C++0x [class]p7:
459 // A standard-layout class is a class that: [...]
460 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000461 data().IsStandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000462 }
463 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000464
Richard Smith1c33fe82012-11-28 06:23:12 +0000465 // Notify the listener if an implicit member was added after the definition
466 // was completed.
467 if (!isBeingDefined() && D->isImplicit())
468 if (ASTMutationListener *L = getASTMutationListener())
469 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000470
Richard Smith328aae52012-11-30 05:11:39 +0000471 // The kind of special member this declaration is, if any.
472 unsigned SMKind = 0;
473
Richard Smith1c33fe82012-11-28 06:23:12 +0000474 // Handle constructors.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000475 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000476 if (!Constructor->isImplicit()) {
477 // Note that we have a user-declared constructor.
478 data().UserDeclaredConstructor = true;
479
480 // C++ [class]p4:
481 // A POD-struct is an aggregate class [...]
482 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
483 // type is technically an aggregate in C++0x since it wouldn't be in 03.
484 data().PlainOldData = false;
485 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000486
Richard Smitha5285072011-09-05 02:13:09 +0000487 // Technically, "user-provided" is only defined for special member
488 // functions, but the intent of the standard is clearly that it should apply
489 // to all functions.
490 bool UserProvided = Constructor->isUserProvided();
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000491
Alexis Hunt88c75c32011-05-09 21:45:35 +0000492 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000493 SMKind |= SMF_DefaultConstructor;
494
495 if (UserProvided)
Alexis Huntea6f0322011-05-11 22:34:38 +0000496 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000497 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000498 data().HasConstexprDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000499 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000500
Chandler Carruthad7d4042011-04-23 23:10:33 +0000501 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000502 unsigned Quals;
503 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000504 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000505
506 if (Quals & Qualifiers::Const)
507 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000508 } else if (Constructor->isMoveConstructor())
509 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000510 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000511
512 // Record if we see any constexpr constructors which are neither copy
513 // nor move constructors.
514 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000515 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000516
Alexis Hunt88c75c32011-05-09 21:45:35 +0000517 // C++ [dcl.init.aggr]p1:
518 // An aggregate is an array or a class with no user-declared
519 // constructors [...].
Richard Smith852c9db2013-04-20 22:23:05 +0000520 // C++11 [dcl.init.aggr]p1:
Alexis Hunt88c75c32011-05-09 21:45:35 +0000521 // An aggregate is an array or a class with no user-provided
522 // constructors [...].
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000523 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smith1c33fe82012-11-28 06:23:12 +0000524 ? UserProvided : !Constructor->isImplicit())
Alexis Hunt88c75c32011-05-09 21:45:35 +0000525 data().Aggregate = false;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000526 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000527
Richard Smith1c33fe82012-11-28 06:23:12 +0000528 // Handle destructors.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000529 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000530 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000531
Richard Smith328aae52012-11-30 05:11:39 +0000532 if (!DD->isImplicit())
Richard Smith1c33fe82012-11-28 06:23:12 +0000533 data().HasIrrelevantDestructor = false;
534
Richard Smith1c33fe82012-11-28 06:23:12 +0000535 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000536 // A destructor is trivial if [...] the destructor is not virtual.
537 if (DD->isVirtual())
538 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000539 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000540
541 // Handle member functions.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000542 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000543 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000544 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000545
546 const ReferenceType *ParamTy =
547 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
548 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
549 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000550 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000551
Richard Smith328aae52012-11-30 05:11:39 +0000552 if (Method->isMoveAssignmentOperator())
553 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000554
Douglas Gregor457104e2010-09-29 04:25:11 +0000555 // Keep the list of conversion functions up-to-date.
556 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000557 // FIXME: We use the 'unsafe' accessor for the access specifier here,
558 // because Sema may not have set it yet. That's really just a misdesign
559 // in Sema. However, LLDB *will* have set the access specifier correctly,
560 // and adds declarations after the class is technically completed,
561 // so completeDefinition()'s overriding of the access specifiers doesn't
562 // work.
563 AccessSpecifier AS = Conversion->getAccessUnsafe();
564
Richard Smith328aae52012-11-30 05:11:39 +0000565 if (Conversion->getPrimaryTemplate()) {
566 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000567 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000568 ASTContext &Ctx = getASTContext();
569 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
570 NamedDecl *Primary =
571 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
572 if (Primary->getPreviousDecl())
573 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
574 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000575 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000576 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000577 }
578 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000579
Richard Smith328aae52012-11-30 05:11:39 +0000580 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000581 // If this is the first declaration of a special member, we no longer have
582 // an implicit trivial special member.
583 data().HasTrivialSpecialMembers &=
584 data().DeclaredSpecialMembers | ~SMKind;
585
586 if (!Method->isImplicit() && !Method->isUserProvided()) {
587 // This method is user-declared but not user-provided. We can't work out
588 // whether it's trivial yet (not until we get to the end of the class).
589 // We'll handle this method in finishedDefaultedOrDeletedMember.
590 } else if (Method->isTrivial())
591 data().HasTrivialSpecialMembers |= SMKind;
592 else
593 data().DeclaredNonTrivialSpecialMembers |= SMKind;
594
Richard Smith328aae52012-11-30 05:11:39 +0000595 // Note when we have declared a declared special member, and suppress the
596 // implicit declaration of this special member.
597 data().DeclaredSpecialMembers |= SMKind;
598
599 if (!Method->isImplicit()) {
600 data().UserDeclaredSpecialMembers |= SMKind;
601
602 // C++03 [class]p4:
603 // A POD-struct is an aggregate class that has [...] no user-defined
604 // copy assignment operator and no user-defined destructor.
605 //
606 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
607 // aggregates could not have any constructors, clear it even for an
608 // explicitly defaulted or deleted constructor.
609 // type is technically an aggregate in C++0x since it wouldn't be in 03.
610 //
611 // Also, a user-declared move assignment operator makes a class non-POD.
612 // This is an extension in C++03.
613 data().PlainOldData = false;
614 }
Richard Smith328aae52012-11-30 05:11:39 +0000615 }
616
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000617 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000618 }
Richard Smith328aae52012-11-30 05:11:39 +0000619
Douglas Gregora832d3e2010-09-28 19:45:33 +0000620 // Handle non-static data members.
621 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregor556e5862011-10-10 17:22:13 +0000622 // C++ [class.bit]p2:
623 // A declaration for a bit-field that omits the identifier declares an
624 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
625 // initialized.
626 if (Field->isUnnamedBitfield())
627 return;
628
Douglas Gregora832d3e2010-09-28 19:45:33 +0000629 // C++ [dcl.init.aggr]p1:
630 // An aggregate is an array or a class (clause 9) with [...] no
631 // private or protected non-static data members (clause 11).
632 //
633 // A POD must be an aggregate.
634 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
635 data().Aggregate = false;
636 data().PlainOldData = false;
637 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000638
639 // C++0x [class]p7:
640 // A standard-layout class is a class that:
641 // [...]
642 // -- has the same access control for all non-static data members,
643 switch (D->getAccess()) {
644 case AS_private: data().HasPrivateFields = true; break;
645 case AS_protected: data().HasProtectedFields = true; break;
646 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000647 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000648 };
649 if ((data().HasPrivateFields + data().HasProtectedFields +
650 data().HasPublicFields) > 1)
Chandler Carruth583edf82011-04-30 10:07:30 +0000651 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000652
Douglas Gregor61226d32011-05-13 01:05:07 +0000653 // Keep track of the presence of mutable fields.
654 if (Field->isMutable())
655 data().HasMutableFields = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000656
657 // C++11 [class.union]p8, DR1460:
658 // If X is a union, a non-static data member of X that is not an anonymous
659 // union is a variant member of X.
660 if (isUnion() && !Field->isAnonymousStructOrUnion())
661 data().HasVariantMembers = true;
662
Chandler Carruthad7d4042011-04-23 23:10:33 +0000663 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000664 // A POD struct is a class that is both a trivial class and a
665 // standard-layout class, and has no non-static data members of type
666 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000667 //
668 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
669 // that does not explicitly have no lifetime makes the class a non-POD.
670 // However, we delay setting PlainOldData to false in this case so that
671 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor6fa69422012-07-23 04:23:39 +0000672 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCall31168b02011-06-15 23:02:42 +0000673 // In this case, the class will become a non-POD class when we complete
674 // the definition.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000675 ASTContext &Context = getASTContext();
676 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000677 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000678 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCall31168b02011-06-15 23:02:42 +0000679 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
680 setHasObjectMember(true);
Eli Friedmana5433322013-07-20 01:06:31 +0000681 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000682 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000683
Chandler Carruthb1963742011-04-30 09:17:45 +0000684 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000685 if (!Field->hasInClassInitializer())
686 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000687
Chandler Carruthb1963742011-04-30 09:17:45 +0000688 // C++0x [class]p7:
689 // A standard-layout class is a class that:
690 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000691 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000692 }
693
Richard Smith3607ffe2012-02-13 03:54:03 +0000694 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000695 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000696 data().HasNonLiteralTypeFieldsOrBases = true;
697
Richard Smithab44d5b2013-12-10 08:25:00 +0000698 if (Field->hasInClassInitializer() ||
699 (Field->isAnonymousStructOrUnion() &&
700 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000701 data().HasInClassInitializer = true;
702
703 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000704 // A default constructor is trivial if [...] no non-static data member
705 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +0000706 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +0000707
Richard Smithe2648ba2012-05-07 01:07:30 +0000708 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +0000709 // An aggregate is a [...] class with [...] no
710 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +0000711 //
712 // This rule was removed in C++1y.
713 if (!getASTContext().getLangOpts().CPlusPlus1y)
714 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +0000715
Richard Smithe2648ba2012-05-07 01:07:30 +0000716 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +0000717 // A POD struct is [...] a trivial class.
718 data().PlainOldData = false;
719 }
720
Richard Smith6b02d462012-12-08 08:32:28 +0000721 // C++11 [class.copy]p23:
722 // A defaulted copy/move assignment operator for a class X is defined
723 // as deleted if X has:
724 // -- a non-static data member of reference type
725 if (T->isReferenceType())
726 data().DefaultedMoveAssignmentIsDeleted = true;
727
Douglas Gregor11c024b2010-09-28 20:50:54 +0000728 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
729 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
730 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +0000731 addedClassSubobject(FieldRec);
732
Richard Smithc91d12c2013-11-25 07:07:05 +0000733 // We may need to perform overload resolution to determine whether a
734 // field can be moved if it's const or volatile qualified.
735 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
736 data().NeedOverloadResolutionForMoveConstructor = true;
737 data().NeedOverloadResolutionForMoveAssignment = true;
738 }
739
Richard Smith6b02d462012-12-08 08:32:28 +0000740 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
741 // A defaulted [special member] for a class X is defined as
742 // deleted if:
743 // -- X is a union-like class that has a variant member with a
744 // non-trivial [corresponding special member]
745 if (isUnion()) {
746 if (FieldRec->hasNonTrivialMoveConstructor())
747 data().DefaultedMoveConstructorIsDeleted = true;
748 if (FieldRec->hasNonTrivialMoveAssignment())
749 data().DefaultedMoveAssignmentIsDeleted = true;
750 if (FieldRec->hasNonTrivialDestructor())
751 data().DefaultedDestructorIsDeleted = true;
752 }
753
Alexis Huntf479f1b2011-05-09 18:22:59 +0000754 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +0000755 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000756 // -- for all the non-static data members of its class that are of
757 // class type (or array thereof), each such class has a trivial
758 // default constructor.
759 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000760 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000761
762 // C++0x [class.copy]p13:
763 // A copy/move constructor for class X is trivial if [...]
764 // [...]
765 // -- for each non-static data member of X that is of class type (or
766 // an array thereof), the constructor selected to copy/move that
767 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000768 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000769 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000770 // If the field doesn't have a simple move constructor, we'll eagerly
771 // declare the move constructor for this class and we'll decide whether
772 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000773 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000774 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000775
776 // C++0x [class.copy]p27:
777 // A copy/move assignment operator for class X is trivial if [...]
778 // [...]
779 // -- for each non-static data member of X that is of class type (or
780 // an array thereof), the assignment operator selected to
781 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000782 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000783 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000784 // If the field doesn't have a simple move assignment, we'll eagerly
785 // declare the move assignment for this class and we'll decide whether
786 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000787 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000788 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000789
Douglas Gregor11c024b2010-09-28 20:50:54 +0000790 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000791 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000792 if (!FieldRec->hasIrrelevantDestructor())
793 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +0000794 if (FieldRec->hasObjectMember())
795 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000796 if (FieldRec->hasVolatileMember())
797 setHasVolatileMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +0000798
799 // C++0x [class]p7:
800 // A standard-layout class is a class that:
801 // -- has no non-static data members of type non-standard-layout
802 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +0000803 if (!FieldRec->isStandardLayout())
804 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000805
806 // C++0x [class]p7:
807 // A standard-layout class is a class that:
808 // [...]
809 // -- has no base classes of the same type as the first non-static
810 // data member.
811 // We don't want to expend bits in the state of the record decl
812 // tracking whether this is the first non-static data member so we
813 // cheat a bit and use some of the existing state: the empty bit.
814 // Virtual bases and virtual methods make a class non-empty, but they
815 // also make it non-standard-layout so we needn't check here.
816 // A non-empty base class may leave the class standard-layout, but not
Richard Smithab44d5b2013-12-10 08:25:00 +0000817 // if we have arrived here, and have at least one non-static data
Chandler Carruth583edf82011-04-30 10:07:30 +0000818 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carruthb1963742011-04-30 09:17:45 +0000819 // data member must come through here with Empty still true, and Empty
820 // will subsequently be set to false below.
Chandler Carruth583edf82011-04-30 10:07:30 +0000821 if (data().IsStandardLayout && data().Empty) {
Chandler Carruthb1963742011-04-30 09:17:45 +0000822 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
823 BE = bases_end();
824 BI != BE; ++BI) {
825 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000826 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000827 break;
828 }
829 }
830 }
Douglas Gregor61226d32011-05-13 01:05:07 +0000831
832 // Keep track of the presence of mutable fields.
833 if (FieldRec->hasMutableFields())
834 data().HasMutableFields = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000835
836 // C++11 [class.copy]p13:
837 // If the implicitly-defined constructor would satisfy the
838 // requirements of a constexpr constructor, the implicitly-defined
839 // constructor is constexpr.
840 // C++11 [dcl.constexpr]p4:
841 // -- every constructor involved in initializing non-static data
842 // members [...] shall be a constexpr constructor
843 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +0000844 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000845 // The standard requires any in-class initializer to be a constant
846 // expression. We consider this to be a defect.
847 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000848
849 // C++11 [class.copy]p8:
850 // The implicitly-declared copy constructor for a class X will have
851 // the form 'X::X(const X&)' if [...] for all the non-static data
852 // members of X that are of a class type M (or array thereof), each
853 // such class type has a copy constructor whose first parameter is
854 // of type 'const M&' or 'const volatile M&'.
855 if (!FieldRec->hasCopyConstructorWithConstParam())
856 data().ImplicitCopyConstructorHasConstParam = false;
857
858 // C++11 [class.copy]p18:
859 // The implicitly-declared copy assignment oeprator for a class X will
860 // have the form 'X& X::operator=(const X&)' if [...] for all the
861 // non-static data members of X that are of a class type M (or array
862 // thereof), each such class type has a copy assignment operator whose
863 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
864 if (!FieldRec->hasCopyAssignmentWithConstParam())
865 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +0000866
867 if (FieldRec->hasUninitializedReferenceMember() &&
868 !Field->hasInClassInitializer())
869 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000870
871 // C++11 [class.union]p8, DR1460:
872 // a non-static data member of an anonymous union that is a member of
873 // X is also a variant member of X.
874 if (FieldRec->hasVariantMembers() &&
875 Field->isAnonymousStructOrUnion())
876 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000877 }
Richard Smithcc36f692011-12-22 02:22:31 +0000878 } else {
879 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +0000880 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +0000881 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +0000882 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000883
884 // C++11 [class.copy]p23:
885 // A defaulted copy/move assignment operator for a class X is defined
886 // as deleted if X has:
887 // -- a non-static data member of const non-class type (or array
888 // thereof)
889 if (T.isConstQualified())
890 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000891 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000892
893 // C++0x [class]p7:
894 // A standard-layout class is a class that:
895 // [...]
896 // -- either has no non-static data members in the most derived
897 // class and at most one base class with non-static data members,
898 // or has no base classes with non-static data members, and
899 // At this point we know that we have a non-static data member, so the last
900 // clause holds.
901 if (!data().HasNoNonEmptyBases)
Chandler Carruth583edf82011-04-30 10:07:30 +0000902 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000903
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000904 // If this is not a zero-length bit-field, then the class is not empty.
905 if (data().Empty) {
Richard Smithcaf33902011-10-10 18:28:20 +0000906 if (!Field->isBitField() ||
907 (!Field->getBitWidth()->isTypeDependent() &&
908 !Field->getBitWidth()->isValueDependent() &&
909 Field->getBitWidthValue(Context) != 0))
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000910 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000911 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000912 }
Douglas Gregor457104e2010-09-29 04:25:11 +0000913
914 // Handle using declarations of conversion functions.
Richard Smitha4ba74c2013-08-30 04:46:40 +0000915 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +0000916 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +0000917 == DeclarationName::CXXConversionFunctionName) {
918 ASTContext &Ctx = getASTContext();
919 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
920 }
921 }
Joao Matose9a3ed42012-08-31 22:18:20 +0000922}
923
Richard Smith92f241f2012-12-08 02:53:02 +0000924void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
925 assert(!D->isImplicit() && !D->isUserProvided());
926
927 // The kind of special member this declaration is, if any.
928 unsigned SMKind = 0;
929
930 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
931 if (Constructor->isDefaultConstructor()) {
932 SMKind |= SMF_DefaultConstructor;
933 if (Constructor->isConstexpr())
934 data().HasConstexprDefaultConstructor = true;
935 }
936 if (Constructor->isCopyConstructor())
937 SMKind |= SMF_CopyConstructor;
938 else if (Constructor->isMoveConstructor())
939 SMKind |= SMF_MoveConstructor;
940 else if (Constructor->isConstexpr())
941 // We may now know that the constructor is constexpr.
942 data().HasConstexprNonCopyMoveConstructor = true;
943 } else if (isa<CXXDestructorDecl>(D))
944 SMKind |= SMF_Destructor;
945 else if (D->isCopyAssignmentOperator())
946 SMKind |= SMF_CopyAssignment;
947 else if (D->isMoveAssignmentOperator())
948 SMKind |= SMF_MoveAssignment;
949
950 // Update which trivial / non-trivial special members we have.
951 // addedMember will have skipped this step for this member.
952 if (D->isTrivial())
953 data().HasTrivialSpecialMembers |= SMKind;
954 else
955 data().DeclaredNonTrivialSpecialMembers |= SMKind;
956}
957
Joao Matose9a3ed42012-08-31 22:18:20 +0000958bool CXXRecordDecl::isCLike() const {
959 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
960 !TemplateOrInstantiation.isNull())
961 return false;
962 if (!hasDefinition())
963 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000964
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +0000965 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000966}
Faisal Valic1a6dc42013-10-23 16:10:50 +0000967
Faisal Vali2b391ab2013-09-26 19:54:12 +0000968bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +0000969 if (!isLambda()) return false;
970 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +0000971}
972
973CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
974 if (!isLambda()) return 0;
Faisal Vali850da1a2013-09-29 17:08:32 +0000975 DeclarationName Name =
976 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000977 DeclContext::lookup_const_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +0000978
979 assert(!Calls.empty() && "Missing lambda call operator!");
980 assert(Calls.size() == 1 && "More than one lambda call operator!");
981
982 NamedDecl *CallOp = Calls.front();
983 if (FunctionTemplateDecl *CallOpTmpl =
984 dyn_cast<FunctionTemplateDecl>(CallOp))
985 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Faisal Vali2b391ab2013-09-26 19:54:12 +0000986
987 return cast<CXXMethodDecl>(CallOp);
988}
989
990CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
991 if (!isLambda()) return 0;
Faisal Vali850da1a2013-09-29 17:08:32 +0000992 DeclarationName Name =
993 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Faisal Vali2b391ab2013-09-26 19:54:12 +0000994 DeclContext::lookup_const_result Invoker = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +0000995 if (Invoker.empty()) return 0;
996 assert(Invoker.size() == 1 && "More than one static invoker operator!");
997 NamedDecl *InvokerFun = Invoker.front();
998 if (FunctionTemplateDecl *InvokerTemplate =
999 dyn_cast<FunctionTemplateDecl>(InvokerFun))
1000 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1001
Faisal Vali571df122013-09-29 08:45:24 +00001002 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001003}
1004
Douglas Gregor9c702202012-02-10 07:45:31 +00001005void CXXRecordDecl::getCaptureFields(
1006 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001007 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001008 Captures.clear();
1009 ThisCapture = 0;
1010
Douglas Gregorc8a73492012-02-13 15:44:47 +00001011 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001012 RecordDecl::field_iterator Field = field_begin();
Douglas Gregore5561632012-02-13 17:20:40 +00001013 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001014 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001015 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001016 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001017 else if (C->capturesVariable())
1018 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001019 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001020 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001021}
1022
Faisal Vali2b391ab2013-09-26 19:54:12 +00001023TemplateParameterList *
1024CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
1025 if (!isLambda()) return 0;
1026 CXXMethodDecl *CallOp = getLambdaCallOperator();
1027 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1028 return Tmpl->getTemplateParameters();
1029 return 0;
1030}
Douglas Gregor9c702202012-02-10 07:45:31 +00001031
John McCall1e3a1a72010-03-15 09:07:48 +00001032static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1033 QualType T;
John McCallda4458e2010-03-31 01:36:47 +00001034 if (isa<UsingShadowDecl>(Conv))
1035 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCall1e3a1a72010-03-15 09:07:48 +00001036 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
1037 T = ConvTemp->getTemplatedDecl()->getResultType();
1038 else
1039 T = cast<CXXConversionDecl>(Conv)->getConversionType();
1040 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001041}
1042
John McCall1e3a1a72010-03-15 09:07:48 +00001043/// Collect the visible conversions of a base class.
1044///
James Dennettb5d5f812012-06-15 22:28:09 +00001045/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001046/// \param InVirtual whether this base class is a virtual base (or a base
1047/// of a virtual base)
1048/// \param Access the access along the inheritance path to this base
1049/// \param ParentHiddenTypes the conversions provided by the inheritors
1050/// of this base
1051/// \param Output the set to which to add conversions from non-virtual bases
1052/// \param VOutput the set to which to add conversions from virtual bases
1053/// \param HiddenVBaseCs the set of conversions which were hidden in a
1054/// virtual base along some inheritance path
1055static void CollectVisibleConversions(ASTContext &Context,
1056 CXXRecordDecl *Record,
1057 bool InVirtual,
1058 AccessSpecifier Access,
1059 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001060 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001061 UnresolvedSetImpl &VOutput,
1062 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1063 // The set of types which have conversions in this class or its
1064 // subclasses. As an optimization, we don't copy the derived set
1065 // unless it might change.
1066 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1067 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1068
1069 // Collect the direct conversions and figure out which conversions
1070 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001071 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1072 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1073 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001074 HiddenTypesBuffer = ParentHiddenTypes;
1075 HiddenTypes = &HiddenTypesBuffer;
1076
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001077 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001078 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1079 bool Hidden = ParentHiddenTypes.count(ConvType);
1080 if (!Hidden)
1081 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001082
1083 // If this conversion is hidden and we're in a virtual base,
1084 // remember that it's hidden along some inheritance path.
1085 if (Hidden && InVirtual)
1086 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1087
1088 // If this conversion isn't hidden, add it to the appropriate output.
1089 else if (!Hidden) {
1090 AccessSpecifier IAccess
1091 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1092
1093 if (InVirtual)
1094 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001095 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001096 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001097 }
1098 }
1099 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001100
John McCall1e3a1a72010-03-15 09:07:48 +00001101 // Collect information recursively from any base classes.
1102 for (CXXRecordDecl::base_class_iterator
1103 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1104 const RecordType *RT = I->getType()->getAs<RecordType>();
1105 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001106
John McCall1e3a1a72010-03-15 09:07:48 +00001107 AccessSpecifier BaseAccess
1108 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1109 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001110
John McCall1e3a1a72010-03-15 09:07:48 +00001111 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1112 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1113 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001114 }
John McCall1e3a1a72010-03-15 09:07:48 +00001115}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001116
John McCall1e3a1a72010-03-15 09:07:48 +00001117/// Collect the visible conversions of a class.
1118///
1119/// This would be extremely straightforward if it weren't for virtual
1120/// bases. It might be worth special-casing that, really.
1121static void CollectVisibleConversions(ASTContext &Context,
1122 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001123 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001124 // The collection of all conversions in virtual bases that we've
1125 // found. These will be added to the output as long as they don't
1126 // appear in the hidden-conversions set.
1127 UnresolvedSet<8> VBaseCs;
1128
1129 // The set of conversions in virtual bases that we've determined to
1130 // be hidden.
1131 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1132
1133 // The set of types hidden by classes derived from this one.
1134 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1135
1136 // Go ahead and collect the direct conversions and add them to the
1137 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001138 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1139 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001140 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001141 for (; ConvI != ConvE; ++ConvI)
1142 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001143
1144 // Recursively collect conversions from base classes.
1145 for (CXXRecordDecl::base_class_iterator
1146 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1147 const RecordType *RT = I->getType()->getAs<RecordType>();
1148 if (!RT) continue;
1149
1150 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1151 I->isVirtual(), I->getAccessSpecifier(),
1152 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1153 }
1154
1155 // Add any unhidden conversions provided by virtual bases.
1156 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1157 I != E; ++I) {
1158 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001159 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001160 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001161}
1162
1163/// getVisibleConversionFunctions - get all conversion functions visible
1164/// in current class; including conversion function templates.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001165std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1166CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001167 ASTContext &Ctx = getASTContext();
1168
1169 ASTUnresolvedSet *Set;
1170 if (bases_begin() == bases_end()) {
1171 // If root class, all conversions are visible.
1172 Set = &data().Conversions.get(Ctx);
1173 } else {
1174 Set = &data().VisibleConversions.get(Ctx);
1175 // If visible conversion list is not evaluated, evaluate it.
1176 if (!data().ComputedVisibleConversions) {
1177 CollectVisibleConversions(Ctx, this, *Set);
1178 data().ComputedVisibleConversions = true;
1179 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001180 }
Richard Smitha4ba74c2013-08-30 04:46:40 +00001181 return std::make_pair(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001182}
1183
John McCallda4458e2010-03-31 01:36:47 +00001184void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1185 // This operation is O(N) but extremely rare. Sema only uses it to
1186 // remove UsingShadowDecls in a class that were followed by a direct
1187 // declaration, e.g.:
1188 // class A : B {
1189 // using B::operator int;
1190 // operator int();
1191 // };
1192 // This is uncommon by itself and even more uncommon in conjunction
1193 // with sufficiently large numbers of directly-declared conversions
1194 // that asymptotic behavior matters.
1195
Richard Smitha4ba74c2013-08-30 04:46:40 +00001196 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001197 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1198 if (Convs[I].getDecl() == ConvDecl) {
1199 Convs.erase(I);
1200 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1201 && "conversion was found multiple times in unresolved set");
1202 return;
1203 }
1204 }
1205
1206 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001207}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001208
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001209CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001210 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001211 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1212
1213 return 0;
1214}
1215
1216void
1217CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1218 TemplateSpecializationKind TSK) {
1219 assert(TemplateOrInstantiation.isNull() &&
1220 "Previous template or instantiation?");
1221 assert(!isa<ClassTemplateSpecializationDecl>(this));
1222 TemplateOrInstantiation
1223 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1224}
1225
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001226TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1227 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001228 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1229 return Spec->getSpecializationKind();
1230
Douglas Gregor06db9f52009-10-12 20:18:28 +00001231 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001232 return MSInfo->getTemplateSpecializationKind();
1233
1234 return TSK_Undeclared;
1235}
1236
1237void
1238CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1239 if (ClassTemplateSpecializationDecl *Spec
1240 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1241 Spec->setSpecializationKind(TSK);
1242 return;
1243 }
1244
Douglas Gregor06db9f52009-10-12 20:18:28 +00001245 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001246 MSInfo->setTemplateSpecializationKind(TSK);
1247 return;
1248 }
1249
David Blaikie83d382b2011-09-23 05:06:16 +00001250 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001251}
1252
Douglas Gregorbac74902010-07-01 14:13:13 +00001253CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1254 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001255 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001256
1257 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001258 = Context.DeclarationNames.getCXXDestructorName(
1259 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001260
David Blaikieff7d47a2012-12-19 00:45:41 +00001261 DeclContext::lookup_const_result R = lookup(Name);
1262 if (R.empty())
Sebastian Redlb469afb2010-09-02 23:19:42 +00001263 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001264
David Blaikieff7d47a2012-12-19 00:45:41 +00001265 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001266 return Dtor;
1267}
1268
Douglas Gregorb11aad82011-02-19 18:51:44 +00001269void CXXRecordDecl::completeDefinition() {
1270 completeDefinition(0);
1271}
1272
1273void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1274 RecordDecl::completeDefinition();
1275
David Blaikiebbafb8a2012-03-11 07:00:24 +00001276 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +00001277 // Objective-C Automatic Reference Counting:
1278 // If a class has a non-static data member of Objective-C pointer
1279 // type (or array thereof), it is a non-POD type and its
Douglas Gregor6fa69422012-07-23 04:23:39 +00001280 // default constructor (if any), copy constructor, move constructor,
1281 // copy assignment operator, move assignment operator, and destructor are
1282 // non-trivial.
John McCall31168b02011-06-15 23:02:42 +00001283 struct DefinitionData &Data = data();
1284 Data.PlainOldData = false;
Richard Smith328aae52012-11-30 05:11:39 +00001285 Data.HasTrivialSpecialMembers = 0;
Richard Smith561fb152012-02-25 07:33:38 +00001286 Data.HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +00001287 }
1288
Douglas Gregor8fb95122010-09-29 00:15:42 +00001289 // If the class may be abstract (but hasn't been marked as such), check for
1290 // any pure final overriders.
1291 if (mayBeAbstract()) {
1292 CXXFinalOverriderMap MyFinalOverriders;
1293 if (!FinalOverriders) {
1294 getFinalOverriders(MyFinalOverriders);
1295 FinalOverriders = &MyFinalOverriders;
1296 }
1297
1298 bool Done = false;
1299 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1300 MEnd = FinalOverriders->end();
1301 M != MEnd && !Done; ++M) {
1302 for (OverridingMethods::iterator SO = M->second.begin(),
1303 SOEnd = M->second.end();
1304 SO != SOEnd && !Done; ++SO) {
1305 assert(SO->second.size() > 0 &&
1306 "All virtual functions have overridding virtual functions");
1307
1308 // C++ [class.abstract]p4:
1309 // A class is abstract if it contains or inherits at least one
1310 // pure virtual function for which the final overrider is pure
1311 // virtual.
1312 if (SO->second.front().Method->isPure()) {
1313 data().Abstract = true;
1314 Done = true;
1315 break;
1316 }
1317 }
1318 }
1319 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001320
1321 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001322 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001323 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001324 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001325}
1326
1327bool CXXRecordDecl::mayBeAbstract() const {
1328 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1329 isDependentContext())
1330 return false;
1331
1332 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1333 BEnd = bases_end();
1334 B != BEnd; ++B) {
1335 CXXRecordDecl *BaseDecl
1336 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1337 if (BaseDecl->isAbstract())
1338 return true;
1339 }
1340
1341 return false;
1342}
1343
David Blaikie68e081d2011-12-20 02:48:34 +00001344void CXXMethodDecl::anchor() { }
1345
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001346bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001347 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001348
1349 if (MD->getStorageClass() == SC_Static)
1350 return true;
1351
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001352 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1353 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001354}
1355
Rafael Espindola49e860b2012-06-26 17:45:31 +00001356static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1357 const CXXMethodDecl *BaseMD) {
1358 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1359 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1360 const CXXMethodDecl *MD = *I;
1361 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1362 return true;
1363 if (recursivelyOverrides(MD, BaseMD))
1364 return true;
1365 }
1366 return false;
1367}
1368
1369CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001370CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1371 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001372 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1373 return this;
1374
1375 // Lookup doesn't work for destructors, so handle them separately.
1376 if (isa<CXXDestructorDecl>(this)) {
1377 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001378 if (MD) {
1379 if (recursivelyOverrides(MD, this))
1380 return MD;
1381 if (MayBeBase && recursivelyOverrides(this, MD))
1382 return MD;
1383 }
Rafael Espindola49e860b2012-06-26 17:45:31 +00001384 return NULL;
1385 }
1386
1387 lookup_const_result Candidates = RD->lookup(getDeclName());
David Blaikieff7d47a2012-12-19 00:45:41 +00001388 for (NamedDecl * const * I = Candidates.begin(); I != Candidates.end(); ++I) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001389 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1390 if (!MD)
1391 continue;
1392 if (recursivelyOverrides(MD, this))
1393 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001394 if (MayBeBase && recursivelyOverrides(this, MD))
1395 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001396 }
1397
1398 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1399 E = RD->bases_end(); I != E; ++I) {
1400 const RecordType *RT = I->getType()->getAs<RecordType>();
1401 if (!RT)
1402 continue;
1403 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1404 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1405 if (T)
1406 return T;
1407 }
1408
1409 return NULL;
1410}
1411
Ted Kremenek21475702008-09-05 17:16:31 +00001412CXXMethodDecl *
1413CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001414 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001415 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001416 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001417 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001418 bool isConstexpr, SourceLocation EndLocation) {
Richard Smithf7981722013-11-22 09:01:48 +00001419 return new (C, RD) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
1420 SC, isInline, isConstexpr, EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001421}
1422
Douglas Gregor72172e92012-01-05 21:55:30 +00001423CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001424 return new (C, ID) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1425 DeclarationNameInfo(), QualType(), 0,
1426 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001427}
1428
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001429bool CXXMethodDecl::isUsualDeallocationFunction() const {
1430 if (getOverloadedOperator() != OO_Delete &&
1431 getOverloadedOperator() != OO_Array_Delete)
1432 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001433
1434 // C++ [basic.stc.dynamic.deallocation]p2:
1435 // A template instance is never a usual deallocation function,
1436 // regardless of its signature.
1437 if (getPrimaryTemplate())
1438 return false;
1439
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001440 // C++ [basic.stc.dynamic.deallocation]p2:
1441 // If a class T has a member deallocation function named operator delete
1442 // with exactly one parameter, then that function is a usual (non-placement)
1443 // deallocation function. [...]
1444 if (getNumParams() == 1)
1445 return true;
1446
1447 // C++ [basic.stc.dynamic.deallocation]p2:
1448 // [...] If class T does not declare such an operator delete but does
1449 // declare a member deallocation function named operator delete with
1450 // exactly two parameters, the second of which has type std::size_t (18.1),
1451 // then this function is a usual deallocation function.
1452 ASTContext &Context = getASTContext();
1453 if (getNumParams() != 2 ||
Chandler Carruth75cc3592010-02-08 18:54:05 +00001454 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1455 Context.getSizeType()))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001456 return false;
1457
1458 // This function is a usual deallocation function if there are no
1459 // single-parameter deallocation functions of the same kind.
David Blaikieff7d47a2012-12-19 00:45:41 +00001460 DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1461 for (DeclContext::lookup_const_result::iterator I = R.begin(), E = R.end();
1462 I != E; ++I) {
1463 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001464 if (FD->getNumParams() == 1)
1465 return false;
1466 }
1467
1468 return true;
1469}
1470
Douglas Gregorb139cd52010-05-01 20:49:11 +00001471bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00001472 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00001473 // A user-declared copy assignment operator X::operator= is a non-static
1474 // non-template member function of class X with exactly one parameter of
1475 // type X, X&, const X&, volatile X& or const volatile X&.
1476 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1477 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001478 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1479 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00001480 return false;
1481
1482 QualType ParamType = getParamDecl(0)->getType();
1483 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1484 ParamType = Ref->getPointeeType();
1485
1486 ASTContext &Context = getASTContext();
1487 QualType ClassType
1488 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1489 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1490}
1491
Alexis Huntfcaeae42011-05-25 20:50:04 +00001492bool CXXMethodDecl::isMoveAssignmentOperator() const {
1493 // C++0x [class.copy]p19:
1494 // A user-declared move assignment operator X::operator= is a non-static
1495 // non-template member function of class X with exactly one parameter of type
1496 // X&&, const X&&, volatile X&&, or const volatile X&&.
1497 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001498 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1499 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00001500 return false;
1501
1502 QualType ParamType = getParamDecl(0)->getType();
1503 if (!isa<RValueReferenceType>(ParamType))
1504 return false;
1505 ParamType = ParamType->getPointeeType();
1506
1507 ASTContext &Context = getASTContext();
1508 QualType ClassType
1509 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1510 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1511}
1512
Anders Carlsson36d87e12009-05-16 23:58:37 +00001513void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001514 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001515 assert(!MD->getParent()->isDependentContext() &&
1516 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00001517 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001518
Douglas Gregor832940b2010-03-02 23:58:15 +00001519 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001520}
1521
1522CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001523 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor832940b2010-03-02 23:58:15 +00001524 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001525}
1526
1527CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001528 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor832940b2010-03-02 23:58:15 +00001529 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001530}
1531
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001532unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001533 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001534 return getASTContext().overridden_methods_size(this);
1535}
1536
Ted Kremenek21475702008-09-05 17:16:31 +00001537QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00001538 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1539 // If the member function is declared const, the type of this is const X*,
1540 // if the member function is declared volatile, the type of this is
1541 // volatile X*, and if the member function is declared const volatile,
1542 // the type of this is const volatile X*.
1543
Ted Kremenek21475702008-09-05 17:16:31 +00001544 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00001545
John McCalle78aac42010-03-10 03:28:59 +00001546 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00001547 ClassTy = C.getQualifiedType(ClassTy,
1548 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00001549 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00001550}
1551
Eli Friedman71a26d82009-12-06 20:50:05 +00001552bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00001553 // If this function is a template instantiation, look at the template from
1554 // which it was instantiated.
1555 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1556 if (!CheckFn)
1557 CheckFn = this;
1558
Eli Friedman71a26d82009-12-06 20:50:05 +00001559 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001560 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +00001561}
1562
Douglas Gregor355efbb2012-02-17 03:02:34 +00001563bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00001564 const CXXRecordDecl *P = getParent();
1565 if (P->isLambda()) {
1566 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
1567 if (StaticInvoker == this) return true;
1568 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
1569 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
1570 }
1571 }
1572 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00001573}
1574
Alexis Hunt1d792652011-01-08 20:30:50 +00001575CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1576 TypeSourceInfo *TInfo, bool IsVirtual,
1577 SourceLocation L, Expr *Init,
1578 SourceLocation R,
1579 SourceLocation EllipsisLoc)
Alexis Hunta50dd462011-01-08 23:01:16 +00001580 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001581 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1582 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001583{
Douglas Gregore8381c02008-11-05 04:29:56 +00001584}
1585
Alexis Hunt1d792652011-01-08 20:30:50 +00001586CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1587 FieldDecl *Member,
1588 SourceLocation MemberLoc,
1589 SourceLocation L, Expr *Init,
1590 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001591 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001592 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichetd583da02010-12-04 09:14:42 +00001593 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1594{
1595}
1596
Alexis Hunt1d792652011-01-08 20:30:50 +00001597CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1598 IndirectFieldDecl *Member,
1599 SourceLocation MemberLoc,
1600 SourceLocation L, Expr *Init,
1601 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001602 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001603 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001604 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001605{
Douglas Gregore8381c02008-11-05 04:29:56 +00001606}
1607
Alexis Hunt1d792652011-01-08 20:30:50 +00001608CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001609 TypeSourceInfo *TInfo,
1610 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00001611 SourceLocation R)
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001612 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1613 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Alexis Huntc5575cc2011-02-26 19:13:13 +00001614 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1615{
1616}
1617
1618CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Alexis Hunt1d792652011-01-08 20:30:50 +00001619 FieldDecl *Member,
1620 SourceLocation MemberLoc,
1621 SourceLocation L, Expr *Init,
1622 SourceLocation R,
1623 VarDecl **Indices,
1624 unsigned NumIndices)
Alexis Hunta50dd462011-01-08 23:01:16 +00001625 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichetd583da02010-12-04 09:14:42 +00001626 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001627 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregor94f9a482010-05-05 05:51:00 +00001628{
1629 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1630 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1631}
1632
Alexis Hunt1d792652011-01-08 20:30:50 +00001633CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1634 FieldDecl *Member,
1635 SourceLocation MemberLoc,
1636 SourceLocation L, Expr *Init,
1637 SourceLocation R,
1638 VarDecl **Indices,
1639 unsigned NumIndices) {
1640 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregor94f9a482010-05-05 05:51:00 +00001641 sizeof(VarDecl *) * NumIndices,
Alexis Hunt1d792652011-01-08 20:30:50 +00001642 llvm::alignOf<CXXCtorInitializer>());
Alexis Hunta50dd462011-01-08 23:01:16 +00001643 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1644 Indices, NumIndices);
Douglas Gregor94f9a482010-05-05 05:51:00 +00001645}
1646
Alexis Hunt1d792652011-01-08 20:30:50 +00001647TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001648 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001649 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001650 else
1651 return TypeLoc();
1652}
1653
Alexis Hunt1d792652011-01-08 20:30:50 +00001654const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001655 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001656 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001657 else
1658 return 0;
1659}
1660
Alexis Hunt1d792652011-01-08 20:30:50 +00001661SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001662 if (isAnyMemberInitializer())
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001663 return getMemberLocation();
Richard Smith938f40b2011-06-11 17:19:42 +00001664
1665 if (isInClassMemberInitializer())
1666 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001667
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001668 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1669 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1670
1671 return SourceLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001672}
1673
Alexis Hunt1d792652011-01-08 20:30:50 +00001674SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001675 if (isInClassMemberInitializer()) {
1676 FieldDecl *D = getAnyMember();
1677 if (Expr *I = D->getInClassInitializer())
1678 return I->getSourceRange();
1679 return SourceRange();
1680 }
1681
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001682 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00001683}
1684
David Blaikie68e081d2011-12-20 02:48:34 +00001685void CXXConstructorDecl::anchor() { }
1686
Douglas Gregor61956c42008-10-31 09:07:45 +00001687CXXConstructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001688CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001689 return new (C, ID) CXXConstructorDecl(0, SourceLocation(),
1690 DeclarationNameInfo(), QualType(),
1691 0, false, false, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001692}
1693
1694CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00001695CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001696 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001697 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001698 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001699 bool isExplicit, bool isInline,
1700 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001701 assert(NameInfo.getName().getNameKind()
1702 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001703 "Name must refer to a constructor");
Richard Smithf7981722013-11-22 09:01:48 +00001704 return new (C, RD) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
1705 isExplicit, isInline,
1706 isImplicitlyDeclared, isConstexpr);
Douglas Gregor61956c42008-10-31 09:07:45 +00001707}
1708
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001709CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1710 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1711 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1712 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1713 return Construct->getConstructor();
1714
1715 return 0;
1716}
1717
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001718bool CXXConstructorDecl::isDefaultConstructor() const {
1719 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001720 // A default constructor for a class X is a constructor of class
1721 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001722 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00001723 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001724}
1725
Mike Stump11289f42009-09-09 15:08:12 +00001726bool
Douglas Gregor507eb872009-12-22 00:34:07 +00001727CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00001728 return isCopyOrMoveConstructor(TypeQuals) &&
1729 getParamDecl(0)->getType()->isLValueReferenceType();
1730}
1731
1732bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1733 return isCopyOrMoveConstructor(TypeQuals) &&
1734 getParamDecl(0)->getType()->isRValueReferenceType();
1735}
1736
1737/// \brief Determine whether this is a copy or move constructor.
1738bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001739 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001740 // A non-template constructor for class X is a copy constructor
1741 // if its first parameter is of type X&, const X&, volatile X& or
1742 // const volatile X&, and either there are no other parameters
1743 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00001744 // C++0x [class.copy]p3:
1745 // A non-template constructor for class X is a move constructor if its
1746 // first parameter is of type X&&, const X&&, volatile X&&, or
1747 // const volatile X&&, and either there are no other parameters or else
1748 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001749 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001750 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorff7028a2009-11-13 23:59:09 +00001751 (getPrimaryTemplate() != 0) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001752 (getDescribedFunctionTemplate() != 0))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001753 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001754
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001755 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00001756
1757 // Do we have a reference type?
1758 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00001759 if (!ParamRefType)
1760 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001761
Douglas Gregorff7028a2009-11-13 23:59:09 +00001762 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00001763 ASTContext &Context = getASTContext();
1764
Douglas Gregorff7028a2009-11-13 23:59:09 +00001765 CanQualType PointeeType
1766 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00001767 CanQualType ClassTy
1768 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001769 if (PointeeType.getUnqualifiedType() != ClassTy)
1770 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001771
John McCall8ccfcb52009-09-24 19:53:00 +00001772 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00001773
1774 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001775 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00001776 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001777}
1778
Anders Carlssond20e7952009-08-28 16:57:08 +00001779bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001780 // C++ [class.conv.ctor]p1:
1781 // A constructor declared without the function-specifier explicit
1782 // that can be called with a single parameter specifies a
1783 // conversion from the type of its first parameter to the type of
1784 // its class. Such a constructor is called a converting
1785 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00001786 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001787 return false;
1788
Mike Stump11289f42009-09-09 15:08:12 +00001789 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00001790 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001791 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00001792 (getNumParams() > 1 &&
1793 (getParamDecl(1)->hasDefaultArg() ||
1794 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001795}
Douglas Gregor61956c42008-10-31 09:07:45 +00001796
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00001797bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00001798 if ((getNumParams() < 1) ||
1799 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1800 (getPrimaryTemplate() == 0) ||
1801 (getDescribedFunctionTemplate() != 0))
1802 return false;
1803
1804 const ParmVarDecl *Param = getParamDecl(0);
1805
1806 ASTContext &Context = getASTContext();
1807 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1808
Douglas Gregorffe14e32009-11-14 01:20:54 +00001809 // Is it the same as our our class type?
1810 CanQualType ClassTy
1811 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1812 if (ParamType.getUnqualifiedType() != ClassTy)
1813 return false;
1814
1815 return true;
1816}
1817
Sebastian Redl08905022011-02-05 19:23:19 +00001818const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1819 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001820 method_iterator It = getASTContext().overridden_methods_begin(this);
1821 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redl08905022011-02-05 19:23:19 +00001822 return 0;
1823
1824 return cast<CXXConstructorDecl>(*It);
1825}
1826
1827void
1828CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1829 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001830 assert(getASTContext().overridden_methods_size(this) == 0 &&
1831 "Base ctor already set.");
1832 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redl08905022011-02-05 19:23:19 +00001833}
1834
David Blaikie68e081d2011-12-20 02:48:34 +00001835void CXXDestructorDecl::anchor() { }
1836
Douglas Gregor831c93f2008-11-05 20:51:48 +00001837CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001838CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001839 return new (C, ID) CXXDestructorDecl(
1840 0, SourceLocation(), DeclarationNameInfo(), QualType(), 0, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001841}
1842
1843CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00001844CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001845 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001846 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001847 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001848 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001849 assert(NameInfo.getName().getNameKind()
1850 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001851 "Name must refer to a destructor");
Richard Smithf7981722013-11-22 09:01:48 +00001852 return new (C, RD) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo,
1853 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001854}
1855
David Blaikie68e081d2011-12-20 02:48:34 +00001856void CXXConversionDecl::anchor() { }
1857
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001858CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001859CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001860 return new (C, ID) CXXConversionDecl(0, SourceLocation(),
1861 DeclarationNameInfo(), QualType(),
1862 0, false, false, false,
1863 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001864}
1865
1866CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001867CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001868 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001869 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001870 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00001871 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00001872 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001873 assert(NameInfo.getName().getNameKind()
1874 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001875 "Name must refer to a conversion function");
Richard Smithf7981722013-11-22 09:01:48 +00001876 return new (C, RD) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
1877 isInline, isExplicit, isConstexpr,
1878 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001879}
1880
Douglas Gregord3b672c2012-02-16 01:06:16 +00001881bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1882 return isImplicit() && getParent()->isLambda() &&
1883 getConversionType()->isBlockPointerType();
1884}
1885
David Blaikie68e081d2011-12-20 02:48:34 +00001886void LinkageSpecDecl::anchor() { }
1887
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00001888LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00001889 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00001890 SourceLocation ExternLoc,
1891 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00001892 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00001893 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00001894 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00001895}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001896
Richard Smithf7981722013-11-22 09:01:48 +00001897LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
1898 unsigned ID) {
1899 return new (C, ID) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1900 lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001901}
1902
David Blaikie68e081d2011-12-20 02:48:34 +00001903void UsingDirectiveDecl::anchor() { }
1904
Douglas Gregor889ceb72009-02-03 19:21:40 +00001905UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1906 SourceLocation L,
1907 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00001908 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001909 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00001910 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001911 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001912 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1913 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00001914 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1915 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00001916}
1917
Richard Smithf7981722013-11-22 09:01:48 +00001918UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
1919 unsigned ID) {
1920 return new (C, ID) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1921 NestedNameSpecifierLoc(),
1922 SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001923}
1924
Sebastian Redla6602e92009-11-23 15:34:23 +00001925NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1926 if (NamespaceAliasDecl *NA =
1927 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1928 return NA->getNamespace();
1929 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1930}
1931
Douglas Gregore57e7522012-01-07 09:11:48 +00001932NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1933 SourceLocation StartLoc,
1934 SourceLocation IdLoc, IdentifierInfo *Id,
1935 NamespaceDecl *PrevDecl)
1936 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1937 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1938{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001939 setPreviousDecl(PrevDecl);
Douglas Gregore57e7522012-01-07 09:11:48 +00001940
1941 if (PrevDecl)
1942 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1943}
1944
Douglas Gregor72172e92012-01-05 21:55:30 +00001945NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001946 bool Inline, SourceLocation StartLoc,
1947 SourceLocation IdLoc, IdentifierInfo *Id,
1948 NamespaceDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001949 return new (C, DC) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00001950}
1951
1952NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001953 return new (C, ID) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1954 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001955}
1956
Rafael Espindola8ef7f682013-11-26 15:12:20 +00001957NamespaceDecl *NamespaceDecl::getNextRedeclaration() {
1958 return RedeclLink.getNext();
1959}
1960NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
1961 return getPreviousDecl();
1962}
1963NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
1964 return getMostRecentDecl();
1965}
1966
David Blaikie68e081d2011-12-20 02:48:34 +00001967void NamespaceAliasDecl::anchor() { }
1968
Mike Stump11289f42009-09-09 15:08:12 +00001969NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00001970 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001971 SourceLocation AliasLoc,
1972 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00001973 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001974 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00001975 NamedDecl *Namespace) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001976 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1977 Namespace = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00001978 return new (C, DC) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1979 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00001980}
1981
Douglas Gregor72172e92012-01-05 21:55:30 +00001982NamespaceAliasDecl *
1983NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001984 return new (C, ID) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(),
1985 0, NestedNameSpecifierLoc(),
1986 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001987}
1988
David Blaikie68e081d2011-12-20 02:48:34 +00001989void UsingShadowDecl::anchor() { }
1990
Douglas Gregor72172e92012-01-05 21:55:30 +00001991UsingShadowDecl *
1992UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001993 return new (C, ID) UsingShadowDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001994}
1995
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00001996UsingDecl *UsingShadowDecl::getUsingDecl() const {
1997 const UsingShadowDecl *Shadow = this;
1998 while (const UsingShadowDecl *NextShadow =
1999 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
2000 Shadow = NextShadow;
2001 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2002}
2003
David Blaikie68e081d2011-12-20 02:48:34 +00002004void UsingDecl::anchor() { }
2005
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002006void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2007 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2008 "declaration already in set");
2009 assert(S->getUsingDecl() == this);
2010
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002011 if (FirstUsingShadow.getPointer())
2012 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2013 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002014}
2015
2016void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2017 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2018 "declaration not in set");
2019 assert(S->getUsingDecl() == this);
2020
2021 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2022
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002023 if (FirstUsingShadow.getPointer() == S) {
2024 FirstUsingShadow.setPointer(
2025 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002026 S->UsingOrNextShadow = this;
2027 return;
2028 }
2029
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002030 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002031 while (Prev->UsingOrNextShadow != S)
2032 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2033 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2034 S->UsingOrNextShadow = this;
2035}
2036
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002037UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2038 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002039 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002040 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002041 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002042}
2043
Douglas Gregor72172e92012-01-05 21:55:30 +00002044UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002045 return new (C, ID) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
2046 DeclarationNameInfo(), false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002047}
2048
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002049SourceRange UsingDecl::getSourceRange() const {
2050 SourceLocation Begin = isAccessDeclaration()
2051 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2052 return SourceRange(Begin, getNameInfo().getEndLoc());
2053}
2054
David Blaikie68e081d2011-12-20 02:48:34 +00002055void UnresolvedUsingValueDecl::anchor() { }
2056
John McCalle61f2ba2009-11-18 02:36:19 +00002057UnresolvedUsingValueDecl *
2058UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2059 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002060 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002061 const DeclarationNameInfo &NameInfo) {
Richard Smithf7981722013-11-22 09:01:48 +00002062 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
2063 QualifierLoc, NameInfo);
John McCalle61f2ba2009-11-18 02:36:19 +00002064}
2065
Douglas Gregor72172e92012-01-05 21:55:30 +00002066UnresolvedUsingValueDecl *
2067UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002068 return new (C, ID) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
2069 NestedNameSpecifierLoc(),
Douglas Gregor72172e92012-01-05 21:55:30 +00002070 DeclarationNameInfo());
2071}
2072
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002073SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2074 SourceLocation Begin = isAccessDeclaration()
2075 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2076 return SourceRange(Begin, getNameInfo().getEndLoc());
2077}
2078
David Blaikie68e081d2011-12-20 02:48:34 +00002079void UnresolvedUsingTypenameDecl::anchor() { }
2080
John McCalle61f2ba2009-11-18 02:36:19 +00002081UnresolvedUsingTypenameDecl *
2082UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2083 SourceLocation UsingLoc,
2084 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002085 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002086 SourceLocation TargetNameLoc,
2087 DeclarationName TargetName) {
Richard Smithf7981722013-11-22 09:01:48 +00002088 return new (C, DC) UnresolvedUsingTypenameDecl(
2089 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
2090 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002091}
2092
Douglas Gregor72172e92012-01-05 21:55:30 +00002093UnresolvedUsingTypenameDecl *
2094UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002095 return new (C, ID) UnresolvedUsingTypenameDecl(
2096 0, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
2097 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00002098}
2099
David Blaikie68e081d2011-12-20 02:48:34 +00002100void StaticAssertDecl::anchor() { }
2101
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002102StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002103 SourceLocation StaticAssertLoc,
2104 Expr *AssertExpr,
2105 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002106 SourceLocation RParenLoc,
2107 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002108 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2109 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002110}
2111
Richard Smithf7981722013-11-22 09:01:48 +00002112StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002113 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002114 return new (C, ID) StaticAssertDecl(0, SourceLocation(), 0, 0,
2115 SourceLocation(), false);
2116}
2117
2118MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2119 SourceLocation L, DeclarationName N,
2120 QualType T, TypeSourceInfo *TInfo,
2121 SourceLocation StartL,
2122 IdentifierInfo *Getter,
2123 IdentifierInfo *Setter) {
2124 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2125}
2126
2127MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2128 unsigned ID) {
2129 return new (C, ID) MSPropertyDecl(0, SourceLocation(), DeclarationName(),
2130 QualType(), 0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00002131}
2132
Anders Carlsson6750d162009-03-26 23:46:50 +00002133static const char *getAccessName(AccessSpecifier AS) {
2134 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002135 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002136 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002137 case AS_public:
2138 return "public";
2139 case AS_private:
2140 return "private";
2141 case AS_protected:
2142 return "protected";
2143 }
David Blaikief47fa302012-01-17 02:30:50 +00002144 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002145}
2146
2147const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2148 AccessSpecifier AS) {
2149 return DB << getAccessName(AS);
2150}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002151
2152const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2153 AccessSpecifier AS) {
2154 return DB << getAccessName(AS);
2155}