blob: ddb61e59502a1b9383f1c9636b045560ff32218b [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 Smith6b8bc072011-08-10 18:11:37 +000041 HasConstexprNonCopyMoveConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000042 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
43 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
44 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000045 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000046 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
47 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sebastian Redl85ea7aa2011-08-30 19:58:05 +000048 DeclaredDestructor(false), FailedImplicitMoveConstructor(false),
49 FailedImplicitMoveAssignment(false), NumBases(0), NumVBases(0), Bases(),
50 VBases(), Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000051}
52
53CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000054 SourceLocation StartLoc, SourceLocation IdLoc,
55 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
56 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000057 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000058 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000059
Jay Foad4ba2a172011-01-12 09:06:06 +000060CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000061 DeclContext *DC, SourceLocation StartLoc,
62 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000063 CXXRecordDecl* PrevDecl,
64 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000065 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
66 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000067
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000068 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000069 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000070 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000071 return R;
72}
73
Jay Foad4ba2a172011-01-12 09:06:06 +000074CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000075 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
76 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000077}
78
Mike Stump1eb44332009-09-09 15:08:12 +000079void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000080CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000081 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000082 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +000083
Douglas Gregor7c789c12010-10-29 22:39:52 +000084 if (!data().Bases.isOffset() && data().NumBases > 0)
85 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000086
Richard Smithdd677232011-10-18 20:08:55 +000087 if (NumBases) {
88 // C++ [dcl.init.aggr]p1:
89 // An aggregate is [...] a class with [...] no base classes [...].
90 data().Aggregate = false;
91
92 // C++ [class]p4:
93 // A POD-struct is an aggregate class...
94 data().PlainOldData = false;
95 }
96
Anders Carlsson6f6de732010-03-29 05:13:12 +000097 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000098 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000099
100 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000101 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
John McCall86ff3082010-02-04 22:26:26 +0000103 data().Bases = new(C) CXXBaseSpecifier [NumBases];
104 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000105 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000106 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000107 // Keep track of inherited vbases for this base class.
108 const CXXBaseSpecifier *Base = Bases[i];
109 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000110 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000111 if (BaseType->isDependentType())
112 continue;
113 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000114 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000115
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000116 // A class with a non-empty base class is not empty.
117 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000118 if (!BaseClassDecl->isEmpty()) {
119 if (!data().Empty) {
120 // C++0x [class]p7:
121 // A standard-layout class is a class that:
122 // [...]
123 // -- either has no non-static data members in the most derived
124 // class and at most one base class with non-static data members,
125 // or has no base classes with non-static data members, and
126 // If this is the second non-empty base, then neither of these two
127 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000128 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000129 }
130
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000131 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000132 data().HasNoNonEmptyBases = false;
133 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000134
Douglas Gregor85606eb2010-09-28 20:50:54 +0000135 // C++ [class.virtual]p1:
136 // A class that declares or inherits a virtual function is called a
137 // polymorphic class.
138 if (BaseClassDecl->isPolymorphic())
139 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000140
Chandler Carrutha8225442011-04-30 09:17:45 +0000141 // C++0x [class]p7:
142 // A standard-layout class is a class that: [...]
143 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000144 if (!BaseClassDecl->isStandardLayout())
145 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000146
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000147 // Record if this base is the first non-literal field or base.
148 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
149 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000150
Anders Carlsson6f6de732010-03-29 05:13:12 +0000151 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000152 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000153 BaseClassDecl->vbases_begin(),
154 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000155 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000156 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000157 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000158 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000159
160 if (Base->isVirtual()) {
161 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000162 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000163 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000164
165 // C++0x [meta.unary.prop] is_empty:
166 // T is a class type, but not a union type, with ... no virtual base
167 // classes
168 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000169
170 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000171 // A default constructor is trivial [...] if:
172 // -- its class has [...] no virtual bases
173 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000174
175 // C++0x [class.copy]p13:
176 // A copy/move constructor for class X is trivial if it is neither
177 // user-provided nor deleted and if
178 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000179 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000180 data().HasTrivialMoveConstructor = false;
181
182 // C++0x [class.copy]p27:
183 // A copy/move assignment operator for class X is trivial if it is
184 // neither user-provided nor deleted and if
185 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000186 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000187 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000188
189 // C++0x [class]p7:
190 // A standard-layout class is a class that: [...]
191 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000192 data().IsStandardLayout = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000193 } else {
194 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000195 // A default constructor is trivial [...] if:
196 // -- all the direct base classes of its class have trivial default
197 // constructors.
198 if (!BaseClassDecl->hasTrivialDefaultConstructor())
199 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000200
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000201 // C++0x [class.copy]p13:
202 // A copy/move constructor for class X is trivial if [...]
203 // [...]
204 // -- the constructor selected to copy/move each direct base class
205 // subobject is trivial, and
206 // FIXME: C++0x: We need to only consider the selected constructor
207 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000208 if (!BaseClassDecl->hasTrivialCopyConstructor())
209 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000210 if (!BaseClassDecl->hasTrivialMoveConstructor())
211 data().HasTrivialMoveConstructor = false;
212
213 // C++0x [class.copy]p27:
214 // A copy/move assignment operator for class X is trivial if [...]
215 // [...]
216 // -- the assignment operator selected to copy/move each direct base
217 // class subobject is trivial, and
218 // FIXME: C++0x: We need to only consider the selected operator instead
219 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000220 if (!BaseClassDecl->hasTrivialCopyAssignment())
221 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000222 if (!BaseClassDecl->hasTrivialMoveAssignment())
223 data().HasTrivialMoveAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000224 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000225
226 // C++ [class.ctor]p3:
227 // A destructor is trivial if all the direct base classes of its class
228 // have trivial destructors.
229 if (!BaseClassDecl->hasTrivialDestructor())
230 data().HasTrivialDestructor = false;
Douglas Gregor2bb11012011-05-13 01:05:07 +0000231
John McCallf85e1932011-06-15 23:02:42 +0000232 // A class has an Objective-C object member if... or any of its bases
233 // has an Objective-C object member.
234 if (BaseClassDecl->hasObjectMember())
235 setHasObjectMember(true);
236
Douglas Gregor2bb11012011-05-13 01:05:07 +0000237 // Keep track of the presence of mutable fields.
238 if (BaseClassDecl->hasMutableFields())
239 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000240 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000241
242 if (VBases.empty())
243 return;
244
245 // Create base specifier for any direct or indirect virtual bases.
246 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
247 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000248 for (int I = 0, E = VBases.size(); I != E; ++I)
249 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000250}
251
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000252/// Callback function for CXXRecordDecl::forallBases that acknowledges
253/// that it saw a base class.
254static bool SawBase(const CXXRecordDecl *, void *) {
255 return true;
256}
257
258bool CXXRecordDecl::hasAnyDependentBases() const {
259 if (!isDependentContext())
260 return false;
261
262 return !forallBases(SawBase, 0);
263}
264
Sean Huntffe37fd2011-05-25 20:50:04 +0000265bool CXXRecordDecl::hasConstCopyConstructor() const {
266 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000267}
268
Chandler Carruthb7e95892011-04-23 10:47:28 +0000269bool CXXRecordDecl::isTriviallyCopyable() const {
270 // C++0x [class]p5:
271 // A trivially copyable class is a class that:
272 // -- has no non-trivial copy constructors,
273 if (!hasTrivialCopyConstructor()) return false;
274 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000275 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000276 // -- has no non-trivial copy assignment operators,
277 if (!hasTrivialCopyAssignment()) return false;
278 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000279 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000280 // -- has a trivial destructor.
281 if (!hasTrivialDestructor()) return false;
282
283 return true;
284}
285
Douglas Gregor0d405db2010-07-01 20:59:04 +0000286/// \brief Perform a simplistic form of overload resolution that only considers
287/// cv-qualifiers on a single parameter, and return the best overload candidate
288/// (if there is one).
289static CXXMethodDecl *
290GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000291 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000292 if (Cands.empty())
293 return 0;
294 if (Cands.size() == 1)
295 return Cands[0].first;
296
297 unsigned Best = 0, N = Cands.size();
298 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000299 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000300 Best = I;
301
302 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000303 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000304 return 0;
305
306 return Cands[Best].first;
307}
308
Sean Huntffe37fd2011-05-25 20:50:04 +0000309CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
310 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000311 QualType ClassType
312 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000313 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000314 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000315 Context.getCanonicalType(ClassType));
316 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000317 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000318 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000319 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000320 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000321 // C++ [class.copy]p2:
322 // A non-template constructor for class X is a copy constructor if [...]
323 if (isa<FunctionTemplateDecl>(*Con))
324 continue;
325
Douglas Gregor0d405db2010-07-01 20:59:04 +0000326 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
327 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000328 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
329 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000330 Found.push_back(std::make_pair(
331 const_cast<CXXConstructorDecl *>(Constructor),
332 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000333 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000334 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000335
336 return cast_or_null<CXXConstructorDecl>(
337 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000338}
339
Sean Huntffe37fd2011-05-25 20:50:04 +0000340CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
341 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
342 if (I->isMoveConstructor())
343 return *I;
344
345 return 0;
346}
347
Douglas Gregorb87786f2010-07-01 17:48:08 +0000348CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
349 ASTContext &Context = getASTContext();
350 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
351 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
352
Chris Lattner5f9e2722011-07-23 10:55:15 +0000353 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000354 DeclContext::lookup_const_iterator Op, OpEnd;
355 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
356 // C++ [class.copy]p9:
357 // A user-declared copy assignment operator is a non-static non-template
358 // member function of class X with exactly one parameter of type X, X&,
359 // const X&, volatile X& or const volatile X&.
360 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
361 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
362 continue;
363
364 const FunctionProtoType *FnType
365 = Method->getType()->getAs<FunctionProtoType>();
366 assert(FnType && "Overloaded operator has no prototype.");
367 // Don't assert on this; an invalid decl might have been left in the AST.
368 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
369 continue;
370
371 QualType ArgType = FnType->getArgType(0);
372 Qualifiers Quals;
373 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
374 ArgType = Ref->getPointeeType();
375 // If we have a const argument and we have a reference to a non-const,
376 // this function does not match.
377 if (ArgIsConst && !ArgType.isConstQualified())
378 continue;
379
380 Quals = ArgType.getQualifiers();
381 } else {
382 // By-value copy-assignment operators are treated like const X&
383 // copy-assignment operators.
384 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
385 }
386
387 if (!Context.hasSameUnqualifiedType(ArgType, Class))
388 continue;
389
390 // Save this copy-assignment operator. It might be "the one".
391 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
392 }
393
394 // Use a simplistic form of overload resolution to find the candidate.
395 return GetBestOverloadCandidateSimple(Found);
396}
397
Sean Huntffe37fd2011-05-25 20:50:04 +0000398CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
399 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
400 if (I->isMoveAssignmentOperator())
401 return *I;
402
403 return 0;
404}
405
Douglas Gregor21386642010-09-28 21:55:22 +0000406void CXXRecordDecl::markedVirtualFunctionPure() {
407 // C++ [class.abstract]p2:
408 // A class is abstract if it has at least one pure virtual function.
409 data().Abstract = true;
410}
411
412void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000413 // Ignore friends and invalid declarations.
414 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000415 return;
416
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000417 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
418 if (FunTmpl)
419 D = FunTmpl->getTemplatedDecl();
420
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000421 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
422 if (Method->isVirtual()) {
423 // C++ [dcl.init.aggr]p1:
424 // An aggregate is an array or a class with [...] no virtual functions.
425 data().Aggregate = false;
426
427 // C++ [class]p4:
428 // A POD-struct is an aggregate class...
429 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000430
431 // Virtual functions make the class non-empty.
432 // FIXME: Standard ref?
433 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000434
435 // C++ [class.virtual]p1:
436 // A class that declares or inherits a virtual function is called a
437 // polymorphic class.
438 data().Polymorphic = true;
439
Sean Hunt023df372011-05-09 18:22:59 +0000440 // C++0x [class.ctor]p5
441 // A default constructor is trivial [...] if:
442 // -- its class has no virtual functions [...]
443 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000444
445 // C++0x [class.copy]p13:
446 // A copy/move constructor for class X is trivial if [...]
447 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000448 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000449 data().HasTrivialMoveConstructor = false;
450
451 // C++0x [class.copy]p27:
452 // A copy/move assignment operator for class X is trivial if [...]
453 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000454 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000455 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000456
Chandler Carrutha8225442011-04-30 09:17:45 +0000457 // C++0x [class]p7:
458 // A standard-layout class is a class that: [...]
459 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000460 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000461 }
462 }
463
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000464 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000465 // Notify that an implicit member was added after the definition
466 // was completed.
467 if (!isBeingDefined())
468 if (ASTMutationListener *L = getASTMutationListener())
469 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000470
Sean Huntffe37fd2011-05-25 20:50:04 +0000471 // If this is a special member function, note that it was added and then
472 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000473 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000474 if (Constructor->isDefaultConstructor())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000475 data().DeclaredDefaultConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000476 else if (Constructor->isCopyConstructor())
477 data().DeclaredCopyConstructor = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000478 else if (Constructor->isMoveConstructor())
479 data().DeclaredMoveConstructor = true;
480 else
481 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000482 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000483 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000484 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000485 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000486 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
487 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000488 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000489 else if (Method->isMoveAssignmentOperator())
490 data().DeclaredMoveAssignment = true;
491 else
492 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000493 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000494 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000495
Sean Huntffe37fd2011-05-25 20:50:04 +0000496NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000497 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000498 }
499
500 // Handle (user-declared) constructors.
501 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
502 // Note that we have a user-declared constructor.
503 data().UserDeclaredConstructor = true;
504
Richard Smith017ab772011-09-05 02:13:09 +0000505 // Technically, "user-provided" is only defined for special member
506 // functions, but the intent of the standard is clearly that it should apply
507 // to all functions.
508 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000509
Sean Hunt023df372011-05-09 18:22:59 +0000510 // C++0x [class.ctor]p5:
511 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000512 if (Constructor->isDefaultConstructor()) {
513 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000514 if (UserProvided) {
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000515 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000516 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000517 }
518 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000519
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000520 // Note when we have a user-declared copy or move constructor, which will
521 // suppress the implicit declaration of those constructors.
522 if (!FunTmpl) {
523 if (Constructor->isCopyConstructor()) {
524 data().UserDeclaredCopyConstructor = true;
525 data().DeclaredCopyConstructor = true;
526
527 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000528 // A copy/move constructor for class X is trivial if it is not
529 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000530 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000531 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000532 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000533 data().UserDeclaredMoveConstructor = true;
534 data().DeclaredMoveConstructor = true;
535
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000536 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000537 // A copy/move constructor for class X is trivial if it is not
538 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000539 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000540 data().HasTrivialMoveConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000541 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000542 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000543 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
544 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000545 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000546 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000547 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000548
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000549 // C++ [dcl.init.aggr]p1:
550 // An aggregate is an array or a class with no user-declared
551 // constructors [...].
552 // C++0x [dcl.init.aggr]p1:
553 // An aggregate is an array or a class with no user-provided
554 // constructors [...].
555 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
556 data().Aggregate = false;
557
558 // C++ [class]p4:
559 // A POD-struct is an aggregate class [...]
560 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
561 // type is technically an aggregate in C++0x since it wouldn't be in 03.
562 data().PlainOldData = false;
563
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000564 return;
565 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000566
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000567 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000568 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000569 data().DeclaredDestructor = true;
570 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000571
572 // C++ [class]p4:
573 // A POD-struct is an aggregate class that has [...] no user-defined
574 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000575 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000576 data().PlainOldData = false;
577
Douglas Gregor45fa5602011-11-07 20:56:01 +0000578 // C++11 [class.dtor]p5:
579 // A destructor is trivial if it is not user-provided and if
580 // -- the destructor is not virtual.
581 if (DD->isUserProvided() || DD->isVirtual())
Sean Huntcf34e752011-05-16 22:41:40 +0000582 data().HasTrivialDestructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000583
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000584 return;
585 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000586
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000587 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000588 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000589 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000590 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000591 // A POD-struct is an aggregate class that [...] has no user-defined
592 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000593 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000594 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000595
Sean Huntffe37fd2011-05-25 20:50:04 +0000596 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000597
Sean Huntffe37fd2011-05-25 20:50:04 +0000598 // Suppress the implicit declaration of a copy constructor.
599 data().UserDeclaredCopyAssignment = true;
600 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000601
Sean Huntffe37fd2011-05-25 20:50:04 +0000602 // C++0x [class.copy]p27:
603 // A copy/move assignment operator for class X is trivial if it is
604 // neither user-provided nor deleted [...]
605 if (Method->isUserProvided())
606 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000607
Sean Huntffe37fd2011-05-25 20:50:04 +0000608 return;
609 }
610
611 if (Method->isMoveAssignmentOperator()) {
612 // This is an extension in C++03 mode, but we'll keep consistency by
613 // taking a move assignment operator to induce non-POD-ness
614 data().PlainOldData = false;
615
616 // This is a move assignment operator.
617 data().UserDeclaredMoveAssignment = true;
618 data().DeclaredMoveAssignment = true;
619
620 // C++0x [class.copy]p27:
621 // A copy/move assignment operator for class X is trivial if it is
622 // neither user-provided nor deleted [...]
623 if (Method->isUserProvided())
624 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000625 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000626
Douglas Gregore80622f2010-09-29 04:25:11 +0000627 // Keep the list of conversion functions up-to-date.
628 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
629 // We don't record specializations.
630 if (Conversion->getPrimaryTemplate())
631 return;
632
633 // FIXME: We intentionally don't use the decl's access here because it
634 // hasn't been set yet. That's really just a misdesign in Sema.
635
636 if (FunTmpl) {
637 if (FunTmpl->getPreviousDeclaration())
638 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
639 FunTmpl);
640 else
641 data().Conversions.addDecl(FunTmpl);
642 } else {
643 if (Conversion->getPreviousDeclaration())
644 data().Conversions.replace(Conversion->getPreviousDeclaration(),
645 Conversion);
646 else
647 data().Conversions.addDecl(Conversion);
648 }
649 }
650
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000651 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000652 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000653
654 // Handle non-static data members.
655 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000656 // C++ [class.bit]p2:
657 // A declaration for a bit-field that omits the identifier declares an
658 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
659 // initialized.
660 if (Field->isUnnamedBitfield())
661 return;
662
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000663 // C++ [dcl.init.aggr]p1:
664 // An aggregate is an array or a class (clause 9) with [...] no
665 // private or protected non-static data members (clause 11).
666 //
667 // A POD must be an aggregate.
668 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
669 data().Aggregate = false;
670 data().PlainOldData = false;
671 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000672
673 // C++0x [class]p7:
674 // A standard-layout class is a class that:
675 // [...]
676 // -- has the same access control for all non-static data members,
677 switch (D->getAccess()) {
678 case AS_private: data().HasPrivateFields = true; break;
679 case AS_protected: data().HasProtectedFields = true; break;
680 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000681 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000682 };
683 if ((data().HasPrivateFields + data().HasProtectedFields +
684 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000685 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000686
Douglas Gregor2bb11012011-05-13 01:05:07 +0000687 // Keep track of the presence of mutable fields.
688 if (Field->isMutable())
689 data().HasMutableFields = true;
690
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000691 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000692 // A POD struct is a class that is both a trivial class and a
693 // standard-layout class, and has no non-static data members of type
694 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000695 //
696 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
697 // that does not explicitly have no lifetime makes the class a non-POD.
698 // However, we delay setting PlainOldData to false in this case so that
699 // Sema has a chance to diagnostic causes where the same class will be
700 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
701 // In this case, the class will become a non-POD class when we complete
702 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000703 ASTContext &Context = getASTContext();
704 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000705 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
706 if (!Context.getLangOptions().ObjCAutoRefCount ||
707 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
708 setHasObjectMember(true);
709 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000710 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000711
Chandler Carrutha8225442011-04-30 09:17:45 +0000712 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000713 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000714
Chandler Carrutha8225442011-04-30 09:17:45 +0000715 // C++0x [class]p7:
716 // A standard-layout class is a class that:
717 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000718 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000719 }
720
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000721 // Record if this field is the first non-literal field or base.
Richard Smith5fa6a042011-10-12 05:08:15 +0000722 // As a slight variation on the standard, we regard mutable members as being
723 // non-literal, since mutating a constexpr variable would break C++11
724 // constant expression semantics.
725 if ((!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType()) ||
726 Field->isMutable())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000727 data().HasNonLiteralTypeFieldsOrBases = true;
728
Richard Smith7a614d82011-06-11 17:19:42 +0000729 if (Field->hasInClassInitializer()) {
730 // C++0x [class]p5:
731 // A default constructor is trivial if [...] no non-static data member
732 // of its class has a brace-or-equal-initializer.
733 data().HasTrivialDefaultConstructor = false;
734
735 // C++0x [dcl.init.aggr]p1:
736 // An aggregate is a [...] class with [...] no
737 // brace-or-equal-initializers for non-static data members.
738 data().Aggregate = false;
739
740 // C++0x [class]p10:
741 // A POD struct is [...] a trivial class.
742 data().PlainOldData = false;
743 }
744
Douglas Gregor85606eb2010-09-28 20:50:54 +0000745 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
746 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
747 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000748 // C++0x [class.ctor]p5:
749 // A defulat constructor is trivial [...] if:
750 // -- for all the non-static data members of its class that are of
751 // class type (or array thereof), each such class has a trivial
752 // default constructor.
753 if (!FieldRec->hasTrivialDefaultConstructor())
754 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000755
756 // C++0x [class.copy]p13:
757 // A copy/move constructor for class X is trivial if [...]
758 // [...]
759 // -- for each non-static data member of X that is of class type (or
760 // an array thereof), the constructor selected to copy/move that
761 // member is trivial;
762 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000763 if (!FieldRec->hasTrivialCopyConstructor())
764 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000765 if (!FieldRec->hasTrivialMoveConstructor())
766 data().HasTrivialMoveConstructor = false;
767
768 // C++0x [class.copy]p27:
769 // A copy/move assignment operator for class X is trivial if [...]
770 // [...]
771 // -- for each non-static data member of X that is of class type (or
772 // an array thereof), the assignment operator selected to
773 // copy/move that member is trivial;
774 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000775 if (!FieldRec->hasTrivialCopyAssignment())
776 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000777 if (!FieldRec->hasTrivialMoveAssignment())
778 data().HasTrivialMoveAssignment = false;
779
Douglas Gregor85606eb2010-09-28 20:50:54 +0000780 if (!FieldRec->hasTrivialDestructor())
781 data().HasTrivialDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000782 if (FieldRec->hasObjectMember())
783 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000784
785 // C++0x [class]p7:
786 // A standard-layout class is a class that:
787 // -- has no non-static data members of type non-standard-layout
788 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000789 if (!FieldRec->isStandardLayout())
790 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000791
792 // C++0x [class]p7:
793 // A standard-layout class is a class that:
794 // [...]
795 // -- has no base classes of the same type as the first non-static
796 // data member.
797 // We don't want to expend bits in the state of the record decl
798 // tracking whether this is the first non-static data member so we
799 // cheat a bit and use some of the existing state: the empty bit.
800 // Virtual bases and virtual methods make a class non-empty, but they
801 // also make it non-standard-layout so we needn't check here.
802 // A non-empty base class may leave the class standard-layout, but not
803 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000804 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000805 // data member must come through here with Empty still true, and Empty
806 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000807 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000808 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
809 BE = bases_end();
810 BI != BE; ++BI) {
811 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000812 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000813 break;
814 }
815 }
816 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000817
818 // Keep track of the presence of mutable fields.
819 if (FieldRec->hasMutableFields())
820 data().HasMutableFields = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000821 }
822 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000823
824 // C++0x [class]p7:
825 // A standard-layout class is a class that:
826 // [...]
827 // -- either has no non-static data members in the most derived
828 // class and at most one base class with non-static data members,
829 // or has no base classes with non-static data members, and
830 // At this point we know that we have a non-static data member, so the last
831 // clause holds.
832 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000833 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000834
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000835 // If this is not a zero-length bit-field, then the class is not empty.
836 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000837 if (!Field->isBitField() ||
838 (!Field->getBitWidth()->isTypeDependent() &&
839 !Field->getBitWidth()->isValueDependent() &&
840 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000841 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000842 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000843 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000844
845 // Handle using declarations of conversion functions.
846 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
847 if (Shadow->getDeclName().getNameKind()
848 == DeclarationName::CXXConversionFunctionName)
849 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000850}
851
John McCallb05b5f32010-03-15 09:07:48 +0000852static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
853 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000854 if (isa<UsingShadowDecl>(Conv))
855 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000856 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
857 T = ConvTemp->getTemplatedDecl()->getResultType();
858 else
859 T = cast<CXXConversionDecl>(Conv)->getConversionType();
860 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000861}
862
John McCallb05b5f32010-03-15 09:07:48 +0000863/// Collect the visible conversions of a base class.
864///
865/// \param Base a base class of the class we're considering
866/// \param InVirtual whether this base class is a virtual base (or a base
867/// of a virtual base)
868/// \param Access the access along the inheritance path to this base
869/// \param ParentHiddenTypes the conversions provided by the inheritors
870/// of this base
871/// \param Output the set to which to add conversions from non-virtual bases
872/// \param VOutput the set to which to add conversions from virtual bases
873/// \param HiddenVBaseCs the set of conversions which were hidden in a
874/// virtual base along some inheritance path
875static void CollectVisibleConversions(ASTContext &Context,
876 CXXRecordDecl *Record,
877 bool InVirtual,
878 AccessSpecifier Access,
879 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
880 UnresolvedSetImpl &Output,
881 UnresolvedSetImpl &VOutput,
882 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
883 // The set of types which have conversions in this class or its
884 // subclasses. As an optimization, we don't copy the derived set
885 // unless it might change.
886 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
887 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
888
889 // Collect the direct conversions and figure out which conversions
890 // will be hidden in the subclasses.
891 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
892 if (!Cs.empty()) {
893 HiddenTypesBuffer = ParentHiddenTypes;
894 HiddenTypes = &HiddenTypesBuffer;
895
896 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
897 bool Hidden =
898 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
899
900 // If this conversion is hidden and we're in a virtual base,
901 // remember that it's hidden along some inheritance path.
902 if (Hidden && InVirtual)
903 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
904
905 // If this conversion isn't hidden, add it to the appropriate output.
906 else if (!Hidden) {
907 AccessSpecifier IAccess
908 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
909
910 if (InVirtual)
911 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000912 else
John McCallb05b5f32010-03-15 09:07:48 +0000913 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000914 }
915 }
916 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000917
John McCallb05b5f32010-03-15 09:07:48 +0000918 // Collect information recursively from any base classes.
919 for (CXXRecordDecl::base_class_iterator
920 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
921 const RecordType *RT = I->getType()->getAs<RecordType>();
922 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000923
John McCallb05b5f32010-03-15 09:07:48 +0000924 AccessSpecifier BaseAccess
925 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
926 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000927
John McCallb05b5f32010-03-15 09:07:48 +0000928 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
929 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
930 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000931 }
John McCallb05b5f32010-03-15 09:07:48 +0000932}
Sebastian Redl9994a342009-10-25 17:03:50 +0000933
John McCallb05b5f32010-03-15 09:07:48 +0000934/// Collect the visible conversions of a class.
935///
936/// This would be extremely straightforward if it weren't for virtual
937/// bases. It might be worth special-casing that, really.
938static void CollectVisibleConversions(ASTContext &Context,
939 CXXRecordDecl *Record,
940 UnresolvedSetImpl &Output) {
941 // The collection of all conversions in virtual bases that we've
942 // found. These will be added to the output as long as they don't
943 // appear in the hidden-conversions set.
944 UnresolvedSet<8> VBaseCs;
945
946 // The set of conversions in virtual bases that we've determined to
947 // be hidden.
948 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
949
950 // The set of types hidden by classes derived from this one.
951 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
952
953 // Go ahead and collect the direct conversions and add them to the
954 // hidden-types set.
955 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
956 Output.append(Cs.begin(), Cs.end());
957 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
958 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
959
960 // Recursively collect conversions from base classes.
961 for (CXXRecordDecl::base_class_iterator
962 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
963 const RecordType *RT = I->getType()->getAs<RecordType>();
964 if (!RT) continue;
965
966 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
967 I->isVirtual(), I->getAccessSpecifier(),
968 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
969 }
970
971 // Add any unhidden conversions provided by virtual bases.
972 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
973 I != E; ++I) {
974 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
975 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000976 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000977}
978
979/// getVisibleConversionFunctions - get all conversion functions visible
980/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000981const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000982 // If root class, all conversions are visible.
983 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000984 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000985 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000986 if (data().ComputedVisibleConversions)
987 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000988 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000989 data().ComputedVisibleConversions = true;
990 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000991}
992
John McCall32daa422010-03-31 01:36:47 +0000993void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
994 // This operation is O(N) but extremely rare. Sema only uses it to
995 // remove UsingShadowDecls in a class that were followed by a direct
996 // declaration, e.g.:
997 // class A : B {
998 // using B::operator int;
999 // operator int();
1000 // };
1001 // This is uncommon by itself and even more uncommon in conjunction
1002 // with sufficiently large numbers of directly-declared conversions
1003 // that asymptotic behavior matters.
1004
1005 UnresolvedSetImpl &Convs = *getConversionFunctions();
1006 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1007 if (Convs[I].getDecl() == ConvDecl) {
1008 Convs.erase(I);
1009 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1010 && "conversion was found multiple times in unresolved set");
1011 return;
1012 }
1013 }
1014
1015 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001016}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001017
Douglas Gregorf6b11852009-10-08 15:14:33 +00001018CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001019 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001020 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1021
1022 return 0;
1023}
1024
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001025MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1026 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1027}
1028
Douglas Gregorf6b11852009-10-08 15:14:33 +00001029void
1030CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1031 TemplateSpecializationKind TSK) {
1032 assert(TemplateOrInstantiation.isNull() &&
1033 "Previous template or instantiation?");
1034 assert(!isa<ClassTemplateSpecializationDecl>(this));
1035 TemplateOrInstantiation
1036 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1037}
1038
Anders Carlssonb13e3572009-12-07 06:33:48 +00001039TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1040 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001041 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1042 return Spec->getSpecializationKind();
1043
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001044 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001045 return MSInfo->getTemplateSpecializationKind();
1046
1047 return TSK_Undeclared;
1048}
1049
1050void
1051CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1052 if (ClassTemplateSpecializationDecl *Spec
1053 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1054 Spec->setSpecializationKind(TSK);
1055 return;
1056 }
1057
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001058 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001059 MSInfo->setTemplateSpecializationKind(TSK);
1060 return;
1061 }
1062
David Blaikieb219cfc2011-09-23 05:06:16 +00001063 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001064}
1065
Douglas Gregor1d110e02010-07-01 14:13:13 +00001066CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1067 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001068 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001069
1070 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001071 = Context.DeclarationNames.getCXXDestructorName(
1072 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001073
John McCallc0bf4622010-02-23 00:48:20 +00001074 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001075 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001076 if (I == E)
1077 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001079 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001080 return Dtor;
1081}
1082
Douglas Gregorda2142f2011-02-19 18:51:44 +00001083void CXXRecordDecl::completeDefinition() {
1084 completeDefinition(0);
1085}
1086
1087void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1088 RecordDecl::completeDefinition();
1089
John McCallf85e1932011-06-15 23:02:42 +00001090 if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) {
1091 // Objective-C Automatic Reference Counting:
1092 // If a class has a non-static data member of Objective-C pointer
1093 // type (or array thereof), it is a non-POD type and its
1094 // default constructor (if any), copy constructor, copy assignment
1095 // operator, and destructor are non-trivial.
1096 struct DefinitionData &Data = data();
1097 Data.PlainOldData = false;
1098 Data.HasTrivialDefaultConstructor = false;
1099 Data.HasTrivialCopyConstructor = false;
1100 Data.HasTrivialCopyAssignment = false;
1101 Data.HasTrivialDestructor = false;
1102 }
1103
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001104 // If the class may be abstract (but hasn't been marked as such), check for
1105 // any pure final overriders.
1106 if (mayBeAbstract()) {
1107 CXXFinalOverriderMap MyFinalOverriders;
1108 if (!FinalOverriders) {
1109 getFinalOverriders(MyFinalOverriders);
1110 FinalOverriders = &MyFinalOverriders;
1111 }
1112
1113 bool Done = false;
1114 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1115 MEnd = FinalOverriders->end();
1116 M != MEnd && !Done; ++M) {
1117 for (OverridingMethods::iterator SO = M->second.begin(),
1118 SOEnd = M->second.end();
1119 SO != SOEnd && !Done; ++SO) {
1120 assert(SO->second.size() > 0 &&
1121 "All virtual functions have overridding virtual functions");
1122
1123 // C++ [class.abstract]p4:
1124 // A class is abstract if it contains or inherits at least one
1125 // pure virtual function for which the final overrider is pure
1126 // virtual.
1127 if (SO->second.front().Method->isPure()) {
1128 data().Abstract = true;
1129 Done = true;
1130 break;
1131 }
1132 }
1133 }
1134 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001135
1136 // Set access bits correctly on the directly-declared conversions.
1137 for (UnresolvedSetIterator I = data().Conversions.begin(),
1138 E = data().Conversions.end();
1139 I != E; ++I)
1140 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001141}
1142
1143bool CXXRecordDecl::mayBeAbstract() const {
1144 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1145 isDependentContext())
1146 return false;
1147
1148 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1149 BEnd = bases_end();
1150 B != BEnd; ++B) {
1151 CXXRecordDecl *BaseDecl
1152 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1153 if (BaseDecl->isAbstract())
1154 return true;
1155 }
1156
1157 return false;
1158}
1159
David Blaikie99ba9e32011-12-20 02:48:34 +00001160void CXXMethodDecl::anchor() { }
1161
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001162CXXMethodDecl *
1163CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001164 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001165 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001166 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001167 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001168 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001169 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001170 isStatic, SCAsWritten, isInline, isConstexpr,
1171 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001172}
1173
Douglas Gregor90916562009-09-29 18:16:17 +00001174bool CXXMethodDecl::isUsualDeallocationFunction() const {
1175 if (getOverloadedOperator() != OO_Delete &&
1176 getOverloadedOperator() != OO_Array_Delete)
1177 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001178
1179 // C++ [basic.stc.dynamic.deallocation]p2:
1180 // A template instance is never a usual deallocation function,
1181 // regardless of its signature.
1182 if (getPrimaryTemplate())
1183 return false;
1184
Douglas Gregor90916562009-09-29 18:16:17 +00001185 // C++ [basic.stc.dynamic.deallocation]p2:
1186 // If a class T has a member deallocation function named operator delete
1187 // with exactly one parameter, then that function is a usual (non-placement)
1188 // deallocation function. [...]
1189 if (getNumParams() == 1)
1190 return true;
1191
1192 // C++ [basic.stc.dynamic.deallocation]p2:
1193 // [...] If class T does not declare such an operator delete but does
1194 // declare a member deallocation function named operator delete with
1195 // exactly two parameters, the second of which has type std::size_t (18.1),
1196 // then this function is a usual deallocation function.
1197 ASTContext &Context = getASTContext();
1198 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001199 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1200 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001201 return false;
1202
1203 // This function is a usual deallocation function if there are no
1204 // single-parameter deallocation functions of the same kind.
1205 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1206 R.first != R.second; ++R.first) {
1207 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1208 if (FD->getNumParams() == 1)
1209 return false;
1210 }
1211
1212 return true;
1213}
1214
Douglas Gregor06a9f362010-05-01 20:49:11 +00001215bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001216 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001217 // A user-declared copy assignment operator X::operator= is a non-static
1218 // non-template member function of class X with exactly one parameter of
1219 // type X, X&, const X&, volatile X& or const volatile X&.
1220 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1221 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001222 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001223 return false;
1224
1225 QualType ParamType = getParamDecl(0)->getType();
1226 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1227 ParamType = Ref->getPointeeType();
1228
1229 ASTContext &Context = getASTContext();
1230 QualType ClassType
1231 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1232 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1233}
1234
Sean Huntffe37fd2011-05-25 20:50:04 +00001235bool CXXMethodDecl::isMoveAssignmentOperator() const {
1236 // C++0x [class.copy]p19:
1237 // A user-declared move assignment operator X::operator= is a non-static
1238 // non-template member function of class X with exactly one parameter of type
1239 // X&&, const X&&, volatile X&&, or const volatile X&&.
1240 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1241 getPrimaryTemplate() || getDescribedFunctionTemplate())
1242 return false;
1243
1244 QualType ParamType = getParamDecl(0)->getType();
1245 if (!isa<RValueReferenceType>(ParamType))
1246 return false;
1247 ParamType = ParamType->getPointeeType();
1248
1249 ASTContext &Context = getASTContext();
1250 QualType ClassType
1251 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1252 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1253}
1254
Anders Carlsson05eb2442009-05-16 23:58:37 +00001255void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001256 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001257 assert(!MD->getParent()->isDependentContext() &&
1258 "Can't add an overridden method to a class template!");
1259
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001260 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001261}
1262
1263CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001264 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001265}
1266
1267CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001268 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001269}
1270
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001271unsigned CXXMethodDecl::size_overridden_methods() const {
1272 return getASTContext().overridden_methods_size(this);
1273}
1274
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001275QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001276 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1277 // If the member function is declared const, the type of this is const X*,
1278 // if the member function is declared volatile, the type of this is
1279 // volatile X*, and if the member function is declared const volatile,
1280 // the type of this is const volatile X*.
1281
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001282 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001283
John McCall3cb0ebd2010-03-10 03:28:59 +00001284 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001285 ClassTy = C.getQualifiedType(ClassTy,
1286 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001287 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001288}
1289
Eli Friedmand7d7f672009-12-06 20:50:05 +00001290bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001291 // If this function is a template instantiation, look at the template from
1292 // which it was instantiated.
1293 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1294 if (!CheckFn)
1295 CheckFn = this;
1296
Eli Friedmand7d7f672009-12-06 20:50:05 +00001297 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001298 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001299}
1300
Sean Huntcbb67482011-01-08 20:30:50 +00001301CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1302 TypeSourceInfo *TInfo, bool IsVirtual,
1303 SourceLocation L, Expr *Init,
1304 SourceLocation R,
1305 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001306 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001307 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1308 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001309{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001310}
1311
Sean Huntcbb67482011-01-08 20:30:50 +00001312CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1313 FieldDecl *Member,
1314 SourceLocation MemberLoc,
1315 SourceLocation L, Expr *Init,
1316 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001317 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001318 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001319 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1320{
1321}
1322
Sean Huntcbb67482011-01-08 20:30:50 +00001323CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1324 IndirectFieldDecl *Member,
1325 SourceLocation MemberLoc,
1326 SourceLocation L, Expr *Init,
1327 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001328 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001329 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001330 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001331{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001332}
1333
Sean Huntcbb67482011-01-08 20:30:50 +00001334CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001335 TypeSourceInfo *TInfo,
1336 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001337 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001338 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1339 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001340 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1341{
1342}
1343
1344CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001345 FieldDecl *Member,
1346 SourceLocation MemberLoc,
1347 SourceLocation L, Expr *Init,
1348 SourceLocation R,
1349 VarDecl **Indices,
1350 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001351 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001352 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001353 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001354{
1355 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1356 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1357}
1358
Sean Huntcbb67482011-01-08 20:30:50 +00001359CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1360 FieldDecl *Member,
1361 SourceLocation MemberLoc,
1362 SourceLocation L, Expr *Init,
1363 SourceLocation R,
1364 VarDecl **Indices,
1365 unsigned NumIndices) {
1366 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001367 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001368 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001369 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1370 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001371}
1372
Sean Huntcbb67482011-01-08 20:30:50 +00001373TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001374 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001375 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001376 else
1377 return TypeLoc();
1378}
1379
Sean Huntcbb67482011-01-08 20:30:50 +00001380const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001381 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001382 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001383 else
1384 return 0;
1385}
1386
Sean Huntcbb67482011-01-08 20:30:50 +00001387SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001388 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001389 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001390
1391 if (isInClassMemberInitializer())
1392 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001393
Douglas Gregor76852c22011-11-01 01:16:03 +00001394 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1395 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1396
1397 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001398}
1399
Sean Huntcbb67482011-01-08 20:30:50 +00001400SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001401 if (isInClassMemberInitializer()) {
1402 FieldDecl *D = getAnyMember();
1403 if (Expr *I = D->getInClassInitializer())
1404 return I->getSourceRange();
1405 return SourceRange();
1406 }
1407
Douglas Gregor802ab452009-12-02 22:36:29 +00001408 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001409}
1410
David Blaikie99ba9e32011-12-20 02:48:34 +00001411void CXXConstructorDecl::anchor() { }
1412
Douglas Gregorb48fe382008-10-31 09:07:45 +00001413CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001414CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001415 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001416 QualType(), 0, false, false, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001417}
1418
1419CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001420CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001421 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001422 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001423 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001424 bool isExplicit, bool isInline,
1425 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001426 assert(NameInfo.getName().getNameKind()
1427 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001428 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001429 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001430 isExplicit, isInline, isImplicitlyDeclared,
1431 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001432}
1433
Douglas Gregor76852c22011-11-01 01:16:03 +00001434CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1435 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1436 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1437 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1438 return Construct->getConstructor();
1439
1440 return 0;
1441}
1442
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001443bool CXXConstructorDecl::isDefaultConstructor() const {
1444 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001445 // A default constructor for a class X is a constructor of class
1446 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001447 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001448 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001449}
1450
Mike Stump1eb44332009-09-09 15:08:12 +00001451bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001452CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001453 return isCopyOrMoveConstructor(TypeQuals) &&
1454 getParamDecl(0)->getType()->isLValueReferenceType();
1455}
1456
1457bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1458 return isCopyOrMoveConstructor(TypeQuals) &&
1459 getParamDecl(0)->getType()->isRValueReferenceType();
1460}
1461
1462/// \brief Determine whether this is a copy or move constructor.
1463bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001464 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001465 // A non-template constructor for class X is a copy constructor
1466 // if its first parameter is of type X&, const X&, volatile X& or
1467 // const volatile X&, and either there are no other parameters
1468 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001469 // C++0x [class.copy]p3:
1470 // A non-template constructor for class X is a move constructor if its
1471 // first parameter is of type X&&, const X&&, volatile X&&, or
1472 // const volatile X&&, and either there are no other parameters or else
1473 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001474 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001475 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001476 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001477 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001478 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001479
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001480 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001481
1482 // Do we have a reference type?
1483 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001484 if (!ParamRefType)
1485 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001486
Douglas Gregorfd476482009-11-13 23:59:09 +00001487 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001488 ASTContext &Context = getASTContext();
1489
Douglas Gregorfd476482009-11-13 23:59:09 +00001490 CanQualType PointeeType
1491 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001492 CanQualType ClassTy
1493 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001494 if (PointeeType.getUnqualifiedType() != ClassTy)
1495 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001496
John McCall0953e762009-09-24 19:53:00 +00001497 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001498
1499 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001500 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001501 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001502}
1503
Anders Carlssonfaccd722009-08-28 16:57:08 +00001504bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001505 // C++ [class.conv.ctor]p1:
1506 // A constructor declared without the function-specifier explicit
1507 // that can be called with a single parameter specifies a
1508 // conversion from the type of its first parameter to the type of
1509 // its class. Such a constructor is called a converting
1510 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001511 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001512 return false;
1513
Mike Stump1eb44332009-09-09 15:08:12 +00001514 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001515 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001516 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001517 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001518}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001519
Douglas Gregor6493cc52010-11-08 17:16:59 +00001520bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001521 if ((getNumParams() < 1) ||
1522 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1523 (getPrimaryTemplate() == 0) ||
1524 (getDescribedFunctionTemplate() != 0))
1525 return false;
1526
1527 const ParmVarDecl *Param = getParamDecl(0);
1528
1529 ASTContext &Context = getASTContext();
1530 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1531
Douglas Gregor66724ea2009-11-14 01:20:54 +00001532 // Is it the same as our our class type?
1533 CanQualType ClassTy
1534 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1535 if (ParamType.getUnqualifiedType() != ClassTy)
1536 return false;
1537
1538 return true;
1539}
1540
Sebastian Redlf677ea32011-02-05 19:23:19 +00001541const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1542 // Hack: we store the inherited constructor in the overridden method table
1543 method_iterator It = begin_overridden_methods();
1544 if (It == end_overridden_methods())
1545 return 0;
1546
1547 return cast<CXXConstructorDecl>(*It);
1548}
1549
1550void
1551CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1552 // Hack: we store the inherited constructor in the overridden method table
1553 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1554 addOverriddenMethod(BaseCtor);
1555}
1556
David Blaikie99ba9e32011-12-20 02:48:34 +00001557void CXXDestructorDecl::anchor() { }
1558
Douglas Gregor42a552f2008-11-05 20:51:48 +00001559CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001560CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001561 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001562 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001563}
1564
1565CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001566CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001567 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001568 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001569 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001570 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001571 assert(NameInfo.getName().getNameKind()
1572 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001573 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001574 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001575 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001576}
1577
David Blaikie99ba9e32011-12-20 02:48:34 +00001578void CXXConversionDecl::anchor() { }
1579
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001580CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001581CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001582 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001583 QualType(), 0, false, false, false,
Douglas Gregorf5251602011-03-08 17:10:18 +00001584 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001585}
1586
1587CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001588CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001589 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001590 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001591 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001592 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001593 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001594 assert(NameInfo.getName().getNameKind()
1595 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001596 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001597 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001598 isInline, isExplicit, isConstexpr,
1599 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001600}
1601
David Blaikie99ba9e32011-12-20 02:48:34 +00001602void LinkageSpecDecl::anchor() { }
1603
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001604LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001605 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001606 SourceLocation ExternLoc,
1607 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001608 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001609 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001610 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001611}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001612
David Blaikie99ba9e32011-12-20 02:48:34 +00001613void UsingDirectiveDecl::anchor() { }
1614
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001615UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1616 SourceLocation L,
1617 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001618 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001619 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001620 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001621 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001622 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1623 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001624 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1625 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001626}
1627
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001628NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1629 if (NamespaceAliasDecl *NA =
1630 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1631 return NA->getNamespace();
1632 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1633}
1634
David Blaikie99ba9e32011-12-20 02:48:34 +00001635void NamespaceAliasDecl::anchor() { }
1636
Mike Stump1eb44332009-09-09 15:08:12 +00001637NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001638 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001639 SourceLocation AliasLoc,
1640 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001641 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001642 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001643 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001644 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1645 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001646 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1647 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001648}
1649
David Blaikie99ba9e32011-12-20 02:48:34 +00001650void UsingShadowDecl::anchor() { }
1651
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001652UsingDecl *UsingShadowDecl::getUsingDecl() const {
1653 const UsingShadowDecl *Shadow = this;
1654 while (const UsingShadowDecl *NextShadow =
1655 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1656 Shadow = NextShadow;
1657 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1658}
1659
David Blaikie99ba9e32011-12-20 02:48:34 +00001660void UsingDecl::anchor() { }
1661
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001662void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1663 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1664 "declaration already in set");
1665 assert(S->getUsingDecl() == this);
1666
1667 if (FirstUsingShadow)
1668 S->UsingOrNextShadow = FirstUsingShadow;
1669 FirstUsingShadow = S;
1670}
1671
1672void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1673 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1674 "declaration not in set");
1675 assert(S->getUsingDecl() == this);
1676
1677 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1678
1679 if (FirstUsingShadow == S) {
1680 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1681 S->UsingOrNextShadow = this;
1682 return;
1683 }
1684
1685 UsingShadowDecl *Prev = FirstUsingShadow;
1686 while (Prev->UsingOrNextShadow != S)
1687 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1688 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1689 S->UsingOrNextShadow = this;
1690}
1691
Douglas Gregordc355712011-02-25 00:36:19 +00001692UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1693 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001694 const DeclarationNameInfo &NameInfo,
1695 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001696 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001697}
1698
David Blaikie99ba9e32011-12-20 02:48:34 +00001699void UnresolvedUsingValueDecl::anchor() { }
1700
John McCall7ba107a2009-11-18 02:36:19 +00001701UnresolvedUsingValueDecl *
1702UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1703 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001704 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001705 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001706 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001707 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001708}
1709
David Blaikie99ba9e32011-12-20 02:48:34 +00001710void UnresolvedUsingTypenameDecl::anchor() { }
1711
John McCall7ba107a2009-11-18 02:36:19 +00001712UnresolvedUsingTypenameDecl *
1713UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1714 SourceLocation UsingLoc,
1715 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001716 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001717 SourceLocation TargetNameLoc,
1718 DeclarationName TargetName) {
1719 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001720 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001721 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001722}
1723
David Blaikie99ba9e32011-12-20 02:48:34 +00001724void StaticAssertDecl::anchor() { }
1725
Anders Carlssonfb311762009-03-14 00:25:26 +00001726StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001727 SourceLocation StaticAssertLoc,
1728 Expr *AssertExpr,
1729 StringLiteral *Message,
1730 SourceLocation RParenLoc) {
1731 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1732 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001733}
1734
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001735static const char *getAccessName(AccessSpecifier AS) {
1736 switch (AS) {
1737 default:
1738 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00001739 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001740 case AS_public:
1741 return "public";
1742 case AS_private:
1743 return "private";
1744 case AS_protected:
1745 return "protected";
1746 }
1747}
1748
1749const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1750 AccessSpecifier AS) {
1751 return DB << getAccessName(AS);
1752}