blob: fcd38ac7213029bc71cb66722977626e0f7cfeb3 [file] [log] [blame]
Ted Kremenek4b7c9832008-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//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
15#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000016#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000017#include "clang/AST/CXXInheritance.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/AST/DeclTemplate.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregor76852c22011-11-01 01:16:03 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000021#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000022#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000023#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Decl Allocation/Deallocation Method Implementations
29//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000030
David Blaikie99ba9e32011-12-20 02:48:34 +000031void AccessSpecDecl::anchor() { }
32
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000033AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
34 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(AccessSpecDecl));
35 return new (Mem) AccessSpecDecl(EmptyShell());
36}
37
John McCall86ff3082010-02-04 22:26:26 +000038CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Richard Smith7d04d3a2012-11-30 05:11:39 +000039 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
Eli Friedman97c134e2009-08-15 22:23:00 +000040 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000041 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000042 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000043 HasMutableFields(false), HasOnlyCMembers(true),
Richard Smithd5bc8672012-12-08 02:01:17 +000044 HasInClassInitializer(false), HasUninitializedReferenceMember(false),
Richard Smithbc2a35d2012-12-08 08:32:28 +000045 NeedOverloadResolutionForMoveConstructor(false),
46 NeedOverloadResolutionForMoveAssignment(false),
47 NeedOverloadResolutionForDestructor(false),
48 DefaultedMoveConstructorIsDeleted(false),
49 DefaultedMoveAssignmentIsDeleted(false),
50 DefaultedDestructorIsDeleted(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000051 HasTrivialSpecialMembers(SMF_All),
Richard Smithac713512012-12-08 02:53:02 +000052 DeclaredNonTrivialSpecialMembers(0),
Richard Smith7d04d3a2012-11-30 05:11:39 +000053 HasIrrelevantDestructor(true),
Richard Smith61802452011-12-22 02:22:31 +000054 HasConstexprNonCopyMoveConstructor(false),
55 DefaultedDefaultConstructorIsConstexpr(true),
Richard Smith7d04d3a2012-11-30 05:11:39 +000056 HasConstexprDefaultConstructor(false),
Sean Hunt023df372011-05-09 18:22:59 +000057 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000058 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smithacf796b2012-11-28 06:23:12 +000059 ImplicitCopyConstructorHasConstParam(true),
60 ImplicitCopyAssignmentHasConstParam(true),
61 HasDeclaredCopyConstructorWithConstParam(false),
62 HasDeclaredCopyAssignmentWithConstParam(false),
63 FailedImplicitMoveConstructor(false), FailedImplicitMoveAssignment(false),
64 IsLambda(false), NumBases(0), NumVBases(0), Bases(), VBases(),
65 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000066}
67
Benjamin Krameree3096a2012-07-04 17:03:33 +000068CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
69 return Bases.get(Definition->getASTContext().getExternalSource());
70}
71
72CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
73 return VBases.get(Definition->getASTContext().getExternalSource());
74}
75
John McCall86ff3082010-02-04 22:26:26 +000076CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000077 SourceLocation StartLoc, SourceLocation IdLoc,
78 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
79 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000080 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000081 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000082
Jay Foad4ba2a172011-01-12 09:06:06 +000083CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000084 DeclContext *DC, SourceLocation StartLoc,
85 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000086 CXXRecordDecl* PrevDecl,
87 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000088 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
89 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000090
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000091 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000092 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000093 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000094 return R;
95}
96
Douglas Gregorda8962a2012-02-13 15:44:47 +000097CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Eli Friedman8da8a662012-09-19 01:18:11 +000098 TypeSourceInfo *Info, SourceLocation Loc,
99 bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +0000100 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
101 0, 0);
102 R->IsBeingDefined = true;
Eli Friedman8da8a662012-09-19 01:18:11 +0000103 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info, Dependent);
Douglas Gregorda8962a2012-02-13 15:44:47 +0000104 C.getTypeDeclType(R, /*PrevDecl=*/0);
105 return R;
106}
107
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000108CXXRecordDecl *
109CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
110 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
111 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
112 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000113}
114
Mike Stump1eb44332009-09-09 15:08:12 +0000115void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000116CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000117 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000118 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000119
Douglas Gregor7c789c12010-10-29 22:39:52 +0000120 if (!data().Bases.isOffset() && data().NumBases > 0)
121 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Richard Smithdd677232011-10-18 20:08:55 +0000123 if (NumBases) {
124 // C++ [dcl.init.aggr]p1:
125 // An aggregate is [...] a class with [...] no base classes [...].
126 data().Aggregate = false;
127
128 // C++ [class]p4:
129 // A POD-struct is an aggregate class...
130 data().PlainOldData = false;
131 }
132
Anders Carlsson6f6de732010-03-29 05:13:12 +0000133 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000134 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000135
136 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000137 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000138
John McCall86ff3082010-02-04 22:26:26 +0000139 data().Bases = new(C) CXXBaseSpecifier [NumBases];
140 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000141 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000142 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000143 // Keep track of inherited vbases for this base class.
144 const CXXBaseSpecifier *Base = Bases[i];
145 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000146 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000147 if (BaseType->isDependentType())
148 continue;
149 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000150 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000151
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000152 // A class with a non-empty base class is not empty.
153 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000154 if (!BaseClassDecl->isEmpty()) {
155 if (!data().Empty) {
156 // C++0x [class]p7:
157 // A standard-layout class is a class that:
158 // [...]
159 // -- either has no non-static data members in the most derived
160 // class and at most one base class with non-static data members,
161 // or has no base classes with non-static data members, and
162 // If this is the second non-empty base, then neither of these two
163 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000164 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000165 }
166
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000167 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000168 data().HasNoNonEmptyBases = false;
169 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000170
Douglas Gregor85606eb2010-09-28 20:50:54 +0000171 // C++ [class.virtual]p1:
172 // A class that declares or inherits a virtual function is called a
173 // polymorphic class.
174 if (BaseClassDecl->isPolymorphic())
175 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000176
Chandler Carrutha8225442011-04-30 09:17:45 +0000177 // C++0x [class]p7:
178 // A standard-layout class is a class that: [...]
179 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000180 if (!BaseClassDecl->isStandardLayout())
181 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000182
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000183 // Record if this base is the first non-literal field or base.
184 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
185 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000186
Anders Carlsson6f6de732010-03-29 05:13:12 +0000187 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000188 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000189 BaseClassDecl->vbases_begin(),
190 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000191 // Add this base if it's not already in the list.
Richard Smithacf796b2012-11-28 06:23:12 +0000192 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000193 VBases.push_back(VBase);
Richard Smithacf796b2012-11-28 06:23:12 +0000194
195 // C++11 [class.copy]p8:
196 // The implicitly-declared copy constructor for a class X will have
197 // the form 'X::X(const X&)' if each [...] virtual base class B of X
198 // has a copy constructor whose first parameter is of type
199 // 'const B&' or 'const volatile B&' [...]
200 if (CXXRecordDecl *VBaseDecl = VBase->getType()->getAsCXXRecordDecl())
201 if (!VBaseDecl->hasCopyConstructorWithConstParam())
202 data().ImplicitCopyConstructorHasConstParam = false;
203 }
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000204 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000205
206 if (Base->isVirtual()) {
207 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000208 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Richard Smithacf796b2012-11-28 06:23:12 +0000209 VBases.push_back(Base);
210
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000211 // C++0x [meta.unary.prop] is_empty:
212 // T is a class type, but not a union type, with ... no virtual base
213 // classes
214 data().Empty = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000215
Richard Smith7d04d3a2012-11-30 05:11:39 +0000216 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
217 // A [default constructor, copy/move constructor, or copy/move assignment
218 // operator for a class X] is trivial [...] if:
219 // -- class X has [...] no virtual base classes
220 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carrutha8225442011-04-30 09:17:45 +0000221
222 // C++0x [class]p7:
223 // A standard-layout class is a class that: [...]
224 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000225 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000226
227 // C++11 [dcl.constexpr]p4:
228 // In the definition of a constexpr constructor [...]
229 // -- the class shall not have any virtual base classes
230 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000231 } else {
232 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000233 // A default constructor is trivial [...] if:
234 // -- all the direct base classes of its class have trivial default
235 // constructors.
236 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000237 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
238
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000239 // C++0x [class.copy]p13:
240 // A copy/move constructor for class X is trivial if [...]
241 // [...]
242 // -- the constructor selected to copy/move each direct base class
243 // subobject is trivial, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000244 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000245 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000246 // If the base class doesn't have a simple move constructor, we'll eagerly
247 // declare it and perform overload resolution to determine which function
248 // it actually calls. If it does have a simple move constructor, this
249 // check is correct.
Richard Smith426391c2012-11-16 00:53:38 +0000250 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000251 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000252
253 // C++0x [class.copy]p27:
254 // A copy/move assignment operator for class X is trivial if [...]
255 // [...]
256 // -- the assignment operator selected to copy/move each direct base
257 // class subobject is trivial, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000258 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000259 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000260 // If the base class doesn't have a simple move assignment, we'll eagerly
261 // declare it and perform overload resolution to determine which function
262 // it actually calls. If it does have a simple move assignment, this
263 // check is correct.
Richard Smith426391c2012-11-16 00:53:38 +0000264 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000265 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smith61802452011-12-22 02:22:31 +0000266
267 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000268 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000269 // requirements of a constexpr constructor, the implicitly-defined
270 // default constructor is constexpr.
271 if (!BaseClassDecl->hasConstexprDefaultConstructor())
272 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000273 }
Richard Smithac713512012-12-08 02:53:02 +0000274
Douglas Gregor85606eb2010-09-28 20:50:54 +0000275 // C++ [class.ctor]p3:
276 // A destructor is trivial if all the direct base classes of its class
277 // have trivial destructors.
278 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000279 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000280
281 if (!BaseClassDecl->hasIrrelevantDestructor())
282 data().HasIrrelevantDestructor = false;
283
Richard Smithacf796b2012-11-28 06:23:12 +0000284 // C++11 [class.copy]p18:
285 // The implicitly-declared copy assignment oeprator for a class X will
286 // have the form 'X& X::operator=(const X&)' if each direct base class B
287 // of X has a copy assignment operator whose parameter is of type 'const
288 // B&', 'const volatile B&', or 'B' [...]
289 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
290 data().ImplicitCopyAssignmentHasConstParam = false;
291
292 // C++11 [class.copy]p8:
293 // The implicitly-declared copy constructor for a class X will have
294 // the form 'X::X(const X&)' if each direct [...] base class B of X
295 // has a copy constructor whose first parameter is of type
296 // 'const B&' or 'const volatile B&' [...]
297 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
298 data().ImplicitCopyConstructorHasConstParam = false;
299
John McCallf85e1932011-06-15 23:02:42 +0000300 // A class has an Objective-C object member if... or any of its bases
301 // has an Objective-C object member.
302 if (BaseClassDecl->hasObjectMember())
303 setHasObjectMember(true);
304
Douglas Gregor2bb11012011-05-13 01:05:07 +0000305 // Keep track of the presence of mutable fields.
306 if (BaseClassDecl->hasMutableFields())
307 data().HasMutableFields = true;
Richard Smithd5bc8672012-12-08 02:01:17 +0000308
309 if (BaseClassDecl->hasUninitializedReferenceMember())
310 data().HasUninitializedReferenceMember = true;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000311
312 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000313 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000314
315 if (VBases.empty())
316 return;
317
318 // Create base specifier for any direct or indirect virtual bases.
319 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
320 data().NumVBases = VBases.size();
Richard Smithbc2a35d2012-12-08 08:32:28 +0000321 for (int I = 0, E = VBases.size(); I != E; ++I) {
322 QualType Type = VBases[I]->getType();
323 if (!Type->isDependentType())
324 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000325 data().getVBases()[I] = *VBases[I];
Richard Smithbc2a35d2012-12-08 08:32:28 +0000326 }
327}
328
329void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
330 // C++11 [class.copy]p11:
331 // A defaulted copy/move constructor for a class X is defined as
332 // deleted if X has:
333 // -- a direct or virtual base class B that cannot be copied/moved [...]
334 // -- a non-static data member of class type M (or array thereof)
335 // that cannot be copied or moved [...]
336 if (!Subobj->hasSimpleMoveConstructor())
337 data().NeedOverloadResolutionForMoveConstructor = true;
338
339 // C++11 [class.copy]p23:
340 // A defaulted copy/move assignment operator for a class X is defined as
341 // deleted if X has:
342 // -- a direct or virtual base class B that cannot be copied/moved [...]
343 // -- a non-static data member of class type M (or array thereof)
344 // that cannot be copied or moved [...]
345 if (!Subobj->hasSimpleMoveAssignment())
346 data().NeedOverloadResolutionForMoveAssignment = true;
347
348 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
349 // A defaulted [ctor or dtor] for a class X is defined as
350 // deleted if X has:
351 // -- any direct or virtual base class [...] has a type with a destructor
352 // that is deleted or inaccessible from the defaulted [ctor or dtor].
353 // -- any non-static data member has a type with a destructor
354 // that is deleted or inaccessible from the defaulted [ctor or dtor].
355 if (!Subobj->hasSimpleDestructor()) {
356 data().NeedOverloadResolutionForMoveConstructor = true;
357 data().NeedOverloadResolutionForDestructor = true;
358 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000359}
360
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000361/// Callback function for CXXRecordDecl::forallBases that acknowledges
362/// that it saw a base class.
363static bool SawBase(const CXXRecordDecl *, void *) {
364 return true;
365}
366
367bool CXXRecordDecl::hasAnyDependentBases() const {
368 if (!isDependentContext())
369 return false;
370
371 return !forallBases(SawBase, 0);
372}
373
Chandler Carruthb7e95892011-04-23 10:47:28 +0000374bool CXXRecordDecl::isTriviallyCopyable() const {
375 // C++0x [class]p5:
376 // A trivially copyable class is a class that:
377 // -- has no non-trivial copy constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000378 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000379 // -- has no non-trivial move constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000380 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000381 // -- has no non-trivial copy assignment operators,
Richard Smith426391c2012-11-16 00:53:38 +0000382 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000383 // -- has no non-trivial move assignment operators, and
Richard Smith426391c2012-11-16 00:53:38 +0000384 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000385 // -- has a trivial destructor.
386 if (!hasTrivialDestructor()) return false;
387
388 return true;
389}
390
Douglas Gregor21386642010-09-28 21:55:22 +0000391void CXXRecordDecl::markedVirtualFunctionPure() {
392 // C++ [class.abstract]p2:
393 // A class is abstract if it has at least one pure virtual function.
394 data().Abstract = true;
395}
396
397void CXXRecordDecl::addedMember(Decl *D) {
Joao Matos17d35c32012-08-31 22:18:20 +0000398 if (!D->isImplicit() &&
399 !isa<FieldDecl>(D) &&
400 !isa<IndirectFieldDecl>(D) &&
401 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
402 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
403 data().HasOnlyCMembers = false;
404
405 // Ignore friends and invalid declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000406 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000407 return;
408
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000409 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
410 if (FunTmpl)
411 D = FunTmpl->getTemplatedDecl();
412
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000413 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
414 if (Method->isVirtual()) {
415 // C++ [dcl.init.aggr]p1:
416 // An aggregate is an array or a class with [...] no virtual functions.
417 data().Aggregate = false;
418
419 // C++ [class]p4:
420 // A POD-struct is an aggregate class...
421 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000422
423 // Virtual functions make the class non-empty.
424 // FIXME: Standard ref?
425 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000426
427 // C++ [class.virtual]p1:
428 // A class that declares or inherits a virtual function is called a
429 // polymorphic class.
430 data().Polymorphic = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000431
Richard Smith7d04d3a2012-11-30 05:11:39 +0000432 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
433 // A [default constructor, copy/move constructor, or copy/move
434 // assignment operator for a class X] is trivial [...] if:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000435 // -- class X has no virtual functions [...]
Richard Smith7d04d3a2012-11-30 05:11:39 +0000436 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000437
Chandler Carrutha8225442011-04-30 09:17:45 +0000438 // C++0x [class]p7:
439 // A standard-layout class is a class that: [...]
440 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000441 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000442 }
443 }
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000444
Richard Smithacf796b2012-11-28 06:23:12 +0000445 // Notify the listener if an implicit member was added after the definition
446 // was completed.
447 if (!isBeingDefined() && D->isImplicit())
448 if (ASTMutationListener *L = getASTMutationListener())
449 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000450
Richard Smith7d04d3a2012-11-30 05:11:39 +0000451 // The kind of special member this declaration is, if any.
452 unsigned SMKind = 0;
453
Richard Smithacf796b2012-11-28 06:23:12 +0000454 // Handle constructors.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000455 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smithacf796b2012-11-28 06:23:12 +0000456 if (!Constructor->isImplicit()) {
457 // Note that we have a user-declared constructor.
458 data().UserDeclaredConstructor = true;
459
460 // C++ [class]p4:
461 // A POD-struct is an aggregate class [...]
462 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
463 // type is technically an aggregate in C++0x since it wouldn't be in 03.
464 data().PlainOldData = false;
465 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000466
Richard Smith017ab772011-09-05 02:13:09 +0000467 // Technically, "user-provided" is only defined for special member
468 // functions, but the intent of the standard is clearly that it should apply
469 // to all functions.
470 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000471
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000472 if (Constructor->isDefaultConstructor()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000473 SMKind |= SMF_DefaultConstructor;
474
475 if (UserProvided)
Sean Huntcdee3fe2011-05-11 22:34:38 +0000476 data().UserProvidedDefaultConstructor = true;
Richard Smithacf796b2012-11-28 06:23:12 +0000477 if (Constructor->isConstexpr())
Richard Smith61802452011-12-22 02:22:31 +0000478 data().HasConstexprDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000479 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000480
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000481 if (!FunTmpl) {
Richard Smithacf796b2012-11-28 06:23:12 +0000482 unsigned Quals;
483 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000484 SMKind |= SMF_CopyConstructor;
Richard Smithacf796b2012-11-28 06:23:12 +0000485
486 if (Quals & Qualifiers::Const)
487 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith7d04d3a2012-11-30 05:11:39 +0000488 } else if (Constructor->isMoveConstructor())
489 SMKind |= SMF_MoveConstructor;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000490 }
Richard Smithacf796b2012-11-28 06:23:12 +0000491
492 // Record if we see any constexpr constructors which are neither copy
493 // nor move constructors.
494 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith6b8bc072011-08-10 18:11:37 +0000495 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000496
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000497 // C++ [dcl.init.aggr]p1:
498 // An aggregate is an array or a class with no user-declared
499 // constructors [...].
500 // C++0x [dcl.init.aggr]p1:
501 // An aggregate is an array or a class with no user-provided
502 // constructors [...].
Richard Smithacf796b2012-11-28 06:23:12 +0000503 if (getASTContext().getLangOpts().CPlusPlus0x
504 ? UserProvided : !Constructor->isImplicit())
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000505 data().Aggregate = false;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000506 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000507
Richard Smithacf796b2012-11-28 06:23:12 +0000508 // Handle destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000509 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000510 SMKind |= SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000511
Richard Smith7d04d3a2012-11-30 05:11:39 +0000512 if (!DD->isImplicit())
Richard Smithacf796b2012-11-28 06:23:12 +0000513 data().HasIrrelevantDestructor = false;
514
Richard Smithacf796b2012-11-28 06:23:12 +0000515 // C++11 [class.dtor]p5:
Richard Smith7d04d3a2012-11-30 05:11:39 +0000516 // A destructor is trivial if [...] the destructor is not virtual.
517 if (DD->isVirtual())
518 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000519 }
Richard Smithacf796b2012-11-28 06:23:12 +0000520
521 // Handle member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000522 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000523 if (Method->isCopyAssignmentOperator()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000524 SMKind |= SMF_CopyAssignment;
Richard Smithacf796b2012-11-28 06:23:12 +0000525
526 const ReferenceType *ParamTy =
527 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
528 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
529 data().HasDeclaredCopyAssignmentWithConstParam = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000530 }
Sean Huntffe37fd2011-05-25 20:50:04 +0000531
Richard Smith7d04d3a2012-11-30 05:11:39 +0000532 if (Method->isMoveAssignmentOperator())
533 SMKind |= SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000534
Douglas Gregore80622f2010-09-29 04:25:11 +0000535 // Keep the list of conversion functions up-to-date.
536 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregore80622f2010-09-29 04:25:11 +0000537 // FIXME: We intentionally don't use the decl's access here because it
538 // hasn't been set yet. That's really just a misdesign in Sema.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000539 if (Conversion->getPrimaryTemplate()) {
540 // We don't record specializations.
541 } else if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000542 if (FunTmpl->getPreviousDecl())
543 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000544 FunTmpl);
545 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000546 data().Conversions.addDecl(getASTContext(), FunTmpl);
Douglas Gregore80622f2010-09-29 04:25:11 +0000547 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000548 if (Conversion->getPreviousDecl())
549 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000550 Conversion);
551 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000552 data().Conversions.addDecl(getASTContext(), Conversion);
Douglas Gregore80622f2010-09-29 04:25:11 +0000553 }
554 }
Richard Smithacf796b2012-11-28 06:23:12 +0000555
Richard Smith7d04d3a2012-11-30 05:11:39 +0000556 if (SMKind) {
Richard Smithac713512012-12-08 02:53:02 +0000557 // If this is the first declaration of a special member, we no longer have
558 // an implicit trivial special member.
559 data().HasTrivialSpecialMembers &=
560 data().DeclaredSpecialMembers | ~SMKind;
561
562 if (!Method->isImplicit() && !Method->isUserProvided()) {
563 // This method is user-declared but not user-provided. We can't work out
564 // whether it's trivial yet (not until we get to the end of the class).
565 // We'll handle this method in finishedDefaultedOrDeletedMember.
566 } else if (Method->isTrivial())
567 data().HasTrivialSpecialMembers |= SMKind;
568 else
569 data().DeclaredNonTrivialSpecialMembers |= SMKind;
570
Richard Smith7d04d3a2012-11-30 05:11:39 +0000571 // Note when we have declared a declared special member, and suppress the
572 // implicit declaration of this special member.
573 data().DeclaredSpecialMembers |= SMKind;
574
575 if (!Method->isImplicit()) {
576 data().UserDeclaredSpecialMembers |= SMKind;
577
578 // C++03 [class]p4:
579 // A POD-struct is an aggregate class that has [...] no user-defined
580 // copy assignment operator and no user-defined destructor.
581 //
582 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
583 // aggregates could not have any constructors, clear it even for an
584 // explicitly defaulted or deleted constructor.
585 // type is technically an aggregate in C++0x since it wouldn't be in 03.
586 //
587 // Also, a user-declared move assignment operator makes a class non-POD.
588 // This is an extension in C++03.
589 data().PlainOldData = false;
590 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000591 }
592
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000593 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000594 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000595
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000596 // Handle non-static data members.
597 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000598 // C++ [class.bit]p2:
599 // A declaration for a bit-field that omits the identifier declares an
600 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
601 // initialized.
602 if (Field->isUnnamedBitfield())
603 return;
604
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000605 // C++ [dcl.init.aggr]p1:
606 // An aggregate is an array or a class (clause 9) with [...] no
607 // private or protected non-static data members (clause 11).
608 //
609 // A POD must be an aggregate.
610 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
611 data().Aggregate = false;
612 data().PlainOldData = false;
613 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000614
615 // C++0x [class]p7:
616 // A standard-layout class is a class that:
617 // [...]
618 // -- has the same access control for all non-static data members,
619 switch (D->getAccess()) {
620 case AS_private: data().HasPrivateFields = true; break;
621 case AS_protected: data().HasProtectedFields = true; break;
622 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000623 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000624 };
625 if ((data().HasPrivateFields + data().HasProtectedFields +
626 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000627 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000628
Douglas Gregor2bb11012011-05-13 01:05:07 +0000629 // Keep track of the presence of mutable fields.
630 if (Field->isMutable())
631 data().HasMutableFields = true;
632
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000633 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000634 // A POD struct is a class that is both a trivial class and a
635 // standard-layout class, and has no non-static data members of type
636 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000637 //
638 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
639 // that does not explicitly have no lifetime makes the class a non-POD.
640 // However, we delay setting PlainOldData to false in this case so that
641 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000642 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000643 // In this case, the class will become a non-POD class when we complete
644 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000645 ASTContext &Context = getASTContext();
646 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000647 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000648 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000649 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
650 setHasObjectMember(true);
651 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000652 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000653
Chandler Carrutha8225442011-04-30 09:17:45 +0000654 if (T->isReferenceType()) {
Richard Smithd5bc8672012-12-08 02:01:17 +0000655 if (!Field->hasInClassInitializer())
656 data().HasUninitializedReferenceMember = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000657
Chandler Carrutha8225442011-04-30 09:17:45 +0000658 // C++0x [class]p7:
659 // A standard-layout class is a class that:
660 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000661 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000662 }
663
Richard Smith86c3ae42012-02-13 03:54:03 +0000664 // Record if this field is the first non-literal or volatile field or base.
665 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000666 data().HasNonLiteralTypeFieldsOrBases = true;
667
Richard Smith7a614d82011-06-11 17:19:42 +0000668 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000669 data().HasInClassInitializer = true;
670
671 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000672 // A default constructor is trivial if [...] no non-static data member
673 // of its class has a brace-or-equal-initializer.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000674 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith7a614d82011-06-11 17:19:42 +0000675
Richard Smithd079abf2012-05-07 01:07:30 +0000676 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000677 // An aggregate is a [...] class with [...] no
678 // brace-or-equal-initializers for non-static data members.
679 data().Aggregate = false;
680
Richard Smithd079abf2012-05-07 01:07:30 +0000681 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000682 // A POD struct is [...] a trivial class.
683 data().PlainOldData = false;
684 }
685
Richard Smithbc2a35d2012-12-08 08:32:28 +0000686 // C++11 [class.copy]p23:
687 // A defaulted copy/move assignment operator for a class X is defined
688 // as deleted if X has:
689 // -- a non-static data member of reference type
690 if (T->isReferenceType())
691 data().DefaultedMoveAssignmentIsDeleted = true;
692
Douglas Gregor85606eb2010-09-28 20:50:54 +0000693 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
694 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
695 if (FieldRec->getDefinition()) {
Richard Smithbc2a35d2012-12-08 08:32:28 +0000696 addedClassSubobject(FieldRec);
697
698 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
699 // A defaulted [special member] for a class X is defined as
700 // deleted if:
701 // -- X is a union-like class that has a variant member with a
702 // non-trivial [corresponding special member]
703 if (isUnion()) {
704 if (FieldRec->hasNonTrivialMoveConstructor())
705 data().DefaultedMoveConstructorIsDeleted = true;
706 if (FieldRec->hasNonTrivialMoveAssignment())
707 data().DefaultedMoveAssignmentIsDeleted = true;
708 if (FieldRec->hasNonTrivialDestructor())
709 data().DefaultedDestructorIsDeleted = true;
710 }
711
Sean Hunt023df372011-05-09 18:22:59 +0000712 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000713 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000714 // -- for all the non-static data members of its class that are of
715 // class type (or array thereof), each such class has a trivial
716 // default constructor.
717 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000718 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000719
720 // C++0x [class.copy]p13:
721 // A copy/move constructor for class X is trivial if [...]
722 // [...]
723 // -- for each non-static data member of X that is of class type (or
724 // an array thereof), the constructor selected to copy/move that
725 // member is trivial;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000726 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000727 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000728 // If the field doesn't have a simple move constructor, we'll eagerly
729 // declare the move constructor for this class and we'll decide whether
730 // it's trivial then.
Richard Smith426391c2012-11-16 00:53:38 +0000731 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000732 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000733
734 // C++0x [class.copy]p27:
735 // A copy/move assignment operator for class X is trivial if [...]
736 // [...]
737 // -- for each non-static data member of X that is of class type (or
738 // an array thereof), the assignment operator selected to
739 // copy/move that member is trivial;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000740 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000741 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000742 // If the field doesn't have a simple move assignment, we'll eagerly
743 // declare the move assignment for this class and we'll decide whether
744 // it's trivial then.
Richard Smith426391c2012-11-16 00:53:38 +0000745 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000746 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000747
Douglas Gregor85606eb2010-09-28 20:50:54 +0000748 if (!FieldRec->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000749 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000750 if (!FieldRec->hasIrrelevantDestructor())
751 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000752 if (FieldRec->hasObjectMember())
753 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000754
755 // C++0x [class]p7:
756 // A standard-layout class is a class that:
757 // -- has no non-static data members of type non-standard-layout
758 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000759 if (!FieldRec->isStandardLayout())
760 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000761
762 // C++0x [class]p7:
763 // A standard-layout class is a class that:
764 // [...]
765 // -- has no base classes of the same type as the first non-static
766 // data member.
767 // We don't want to expend bits in the state of the record decl
768 // tracking whether this is the first non-static data member so we
769 // cheat a bit and use some of the existing state: the empty bit.
770 // Virtual bases and virtual methods make a class non-empty, but they
771 // also make it non-standard-layout so we needn't check here.
772 // A non-empty base class may leave the class standard-layout, but not
773 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000774 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000775 // data member must come through here with Empty still true, and Empty
776 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000777 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000778 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
779 BE = bases_end();
780 BI != BE; ++BI) {
781 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000782 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000783 break;
784 }
785 }
786 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000787
788 // Keep track of the presence of mutable fields.
789 if (FieldRec->hasMutableFields())
790 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000791
792 // C++11 [class.copy]p13:
793 // If the implicitly-defined constructor would satisfy the
794 // requirements of a constexpr constructor, the implicitly-defined
795 // constructor is constexpr.
796 // C++11 [dcl.constexpr]p4:
797 // -- every constructor involved in initializing non-static data
798 // members [...] shall be a constexpr constructor
799 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000800 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000801 // The standard requires any in-class initializer to be a constant
802 // expression. We consider this to be a defect.
803 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithacf796b2012-11-28 06:23:12 +0000804
805 // C++11 [class.copy]p8:
806 // The implicitly-declared copy constructor for a class X will have
807 // the form 'X::X(const X&)' if [...] for all the non-static data
808 // members of X that are of a class type M (or array thereof), each
809 // such class type has a copy constructor whose first parameter is
810 // of type 'const M&' or 'const volatile M&'.
811 if (!FieldRec->hasCopyConstructorWithConstParam())
812 data().ImplicitCopyConstructorHasConstParam = false;
813
814 // C++11 [class.copy]p18:
815 // The implicitly-declared copy assignment oeprator for a class X will
816 // have the form 'X& X::operator=(const X&)' if [...] for all the
817 // non-static data members of X that are of a class type M (or array
818 // thereof), each such class type has a copy assignment operator whose
819 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
820 if (!FieldRec->hasCopyAssignmentWithConstParam())
821 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smithd5bc8672012-12-08 02:01:17 +0000822
823 if (FieldRec->hasUninitializedReferenceMember() &&
824 !Field->hasInClassInitializer())
825 data().HasUninitializedReferenceMember = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000826 }
Richard Smith61802452011-12-22 02:22:31 +0000827 } else {
828 // Base element type of field is a non-class type.
Richard Smithd3861ce2012-06-10 07:07:24 +0000829 if (!T->isLiteralType() ||
830 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000831 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000832
833 // C++11 [class.copy]p23:
834 // A defaulted copy/move assignment operator for a class X is defined
835 // as deleted if X has:
836 // -- a non-static data member of const non-class type (or array
837 // thereof)
838 if (T.isConstQualified())
839 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000840 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000841
842 // C++0x [class]p7:
843 // A standard-layout class is a class that:
844 // [...]
845 // -- either has no non-static data members in the most derived
846 // class and at most one base class with non-static data members,
847 // or has no base classes with non-static data members, and
848 // At this point we know that we have a non-static data member, so the last
849 // clause holds.
850 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000851 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000852
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000853 // If this is not a zero-length bit-field, then the class is not empty.
854 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000855 if (!Field->isBitField() ||
856 (!Field->getBitWidth()->isTypeDependent() &&
857 !Field->getBitWidth()->isValueDependent() &&
858 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000859 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000860 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000861 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000862
863 // Handle using declarations of conversion functions.
864 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
865 if (Shadow->getDeclName().getNameKind()
866 == DeclarationName::CXXConversionFunctionName)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000867 data().Conversions.addDecl(getASTContext(), Shadow, Shadow->getAccess());
Joao Matos17d35c32012-08-31 22:18:20 +0000868}
869
Richard Smithac713512012-12-08 02:53:02 +0000870void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
871 assert(!D->isImplicit() && !D->isUserProvided());
872
873 // The kind of special member this declaration is, if any.
874 unsigned SMKind = 0;
875
876 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
877 if (Constructor->isDefaultConstructor()) {
878 SMKind |= SMF_DefaultConstructor;
879 if (Constructor->isConstexpr())
880 data().HasConstexprDefaultConstructor = true;
881 }
882 if (Constructor->isCopyConstructor())
883 SMKind |= SMF_CopyConstructor;
884 else if (Constructor->isMoveConstructor())
885 SMKind |= SMF_MoveConstructor;
886 else if (Constructor->isConstexpr())
887 // We may now know that the constructor is constexpr.
888 data().HasConstexprNonCopyMoveConstructor = true;
889 } else if (isa<CXXDestructorDecl>(D))
890 SMKind |= SMF_Destructor;
891 else if (D->isCopyAssignmentOperator())
892 SMKind |= SMF_CopyAssignment;
893 else if (D->isMoveAssignmentOperator())
894 SMKind |= SMF_MoveAssignment;
895
896 // Update which trivial / non-trivial special members we have.
897 // addedMember will have skipped this step for this member.
898 if (D->isTrivial())
899 data().HasTrivialSpecialMembers |= SMKind;
900 else
901 data().DeclaredNonTrivialSpecialMembers |= SMKind;
902}
903
Joao Matos17d35c32012-08-31 22:18:20 +0000904bool CXXRecordDecl::isCLike() const {
905 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
906 !TemplateOrInstantiation.isNull())
907 return false;
908 if (!hasDefinition())
909 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000910
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000911 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000912}
913
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000914void CXXRecordDecl::getCaptureFields(
915 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000916 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000917 Captures.clear();
918 ThisCapture = 0;
919
Douglas Gregorda8962a2012-02-13 15:44:47 +0000920 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000921 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000922 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000923 C != CEnd; ++C, ++Field) {
924 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +0000925 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000926 continue;
927 }
928
David Blaikie581deb32012-06-06 20:45:41 +0000929 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000930 }
931}
932
933
John McCallb05b5f32010-03-15 09:07:48 +0000934static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
935 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000936 if (isa<UsingShadowDecl>(Conv))
937 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000938 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
939 T = ConvTemp->getTemplatedDecl()->getResultType();
940 else
941 T = cast<CXXConversionDecl>(Conv)->getConversionType();
942 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000943}
944
John McCallb05b5f32010-03-15 09:07:48 +0000945/// Collect the visible conversions of a base class.
946///
James Dennetta1253502012-06-15 22:28:09 +0000947/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +0000948/// \param InVirtual whether this base class is a virtual base (or a base
949/// of a virtual base)
950/// \param Access the access along the inheritance path to this base
951/// \param ParentHiddenTypes the conversions provided by the inheritors
952/// of this base
953/// \param Output the set to which to add conversions from non-virtual bases
954/// \param VOutput the set to which to add conversions from virtual bases
955/// \param HiddenVBaseCs the set of conversions which were hidden in a
956/// virtual base along some inheritance path
957static void CollectVisibleConversions(ASTContext &Context,
958 CXXRecordDecl *Record,
959 bool InVirtual,
960 AccessSpecifier Access,
961 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000962 ASTUnresolvedSet &Output,
John McCallb05b5f32010-03-15 09:07:48 +0000963 UnresolvedSetImpl &VOutput,
964 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
965 // The set of types which have conversions in this class or its
966 // subclasses. As an optimization, we don't copy the derived set
967 // unless it might change.
968 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
969 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
970
971 // Collect the direct conversions and figure out which conversions
972 // will be hidden in the subclasses.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000973 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
974 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
975 if (ConvI != ConvE) {
John McCallb05b5f32010-03-15 09:07:48 +0000976 HiddenTypesBuffer = ParentHiddenTypes;
977 HiddenTypes = &HiddenTypesBuffer;
978
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000979 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +0000980 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
981 bool Hidden = ParentHiddenTypes.count(ConvType);
982 if (!Hidden)
983 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +0000984
985 // If this conversion is hidden and we're in a virtual base,
986 // remember that it's hidden along some inheritance path.
987 if (Hidden && InVirtual)
988 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
989
990 // If this conversion isn't hidden, add it to the appropriate output.
991 else if (!Hidden) {
992 AccessSpecifier IAccess
993 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
994
995 if (InVirtual)
996 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000997 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000998 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000999 }
1000 }
1001 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001002
John McCallb05b5f32010-03-15 09:07:48 +00001003 // Collect information recursively from any base classes.
1004 for (CXXRecordDecl::base_class_iterator
1005 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1006 const RecordType *RT = I->getType()->getAs<RecordType>();
1007 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001008
John McCallb05b5f32010-03-15 09:07:48 +00001009 AccessSpecifier BaseAccess
1010 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1011 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001012
John McCallb05b5f32010-03-15 09:07:48 +00001013 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1014 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1015 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001016 }
John McCallb05b5f32010-03-15 09:07:48 +00001017}
Sebastian Redl9994a342009-10-25 17:03:50 +00001018
John McCallb05b5f32010-03-15 09:07:48 +00001019/// Collect the visible conversions of a class.
1020///
1021/// This would be extremely straightforward if it weren't for virtual
1022/// bases. It might be worth special-casing that, really.
1023static void CollectVisibleConversions(ASTContext &Context,
1024 CXXRecordDecl *Record,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001025 ASTUnresolvedSet &Output) {
John McCallb05b5f32010-03-15 09:07:48 +00001026 // The collection of all conversions in virtual bases that we've
1027 // found. These will be added to the output as long as they don't
1028 // appear in the hidden-conversions set.
1029 UnresolvedSet<8> VBaseCs;
1030
1031 // The set of conversions in virtual bases that we've determined to
1032 // be hidden.
1033 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1034
1035 // The set of types hidden by classes derived from this one.
1036 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1037
1038 // Go ahead and collect the direct conversions and add them to the
1039 // hidden-types set.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001040 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1041 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001042 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001043 for (; ConvI != ConvE; ++ConvI)
1044 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCallb05b5f32010-03-15 09:07:48 +00001045
1046 // Recursively collect conversions from base classes.
1047 for (CXXRecordDecl::base_class_iterator
1048 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1049 const RecordType *RT = I->getType()->getAs<RecordType>();
1050 if (!RT) continue;
1051
1052 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1053 I->isVirtual(), I->getAccessSpecifier(),
1054 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1055 }
1056
1057 // Add any unhidden conversions provided by virtual bases.
1058 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1059 I != E; ++I) {
1060 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001061 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001062 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001063}
1064
1065/// getVisibleConversionFunctions - get all conversion functions visible
1066/// in current class; including conversion function templates.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001067std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1068CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001069 // If root class, all conversions are visible.
1070 if (bases_begin() == bases_end())
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001071 return std::make_pair(data().Conversions.begin(), data().Conversions.end());
Fariborz Jahanian62509212009-09-12 18:26:03 +00001072 // If visible conversion list is already evaluated, return it.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001073 if (!data().ComputedVisibleConversions) {
1074 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
1075 data().ComputedVisibleConversions = true;
1076 }
1077 return std::make_pair(data().VisibleConversions.begin(),
1078 data().VisibleConversions.end());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001079}
1080
John McCall32daa422010-03-31 01:36:47 +00001081void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1082 // This operation is O(N) but extremely rare. Sema only uses it to
1083 // remove UsingShadowDecls in a class that were followed by a direct
1084 // declaration, e.g.:
1085 // class A : B {
1086 // using B::operator int;
1087 // operator int();
1088 // };
1089 // This is uncommon by itself and even more uncommon in conjunction
1090 // with sufficiently large numbers of directly-declared conversions
1091 // that asymptotic behavior matters.
1092
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001093 ASTUnresolvedSet &Convs = data().Conversions;
John McCall32daa422010-03-31 01:36:47 +00001094 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1095 if (Convs[I].getDecl() == ConvDecl) {
1096 Convs.erase(I);
1097 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1098 && "conversion was found multiple times in unresolved set");
1099 return;
1100 }
1101 }
1102
1103 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001104}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001105
Douglas Gregorf6b11852009-10-08 15:14:33 +00001106CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001107 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001108 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1109
1110 return 0;
1111}
1112
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001113MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1114 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1115}
1116
Douglas Gregorf6b11852009-10-08 15:14:33 +00001117void
1118CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1119 TemplateSpecializationKind TSK) {
1120 assert(TemplateOrInstantiation.isNull() &&
1121 "Previous template or instantiation?");
1122 assert(!isa<ClassTemplateSpecializationDecl>(this));
1123 TemplateOrInstantiation
1124 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1125}
1126
Anders Carlssonb13e3572009-12-07 06:33:48 +00001127TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1128 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001129 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1130 return Spec->getSpecializationKind();
1131
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001132 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001133 return MSInfo->getTemplateSpecializationKind();
1134
1135 return TSK_Undeclared;
1136}
1137
1138void
1139CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1140 if (ClassTemplateSpecializationDecl *Spec
1141 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1142 Spec->setSpecializationKind(TSK);
1143 return;
1144 }
1145
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001146 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001147 MSInfo->setTemplateSpecializationKind(TSK);
1148 return;
1149 }
1150
David Blaikieb219cfc2011-09-23 05:06:16 +00001151 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001152}
1153
Douglas Gregor1d110e02010-07-01 14:13:13 +00001154CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1155 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001156 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001157
1158 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001159 = Context.DeclarationNames.getCXXDestructorName(
1160 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001161
David Blaikie3bc93e32012-12-19 00:45:41 +00001162 DeclContext::lookup_const_result R = lookup(Name);
1163 if (R.empty())
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001164 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001165
David Blaikie3bc93e32012-12-19 00:45:41 +00001166 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson7267c162009-05-29 21:03:38 +00001167 return Dtor;
1168}
1169
Douglas Gregorda2142f2011-02-19 18:51:44 +00001170void CXXRecordDecl::completeDefinition() {
1171 completeDefinition(0);
1172}
1173
1174void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1175 RecordDecl::completeDefinition();
1176
David Blaikie4e4d0842012-03-11 07:00:24 +00001177 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001178 // Objective-C Automatic Reference Counting:
1179 // If a class has a non-static data member of Objective-C pointer
1180 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001181 // default constructor (if any), copy constructor, move constructor,
1182 // copy assignment operator, move assignment operator, and destructor are
1183 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001184 struct DefinitionData &Data = data();
1185 Data.PlainOldData = false;
Richard Smith7d04d3a2012-11-30 05:11:39 +00001186 Data.HasTrivialSpecialMembers = 0;
Richard Smithdfefb842012-02-25 07:33:38 +00001187 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001188 }
1189
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001190 // If the class may be abstract (but hasn't been marked as such), check for
1191 // any pure final overriders.
1192 if (mayBeAbstract()) {
1193 CXXFinalOverriderMap MyFinalOverriders;
1194 if (!FinalOverriders) {
1195 getFinalOverriders(MyFinalOverriders);
1196 FinalOverriders = &MyFinalOverriders;
1197 }
1198
1199 bool Done = false;
1200 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1201 MEnd = FinalOverriders->end();
1202 M != MEnd && !Done; ++M) {
1203 for (OverridingMethods::iterator SO = M->second.begin(),
1204 SOEnd = M->second.end();
1205 SO != SOEnd && !Done; ++SO) {
1206 assert(SO->second.size() > 0 &&
1207 "All virtual functions have overridding virtual functions");
1208
1209 // C++ [class.abstract]p4:
1210 // A class is abstract if it contains or inherits at least one
1211 // pure virtual function for which the final overrider is pure
1212 // virtual.
1213 if (SO->second.front().Method->isPure()) {
1214 data().Abstract = true;
1215 Done = true;
1216 break;
1217 }
1218 }
1219 }
1220 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001221
1222 // Set access bits correctly on the directly-declared conversions.
1223 for (UnresolvedSetIterator I = data().Conversions.begin(),
1224 E = data().Conversions.end();
1225 I != E; ++I)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001226 I.setAccess((*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001227}
1228
1229bool CXXRecordDecl::mayBeAbstract() const {
1230 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1231 isDependentContext())
1232 return false;
1233
1234 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1235 BEnd = bases_end();
1236 B != BEnd; ++B) {
1237 CXXRecordDecl *BaseDecl
1238 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1239 if (BaseDecl->isAbstract())
1240 return true;
1241 }
1242
1243 return false;
1244}
1245
David Blaikie99ba9e32011-12-20 02:48:34 +00001246void CXXMethodDecl::anchor() { }
1247
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001248static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1249 const CXXMethodDecl *BaseMD) {
1250 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1251 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1252 const CXXMethodDecl *MD = *I;
1253 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1254 return true;
1255 if (recursivelyOverrides(MD, BaseMD))
1256 return true;
1257 }
1258 return false;
1259}
1260
1261CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001262CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1263 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001264 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1265 return this;
1266
1267 // Lookup doesn't work for destructors, so handle them separately.
1268 if (isa<CXXDestructorDecl>(this)) {
1269 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001270 if (MD) {
1271 if (recursivelyOverrides(MD, this))
1272 return MD;
1273 if (MayBeBase && recursivelyOverrides(this, MD))
1274 return MD;
1275 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001276 return NULL;
1277 }
1278
1279 lookup_const_result Candidates = RD->lookup(getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +00001280 for (NamedDecl * const * I = Candidates.begin(); I != Candidates.end(); ++I) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001281 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1282 if (!MD)
1283 continue;
1284 if (recursivelyOverrides(MD, this))
1285 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001286 if (MayBeBase && recursivelyOverrides(this, MD))
1287 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001288 }
1289
1290 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1291 E = RD->bases_end(); I != E; ++I) {
1292 const RecordType *RT = I->getType()->getAs<RecordType>();
1293 if (!RT)
1294 continue;
1295 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1296 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1297 if (T)
1298 return T;
1299 }
1300
1301 return NULL;
1302}
1303
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001304CXXMethodDecl *
1305CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001306 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001307 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001308 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001309 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001310 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001311 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001312 isStatic, SCAsWritten, isInline, isConstexpr,
1313 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001314}
1315
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001316CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1317 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1318 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1319 DeclarationNameInfo(), QualType(),
1320 0, false, SC_None, false, false,
1321 SourceLocation());
1322}
1323
Douglas Gregor90916562009-09-29 18:16:17 +00001324bool CXXMethodDecl::isUsualDeallocationFunction() const {
1325 if (getOverloadedOperator() != OO_Delete &&
1326 getOverloadedOperator() != OO_Array_Delete)
1327 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001328
1329 // C++ [basic.stc.dynamic.deallocation]p2:
1330 // A template instance is never a usual deallocation function,
1331 // regardless of its signature.
1332 if (getPrimaryTemplate())
1333 return false;
1334
Douglas Gregor90916562009-09-29 18:16:17 +00001335 // C++ [basic.stc.dynamic.deallocation]p2:
1336 // If a class T has a member deallocation function named operator delete
1337 // with exactly one parameter, then that function is a usual (non-placement)
1338 // deallocation function. [...]
1339 if (getNumParams() == 1)
1340 return true;
1341
1342 // C++ [basic.stc.dynamic.deallocation]p2:
1343 // [...] If class T does not declare such an operator delete but does
1344 // declare a member deallocation function named operator delete with
1345 // exactly two parameters, the second of which has type std::size_t (18.1),
1346 // then this function is a usual deallocation function.
1347 ASTContext &Context = getASTContext();
1348 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001349 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1350 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001351 return false;
1352
1353 // This function is a usual deallocation function if there are no
1354 // single-parameter deallocation functions of the same kind.
David Blaikie3bc93e32012-12-19 00:45:41 +00001355 DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1356 for (DeclContext::lookup_const_result::iterator I = R.begin(), E = R.end();
1357 I != E; ++I) {
1358 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregor90916562009-09-29 18:16:17 +00001359 if (FD->getNumParams() == 1)
1360 return false;
1361 }
1362
1363 return true;
1364}
1365
Douglas Gregor06a9f362010-05-01 20:49:11 +00001366bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001367 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001368 // A user-declared copy assignment operator X::operator= is a non-static
1369 // non-template member function of class X with exactly one parameter of
1370 // type X, X&, const X&, volatile X& or const volatile X&.
1371 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1372 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001373 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001374 return false;
1375
1376 QualType ParamType = getParamDecl(0)->getType();
1377 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1378 ParamType = Ref->getPointeeType();
1379
1380 ASTContext &Context = getASTContext();
1381 QualType ClassType
1382 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1383 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1384}
1385
Sean Huntffe37fd2011-05-25 20:50:04 +00001386bool CXXMethodDecl::isMoveAssignmentOperator() const {
1387 // C++0x [class.copy]p19:
1388 // A user-declared move assignment operator X::operator= is a non-static
1389 // non-template member function of class X with exactly one parameter of type
1390 // X&&, const X&&, volatile X&&, or const volatile X&&.
1391 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1392 getPrimaryTemplate() || getDescribedFunctionTemplate())
1393 return false;
1394
1395 QualType ParamType = getParamDecl(0)->getType();
1396 if (!isa<RValueReferenceType>(ParamType))
1397 return false;
1398 ParamType = ParamType->getPointeeType();
1399
1400 ASTContext &Context = getASTContext();
1401 QualType ClassType
1402 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1403 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1404}
1405
Anders Carlsson05eb2442009-05-16 23:58:37 +00001406void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001407 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001408 assert(!MD->getParent()->isDependentContext() &&
1409 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001410 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001411
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001412 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001413}
1414
1415CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001416 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001417 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001418}
1419
1420CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001421 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001422 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001423}
1424
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001425unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001426 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001427 return getASTContext().overridden_methods_size(this);
1428}
1429
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001430QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001431 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1432 // If the member function is declared const, the type of this is const X*,
1433 // if the member function is declared volatile, the type of this is
1434 // volatile X*, and if the member function is declared const volatile,
1435 // the type of this is const volatile X*.
1436
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001437 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001438
John McCall3cb0ebd2010-03-10 03:28:59 +00001439 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001440 ClassTy = C.getQualifiedType(ClassTy,
1441 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001442 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001443}
1444
Eli Friedmand7d7f672009-12-06 20:50:05 +00001445bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001446 // If this function is a template instantiation, look at the template from
1447 // which it was instantiated.
1448 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1449 if (!CheckFn)
1450 CheckFn = this;
1451
Eli Friedmand7d7f672009-12-06 20:50:05 +00001452 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001453 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001454}
1455
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001456bool CXXMethodDecl::isLambdaStaticInvoker() const {
1457 return getParent()->isLambda() &&
1458 getIdentifier() && getIdentifier()->getName() == "__invoke";
1459}
1460
1461
Sean Huntcbb67482011-01-08 20:30:50 +00001462CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1463 TypeSourceInfo *TInfo, bool IsVirtual,
1464 SourceLocation L, Expr *Init,
1465 SourceLocation R,
1466 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001467 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001468 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1469 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001470{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001471}
1472
Sean Huntcbb67482011-01-08 20:30:50 +00001473CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1474 FieldDecl *Member,
1475 SourceLocation MemberLoc,
1476 SourceLocation L, Expr *Init,
1477 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001478 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001479 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001480 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1481{
1482}
1483
Sean Huntcbb67482011-01-08 20:30:50 +00001484CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1485 IndirectFieldDecl *Member,
1486 SourceLocation MemberLoc,
1487 SourceLocation L, Expr *Init,
1488 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001489 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001490 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001491 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001492{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001493}
1494
Sean Huntcbb67482011-01-08 20:30:50 +00001495CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001496 TypeSourceInfo *TInfo,
1497 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001498 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001499 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1500 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001501 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1502{
1503}
1504
1505CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001506 FieldDecl *Member,
1507 SourceLocation MemberLoc,
1508 SourceLocation L, Expr *Init,
1509 SourceLocation R,
1510 VarDecl **Indices,
1511 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001512 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001513 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001514 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001515{
1516 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1517 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1518}
1519
Sean Huntcbb67482011-01-08 20:30:50 +00001520CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1521 FieldDecl *Member,
1522 SourceLocation MemberLoc,
1523 SourceLocation L, Expr *Init,
1524 SourceLocation R,
1525 VarDecl **Indices,
1526 unsigned NumIndices) {
1527 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001528 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001529 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001530 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1531 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001532}
1533
Sean Huntcbb67482011-01-08 20:30:50 +00001534TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001535 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001536 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001537 else
1538 return TypeLoc();
1539}
1540
Sean Huntcbb67482011-01-08 20:30:50 +00001541const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001542 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001543 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001544 else
1545 return 0;
1546}
1547
Sean Huntcbb67482011-01-08 20:30:50 +00001548SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001549 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001550 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001551
1552 if (isInClassMemberInitializer())
1553 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001554
Douglas Gregor76852c22011-11-01 01:16:03 +00001555 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1556 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1557
1558 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001559}
1560
Sean Huntcbb67482011-01-08 20:30:50 +00001561SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001562 if (isInClassMemberInitializer()) {
1563 FieldDecl *D = getAnyMember();
1564 if (Expr *I = D->getInClassInitializer())
1565 return I->getSourceRange();
1566 return SourceRange();
1567 }
1568
Douglas Gregor802ab452009-12-02 22:36:29 +00001569 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001570}
1571
David Blaikie99ba9e32011-12-20 02:48:34 +00001572void CXXConstructorDecl::anchor() { }
1573
Douglas Gregorb48fe382008-10-31 09:07:45 +00001574CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001575CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1576 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1577 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1578 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001579}
1580
1581CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001582CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001583 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001584 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001585 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001586 bool isExplicit, bool isInline,
1587 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001588 assert(NameInfo.getName().getNameKind()
1589 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001590 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001591 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001592 isExplicit, isInline, isImplicitlyDeclared,
1593 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001594}
1595
Douglas Gregor76852c22011-11-01 01:16:03 +00001596CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1597 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1598 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1599 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1600 return Construct->getConstructor();
1601
1602 return 0;
1603}
1604
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001605bool CXXConstructorDecl::isDefaultConstructor() const {
1606 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001607 // A default constructor for a class X is a constructor of class
1608 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001609 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001610 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001611}
1612
Mike Stump1eb44332009-09-09 15:08:12 +00001613bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001614CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001615 return isCopyOrMoveConstructor(TypeQuals) &&
1616 getParamDecl(0)->getType()->isLValueReferenceType();
1617}
1618
1619bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1620 return isCopyOrMoveConstructor(TypeQuals) &&
1621 getParamDecl(0)->getType()->isRValueReferenceType();
1622}
1623
1624/// \brief Determine whether this is a copy or move constructor.
1625bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001626 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001627 // A non-template constructor for class X is a copy constructor
1628 // if its first parameter is of type X&, const X&, volatile X& or
1629 // const volatile X&, and either there are no other parameters
1630 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001631 // C++0x [class.copy]p3:
1632 // A non-template constructor for class X is a move constructor if its
1633 // first parameter is of type X&&, const X&&, volatile X&&, or
1634 // const volatile X&&, and either there are no other parameters or else
1635 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001636 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001637 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001638 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001639 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001640 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001641
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001642 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001643
1644 // Do we have a reference type?
1645 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001646 if (!ParamRefType)
1647 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001648
Douglas Gregorfd476482009-11-13 23:59:09 +00001649 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001650 ASTContext &Context = getASTContext();
1651
Douglas Gregorfd476482009-11-13 23:59:09 +00001652 CanQualType PointeeType
1653 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001654 CanQualType ClassTy
1655 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001656 if (PointeeType.getUnqualifiedType() != ClassTy)
1657 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001658
John McCall0953e762009-09-24 19:53:00 +00001659 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001660
1661 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001662 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001663 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001664}
1665
Anders Carlssonfaccd722009-08-28 16:57:08 +00001666bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001667 // C++ [class.conv.ctor]p1:
1668 // A constructor declared without the function-specifier explicit
1669 // that can be called with a single parameter specifies a
1670 // conversion from the type of its first parameter to the type of
1671 // its class. Such a constructor is called a converting
1672 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001673 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001674 return false;
1675
Mike Stump1eb44332009-09-09 15:08:12 +00001676 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001677 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001678 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001679 (getNumParams() > 1 &&
1680 (getParamDecl(1)->hasDefaultArg() ||
1681 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001682}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001683
Douglas Gregor6493cc52010-11-08 17:16:59 +00001684bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001685 if ((getNumParams() < 1) ||
1686 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1687 (getPrimaryTemplate() == 0) ||
1688 (getDescribedFunctionTemplate() != 0))
1689 return false;
1690
1691 const ParmVarDecl *Param = getParamDecl(0);
1692
1693 ASTContext &Context = getASTContext();
1694 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1695
Douglas Gregor66724ea2009-11-14 01:20:54 +00001696 // Is it the same as our our class type?
1697 CanQualType ClassTy
1698 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1699 if (ParamType.getUnqualifiedType() != ClassTy)
1700 return false;
1701
1702 return true;
1703}
1704
Sebastian Redlf677ea32011-02-05 19:23:19 +00001705const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1706 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001707 method_iterator It = getASTContext().overridden_methods_begin(this);
1708 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001709 return 0;
1710
1711 return cast<CXXConstructorDecl>(*It);
1712}
1713
1714void
1715CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1716 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001717 assert(getASTContext().overridden_methods_size(this) == 0 &&
1718 "Base ctor already set.");
1719 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001720}
1721
David Blaikie99ba9e32011-12-20 02:48:34 +00001722void CXXDestructorDecl::anchor() { }
1723
Douglas Gregor42a552f2008-11-05 20:51:48 +00001724CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001725CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1726 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1727 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001728 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001729}
1730
1731CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001732CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001733 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001734 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001735 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001736 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001737 assert(NameInfo.getName().getNameKind()
1738 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001739 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001740 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001741 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001742}
1743
David Blaikie99ba9e32011-12-20 02:48:34 +00001744void CXXConversionDecl::anchor() { }
1745
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001746CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001747CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1748 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1749 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1750 QualType(), 0, false, false, false,
1751 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001752}
1753
1754CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001755CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001756 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001757 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001758 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001759 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001760 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001761 assert(NameInfo.getName().getNameKind()
1762 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001763 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001764 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001765 isInline, isExplicit, isConstexpr,
1766 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001767}
1768
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001769bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1770 return isImplicit() && getParent()->isLambda() &&
1771 getConversionType()->isBlockPointerType();
1772}
1773
David Blaikie99ba9e32011-12-20 02:48:34 +00001774void LinkageSpecDecl::anchor() { }
1775
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001776LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001777 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001778 SourceLocation ExternLoc,
1779 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001780 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001781 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001782 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001783}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001784
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001785LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1786 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1787 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1788 lang_c, SourceLocation());
1789}
1790
David Blaikie99ba9e32011-12-20 02:48:34 +00001791void UsingDirectiveDecl::anchor() { }
1792
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001793UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1794 SourceLocation L,
1795 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001796 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001797 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001798 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001799 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001800 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1801 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001802 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1803 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001804}
1805
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001806UsingDirectiveDecl *
1807UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1808 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1809 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1810 NestedNameSpecifierLoc(),
1811 SourceLocation(), 0, 0);
1812}
1813
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001814NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1815 if (NamespaceAliasDecl *NA =
1816 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1817 return NA->getNamespace();
1818 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1819}
1820
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001821void NamespaceDecl::anchor() { }
1822
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001823NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1824 SourceLocation StartLoc,
1825 SourceLocation IdLoc, IdentifierInfo *Id,
1826 NamespaceDecl *PrevDecl)
1827 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1828 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1829{
1830 setPreviousDeclaration(PrevDecl);
1831
1832 if (PrevDecl)
1833 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1834}
1835
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001836NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001837 bool Inline, SourceLocation StartLoc,
1838 SourceLocation IdLoc, IdentifierInfo *Id,
1839 NamespaceDecl *PrevDecl) {
1840 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001841}
1842
1843NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1844 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001845 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1846 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001847}
1848
David Blaikie99ba9e32011-12-20 02:48:34 +00001849void NamespaceAliasDecl::anchor() { }
1850
Mike Stump1eb44332009-09-09 15:08:12 +00001851NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001852 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001853 SourceLocation AliasLoc,
1854 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001855 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001856 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001857 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001858 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1859 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001860 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1861 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001862}
1863
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001864NamespaceAliasDecl *
1865NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1866 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1867 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1868 NestedNameSpecifierLoc(),
1869 SourceLocation(), 0);
1870}
1871
David Blaikie99ba9e32011-12-20 02:48:34 +00001872void UsingShadowDecl::anchor() { }
1873
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001874UsingShadowDecl *
1875UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1876 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1877 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1878}
1879
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001880UsingDecl *UsingShadowDecl::getUsingDecl() const {
1881 const UsingShadowDecl *Shadow = this;
1882 while (const UsingShadowDecl *NextShadow =
1883 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1884 Shadow = NextShadow;
1885 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1886}
1887
David Blaikie99ba9e32011-12-20 02:48:34 +00001888void UsingDecl::anchor() { }
1889
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001890void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1891 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1892 "declaration already in set");
1893 assert(S->getUsingDecl() == this);
1894
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001895 if (FirstUsingShadow.getPointer())
1896 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1897 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001898}
1899
1900void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1901 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1902 "declaration not in set");
1903 assert(S->getUsingDecl() == this);
1904
1905 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1906
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001907 if (FirstUsingShadow.getPointer() == S) {
1908 FirstUsingShadow.setPointer(
1909 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001910 S->UsingOrNextShadow = this;
1911 return;
1912 }
1913
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001914 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001915 while (Prev->UsingOrNextShadow != S)
1916 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1917 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1918 S->UsingOrNextShadow = this;
1919}
1920
Douglas Gregordc355712011-02-25 00:36:19 +00001921UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1922 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001923 const DeclarationNameInfo &NameInfo,
1924 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001925 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001926}
1927
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001928UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1929 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1930 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1931 DeclarationNameInfo(), false);
1932}
1933
David Blaikie99ba9e32011-12-20 02:48:34 +00001934void UnresolvedUsingValueDecl::anchor() { }
1935
John McCall7ba107a2009-11-18 02:36:19 +00001936UnresolvedUsingValueDecl *
1937UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1938 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001939 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001940 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001941 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001942 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001943}
1944
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001945UnresolvedUsingValueDecl *
1946UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1947 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1948 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1949 NestedNameSpecifierLoc(),
1950 DeclarationNameInfo());
1951}
1952
David Blaikie99ba9e32011-12-20 02:48:34 +00001953void UnresolvedUsingTypenameDecl::anchor() { }
1954
John McCall7ba107a2009-11-18 02:36:19 +00001955UnresolvedUsingTypenameDecl *
1956UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1957 SourceLocation UsingLoc,
1958 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001959 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001960 SourceLocation TargetNameLoc,
1961 DeclarationName TargetName) {
1962 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001963 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001964 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001965}
1966
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001967UnresolvedUsingTypenameDecl *
1968UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1969 void *Mem = AllocateDeserializedDecl(C, ID,
1970 sizeof(UnresolvedUsingTypenameDecl));
1971 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
1972 SourceLocation(),
1973 NestedNameSpecifierLoc(),
1974 SourceLocation(),
1975 0);
1976}
1977
David Blaikie99ba9e32011-12-20 02:48:34 +00001978void StaticAssertDecl::anchor() { }
1979
Anders Carlssonfb311762009-03-14 00:25:26 +00001980StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001981 SourceLocation StaticAssertLoc,
1982 Expr *AssertExpr,
1983 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00001984 SourceLocation RParenLoc,
1985 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001986 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00001987 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00001988}
1989
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001990StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
1991 unsigned ID) {
1992 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00001993 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
1994 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001995}
1996
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001997static const char *getAccessName(AccessSpecifier AS) {
1998 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001999 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002000 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002001 case AS_public:
2002 return "public";
2003 case AS_private:
2004 return "private";
2005 case AS_protected:
2006 return "protected";
2007 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002008 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002009}
2010
2011const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2012 AccessSpecifier AS) {
2013 return DB << getAccessName(AS);
2014}
Richard Smithf15fda02012-02-02 01:16:57 +00002015
2016const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2017 AccessSpecifier AS) {
2018 return DB << getAccessName(AS);
2019}