blob: 90a2b4ab4691109bc399a50201dc18350fa8993d [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);
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +0000304
305 if (BaseClassDecl->hasVolatileMember())
306 setHasVolatileMember(true);
John McCallf85e1932011-06-15 23:02:42 +0000307
Douglas Gregor2bb11012011-05-13 01:05:07 +0000308 // Keep track of the presence of mutable fields.
309 if (BaseClassDecl->hasMutableFields())
310 data().HasMutableFields = true;
Richard Smithd5bc8672012-12-08 02:01:17 +0000311
312 if (BaseClassDecl->hasUninitializedReferenceMember())
313 data().HasUninitializedReferenceMember = true;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000314
315 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000316 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000317
318 if (VBases.empty())
319 return;
320
321 // Create base specifier for any direct or indirect virtual bases.
322 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
323 data().NumVBases = VBases.size();
Richard Smithbc2a35d2012-12-08 08:32:28 +0000324 for (int I = 0, E = VBases.size(); I != E; ++I) {
325 QualType Type = VBases[I]->getType();
326 if (!Type->isDependentType())
327 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000328 data().getVBases()[I] = *VBases[I];
Richard Smithbc2a35d2012-12-08 08:32:28 +0000329 }
330}
331
332void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
333 // C++11 [class.copy]p11:
334 // A defaulted copy/move constructor for a class X is defined as
335 // deleted if X has:
336 // -- a direct or virtual base class B that cannot be copied/moved [...]
337 // -- a non-static data member of class type M (or array thereof)
338 // that cannot be copied or moved [...]
339 if (!Subobj->hasSimpleMoveConstructor())
340 data().NeedOverloadResolutionForMoveConstructor = true;
341
342 // C++11 [class.copy]p23:
343 // A defaulted copy/move assignment operator for a class X is defined as
344 // deleted if X has:
345 // -- a direct or virtual base class B that cannot be copied/moved [...]
346 // -- a non-static data member of class type M (or array thereof)
347 // that cannot be copied or moved [...]
348 if (!Subobj->hasSimpleMoveAssignment())
349 data().NeedOverloadResolutionForMoveAssignment = true;
350
351 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
352 // A defaulted [ctor or dtor] for a class X is defined as
353 // deleted if X has:
354 // -- any direct or virtual base class [...] has a type with a destructor
355 // that is deleted or inaccessible from the defaulted [ctor or dtor].
356 // -- any non-static data member has a type with a destructor
357 // that is deleted or inaccessible from the defaulted [ctor or dtor].
358 if (!Subobj->hasSimpleDestructor()) {
359 data().NeedOverloadResolutionForMoveConstructor = true;
360 data().NeedOverloadResolutionForDestructor = true;
361 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000362}
363
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000364/// Callback function for CXXRecordDecl::forallBases that acknowledges
365/// that it saw a base class.
366static bool SawBase(const CXXRecordDecl *, void *) {
367 return true;
368}
369
370bool CXXRecordDecl::hasAnyDependentBases() const {
371 if (!isDependentContext())
372 return false;
373
374 return !forallBases(SawBase, 0);
375}
376
Chandler Carruthb7e95892011-04-23 10:47:28 +0000377bool CXXRecordDecl::isTriviallyCopyable() const {
378 // C++0x [class]p5:
379 // A trivially copyable class is a class that:
380 // -- has no non-trivial copy constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000381 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000382 // -- has no non-trivial move constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000383 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000384 // -- has no non-trivial copy assignment operators,
Richard Smith426391c2012-11-16 00:53:38 +0000385 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000386 // -- has no non-trivial move assignment operators, and
Richard Smith426391c2012-11-16 00:53:38 +0000387 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000388 // -- has a trivial destructor.
389 if (!hasTrivialDestructor()) return false;
390
391 return true;
392}
393
Douglas Gregor21386642010-09-28 21:55:22 +0000394void CXXRecordDecl::markedVirtualFunctionPure() {
395 // C++ [class.abstract]p2:
396 // A class is abstract if it has at least one pure virtual function.
397 data().Abstract = true;
398}
399
400void CXXRecordDecl::addedMember(Decl *D) {
Joao Matos17d35c32012-08-31 22:18:20 +0000401 if (!D->isImplicit() &&
402 !isa<FieldDecl>(D) &&
403 !isa<IndirectFieldDecl>(D) &&
404 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
405 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
406 data().HasOnlyCMembers = false;
407
408 // Ignore friends and invalid declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000409 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000410 return;
411
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000412 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
413 if (FunTmpl)
414 D = FunTmpl->getTemplatedDecl();
415
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000416 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
417 if (Method->isVirtual()) {
418 // C++ [dcl.init.aggr]p1:
419 // An aggregate is an array or a class with [...] no virtual functions.
420 data().Aggregate = false;
421
422 // C++ [class]p4:
423 // A POD-struct is an aggregate class...
424 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000425
426 // Virtual functions make the class non-empty.
427 // FIXME: Standard ref?
428 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000429
430 // C++ [class.virtual]p1:
431 // A class that declares or inherits a virtual function is called a
432 // polymorphic class.
433 data().Polymorphic = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000434
Richard Smith7d04d3a2012-11-30 05:11:39 +0000435 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
436 // A [default constructor, copy/move constructor, or copy/move
437 // assignment operator for a class X] is trivial [...] if:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000438 // -- class X has no virtual functions [...]
Richard Smith7d04d3a2012-11-30 05:11:39 +0000439 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000440
Chandler Carrutha8225442011-04-30 09:17:45 +0000441 // C++0x [class]p7:
442 // A standard-layout class is a class that: [...]
443 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000444 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000445 }
446 }
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000447
Richard Smithacf796b2012-11-28 06:23:12 +0000448 // Notify the listener if an implicit member was added after the definition
449 // was completed.
450 if (!isBeingDefined() && D->isImplicit())
451 if (ASTMutationListener *L = getASTMutationListener())
452 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000453
Richard Smith7d04d3a2012-11-30 05:11:39 +0000454 // The kind of special member this declaration is, if any.
455 unsigned SMKind = 0;
456
Richard Smithacf796b2012-11-28 06:23:12 +0000457 // Handle constructors.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000458 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smithacf796b2012-11-28 06:23:12 +0000459 if (!Constructor->isImplicit()) {
460 // Note that we have a user-declared constructor.
461 data().UserDeclaredConstructor = true;
462
463 // C++ [class]p4:
464 // A POD-struct is an aggregate class [...]
465 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
466 // type is technically an aggregate in C++0x since it wouldn't be in 03.
467 data().PlainOldData = false;
468 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000469
Richard Smith017ab772011-09-05 02:13:09 +0000470 // Technically, "user-provided" is only defined for special member
471 // functions, but the intent of the standard is clearly that it should apply
472 // to all functions.
473 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000474
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000475 if (Constructor->isDefaultConstructor()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000476 SMKind |= SMF_DefaultConstructor;
477
478 if (UserProvided)
Sean Huntcdee3fe2011-05-11 22:34:38 +0000479 data().UserProvidedDefaultConstructor = true;
Richard Smithacf796b2012-11-28 06:23:12 +0000480 if (Constructor->isConstexpr())
Richard Smith61802452011-12-22 02:22:31 +0000481 data().HasConstexprDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000482 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000483
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000484 if (!FunTmpl) {
Richard Smithacf796b2012-11-28 06:23:12 +0000485 unsigned Quals;
486 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000487 SMKind |= SMF_CopyConstructor;
Richard Smithacf796b2012-11-28 06:23:12 +0000488
489 if (Quals & Qualifiers::Const)
490 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith7d04d3a2012-11-30 05:11:39 +0000491 } else if (Constructor->isMoveConstructor())
492 SMKind |= SMF_MoveConstructor;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000493 }
Richard Smithacf796b2012-11-28 06:23:12 +0000494
495 // Record if we see any constexpr constructors which are neither copy
496 // nor move constructors.
497 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith6b8bc072011-08-10 18:11:37 +0000498 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000499
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000500 // C++ [dcl.init.aggr]p1:
501 // An aggregate is an array or a class with no user-declared
502 // constructors [...].
503 // C++0x [dcl.init.aggr]p1:
504 // An aggregate is an array or a class with no user-provided
505 // constructors [...].
Richard Smith80ad52f2013-01-02 11:42:31 +0000506 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smithacf796b2012-11-28 06:23:12 +0000507 ? UserProvided : !Constructor->isImplicit())
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000508 data().Aggregate = false;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000509 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000510
Richard Smithacf796b2012-11-28 06:23:12 +0000511 // Handle destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000512 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000513 SMKind |= SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000514
Richard Smith7d04d3a2012-11-30 05:11:39 +0000515 if (!DD->isImplicit())
Richard Smithacf796b2012-11-28 06:23:12 +0000516 data().HasIrrelevantDestructor = false;
517
Richard Smithacf796b2012-11-28 06:23:12 +0000518 // C++11 [class.dtor]p5:
Richard Smith7d04d3a2012-11-30 05:11:39 +0000519 // A destructor is trivial if [...] the destructor is not virtual.
520 if (DD->isVirtual())
521 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000522 }
Richard Smithacf796b2012-11-28 06:23:12 +0000523
524 // Handle member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000525 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000526 if (Method->isCopyAssignmentOperator()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000527 SMKind |= SMF_CopyAssignment;
Richard Smithacf796b2012-11-28 06:23:12 +0000528
529 const ReferenceType *ParamTy =
530 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
531 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
532 data().HasDeclaredCopyAssignmentWithConstParam = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000533 }
Sean Huntffe37fd2011-05-25 20:50:04 +0000534
Richard Smith7d04d3a2012-11-30 05:11:39 +0000535 if (Method->isMoveAssignmentOperator())
536 SMKind |= SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000537
Douglas Gregore80622f2010-09-29 04:25:11 +0000538 // Keep the list of conversion functions up-to-date.
539 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregore80622f2010-09-29 04:25:11 +0000540 // FIXME: We intentionally don't use the decl's access here because it
541 // hasn't been set yet. That's really just a misdesign in Sema.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000542 if (Conversion->getPrimaryTemplate()) {
543 // We don't record specializations.
544 } else if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000545 if (FunTmpl->getPreviousDecl())
546 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000547 FunTmpl);
548 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000549 data().Conversions.addDecl(getASTContext(), FunTmpl);
Douglas Gregore80622f2010-09-29 04:25:11 +0000550 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000551 if (Conversion->getPreviousDecl())
552 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000553 Conversion);
554 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000555 data().Conversions.addDecl(getASTContext(), Conversion);
Douglas Gregore80622f2010-09-29 04:25:11 +0000556 }
557 }
Richard Smithacf796b2012-11-28 06:23:12 +0000558
Richard Smith7d04d3a2012-11-30 05:11:39 +0000559 if (SMKind) {
Richard Smithac713512012-12-08 02:53:02 +0000560 // If this is the first declaration of a special member, we no longer have
561 // an implicit trivial special member.
562 data().HasTrivialSpecialMembers &=
563 data().DeclaredSpecialMembers | ~SMKind;
564
565 if (!Method->isImplicit() && !Method->isUserProvided()) {
566 // This method is user-declared but not user-provided. We can't work out
567 // whether it's trivial yet (not until we get to the end of the class).
568 // We'll handle this method in finishedDefaultedOrDeletedMember.
569 } else if (Method->isTrivial())
570 data().HasTrivialSpecialMembers |= SMKind;
571 else
572 data().DeclaredNonTrivialSpecialMembers |= SMKind;
573
Richard Smith7d04d3a2012-11-30 05:11:39 +0000574 // Note when we have declared a declared special member, and suppress the
575 // implicit declaration of this special member.
576 data().DeclaredSpecialMembers |= SMKind;
577
578 if (!Method->isImplicit()) {
579 data().UserDeclaredSpecialMembers |= SMKind;
580
581 // C++03 [class]p4:
582 // A POD-struct is an aggregate class that has [...] no user-defined
583 // copy assignment operator and no user-defined destructor.
584 //
585 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
586 // aggregates could not have any constructors, clear it even for an
587 // explicitly defaulted or deleted constructor.
588 // type is technically an aggregate in C++0x since it wouldn't be in 03.
589 //
590 // Also, a user-declared move assignment operator makes a class non-POD.
591 // This is an extension in C++03.
592 data().PlainOldData = false;
593 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000594 }
595
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000596 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000597 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000598
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000599 // Handle non-static data members.
600 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000601 // C++ [class.bit]p2:
602 // A declaration for a bit-field that omits the identifier declares an
603 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
604 // initialized.
605 if (Field->isUnnamedBitfield())
606 return;
607
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000608 // C++ [dcl.init.aggr]p1:
609 // An aggregate is an array or a class (clause 9) with [...] no
610 // private or protected non-static data members (clause 11).
611 //
612 // A POD must be an aggregate.
613 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
614 data().Aggregate = false;
615 data().PlainOldData = false;
616 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000617
618 // C++0x [class]p7:
619 // A standard-layout class is a class that:
620 // [...]
621 // -- has the same access control for all non-static data members,
622 switch (D->getAccess()) {
623 case AS_private: data().HasPrivateFields = true; break;
624 case AS_protected: data().HasProtectedFields = true; break;
625 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000626 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000627 };
628 if ((data().HasPrivateFields + data().HasProtectedFields +
629 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000630 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000631
Douglas Gregor2bb11012011-05-13 01:05:07 +0000632 // Keep track of the presence of mutable fields.
633 if (Field->isMutable())
634 data().HasMutableFields = true;
635
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000636 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000637 // A POD struct is a class that is both a trivial class and a
638 // standard-layout class, and has no non-static data members of type
639 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000640 //
641 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
642 // that does not explicitly have no lifetime makes the class a non-POD.
643 // However, we delay setting PlainOldData to false in this case so that
644 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000645 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000646 // In this case, the class will become a non-POD class when we complete
647 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000648 ASTContext &Context = getASTContext();
649 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000650 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000651 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000652 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
653 setHasObjectMember(true);
654 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000655 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000656
Chandler Carrutha8225442011-04-30 09:17:45 +0000657 if (T->isReferenceType()) {
Richard Smithd5bc8672012-12-08 02:01:17 +0000658 if (!Field->hasInClassInitializer())
659 data().HasUninitializedReferenceMember = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000660
Chandler Carrutha8225442011-04-30 09:17:45 +0000661 // C++0x [class]p7:
662 // A standard-layout class is a class that:
663 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000664 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000665 }
666
Richard Smith86c3ae42012-02-13 03:54:03 +0000667 // Record if this field is the first non-literal or volatile field or base.
668 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000669 data().HasNonLiteralTypeFieldsOrBases = true;
670
Richard Smith7a614d82011-06-11 17:19:42 +0000671 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000672 data().HasInClassInitializer = true;
673
674 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000675 // A default constructor is trivial if [...] no non-static data member
676 // of its class has a brace-or-equal-initializer.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000677 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith7a614d82011-06-11 17:19:42 +0000678
Richard Smithd079abf2012-05-07 01:07:30 +0000679 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000680 // An aggregate is a [...] class with [...] no
681 // brace-or-equal-initializers for non-static data members.
682 data().Aggregate = false;
683
Richard Smithd079abf2012-05-07 01:07:30 +0000684 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000685 // A POD struct is [...] a trivial class.
686 data().PlainOldData = false;
687 }
688
Richard Smithbc2a35d2012-12-08 08:32:28 +0000689 // C++11 [class.copy]p23:
690 // A defaulted copy/move assignment operator for a class X is defined
691 // as deleted if X has:
692 // -- a non-static data member of reference type
693 if (T->isReferenceType())
694 data().DefaultedMoveAssignmentIsDeleted = true;
695
Douglas Gregor85606eb2010-09-28 20:50:54 +0000696 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
697 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
698 if (FieldRec->getDefinition()) {
Richard Smithbc2a35d2012-12-08 08:32:28 +0000699 addedClassSubobject(FieldRec);
700
701 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
702 // A defaulted [special member] for a class X is defined as
703 // deleted if:
704 // -- X is a union-like class that has a variant member with a
705 // non-trivial [corresponding special member]
706 if (isUnion()) {
707 if (FieldRec->hasNonTrivialMoveConstructor())
708 data().DefaultedMoveConstructorIsDeleted = true;
709 if (FieldRec->hasNonTrivialMoveAssignment())
710 data().DefaultedMoveAssignmentIsDeleted = true;
711 if (FieldRec->hasNonTrivialDestructor())
712 data().DefaultedDestructorIsDeleted = true;
713 }
714
Sean Hunt023df372011-05-09 18:22:59 +0000715 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000716 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000717 // -- for all the non-static data members of its class that are of
718 // class type (or array thereof), each such class has a trivial
719 // default constructor.
720 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000721 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000722
723 // C++0x [class.copy]p13:
724 // A copy/move constructor for class X is trivial if [...]
725 // [...]
726 // -- for each non-static data member of X that is of class type (or
727 // an array thereof), the constructor selected to copy/move that
728 // member is trivial;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000729 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000730 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000731 // If the field doesn't have a simple move constructor, we'll eagerly
732 // declare the move constructor for this class and we'll decide whether
733 // it's trivial then.
Richard Smith426391c2012-11-16 00:53:38 +0000734 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000735 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000736
737 // C++0x [class.copy]p27:
738 // A copy/move assignment operator for class X is trivial if [...]
739 // [...]
740 // -- for each non-static data member of X that is of class type (or
741 // an array thereof), the assignment operator selected to
742 // copy/move that member is trivial;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000743 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000744 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000745 // If the field doesn't have a simple move assignment, we'll eagerly
746 // declare the move assignment for this class and we'll decide whether
747 // it's trivial then.
Richard Smith426391c2012-11-16 00:53:38 +0000748 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000749 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000750
Douglas Gregor85606eb2010-09-28 20:50:54 +0000751 if (!FieldRec->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000752 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000753 if (!FieldRec->hasIrrelevantDestructor())
754 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000755 if (FieldRec->hasObjectMember())
756 setHasObjectMember(true);
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +0000757 if (FieldRec->hasVolatileMember())
758 setHasVolatileMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000759
760 // C++0x [class]p7:
761 // A standard-layout class is a class that:
762 // -- has no non-static data members of type non-standard-layout
763 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000764 if (!FieldRec->isStandardLayout())
765 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000766
767 // C++0x [class]p7:
768 // A standard-layout class is a class that:
769 // [...]
770 // -- has no base classes of the same type as the first non-static
771 // data member.
772 // We don't want to expend bits in the state of the record decl
773 // tracking whether this is the first non-static data member so we
774 // cheat a bit and use some of the existing state: the empty bit.
775 // Virtual bases and virtual methods make a class non-empty, but they
776 // also make it non-standard-layout so we needn't check here.
777 // A non-empty base class may leave the class standard-layout, but not
778 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000779 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000780 // data member must come through here with Empty still true, and Empty
781 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000782 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000783 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
784 BE = bases_end();
785 BI != BE; ++BI) {
786 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000787 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000788 break;
789 }
790 }
791 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000792
793 // Keep track of the presence of mutable fields.
794 if (FieldRec->hasMutableFields())
795 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000796
797 // C++11 [class.copy]p13:
798 // If the implicitly-defined constructor would satisfy the
799 // requirements of a constexpr constructor, the implicitly-defined
800 // constructor is constexpr.
801 // C++11 [dcl.constexpr]p4:
802 // -- every constructor involved in initializing non-static data
803 // members [...] shall be a constexpr constructor
804 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000805 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000806 // The standard requires any in-class initializer to be a constant
807 // expression. We consider this to be a defect.
808 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithacf796b2012-11-28 06:23:12 +0000809
810 // C++11 [class.copy]p8:
811 // The implicitly-declared copy constructor for a class X will have
812 // the form 'X::X(const X&)' if [...] for all the non-static data
813 // members of X that are of a class type M (or array thereof), each
814 // such class type has a copy constructor whose first parameter is
815 // of type 'const M&' or 'const volatile M&'.
816 if (!FieldRec->hasCopyConstructorWithConstParam())
817 data().ImplicitCopyConstructorHasConstParam = false;
818
819 // C++11 [class.copy]p18:
820 // The implicitly-declared copy assignment oeprator for a class X will
821 // have the form 'X& X::operator=(const X&)' if [...] for all the
822 // non-static data members of X that are of a class type M (or array
823 // thereof), each such class type has a copy assignment operator whose
824 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
825 if (!FieldRec->hasCopyAssignmentWithConstParam())
826 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smithd5bc8672012-12-08 02:01:17 +0000827
828 if (FieldRec->hasUninitializedReferenceMember() &&
829 !Field->hasInClassInitializer())
830 data().HasUninitializedReferenceMember = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000831 }
Richard Smith61802452011-12-22 02:22:31 +0000832 } else {
833 // Base element type of field is a non-class type.
Richard Smithd3861ce2012-06-10 07:07:24 +0000834 if (!T->isLiteralType() ||
835 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000836 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000837
838 // C++11 [class.copy]p23:
839 // A defaulted copy/move assignment operator for a class X is defined
840 // as deleted if X has:
841 // -- a non-static data member of const non-class type (or array
842 // thereof)
843 if (T.isConstQualified())
844 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000845 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000846
847 // C++0x [class]p7:
848 // A standard-layout class is a class that:
849 // [...]
850 // -- either has no non-static data members in the most derived
851 // class and at most one base class with non-static data members,
852 // or has no base classes with non-static data members, and
853 // At this point we know that we have a non-static data member, so the last
854 // clause holds.
855 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000856 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000857
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000858 // If this is not a zero-length bit-field, then the class is not empty.
859 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000860 if (!Field->isBitField() ||
861 (!Field->getBitWidth()->isTypeDependent() &&
862 !Field->getBitWidth()->isValueDependent() &&
863 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000864 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000865 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000866 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000867
868 // Handle using declarations of conversion functions.
869 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
870 if (Shadow->getDeclName().getNameKind()
871 == DeclarationName::CXXConversionFunctionName)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000872 data().Conversions.addDecl(getASTContext(), Shadow, Shadow->getAccess());
Joao Matos17d35c32012-08-31 22:18:20 +0000873}
874
Richard Smithac713512012-12-08 02:53:02 +0000875void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
876 assert(!D->isImplicit() && !D->isUserProvided());
877
878 // The kind of special member this declaration is, if any.
879 unsigned SMKind = 0;
880
881 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
882 if (Constructor->isDefaultConstructor()) {
883 SMKind |= SMF_DefaultConstructor;
884 if (Constructor->isConstexpr())
885 data().HasConstexprDefaultConstructor = true;
886 }
887 if (Constructor->isCopyConstructor())
888 SMKind |= SMF_CopyConstructor;
889 else if (Constructor->isMoveConstructor())
890 SMKind |= SMF_MoveConstructor;
891 else if (Constructor->isConstexpr())
892 // We may now know that the constructor is constexpr.
893 data().HasConstexprNonCopyMoveConstructor = true;
894 } else if (isa<CXXDestructorDecl>(D))
895 SMKind |= SMF_Destructor;
896 else if (D->isCopyAssignmentOperator())
897 SMKind |= SMF_CopyAssignment;
898 else if (D->isMoveAssignmentOperator())
899 SMKind |= SMF_MoveAssignment;
900
901 // Update which trivial / non-trivial special members we have.
902 // addedMember will have skipped this step for this member.
903 if (D->isTrivial())
904 data().HasTrivialSpecialMembers |= SMKind;
905 else
906 data().DeclaredNonTrivialSpecialMembers |= SMKind;
907}
908
Joao Matos17d35c32012-08-31 22:18:20 +0000909bool CXXRecordDecl::isCLike() const {
910 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
911 !TemplateOrInstantiation.isNull())
912 return false;
913 if (!hasDefinition())
914 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000915
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000916 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000917}
918
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000919void CXXRecordDecl::getCaptureFields(
920 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000921 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000922 Captures.clear();
923 ThisCapture = 0;
924
Douglas Gregorda8962a2012-02-13 15:44:47 +0000925 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000926 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000927 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000928 C != CEnd; ++C, ++Field) {
929 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +0000930 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000931 continue;
932 }
933
David Blaikie581deb32012-06-06 20:45:41 +0000934 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000935 }
936}
937
938
John McCallb05b5f32010-03-15 09:07:48 +0000939static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
940 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000941 if (isa<UsingShadowDecl>(Conv))
942 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000943 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
944 T = ConvTemp->getTemplatedDecl()->getResultType();
945 else
946 T = cast<CXXConversionDecl>(Conv)->getConversionType();
947 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000948}
949
John McCallb05b5f32010-03-15 09:07:48 +0000950/// Collect the visible conversions of a base class.
951///
James Dennetta1253502012-06-15 22:28:09 +0000952/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +0000953/// \param InVirtual whether this base class is a virtual base (or a base
954/// of a virtual base)
955/// \param Access the access along the inheritance path to this base
956/// \param ParentHiddenTypes the conversions provided by the inheritors
957/// of this base
958/// \param Output the set to which to add conversions from non-virtual bases
959/// \param VOutput the set to which to add conversions from virtual bases
960/// \param HiddenVBaseCs the set of conversions which were hidden in a
961/// virtual base along some inheritance path
962static void CollectVisibleConversions(ASTContext &Context,
963 CXXRecordDecl *Record,
964 bool InVirtual,
965 AccessSpecifier Access,
966 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000967 ASTUnresolvedSet &Output,
John McCallb05b5f32010-03-15 09:07:48 +0000968 UnresolvedSetImpl &VOutput,
969 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
970 // The set of types which have conversions in this class or its
971 // subclasses. As an optimization, we don't copy the derived set
972 // unless it might change.
973 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
974 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
975
976 // Collect the direct conversions and figure out which conversions
977 // will be hidden in the subclasses.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000978 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
979 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
980 if (ConvI != ConvE) {
John McCallb05b5f32010-03-15 09:07:48 +0000981 HiddenTypesBuffer = ParentHiddenTypes;
982 HiddenTypes = &HiddenTypesBuffer;
983
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000984 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +0000985 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
986 bool Hidden = ParentHiddenTypes.count(ConvType);
987 if (!Hidden)
988 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +0000989
990 // If this conversion is hidden and we're in a virtual base,
991 // remember that it's hidden along some inheritance path.
992 if (Hidden && InVirtual)
993 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
994
995 // If this conversion isn't hidden, add it to the appropriate output.
996 else if (!Hidden) {
997 AccessSpecifier IAccess
998 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
999
1000 if (InVirtual)
1001 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001002 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001003 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001004 }
1005 }
1006 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001007
John McCallb05b5f32010-03-15 09:07:48 +00001008 // Collect information recursively from any base classes.
1009 for (CXXRecordDecl::base_class_iterator
1010 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1011 const RecordType *RT = I->getType()->getAs<RecordType>();
1012 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001013
John McCallb05b5f32010-03-15 09:07:48 +00001014 AccessSpecifier BaseAccess
1015 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1016 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001017
John McCallb05b5f32010-03-15 09:07:48 +00001018 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1019 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1020 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001021 }
John McCallb05b5f32010-03-15 09:07:48 +00001022}
Sebastian Redl9994a342009-10-25 17:03:50 +00001023
John McCallb05b5f32010-03-15 09:07:48 +00001024/// Collect the visible conversions of a class.
1025///
1026/// This would be extremely straightforward if it weren't for virtual
1027/// bases. It might be worth special-casing that, really.
1028static void CollectVisibleConversions(ASTContext &Context,
1029 CXXRecordDecl *Record,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001030 ASTUnresolvedSet &Output) {
John McCallb05b5f32010-03-15 09:07:48 +00001031 // The collection of all conversions in virtual bases that we've
1032 // found. These will be added to the output as long as they don't
1033 // appear in the hidden-conversions set.
1034 UnresolvedSet<8> VBaseCs;
1035
1036 // The set of conversions in virtual bases that we've determined to
1037 // be hidden.
1038 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1039
1040 // The set of types hidden by classes derived from this one.
1041 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1042
1043 // Go ahead and collect the direct conversions and add them to the
1044 // hidden-types set.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001045 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1046 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001047 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001048 for (; ConvI != ConvE; ++ConvI)
1049 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCallb05b5f32010-03-15 09:07:48 +00001050
1051 // Recursively collect conversions from base classes.
1052 for (CXXRecordDecl::base_class_iterator
1053 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1054 const RecordType *RT = I->getType()->getAs<RecordType>();
1055 if (!RT) continue;
1056
1057 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1058 I->isVirtual(), I->getAccessSpecifier(),
1059 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1060 }
1061
1062 // Add any unhidden conversions provided by virtual bases.
1063 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1064 I != E; ++I) {
1065 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001066 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001067 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001068}
1069
1070/// getVisibleConversionFunctions - get all conversion functions visible
1071/// in current class; including conversion function templates.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001072std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1073CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001074 // If root class, all conversions are visible.
1075 if (bases_begin() == bases_end())
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001076 return std::make_pair(data().Conversions.begin(), data().Conversions.end());
Fariborz Jahanian62509212009-09-12 18:26:03 +00001077 // If visible conversion list is already evaluated, return it.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001078 if (!data().ComputedVisibleConversions) {
1079 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
1080 data().ComputedVisibleConversions = true;
1081 }
1082 return std::make_pair(data().VisibleConversions.begin(),
1083 data().VisibleConversions.end());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001084}
1085
John McCall32daa422010-03-31 01:36:47 +00001086void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1087 // This operation is O(N) but extremely rare. Sema only uses it to
1088 // remove UsingShadowDecls in a class that were followed by a direct
1089 // declaration, e.g.:
1090 // class A : B {
1091 // using B::operator int;
1092 // operator int();
1093 // };
1094 // This is uncommon by itself and even more uncommon in conjunction
1095 // with sufficiently large numbers of directly-declared conversions
1096 // that asymptotic behavior matters.
1097
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001098 ASTUnresolvedSet &Convs = data().Conversions;
John McCall32daa422010-03-31 01:36:47 +00001099 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1100 if (Convs[I].getDecl() == ConvDecl) {
1101 Convs.erase(I);
1102 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1103 && "conversion was found multiple times in unresolved set");
1104 return;
1105 }
1106 }
1107
1108 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001109}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001110
Douglas Gregorf6b11852009-10-08 15:14:33 +00001111CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001112 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001113 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1114
1115 return 0;
1116}
1117
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001118MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1119 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1120}
1121
Douglas Gregorf6b11852009-10-08 15:14:33 +00001122void
1123CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1124 TemplateSpecializationKind TSK) {
1125 assert(TemplateOrInstantiation.isNull() &&
1126 "Previous template or instantiation?");
1127 assert(!isa<ClassTemplateSpecializationDecl>(this));
1128 TemplateOrInstantiation
1129 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1130}
1131
Anders Carlssonb13e3572009-12-07 06:33:48 +00001132TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1133 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001134 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1135 return Spec->getSpecializationKind();
1136
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001137 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001138 return MSInfo->getTemplateSpecializationKind();
1139
1140 return TSK_Undeclared;
1141}
1142
1143void
1144CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1145 if (ClassTemplateSpecializationDecl *Spec
1146 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1147 Spec->setSpecializationKind(TSK);
1148 return;
1149 }
1150
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001151 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001152 MSInfo->setTemplateSpecializationKind(TSK);
1153 return;
1154 }
1155
David Blaikieb219cfc2011-09-23 05:06:16 +00001156 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001157}
1158
Douglas Gregor1d110e02010-07-01 14:13:13 +00001159CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1160 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001161 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001162
1163 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001164 = Context.DeclarationNames.getCXXDestructorName(
1165 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001166
David Blaikie3bc93e32012-12-19 00:45:41 +00001167 DeclContext::lookup_const_result R = lookup(Name);
1168 if (R.empty())
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001169 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001170
David Blaikie3bc93e32012-12-19 00:45:41 +00001171 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson7267c162009-05-29 21:03:38 +00001172 return Dtor;
1173}
1174
Douglas Gregorda2142f2011-02-19 18:51:44 +00001175void CXXRecordDecl::completeDefinition() {
1176 completeDefinition(0);
1177}
1178
1179void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1180 RecordDecl::completeDefinition();
1181
David Blaikie4e4d0842012-03-11 07:00:24 +00001182 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001183 // Objective-C Automatic Reference Counting:
1184 // If a class has a non-static data member of Objective-C pointer
1185 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001186 // default constructor (if any), copy constructor, move constructor,
1187 // copy assignment operator, move assignment operator, and destructor are
1188 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001189 struct DefinitionData &Data = data();
1190 Data.PlainOldData = false;
Richard Smith7d04d3a2012-11-30 05:11:39 +00001191 Data.HasTrivialSpecialMembers = 0;
Richard Smithdfefb842012-02-25 07:33:38 +00001192 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001193 }
1194
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001195 // If the class may be abstract (but hasn't been marked as such), check for
1196 // any pure final overriders.
1197 if (mayBeAbstract()) {
1198 CXXFinalOverriderMap MyFinalOverriders;
1199 if (!FinalOverriders) {
1200 getFinalOverriders(MyFinalOverriders);
1201 FinalOverriders = &MyFinalOverriders;
1202 }
1203
1204 bool Done = false;
1205 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1206 MEnd = FinalOverriders->end();
1207 M != MEnd && !Done; ++M) {
1208 for (OverridingMethods::iterator SO = M->second.begin(),
1209 SOEnd = M->second.end();
1210 SO != SOEnd && !Done; ++SO) {
1211 assert(SO->second.size() > 0 &&
1212 "All virtual functions have overridding virtual functions");
1213
1214 // C++ [class.abstract]p4:
1215 // A class is abstract if it contains or inherits at least one
1216 // pure virtual function for which the final overrider is pure
1217 // virtual.
1218 if (SO->second.front().Method->isPure()) {
1219 data().Abstract = true;
1220 Done = true;
1221 break;
1222 }
1223 }
1224 }
1225 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001226
1227 // Set access bits correctly on the directly-declared conversions.
1228 for (UnresolvedSetIterator I = data().Conversions.begin(),
1229 E = data().Conversions.end();
1230 I != E; ++I)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001231 I.setAccess((*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001232}
1233
1234bool CXXRecordDecl::mayBeAbstract() const {
1235 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1236 isDependentContext())
1237 return false;
1238
1239 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1240 BEnd = bases_end();
1241 B != BEnd; ++B) {
1242 CXXRecordDecl *BaseDecl
1243 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1244 if (BaseDecl->isAbstract())
1245 return true;
1246 }
1247
1248 return false;
1249}
1250
David Blaikie99ba9e32011-12-20 02:48:34 +00001251void CXXMethodDecl::anchor() { }
1252
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001253static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1254 const CXXMethodDecl *BaseMD) {
1255 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1256 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1257 const CXXMethodDecl *MD = *I;
1258 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1259 return true;
1260 if (recursivelyOverrides(MD, BaseMD))
1261 return true;
1262 }
1263 return false;
1264}
1265
1266CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001267CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1268 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001269 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1270 return this;
1271
1272 // Lookup doesn't work for destructors, so handle them separately.
1273 if (isa<CXXDestructorDecl>(this)) {
1274 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001275 if (MD) {
1276 if (recursivelyOverrides(MD, this))
1277 return MD;
1278 if (MayBeBase && recursivelyOverrides(this, MD))
1279 return MD;
1280 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001281 return NULL;
1282 }
1283
1284 lookup_const_result Candidates = RD->lookup(getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +00001285 for (NamedDecl * const * I = Candidates.begin(); I != Candidates.end(); ++I) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001286 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1287 if (!MD)
1288 continue;
1289 if (recursivelyOverrides(MD, this))
1290 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001291 if (MayBeBase && recursivelyOverrides(this, MD))
1292 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001293 }
1294
1295 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1296 E = RD->bases_end(); I != E; ++I) {
1297 const RecordType *RT = I->getType()->getAs<RecordType>();
1298 if (!RT)
1299 continue;
1300 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1301 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1302 if (T)
1303 return T;
1304 }
1305
1306 return NULL;
1307}
1308
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001309CXXMethodDecl *
1310CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001311 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001312 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001313 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001314 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001315 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001316 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001317 isStatic, SCAsWritten, isInline, isConstexpr,
1318 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001319}
1320
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001321CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1322 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1323 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1324 DeclarationNameInfo(), QualType(),
1325 0, false, SC_None, false, false,
1326 SourceLocation());
1327}
1328
Douglas Gregor90916562009-09-29 18:16:17 +00001329bool CXXMethodDecl::isUsualDeallocationFunction() const {
1330 if (getOverloadedOperator() != OO_Delete &&
1331 getOverloadedOperator() != OO_Array_Delete)
1332 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001333
1334 // C++ [basic.stc.dynamic.deallocation]p2:
1335 // A template instance is never a usual deallocation function,
1336 // regardless of its signature.
1337 if (getPrimaryTemplate())
1338 return false;
1339
Douglas Gregor90916562009-09-29 18:16:17 +00001340 // C++ [basic.stc.dynamic.deallocation]p2:
1341 // If a class T has a member deallocation function named operator delete
1342 // with exactly one parameter, then that function is a usual (non-placement)
1343 // deallocation function. [...]
1344 if (getNumParams() == 1)
1345 return true;
1346
1347 // C++ [basic.stc.dynamic.deallocation]p2:
1348 // [...] If class T does not declare such an operator delete but does
1349 // declare a member deallocation function named operator delete with
1350 // exactly two parameters, the second of which has type std::size_t (18.1),
1351 // then this function is a usual deallocation function.
1352 ASTContext &Context = getASTContext();
1353 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001354 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1355 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001356 return false;
1357
1358 // This function is a usual deallocation function if there are no
1359 // single-parameter deallocation functions of the same kind.
David Blaikie3bc93e32012-12-19 00:45:41 +00001360 DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1361 for (DeclContext::lookup_const_result::iterator I = R.begin(), E = R.end();
1362 I != E; ++I) {
1363 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregor90916562009-09-29 18:16:17 +00001364 if (FD->getNumParams() == 1)
1365 return false;
1366 }
1367
1368 return true;
1369}
1370
Douglas Gregor06a9f362010-05-01 20:49:11 +00001371bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001372 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001373 // A user-declared copy assignment operator X::operator= is a non-static
1374 // non-template member function of class X with exactly one parameter of
1375 // type X, X&, const X&, volatile X& or const volatile X&.
1376 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1377 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001378 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001379 return false;
1380
1381 QualType ParamType = getParamDecl(0)->getType();
1382 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1383 ParamType = Ref->getPointeeType();
1384
1385 ASTContext &Context = getASTContext();
1386 QualType ClassType
1387 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1388 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1389}
1390
Sean Huntffe37fd2011-05-25 20:50:04 +00001391bool CXXMethodDecl::isMoveAssignmentOperator() const {
1392 // C++0x [class.copy]p19:
1393 // A user-declared move assignment operator X::operator= is a non-static
1394 // non-template member function of class X with exactly one parameter of type
1395 // X&&, const X&&, volatile X&&, or const volatile X&&.
1396 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1397 getPrimaryTemplate() || getDescribedFunctionTemplate())
1398 return false;
1399
1400 QualType ParamType = getParamDecl(0)->getType();
1401 if (!isa<RValueReferenceType>(ParamType))
1402 return false;
1403 ParamType = ParamType->getPointeeType();
1404
1405 ASTContext &Context = getASTContext();
1406 QualType ClassType
1407 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1408 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1409}
1410
Anders Carlsson05eb2442009-05-16 23:58:37 +00001411void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001412 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001413 assert(!MD->getParent()->isDependentContext() &&
1414 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001415 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001416
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001417 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001418}
1419
1420CXXMethodDecl::method_iterator CXXMethodDecl::begin_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_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001423}
1424
1425CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001426 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001427 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001428}
1429
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001430unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001431 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001432 return getASTContext().overridden_methods_size(this);
1433}
1434
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001435QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001436 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1437 // If the member function is declared const, the type of this is const X*,
1438 // if the member function is declared volatile, the type of this is
1439 // volatile X*, and if the member function is declared const volatile,
1440 // the type of this is const volatile X*.
1441
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001442 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001443
John McCall3cb0ebd2010-03-10 03:28:59 +00001444 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001445 ClassTy = C.getQualifiedType(ClassTy,
1446 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001447 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001448}
1449
Eli Friedmand7d7f672009-12-06 20:50:05 +00001450bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001451 // If this function is a template instantiation, look at the template from
1452 // which it was instantiated.
1453 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1454 if (!CheckFn)
1455 CheckFn = this;
1456
Eli Friedmand7d7f672009-12-06 20:50:05 +00001457 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001458 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001459}
1460
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001461bool CXXMethodDecl::isLambdaStaticInvoker() const {
1462 return getParent()->isLambda() &&
1463 getIdentifier() && getIdentifier()->getName() == "__invoke";
1464}
1465
1466
Sean Huntcbb67482011-01-08 20:30:50 +00001467CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1468 TypeSourceInfo *TInfo, bool IsVirtual,
1469 SourceLocation L, Expr *Init,
1470 SourceLocation R,
1471 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001472 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001473 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1474 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001475{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001476}
1477
Sean Huntcbb67482011-01-08 20:30:50 +00001478CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1479 FieldDecl *Member,
1480 SourceLocation MemberLoc,
1481 SourceLocation L, Expr *Init,
1482 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001483 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001484 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001485 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1486{
1487}
1488
Sean Huntcbb67482011-01-08 20:30:50 +00001489CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1490 IndirectFieldDecl *Member,
1491 SourceLocation MemberLoc,
1492 SourceLocation L, Expr *Init,
1493 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001494 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001495 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001496 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001497{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001498}
1499
Sean Huntcbb67482011-01-08 20:30:50 +00001500CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001501 TypeSourceInfo *TInfo,
1502 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001503 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001504 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1505 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001506 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1507{
1508}
1509
1510CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001511 FieldDecl *Member,
1512 SourceLocation MemberLoc,
1513 SourceLocation L, Expr *Init,
1514 SourceLocation R,
1515 VarDecl **Indices,
1516 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001517 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001518 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001519 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001520{
1521 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1522 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1523}
1524
Sean Huntcbb67482011-01-08 20:30:50 +00001525CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1526 FieldDecl *Member,
1527 SourceLocation MemberLoc,
1528 SourceLocation L, Expr *Init,
1529 SourceLocation R,
1530 VarDecl **Indices,
1531 unsigned NumIndices) {
1532 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001533 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001534 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001535 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1536 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001537}
1538
Sean Huntcbb67482011-01-08 20:30:50 +00001539TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001540 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001541 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001542 else
1543 return TypeLoc();
1544}
1545
Sean Huntcbb67482011-01-08 20:30:50 +00001546const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001547 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001548 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001549 else
1550 return 0;
1551}
1552
Sean Huntcbb67482011-01-08 20:30:50 +00001553SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001554 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001555 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001556
1557 if (isInClassMemberInitializer())
1558 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001559
Douglas Gregor76852c22011-11-01 01:16:03 +00001560 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1561 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1562
1563 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001564}
1565
Sean Huntcbb67482011-01-08 20:30:50 +00001566SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001567 if (isInClassMemberInitializer()) {
1568 FieldDecl *D = getAnyMember();
1569 if (Expr *I = D->getInClassInitializer())
1570 return I->getSourceRange();
1571 return SourceRange();
1572 }
1573
Douglas Gregor802ab452009-12-02 22:36:29 +00001574 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001575}
1576
David Blaikie99ba9e32011-12-20 02:48:34 +00001577void CXXConstructorDecl::anchor() { }
1578
Douglas Gregorb48fe382008-10-31 09:07:45 +00001579CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001580CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1581 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1582 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1583 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001584}
1585
1586CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001587CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001588 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001589 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001590 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001591 bool isExplicit, bool isInline,
1592 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001593 assert(NameInfo.getName().getNameKind()
1594 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001595 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001596 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001597 isExplicit, isInline, isImplicitlyDeclared,
1598 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001599}
1600
Douglas Gregor76852c22011-11-01 01:16:03 +00001601CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1602 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1603 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1604 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1605 return Construct->getConstructor();
1606
1607 return 0;
1608}
1609
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001610bool CXXConstructorDecl::isDefaultConstructor() const {
1611 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001612 // A default constructor for a class X is a constructor of class
1613 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001614 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001615 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001616}
1617
Mike Stump1eb44332009-09-09 15:08:12 +00001618bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001619CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001620 return isCopyOrMoveConstructor(TypeQuals) &&
1621 getParamDecl(0)->getType()->isLValueReferenceType();
1622}
1623
1624bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1625 return isCopyOrMoveConstructor(TypeQuals) &&
1626 getParamDecl(0)->getType()->isRValueReferenceType();
1627}
1628
1629/// \brief Determine whether this is a copy or move constructor.
1630bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001631 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001632 // A non-template constructor for class X is a copy constructor
1633 // if its first parameter is of type X&, const X&, volatile X& or
1634 // const volatile X&, and either there are no other parameters
1635 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001636 // C++0x [class.copy]p3:
1637 // A non-template constructor for class X is a move constructor if its
1638 // first parameter is of type X&&, const X&&, volatile X&&, or
1639 // const volatile X&&, and either there are no other parameters or else
1640 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001641 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001642 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001643 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001644 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001645 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001646
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001647 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001648
1649 // Do we have a reference type?
1650 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001651 if (!ParamRefType)
1652 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001653
Douglas Gregorfd476482009-11-13 23:59:09 +00001654 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001655 ASTContext &Context = getASTContext();
1656
Douglas Gregorfd476482009-11-13 23:59:09 +00001657 CanQualType PointeeType
1658 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001659 CanQualType ClassTy
1660 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001661 if (PointeeType.getUnqualifiedType() != ClassTy)
1662 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001663
John McCall0953e762009-09-24 19:53:00 +00001664 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001665
1666 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001667 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001668 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001669}
1670
Anders Carlssonfaccd722009-08-28 16:57:08 +00001671bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001672 // C++ [class.conv.ctor]p1:
1673 // A constructor declared without the function-specifier explicit
1674 // that can be called with a single parameter specifies a
1675 // conversion from the type of its first parameter to the type of
1676 // its class. Such a constructor is called a converting
1677 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001678 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001679 return false;
1680
Mike Stump1eb44332009-09-09 15:08:12 +00001681 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001682 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001683 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001684 (getNumParams() > 1 &&
1685 (getParamDecl(1)->hasDefaultArg() ||
1686 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001687}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001688
Douglas Gregor6493cc52010-11-08 17:16:59 +00001689bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001690 if ((getNumParams() < 1) ||
1691 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1692 (getPrimaryTemplate() == 0) ||
1693 (getDescribedFunctionTemplate() != 0))
1694 return false;
1695
1696 const ParmVarDecl *Param = getParamDecl(0);
1697
1698 ASTContext &Context = getASTContext();
1699 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1700
Douglas Gregor66724ea2009-11-14 01:20:54 +00001701 // Is it the same as our our class type?
1702 CanQualType ClassTy
1703 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1704 if (ParamType.getUnqualifiedType() != ClassTy)
1705 return false;
1706
1707 return true;
1708}
1709
Sebastian Redlf677ea32011-02-05 19:23:19 +00001710const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1711 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001712 method_iterator It = getASTContext().overridden_methods_begin(this);
1713 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001714 return 0;
1715
1716 return cast<CXXConstructorDecl>(*It);
1717}
1718
1719void
1720CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1721 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001722 assert(getASTContext().overridden_methods_size(this) == 0 &&
1723 "Base ctor already set.");
1724 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001725}
1726
David Blaikie99ba9e32011-12-20 02:48:34 +00001727void CXXDestructorDecl::anchor() { }
1728
Douglas Gregor42a552f2008-11-05 20:51:48 +00001729CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001730CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1731 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1732 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001733 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001734}
1735
1736CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001737CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001738 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001739 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001740 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001741 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001742 assert(NameInfo.getName().getNameKind()
1743 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001744 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001745 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001746 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001747}
1748
David Blaikie99ba9e32011-12-20 02:48:34 +00001749void CXXConversionDecl::anchor() { }
1750
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001751CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001752CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1753 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1754 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1755 QualType(), 0, false, false, false,
1756 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001757}
1758
1759CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001760CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001761 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001762 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001763 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001764 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001765 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001766 assert(NameInfo.getName().getNameKind()
1767 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001768 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001769 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001770 isInline, isExplicit, isConstexpr,
1771 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001772}
1773
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001774bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1775 return isImplicit() && getParent()->isLambda() &&
1776 getConversionType()->isBlockPointerType();
1777}
1778
David Blaikie99ba9e32011-12-20 02:48:34 +00001779void LinkageSpecDecl::anchor() { }
1780
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001781LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001782 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001783 SourceLocation ExternLoc,
1784 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001785 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001786 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001787 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001788}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001789
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001790LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1791 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1792 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1793 lang_c, SourceLocation());
1794}
1795
David Blaikie99ba9e32011-12-20 02:48:34 +00001796void UsingDirectiveDecl::anchor() { }
1797
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001798UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1799 SourceLocation L,
1800 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001801 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001802 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001803 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001804 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001805 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1806 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001807 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1808 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001809}
1810
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001811UsingDirectiveDecl *
1812UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1813 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1814 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1815 NestedNameSpecifierLoc(),
1816 SourceLocation(), 0, 0);
1817}
1818
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001819NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1820 if (NamespaceAliasDecl *NA =
1821 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1822 return NA->getNamespace();
1823 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1824}
1825
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001826void NamespaceDecl::anchor() { }
1827
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001828NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1829 SourceLocation StartLoc,
1830 SourceLocation IdLoc, IdentifierInfo *Id,
1831 NamespaceDecl *PrevDecl)
1832 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1833 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1834{
1835 setPreviousDeclaration(PrevDecl);
1836
1837 if (PrevDecl)
1838 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1839}
1840
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001841NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001842 bool Inline, SourceLocation StartLoc,
1843 SourceLocation IdLoc, IdentifierInfo *Id,
1844 NamespaceDecl *PrevDecl) {
1845 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001846}
1847
1848NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1849 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001850 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1851 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001852}
1853
David Blaikie99ba9e32011-12-20 02:48:34 +00001854void NamespaceAliasDecl::anchor() { }
1855
Mike Stump1eb44332009-09-09 15:08:12 +00001856NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001857 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001858 SourceLocation AliasLoc,
1859 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001860 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001861 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001862 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001863 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1864 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001865 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1866 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001867}
1868
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001869NamespaceAliasDecl *
1870NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1871 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1872 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1873 NestedNameSpecifierLoc(),
1874 SourceLocation(), 0);
1875}
1876
David Blaikie99ba9e32011-12-20 02:48:34 +00001877void UsingShadowDecl::anchor() { }
1878
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001879UsingShadowDecl *
1880UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1881 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1882 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1883}
1884
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001885UsingDecl *UsingShadowDecl::getUsingDecl() const {
1886 const UsingShadowDecl *Shadow = this;
1887 while (const UsingShadowDecl *NextShadow =
1888 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1889 Shadow = NextShadow;
1890 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1891}
1892
David Blaikie99ba9e32011-12-20 02:48:34 +00001893void UsingDecl::anchor() { }
1894
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001895void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1896 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1897 "declaration already in set");
1898 assert(S->getUsingDecl() == this);
1899
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001900 if (FirstUsingShadow.getPointer())
1901 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1902 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001903}
1904
1905void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1906 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1907 "declaration not in set");
1908 assert(S->getUsingDecl() == this);
1909
1910 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1911
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001912 if (FirstUsingShadow.getPointer() == S) {
1913 FirstUsingShadow.setPointer(
1914 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001915 S->UsingOrNextShadow = this;
1916 return;
1917 }
1918
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001919 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001920 while (Prev->UsingOrNextShadow != S)
1921 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1922 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1923 S->UsingOrNextShadow = this;
1924}
1925
Douglas Gregordc355712011-02-25 00:36:19 +00001926UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1927 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001928 const DeclarationNameInfo &NameInfo,
1929 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001930 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001931}
1932
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001933UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1934 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1935 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1936 DeclarationNameInfo(), false);
1937}
1938
David Blaikie99ba9e32011-12-20 02:48:34 +00001939void UnresolvedUsingValueDecl::anchor() { }
1940
John McCall7ba107a2009-11-18 02:36:19 +00001941UnresolvedUsingValueDecl *
1942UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1943 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001944 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001945 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001946 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001947 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001948}
1949
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001950UnresolvedUsingValueDecl *
1951UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1952 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1953 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1954 NestedNameSpecifierLoc(),
1955 DeclarationNameInfo());
1956}
1957
David Blaikie99ba9e32011-12-20 02:48:34 +00001958void UnresolvedUsingTypenameDecl::anchor() { }
1959
John McCall7ba107a2009-11-18 02:36:19 +00001960UnresolvedUsingTypenameDecl *
1961UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1962 SourceLocation UsingLoc,
1963 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001964 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001965 SourceLocation TargetNameLoc,
1966 DeclarationName TargetName) {
1967 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001968 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001969 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001970}
1971
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001972UnresolvedUsingTypenameDecl *
1973UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1974 void *Mem = AllocateDeserializedDecl(C, ID,
1975 sizeof(UnresolvedUsingTypenameDecl));
1976 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
1977 SourceLocation(),
1978 NestedNameSpecifierLoc(),
1979 SourceLocation(),
1980 0);
1981}
1982
David Blaikie99ba9e32011-12-20 02:48:34 +00001983void StaticAssertDecl::anchor() { }
1984
Anders Carlssonfb311762009-03-14 00:25:26 +00001985StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001986 SourceLocation StaticAssertLoc,
1987 Expr *AssertExpr,
1988 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00001989 SourceLocation RParenLoc,
1990 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001991 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00001992 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00001993}
1994
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001995StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
1996 unsigned ID) {
1997 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00001998 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
1999 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002000}
2001
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002002static const char *getAccessName(AccessSpecifier AS) {
2003 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002004 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002005 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002006 case AS_public:
2007 return "public";
2008 case AS_private:
2009 return "private";
2010 case AS_protected:
2011 return "protected";
2012 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002013 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002014}
2015
2016const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2017 AccessSpecifier AS) {
2018 return DB << getAccessName(AS);
2019}
Richard Smithf15fda02012-02-02 01:16:57 +00002020
2021const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2022 AccessSpecifier AS) {
2023 return DB << getAccessName(AS);
2024}