blob: dfcc154d98539d915dfc5b72ce5b2621d0ad51d9 [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 Smith7d04d3a2012-11-30 05:11:39 +000045 HasTrivialSpecialMembers(SMF_All),
Richard Smithac713512012-12-08 02:53:02 +000046 DeclaredNonTrivialSpecialMembers(0),
Richard Smith7d04d3a2012-11-30 05:11:39 +000047 HasIrrelevantDestructor(true),
Richard Smith61802452011-12-22 02:22:31 +000048 HasConstexprNonCopyMoveConstructor(false),
49 DefaultedDefaultConstructorIsConstexpr(true),
Richard Smith7d04d3a2012-11-30 05:11:39 +000050 HasConstexprDefaultConstructor(false),
Sean Hunt023df372011-05-09 18:22:59 +000051 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000052 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smithacf796b2012-11-28 06:23:12 +000053 ImplicitCopyConstructorHasConstParam(true),
54 ImplicitCopyAssignmentHasConstParam(true),
55 HasDeclaredCopyConstructorWithConstParam(false),
56 HasDeclaredCopyAssignmentWithConstParam(false),
57 FailedImplicitMoveConstructor(false), FailedImplicitMoveAssignment(false),
58 IsLambda(false), NumBases(0), NumVBases(0), Bases(), VBases(),
59 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000060}
61
Benjamin Krameree3096a2012-07-04 17:03:33 +000062CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
63 return Bases.get(Definition->getASTContext().getExternalSource());
64}
65
66CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
67 return VBases.get(Definition->getASTContext().getExternalSource());
68}
69
John McCall86ff3082010-02-04 22:26:26 +000070CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000071 SourceLocation StartLoc, SourceLocation IdLoc,
72 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
73 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000074 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000075 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000076
Jay Foad4ba2a172011-01-12 09:06:06 +000077CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000078 DeclContext *DC, SourceLocation StartLoc,
79 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000080 CXXRecordDecl* PrevDecl,
81 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000082 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
83 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000084
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000085 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000086 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000087 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000088 return R;
89}
90
Douglas Gregorda8962a2012-02-13 15:44:47 +000091CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Eli Friedman8da8a662012-09-19 01:18:11 +000092 TypeSourceInfo *Info, SourceLocation Loc,
93 bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +000094 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
95 0, 0);
96 R->IsBeingDefined = true;
Eli Friedman8da8a662012-09-19 01:18:11 +000097 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info, Dependent);
Douglas Gregorda8962a2012-02-13 15:44:47 +000098 C.getTypeDeclType(R, /*PrevDecl=*/0);
99 return R;
100}
101
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000102CXXRecordDecl *
103CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
104 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
105 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
106 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000107}
108
Mike Stump1eb44332009-09-09 15:08:12 +0000109void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000110CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000111 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000112 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000113
Douglas Gregor7c789c12010-10-29 22:39:52 +0000114 if (!data().Bases.isOffset() && data().NumBases > 0)
115 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Richard Smithdd677232011-10-18 20:08:55 +0000117 if (NumBases) {
118 // C++ [dcl.init.aggr]p1:
119 // An aggregate is [...] a class with [...] no base classes [...].
120 data().Aggregate = false;
121
122 // C++ [class]p4:
123 // A POD-struct is an aggregate class...
124 data().PlainOldData = false;
125 }
126
Anders Carlsson6f6de732010-03-29 05:13:12 +0000127 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000128 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000129
130 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000131 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
John McCall86ff3082010-02-04 22:26:26 +0000133 data().Bases = new(C) CXXBaseSpecifier [NumBases];
134 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000135 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000136 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000137 // Keep track of inherited vbases for this base class.
138 const CXXBaseSpecifier *Base = Bases[i];
139 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000140 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000141 if (BaseType->isDependentType())
142 continue;
143 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000144 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000145
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000146 // A class with a non-empty base class is not empty.
147 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000148 if (!BaseClassDecl->isEmpty()) {
149 if (!data().Empty) {
150 // C++0x [class]p7:
151 // A standard-layout class is a class that:
152 // [...]
153 // -- either has no non-static data members in the most derived
154 // class and at most one base class with non-static data members,
155 // or has no base classes with non-static data members, and
156 // If this is the second non-empty base, then neither of these two
157 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000158 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000159 }
160
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000161 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000162 data().HasNoNonEmptyBases = false;
163 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000164
Douglas Gregor85606eb2010-09-28 20:50:54 +0000165 // C++ [class.virtual]p1:
166 // A class that declares or inherits a virtual function is called a
167 // polymorphic class.
168 if (BaseClassDecl->isPolymorphic())
169 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000170
Chandler Carrutha8225442011-04-30 09:17:45 +0000171 // C++0x [class]p7:
172 // A standard-layout class is a class that: [...]
173 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000174 if (!BaseClassDecl->isStandardLayout())
175 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000176
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000177 // Record if this base is the first non-literal field or base.
178 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
179 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000180
Anders Carlsson6f6de732010-03-29 05:13:12 +0000181 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000182 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000183 BaseClassDecl->vbases_begin(),
184 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000185 // Add this base if it's not already in the list.
Richard Smithacf796b2012-11-28 06:23:12 +0000186 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000187 VBases.push_back(VBase);
Richard Smithacf796b2012-11-28 06:23:12 +0000188
189 // C++11 [class.copy]p8:
190 // The implicitly-declared copy constructor for a class X will have
191 // the form 'X::X(const X&)' if each [...] virtual base class B of X
192 // has a copy constructor whose first parameter is of type
193 // 'const B&' or 'const volatile B&' [...]
194 if (CXXRecordDecl *VBaseDecl = VBase->getType()->getAsCXXRecordDecl())
195 if (!VBaseDecl->hasCopyConstructorWithConstParam())
196 data().ImplicitCopyConstructorHasConstParam = false;
197 }
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000198 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000199
200 if (Base->isVirtual()) {
201 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000202 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Richard Smithacf796b2012-11-28 06:23:12 +0000203 VBases.push_back(Base);
204
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000205 // C++0x [meta.unary.prop] is_empty:
206 // T is a class type, but not a union type, with ... no virtual base
207 // classes
208 data().Empty = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000209
Richard Smith7d04d3a2012-11-30 05:11:39 +0000210 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
211 // A [default constructor, copy/move constructor, or copy/move assignment
212 // operator for a class X] is trivial [...] if:
213 // -- class X has [...] no virtual base classes
214 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carrutha8225442011-04-30 09:17:45 +0000215
216 // C++0x [class]p7:
217 // A standard-layout class is a class that: [...]
218 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000219 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000220
221 // C++11 [dcl.constexpr]p4:
222 // In the definition of a constexpr constructor [...]
223 // -- the class shall not have any virtual base classes
224 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000225 } else {
226 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000227 // A default constructor is trivial [...] if:
228 // -- all the direct base classes of its class have trivial default
229 // constructors.
230 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000231 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
232
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000233 // C++0x [class.copy]p13:
234 // A copy/move constructor for class X is trivial if [...]
235 // [...]
236 // -- the constructor selected to copy/move each direct base class
237 // subobject is trivial, and
238 // FIXME: C++0x: We need to only consider the selected constructor
Richard Smith93af2b82012-11-14 07:36:28 +0000239 // instead of all of them. For now, we treat a move constructor as being
240 // non-trivial if it calls anything other than a trivial move constructor.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000241 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000242 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith426391c2012-11-16 00:53:38 +0000243 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000244 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000245
246 // C++0x [class.copy]p27:
247 // A copy/move assignment operator for class X is trivial if [...]
248 // [...]
249 // -- the assignment operator selected to copy/move each direct base
250 // class subobject is trivial, and
251 // FIXME: C++0x: We need to only consider the selected operator instead
252 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000253 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000254 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith426391c2012-11-16 00:53:38 +0000255 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000256 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smith61802452011-12-22 02:22:31 +0000257
258 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000259 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000260 // requirements of a constexpr constructor, the implicitly-defined
261 // default constructor is constexpr.
262 if (!BaseClassDecl->hasConstexprDefaultConstructor())
263 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000264 }
Richard Smithac713512012-12-08 02:53:02 +0000265
Douglas Gregor85606eb2010-09-28 20:50:54 +0000266 // C++ [class.ctor]p3:
267 // A destructor is trivial if all the direct base classes of its class
268 // have trivial destructors.
269 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000270 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000271
272 if (!BaseClassDecl->hasIrrelevantDestructor())
273 data().HasIrrelevantDestructor = false;
274
Richard Smithacf796b2012-11-28 06:23:12 +0000275 // C++11 [class.copy]p18:
276 // The implicitly-declared copy assignment oeprator for a class X will
277 // have the form 'X& X::operator=(const X&)' if each direct base class B
278 // of X has a copy assignment operator whose parameter is of type 'const
279 // B&', 'const volatile B&', or 'B' [...]
280 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
281 data().ImplicitCopyAssignmentHasConstParam = false;
282
283 // C++11 [class.copy]p8:
284 // The implicitly-declared copy constructor for a class X will have
285 // the form 'X::X(const X&)' if each direct [...] base class B of X
286 // has a copy constructor whose first parameter is of type
287 // 'const B&' or 'const volatile B&' [...]
288 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
289 data().ImplicitCopyConstructorHasConstParam = false;
290
John McCallf85e1932011-06-15 23:02:42 +0000291 // A class has an Objective-C object member if... or any of its bases
292 // has an Objective-C object member.
293 if (BaseClassDecl->hasObjectMember())
294 setHasObjectMember(true);
295
Douglas Gregor2bb11012011-05-13 01:05:07 +0000296 // Keep track of the presence of mutable fields.
297 if (BaseClassDecl->hasMutableFields())
298 data().HasMutableFields = true;
Richard Smithd5bc8672012-12-08 02:01:17 +0000299
300 if (BaseClassDecl->hasUninitializedReferenceMember())
301 data().HasUninitializedReferenceMember = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000302 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000303
304 if (VBases.empty())
305 return;
306
307 // Create base specifier for any direct or indirect virtual bases.
308 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
309 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000310 for (int I = 0, E = VBases.size(); I != E; ++I)
311 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000312}
313
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000314/// Callback function for CXXRecordDecl::forallBases that acknowledges
315/// that it saw a base class.
316static bool SawBase(const CXXRecordDecl *, void *) {
317 return true;
318}
319
320bool CXXRecordDecl::hasAnyDependentBases() const {
321 if (!isDependentContext())
322 return false;
323
324 return !forallBases(SawBase, 0);
325}
326
Sean Huntffe37fd2011-05-25 20:50:04 +0000327bool CXXRecordDecl::hasConstCopyConstructor() const {
328 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000329}
330
Chandler Carruthb7e95892011-04-23 10:47:28 +0000331bool CXXRecordDecl::isTriviallyCopyable() const {
332 // C++0x [class]p5:
333 // A trivially copyable class is a class that:
334 // -- has no non-trivial copy constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000335 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000336 // -- has no non-trivial move constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000337 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000338 // -- has no non-trivial copy assignment operators,
Richard Smith426391c2012-11-16 00:53:38 +0000339 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000340 // -- has no non-trivial move assignment operators, and
Richard Smith426391c2012-11-16 00:53:38 +0000341 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000342 // -- has a trivial destructor.
343 if (!hasTrivialDestructor()) return false;
344
345 return true;
346}
347
Douglas Gregor0d405db2010-07-01 20:59:04 +0000348/// \brief Perform a simplistic form of overload resolution that only considers
349/// cv-qualifiers on a single parameter, and return the best overload candidate
350/// (if there is one).
351static CXXMethodDecl *
352GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000353 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000354 if (Cands.empty())
355 return 0;
356 if (Cands.size() == 1)
357 return Cands[0].first;
358
359 unsigned Best = 0, N = Cands.size();
360 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000361 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000362 Best = I;
363
Benjamin Kramer2cd7f412012-07-30 15:53:26 +0000364 for (unsigned I = 0; I != N; ++I)
365 if (I != Best && Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000366 return 0;
367
368 return Cands[Best].first;
369}
370
Sean Huntffe37fd2011-05-25 20:50:04 +0000371CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
372 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000373 QualType ClassType
374 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000375 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000376 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000377 Context.getCanonicalType(ClassType));
378 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000379 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000380 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000381 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000382 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000383 // C++ [class.copy]p2:
384 // A non-template constructor for class X is a copy constructor if [...]
385 if (isa<FunctionTemplateDecl>(*Con))
386 continue;
387
Douglas Gregor0d405db2010-07-01 20:59:04 +0000388 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
389 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000390 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
391 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000392 Found.push_back(std::make_pair(
393 const_cast<CXXConstructorDecl *>(Constructor),
394 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000395 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000396 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000397
398 return cast_or_null<CXXConstructorDecl>(
399 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000400}
401
Sean Huntffe37fd2011-05-25 20:50:04 +0000402CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
403 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
404 if (I->isMoveConstructor())
David Blaikie581deb32012-06-06 20:45:41 +0000405 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000406
407 return 0;
408}
409
Douglas Gregorb87786f2010-07-01 17:48:08 +0000410CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
411 ASTContext &Context = getASTContext();
412 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
413 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
414
Chris Lattner5f9e2722011-07-23 10:55:15 +0000415 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000416 DeclContext::lookup_const_iterator Op, OpEnd;
417 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
418 // C++ [class.copy]p9:
419 // A user-declared copy assignment operator is a non-static non-template
420 // member function of class X with exactly one parameter of type X, X&,
421 // const X&, volatile X& or const volatile X&.
422 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
423 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
424 continue;
425
426 const FunctionProtoType *FnType
427 = Method->getType()->getAs<FunctionProtoType>();
428 assert(FnType && "Overloaded operator has no prototype.");
429 // Don't assert on this; an invalid decl might have been left in the AST.
430 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
431 continue;
432
433 QualType ArgType = FnType->getArgType(0);
434 Qualifiers Quals;
435 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
436 ArgType = Ref->getPointeeType();
437 // If we have a const argument and we have a reference to a non-const,
438 // this function does not match.
439 if (ArgIsConst && !ArgType.isConstQualified())
440 continue;
441
442 Quals = ArgType.getQualifiers();
443 } else {
444 // By-value copy-assignment operators are treated like const X&
445 // copy-assignment operators.
446 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
447 }
448
449 if (!Context.hasSameUnqualifiedType(ArgType, Class))
450 continue;
451
452 // Save this copy-assignment operator. It might be "the one".
453 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
454 }
455
456 // Use a simplistic form of overload resolution to find the candidate.
457 return GetBestOverloadCandidateSimple(Found);
458}
459
Sean Huntffe37fd2011-05-25 20:50:04 +0000460CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
461 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
462 if (I->isMoveAssignmentOperator())
David Blaikie581deb32012-06-06 20:45:41 +0000463 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000464
465 return 0;
466}
467
Douglas Gregor21386642010-09-28 21:55:22 +0000468void CXXRecordDecl::markedVirtualFunctionPure() {
469 // C++ [class.abstract]p2:
470 // A class is abstract if it has at least one pure virtual function.
471 data().Abstract = true;
472}
473
474void CXXRecordDecl::addedMember(Decl *D) {
Joao Matos17d35c32012-08-31 22:18:20 +0000475 if (!D->isImplicit() &&
476 !isa<FieldDecl>(D) &&
477 !isa<IndirectFieldDecl>(D) &&
478 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
479 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
480 data().HasOnlyCMembers = false;
481
482 // Ignore friends and invalid declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000483 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000484 return;
485
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000486 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
487 if (FunTmpl)
488 D = FunTmpl->getTemplatedDecl();
489
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000490 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
491 if (Method->isVirtual()) {
492 // C++ [dcl.init.aggr]p1:
493 // An aggregate is an array or a class with [...] no virtual functions.
494 data().Aggregate = false;
495
496 // C++ [class]p4:
497 // A POD-struct is an aggregate class...
498 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000499
500 // Virtual functions make the class non-empty.
501 // FIXME: Standard ref?
502 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000503
504 // C++ [class.virtual]p1:
505 // A class that declares or inherits a virtual function is called a
506 // polymorphic class.
507 data().Polymorphic = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000508
Richard Smith7d04d3a2012-11-30 05:11:39 +0000509 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
510 // A [default constructor, copy/move constructor, or copy/move
511 // assignment operator for a class X] is trivial [...] if:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000512 // -- class X has no virtual functions [...]
Richard Smith7d04d3a2012-11-30 05:11:39 +0000513 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000514
Chandler Carrutha8225442011-04-30 09:17:45 +0000515 // C++0x [class]p7:
516 // A standard-layout class is a class that: [...]
517 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000518 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000519 }
520 }
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000521
Richard Smithacf796b2012-11-28 06:23:12 +0000522 // Notify the listener if an implicit member was added after the definition
523 // was completed.
524 if (!isBeingDefined() && D->isImplicit())
525 if (ASTMutationListener *L = getASTMutationListener())
526 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000527
Richard Smith7d04d3a2012-11-30 05:11:39 +0000528 // The kind of special member this declaration is, if any.
529 unsigned SMKind = 0;
530
Richard Smithacf796b2012-11-28 06:23:12 +0000531 // Handle constructors.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000532 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smithacf796b2012-11-28 06:23:12 +0000533 if (!Constructor->isImplicit()) {
534 // Note that we have a user-declared constructor.
535 data().UserDeclaredConstructor = true;
536
537 // C++ [class]p4:
538 // A POD-struct is an aggregate class [...]
539 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
540 // type is technically an aggregate in C++0x since it wouldn't be in 03.
541 data().PlainOldData = false;
542 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000543
Richard Smith017ab772011-09-05 02:13:09 +0000544 // Technically, "user-provided" is only defined for special member
545 // functions, but the intent of the standard is clearly that it should apply
546 // to all functions.
547 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000548
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000549 if (Constructor->isDefaultConstructor()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000550 SMKind |= SMF_DefaultConstructor;
551
552 if (UserProvided)
Sean Huntcdee3fe2011-05-11 22:34:38 +0000553 data().UserProvidedDefaultConstructor = true;
Richard Smithacf796b2012-11-28 06:23:12 +0000554 if (Constructor->isConstexpr())
Richard Smith61802452011-12-22 02:22:31 +0000555 data().HasConstexprDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000556 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000557
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000558 if (!FunTmpl) {
Richard Smithacf796b2012-11-28 06:23:12 +0000559 unsigned Quals;
560 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000561 SMKind |= SMF_CopyConstructor;
Richard Smithacf796b2012-11-28 06:23:12 +0000562
563 if (Quals & Qualifiers::Const)
564 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith7d04d3a2012-11-30 05:11:39 +0000565 } else if (Constructor->isMoveConstructor())
566 SMKind |= SMF_MoveConstructor;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000567 }
Richard Smithacf796b2012-11-28 06:23:12 +0000568
569 // Record if we see any constexpr constructors which are neither copy
570 // nor move constructors.
571 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith6b8bc072011-08-10 18:11:37 +0000572 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000573
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000574 // C++ [dcl.init.aggr]p1:
575 // An aggregate is an array or a class with no user-declared
576 // constructors [...].
577 // C++0x [dcl.init.aggr]p1:
578 // An aggregate is an array or a class with no user-provided
579 // constructors [...].
Richard Smithacf796b2012-11-28 06:23:12 +0000580 if (getASTContext().getLangOpts().CPlusPlus0x
581 ? UserProvided : !Constructor->isImplicit())
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000582 data().Aggregate = false;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000583 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000584
Richard Smithacf796b2012-11-28 06:23:12 +0000585 // Handle destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000586 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000587 SMKind |= SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000588
Richard Smith7d04d3a2012-11-30 05:11:39 +0000589 if (!DD->isImplicit())
Richard Smithacf796b2012-11-28 06:23:12 +0000590 data().HasIrrelevantDestructor = false;
591
Richard Smithacf796b2012-11-28 06:23:12 +0000592 // C++11 [class.dtor]p5:
Richard Smith7d04d3a2012-11-30 05:11:39 +0000593 // A destructor is trivial if [...] the destructor is not virtual.
594 if (DD->isVirtual())
595 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000596 }
Richard Smithacf796b2012-11-28 06:23:12 +0000597
598 // Handle member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000599 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000600 if (Method->isCopyAssignmentOperator()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000601 SMKind |= SMF_CopyAssignment;
Richard Smithacf796b2012-11-28 06:23:12 +0000602
603 const ReferenceType *ParamTy =
604 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
605 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
606 data().HasDeclaredCopyAssignmentWithConstParam = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000607 }
Sean Huntffe37fd2011-05-25 20:50:04 +0000608
Richard Smith7d04d3a2012-11-30 05:11:39 +0000609 if (Method->isMoveAssignmentOperator())
610 SMKind |= SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000611
Douglas Gregore80622f2010-09-29 04:25:11 +0000612 // Keep the list of conversion functions up-to-date.
613 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregore80622f2010-09-29 04:25:11 +0000614 // FIXME: We intentionally don't use the decl's access here because it
615 // hasn't been set yet. That's really just a misdesign in Sema.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000616 if (Conversion->getPrimaryTemplate()) {
617 // We don't record specializations.
618 } else if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000619 if (FunTmpl->getPreviousDecl())
620 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000621 FunTmpl);
622 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000623 data().Conversions.addDecl(getASTContext(), FunTmpl);
Douglas Gregore80622f2010-09-29 04:25:11 +0000624 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000625 if (Conversion->getPreviousDecl())
626 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000627 Conversion);
628 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000629 data().Conversions.addDecl(getASTContext(), Conversion);
Douglas Gregore80622f2010-09-29 04:25:11 +0000630 }
631 }
Richard Smithacf796b2012-11-28 06:23:12 +0000632
Richard Smith7d04d3a2012-11-30 05:11:39 +0000633 if (SMKind) {
Richard Smithac713512012-12-08 02:53:02 +0000634 // If this is the first declaration of a special member, we no longer have
635 // an implicit trivial special member.
636 data().HasTrivialSpecialMembers &=
637 data().DeclaredSpecialMembers | ~SMKind;
638
639 if (!Method->isImplicit() && !Method->isUserProvided()) {
640 // This method is user-declared but not user-provided. We can't work out
641 // whether it's trivial yet (not until we get to the end of the class).
642 // We'll handle this method in finishedDefaultedOrDeletedMember.
643 } else if (Method->isTrivial())
644 data().HasTrivialSpecialMembers |= SMKind;
645 else
646 data().DeclaredNonTrivialSpecialMembers |= SMKind;
647
Richard Smith7d04d3a2012-11-30 05:11:39 +0000648 // Note when we have declared a declared special member, and suppress the
649 // implicit declaration of this special member.
650 data().DeclaredSpecialMembers |= SMKind;
651
652 if (!Method->isImplicit()) {
653 data().UserDeclaredSpecialMembers |= SMKind;
654
655 // C++03 [class]p4:
656 // A POD-struct is an aggregate class that has [...] no user-defined
657 // copy assignment operator and no user-defined destructor.
658 //
659 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
660 // aggregates could not have any constructors, clear it even for an
661 // explicitly defaulted or deleted constructor.
662 // type is technically an aggregate in C++0x since it wouldn't be in 03.
663 //
664 // Also, a user-declared move assignment operator makes a class non-POD.
665 // This is an extension in C++03.
666 data().PlainOldData = false;
667 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000668 }
669
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000670 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000671 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000672
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000673 // Handle non-static data members.
674 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000675 // C++ [class.bit]p2:
676 // A declaration for a bit-field that omits the identifier declares an
677 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
678 // initialized.
679 if (Field->isUnnamedBitfield())
680 return;
681
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000682 // C++ [dcl.init.aggr]p1:
683 // An aggregate is an array or a class (clause 9) with [...] no
684 // private or protected non-static data members (clause 11).
685 //
686 // A POD must be an aggregate.
687 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
688 data().Aggregate = false;
689 data().PlainOldData = false;
690 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000691
692 // C++0x [class]p7:
693 // A standard-layout class is a class that:
694 // [...]
695 // -- has the same access control for all non-static data members,
696 switch (D->getAccess()) {
697 case AS_private: data().HasPrivateFields = true; break;
698 case AS_protected: data().HasProtectedFields = true; break;
699 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000700 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000701 };
702 if ((data().HasPrivateFields + data().HasProtectedFields +
703 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000704 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000705
Douglas Gregor2bb11012011-05-13 01:05:07 +0000706 // Keep track of the presence of mutable fields.
707 if (Field->isMutable())
708 data().HasMutableFields = true;
709
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000710 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000711 // A POD struct is a class that is both a trivial class and a
712 // standard-layout class, and has no non-static data members of type
713 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000714 //
715 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
716 // that does not explicitly have no lifetime makes the class a non-POD.
717 // However, we delay setting PlainOldData to false in this case so that
718 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000719 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000720 // In this case, the class will become a non-POD class when we complete
721 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000722 ASTContext &Context = getASTContext();
723 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000724 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000725 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000726 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
727 setHasObjectMember(true);
728 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000729 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000730
Chandler Carrutha8225442011-04-30 09:17:45 +0000731 if (T->isReferenceType()) {
Richard Smithd5bc8672012-12-08 02:01:17 +0000732 if (!Field->hasInClassInitializer())
733 data().HasUninitializedReferenceMember = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000734
Chandler Carrutha8225442011-04-30 09:17:45 +0000735 // C++0x [class]p7:
736 // A standard-layout class is a class that:
737 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000738 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000739 }
740
Richard Smith86c3ae42012-02-13 03:54:03 +0000741 // Record if this field is the first non-literal or volatile field or base.
742 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000743 data().HasNonLiteralTypeFieldsOrBases = true;
744
Richard Smith7a614d82011-06-11 17:19:42 +0000745 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000746 data().HasInClassInitializer = true;
747
748 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000749 // A default constructor is trivial if [...] no non-static data member
750 // of its class has a brace-or-equal-initializer.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000751 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith7a614d82011-06-11 17:19:42 +0000752
Richard Smithd079abf2012-05-07 01:07:30 +0000753 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000754 // An aggregate is a [...] class with [...] no
755 // brace-or-equal-initializers for non-static data members.
756 data().Aggregate = false;
757
Richard Smithd079abf2012-05-07 01:07:30 +0000758 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000759 // A POD struct is [...] a trivial class.
760 data().PlainOldData = false;
761 }
762
Douglas Gregor85606eb2010-09-28 20:50:54 +0000763 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
764 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
765 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000766 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000767 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000768 // -- for all the non-static data members of its class that are of
769 // class type (or array thereof), each such class has a trivial
770 // default constructor.
771 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000772 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000773
774 // C++0x [class.copy]p13:
775 // A copy/move constructor for class X is trivial if [...]
776 // [...]
777 // -- for each non-static data member of X that is of class type (or
778 // an array thereof), the constructor selected to copy/move that
779 // member is trivial;
780 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000781 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000782 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith426391c2012-11-16 00:53:38 +0000783 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000784 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000785
786 // C++0x [class.copy]p27:
787 // A copy/move assignment operator for class X is trivial if [...]
788 // [...]
789 // -- for each non-static data member of X that is of class type (or
790 // an array thereof), the assignment operator selected to
791 // copy/move that member is trivial;
792 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000793 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000794 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith426391c2012-11-16 00:53:38 +0000795 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000796 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000797
Douglas Gregor85606eb2010-09-28 20:50:54 +0000798 if (!FieldRec->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000799 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000800 if (!FieldRec->hasIrrelevantDestructor())
801 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000802 if (FieldRec->hasObjectMember())
803 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000804
805 // C++0x [class]p7:
806 // A standard-layout class is a class that:
807 // -- has no non-static data members of type non-standard-layout
808 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000809 if (!FieldRec->isStandardLayout())
810 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000811
812 // C++0x [class]p7:
813 // A standard-layout class is a class that:
814 // [...]
815 // -- has no base classes of the same type as the first non-static
816 // data member.
817 // We don't want to expend bits in the state of the record decl
818 // tracking whether this is the first non-static data member so we
819 // cheat a bit and use some of the existing state: the empty bit.
820 // Virtual bases and virtual methods make a class non-empty, but they
821 // also make it non-standard-layout so we needn't check here.
822 // A non-empty base class may leave the class standard-layout, but not
823 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000824 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000825 // data member must come through here with Empty still true, and Empty
826 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000827 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000828 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
829 BE = bases_end();
830 BI != BE; ++BI) {
831 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000832 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000833 break;
834 }
835 }
836 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000837
838 // Keep track of the presence of mutable fields.
839 if (FieldRec->hasMutableFields())
840 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000841
842 // C++11 [class.copy]p13:
843 // If the implicitly-defined constructor would satisfy the
844 // requirements of a constexpr constructor, the implicitly-defined
845 // constructor is constexpr.
846 // C++11 [dcl.constexpr]p4:
847 // -- every constructor involved in initializing non-static data
848 // members [...] shall be a constexpr constructor
849 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000850 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000851 // The standard requires any in-class initializer to be a constant
852 // expression. We consider this to be a defect.
853 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithacf796b2012-11-28 06:23:12 +0000854
855 // C++11 [class.copy]p8:
856 // The implicitly-declared copy constructor for a class X will have
857 // the form 'X::X(const X&)' if [...] for all the non-static data
858 // members of X that are of a class type M (or array thereof), each
859 // such class type has a copy constructor whose first parameter is
860 // of type 'const M&' or 'const volatile M&'.
861 if (!FieldRec->hasCopyConstructorWithConstParam())
862 data().ImplicitCopyConstructorHasConstParam = false;
863
864 // C++11 [class.copy]p18:
865 // The implicitly-declared copy assignment oeprator for a class X will
866 // have the form 'X& X::operator=(const X&)' if [...] for all the
867 // non-static data members of X that are of a class type M (or array
868 // thereof), each such class type has a copy assignment operator whose
869 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
870 if (!FieldRec->hasCopyAssignmentWithConstParam())
871 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smithd5bc8672012-12-08 02:01:17 +0000872
873 if (FieldRec->hasUninitializedReferenceMember() &&
874 !Field->hasInClassInitializer())
875 data().HasUninitializedReferenceMember = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000876 }
Richard Smith61802452011-12-22 02:22:31 +0000877 } else {
878 // Base element type of field is a non-class type.
Richard Smithd3861ce2012-06-10 07:07:24 +0000879 if (!T->isLiteralType() ||
880 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000881 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000882 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000883
884 // C++0x [class]p7:
885 // A standard-layout class is a class that:
886 // [...]
887 // -- either has no non-static data members in the most derived
888 // class and at most one base class with non-static data members,
889 // or has no base classes with non-static data members, and
890 // At this point we know that we have a non-static data member, so the last
891 // clause holds.
892 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000893 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000894
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000895 // If this is not a zero-length bit-field, then the class is not empty.
896 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000897 if (!Field->isBitField() ||
898 (!Field->getBitWidth()->isTypeDependent() &&
899 !Field->getBitWidth()->isValueDependent() &&
900 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000901 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000902 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000903 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000904
905 // Handle using declarations of conversion functions.
906 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
907 if (Shadow->getDeclName().getNameKind()
908 == DeclarationName::CXXConversionFunctionName)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000909 data().Conversions.addDecl(getASTContext(), Shadow, Shadow->getAccess());
Joao Matos17d35c32012-08-31 22:18:20 +0000910}
911
Richard Smithac713512012-12-08 02:53:02 +0000912void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
913 assert(!D->isImplicit() && !D->isUserProvided());
914
915 // The kind of special member this declaration is, if any.
916 unsigned SMKind = 0;
917
918 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
919 if (Constructor->isDefaultConstructor()) {
920 SMKind |= SMF_DefaultConstructor;
921 if (Constructor->isConstexpr())
922 data().HasConstexprDefaultConstructor = true;
923 }
924 if (Constructor->isCopyConstructor())
925 SMKind |= SMF_CopyConstructor;
926 else if (Constructor->isMoveConstructor())
927 SMKind |= SMF_MoveConstructor;
928 else if (Constructor->isConstexpr())
929 // We may now know that the constructor is constexpr.
930 data().HasConstexprNonCopyMoveConstructor = true;
931 } else if (isa<CXXDestructorDecl>(D))
932 SMKind |= SMF_Destructor;
933 else if (D->isCopyAssignmentOperator())
934 SMKind |= SMF_CopyAssignment;
935 else if (D->isMoveAssignmentOperator())
936 SMKind |= SMF_MoveAssignment;
937
938 // Update which trivial / non-trivial special members we have.
939 // addedMember will have skipped this step for this member.
940 if (D->isTrivial())
941 data().HasTrivialSpecialMembers |= SMKind;
942 else
943 data().DeclaredNonTrivialSpecialMembers |= SMKind;
944}
945
Joao Matos17d35c32012-08-31 22:18:20 +0000946bool CXXRecordDecl::isCLike() const {
947 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
948 !TemplateOrInstantiation.isNull())
949 return false;
950 if (!hasDefinition())
951 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000952
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000953 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000954}
955
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000956void CXXRecordDecl::getCaptureFields(
957 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000958 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000959 Captures.clear();
960 ThisCapture = 0;
961
Douglas Gregorda8962a2012-02-13 15:44:47 +0000962 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000963 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000964 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000965 C != CEnd; ++C, ++Field) {
966 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +0000967 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000968 continue;
969 }
970
David Blaikie581deb32012-06-06 20:45:41 +0000971 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000972 }
973}
974
975
John McCallb05b5f32010-03-15 09:07:48 +0000976static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
977 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000978 if (isa<UsingShadowDecl>(Conv))
979 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000980 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
981 T = ConvTemp->getTemplatedDecl()->getResultType();
982 else
983 T = cast<CXXConversionDecl>(Conv)->getConversionType();
984 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000985}
986
John McCallb05b5f32010-03-15 09:07:48 +0000987/// Collect the visible conversions of a base class.
988///
James Dennetta1253502012-06-15 22:28:09 +0000989/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +0000990/// \param InVirtual whether this base class is a virtual base (or a base
991/// of a virtual base)
992/// \param Access the access along the inheritance path to this base
993/// \param ParentHiddenTypes the conversions provided by the inheritors
994/// of this base
995/// \param Output the set to which to add conversions from non-virtual bases
996/// \param VOutput the set to which to add conversions from virtual bases
997/// \param HiddenVBaseCs the set of conversions which were hidden in a
998/// virtual base along some inheritance path
999static void CollectVisibleConversions(ASTContext &Context,
1000 CXXRecordDecl *Record,
1001 bool InVirtual,
1002 AccessSpecifier Access,
1003 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001004 ASTUnresolvedSet &Output,
John McCallb05b5f32010-03-15 09:07:48 +00001005 UnresolvedSetImpl &VOutput,
1006 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1007 // The set of types which have conversions in this class or its
1008 // subclasses. As an optimization, we don't copy the derived set
1009 // unless it might change.
1010 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1011 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1012
1013 // Collect the direct conversions and figure out which conversions
1014 // will be hidden in the subclasses.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001015 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1016 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1017 if (ConvI != ConvE) {
John McCallb05b5f32010-03-15 09:07:48 +00001018 HiddenTypesBuffer = ParentHiddenTypes;
1019 HiddenTypes = &HiddenTypesBuffer;
1020
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001021 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +00001022 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1023 bool Hidden = ParentHiddenTypes.count(ConvType);
1024 if (!Hidden)
1025 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001026
1027 // If this conversion is hidden and we're in a virtual base,
1028 // remember that it's hidden along some inheritance path.
1029 if (Hidden && InVirtual)
1030 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1031
1032 // If this conversion isn't hidden, add it to the appropriate output.
1033 else if (!Hidden) {
1034 AccessSpecifier IAccess
1035 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1036
1037 if (InVirtual)
1038 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001039 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001040 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001041 }
1042 }
1043 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001044
John McCallb05b5f32010-03-15 09:07:48 +00001045 // Collect information recursively from any base classes.
1046 for (CXXRecordDecl::base_class_iterator
1047 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1048 const RecordType *RT = I->getType()->getAs<RecordType>();
1049 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001050
John McCallb05b5f32010-03-15 09:07:48 +00001051 AccessSpecifier BaseAccess
1052 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1053 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001054
John McCallb05b5f32010-03-15 09:07:48 +00001055 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1056 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1057 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001058 }
John McCallb05b5f32010-03-15 09:07:48 +00001059}
Sebastian Redl9994a342009-10-25 17:03:50 +00001060
John McCallb05b5f32010-03-15 09:07:48 +00001061/// Collect the visible conversions of a class.
1062///
1063/// This would be extremely straightforward if it weren't for virtual
1064/// bases. It might be worth special-casing that, really.
1065static void CollectVisibleConversions(ASTContext &Context,
1066 CXXRecordDecl *Record,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001067 ASTUnresolvedSet &Output) {
John McCallb05b5f32010-03-15 09:07:48 +00001068 // The collection of all conversions in virtual bases that we've
1069 // found. These will be added to the output as long as they don't
1070 // appear in the hidden-conversions set.
1071 UnresolvedSet<8> VBaseCs;
1072
1073 // The set of conversions in virtual bases that we've determined to
1074 // be hidden.
1075 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1076
1077 // The set of types hidden by classes derived from this one.
1078 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1079
1080 // Go ahead and collect the direct conversions and add them to the
1081 // hidden-types set.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001082 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1083 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001084 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001085 for (; ConvI != ConvE; ++ConvI)
1086 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCallb05b5f32010-03-15 09:07:48 +00001087
1088 // Recursively collect conversions from base classes.
1089 for (CXXRecordDecl::base_class_iterator
1090 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1091 const RecordType *RT = I->getType()->getAs<RecordType>();
1092 if (!RT) continue;
1093
1094 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1095 I->isVirtual(), I->getAccessSpecifier(),
1096 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1097 }
1098
1099 // Add any unhidden conversions provided by virtual bases.
1100 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1101 I != E; ++I) {
1102 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001103 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001104 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001105}
1106
1107/// getVisibleConversionFunctions - get all conversion functions visible
1108/// in current class; including conversion function templates.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001109std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1110CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001111 // If root class, all conversions are visible.
1112 if (bases_begin() == bases_end())
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001113 return std::make_pair(data().Conversions.begin(), data().Conversions.end());
Fariborz Jahanian62509212009-09-12 18:26:03 +00001114 // If visible conversion list is already evaluated, return it.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001115 if (!data().ComputedVisibleConversions) {
1116 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
1117 data().ComputedVisibleConversions = true;
1118 }
1119 return std::make_pair(data().VisibleConversions.begin(),
1120 data().VisibleConversions.end());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001121}
1122
John McCall32daa422010-03-31 01:36:47 +00001123void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1124 // This operation is O(N) but extremely rare. Sema only uses it to
1125 // remove UsingShadowDecls in a class that were followed by a direct
1126 // declaration, e.g.:
1127 // class A : B {
1128 // using B::operator int;
1129 // operator int();
1130 // };
1131 // This is uncommon by itself and even more uncommon in conjunction
1132 // with sufficiently large numbers of directly-declared conversions
1133 // that asymptotic behavior matters.
1134
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001135 ASTUnresolvedSet &Convs = data().Conversions;
John McCall32daa422010-03-31 01:36:47 +00001136 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1137 if (Convs[I].getDecl() == ConvDecl) {
1138 Convs.erase(I);
1139 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1140 && "conversion was found multiple times in unresolved set");
1141 return;
1142 }
1143 }
1144
1145 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001146}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001147
Douglas Gregorf6b11852009-10-08 15:14:33 +00001148CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001149 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001150 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1151
1152 return 0;
1153}
1154
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001155MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1156 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1157}
1158
Douglas Gregorf6b11852009-10-08 15:14:33 +00001159void
1160CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1161 TemplateSpecializationKind TSK) {
1162 assert(TemplateOrInstantiation.isNull() &&
1163 "Previous template or instantiation?");
1164 assert(!isa<ClassTemplateSpecializationDecl>(this));
1165 TemplateOrInstantiation
1166 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1167}
1168
Anders Carlssonb13e3572009-12-07 06:33:48 +00001169TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1170 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001171 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1172 return Spec->getSpecializationKind();
1173
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001174 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001175 return MSInfo->getTemplateSpecializationKind();
1176
1177 return TSK_Undeclared;
1178}
1179
1180void
1181CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1182 if (ClassTemplateSpecializationDecl *Spec
1183 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1184 Spec->setSpecializationKind(TSK);
1185 return;
1186 }
1187
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001188 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001189 MSInfo->setTemplateSpecializationKind(TSK);
1190 return;
1191 }
1192
David Blaikieb219cfc2011-09-23 05:06:16 +00001193 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001194}
1195
Douglas Gregor1d110e02010-07-01 14:13:13 +00001196CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1197 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001198 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001199
1200 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001201 = Context.DeclarationNames.getCXXDestructorName(
1202 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001203
John McCallc0bf4622010-02-23 00:48:20 +00001204 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001205 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001206 if (I == E)
1207 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001209 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001210 return Dtor;
1211}
1212
Douglas Gregorda2142f2011-02-19 18:51:44 +00001213void CXXRecordDecl::completeDefinition() {
1214 completeDefinition(0);
1215}
1216
1217void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1218 RecordDecl::completeDefinition();
1219
David Blaikie4e4d0842012-03-11 07:00:24 +00001220 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001221 // Objective-C Automatic Reference Counting:
1222 // If a class has a non-static data member of Objective-C pointer
1223 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001224 // default constructor (if any), copy constructor, move constructor,
1225 // copy assignment operator, move assignment operator, and destructor are
1226 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001227 struct DefinitionData &Data = data();
1228 Data.PlainOldData = false;
Richard Smith7d04d3a2012-11-30 05:11:39 +00001229 Data.HasTrivialSpecialMembers = 0;
Richard Smithdfefb842012-02-25 07:33:38 +00001230 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001231 }
1232
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001233 // If the class may be abstract (but hasn't been marked as such), check for
1234 // any pure final overriders.
1235 if (mayBeAbstract()) {
1236 CXXFinalOverriderMap MyFinalOverriders;
1237 if (!FinalOverriders) {
1238 getFinalOverriders(MyFinalOverriders);
1239 FinalOverriders = &MyFinalOverriders;
1240 }
1241
1242 bool Done = false;
1243 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1244 MEnd = FinalOverriders->end();
1245 M != MEnd && !Done; ++M) {
1246 for (OverridingMethods::iterator SO = M->second.begin(),
1247 SOEnd = M->second.end();
1248 SO != SOEnd && !Done; ++SO) {
1249 assert(SO->second.size() > 0 &&
1250 "All virtual functions have overridding virtual functions");
1251
1252 // C++ [class.abstract]p4:
1253 // A class is abstract if it contains or inherits at least one
1254 // pure virtual function for which the final overrider is pure
1255 // virtual.
1256 if (SO->second.front().Method->isPure()) {
1257 data().Abstract = true;
1258 Done = true;
1259 break;
1260 }
1261 }
1262 }
1263 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001264
1265 // Set access bits correctly on the directly-declared conversions.
1266 for (UnresolvedSetIterator I = data().Conversions.begin(),
1267 E = data().Conversions.end();
1268 I != E; ++I)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001269 I.setAccess((*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001270}
1271
1272bool CXXRecordDecl::mayBeAbstract() const {
1273 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1274 isDependentContext())
1275 return false;
1276
1277 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1278 BEnd = bases_end();
1279 B != BEnd; ++B) {
1280 CXXRecordDecl *BaseDecl
1281 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1282 if (BaseDecl->isAbstract())
1283 return true;
1284 }
1285
1286 return false;
1287}
1288
David Blaikie99ba9e32011-12-20 02:48:34 +00001289void CXXMethodDecl::anchor() { }
1290
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001291static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1292 const CXXMethodDecl *BaseMD) {
1293 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1294 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1295 const CXXMethodDecl *MD = *I;
1296 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1297 return true;
1298 if (recursivelyOverrides(MD, BaseMD))
1299 return true;
1300 }
1301 return false;
1302}
1303
1304CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001305CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1306 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001307 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1308 return this;
1309
1310 // Lookup doesn't work for destructors, so handle them separately.
1311 if (isa<CXXDestructorDecl>(this)) {
1312 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001313 if (MD) {
1314 if (recursivelyOverrides(MD, this))
1315 return MD;
1316 if (MayBeBase && recursivelyOverrides(this, MD))
1317 return MD;
1318 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001319 return NULL;
1320 }
1321
1322 lookup_const_result Candidates = RD->lookup(getDeclName());
1323 for (NamedDecl * const * I = Candidates.first; I != Candidates.second; ++I) {
1324 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1325 if (!MD)
1326 continue;
1327 if (recursivelyOverrides(MD, this))
1328 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001329 if (MayBeBase && recursivelyOverrides(this, MD))
1330 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001331 }
1332
1333 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1334 E = RD->bases_end(); I != E; ++I) {
1335 const RecordType *RT = I->getType()->getAs<RecordType>();
1336 if (!RT)
1337 continue;
1338 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1339 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1340 if (T)
1341 return T;
1342 }
1343
1344 return NULL;
1345}
1346
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001347CXXMethodDecl *
1348CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001349 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001350 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001351 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001352 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001353 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001354 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001355 isStatic, SCAsWritten, isInline, isConstexpr,
1356 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001357}
1358
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001359CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1360 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1361 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1362 DeclarationNameInfo(), QualType(),
1363 0, false, SC_None, false, false,
1364 SourceLocation());
1365}
1366
Douglas Gregor90916562009-09-29 18:16:17 +00001367bool CXXMethodDecl::isUsualDeallocationFunction() const {
1368 if (getOverloadedOperator() != OO_Delete &&
1369 getOverloadedOperator() != OO_Array_Delete)
1370 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001371
1372 // C++ [basic.stc.dynamic.deallocation]p2:
1373 // A template instance is never a usual deallocation function,
1374 // regardless of its signature.
1375 if (getPrimaryTemplate())
1376 return false;
1377
Douglas Gregor90916562009-09-29 18:16:17 +00001378 // C++ [basic.stc.dynamic.deallocation]p2:
1379 // If a class T has a member deallocation function named operator delete
1380 // with exactly one parameter, then that function is a usual (non-placement)
1381 // deallocation function. [...]
1382 if (getNumParams() == 1)
1383 return true;
1384
1385 // C++ [basic.stc.dynamic.deallocation]p2:
1386 // [...] If class T does not declare such an operator delete but does
1387 // declare a member deallocation function named operator delete with
1388 // exactly two parameters, the second of which has type std::size_t (18.1),
1389 // then this function is a usual deallocation function.
1390 ASTContext &Context = getASTContext();
1391 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001392 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1393 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001394 return false;
1395
1396 // This function is a usual deallocation function if there are no
1397 // single-parameter deallocation functions of the same kind.
1398 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1399 R.first != R.second; ++R.first) {
1400 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1401 if (FD->getNumParams() == 1)
1402 return false;
1403 }
1404
1405 return true;
1406}
1407
Douglas Gregor06a9f362010-05-01 20:49:11 +00001408bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001409 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001410 // A user-declared copy assignment operator X::operator= is a non-static
1411 // non-template member function of class X with exactly one parameter of
1412 // type X, X&, const X&, volatile X& or const volatile X&.
1413 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1414 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001415 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001416 return false;
1417
1418 QualType ParamType = getParamDecl(0)->getType();
1419 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1420 ParamType = Ref->getPointeeType();
1421
1422 ASTContext &Context = getASTContext();
1423 QualType ClassType
1424 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1425 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1426}
1427
Sean Huntffe37fd2011-05-25 20:50:04 +00001428bool CXXMethodDecl::isMoveAssignmentOperator() const {
1429 // C++0x [class.copy]p19:
1430 // A user-declared move assignment operator X::operator= is a non-static
1431 // non-template member function of class X with exactly one parameter of type
1432 // X&&, const X&&, volatile X&&, or const volatile X&&.
1433 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1434 getPrimaryTemplate() || getDescribedFunctionTemplate())
1435 return false;
1436
1437 QualType ParamType = getParamDecl(0)->getType();
1438 if (!isa<RValueReferenceType>(ParamType))
1439 return false;
1440 ParamType = ParamType->getPointeeType();
1441
1442 ASTContext &Context = getASTContext();
1443 QualType ClassType
1444 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1445 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1446}
1447
Anders Carlsson05eb2442009-05-16 23:58:37 +00001448void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001449 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001450 assert(!MD->getParent()->isDependentContext() &&
1451 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001452 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001453
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001454 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001455}
1456
1457CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001458 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001459 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001460}
1461
1462CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001463 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001464 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001465}
1466
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001467unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001468 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001469 return getASTContext().overridden_methods_size(this);
1470}
1471
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001472QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001473 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1474 // If the member function is declared const, the type of this is const X*,
1475 // if the member function is declared volatile, the type of this is
1476 // volatile X*, and if the member function is declared const volatile,
1477 // the type of this is const volatile X*.
1478
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001479 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001480
John McCall3cb0ebd2010-03-10 03:28:59 +00001481 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001482 ClassTy = C.getQualifiedType(ClassTy,
1483 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001484 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001485}
1486
Eli Friedmand7d7f672009-12-06 20:50:05 +00001487bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001488 // If this function is a template instantiation, look at the template from
1489 // which it was instantiated.
1490 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1491 if (!CheckFn)
1492 CheckFn = this;
1493
Eli Friedmand7d7f672009-12-06 20:50:05 +00001494 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001495 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001496}
1497
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001498bool CXXMethodDecl::isLambdaStaticInvoker() const {
1499 return getParent()->isLambda() &&
1500 getIdentifier() && getIdentifier()->getName() == "__invoke";
1501}
1502
1503
Sean Huntcbb67482011-01-08 20:30:50 +00001504CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1505 TypeSourceInfo *TInfo, bool IsVirtual,
1506 SourceLocation L, Expr *Init,
1507 SourceLocation R,
1508 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001509 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001510 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1511 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001512{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001513}
1514
Sean Huntcbb67482011-01-08 20:30:50 +00001515CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1516 FieldDecl *Member,
1517 SourceLocation MemberLoc,
1518 SourceLocation L, Expr *Init,
1519 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001520 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001521 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001522 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1523{
1524}
1525
Sean Huntcbb67482011-01-08 20:30:50 +00001526CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1527 IndirectFieldDecl *Member,
1528 SourceLocation MemberLoc,
1529 SourceLocation L, Expr *Init,
1530 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001531 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001532 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001533 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001534{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001535}
1536
Sean Huntcbb67482011-01-08 20:30:50 +00001537CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001538 TypeSourceInfo *TInfo,
1539 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001540 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001541 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1542 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001543 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1544{
1545}
1546
1547CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001548 FieldDecl *Member,
1549 SourceLocation MemberLoc,
1550 SourceLocation L, Expr *Init,
1551 SourceLocation R,
1552 VarDecl **Indices,
1553 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001554 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001555 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001556 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001557{
1558 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1559 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1560}
1561
Sean Huntcbb67482011-01-08 20:30:50 +00001562CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1563 FieldDecl *Member,
1564 SourceLocation MemberLoc,
1565 SourceLocation L, Expr *Init,
1566 SourceLocation R,
1567 VarDecl **Indices,
1568 unsigned NumIndices) {
1569 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001570 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001571 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001572 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1573 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001574}
1575
Sean Huntcbb67482011-01-08 20:30:50 +00001576TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001577 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001578 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001579 else
1580 return TypeLoc();
1581}
1582
Sean Huntcbb67482011-01-08 20:30:50 +00001583const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001584 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001585 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001586 else
1587 return 0;
1588}
1589
Sean Huntcbb67482011-01-08 20:30:50 +00001590SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001591 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001592 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001593
1594 if (isInClassMemberInitializer())
1595 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001596
Douglas Gregor76852c22011-11-01 01:16:03 +00001597 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1598 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1599
1600 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001601}
1602
Sean Huntcbb67482011-01-08 20:30:50 +00001603SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001604 if (isInClassMemberInitializer()) {
1605 FieldDecl *D = getAnyMember();
1606 if (Expr *I = D->getInClassInitializer())
1607 return I->getSourceRange();
1608 return SourceRange();
1609 }
1610
Douglas Gregor802ab452009-12-02 22:36:29 +00001611 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001612}
1613
David Blaikie99ba9e32011-12-20 02:48:34 +00001614void CXXConstructorDecl::anchor() { }
1615
Douglas Gregorb48fe382008-10-31 09:07:45 +00001616CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001617CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1618 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1619 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1620 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001621}
1622
1623CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001624CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001625 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001626 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001627 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001628 bool isExplicit, bool isInline,
1629 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001630 assert(NameInfo.getName().getNameKind()
1631 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001632 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001633 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001634 isExplicit, isInline, isImplicitlyDeclared,
1635 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001636}
1637
Douglas Gregor76852c22011-11-01 01:16:03 +00001638CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1639 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1640 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1641 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1642 return Construct->getConstructor();
1643
1644 return 0;
1645}
1646
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001647bool CXXConstructorDecl::isDefaultConstructor() const {
1648 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001649 // A default constructor for a class X is a constructor of class
1650 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001651 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001652 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001653}
1654
Mike Stump1eb44332009-09-09 15:08:12 +00001655bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001656CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001657 return isCopyOrMoveConstructor(TypeQuals) &&
1658 getParamDecl(0)->getType()->isLValueReferenceType();
1659}
1660
1661bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1662 return isCopyOrMoveConstructor(TypeQuals) &&
1663 getParamDecl(0)->getType()->isRValueReferenceType();
1664}
1665
1666/// \brief Determine whether this is a copy or move constructor.
1667bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001668 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001669 // A non-template constructor for class X is a copy constructor
1670 // if its first parameter is of type X&, const X&, volatile X& or
1671 // const volatile X&, and either there are no other parameters
1672 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001673 // C++0x [class.copy]p3:
1674 // A non-template constructor for class X is a move constructor if its
1675 // first parameter is of type X&&, const X&&, volatile X&&, or
1676 // const volatile X&&, and either there are no other parameters or else
1677 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001678 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001679 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001680 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001681 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001682 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001683
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001684 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001685
1686 // Do we have a reference type?
1687 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001688 if (!ParamRefType)
1689 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001690
Douglas Gregorfd476482009-11-13 23:59:09 +00001691 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001692 ASTContext &Context = getASTContext();
1693
Douglas Gregorfd476482009-11-13 23:59:09 +00001694 CanQualType PointeeType
1695 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001696 CanQualType ClassTy
1697 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001698 if (PointeeType.getUnqualifiedType() != ClassTy)
1699 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001700
John McCall0953e762009-09-24 19:53:00 +00001701 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001702
1703 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001704 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001705 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001706}
1707
Anders Carlssonfaccd722009-08-28 16:57:08 +00001708bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001709 // C++ [class.conv.ctor]p1:
1710 // A constructor declared without the function-specifier explicit
1711 // that can be called with a single parameter specifies a
1712 // conversion from the type of its first parameter to the type of
1713 // its class. Such a constructor is called a converting
1714 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001715 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001716 return false;
1717
Mike Stump1eb44332009-09-09 15:08:12 +00001718 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001719 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001720 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001721 (getNumParams() > 1 &&
1722 (getParamDecl(1)->hasDefaultArg() ||
1723 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001724}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001725
Douglas Gregor6493cc52010-11-08 17:16:59 +00001726bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001727 if ((getNumParams() < 1) ||
1728 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1729 (getPrimaryTemplate() == 0) ||
1730 (getDescribedFunctionTemplate() != 0))
1731 return false;
1732
1733 const ParmVarDecl *Param = getParamDecl(0);
1734
1735 ASTContext &Context = getASTContext();
1736 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1737
Douglas Gregor66724ea2009-11-14 01:20:54 +00001738 // Is it the same as our our class type?
1739 CanQualType ClassTy
1740 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1741 if (ParamType.getUnqualifiedType() != ClassTy)
1742 return false;
1743
1744 return true;
1745}
1746
Sebastian Redlf677ea32011-02-05 19:23:19 +00001747const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1748 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001749 method_iterator It = getASTContext().overridden_methods_begin(this);
1750 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001751 return 0;
1752
1753 return cast<CXXConstructorDecl>(*It);
1754}
1755
1756void
1757CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1758 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001759 assert(getASTContext().overridden_methods_size(this) == 0 &&
1760 "Base ctor already set.");
1761 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001762}
1763
David Blaikie99ba9e32011-12-20 02:48:34 +00001764void CXXDestructorDecl::anchor() { }
1765
Douglas Gregor42a552f2008-11-05 20:51:48 +00001766CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001767CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1768 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1769 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001770 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001771}
1772
1773CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001774CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001775 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001776 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001777 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001778 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001779 assert(NameInfo.getName().getNameKind()
1780 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001781 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001782 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001783 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001784}
1785
David Blaikie99ba9e32011-12-20 02:48:34 +00001786void CXXConversionDecl::anchor() { }
1787
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001788CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001789CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1790 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1791 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1792 QualType(), 0, false, false, false,
1793 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001794}
1795
1796CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001797CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001798 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001799 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001800 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001801 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001802 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001803 assert(NameInfo.getName().getNameKind()
1804 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001805 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001806 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001807 isInline, isExplicit, isConstexpr,
1808 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001809}
1810
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001811bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1812 return isImplicit() && getParent()->isLambda() &&
1813 getConversionType()->isBlockPointerType();
1814}
1815
David Blaikie99ba9e32011-12-20 02:48:34 +00001816void LinkageSpecDecl::anchor() { }
1817
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001818LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001819 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001820 SourceLocation ExternLoc,
1821 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001822 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001823 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001824 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001825}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001826
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001827LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1828 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1829 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1830 lang_c, SourceLocation());
1831}
1832
David Blaikie99ba9e32011-12-20 02:48:34 +00001833void UsingDirectiveDecl::anchor() { }
1834
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001835UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1836 SourceLocation L,
1837 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001838 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001839 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001840 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001841 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001842 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1843 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001844 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1845 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001846}
1847
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001848UsingDirectiveDecl *
1849UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1850 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1851 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1852 NestedNameSpecifierLoc(),
1853 SourceLocation(), 0, 0);
1854}
1855
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001856NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1857 if (NamespaceAliasDecl *NA =
1858 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1859 return NA->getNamespace();
1860 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1861}
1862
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001863void NamespaceDecl::anchor() { }
1864
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001865NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1866 SourceLocation StartLoc,
1867 SourceLocation IdLoc, IdentifierInfo *Id,
1868 NamespaceDecl *PrevDecl)
1869 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1870 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1871{
1872 setPreviousDeclaration(PrevDecl);
1873
1874 if (PrevDecl)
1875 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1876}
1877
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001878NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001879 bool Inline, SourceLocation StartLoc,
1880 SourceLocation IdLoc, IdentifierInfo *Id,
1881 NamespaceDecl *PrevDecl) {
1882 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001883}
1884
1885NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1886 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001887 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1888 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001889}
1890
David Blaikie99ba9e32011-12-20 02:48:34 +00001891void NamespaceAliasDecl::anchor() { }
1892
Mike Stump1eb44332009-09-09 15:08:12 +00001893NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001894 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001895 SourceLocation AliasLoc,
1896 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001897 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001898 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001899 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001900 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1901 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001902 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1903 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001904}
1905
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001906NamespaceAliasDecl *
1907NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1908 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1909 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1910 NestedNameSpecifierLoc(),
1911 SourceLocation(), 0);
1912}
1913
David Blaikie99ba9e32011-12-20 02:48:34 +00001914void UsingShadowDecl::anchor() { }
1915
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001916UsingShadowDecl *
1917UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1918 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1919 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1920}
1921
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001922UsingDecl *UsingShadowDecl::getUsingDecl() const {
1923 const UsingShadowDecl *Shadow = this;
1924 while (const UsingShadowDecl *NextShadow =
1925 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1926 Shadow = NextShadow;
1927 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1928}
1929
David Blaikie99ba9e32011-12-20 02:48:34 +00001930void UsingDecl::anchor() { }
1931
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001932void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1933 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1934 "declaration already in set");
1935 assert(S->getUsingDecl() == this);
1936
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001937 if (FirstUsingShadow.getPointer())
1938 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1939 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001940}
1941
1942void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1943 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1944 "declaration not in set");
1945 assert(S->getUsingDecl() == this);
1946
1947 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1948
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001949 if (FirstUsingShadow.getPointer() == S) {
1950 FirstUsingShadow.setPointer(
1951 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001952 S->UsingOrNextShadow = this;
1953 return;
1954 }
1955
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001956 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001957 while (Prev->UsingOrNextShadow != S)
1958 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1959 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1960 S->UsingOrNextShadow = this;
1961}
1962
Douglas Gregordc355712011-02-25 00:36:19 +00001963UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1964 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001965 const DeclarationNameInfo &NameInfo,
1966 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001967 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001968}
1969
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001970UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1971 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1972 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1973 DeclarationNameInfo(), false);
1974}
1975
David Blaikie99ba9e32011-12-20 02:48:34 +00001976void UnresolvedUsingValueDecl::anchor() { }
1977
John McCall7ba107a2009-11-18 02:36:19 +00001978UnresolvedUsingValueDecl *
1979UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1980 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001981 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001982 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001983 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001984 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001985}
1986
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001987UnresolvedUsingValueDecl *
1988UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1989 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1990 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1991 NestedNameSpecifierLoc(),
1992 DeclarationNameInfo());
1993}
1994
David Blaikie99ba9e32011-12-20 02:48:34 +00001995void UnresolvedUsingTypenameDecl::anchor() { }
1996
John McCall7ba107a2009-11-18 02:36:19 +00001997UnresolvedUsingTypenameDecl *
1998UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1999 SourceLocation UsingLoc,
2000 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002001 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002002 SourceLocation TargetNameLoc,
2003 DeclarationName TargetName) {
2004 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002005 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002006 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00002007}
2008
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002009UnresolvedUsingTypenameDecl *
2010UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2011 void *Mem = AllocateDeserializedDecl(C, ID,
2012 sizeof(UnresolvedUsingTypenameDecl));
2013 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2014 SourceLocation(),
2015 NestedNameSpecifierLoc(),
2016 SourceLocation(),
2017 0);
2018}
2019
David Blaikie99ba9e32011-12-20 02:48:34 +00002020void StaticAssertDecl::anchor() { }
2021
Anders Carlssonfb311762009-03-14 00:25:26 +00002022StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002023 SourceLocation StaticAssertLoc,
2024 Expr *AssertExpr,
2025 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002026 SourceLocation RParenLoc,
2027 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002028 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002029 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00002030}
2031
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002032StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2033 unsigned ID) {
2034 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00002035 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
2036 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002037}
2038
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002039static const char *getAccessName(AccessSpecifier AS) {
2040 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002041 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002042 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002043 case AS_public:
2044 return "public";
2045 case AS_private:
2046 return "private";
2047 case AS_protected:
2048 return "protected";
2049 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002050 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002051}
2052
2053const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2054 AccessSpecifier AS) {
2055 return DB << getAccessName(AS);
2056}
Richard Smithf15fda02012-02-02 01:16:57 +00002057
2058const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2059 AccessSpecifier AS) {
2060 return DB << getAccessName(AS);
2061}