blob: c1aa35f305cf6d0b6fc75c23dc523a0b15428ba7 [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"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000017#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000018#include "clang/AST/CXXInheritance.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
John McCall86ff3082010-02-04 22:26:26 +000033CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
34 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000035 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
36 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000037 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000038 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000039 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Douglas Gregor2bb11012011-05-13 01:05:07 +000040 HasMutableFields(false), HasTrivialDefaultConstructor(true),
Richard Smith61802452011-12-22 02:22:31 +000041 HasConstexprNonCopyMoveConstructor(false),
42 DefaultedDefaultConstructorIsConstexpr(true),
43 DefaultedCopyConstructorIsConstexpr(true),
44 DefaultedMoveConstructorIsConstexpr(true),
45 HasConstexprDefaultConstructor(false), HasConstexprCopyConstructor(false),
46 HasConstexprMoveConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000047 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
48 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
49 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000050 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000051 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
52 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sebastian Redl85ea7aa2011-08-30 19:58:05 +000053 DeclaredDestructor(false), FailedImplicitMoveConstructor(false),
54 FailedImplicitMoveAssignment(false), NumBases(0), NumVBases(0), Bases(),
55 VBases(), Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000056}
57
58CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000059 SourceLocation StartLoc, SourceLocation IdLoc,
60 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
61 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000062 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000063 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000064
Jay Foad4ba2a172011-01-12 09:06:06 +000065CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000066 DeclContext *DC, SourceLocation StartLoc,
67 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000068 CXXRecordDecl* PrevDecl,
69 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000070 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
71 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000073 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000074 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000075 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000076 return R;
77}
78
Jay Foad4ba2a172011-01-12 09:06:06 +000079CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000080 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
81 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000082}
83
Mike Stump1eb44332009-09-09 15:08:12 +000084void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000085CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000086 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000087 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +000088
Douglas Gregor7c789c12010-10-29 22:39:52 +000089 if (!data().Bases.isOffset() && data().NumBases > 0)
90 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000091
Richard Smithdd677232011-10-18 20:08:55 +000092 if (NumBases) {
93 // C++ [dcl.init.aggr]p1:
94 // An aggregate is [...] a class with [...] no base classes [...].
95 data().Aggregate = false;
96
97 // C++ [class]p4:
98 // A POD-struct is an aggregate class...
99 data().PlainOldData = false;
100 }
101
Anders Carlsson6f6de732010-03-29 05:13:12 +0000102 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000103 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000104
105 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000106 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
John McCall86ff3082010-02-04 22:26:26 +0000108 data().Bases = new(C) CXXBaseSpecifier [NumBases];
109 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000110 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000111 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000112 // Keep track of inherited vbases for this base class.
113 const CXXBaseSpecifier *Base = Bases[i];
114 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000115 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000116 if (BaseType->isDependentType())
117 continue;
118 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000119 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000120
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000121 // A class with a non-empty base class is not empty.
122 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000123 if (!BaseClassDecl->isEmpty()) {
124 if (!data().Empty) {
125 // C++0x [class]p7:
126 // A standard-layout class is a class that:
127 // [...]
128 // -- either has no non-static data members in the most derived
129 // class and at most one base class with non-static data members,
130 // or has no base classes with non-static data members, and
131 // If this is the second non-empty base, then neither of these two
132 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000133 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000134 }
135
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000136 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000137 data().HasNoNonEmptyBases = false;
138 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000139
Douglas Gregor85606eb2010-09-28 20:50:54 +0000140 // C++ [class.virtual]p1:
141 // A class that declares or inherits a virtual function is called a
142 // polymorphic class.
143 if (BaseClassDecl->isPolymorphic())
144 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000145
Chandler Carrutha8225442011-04-30 09:17:45 +0000146 // C++0x [class]p7:
147 // A standard-layout class is a class that: [...]
148 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000149 if (!BaseClassDecl->isStandardLayout())
150 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000151
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000152 // Record if this base is the first non-literal field or base.
153 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
154 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000155
Anders Carlsson6f6de732010-03-29 05:13:12 +0000156 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000157 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000158 BaseClassDecl->vbases_begin(),
159 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000160 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000161 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000162 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000163 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000164
165 if (Base->isVirtual()) {
166 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000167 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000168 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000169
170 // C++0x [meta.unary.prop] is_empty:
171 // T is a class type, but not a union type, with ... no virtual base
172 // classes
173 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000174
175 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000176 // A default constructor is trivial [...] if:
177 // -- its class has [...] no virtual bases
178 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000179
180 // C++0x [class.copy]p13:
181 // A copy/move constructor for class X is trivial if it is neither
182 // user-provided nor deleted and if
183 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000184 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000185 data().HasTrivialMoveConstructor = false;
186
187 // C++0x [class.copy]p27:
188 // A copy/move assignment operator for class X is trivial if it is
189 // neither user-provided nor deleted and if
190 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000191 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000192 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000193
194 // C++0x [class]p7:
195 // A standard-layout class is a class that: [...]
196 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000197 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000198
199 // C++11 [dcl.constexpr]p4:
200 // In the definition of a constexpr constructor [...]
201 // -- the class shall not have any virtual base classes
202 data().DefaultedDefaultConstructorIsConstexpr = false;
203 data().DefaultedCopyConstructorIsConstexpr = false;
204 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000205 } else {
206 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000207 // A default constructor is trivial [...] if:
208 // -- all the direct base classes of its class have trivial default
209 // constructors.
210 if (!BaseClassDecl->hasTrivialDefaultConstructor())
211 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000212
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000213 // C++0x [class.copy]p13:
214 // A copy/move constructor for class X is trivial if [...]
215 // [...]
216 // -- the constructor selected to copy/move each direct base class
217 // subobject is trivial, and
218 // FIXME: C++0x: We need to only consider the selected constructor
219 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000220 if (!BaseClassDecl->hasTrivialCopyConstructor())
221 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000222 if (!BaseClassDecl->hasTrivialMoveConstructor())
223 data().HasTrivialMoveConstructor = false;
224
225 // C++0x [class.copy]p27:
226 // A copy/move assignment operator for class X is trivial if [...]
227 // [...]
228 // -- the assignment operator selected to copy/move each direct base
229 // class subobject is trivial, and
230 // FIXME: C++0x: We need to only consider the selected operator instead
231 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000232 if (!BaseClassDecl->hasTrivialCopyAssignment())
233 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000234 if (!BaseClassDecl->hasTrivialMoveAssignment())
235 data().HasTrivialMoveAssignment = false;
Richard Smith61802452011-12-22 02:22:31 +0000236
237 // C++11 [class.ctor]p6:
238 // If that user-written default cosntructor would satisfy the
239 // requirements of a constexpr constructor, the implicitly-defined
240 // default constructor is constexpr.
241 if (!BaseClassDecl->hasConstexprDefaultConstructor())
242 data().DefaultedDefaultConstructorIsConstexpr = false;
243
244 // C++11 [class.copy]p13:
245 // If the implicitly-defined constructor would satisfy the requirements
246 // of a constexpr constructor, the implicitly-defined constructor is
247 // constexpr.
248 // C++11 [dcl.constexpr]p4:
249 // -- every constructor involved in initializing [...] base class
250 // sub-objects shall be a constexpr constructor
251 if (!BaseClassDecl->hasConstexprCopyConstructor())
252 data().DefaultedCopyConstructorIsConstexpr = false;
253 if (BaseClassDecl->hasDeclaredMoveConstructor() ||
254 BaseClassDecl->needsImplicitMoveConstructor())
255 // FIXME: If the implicit move constructor generated for the base class
256 // would be ill-formed, the implicit move constructor generated for the
257 // derived class calls the base class' copy constructor.
258 data().DefaultedMoveConstructorIsConstexpr &=
259 !BaseClassDecl->hasConstexprMoveConstructor();
260 else if (!BaseClassDecl->hasConstexprCopyConstructor())
261 data().DefaultedMoveConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000262 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000263
264 // C++ [class.ctor]p3:
265 // A destructor is trivial if all the direct base classes of its class
266 // have trivial destructors.
267 if (!BaseClassDecl->hasTrivialDestructor())
268 data().HasTrivialDestructor = false;
Douglas Gregor2bb11012011-05-13 01:05:07 +0000269
John McCallf85e1932011-06-15 23:02:42 +0000270 // A class has an Objective-C object member if... or any of its bases
271 // has an Objective-C object member.
272 if (BaseClassDecl->hasObjectMember())
273 setHasObjectMember(true);
274
Douglas Gregor2bb11012011-05-13 01:05:07 +0000275 // Keep track of the presence of mutable fields.
276 if (BaseClassDecl->hasMutableFields())
277 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000278 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000279
280 if (VBases.empty())
281 return;
282
283 // Create base specifier for any direct or indirect virtual bases.
284 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
285 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000286 for (int I = 0, E = VBases.size(); I != E; ++I)
287 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000288}
289
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000290/// Callback function for CXXRecordDecl::forallBases that acknowledges
291/// that it saw a base class.
292static bool SawBase(const CXXRecordDecl *, void *) {
293 return true;
294}
295
296bool CXXRecordDecl::hasAnyDependentBases() const {
297 if (!isDependentContext())
298 return false;
299
300 return !forallBases(SawBase, 0);
301}
302
Sean Huntffe37fd2011-05-25 20:50:04 +0000303bool CXXRecordDecl::hasConstCopyConstructor() const {
304 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000305}
306
Chandler Carruthb7e95892011-04-23 10:47:28 +0000307bool CXXRecordDecl::isTriviallyCopyable() const {
308 // C++0x [class]p5:
309 // A trivially copyable class is a class that:
310 // -- has no non-trivial copy constructors,
311 if (!hasTrivialCopyConstructor()) return false;
312 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000313 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000314 // -- has no non-trivial copy assignment operators,
315 if (!hasTrivialCopyAssignment()) return false;
316 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000317 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000318 // -- has a trivial destructor.
319 if (!hasTrivialDestructor()) return false;
320
321 return true;
322}
323
Douglas Gregor0d405db2010-07-01 20:59:04 +0000324/// \brief Perform a simplistic form of overload resolution that only considers
325/// cv-qualifiers on a single parameter, and return the best overload candidate
326/// (if there is one).
327static CXXMethodDecl *
328GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000329 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000330 if (Cands.empty())
331 return 0;
332 if (Cands.size() == 1)
333 return Cands[0].first;
334
335 unsigned Best = 0, N = Cands.size();
336 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000337 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000338 Best = I;
339
340 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000341 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000342 return 0;
343
344 return Cands[Best].first;
345}
346
Sean Huntffe37fd2011-05-25 20:50:04 +0000347CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
348 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000349 QualType ClassType
350 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000351 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000352 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000353 Context.getCanonicalType(ClassType));
354 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000355 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000356 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000357 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000358 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000359 // C++ [class.copy]p2:
360 // A non-template constructor for class X is a copy constructor if [...]
361 if (isa<FunctionTemplateDecl>(*Con))
362 continue;
363
Douglas Gregor0d405db2010-07-01 20:59:04 +0000364 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
365 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000366 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
367 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000368 Found.push_back(std::make_pair(
369 const_cast<CXXConstructorDecl *>(Constructor),
370 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000371 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000372 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000373
374 return cast_or_null<CXXConstructorDecl>(
375 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000376}
377
Sean Huntffe37fd2011-05-25 20:50:04 +0000378CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
379 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
380 if (I->isMoveConstructor())
381 return *I;
382
383 return 0;
384}
385
Douglas Gregorb87786f2010-07-01 17:48:08 +0000386CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
387 ASTContext &Context = getASTContext();
388 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
389 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
390
Chris Lattner5f9e2722011-07-23 10:55:15 +0000391 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000392 DeclContext::lookup_const_iterator Op, OpEnd;
393 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
394 // C++ [class.copy]p9:
395 // A user-declared copy assignment operator is a non-static non-template
396 // member function of class X with exactly one parameter of type X, X&,
397 // const X&, volatile X& or const volatile X&.
398 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
399 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
400 continue;
401
402 const FunctionProtoType *FnType
403 = Method->getType()->getAs<FunctionProtoType>();
404 assert(FnType && "Overloaded operator has no prototype.");
405 // Don't assert on this; an invalid decl might have been left in the AST.
406 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
407 continue;
408
409 QualType ArgType = FnType->getArgType(0);
410 Qualifiers Quals;
411 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
412 ArgType = Ref->getPointeeType();
413 // If we have a const argument and we have a reference to a non-const,
414 // this function does not match.
415 if (ArgIsConst && !ArgType.isConstQualified())
416 continue;
417
418 Quals = ArgType.getQualifiers();
419 } else {
420 // By-value copy-assignment operators are treated like const X&
421 // copy-assignment operators.
422 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
423 }
424
425 if (!Context.hasSameUnqualifiedType(ArgType, Class))
426 continue;
427
428 // Save this copy-assignment operator. It might be "the one".
429 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
430 }
431
432 // Use a simplistic form of overload resolution to find the candidate.
433 return GetBestOverloadCandidateSimple(Found);
434}
435
Sean Huntffe37fd2011-05-25 20:50:04 +0000436CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
437 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
438 if (I->isMoveAssignmentOperator())
439 return *I;
440
441 return 0;
442}
443
Douglas Gregor21386642010-09-28 21:55:22 +0000444void CXXRecordDecl::markedVirtualFunctionPure() {
445 // C++ [class.abstract]p2:
446 // A class is abstract if it has at least one pure virtual function.
447 data().Abstract = true;
448}
449
450void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000451 // Ignore friends and invalid declarations.
452 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000453 return;
454
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000455 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
456 if (FunTmpl)
457 D = FunTmpl->getTemplatedDecl();
458
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000459 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
460 if (Method->isVirtual()) {
461 // C++ [dcl.init.aggr]p1:
462 // An aggregate is an array or a class with [...] no virtual functions.
463 data().Aggregate = false;
464
465 // C++ [class]p4:
466 // A POD-struct is an aggregate class...
467 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000468
469 // Virtual functions make the class non-empty.
470 // FIXME: Standard ref?
471 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000472
473 // C++ [class.virtual]p1:
474 // A class that declares or inherits a virtual function is called a
475 // polymorphic class.
476 data().Polymorphic = true;
477
Sean Hunt023df372011-05-09 18:22:59 +0000478 // C++0x [class.ctor]p5
479 // A default constructor is trivial [...] if:
480 // -- its class has no virtual functions [...]
481 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000482
483 // C++0x [class.copy]p13:
484 // A copy/move constructor for class X is trivial if [...]
485 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000486 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000487 data().HasTrivialMoveConstructor = false;
488
489 // C++0x [class.copy]p27:
490 // A copy/move assignment operator for class X is trivial if [...]
491 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000492 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000493 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000494
Chandler Carrutha8225442011-04-30 09:17:45 +0000495 // C++0x [class]p7:
496 // A standard-layout class is a class that: [...]
497 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000498 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000499 }
500 }
501
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000502 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000503 // Notify that an implicit member was added after the definition
504 // was completed.
505 if (!isBeingDefined())
506 if (ASTMutationListener *L = getASTMutationListener())
507 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000508
Sean Huntffe37fd2011-05-25 20:50:04 +0000509 // If this is a special member function, note that it was added and then
510 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000511 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith61802452011-12-22 02:22:31 +0000512 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000513 data().DeclaredDefaultConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000514 if (Constructor->isConstexpr()) {
515 data().HasConstexprDefaultConstructor = true;
516 data().HasConstexprNonCopyMoveConstructor = true;
517 }
518 } else if (Constructor->isCopyConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000519 data().DeclaredCopyConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000520 if (Constructor->isConstexpr())
521 data().HasConstexprCopyConstructor = true;
522 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000523 data().DeclaredMoveConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000524 if (Constructor->isConstexpr())
525 data().HasConstexprMoveConstructor = true;
526 } else
Sean Huntffe37fd2011-05-25 20:50:04 +0000527 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000528 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000529 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000530 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000531 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000532 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
533 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000534 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000535 else if (Method->isMoveAssignmentOperator())
536 data().DeclaredMoveAssignment = true;
537 else
538 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000539 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000540 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000541
Sean Huntffe37fd2011-05-25 20:50:04 +0000542NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000543 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000544 }
545
546 // Handle (user-declared) constructors.
547 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
548 // Note that we have a user-declared constructor.
549 data().UserDeclaredConstructor = true;
550
Richard Smith017ab772011-09-05 02:13:09 +0000551 // Technically, "user-provided" is only defined for special member
552 // functions, but the intent of the standard is clearly that it should apply
553 // to all functions.
554 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000555
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000556 if (Constructor->isDefaultConstructor()) {
557 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000558 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000559 // C++0x [class.ctor]p5:
560 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000561 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000562 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000563 }
Richard Smith61802452011-12-22 02:22:31 +0000564 if (Constructor->isConstexpr()) {
565 data().HasConstexprDefaultConstructor = true;
566 data().HasConstexprNonCopyMoveConstructor = true;
567 }
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000568 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000569
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000570 // Note when we have a user-declared copy or move constructor, which will
571 // suppress the implicit declaration of those constructors.
572 if (!FunTmpl) {
573 if (Constructor->isCopyConstructor()) {
574 data().UserDeclaredCopyConstructor = true;
575 data().DeclaredCopyConstructor = true;
576
577 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000578 // A copy/move constructor for class X is trivial if it is not
579 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000580 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000581 data().HasTrivialCopyConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000582
583 if (Constructor->isConstexpr())
584 data().HasConstexprCopyConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000585 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000586 data().UserDeclaredMoveConstructor = true;
587 data().DeclaredMoveConstructor = true;
588
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000589 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000590 // A copy/move constructor for class X is trivial if it is not
591 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000592 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000593 data().HasTrivialMoveConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000594
595 if (Constructor->isConstexpr())
596 data().HasConstexprMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000597 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000598 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000599 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
600 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000601 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000602 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000603 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000604
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000605 // C++ [dcl.init.aggr]p1:
606 // An aggregate is an array or a class with no user-declared
607 // constructors [...].
608 // C++0x [dcl.init.aggr]p1:
609 // An aggregate is an array or a class with no user-provided
610 // constructors [...].
611 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
612 data().Aggregate = false;
613
614 // C++ [class]p4:
615 // A POD-struct is an aggregate class [...]
616 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
617 // type is technically an aggregate in C++0x since it wouldn't be in 03.
618 data().PlainOldData = false;
619
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000620 return;
621 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000622
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000623 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000624 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000625 data().DeclaredDestructor = true;
626 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000627
628 // C++ [class]p4:
629 // A POD-struct is an aggregate class that has [...] no user-defined
630 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000631 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000632 data().PlainOldData = false;
633
Douglas Gregor45fa5602011-11-07 20:56:01 +0000634 // C++11 [class.dtor]p5:
635 // A destructor is trivial if it is not user-provided and if
636 // -- the destructor is not virtual.
Richard Smith61802452011-12-22 02:22:31 +0000637 if (DD->isUserProvided() || DD->isVirtual()) {
Sean Huntcf34e752011-05-16 22:41:40 +0000638 data().HasTrivialDestructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000639 // C++11 [dcl.constexpr]p1:
640 // The constexpr specifier shall be applied only to [...] the
641 // declaration of a static data member of a literal type.
642 // C++11 [basic.types]p10:
643 // A type is a literal type if it is [...] a class type that [...] has
644 // a trivial destructor.
645 data().DefaultedDefaultConstructorIsConstexpr = false;
646 data().DefaultedCopyConstructorIsConstexpr = false;
647 data().DefaultedMoveConstructorIsConstexpr = false;
648 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000649
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000650 return;
651 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000652
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000653 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000654 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000655 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000656 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000657 // A POD-struct is an aggregate class that [...] has no user-defined
658 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000659 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000660 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000661
Sean Huntffe37fd2011-05-25 20:50:04 +0000662 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000663
Sean Huntffe37fd2011-05-25 20:50:04 +0000664 // Suppress the implicit declaration of a copy constructor.
665 data().UserDeclaredCopyAssignment = true;
666 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000667
Sean Huntffe37fd2011-05-25 20:50:04 +0000668 // C++0x [class.copy]p27:
669 // A copy/move assignment operator for class X is trivial if it is
670 // neither user-provided nor deleted [...]
671 if (Method->isUserProvided())
672 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000673
Sean Huntffe37fd2011-05-25 20:50:04 +0000674 return;
675 }
676
677 if (Method->isMoveAssignmentOperator()) {
678 // This is an extension in C++03 mode, but we'll keep consistency by
679 // taking a move assignment operator to induce non-POD-ness
680 data().PlainOldData = false;
681
682 // This is a move assignment operator.
683 data().UserDeclaredMoveAssignment = true;
684 data().DeclaredMoveAssignment = true;
685
686 // C++0x [class.copy]p27:
687 // A copy/move assignment operator for class X is trivial if it is
688 // neither user-provided nor deleted [...]
689 if (Method->isUserProvided())
690 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000691 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000692
Douglas Gregore80622f2010-09-29 04:25:11 +0000693 // Keep the list of conversion functions up-to-date.
694 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
695 // We don't record specializations.
696 if (Conversion->getPrimaryTemplate())
697 return;
698
699 // FIXME: We intentionally don't use the decl's access here because it
700 // hasn't been set yet. That's really just a misdesign in Sema.
701
702 if (FunTmpl) {
703 if (FunTmpl->getPreviousDeclaration())
704 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
705 FunTmpl);
706 else
707 data().Conversions.addDecl(FunTmpl);
708 } else {
709 if (Conversion->getPreviousDeclaration())
710 data().Conversions.replace(Conversion->getPreviousDeclaration(),
711 Conversion);
712 else
713 data().Conversions.addDecl(Conversion);
714 }
715 }
716
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000717 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000718 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000719
720 // Handle non-static data members.
721 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000722 // C++ [class.bit]p2:
723 // A declaration for a bit-field that omits the identifier declares an
724 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
725 // initialized.
726 if (Field->isUnnamedBitfield())
727 return;
728
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000729 // C++ [dcl.init.aggr]p1:
730 // An aggregate is an array or a class (clause 9) with [...] no
731 // private or protected non-static data members (clause 11).
732 //
733 // A POD must be an aggregate.
734 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
735 data().Aggregate = false;
736 data().PlainOldData = false;
737 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000738
739 // C++0x [class]p7:
740 // A standard-layout class is a class that:
741 // [...]
742 // -- has the same access control for all non-static data members,
743 switch (D->getAccess()) {
744 case AS_private: data().HasPrivateFields = true; break;
745 case AS_protected: data().HasProtectedFields = true; break;
746 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000747 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000748 };
749 if ((data().HasPrivateFields + data().HasProtectedFields +
750 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000751 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000752
Douglas Gregor2bb11012011-05-13 01:05:07 +0000753 // Keep track of the presence of mutable fields.
754 if (Field->isMutable())
755 data().HasMutableFields = true;
756
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000757 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000758 // A POD struct is a class that is both a trivial class and a
759 // standard-layout class, and has no non-static data members of type
760 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000761 //
762 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
763 // that does not explicitly have no lifetime makes the class a non-POD.
764 // However, we delay setting PlainOldData to false in this case so that
765 // Sema has a chance to diagnostic causes where the same class will be
766 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
767 // In this case, the class will become a non-POD class when we complete
768 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000769 ASTContext &Context = getASTContext();
770 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000771 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
772 if (!Context.getLangOptions().ObjCAutoRefCount ||
773 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
774 setHasObjectMember(true);
775 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000776 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000777
Chandler Carrutha8225442011-04-30 09:17:45 +0000778 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000779 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000780
Chandler Carrutha8225442011-04-30 09:17:45 +0000781 // C++0x [class]p7:
782 // A standard-layout class is a class that:
783 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000784 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000785 }
786
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000787 // Record if this field is the first non-literal field or base.
Richard Smith5fa6a042011-10-12 05:08:15 +0000788 // As a slight variation on the standard, we regard mutable members as being
789 // non-literal, since mutating a constexpr variable would break C++11
790 // constant expression semantics.
791 if ((!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType()) ||
792 Field->isMutable())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000793 data().HasNonLiteralTypeFieldsOrBases = true;
794
Richard Smith7a614d82011-06-11 17:19:42 +0000795 if (Field->hasInClassInitializer()) {
796 // C++0x [class]p5:
797 // A default constructor is trivial if [...] no non-static data member
798 // of its class has a brace-or-equal-initializer.
799 data().HasTrivialDefaultConstructor = false;
800
801 // C++0x [dcl.init.aggr]p1:
802 // An aggregate is a [...] class with [...] no
803 // brace-or-equal-initializers for non-static data members.
804 data().Aggregate = false;
805
806 // C++0x [class]p10:
807 // A POD struct is [...] a trivial class.
808 data().PlainOldData = false;
809 }
810
Douglas Gregor85606eb2010-09-28 20:50:54 +0000811 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
812 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
813 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000814 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000815 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000816 // -- for all the non-static data members of its class that are of
817 // class type (or array thereof), each such class has a trivial
818 // default constructor.
819 if (!FieldRec->hasTrivialDefaultConstructor())
820 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000821
822 // C++0x [class.copy]p13:
823 // A copy/move constructor for class X is trivial if [...]
824 // [...]
825 // -- for each non-static data member of X that is of class type (or
826 // an array thereof), the constructor selected to copy/move that
827 // member is trivial;
828 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000829 if (!FieldRec->hasTrivialCopyConstructor())
830 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000831 if (!FieldRec->hasTrivialMoveConstructor())
832 data().HasTrivialMoveConstructor = false;
833
834 // C++0x [class.copy]p27:
835 // A copy/move assignment operator for class X is trivial if [...]
836 // [...]
837 // -- for each non-static data member of X that is of class type (or
838 // an array thereof), the assignment operator selected to
839 // copy/move that member is trivial;
840 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000841 if (!FieldRec->hasTrivialCopyAssignment())
842 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000843 if (!FieldRec->hasTrivialMoveAssignment())
844 data().HasTrivialMoveAssignment = false;
845
Douglas Gregor85606eb2010-09-28 20:50:54 +0000846 if (!FieldRec->hasTrivialDestructor())
847 data().HasTrivialDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000848 if (FieldRec->hasObjectMember())
849 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000850
851 // C++0x [class]p7:
852 // A standard-layout class is a class that:
853 // -- has no non-static data members of type non-standard-layout
854 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000855 if (!FieldRec->isStandardLayout())
856 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000857
858 // C++0x [class]p7:
859 // A standard-layout class is a class that:
860 // [...]
861 // -- has no base classes of the same type as the first non-static
862 // data member.
863 // We don't want to expend bits in the state of the record decl
864 // tracking whether this is the first non-static data member so we
865 // cheat a bit and use some of the existing state: the empty bit.
866 // Virtual bases and virtual methods make a class non-empty, but they
867 // also make it non-standard-layout so we needn't check here.
868 // A non-empty base class may leave the class standard-layout, but not
869 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000870 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000871 // data member must come through here with Empty still true, and Empty
872 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000873 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000874 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
875 BE = bases_end();
876 BI != BE; ++BI) {
877 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000878 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000879 break;
880 }
881 }
882 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000883
884 // Keep track of the presence of mutable fields.
885 if (FieldRec->hasMutableFields())
886 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000887
888 // C++11 [class.copy]p13:
889 // If the implicitly-defined constructor would satisfy the
890 // requirements of a constexpr constructor, the implicitly-defined
891 // constructor is constexpr.
892 // C++11 [dcl.constexpr]p4:
893 // -- every constructor involved in initializing non-static data
894 // members [...] shall be a constexpr constructor
895 if (!Field->hasInClassInitializer() &&
896 !FieldRec->hasConstexprDefaultConstructor())
897 // The standard requires any in-class initializer to be a constant
898 // expression. We consider this to be a defect.
899 data().DefaultedDefaultConstructorIsConstexpr = false;
900
901 if (!FieldRec->hasConstexprCopyConstructor())
902 data().DefaultedCopyConstructorIsConstexpr = false;
903
904 if (FieldRec->hasDeclaredMoveConstructor() ||
905 FieldRec->needsImplicitMoveConstructor())
906 // FIXME: If the implicit move constructor generated for the member's
907 // class would be ill-formed, the implicit move constructor generated
908 // for this class calls the member's copy constructor.
909 data().DefaultedMoveConstructorIsConstexpr &=
910 FieldRec->hasConstexprMoveConstructor();
911 else if (!FieldRec->hasConstexprCopyConstructor())
912 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000913 }
Richard Smith61802452011-12-22 02:22:31 +0000914 } else {
915 // Base element type of field is a non-class type.
916 if (!T->isLiteralType()) {
917 data().DefaultedDefaultConstructorIsConstexpr = false;
918 data().DefaultedCopyConstructorIsConstexpr = false;
919 data().DefaultedMoveConstructorIsConstexpr = false;
920 } else if (!Field->hasInClassInitializer())
921 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000922 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000923
924 // C++0x [class]p7:
925 // A standard-layout class is a class that:
926 // [...]
927 // -- either has no non-static data members in the most derived
928 // class and at most one base class with non-static data members,
929 // or has no base classes with non-static data members, and
930 // At this point we know that we have a non-static data member, so the last
931 // clause holds.
932 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000933 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000934
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000935 // If this is not a zero-length bit-field, then the class is not empty.
936 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000937 if (!Field->isBitField() ||
938 (!Field->getBitWidth()->isTypeDependent() &&
939 !Field->getBitWidth()->isValueDependent() &&
940 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000941 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000942 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000943 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000944
945 // Handle using declarations of conversion functions.
946 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
947 if (Shadow->getDeclName().getNameKind()
948 == DeclarationName::CXXConversionFunctionName)
949 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000950}
951
John McCallb05b5f32010-03-15 09:07:48 +0000952static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
953 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000954 if (isa<UsingShadowDecl>(Conv))
955 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000956 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
957 T = ConvTemp->getTemplatedDecl()->getResultType();
958 else
959 T = cast<CXXConversionDecl>(Conv)->getConversionType();
960 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000961}
962
John McCallb05b5f32010-03-15 09:07:48 +0000963/// Collect the visible conversions of a base class.
964///
965/// \param Base a base class of the class we're considering
966/// \param InVirtual whether this base class is a virtual base (or a base
967/// of a virtual base)
968/// \param Access the access along the inheritance path to this base
969/// \param ParentHiddenTypes the conversions provided by the inheritors
970/// of this base
971/// \param Output the set to which to add conversions from non-virtual bases
972/// \param VOutput the set to which to add conversions from virtual bases
973/// \param HiddenVBaseCs the set of conversions which were hidden in a
974/// virtual base along some inheritance path
975static void CollectVisibleConversions(ASTContext &Context,
976 CXXRecordDecl *Record,
977 bool InVirtual,
978 AccessSpecifier Access,
979 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
980 UnresolvedSetImpl &Output,
981 UnresolvedSetImpl &VOutput,
982 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
983 // The set of types which have conversions in this class or its
984 // subclasses. As an optimization, we don't copy the derived set
985 // unless it might change.
986 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
987 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
988
989 // Collect the direct conversions and figure out which conversions
990 // will be hidden in the subclasses.
991 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
992 if (!Cs.empty()) {
993 HiddenTypesBuffer = ParentHiddenTypes;
994 HiddenTypes = &HiddenTypesBuffer;
995
996 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
997 bool Hidden =
998 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
999
1000 // If this conversion is hidden and we're in a virtual base,
1001 // remember that it's hidden along some inheritance path.
1002 if (Hidden && InVirtual)
1003 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1004
1005 // If this conversion isn't hidden, add it to the appropriate output.
1006 else if (!Hidden) {
1007 AccessSpecifier IAccess
1008 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1009
1010 if (InVirtual)
1011 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001012 else
John McCallb05b5f32010-03-15 09:07:48 +00001013 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001014 }
1015 }
1016 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001017
John McCallb05b5f32010-03-15 09:07:48 +00001018 // Collect information recursively from any base classes.
1019 for (CXXRecordDecl::base_class_iterator
1020 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1021 const RecordType *RT = I->getType()->getAs<RecordType>();
1022 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001023
John McCallb05b5f32010-03-15 09:07:48 +00001024 AccessSpecifier BaseAccess
1025 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1026 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001027
John McCallb05b5f32010-03-15 09:07:48 +00001028 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1029 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1030 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001031 }
John McCallb05b5f32010-03-15 09:07:48 +00001032}
Sebastian Redl9994a342009-10-25 17:03:50 +00001033
John McCallb05b5f32010-03-15 09:07:48 +00001034/// Collect the visible conversions of a class.
1035///
1036/// This would be extremely straightforward if it weren't for virtual
1037/// bases. It might be worth special-casing that, really.
1038static void CollectVisibleConversions(ASTContext &Context,
1039 CXXRecordDecl *Record,
1040 UnresolvedSetImpl &Output) {
1041 // The collection of all conversions in virtual bases that we've
1042 // found. These will be added to the output as long as they don't
1043 // appear in the hidden-conversions set.
1044 UnresolvedSet<8> VBaseCs;
1045
1046 // The set of conversions in virtual bases that we've determined to
1047 // be hidden.
1048 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1049
1050 // The set of types hidden by classes derived from this one.
1051 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1052
1053 // Go ahead and collect the direct conversions and add them to the
1054 // hidden-types set.
1055 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1056 Output.append(Cs.begin(), Cs.end());
1057 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1058 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1059
1060 // Recursively collect conversions from base classes.
1061 for (CXXRecordDecl::base_class_iterator
1062 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1063 const RecordType *RT = I->getType()->getAs<RecordType>();
1064 if (!RT) continue;
1065
1066 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1067 I->isVirtual(), I->getAccessSpecifier(),
1068 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1069 }
1070
1071 // Add any unhidden conversions provided by virtual bases.
1072 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1073 I != E; ++I) {
1074 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1075 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001076 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001077}
1078
1079/// getVisibleConversionFunctions - get all conversion functions visible
1080/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +00001081const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001082 // If root class, all conversions are visible.
1083 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +00001084 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +00001085 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +00001086 if (data().ComputedVisibleConversions)
1087 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +00001088 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001089 data().ComputedVisibleConversions = true;
1090 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001091}
1092
John McCall32daa422010-03-31 01:36:47 +00001093void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1094 // This operation is O(N) but extremely rare. Sema only uses it to
1095 // remove UsingShadowDecls in a class that were followed by a direct
1096 // declaration, e.g.:
1097 // class A : B {
1098 // using B::operator int;
1099 // operator int();
1100 // };
1101 // This is uncommon by itself and even more uncommon in conjunction
1102 // with sufficiently large numbers of directly-declared conversions
1103 // that asymptotic behavior matters.
1104
1105 UnresolvedSetImpl &Convs = *getConversionFunctions();
1106 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1107 if (Convs[I].getDecl() == ConvDecl) {
1108 Convs.erase(I);
1109 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1110 && "conversion was found multiple times in unresolved set");
1111 return;
1112 }
1113 }
1114
1115 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001116}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001117
Douglas Gregorf6b11852009-10-08 15:14:33 +00001118CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001119 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001120 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1121
1122 return 0;
1123}
1124
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001125MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1126 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1127}
1128
Douglas Gregorf6b11852009-10-08 15:14:33 +00001129void
1130CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1131 TemplateSpecializationKind TSK) {
1132 assert(TemplateOrInstantiation.isNull() &&
1133 "Previous template or instantiation?");
1134 assert(!isa<ClassTemplateSpecializationDecl>(this));
1135 TemplateOrInstantiation
1136 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1137}
1138
Anders Carlssonb13e3572009-12-07 06:33:48 +00001139TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1140 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001141 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1142 return Spec->getSpecializationKind();
1143
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001144 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001145 return MSInfo->getTemplateSpecializationKind();
1146
1147 return TSK_Undeclared;
1148}
1149
1150void
1151CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1152 if (ClassTemplateSpecializationDecl *Spec
1153 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1154 Spec->setSpecializationKind(TSK);
1155 return;
1156 }
1157
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001158 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001159 MSInfo->setTemplateSpecializationKind(TSK);
1160 return;
1161 }
1162
David Blaikieb219cfc2011-09-23 05:06:16 +00001163 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001164}
1165
Douglas Gregor1d110e02010-07-01 14:13:13 +00001166CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1167 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001168 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001169
1170 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001171 = Context.DeclarationNames.getCXXDestructorName(
1172 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001173
John McCallc0bf4622010-02-23 00:48:20 +00001174 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001175 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001176 if (I == E)
1177 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001179 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001180 return Dtor;
1181}
1182
Douglas Gregorda2142f2011-02-19 18:51:44 +00001183void CXXRecordDecl::completeDefinition() {
1184 completeDefinition(0);
1185}
1186
1187void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1188 RecordDecl::completeDefinition();
1189
John McCallf85e1932011-06-15 23:02:42 +00001190 if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) {
1191 // Objective-C Automatic Reference Counting:
1192 // If a class has a non-static data member of Objective-C pointer
1193 // type (or array thereof), it is a non-POD type and its
1194 // default constructor (if any), copy constructor, copy assignment
1195 // operator, and destructor are non-trivial.
1196 struct DefinitionData &Data = data();
1197 Data.PlainOldData = false;
1198 Data.HasTrivialDefaultConstructor = false;
1199 Data.HasTrivialCopyConstructor = false;
1200 Data.HasTrivialCopyAssignment = false;
1201 Data.HasTrivialDestructor = false;
1202 }
1203
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001204 // If the class may be abstract (but hasn't been marked as such), check for
1205 // any pure final overriders.
1206 if (mayBeAbstract()) {
1207 CXXFinalOverriderMap MyFinalOverriders;
1208 if (!FinalOverriders) {
1209 getFinalOverriders(MyFinalOverriders);
1210 FinalOverriders = &MyFinalOverriders;
1211 }
1212
1213 bool Done = false;
1214 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1215 MEnd = FinalOverriders->end();
1216 M != MEnd && !Done; ++M) {
1217 for (OverridingMethods::iterator SO = M->second.begin(),
1218 SOEnd = M->second.end();
1219 SO != SOEnd && !Done; ++SO) {
1220 assert(SO->second.size() > 0 &&
1221 "All virtual functions have overridding virtual functions");
1222
1223 // C++ [class.abstract]p4:
1224 // A class is abstract if it contains or inherits at least one
1225 // pure virtual function for which the final overrider is pure
1226 // virtual.
1227 if (SO->second.front().Method->isPure()) {
1228 data().Abstract = true;
1229 Done = true;
1230 break;
1231 }
1232 }
1233 }
1234 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001235
1236 // Set access bits correctly on the directly-declared conversions.
1237 for (UnresolvedSetIterator I = data().Conversions.begin(),
1238 E = data().Conversions.end();
1239 I != E; ++I)
1240 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001241}
1242
1243bool CXXRecordDecl::mayBeAbstract() const {
1244 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1245 isDependentContext())
1246 return false;
1247
1248 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1249 BEnd = bases_end();
1250 B != BEnd; ++B) {
1251 CXXRecordDecl *BaseDecl
1252 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1253 if (BaseDecl->isAbstract())
1254 return true;
1255 }
1256
1257 return false;
1258}
1259
David Blaikie99ba9e32011-12-20 02:48:34 +00001260void CXXMethodDecl::anchor() { }
1261
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001262CXXMethodDecl *
1263CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001264 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001265 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001266 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001267 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001268 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001269 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001270 isStatic, SCAsWritten, isInline, isConstexpr,
1271 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001272}
1273
Douglas Gregor90916562009-09-29 18:16:17 +00001274bool CXXMethodDecl::isUsualDeallocationFunction() const {
1275 if (getOverloadedOperator() != OO_Delete &&
1276 getOverloadedOperator() != OO_Array_Delete)
1277 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001278
1279 // C++ [basic.stc.dynamic.deallocation]p2:
1280 // A template instance is never a usual deallocation function,
1281 // regardless of its signature.
1282 if (getPrimaryTemplate())
1283 return false;
1284
Douglas Gregor90916562009-09-29 18:16:17 +00001285 // C++ [basic.stc.dynamic.deallocation]p2:
1286 // If a class T has a member deallocation function named operator delete
1287 // with exactly one parameter, then that function is a usual (non-placement)
1288 // deallocation function. [...]
1289 if (getNumParams() == 1)
1290 return true;
1291
1292 // C++ [basic.stc.dynamic.deallocation]p2:
1293 // [...] If class T does not declare such an operator delete but does
1294 // declare a member deallocation function named operator delete with
1295 // exactly two parameters, the second of which has type std::size_t (18.1),
1296 // then this function is a usual deallocation function.
1297 ASTContext &Context = getASTContext();
1298 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001299 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1300 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001301 return false;
1302
1303 // This function is a usual deallocation function if there are no
1304 // single-parameter deallocation functions of the same kind.
1305 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1306 R.first != R.second; ++R.first) {
1307 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1308 if (FD->getNumParams() == 1)
1309 return false;
1310 }
1311
1312 return true;
1313}
1314
Douglas Gregor06a9f362010-05-01 20:49:11 +00001315bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001316 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001317 // A user-declared copy assignment operator X::operator= is a non-static
1318 // non-template member function of class X with exactly one parameter of
1319 // type X, X&, const X&, volatile X& or const volatile X&.
1320 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1321 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001322 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001323 return false;
1324
1325 QualType ParamType = getParamDecl(0)->getType();
1326 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1327 ParamType = Ref->getPointeeType();
1328
1329 ASTContext &Context = getASTContext();
1330 QualType ClassType
1331 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1332 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1333}
1334
Sean Huntffe37fd2011-05-25 20:50:04 +00001335bool CXXMethodDecl::isMoveAssignmentOperator() const {
1336 // C++0x [class.copy]p19:
1337 // A user-declared move assignment operator X::operator= is a non-static
1338 // non-template member function of class X with exactly one parameter of type
1339 // X&&, const X&&, volatile X&&, or const volatile X&&.
1340 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1341 getPrimaryTemplate() || getDescribedFunctionTemplate())
1342 return false;
1343
1344 QualType ParamType = getParamDecl(0)->getType();
1345 if (!isa<RValueReferenceType>(ParamType))
1346 return false;
1347 ParamType = ParamType->getPointeeType();
1348
1349 ASTContext &Context = getASTContext();
1350 QualType ClassType
1351 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1352 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1353}
1354
Anders Carlsson05eb2442009-05-16 23:58:37 +00001355void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001356 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001357 assert(!MD->getParent()->isDependentContext() &&
1358 "Can't add an overridden method to a class template!");
1359
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001360 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001361}
1362
1363CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001364 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001365}
1366
1367CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001368 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001369}
1370
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001371unsigned CXXMethodDecl::size_overridden_methods() const {
1372 return getASTContext().overridden_methods_size(this);
1373}
1374
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001375QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001376 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1377 // If the member function is declared const, the type of this is const X*,
1378 // if the member function is declared volatile, the type of this is
1379 // volatile X*, and if the member function is declared const volatile,
1380 // the type of this is const volatile X*.
1381
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001382 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001383
John McCall3cb0ebd2010-03-10 03:28:59 +00001384 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001385 ClassTy = C.getQualifiedType(ClassTy,
1386 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001387 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001388}
1389
Eli Friedmand7d7f672009-12-06 20:50:05 +00001390bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001391 // If this function is a template instantiation, look at the template from
1392 // which it was instantiated.
1393 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1394 if (!CheckFn)
1395 CheckFn = this;
1396
Eli Friedmand7d7f672009-12-06 20:50:05 +00001397 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001398 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001399}
1400
Sean Huntcbb67482011-01-08 20:30:50 +00001401CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1402 TypeSourceInfo *TInfo, bool IsVirtual,
1403 SourceLocation L, Expr *Init,
1404 SourceLocation R,
1405 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001406 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001407 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1408 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001409{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001410}
1411
Sean Huntcbb67482011-01-08 20:30:50 +00001412CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1413 FieldDecl *Member,
1414 SourceLocation MemberLoc,
1415 SourceLocation L, Expr *Init,
1416 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001417 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001418 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001419 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1420{
1421}
1422
Sean Huntcbb67482011-01-08 20:30:50 +00001423CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1424 IndirectFieldDecl *Member,
1425 SourceLocation MemberLoc,
1426 SourceLocation L, Expr *Init,
1427 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001428 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001429 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001430 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001431{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001432}
1433
Sean Huntcbb67482011-01-08 20:30:50 +00001434CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001435 TypeSourceInfo *TInfo,
1436 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001437 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001438 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1439 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001440 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1441{
1442}
1443
1444CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001445 FieldDecl *Member,
1446 SourceLocation MemberLoc,
1447 SourceLocation L, Expr *Init,
1448 SourceLocation R,
1449 VarDecl **Indices,
1450 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001451 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001452 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001453 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001454{
1455 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1456 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1457}
1458
Sean Huntcbb67482011-01-08 20:30:50 +00001459CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1460 FieldDecl *Member,
1461 SourceLocation MemberLoc,
1462 SourceLocation L, Expr *Init,
1463 SourceLocation R,
1464 VarDecl **Indices,
1465 unsigned NumIndices) {
1466 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001467 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001468 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001469 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1470 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001471}
1472
Sean Huntcbb67482011-01-08 20:30:50 +00001473TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001474 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001475 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001476 else
1477 return TypeLoc();
1478}
1479
Sean Huntcbb67482011-01-08 20:30:50 +00001480const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001481 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001482 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001483 else
1484 return 0;
1485}
1486
Sean Huntcbb67482011-01-08 20:30:50 +00001487SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001488 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001489 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001490
1491 if (isInClassMemberInitializer())
1492 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001493
Douglas Gregor76852c22011-11-01 01:16:03 +00001494 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1495 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1496
1497 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001498}
1499
Sean Huntcbb67482011-01-08 20:30:50 +00001500SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001501 if (isInClassMemberInitializer()) {
1502 FieldDecl *D = getAnyMember();
1503 if (Expr *I = D->getInClassInitializer())
1504 return I->getSourceRange();
1505 return SourceRange();
1506 }
1507
Douglas Gregor802ab452009-12-02 22:36:29 +00001508 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001509}
1510
David Blaikie99ba9e32011-12-20 02:48:34 +00001511void CXXConstructorDecl::anchor() { }
1512
Douglas Gregorb48fe382008-10-31 09:07:45 +00001513CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001514CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001515 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001516 QualType(), 0, false, false, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001517}
1518
1519CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001520CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001521 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001522 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001523 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001524 bool isExplicit, bool isInline,
1525 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001526 assert(NameInfo.getName().getNameKind()
1527 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001528 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001529 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001530 isExplicit, isInline, isImplicitlyDeclared,
1531 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001532}
1533
Douglas Gregor76852c22011-11-01 01:16:03 +00001534CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1535 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1536 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1537 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1538 return Construct->getConstructor();
1539
1540 return 0;
1541}
1542
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001543bool CXXConstructorDecl::isDefaultConstructor() const {
1544 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001545 // A default constructor for a class X is a constructor of class
1546 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001547 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001548 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001549}
1550
Mike Stump1eb44332009-09-09 15:08:12 +00001551bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001552CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001553 return isCopyOrMoveConstructor(TypeQuals) &&
1554 getParamDecl(0)->getType()->isLValueReferenceType();
1555}
1556
1557bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1558 return isCopyOrMoveConstructor(TypeQuals) &&
1559 getParamDecl(0)->getType()->isRValueReferenceType();
1560}
1561
1562/// \brief Determine whether this is a copy or move constructor.
1563bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001564 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001565 // A non-template constructor for class X is a copy constructor
1566 // if its first parameter is of type X&, const X&, volatile X& or
1567 // const volatile X&, and either there are no other parameters
1568 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001569 // C++0x [class.copy]p3:
1570 // A non-template constructor for class X is a move constructor if its
1571 // first parameter is of type X&&, const X&&, volatile X&&, or
1572 // const volatile X&&, and either there are no other parameters or else
1573 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001574 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001575 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001576 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001577 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001578 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001579
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001580 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001581
1582 // Do we have a reference type?
1583 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001584 if (!ParamRefType)
1585 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001586
Douglas Gregorfd476482009-11-13 23:59:09 +00001587 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001588 ASTContext &Context = getASTContext();
1589
Douglas Gregorfd476482009-11-13 23:59:09 +00001590 CanQualType PointeeType
1591 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001592 CanQualType ClassTy
1593 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001594 if (PointeeType.getUnqualifiedType() != ClassTy)
1595 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001596
John McCall0953e762009-09-24 19:53:00 +00001597 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001598
1599 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001600 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001601 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001602}
1603
Anders Carlssonfaccd722009-08-28 16:57:08 +00001604bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001605 // C++ [class.conv.ctor]p1:
1606 // A constructor declared without the function-specifier explicit
1607 // that can be called with a single parameter specifies a
1608 // conversion from the type of its first parameter to the type of
1609 // its class. Such a constructor is called a converting
1610 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001611 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001612 return false;
1613
Mike Stump1eb44332009-09-09 15:08:12 +00001614 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001615 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001616 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001617 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001618}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001619
Douglas Gregor6493cc52010-11-08 17:16:59 +00001620bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001621 if ((getNumParams() < 1) ||
1622 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1623 (getPrimaryTemplate() == 0) ||
1624 (getDescribedFunctionTemplate() != 0))
1625 return false;
1626
1627 const ParmVarDecl *Param = getParamDecl(0);
1628
1629 ASTContext &Context = getASTContext();
1630 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1631
Douglas Gregor66724ea2009-11-14 01:20:54 +00001632 // Is it the same as our our class type?
1633 CanQualType ClassTy
1634 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1635 if (ParamType.getUnqualifiedType() != ClassTy)
1636 return false;
1637
1638 return true;
1639}
1640
Sebastian Redlf677ea32011-02-05 19:23:19 +00001641const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1642 // Hack: we store the inherited constructor in the overridden method table
1643 method_iterator It = begin_overridden_methods();
1644 if (It == end_overridden_methods())
1645 return 0;
1646
1647 return cast<CXXConstructorDecl>(*It);
1648}
1649
1650void
1651CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1652 // Hack: we store the inherited constructor in the overridden method table
1653 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1654 addOverriddenMethod(BaseCtor);
1655}
1656
David Blaikie99ba9e32011-12-20 02:48:34 +00001657void CXXDestructorDecl::anchor() { }
1658
Douglas Gregor42a552f2008-11-05 20:51:48 +00001659CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001660CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001661 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001662 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001663}
1664
1665CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001666CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001667 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001668 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001669 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001670 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001671 assert(NameInfo.getName().getNameKind()
1672 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001673 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001674 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001675 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001676}
1677
David Blaikie99ba9e32011-12-20 02:48:34 +00001678void CXXConversionDecl::anchor() { }
1679
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001680CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001681CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001682 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001683 QualType(), 0, false, false, false,
Douglas Gregorf5251602011-03-08 17:10:18 +00001684 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001685}
1686
1687CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001688CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001689 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001690 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001691 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001692 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001693 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001694 assert(NameInfo.getName().getNameKind()
1695 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001696 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001697 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001698 isInline, isExplicit, isConstexpr,
1699 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001700}
1701
David Blaikie99ba9e32011-12-20 02:48:34 +00001702void LinkageSpecDecl::anchor() { }
1703
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001704LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001705 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001706 SourceLocation ExternLoc,
1707 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001708 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001709 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001710 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001711}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001712
David Blaikie99ba9e32011-12-20 02:48:34 +00001713void UsingDirectiveDecl::anchor() { }
1714
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001715UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1716 SourceLocation L,
1717 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001718 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001719 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001720 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001721 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001722 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1723 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001724 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1725 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001726}
1727
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001728NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1729 if (NamespaceAliasDecl *NA =
1730 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1731 return NA->getNamespace();
1732 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1733}
1734
David Blaikie99ba9e32011-12-20 02:48:34 +00001735void NamespaceAliasDecl::anchor() { }
1736
Mike Stump1eb44332009-09-09 15:08:12 +00001737NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001738 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001739 SourceLocation AliasLoc,
1740 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001741 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001742 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001743 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001744 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1745 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001746 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1747 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001748}
1749
David Blaikie99ba9e32011-12-20 02:48:34 +00001750void UsingShadowDecl::anchor() { }
1751
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001752UsingDecl *UsingShadowDecl::getUsingDecl() const {
1753 const UsingShadowDecl *Shadow = this;
1754 while (const UsingShadowDecl *NextShadow =
1755 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1756 Shadow = NextShadow;
1757 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1758}
1759
David Blaikie99ba9e32011-12-20 02:48:34 +00001760void UsingDecl::anchor() { }
1761
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001762void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1763 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1764 "declaration already in set");
1765 assert(S->getUsingDecl() == this);
1766
1767 if (FirstUsingShadow)
1768 S->UsingOrNextShadow = FirstUsingShadow;
1769 FirstUsingShadow = S;
1770}
1771
1772void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1773 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1774 "declaration not in set");
1775 assert(S->getUsingDecl() == this);
1776
1777 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1778
1779 if (FirstUsingShadow == S) {
1780 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1781 S->UsingOrNextShadow = this;
1782 return;
1783 }
1784
1785 UsingShadowDecl *Prev = FirstUsingShadow;
1786 while (Prev->UsingOrNextShadow != S)
1787 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1788 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1789 S->UsingOrNextShadow = this;
1790}
1791
Douglas Gregordc355712011-02-25 00:36:19 +00001792UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1793 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001794 const DeclarationNameInfo &NameInfo,
1795 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001796 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001797}
1798
David Blaikie99ba9e32011-12-20 02:48:34 +00001799void UnresolvedUsingValueDecl::anchor() { }
1800
John McCall7ba107a2009-11-18 02:36:19 +00001801UnresolvedUsingValueDecl *
1802UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1803 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001804 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001805 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001806 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001807 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001808}
1809
David Blaikie99ba9e32011-12-20 02:48:34 +00001810void UnresolvedUsingTypenameDecl::anchor() { }
1811
John McCall7ba107a2009-11-18 02:36:19 +00001812UnresolvedUsingTypenameDecl *
1813UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1814 SourceLocation UsingLoc,
1815 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001816 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001817 SourceLocation TargetNameLoc,
1818 DeclarationName TargetName) {
1819 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001820 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001821 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001822}
1823
David Blaikie99ba9e32011-12-20 02:48:34 +00001824void StaticAssertDecl::anchor() { }
1825
Anders Carlssonfb311762009-03-14 00:25:26 +00001826StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001827 SourceLocation StaticAssertLoc,
1828 Expr *AssertExpr,
1829 StringLiteral *Message,
1830 SourceLocation RParenLoc) {
1831 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1832 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001833}
1834
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001835static const char *getAccessName(AccessSpecifier AS) {
1836 switch (AS) {
1837 default:
1838 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00001839 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001840 case AS_public:
1841 return "public";
1842 case AS_private:
1843 return "private";
1844 case AS_protected:
1845 return "protected";
1846 }
1847}
1848
1849const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1850 AccessSpecifier AS) {
1851 return DB << getAccessName(AS);
1852}