blob: 576d0d51483723e1b0c28b5989db025200ba8479 [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 Gregor802ab452009-12-02 22:36:29 +000020#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000021#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000022#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000023#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// Decl Allocation/Deallocation Method Implementations
28//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000029
John McCall86ff3082010-02-04 22:26:26 +000030CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
31 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000032 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
33 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000034 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000035 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000036 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Douglas Gregor2bb11012011-05-13 01:05:07 +000037 HasMutableFields(false), HasTrivialDefaultConstructor(true),
Richard Smith6b8bc072011-08-10 18:11:37 +000038 HasConstexprNonCopyMoveConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000039 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
40 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
41 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000042 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000043 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
44 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sebastian Redl85ea7aa2011-08-30 19:58:05 +000045 DeclaredDestructor(false), FailedImplicitMoveConstructor(false),
46 FailedImplicitMoveAssignment(false), NumBases(0), NumVBases(0), Bases(),
47 VBases(), Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000048}
49
50CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000051 SourceLocation StartLoc, SourceLocation IdLoc,
52 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
53 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000054 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000055 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000056
Jay Foad4ba2a172011-01-12 09:06:06 +000057CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000058 DeclContext *DC, SourceLocation StartLoc,
59 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000060 CXXRecordDecl* PrevDecl,
61 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000062 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
63 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000064
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000065 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000066 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000067 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000068 return R;
69}
70
Jay Foad4ba2a172011-01-12 09:06:06 +000071CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000072 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
73 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000074}
75
Mike Stump1eb44332009-09-09 15:08:12 +000076void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000077CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000078 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000079 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +000080
Douglas Gregor7c789c12010-10-29 22:39:52 +000081 if (!data().Bases.isOffset() && data().NumBases > 0)
82 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000083
Richard Smithdd677232011-10-18 20:08:55 +000084 if (NumBases) {
85 // C++ [dcl.init.aggr]p1:
86 // An aggregate is [...] a class with [...] no base classes [...].
87 data().Aggregate = false;
88
89 // C++ [class]p4:
90 // A POD-struct is an aggregate class...
91 data().PlainOldData = false;
92 }
93
Anders Carlsson6f6de732010-03-29 05:13:12 +000094 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000095 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000096
97 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +000098 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000099
John McCall86ff3082010-02-04 22:26:26 +0000100 data().Bases = new(C) CXXBaseSpecifier [NumBases];
101 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000102 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000103 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000104 // Keep track of inherited vbases for this base class.
105 const CXXBaseSpecifier *Base = Bases[i];
106 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000107 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000108 if (BaseType->isDependentType())
109 continue;
110 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000111 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000112
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000113 // A class with a non-empty base class is not empty.
114 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000115 if (!BaseClassDecl->isEmpty()) {
116 if (!data().Empty) {
117 // C++0x [class]p7:
118 // A standard-layout class is a class that:
119 // [...]
120 // -- either has no non-static data members in the most derived
121 // class and at most one base class with non-static data members,
122 // or has no base classes with non-static data members, and
123 // If this is the second non-empty base, then neither of these two
124 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000125 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000126 }
127
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000128 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000129 data().HasNoNonEmptyBases = false;
130 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000131
Douglas Gregor85606eb2010-09-28 20:50:54 +0000132 // C++ [class.virtual]p1:
133 // A class that declares or inherits a virtual function is called a
134 // polymorphic class.
135 if (BaseClassDecl->isPolymorphic())
136 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000137
Chandler Carrutha8225442011-04-30 09:17:45 +0000138 // C++0x [class]p7:
139 // A standard-layout class is a class that: [...]
140 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000141 if (!BaseClassDecl->isStandardLayout())
142 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000143
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000144 // Record if this base is the first non-literal field or base.
145 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
146 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000147
Anders Carlsson6f6de732010-03-29 05:13:12 +0000148 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000149 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000150 BaseClassDecl->vbases_begin(),
151 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000152 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000153 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000154 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000155 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000156
157 if (Base->isVirtual()) {
158 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000159 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000160 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000161
162 // C++0x [meta.unary.prop] is_empty:
163 // T is a class type, but not a union type, with ... no virtual base
164 // classes
165 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000166
167 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000168 // A default constructor is trivial [...] if:
169 // -- its class has [...] no virtual bases
170 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000171
172 // C++0x [class.copy]p13:
173 // A copy/move constructor for class X is trivial if it is neither
174 // user-provided nor deleted and if
175 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000176 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000177 data().HasTrivialMoveConstructor = false;
178
179 // C++0x [class.copy]p27:
180 // A copy/move assignment operator for class X is trivial if it is
181 // neither user-provided nor deleted and if
182 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000183 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000184 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000185
186 // C++0x [class]p7:
187 // A standard-layout class is a class that: [...]
188 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000189 data().IsStandardLayout = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000190 } else {
191 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000192 // A default constructor is trivial [...] if:
193 // -- all the direct base classes of its class have trivial default
194 // constructors.
195 if (!BaseClassDecl->hasTrivialDefaultConstructor())
196 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000197
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000198 // C++0x [class.copy]p13:
199 // A copy/move constructor for class X is trivial if [...]
200 // [...]
201 // -- the constructor selected to copy/move each direct base class
202 // subobject is trivial, and
203 // FIXME: C++0x: We need to only consider the selected constructor
204 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000205 if (!BaseClassDecl->hasTrivialCopyConstructor())
206 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000207 if (!BaseClassDecl->hasTrivialMoveConstructor())
208 data().HasTrivialMoveConstructor = false;
209
210 // C++0x [class.copy]p27:
211 // A copy/move assignment operator for class X is trivial if [...]
212 // [...]
213 // -- the assignment operator selected to copy/move each direct base
214 // class subobject is trivial, and
215 // FIXME: C++0x: We need to only consider the selected operator instead
216 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000217 if (!BaseClassDecl->hasTrivialCopyAssignment())
218 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000219 if (!BaseClassDecl->hasTrivialMoveAssignment())
220 data().HasTrivialMoveAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000221 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000222
223 // C++ [class.ctor]p3:
224 // A destructor is trivial if all the direct base classes of its class
225 // have trivial destructors.
226 if (!BaseClassDecl->hasTrivialDestructor())
227 data().HasTrivialDestructor = false;
Douglas Gregor2bb11012011-05-13 01:05:07 +0000228
John McCallf85e1932011-06-15 23:02:42 +0000229 // A class has an Objective-C object member if... or any of its bases
230 // has an Objective-C object member.
231 if (BaseClassDecl->hasObjectMember())
232 setHasObjectMember(true);
233
Douglas Gregor2bb11012011-05-13 01:05:07 +0000234 // Keep track of the presence of mutable fields.
235 if (BaseClassDecl->hasMutableFields())
236 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000237 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000238
239 if (VBases.empty())
240 return;
241
242 // Create base specifier for any direct or indirect virtual bases.
243 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
244 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000245 for (int I = 0, E = VBases.size(); I != E; ++I)
246 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000247}
248
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000249/// Callback function for CXXRecordDecl::forallBases that acknowledges
250/// that it saw a base class.
251static bool SawBase(const CXXRecordDecl *, void *) {
252 return true;
253}
254
255bool CXXRecordDecl::hasAnyDependentBases() const {
256 if (!isDependentContext())
257 return false;
258
259 return !forallBases(SawBase, 0);
260}
261
Sean Huntffe37fd2011-05-25 20:50:04 +0000262bool CXXRecordDecl::hasConstCopyConstructor() const {
263 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000264}
265
Chandler Carruthb7e95892011-04-23 10:47:28 +0000266bool CXXRecordDecl::isTriviallyCopyable() const {
267 // C++0x [class]p5:
268 // A trivially copyable class is a class that:
269 // -- has no non-trivial copy constructors,
270 if (!hasTrivialCopyConstructor()) return false;
271 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000272 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000273 // -- has no non-trivial copy assignment operators,
274 if (!hasTrivialCopyAssignment()) return false;
275 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000276 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000277 // -- has a trivial destructor.
278 if (!hasTrivialDestructor()) return false;
279
280 return true;
281}
282
Douglas Gregor0d405db2010-07-01 20:59:04 +0000283/// \brief Perform a simplistic form of overload resolution that only considers
284/// cv-qualifiers on a single parameter, and return the best overload candidate
285/// (if there is one).
286static CXXMethodDecl *
287GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000288 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000289 if (Cands.empty())
290 return 0;
291 if (Cands.size() == 1)
292 return Cands[0].first;
293
294 unsigned Best = 0, N = Cands.size();
295 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000296 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000297 Best = I;
298
299 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000300 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000301 return 0;
302
303 return Cands[Best].first;
304}
305
Sean Huntffe37fd2011-05-25 20:50:04 +0000306CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
307 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000308 QualType ClassType
309 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000310 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000311 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000312 Context.getCanonicalType(ClassType));
313 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000314 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000315 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000316 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000317 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000318 // C++ [class.copy]p2:
319 // A non-template constructor for class X is a copy constructor if [...]
320 if (isa<FunctionTemplateDecl>(*Con))
321 continue;
322
Douglas Gregor0d405db2010-07-01 20:59:04 +0000323 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
324 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000325 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
326 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000327 Found.push_back(std::make_pair(
328 const_cast<CXXConstructorDecl *>(Constructor),
329 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000330 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000331 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000332
333 return cast_or_null<CXXConstructorDecl>(
334 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000335}
336
Sean Huntffe37fd2011-05-25 20:50:04 +0000337CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
338 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
339 if (I->isMoveConstructor())
340 return *I;
341
342 return 0;
343}
344
Douglas Gregorb87786f2010-07-01 17:48:08 +0000345CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
346 ASTContext &Context = getASTContext();
347 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
348 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
349
Chris Lattner5f9e2722011-07-23 10:55:15 +0000350 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000351 DeclContext::lookup_const_iterator Op, OpEnd;
352 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
353 // C++ [class.copy]p9:
354 // A user-declared copy assignment operator is a non-static non-template
355 // member function of class X with exactly one parameter of type X, X&,
356 // const X&, volatile X& or const volatile X&.
357 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
358 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
359 continue;
360
361 const FunctionProtoType *FnType
362 = Method->getType()->getAs<FunctionProtoType>();
363 assert(FnType && "Overloaded operator has no prototype.");
364 // Don't assert on this; an invalid decl might have been left in the AST.
365 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
366 continue;
367
368 QualType ArgType = FnType->getArgType(0);
369 Qualifiers Quals;
370 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
371 ArgType = Ref->getPointeeType();
372 // If we have a const argument and we have a reference to a non-const,
373 // this function does not match.
374 if (ArgIsConst && !ArgType.isConstQualified())
375 continue;
376
377 Quals = ArgType.getQualifiers();
378 } else {
379 // By-value copy-assignment operators are treated like const X&
380 // copy-assignment operators.
381 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
382 }
383
384 if (!Context.hasSameUnqualifiedType(ArgType, Class))
385 continue;
386
387 // Save this copy-assignment operator. It might be "the one".
388 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
389 }
390
391 // Use a simplistic form of overload resolution to find the candidate.
392 return GetBestOverloadCandidateSimple(Found);
393}
394
Sean Huntffe37fd2011-05-25 20:50:04 +0000395CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
396 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
397 if (I->isMoveAssignmentOperator())
398 return *I;
399
400 return 0;
401}
402
Douglas Gregor21386642010-09-28 21:55:22 +0000403void CXXRecordDecl::markedVirtualFunctionPure() {
404 // C++ [class.abstract]p2:
405 // A class is abstract if it has at least one pure virtual function.
406 data().Abstract = true;
407}
408
409void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000410 // Ignore friends and invalid declarations.
411 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000412 return;
413
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000414 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
415 if (FunTmpl)
416 D = FunTmpl->getTemplatedDecl();
417
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000418 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
419 if (Method->isVirtual()) {
420 // C++ [dcl.init.aggr]p1:
421 // An aggregate is an array or a class with [...] no virtual functions.
422 data().Aggregate = false;
423
424 // C++ [class]p4:
425 // A POD-struct is an aggregate class...
426 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000427
428 // Virtual functions make the class non-empty.
429 // FIXME: Standard ref?
430 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000431
432 // C++ [class.virtual]p1:
433 // A class that declares or inherits a virtual function is called a
434 // polymorphic class.
435 data().Polymorphic = true;
436
Sean Hunt023df372011-05-09 18:22:59 +0000437 // C++0x [class.ctor]p5
438 // A default constructor is trivial [...] if:
439 // -- its class has no virtual functions [...]
440 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000441
442 // C++0x [class.copy]p13:
443 // A copy/move constructor for class X is trivial if [...]
444 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000445 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000446 data().HasTrivialMoveConstructor = false;
447
448 // C++0x [class.copy]p27:
449 // A copy/move assignment operator for class X is trivial if [...]
450 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000451 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000452 data().HasTrivialMoveAssignment = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000453 // FIXME: Destructor?
Chandler Carrutha8225442011-04-30 09:17:45 +0000454
455 // C++0x [class]p7:
456 // A standard-layout class is a class that: [...]
457 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000458 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000459 }
460 }
461
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000462 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000463 // Notify that an implicit member was added after the definition
464 // was completed.
465 if (!isBeingDefined())
466 if (ASTMutationListener *L = getASTMutationListener())
467 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000468
Sean Huntffe37fd2011-05-25 20:50:04 +0000469 // If this is a special member function, note that it was added and then
470 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000471 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000472 if (Constructor->isDefaultConstructor())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000473 data().DeclaredDefaultConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000474 else if (Constructor->isCopyConstructor())
475 data().DeclaredCopyConstructor = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000476 else if (Constructor->isMoveConstructor())
477 data().DeclaredMoveConstructor = true;
478 else
479 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000480 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000481 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000482 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000483 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000484 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
485 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000486 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000487 else if (Method->isMoveAssignmentOperator())
488 data().DeclaredMoveAssignment = true;
489 else
490 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000491 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000492 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000493
Sean Huntffe37fd2011-05-25 20:50:04 +0000494NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000495 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000496 }
497
498 // Handle (user-declared) constructors.
499 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
500 // Note that we have a user-declared constructor.
501 data().UserDeclaredConstructor = true;
502
Richard Smith017ab772011-09-05 02:13:09 +0000503 // Technically, "user-provided" is only defined for special member
504 // functions, but the intent of the standard is clearly that it should apply
505 // to all functions.
506 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000507
Sean Hunt023df372011-05-09 18:22:59 +0000508 // C++0x [class.ctor]p5:
509 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000510 if (Constructor->isDefaultConstructor()) {
511 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000512 if (UserProvided) {
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000513 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000514 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000515 }
516 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000517
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000518 // Note when we have a user-declared copy or move constructor, which will
519 // suppress the implicit declaration of those constructors.
520 if (!FunTmpl) {
521 if (Constructor->isCopyConstructor()) {
522 data().UserDeclaredCopyConstructor = true;
523 data().DeclaredCopyConstructor = true;
524
525 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000526 // A copy/move constructor for class X is trivial if it is not
527 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000528 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000529 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000530 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000531 data().UserDeclaredMoveConstructor = true;
532 data().DeclaredMoveConstructor = true;
533
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000534 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000535 // A copy/move constructor for class X is trivial if it is not
536 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000537 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000538 data().HasTrivialMoveConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000539 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000540 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000541 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
542 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000543 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000544 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000545 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000546
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000547 // C++ [dcl.init.aggr]p1:
548 // An aggregate is an array or a class with no user-declared
549 // constructors [...].
550 // C++0x [dcl.init.aggr]p1:
551 // An aggregate is an array or a class with no user-provided
552 // constructors [...].
553 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
554 data().Aggregate = false;
555
556 // C++ [class]p4:
557 // A POD-struct is an aggregate class [...]
558 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
559 // type is technically an aggregate in C++0x since it wouldn't be in 03.
560 data().PlainOldData = false;
561
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000562 return;
563 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000564
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000565 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000566 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000567 data().DeclaredDestructor = true;
568 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000569
570 // C++ [class]p4:
571 // A POD-struct is an aggregate class that has [...] no user-defined
572 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000573 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000574 data().PlainOldData = false;
575
Sean Huntcf34e752011-05-16 22:41:40 +0000576 // C++0x [class.dtor]p5:
577 // A destructor is trivial if it is not user-provided and [...]
578 if (DD->isUserProvided())
579 data().HasTrivialDestructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000580
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000581 return;
582 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000583
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000584 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000585 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000586 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000587 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000588 // A POD-struct is an aggregate class that [...] has no user-defined
589 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000590 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000591 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000592
Sean Huntffe37fd2011-05-25 20:50:04 +0000593 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000594
Sean Huntffe37fd2011-05-25 20:50:04 +0000595 // Suppress the implicit declaration of a copy constructor.
596 data().UserDeclaredCopyAssignment = true;
597 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000598
Sean Huntffe37fd2011-05-25 20:50:04 +0000599 // C++0x [class.copy]p27:
600 // A copy/move assignment operator for class X is trivial if it is
601 // neither user-provided nor deleted [...]
602 if (Method->isUserProvided())
603 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000604
Sean Huntffe37fd2011-05-25 20:50:04 +0000605 return;
606 }
607
608 if (Method->isMoveAssignmentOperator()) {
609 // This is an extension in C++03 mode, but we'll keep consistency by
610 // taking a move assignment operator to induce non-POD-ness
611 data().PlainOldData = false;
612
613 // This is a move assignment operator.
614 data().UserDeclaredMoveAssignment = true;
615 data().DeclaredMoveAssignment = true;
616
617 // C++0x [class.copy]p27:
618 // A copy/move assignment operator for class X is trivial if it is
619 // neither user-provided nor deleted [...]
620 if (Method->isUserProvided())
621 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000622 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000623
Douglas Gregore80622f2010-09-29 04:25:11 +0000624 // Keep the list of conversion functions up-to-date.
625 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
626 // We don't record specializations.
627 if (Conversion->getPrimaryTemplate())
628 return;
629
630 // FIXME: We intentionally don't use the decl's access here because it
631 // hasn't been set yet. That's really just a misdesign in Sema.
632
633 if (FunTmpl) {
634 if (FunTmpl->getPreviousDeclaration())
635 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
636 FunTmpl);
637 else
638 data().Conversions.addDecl(FunTmpl);
639 } else {
640 if (Conversion->getPreviousDeclaration())
641 data().Conversions.replace(Conversion->getPreviousDeclaration(),
642 Conversion);
643 else
644 data().Conversions.addDecl(Conversion);
645 }
646 }
647
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000648 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000649 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000650
651 // Handle non-static data members.
652 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000653 // C++ [class.bit]p2:
654 // A declaration for a bit-field that omits the identifier declares an
655 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
656 // initialized.
657 if (Field->isUnnamedBitfield())
658 return;
659
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000660 // C++ [dcl.init.aggr]p1:
661 // An aggregate is an array or a class (clause 9) with [...] no
662 // private or protected non-static data members (clause 11).
663 //
664 // A POD must be an aggregate.
665 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
666 data().Aggregate = false;
667 data().PlainOldData = false;
668 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000669
670 // C++0x [class]p7:
671 // A standard-layout class is a class that:
672 // [...]
673 // -- has the same access control for all non-static data members,
674 switch (D->getAccess()) {
675 case AS_private: data().HasPrivateFields = true; break;
676 case AS_protected: data().HasProtectedFields = true; break;
677 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000678 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000679 };
680 if ((data().HasPrivateFields + data().HasProtectedFields +
681 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000682 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000683
Douglas Gregor2bb11012011-05-13 01:05:07 +0000684 // Keep track of the presence of mutable fields.
685 if (Field->isMutable())
686 data().HasMutableFields = true;
687
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000688 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000689 // A POD struct is a class that is both a trivial class and a
690 // standard-layout class, and has no non-static data members of type
691 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000692 //
693 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
694 // that does not explicitly have no lifetime makes the class a non-POD.
695 // However, we delay setting PlainOldData to false in this case so that
696 // Sema has a chance to diagnostic causes where the same class will be
697 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
698 // In this case, the class will become a non-POD class when we complete
699 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000700 ASTContext &Context = getASTContext();
701 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000702 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
703 if (!Context.getLangOptions().ObjCAutoRefCount ||
704 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
705 setHasObjectMember(true);
706 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000707 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000708
Chandler Carrutha8225442011-04-30 09:17:45 +0000709 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000710 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000711
Chandler Carrutha8225442011-04-30 09:17:45 +0000712 // C++0x [class]p7:
713 // A standard-layout class is a class that:
714 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000715 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000716 }
717
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000718 // Record if this field is the first non-literal field or base.
Richard Smith5fa6a042011-10-12 05:08:15 +0000719 // As a slight variation on the standard, we regard mutable members as being
720 // non-literal, since mutating a constexpr variable would break C++11
721 // constant expression semantics.
722 if ((!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType()) ||
723 Field->isMutable())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000724 data().HasNonLiteralTypeFieldsOrBases = true;
725
Richard Smith7a614d82011-06-11 17:19:42 +0000726 if (Field->hasInClassInitializer()) {
727 // C++0x [class]p5:
728 // A default constructor is trivial if [...] no non-static data member
729 // of its class has a brace-or-equal-initializer.
730 data().HasTrivialDefaultConstructor = false;
731
732 // C++0x [dcl.init.aggr]p1:
733 // An aggregate is a [...] class with [...] no
734 // brace-or-equal-initializers for non-static data members.
735 data().Aggregate = false;
736
737 // C++0x [class]p10:
738 // A POD struct is [...] a trivial class.
739 data().PlainOldData = false;
740 }
741
Douglas Gregor85606eb2010-09-28 20:50:54 +0000742 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
743 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
744 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000745 // C++0x [class.ctor]p5:
746 // A defulat constructor is trivial [...] if:
747 // -- for all the non-static data members of its class that are of
748 // class type (or array thereof), each such class has a trivial
749 // default constructor.
750 if (!FieldRec->hasTrivialDefaultConstructor())
751 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000752
753 // C++0x [class.copy]p13:
754 // A copy/move constructor for class X is trivial if [...]
755 // [...]
756 // -- for each non-static data member of X that is of class type (or
757 // an array thereof), the constructor selected to copy/move that
758 // member is trivial;
759 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000760 if (!FieldRec->hasTrivialCopyConstructor())
761 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000762 if (!FieldRec->hasTrivialMoveConstructor())
763 data().HasTrivialMoveConstructor = false;
764
765 // C++0x [class.copy]p27:
766 // A copy/move assignment operator for class X is trivial if [...]
767 // [...]
768 // -- for each non-static data member of X that is of class type (or
769 // an array thereof), the assignment operator selected to
770 // copy/move that member is trivial;
771 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000772 if (!FieldRec->hasTrivialCopyAssignment())
773 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000774 if (!FieldRec->hasTrivialMoveAssignment())
775 data().HasTrivialMoveAssignment = false;
776
Douglas Gregor85606eb2010-09-28 20:50:54 +0000777 if (!FieldRec->hasTrivialDestructor())
778 data().HasTrivialDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000779 if (FieldRec->hasObjectMember())
780 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000781
782 // C++0x [class]p7:
783 // A standard-layout class is a class that:
784 // -- has no non-static data members of type non-standard-layout
785 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000786 if (!FieldRec->isStandardLayout())
787 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000788
789 // C++0x [class]p7:
790 // A standard-layout class is a class that:
791 // [...]
792 // -- has no base classes of the same type as the first non-static
793 // data member.
794 // We don't want to expend bits in the state of the record decl
795 // tracking whether this is the first non-static data member so we
796 // cheat a bit and use some of the existing state: the empty bit.
797 // Virtual bases and virtual methods make a class non-empty, but they
798 // also make it non-standard-layout so we needn't check here.
799 // A non-empty base class may leave the class standard-layout, but not
800 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000801 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000802 // data member must come through here with Empty still true, and Empty
803 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000804 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000805 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
806 BE = bases_end();
807 BI != BE; ++BI) {
808 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000809 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000810 break;
811 }
812 }
813 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000814
815 // Keep track of the presence of mutable fields.
816 if (FieldRec->hasMutableFields())
817 data().HasMutableFields = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000818 }
819 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000820
821 // C++0x [class]p7:
822 // A standard-layout class is a class that:
823 // [...]
824 // -- either has no non-static data members in the most derived
825 // class and at most one base class with non-static data members,
826 // or has no base classes with non-static data members, and
827 // At this point we know that we have a non-static data member, so the last
828 // clause holds.
829 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000830 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000831
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000832 // If this is not a zero-length bit-field, then the class is not empty.
833 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000834 if (!Field->isBitField() ||
835 (!Field->getBitWidth()->isTypeDependent() &&
836 !Field->getBitWidth()->isValueDependent() &&
837 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000838 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000839 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000840 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000841
842 // Handle using declarations of conversion functions.
843 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
844 if (Shadow->getDeclName().getNameKind()
845 == DeclarationName::CXXConversionFunctionName)
846 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000847}
848
John McCallb05b5f32010-03-15 09:07:48 +0000849static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
850 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000851 if (isa<UsingShadowDecl>(Conv))
852 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000853 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
854 T = ConvTemp->getTemplatedDecl()->getResultType();
855 else
856 T = cast<CXXConversionDecl>(Conv)->getConversionType();
857 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000858}
859
John McCallb05b5f32010-03-15 09:07:48 +0000860/// Collect the visible conversions of a base class.
861///
862/// \param Base a base class of the class we're considering
863/// \param InVirtual whether this base class is a virtual base (or a base
864/// of a virtual base)
865/// \param Access the access along the inheritance path to this base
866/// \param ParentHiddenTypes the conversions provided by the inheritors
867/// of this base
868/// \param Output the set to which to add conversions from non-virtual bases
869/// \param VOutput the set to which to add conversions from virtual bases
870/// \param HiddenVBaseCs the set of conversions which were hidden in a
871/// virtual base along some inheritance path
872static void CollectVisibleConversions(ASTContext &Context,
873 CXXRecordDecl *Record,
874 bool InVirtual,
875 AccessSpecifier Access,
876 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
877 UnresolvedSetImpl &Output,
878 UnresolvedSetImpl &VOutput,
879 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
880 // The set of types which have conversions in this class or its
881 // subclasses. As an optimization, we don't copy the derived set
882 // unless it might change.
883 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
884 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
885
886 // Collect the direct conversions and figure out which conversions
887 // will be hidden in the subclasses.
888 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
889 if (!Cs.empty()) {
890 HiddenTypesBuffer = ParentHiddenTypes;
891 HiddenTypes = &HiddenTypesBuffer;
892
893 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
894 bool Hidden =
895 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
896
897 // If this conversion is hidden and we're in a virtual base,
898 // remember that it's hidden along some inheritance path.
899 if (Hidden && InVirtual)
900 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
901
902 // If this conversion isn't hidden, add it to the appropriate output.
903 else if (!Hidden) {
904 AccessSpecifier IAccess
905 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
906
907 if (InVirtual)
908 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000909 else
John McCallb05b5f32010-03-15 09:07:48 +0000910 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000911 }
912 }
913 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000914
John McCallb05b5f32010-03-15 09:07:48 +0000915 // Collect information recursively from any base classes.
916 for (CXXRecordDecl::base_class_iterator
917 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
918 const RecordType *RT = I->getType()->getAs<RecordType>();
919 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000920
John McCallb05b5f32010-03-15 09:07:48 +0000921 AccessSpecifier BaseAccess
922 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
923 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000924
John McCallb05b5f32010-03-15 09:07:48 +0000925 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
926 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
927 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000928 }
John McCallb05b5f32010-03-15 09:07:48 +0000929}
Sebastian Redl9994a342009-10-25 17:03:50 +0000930
John McCallb05b5f32010-03-15 09:07:48 +0000931/// Collect the visible conversions of a class.
932///
933/// This would be extremely straightforward if it weren't for virtual
934/// bases. It might be worth special-casing that, really.
935static void CollectVisibleConversions(ASTContext &Context,
936 CXXRecordDecl *Record,
937 UnresolvedSetImpl &Output) {
938 // The collection of all conversions in virtual bases that we've
939 // found. These will be added to the output as long as they don't
940 // appear in the hidden-conversions set.
941 UnresolvedSet<8> VBaseCs;
942
943 // The set of conversions in virtual bases that we've determined to
944 // be hidden.
945 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
946
947 // The set of types hidden by classes derived from this one.
948 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
949
950 // Go ahead and collect the direct conversions and add them to the
951 // hidden-types set.
952 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
953 Output.append(Cs.begin(), Cs.end());
954 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
955 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
956
957 // Recursively collect conversions from base classes.
958 for (CXXRecordDecl::base_class_iterator
959 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
960 const RecordType *RT = I->getType()->getAs<RecordType>();
961 if (!RT) continue;
962
963 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
964 I->isVirtual(), I->getAccessSpecifier(),
965 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
966 }
967
968 // Add any unhidden conversions provided by virtual bases.
969 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
970 I != E; ++I) {
971 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
972 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000973 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000974}
975
976/// getVisibleConversionFunctions - get all conversion functions visible
977/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000978const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000979 // If root class, all conversions are visible.
980 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000981 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000982 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000983 if (data().ComputedVisibleConversions)
984 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000985 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000986 data().ComputedVisibleConversions = true;
987 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000988}
989
John McCall32daa422010-03-31 01:36:47 +0000990void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
991 // This operation is O(N) but extremely rare. Sema only uses it to
992 // remove UsingShadowDecls in a class that were followed by a direct
993 // declaration, e.g.:
994 // class A : B {
995 // using B::operator int;
996 // operator int();
997 // };
998 // This is uncommon by itself and even more uncommon in conjunction
999 // with sufficiently large numbers of directly-declared conversions
1000 // that asymptotic behavior matters.
1001
1002 UnresolvedSetImpl &Convs = *getConversionFunctions();
1003 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1004 if (Convs[I].getDecl() == ConvDecl) {
1005 Convs.erase(I);
1006 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1007 && "conversion was found multiple times in unresolved set");
1008 return;
1009 }
1010 }
1011
1012 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001013}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001014
Douglas Gregorf6b11852009-10-08 15:14:33 +00001015CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001016 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001017 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1018
1019 return 0;
1020}
1021
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001022MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1023 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1024}
1025
Douglas Gregorf6b11852009-10-08 15:14:33 +00001026void
1027CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1028 TemplateSpecializationKind TSK) {
1029 assert(TemplateOrInstantiation.isNull() &&
1030 "Previous template or instantiation?");
1031 assert(!isa<ClassTemplateSpecializationDecl>(this));
1032 TemplateOrInstantiation
1033 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1034}
1035
Anders Carlssonb13e3572009-12-07 06:33:48 +00001036TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1037 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001038 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1039 return Spec->getSpecializationKind();
1040
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001041 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001042 return MSInfo->getTemplateSpecializationKind();
1043
1044 return TSK_Undeclared;
1045}
1046
1047void
1048CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1049 if (ClassTemplateSpecializationDecl *Spec
1050 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1051 Spec->setSpecializationKind(TSK);
1052 return;
1053 }
1054
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001055 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001056 MSInfo->setTemplateSpecializationKind(TSK);
1057 return;
1058 }
1059
David Blaikieb219cfc2011-09-23 05:06:16 +00001060 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001061}
1062
Douglas Gregor1d110e02010-07-01 14:13:13 +00001063CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1064 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001065 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001066
1067 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001068 = Context.DeclarationNames.getCXXDestructorName(
1069 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001070
John McCallc0bf4622010-02-23 00:48:20 +00001071 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001072 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001073 if (I == E)
1074 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001076 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001077 return Dtor;
1078}
1079
Douglas Gregorda2142f2011-02-19 18:51:44 +00001080void CXXRecordDecl::completeDefinition() {
1081 completeDefinition(0);
1082}
1083
1084void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1085 RecordDecl::completeDefinition();
1086
John McCallf85e1932011-06-15 23:02:42 +00001087 if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) {
1088 // Objective-C Automatic Reference Counting:
1089 // If a class has a non-static data member of Objective-C pointer
1090 // type (or array thereof), it is a non-POD type and its
1091 // default constructor (if any), copy constructor, copy assignment
1092 // operator, and destructor are non-trivial.
1093 struct DefinitionData &Data = data();
1094 Data.PlainOldData = false;
1095 Data.HasTrivialDefaultConstructor = false;
1096 Data.HasTrivialCopyConstructor = false;
1097 Data.HasTrivialCopyAssignment = false;
1098 Data.HasTrivialDestructor = false;
1099 }
1100
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001101 // If the class may be abstract (but hasn't been marked as such), check for
1102 // any pure final overriders.
1103 if (mayBeAbstract()) {
1104 CXXFinalOverriderMap MyFinalOverriders;
1105 if (!FinalOverriders) {
1106 getFinalOverriders(MyFinalOverriders);
1107 FinalOverriders = &MyFinalOverriders;
1108 }
1109
1110 bool Done = false;
1111 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1112 MEnd = FinalOverriders->end();
1113 M != MEnd && !Done; ++M) {
1114 for (OverridingMethods::iterator SO = M->second.begin(),
1115 SOEnd = M->second.end();
1116 SO != SOEnd && !Done; ++SO) {
1117 assert(SO->second.size() > 0 &&
1118 "All virtual functions have overridding virtual functions");
1119
1120 // C++ [class.abstract]p4:
1121 // A class is abstract if it contains or inherits at least one
1122 // pure virtual function for which the final overrider is pure
1123 // virtual.
1124 if (SO->second.front().Method->isPure()) {
1125 data().Abstract = true;
1126 Done = true;
1127 break;
1128 }
1129 }
1130 }
1131 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001132
1133 // Set access bits correctly on the directly-declared conversions.
1134 for (UnresolvedSetIterator I = data().Conversions.begin(),
1135 E = data().Conversions.end();
1136 I != E; ++I)
1137 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001138}
1139
1140bool CXXRecordDecl::mayBeAbstract() const {
1141 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1142 isDependentContext())
1143 return false;
1144
1145 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1146 BEnd = bases_end();
1147 B != BEnd; ++B) {
1148 CXXRecordDecl *BaseDecl
1149 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1150 if (BaseDecl->isAbstract())
1151 return true;
1152 }
1153
1154 return false;
1155}
1156
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001157CXXMethodDecl *
1158CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001159 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001160 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001161 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001162 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001163 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001164 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001165 isStatic, SCAsWritten, isInline, isConstexpr,
1166 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001167}
1168
Douglas Gregor90916562009-09-29 18:16:17 +00001169bool CXXMethodDecl::isUsualDeallocationFunction() const {
1170 if (getOverloadedOperator() != OO_Delete &&
1171 getOverloadedOperator() != OO_Array_Delete)
1172 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001173
1174 // C++ [basic.stc.dynamic.deallocation]p2:
1175 // A template instance is never a usual deallocation function,
1176 // regardless of its signature.
1177 if (getPrimaryTemplate())
1178 return false;
1179
Douglas Gregor90916562009-09-29 18:16:17 +00001180 // C++ [basic.stc.dynamic.deallocation]p2:
1181 // If a class T has a member deallocation function named operator delete
1182 // with exactly one parameter, then that function is a usual (non-placement)
1183 // deallocation function. [...]
1184 if (getNumParams() == 1)
1185 return true;
1186
1187 // C++ [basic.stc.dynamic.deallocation]p2:
1188 // [...] If class T does not declare such an operator delete but does
1189 // declare a member deallocation function named operator delete with
1190 // exactly two parameters, the second of which has type std::size_t (18.1),
1191 // then this function is a usual deallocation function.
1192 ASTContext &Context = getASTContext();
1193 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001194 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1195 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001196 return false;
1197
1198 // This function is a usual deallocation function if there are no
1199 // single-parameter deallocation functions of the same kind.
1200 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1201 R.first != R.second; ++R.first) {
1202 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1203 if (FD->getNumParams() == 1)
1204 return false;
1205 }
1206
1207 return true;
1208}
1209
Douglas Gregor06a9f362010-05-01 20:49:11 +00001210bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001211 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001212 // A user-declared copy assignment operator X::operator= is a non-static
1213 // non-template member function of class X with exactly one parameter of
1214 // type X, X&, const X&, volatile X& or const volatile X&.
1215 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1216 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001217 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001218 return false;
1219
1220 QualType ParamType = getParamDecl(0)->getType();
1221 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1222 ParamType = Ref->getPointeeType();
1223
1224 ASTContext &Context = getASTContext();
1225 QualType ClassType
1226 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1227 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1228}
1229
Sean Huntffe37fd2011-05-25 20:50:04 +00001230bool CXXMethodDecl::isMoveAssignmentOperator() const {
1231 // C++0x [class.copy]p19:
1232 // A user-declared move assignment operator X::operator= is a non-static
1233 // non-template member function of class X with exactly one parameter of type
1234 // X&&, const X&&, volatile X&&, or const volatile X&&.
1235 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1236 getPrimaryTemplate() || getDescribedFunctionTemplate())
1237 return false;
1238
1239 QualType ParamType = getParamDecl(0)->getType();
1240 if (!isa<RValueReferenceType>(ParamType))
1241 return false;
1242 ParamType = ParamType->getPointeeType();
1243
1244 ASTContext &Context = getASTContext();
1245 QualType ClassType
1246 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1247 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1248}
1249
Anders Carlsson05eb2442009-05-16 23:58:37 +00001250void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001251 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001252 assert(!MD->getParent()->isDependentContext() &&
1253 "Can't add an overridden method to a class template!");
1254
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001255 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001256}
1257
1258CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001259 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001260}
1261
1262CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001263 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001264}
1265
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001266unsigned CXXMethodDecl::size_overridden_methods() const {
1267 return getASTContext().overridden_methods_size(this);
1268}
1269
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001270QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001271 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1272 // If the member function is declared const, the type of this is const X*,
1273 // if the member function is declared volatile, the type of this is
1274 // volatile X*, and if the member function is declared const volatile,
1275 // the type of this is const volatile X*.
1276
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001277 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001278
John McCall3cb0ebd2010-03-10 03:28:59 +00001279 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001280 ClassTy = C.getQualifiedType(ClassTy,
1281 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001282 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001283}
1284
Eli Friedmand7d7f672009-12-06 20:50:05 +00001285bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001286 // If this function is a template instantiation, look at the template from
1287 // which it was instantiated.
1288 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1289 if (!CheckFn)
1290 CheckFn = this;
1291
Eli Friedmand7d7f672009-12-06 20:50:05 +00001292 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001293 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001294}
1295
Sean Huntcbb67482011-01-08 20:30:50 +00001296CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1297 TypeSourceInfo *TInfo, bool IsVirtual,
1298 SourceLocation L, Expr *Init,
1299 SourceLocation R,
1300 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001301 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001302 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1303 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001304{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001305}
1306
Sean Huntcbb67482011-01-08 20:30:50 +00001307CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1308 FieldDecl *Member,
1309 SourceLocation MemberLoc,
1310 SourceLocation L, Expr *Init,
1311 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001312 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001313 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1314 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1315{
1316}
1317
Sean Huntcbb67482011-01-08 20:30:50 +00001318CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1319 IndirectFieldDecl *Member,
1320 SourceLocation MemberLoc,
1321 SourceLocation L, Expr *Init,
1322 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001323 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001324 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001325 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001326{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001327}
1328
Sean Huntcbb67482011-01-08 20:30:50 +00001329CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Hunt41717662011-02-26 19:13:13 +00001330 SourceLocation D, SourceLocation L,
1331 CXXConstructorDecl *Target, Expr *Init,
1332 SourceLocation R)
1333 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1334 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1335 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1336{
1337}
1338
1339CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001340 FieldDecl *Member,
1341 SourceLocation MemberLoc,
1342 SourceLocation L, Expr *Init,
1343 SourceLocation R,
1344 VarDecl **Indices,
1345 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001346 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001347 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001348 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001349{
1350 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1351 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1352}
1353
Sean Huntcbb67482011-01-08 20:30:50 +00001354CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1355 FieldDecl *Member,
1356 SourceLocation MemberLoc,
1357 SourceLocation L, Expr *Init,
1358 SourceLocation R,
1359 VarDecl **Indices,
1360 unsigned NumIndices) {
1361 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001362 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001363 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001364 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1365 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001366}
1367
Sean Huntcbb67482011-01-08 20:30:50 +00001368TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001369 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001370 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001371 else
1372 return TypeLoc();
1373}
1374
Sean Huntcbb67482011-01-08 20:30:50 +00001375const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001376 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001377 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001378 else
1379 return 0;
1380}
1381
Sean Huntcbb67482011-01-08 20:30:50 +00001382SourceLocation CXXCtorInitializer::getSourceLocation() const {
Sean Hunt41717662011-02-26 19:13:13 +00001383 if (isAnyMemberInitializer() || isDelegatingInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001384 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001385
1386 if (isInClassMemberInitializer())
1387 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001388
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001389 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001390}
1391
Sean Huntcbb67482011-01-08 20:30:50 +00001392SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001393 if (isInClassMemberInitializer()) {
1394 FieldDecl *D = getAnyMember();
1395 if (Expr *I = D->getInClassInitializer())
1396 return I->getSourceRange();
1397 return SourceRange();
1398 }
1399
Douglas Gregor802ab452009-12-02 22:36:29 +00001400 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001401}
1402
Douglas Gregorb48fe382008-10-31 09:07:45 +00001403CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001404CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001405 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001406 QualType(), 0, false, false, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001407}
1408
1409CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001410CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001411 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001412 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001413 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001414 bool isExplicit, bool isInline,
1415 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001416 assert(NameInfo.getName().getNameKind()
1417 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001418 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001419 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001420 isExplicit, isInline, isImplicitlyDeclared,
1421 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001422}
1423
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001424bool CXXConstructorDecl::isDefaultConstructor() const {
1425 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001426 // A default constructor for a class X is a constructor of class
1427 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001428 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001429 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001430}
1431
Mike Stump1eb44332009-09-09 15:08:12 +00001432bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001433CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001434 return isCopyOrMoveConstructor(TypeQuals) &&
1435 getParamDecl(0)->getType()->isLValueReferenceType();
1436}
1437
1438bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1439 return isCopyOrMoveConstructor(TypeQuals) &&
1440 getParamDecl(0)->getType()->isRValueReferenceType();
1441}
1442
1443/// \brief Determine whether this is a copy or move constructor.
1444bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001445 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001446 // A non-template constructor for class X is a copy constructor
1447 // if its first parameter is of type X&, const X&, volatile X& or
1448 // const volatile X&, and either there are no other parameters
1449 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001450 // C++0x [class.copy]p3:
1451 // A non-template constructor for class X is a move constructor if its
1452 // first parameter is of type X&&, const X&&, volatile X&&, or
1453 // const volatile X&&, and either there are no other parameters or else
1454 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001455 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001456 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001457 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001458 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001459 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001460
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001461 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001462
1463 // Do we have a reference type?
1464 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001465 if (!ParamRefType)
1466 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001467
Douglas Gregorfd476482009-11-13 23:59:09 +00001468 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001469 ASTContext &Context = getASTContext();
1470
Douglas Gregorfd476482009-11-13 23:59:09 +00001471 CanQualType PointeeType
1472 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001473 CanQualType ClassTy
1474 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001475 if (PointeeType.getUnqualifiedType() != ClassTy)
1476 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001477
John McCall0953e762009-09-24 19:53:00 +00001478 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001479
1480 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001481 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001482 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001483}
1484
Anders Carlssonfaccd722009-08-28 16:57:08 +00001485bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001486 // C++ [class.conv.ctor]p1:
1487 // A constructor declared without the function-specifier explicit
1488 // that can be called with a single parameter specifies a
1489 // conversion from the type of its first parameter to the type of
1490 // its class. Such a constructor is called a converting
1491 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001492 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001493 return false;
1494
Mike Stump1eb44332009-09-09 15:08:12 +00001495 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001496 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001497 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001498 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001499}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001500
Douglas Gregor6493cc52010-11-08 17:16:59 +00001501bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001502 if ((getNumParams() < 1) ||
1503 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1504 (getPrimaryTemplate() == 0) ||
1505 (getDescribedFunctionTemplate() != 0))
1506 return false;
1507
1508 const ParmVarDecl *Param = getParamDecl(0);
1509
1510 ASTContext &Context = getASTContext();
1511 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1512
Douglas Gregor66724ea2009-11-14 01:20:54 +00001513 // Is it the same as our our class type?
1514 CanQualType ClassTy
1515 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1516 if (ParamType.getUnqualifiedType() != ClassTy)
1517 return false;
1518
1519 return true;
1520}
1521
Sebastian Redlf677ea32011-02-05 19:23:19 +00001522const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1523 // Hack: we store the inherited constructor in the overridden method table
1524 method_iterator It = begin_overridden_methods();
1525 if (It == end_overridden_methods())
1526 return 0;
1527
1528 return cast<CXXConstructorDecl>(*It);
1529}
1530
1531void
1532CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1533 // Hack: we store the inherited constructor in the overridden method table
1534 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1535 addOverriddenMethod(BaseCtor);
1536}
1537
Douglas Gregor42a552f2008-11-05 20:51:48 +00001538CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001539CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001540 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001541 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001542}
1543
1544CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001545CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001546 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001547 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001548 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001549 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001550 assert(NameInfo.getName().getNameKind()
1551 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001552 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001553 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001554 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001555}
1556
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001557CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001558CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001559 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001560 QualType(), 0, false, false, false,
Douglas Gregorf5251602011-03-08 17:10:18 +00001561 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001562}
1563
1564CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001565CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001566 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001567 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001568 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001569 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001570 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001571 assert(NameInfo.getName().getNameKind()
1572 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001573 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001574 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001575 isInline, isExplicit, isConstexpr,
1576 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001577}
1578
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001579LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001580 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001581 SourceLocation ExternLoc,
1582 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001583 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001584 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001585 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001586}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001587
1588UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1589 SourceLocation L,
1590 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001591 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001592 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001593 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001594 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001595 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1596 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001597 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1598 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001599}
1600
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001601NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1602 if (NamespaceAliasDecl *NA =
1603 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1604 return NA->getNamespace();
1605 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1606}
1607
Mike Stump1eb44332009-09-09 15:08:12 +00001608NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001609 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001610 SourceLocation AliasLoc,
1611 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001612 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001613 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001614 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001615 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1616 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001617 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1618 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001619}
1620
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001621UsingDecl *UsingShadowDecl::getUsingDecl() const {
1622 const UsingShadowDecl *Shadow = this;
1623 while (const UsingShadowDecl *NextShadow =
1624 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1625 Shadow = NextShadow;
1626 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1627}
1628
1629void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1630 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1631 "declaration already in set");
1632 assert(S->getUsingDecl() == this);
1633
1634 if (FirstUsingShadow)
1635 S->UsingOrNextShadow = FirstUsingShadow;
1636 FirstUsingShadow = S;
1637}
1638
1639void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1640 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1641 "declaration not in set");
1642 assert(S->getUsingDecl() == this);
1643
1644 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1645
1646 if (FirstUsingShadow == S) {
1647 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1648 S->UsingOrNextShadow = this;
1649 return;
1650 }
1651
1652 UsingShadowDecl *Prev = FirstUsingShadow;
1653 while (Prev->UsingOrNextShadow != S)
1654 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1655 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1656 S->UsingOrNextShadow = this;
1657}
1658
Douglas Gregordc355712011-02-25 00:36:19 +00001659UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1660 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001661 const DeclarationNameInfo &NameInfo,
1662 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001663 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001664}
1665
John McCall7ba107a2009-11-18 02:36:19 +00001666UnresolvedUsingValueDecl *
1667UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1668 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001669 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001670 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001671 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001672 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001673}
1674
1675UnresolvedUsingTypenameDecl *
1676UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1677 SourceLocation UsingLoc,
1678 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001679 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001680 SourceLocation TargetNameLoc,
1681 DeclarationName TargetName) {
1682 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001683 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001684 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001685}
1686
Anders Carlssonfb311762009-03-14 00:25:26 +00001687StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001688 SourceLocation StaticAssertLoc,
1689 Expr *AssertExpr,
1690 StringLiteral *Message,
1691 SourceLocation RParenLoc) {
1692 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1693 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001694}
1695
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001696static const char *getAccessName(AccessSpecifier AS) {
1697 switch (AS) {
1698 default:
1699 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00001700 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001701 case AS_public:
1702 return "public";
1703 case AS_private:
1704 return "private";
1705 case AS_protected:
1706 return "protected";
1707 }
1708}
1709
1710const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1711 AccessSpecifier AS) {
1712 return DB << getAccessName(AS);
1713}