blob: b8c20e4b06db67ab0a54241a3b7ca74c42f27278 [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
John McCall86ff3082010-02-04 22:26:26 +000031CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
32 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000033 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
34 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000035 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000036 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000037 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Douglas Gregor2bb11012011-05-13 01:05:07 +000038 HasMutableFields(false), HasTrivialDefaultConstructor(true),
Richard Smith6b8bc072011-08-10 18:11:37 +000039 HasConstexprNonCopyMoveConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000040 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
41 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
42 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000043 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000044 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
45 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sebastian Redl85ea7aa2011-08-30 19:58:05 +000046 DeclaredDestructor(false), FailedImplicitMoveConstructor(false),
47 FailedImplicitMoveAssignment(false), NumBases(0), NumVBases(0), Bases(),
48 VBases(), Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000049}
50
51CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000052 SourceLocation StartLoc, SourceLocation IdLoc,
53 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
54 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000055 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000056 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000057
Jay Foad4ba2a172011-01-12 09:06:06 +000058CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000059 DeclContext *DC, SourceLocation StartLoc,
60 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000061 CXXRecordDecl* PrevDecl,
62 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000063 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
64 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000065
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000066 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000067 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000068 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000069 return R;
70}
71
Jay Foad4ba2a172011-01-12 09:06:06 +000072CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000073 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
74 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000075}
76
Mike Stump1eb44332009-09-09 15:08:12 +000077void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000078CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000079 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000080 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +000081
Douglas Gregor7c789c12010-10-29 22:39:52 +000082 if (!data().Bases.isOffset() && data().NumBases > 0)
83 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000084
Richard Smithdd677232011-10-18 20:08:55 +000085 if (NumBases) {
86 // C++ [dcl.init.aggr]p1:
87 // An aggregate is [...] a class with [...] no base classes [...].
88 data().Aggregate = false;
89
90 // C++ [class]p4:
91 // A POD-struct is an aggregate class...
92 data().PlainOldData = false;
93 }
94
Anders Carlsson6f6de732010-03-29 05:13:12 +000095 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000096 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000097
98 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +000099 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000100
John McCall86ff3082010-02-04 22:26:26 +0000101 data().Bases = new(C) CXXBaseSpecifier [NumBases];
102 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000103 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000104 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000105 // Keep track of inherited vbases for this base class.
106 const CXXBaseSpecifier *Base = Bases[i];
107 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000108 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000109 if (BaseType->isDependentType())
110 continue;
111 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000112 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000113
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000114 // A class with a non-empty base class is not empty.
115 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000116 if (!BaseClassDecl->isEmpty()) {
117 if (!data().Empty) {
118 // C++0x [class]p7:
119 // A standard-layout class is a class that:
120 // [...]
121 // -- either has no non-static data members in the most derived
122 // class and at most one base class with non-static data members,
123 // or has no base classes with non-static data members, and
124 // If this is the second non-empty base, then neither of these two
125 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000126 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000127 }
128
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000129 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000130 data().HasNoNonEmptyBases = false;
131 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000132
Douglas Gregor85606eb2010-09-28 20:50:54 +0000133 // C++ [class.virtual]p1:
134 // A class that declares or inherits a virtual function is called a
135 // polymorphic class.
136 if (BaseClassDecl->isPolymorphic())
137 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000138
Chandler Carrutha8225442011-04-30 09:17:45 +0000139 // C++0x [class]p7:
140 // A standard-layout class is a class that: [...]
141 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000142 if (!BaseClassDecl->isStandardLayout())
143 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000144
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000145 // Record if this base is the first non-literal field or base.
146 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
147 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000148
Anders Carlsson6f6de732010-03-29 05:13:12 +0000149 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000150 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000151 BaseClassDecl->vbases_begin(),
152 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000153 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000154 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000155 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000156 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000157
158 if (Base->isVirtual()) {
159 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000160 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000161 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000162
163 // C++0x [meta.unary.prop] is_empty:
164 // T is a class type, but not a union type, with ... no virtual base
165 // classes
166 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000167
168 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000169 // A default constructor is trivial [...] if:
170 // -- its class has [...] no virtual bases
171 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000172
173 // C++0x [class.copy]p13:
174 // A copy/move constructor for class X is trivial if it is neither
175 // user-provided nor deleted and if
176 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000177 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000178 data().HasTrivialMoveConstructor = false;
179
180 // C++0x [class.copy]p27:
181 // A copy/move assignment operator for class X is trivial if it is
182 // neither user-provided nor deleted and if
183 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000184 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000185 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000186
187 // C++0x [class]p7:
188 // A standard-layout class is a class that: [...]
189 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000190 data().IsStandardLayout = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000191 } else {
192 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000193 // A default constructor is trivial [...] if:
194 // -- all the direct base classes of its class have trivial default
195 // constructors.
196 if (!BaseClassDecl->hasTrivialDefaultConstructor())
197 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000198
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000199 // C++0x [class.copy]p13:
200 // A copy/move constructor for class X is trivial if [...]
201 // [...]
202 // -- the constructor selected to copy/move each direct base class
203 // subobject is trivial, and
204 // FIXME: C++0x: We need to only consider the selected constructor
205 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000206 if (!BaseClassDecl->hasTrivialCopyConstructor())
207 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000208 if (!BaseClassDecl->hasTrivialMoveConstructor())
209 data().HasTrivialMoveConstructor = false;
210
211 // C++0x [class.copy]p27:
212 // A copy/move assignment operator for class X is trivial if [...]
213 // [...]
214 // -- the assignment operator selected to copy/move each direct base
215 // class subobject is trivial, and
216 // FIXME: C++0x: We need to only consider the selected operator instead
217 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000218 if (!BaseClassDecl->hasTrivialCopyAssignment())
219 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000220 if (!BaseClassDecl->hasTrivialMoveAssignment())
221 data().HasTrivialMoveAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000222 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000223
224 // C++ [class.ctor]p3:
225 // A destructor is trivial if all the direct base classes of its class
226 // have trivial destructors.
227 if (!BaseClassDecl->hasTrivialDestructor())
228 data().HasTrivialDestructor = false;
Douglas Gregor2bb11012011-05-13 01:05:07 +0000229
John McCallf85e1932011-06-15 23:02:42 +0000230 // A class has an Objective-C object member if... or any of its bases
231 // has an Objective-C object member.
232 if (BaseClassDecl->hasObjectMember())
233 setHasObjectMember(true);
234
Douglas Gregor2bb11012011-05-13 01:05:07 +0000235 // Keep track of the presence of mutable fields.
236 if (BaseClassDecl->hasMutableFields())
237 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000238 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000239
240 if (VBases.empty())
241 return;
242
243 // Create base specifier for any direct or indirect virtual bases.
244 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
245 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000246 for (int I = 0, E = VBases.size(); I != E; ++I)
247 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000248}
249
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000250/// Callback function for CXXRecordDecl::forallBases that acknowledges
251/// that it saw a base class.
252static bool SawBase(const CXXRecordDecl *, void *) {
253 return true;
254}
255
256bool CXXRecordDecl::hasAnyDependentBases() const {
257 if (!isDependentContext())
258 return false;
259
260 return !forallBases(SawBase, 0);
261}
262
Sean Huntffe37fd2011-05-25 20:50:04 +0000263bool CXXRecordDecl::hasConstCopyConstructor() const {
264 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000265}
266
Chandler Carruthb7e95892011-04-23 10:47:28 +0000267bool CXXRecordDecl::isTriviallyCopyable() const {
268 // C++0x [class]p5:
269 // A trivially copyable class is a class that:
270 // -- has no non-trivial copy constructors,
271 if (!hasTrivialCopyConstructor()) return false;
272 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000273 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000274 // -- has no non-trivial copy assignment operators,
275 if (!hasTrivialCopyAssignment()) return false;
276 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000277 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000278 // -- has a trivial destructor.
279 if (!hasTrivialDestructor()) return false;
280
281 return true;
282}
283
Douglas Gregor0d405db2010-07-01 20:59:04 +0000284/// \brief Perform a simplistic form of overload resolution that only considers
285/// cv-qualifiers on a single parameter, and return the best overload candidate
286/// (if there is one).
287static CXXMethodDecl *
288GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000289 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000290 if (Cands.empty())
291 return 0;
292 if (Cands.size() == 1)
293 return Cands[0].first;
294
295 unsigned Best = 0, N = Cands.size();
296 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000297 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000298 Best = I;
299
300 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000301 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000302 return 0;
303
304 return Cands[Best].first;
305}
306
Sean Huntffe37fd2011-05-25 20:50:04 +0000307CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
308 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000309 QualType ClassType
310 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000311 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000312 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000313 Context.getCanonicalType(ClassType));
314 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000315 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000316 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000317 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000318 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000319 // C++ [class.copy]p2:
320 // A non-template constructor for class X is a copy constructor if [...]
321 if (isa<FunctionTemplateDecl>(*Con))
322 continue;
323
Douglas Gregor0d405db2010-07-01 20:59:04 +0000324 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
325 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000326 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
327 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000328 Found.push_back(std::make_pair(
329 const_cast<CXXConstructorDecl *>(Constructor),
330 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000331 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000332 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000333
334 return cast_or_null<CXXConstructorDecl>(
335 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000336}
337
Sean Huntffe37fd2011-05-25 20:50:04 +0000338CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
339 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
340 if (I->isMoveConstructor())
341 return *I;
342
343 return 0;
344}
345
Douglas Gregorb87786f2010-07-01 17:48:08 +0000346CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
347 ASTContext &Context = getASTContext();
348 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
349 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
350
Chris Lattner5f9e2722011-07-23 10:55:15 +0000351 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000352 DeclContext::lookup_const_iterator Op, OpEnd;
353 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
354 // C++ [class.copy]p9:
355 // A user-declared copy assignment operator is a non-static non-template
356 // member function of class X with exactly one parameter of type X, X&,
357 // const X&, volatile X& or const volatile X&.
358 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
359 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
360 continue;
361
362 const FunctionProtoType *FnType
363 = Method->getType()->getAs<FunctionProtoType>();
364 assert(FnType && "Overloaded operator has no prototype.");
365 // Don't assert on this; an invalid decl might have been left in the AST.
366 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
367 continue;
368
369 QualType ArgType = FnType->getArgType(0);
370 Qualifiers Quals;
371 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
372 ArgType = Ref->getPointeeType();
373 // If we have a const argument and we have a reference to a non-const,
374 // this function does not match.
375 if (ArgIsConst && !ArgType.isConstQualified())
376 continue;
377
378 Quals = ArgType.getQualifiers();
379 } else {
380 // By-value copy-assignment operators are treated like const X&
381 // copy-assignment operators.
382 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
383 }
384
385 if (!Context.hasSameUnqualifiedType(ArgType, Class))
386 continue;
387
388 // Save this copy-assignment operator. It might be "the one".
389 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
390 }
391
392 // Use a simplistic form of overload resolution to find the candidate.
393 return GetBestOverloadCandidateSimple(Found);
394}
395
Sean Huntffe37fd2011-05-25 20:50:04 +0000396CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
397 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
398 if (I->isMoveAssignmentOperator())
399 return *I;
400
401 return 0;
402}
403
Douglas Gregor21386642010-09-28 21:55:22 +0000404void CXXRecordDecl::markedVirtualFunctionPure() {
405 // C++ [class.abstract]p2:
406 // A class is abstract if it has at least one pure virtual function.
407 data().Abstract = true;
408}
409
410void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000411 // Ignore friends and invalid declarations.
412 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000413 return;
414
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000415 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
416 if (FunTmpl)
417 D = FunTmpl->getTemplatedDecl();
418
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000419 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
420 if (Method->isVirtual()) {
421 // C++ [dcl.init.aggr]p1:
422 // An aggregate is an array or a class with [...] no virtual functions.
423 data().Aggregate = false;
424
425 // C++ [class]p4:
426 // A POD-struct is an aggregate class...
427 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000428
429 // Virtual functions make the class non-empty.
430 // FIXME: Standard ref?
431 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000432
433 // C++ [class.virtual]p1:
434 // A class that declares or inherits a virtual function is called a
435 // polymorphic class.
436 data().Polymorphic = true;
437
Sean Hunt023df372011-05-09 18:22:59 +0000438 // C++0x [class.ctor]p5
439 // A default constructor is trivial [...] if:
440 // -- its class has no virtual functions [...]
441 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000442
443 // C++0x [class.copy]p13:
444 // A copy/move constructor for class X is trivial if [...]
445 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000446 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000447 data().HasTrivialMoveConstructor = false;
448
449 // C++0x [class.copy]p27:
450 // A copy/move assignment operator for class X is trivial if [...]
451 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000452 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000453 data().HasTrivialMoveAssignment = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000454 // FIXME: Destructor?
Chandler Carrutha8225442011-04-30 09:17:45 +0000455
456 // C++0x [class]p7:
457 // A standard-layout class is a class that: [...]
458 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000459 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000460 }
461 }
462
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000463 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000464 // Notify that an implicit member was added after the definition
465 // was completed.
466 if (!isBeingDefined())
467 if (ASTMutationListener *L = getASTMutationListener())
468 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000469
Sean Huntffe37fd2011-05-25 20:50:04 +0000470 // If this is a special member function, note that it was added and then
471 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000472 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000473 if (Constructor->isDefaultConstructor())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000474 data().DeclaredDefaultConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000475 else if (Constructor->isCopyConstructor())
476 data().DeclaredCopyConstructor = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000477 else if (Constructor->isMoveConstructor())
478 data().DeclaredMoveConstructor = true;
479 else
480 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000481 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000482 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000483 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000484 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000485 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
486 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000487 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000488 else if (Method->isMoveAssignmentOperator())
489 data().DeclaredMoveAssignment = true;
490 else
491 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000492 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000493 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000494
Sean Huntffe37fd2011-05-25 20:50:04 +0000495NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000496 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000497 }
498
499 // Handle (user-declared) constructors.
500 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
501 // Note that we have a user-declared constructor.
502 data().UserDeclaredConstructor = true;
503
Richard Smith017ab772011-09-05 02:13:09 +0000504 // Technically, "user-provided" is only defined for special member
505 // functions, but the intent of the standard is clearly that it should apply
506 // to all functions.
507 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000508
Sean Hunt023df372011-05-09 18:22:59 +0000509 // C++0x [class.ctor]p5:
510 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000511 if (Constructor->isDefaultConstructor()) {
512 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000513 if (UserProvided) {
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000514 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000515 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000516 }
517 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000518
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000519 // Note when we have a user-declared copy or move constructor, which will
520 // suppress the implicit declaration of those constructors.
521 if (!FunTmpl) {
522 if (Constructor->isCopyConstructor()) {
523 data().UserDeclaredCopyConstructor = true;
524 data().DeclaredCopyConstructor = true;
525
526 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000527 // A copy/move constructor for class X is trivial if it is not
528 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000529 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000530 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000531 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000532 data().UserDeclaredMoveConstructor = true;
533 data().DeclaredMoveConstructor = true;
534
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000535 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000536 // A copy/move constructor for class X is trivial if it is not
537 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000538 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000539 data().HasTrivialMoveConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000540 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000541 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000542 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
543 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000544 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000545 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000546 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000547
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000548 // C++ [dcl.init.aggr]p1:
549 // An aggregate is an array or a class with no user-declared
550 // constructors [...].
551 // C++0x [dcl.init.aggr]p1:
552 // An aggregate is an array or a class with no user-provided
553 // constructors [...].
554 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
555 data().Aggregate = false;
556
557 // C++ [class]p4:
558 // A POD-struct is an aggregate class [...]
559 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
560 // type is technically an aggregate in C++0x since it wouldn't be in 03.
561 data().PlainOldData = false;
562
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000563 return;
564 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000565
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000566 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000567 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000568 data().DeclaredDestructor = true;
569 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000570
571 // C++ [class]p4:
572 // A POD-struct is an aggregate class that has [...] no user-defined
573 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000574 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000575 data().PlainOldData = false;
576
Sean Huntcf34e752011-05-16 22:41:40 +0000577 // C++0x [class.dtor]p5:
578 // A destructor is trivial if it is not user-provided and [...]
579 if (DD->isUserProvided())
580 data().HasTrivialDestructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000581
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000582 return;
583 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000584
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000585 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000586 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000587 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000588 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000589 // A POD-struct is an aggregate class that [...] has no user-defined
590 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000591 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000592 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000593
Sean Huntffe37fd2011-05-25 20:50:04 +0000594 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000595
Sean Huntffe37fd2011-05-25 20:50:04 +0000596 // Suppress the implicit declaration of a copy constructor.
597 data().UserDeclaredCopyAssignment = true;
598 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000599
Sean Huntffe37fd2011-05-25 20:50:04 +0000600 // C++0x [class.copy]p27:
601 // A copy/move assignment operator for class X is trivial if it is
602 // neither user-provided nor deleted [...]
603 if (Method->isUserProvided())
604 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000605
Sean Huntffe37fd2011-05-25 20:50:04 +0000606 return;
607 }
608
609 if (Method->isMoveAssignmentOperator()) {
610 // This is an extension in C++03 mode, but we'll keep consistency by
611 // taking a move assignment operator to induce non-POD-ness
612 data().PlainOldData = false;
613
614 // This is a move assignment operator.
615 data().UserDeclaredMoveAssignment = true;
616 data().DeclaredMoveAssignment = true;
617
618 // C++0x [class.copy]p27:
619 // A copy/move assignment operator for class X is trivial if it is
620 // neither user-provided nor deleted [...]
621 if (Method->isUserProvided())
622 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000623 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000624
Douglas Gregore80622f2010-09-29 04:25:11 +0000625 // Keep the list of conversion functions up-to-date.
626 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
627 // We don't record specializations.
628 if (Conversion->getPrimaryTemplate())
629 return;
630
631 // FIXME: We intentionally don't use the decl's access here because it
632 // hasn't been set yet. That's really just a misdesign in Sema.
633
634 if (FunTmpl) {
635 if (FunTmpl->getPreviousDeclaration())
636 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
637 FunTmpl);
638 else
639 data().Conversions.addDecl(FunTmpl);
640 } else {
641 if (Conversion->getPreviousDeclaration())
642 data().Conversions.replace(Conversion->getPreviousDeclaration(),
643 Conversion);
644 else
645 data().Conversions.addDecl(Conversion);
646 }
647 }
648
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000649 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000650 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000651
652 // Handle non-static data members.
653 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000654 // C++ [class.bit]p2:
655 // A declaration for a bit-field that omits the identifier declares an
656 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
657 // initialized.
658 if (Field->isUnnamedBitfield())
659 return;
660
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000661 // C++ [dcl.init.aggr]p1:
662 // An aggregate is an array or a class (clause 9) with [...] no
663 // private or protected non-static data members (clause 11).
664 //
665 // A POD must be an aggregate.
666 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
667 data().Aggregate = false;
668 data().PlainOldData = false;
669 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000670
671 // C++0x [class]p7:
672 // A standard-layout class is a class that:
673 // [...]
674 // -- has the same access control for all non-static data members,
675 switch (D->getAccess()) {
676 case AS_private: data().HasPrivateFields = true; break;
677 case AS_protected: data().HasProtectedFields = true; break;
678 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000679 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000680 };
681 if ((data().HasPrivateFields + data().HasProtectedFields +
682 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000683 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000684
Douglas Gregor2bb11012011-05-13 01:05:07 +0000685 // Keep track of the presence of mutable fields.
686 if (Field->isMutable())
687 data().HasMutableFields = true;
688
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000689 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000690 // A POD struct is a class that is both a trivial class and a
691 // standard-layout class, and has no non-static data members of type
692 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000693 //
694 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
695 // that does not explicitly have no lifetime makes the class a non-POD.
696 // However, we delay setting PlainOldData to false in this case so that
697 // Sema has a chance to diagnostic causes where the same class will be
698 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
699 // In this case, the class will become a non-POD class when we complete
700 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000701 ASTContext &Context = getASTContext();
702 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000703 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
704 if (!Context.getLangOptions().ObjCAutoRefCount ||
705 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
706 setHasObjectMember(true);
707 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000708 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000709
Chandler Carrutha8225442011-04-30 09:17:45 +0000710 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000711 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000712
Chandler Carrutha8225442011-04-30 09:17:45 +0000713 // C++0x [class]p7:
714 // A standard-layout class is a class that:
715 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000716 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000717 }
718
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000719 // Record if this field is the first non-literal field or base.
Richard Smith5fa6a042011-10-12 05:08:15 +0000720 // As a slight variation on the standard, we regard mutable members as being
721 // non-literal, since mutating a constexpr variable would break C++11
722 // constant expression semantics.
723 if ((!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType()) ||
724 Field->isMutable())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000725 data().HasNonLiteralTypeFieldsOrBases = true;
726
Richard Smith7a614d82011-06-11 17:19:42 +0000727 if (Field->hasInClassInitializer()) {
728 // C++0x [class]p5:
729 // A default constructor is trivial if [...] no non-static data member
730 // of its class has a brace-or-equal-initializer.
731 data().HasTrivialDefaultConstructor = false;
732
733 // C++0x [dcl.init.aggr]p1:
734 // An aggregate is a [...] class with [...] no
735 // brace-or-equal-initializers for non-static data members.
736 data().Aggregate = false;
737
738 // C++0x [class]p10:
739 // A POD struct is [...] a trivial class.
740 data().PlainOldData = false;
741 }
742
Douglas Gregor85606eb2010-09-28 20:50:54 +0000743 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
744 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
745 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000746 // C++0x [class.ctor]p5:
747 // A defulat constructor is trivial [...] if:
748 // -- for all the non-static data members of its class that are of
749 // class type (or array thereof), each such class has a trivial
750 // default constructor.
751 if (!FieldRec->hasTrivialDefaultConstructor())
752 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000753
754 // C++0x [class.copy]p13:
755 // A copy/move constructor for class X is trivial if [...]
756 // [...]
757 // -- for each non-static data member of X that is of class type (or
758 // an array thereof), the constructor selected to copy/move that
759 // member is trivial;
760 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000761 if (!FieldRec->hasTrivialCopyConstructor())
762 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000763 if (!FieldRec->hasTrivialMoveConstructor())
764 data().HasTrivialMoveConstructor = false;
765
766 // C++0x [class.copy]p27:
767 // A copy/move assignment operator for class X is trivial if [...]
768 // [...]
769 // -- for each non-static data member of X that is of class type (or
770 // an array thereof), the assignment operator selected to
771 // copy/move that member is trivial;
772 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000773 if (!FieldRec->hasTrivialCopyAssignment())
774 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000775 if (!FieldRec->hasTrivialMoveAssignment())
776 data().HasTrivialMoveAssignment = false;
777
Douglas Gregor85606eb2010-09-28 20:50:54 +0000778 if (!FieldRec->hasTrivialDestructor())
779 data().HasTrivialDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000780 if (FieldRec->hasObjectMember())
781 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000782
783 // C++0x [class]p7:
784 // A standard-layout class is a class that:
785 // -- has no non-static data members of type non-standard-layout
786 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000787 if (!FieldRec->isStandardLayout())
788 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000789
790 // C++0x [class]p7:
791 // A standard-layout class is a class that:
792 // [...]
793 // -- has no base classes of the same type as the first non-static
794 // data member.
795 // We don't want to expend bits in the state of the record decl
796 // tracking whether this is the first non-static data member so we
797 // cheat a bit and use some of the existing state: the empty bit.
798 // Virtual bases and virtual methods make a class non-empty, but they
799 // also make it non-standard-layout so we needn't check here.
800 // A non-empty base class may leave the class standard-layout, but not
801 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000802 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000803 // data member must come through here with Empty still true, and Empty
804 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000805 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000806 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
807 BE = bases_end();
808 BI != BE; ++BI) {
809 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000810 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000811 break;
812 }
813 }
814 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000815
816 // Keep track of the presence of mutable fields.
817 if (FieldRec->hasMutableFields())
818 data().HasMutableFields = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000819 }
820 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000821
822 // C++0x [class]p7:
823 // A standard-layout class is a class that:
824 // [...]
825 // -- either has no non-static data members in the most derived
826 // class and at most one base class with non-static data members,
827 // or has no base classes with non-static data members, and
828 // At this point we know that we have a non-static data member, so the last
829 // clause holds.
830 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000831 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000832
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000833 // If this is not a zero-length bit-field, then the class is not empty.
834 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000835 if (!Field->isBitField() ||
836 (!Field->getBitWidth()->isTypeDependent() &&
837 !Field->getBitWidth()->isValueDependent() &&
838 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000839 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000840 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000841 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000842
843 // Handle using declarations of conversion functions.
844 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
845 if (Shadow->getDeclName().getNameKind()
846 == DeclarationName::CXXConversionFunctionName)
847 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000848}
849
John McCallb05b5f32010-03-15 09:07:48 +0000850static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
851 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000852 if (isa<UsingShadowDecl>(Conv))
853 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000854 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
855 T = ConvTemp->getTemplatedDecl()->getResultType();
856 else
857 T = cast<CXXConversionDecl>(Conv)->getConversionType();
858 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000859}
860
John McCallb05b5f32010-03-15 09:07:48 +0000861/// Collect the visible conversions of a base class.
862///
863/// \param Base a base class of the class we're considering
864/// \param InVirtual whether this base class is a virtual base (or a base
865/// of a virtual base)
866/// \param Access the access along the inheritance path to this base
867/// \param ParentHiddenTypes the conversions provided by the inheritors
868/// of this base
869/// \param Output the set to which to add conversions from non-virtual bases
870/// \param VOutput the set to which to add conversions from virtual bases
871/// \param HiddenVBaseCs the set of conversions which were hidden in a
872/// virtual base along some inheritance path
873static void CollectVisibleConversions(ASTContext &Context,
874 CXXRecordDecl *Record,
875 bool InVirtual,
876 AccessSpecifier Access,
877 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
878 UnresolvedSetImpl &Output,
879 UnresolvedSetImpl &VOutput,
880 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
881 // The set of types which have conversions in this class or its
882 // subclasses. As an optimization, we don't copy the derived set
883 // unless it might change.
884 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
885 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
886
887 // Collect the direct conversions and figure out which conversions
888 // will be hidden in the subclasses.
889 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
890 if (!Cs.empty()) {
891 HiddenTypesBuffer = ParentHiddenTypes;
892 HiddenTypes = &HiddenTypesBuffer;
893
894 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
895 bool Hidden =
896 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
897
898 // If this conversion is hidden and we're in a virtual base,
899 // remember that it's hidden along some inheritance path.
900 if (Hidden && InVirtual)
901 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
902
903 // If this conversion isn't hidden, add it to the appropriate output.
904 else if (!Hidden) {
905 AccessSpecifier IAccess
906 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
907
908 if (InVirtual)
909 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000910 else
John McCallb05b5f32010-03-15 09:07:48 +0000911 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000912 }
913 }
914 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000915
John McCallb05b5f32010-03-15 09:07:48 +0000916 // Collect information recursively from any base classes.
917 for (CXXRecordDecl::base_class_iterator
918 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
919 const RecordType *RT = I->getType()->getAs<RecordType>();
920 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000921
John McCallb05b5f32010-03-15 09:07:48 +0000922 AccessSpecifier BaseAccess
923 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
924 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000925
John McCallb05b5f32010-03-15 09:07:48 +0000926 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
927 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
928 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000929 }
John McCallb05b5f32010-03-15 09:07:48 +0000930}
Sebastian Redl9994a342009-10-25 17:03:50 +0000931
John McCallb05b5f32010-03-15 09:07:48 +0000932/// Collect the visible conversions of a class.
933///
934/// This would be extremely straightforward if it weren't for virtual
935/// bases. It might be worth special-casing that, really.
936static void CollectVisibleConversions(ASTContext &Context,
937 CXXRecordDecl *Record,
938 UnresolvedSetImpl &Output) {
939 // The collection of all conversions in virtual bases that we've
940 // found. These will be added to the output as long as they don't
941 // appear in the hidden-conversions set.
942 UnresolvedSet<8> VBaseCs;
943
944 // The set of conversions in virtual bases that we've determined to
945 // be hidden.
946 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
947
948 // The set of types hidden by classes derived from this one.
949 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
950
951 // Go ahead and collect the direct conversions and add them to the
952 // hidden-types set.
953 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
954 Output.append(Cs.begin(), Cs.end());
955 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
956 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
957
958 // Recursively collect conversions from base classes.
959 for (CXXRecordDecl::base_class_iterator
960 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
961 const RecordType *RT = I->getType()->getAs<RecordType>();
962 if (!RT) continue;
963
964 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
965 I->isVirtual(), I->getAccessSpecifier(),
966 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
967 }
968
969 // Add any unhidden conversions provided by virtual bases.
970 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
971 I != E; ++I) {
972 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
973 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000974 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000975}
976
977/// getVisibleConversionFunctions - get all conversion functions visible
978/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000979const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000980 // If root class, all conversions are visible.
981 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000982 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000983 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000984 if (data().ComputedVisibleConversions)
985 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000986 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000987 data().ComputedVisibleConversions = true;
988 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000989}
990
John McCall32daa422010-03-31 01:36:47 +0000991void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
992 // This operation is O(N) but extremely rare. Sema only uses it to
993 // remove UsingShadowDecls in a class that were followed by a direct
994 // declaration, e.g.:
995 // class A : B {
996 // using B::operator int;
997 // operator int();
998 // };
999 // This is uncommon by itself and even more uncommon in conjunction
1000 // with sufficiently large numbers of directly-declared conversions
1001 // that asymptotic behavior matters.
1002
1003 UnresolvedSetImpl &Convs = *getConversionFunctions();
1004 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1005 if (Convs[I].getDecl() == ConvDecl) {
1006 Convs.erase(I);
1007 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1008 && "conversion was found multiple times in unresolved set");
1009 return;
1010 }
1011 }
1012
1013 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001014}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001015
Douglas Gregorf6b11852009-10-08 15:14:33 +00001016CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001017 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001018 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1019
1020 return 0;
1021}
1022
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001023MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1024 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1025}
1026
Douglas Gregorf6b11852009-10-08 15:14:33 +00001027void
1028CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1029 TemplateSpecializationKind TSK) {
1030 assert(TemplateOrInstantiation.isNull() &&
1031 "Previous template or instantiation?");
1032 assert(!isa<ClassTemplateSpecializationDecl>(this));
1033 TemplateOrInstantiation
1034 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1035}
1036
Anders Carlssonb13e3572009-12-07 06:33:48 +00001037TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1038 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001039 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1040 return Spec->getSpecializationKind();
1041
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001042 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001043 return MSInfo->getTemplateSpecializationKind();
1044
1045 return TSK_Undeclared;
1046}
1047
1048void
1049CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1050 if (ClassTemplateSpecializationDecl *Spec
1051 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1052 Spec->setSpecializationKind(TSK);
1053 return;
1054 }
1055
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001056 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001057 MSInfo->setTemplateSpecializationKind(TSK);
1058 return;
1059 }
1060
David Blaikieb219cfc2011-09-23 05:06:16 +00001061 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001062}
1063
Douglas Gregor1d110e02010-07-01 14:13:13 +00001064CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1065 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001066 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001067
1068 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001069 = Context.DeclarationNames.getCXXDestructorName(
1070 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001071
John McCallc0bf4622010-02-23 00:48:20 +00001072 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001073 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001074 if (I == E)
1075 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001077 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001078 return Dtor;
1079}
1080
Douglas Gregorda2142f2011-02-19 18:51:44 +00001081void CXXRecordDecl::completeDefinition() {
1082 completeDefinition(0);
1083}
1084
1085void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1086 RecordDecl::completeDefinition();
1087
John McCallf85e1932011-06-15 23:02:42 +00001088 if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) {
1089 // Objective-C Automatic Reference Counting:
1090 // If a class has a non-static data member of Objective-C pointer
1091 // type (or array thereof), it is a non-POD type and its
1092 // default constructor (if any), copy constructor, copy assignment
1093 // operator, and destructor are non-trivial.
1094 struct DefinitionData &Data = data();
1095 Data.PlainOldData = false;
1096 Data.HasTrivialDefaultConstructor = false;
1097 Data.HasTrivialCopyConstructor = false;
1098 Data.HasTrivialCopyAssignment = false;
1099 Data.HasTrivialDestructor = false;
1100 }
1101
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001102 // If the class may be abstract (but hasn't been marked as such), check for
1103 // any pure final overriders.
1104 if (mayBeAbstract()) {
1105 CXXFinalOverriderMap MyFinalOverriders;
1106 if (!FinalOverriders) {
1107 getFinalOverriders(MyFinalOverriders);
1108 FinalOverriders = &MyFinalOverriders;
1109 }
1110
1111 bool Done = false;
1112 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1113 MEnd = FinalOverriders->end();
1114 M != MEnd && !Done; ++M) {
1115 for (OverridingMethods::iterator SO = M->second.begin(),
1116 SOEnd = M->second.end();
1117 SO != SOEnd && !Done; ++SO) {
1118 assert(SO->second.size() > 0 &&
1119 "All virtual functions have overridding virtual functions");
1120
1121 // C++ [class.abstract]p4:
1122 // A class is abstract if it contains or inherits at least one
1123 // pure virtual function for which the final overrider is pure
1124 // virtual.
1125 if (SO->second.front().Method->isPure()) {
1126 data().Abstract = true;
1127 Done = true;
1128 break;
1129 }
1130 }
1131 }
1132 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001133
1134 // Set access bits correctly on the directly-declared conversions.
1135 for (UnresolvedSetIterator I = data().Conversions.begin(),
1136 E = data().Conversions.end();
1137 I != E; ++I)
1138 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001139}
1140
1141bool CXXRecordDecl::mayBeAbstract() const {
1142 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1143 isDependentContext())
1144 return false;
1145
1146 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1147 BEnd = bases_end();
1148 B != BEnd; ++B) {
1149 CXXRecordDecl *BaseDecl
1150 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1151 if (BaseDecl->isAbstract())
1152 return true;
1153 }
1154
1155 return false;
1156}
1157
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001158CXXMethodDecl *
1159CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001160 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001161 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001162 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001163 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001164 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001165 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001166 isStatic, SCAsWritten, isInline, isConstexpr,
1167 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001168}
1169
Douglas Gregor90916562009-09-29 18:16:17 +00001170bool CXXMethodDecl::isUsualDeallocationFunction() const {
1171 if (getOverloadedOperator() != OO_Delete &&
1172 getOverloadedOperator() != OO_Array_Delete)
1173 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001174
1175 // C++ [basic.stc.dynamic.deallocation]p2:
1176 // A template instance is never a usual deallocation function,
1177 // regardless of its signature.
1178 if (getPrimaryTemplate())
1179 return false;
1180
Douglas Gregor90916562009-09-29 18:16:17 +00001181 // C++ [basic.stc.dynamic.deallocation]p2:
1182 // If a class T has a member deallocation function named operator delete
1183 // with exactly one parameter, then that function is a usual (non-placement)
1184 // deallocation function. [...]
1185 if (getNumParams() == 1)
1186 return true;
1187
1188 // C++ [basic.stc.dynamic.deallocation]p2:
1189 // [...] If class T does not declare such an operator delete but does
1190 // declare a member deallocation function named operator delete with
1191 // exactly two parameters, the second of which has type std::size_t (18.1),
1192 // then this function is a usual deallocation function.
1193 ASTContext &Context = getASTContext();
1194 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001195 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1196 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001197 return false;
1198
1199 // This function is a usual deallocation function if there are no
1200 // single-parameter deallocation functions of the same kind.
1201 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1202 R.first != R.second; ++R.first) {
1203 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1204 if (FD->getNumParams() == 1)
1205 return false;
1206 }
1207
1208 return true;
1209}
1210
Douglas Gregor06a9f362010-05-01 20:49:11 +00001211bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001212 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001213 // A user-declared copy assignment operator X::operator= is a non-static
1214 // non-template member function of class X with exactly one parameter of
1215 // type X, X&, const X&, volatile X& or const volatile X&.
1216 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1217 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001218 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001219 return false;
1220
1221 QualType ParamType = getParamDecl(0)->getType();
1222 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1223 ParamType = Ref->getPointeeType();
1224
1225 ASTContext &Context = getASTContext();
1226 QualType ClassType
1227 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1228 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1229}
1230
Sean Huntffe37fd2011-05-25 20:50:04 +00001231bool CXXMethodDecl::isMoveAssignmentOperator() const {
1232 // C++0x [class.copy]p19:
1233 // A user-declared move assignment operator X::operator= is a non-static
1234 // non-template member function of class X with exactly one parameter of type
1235 // X&&, const X&&, volatile X&&, or const volatile X&&.
1236 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1237 getPrimaryTemplate() || getDescribedFunctionTemplate())
1238 return false;
1239
1240 QualType ParamType = getParamDecl(0)->getType();
1241 if (!isa<RValueReferenceType>(ParamType))
1242 return false;
1243 ParamType = ParamType->getPointeeType();
1244
1245 ASTContext &Context = getASTContext();
1246 QualType ClassType
1247 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1248 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1249}
1250
Anders Carlsson05eb2442009-05-16 23:58:37 +00001251void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001252 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001253 assert(!MD->getParent()->isDependentContext() &&
1254 "Can't add an overridden method to a class template!");
1255
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001256 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001257}
1258
1259CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001260 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001261}
1262
1263CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001264 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001265}
1266
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001267unsigned CXXMethodDecl::size_overridden_methods() const {
1268 return getASTContext().overridden_methods_size(this);
1269}
1270
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001271QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001272 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1273 // If the member function is declared const, the type of this is const X*,
1274 // if the member function is declared volatile, the type of this is
1275 // volatile X*, and if the member function is declared const volatile,
1276 // the type of this is const volatile X*.
1277
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001278 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001279
John McCall3cb0ebd2010-03-10 03:28:59 +00001280 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001281 ClassTy = C.getQualifiedType(ClassTy,
1282 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001283 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001284}
1285
Eli Friedmand7d7f672009-12-06 20:50:05 +00001286bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001287 // If this function is a template instantiation, look at the template from
1288 // which it was instantiated.
1289 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1290 if (!CheckFn)
1291 CheckFn = this;
1292
Eli Friedmand7d7f672009-12-06 20:50:05 +00001293 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001294 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001295}
1296
Sean Huntcbb67482011-01-08 20:30:50 +00001297CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1298 TypeSourceInfo *TInfo, bool IsVirtual,
1299 SourceLocation L, Expr *Init,
1300 SourceLocation R,
1301 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001302 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001303 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1304 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001305{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001306}
1307
Sean Huntcbb67482011-01-08 20:30:50 +00001308CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1309 FieldDecl *Member,
1310 SourceLocation MemberLoc,
1311 SourceLocation L, Expr *Init,
1312 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001313 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001314 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001315 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1316{
1317}
1318
Sean Huntcbb67482011-01-08 20:30:50 +00001319CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1320 IndirectFieldDecl *Member,
1321 SourceLocation MemberLoc,
1322 SourceLocation L, Expr *Init,
1323 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001324 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001325 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001326 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001327{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001328}
1329
Sean Huntcbb67482011-01-08 20:30:50 +00001330CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001331 TypeSourceInfo *TInfo,
1332 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001333 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001334 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1335 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001336 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1337{
1338}
1339
1340CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001341 FieldDecl *Member,
1342 SourceLocation MemberLoc,
1343 SourceLocation L, Expr *Init,
1344 SourceLocation R,
1345 VarDecl **Indices,
1346 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001347 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001348 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001349 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001350{
1351 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1352 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1353}
1354
Sean Huntcbb67482011-01-08 20:30:50 +00001355CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1356 FieldDecl *Member,
1357 SourceLocation MemberLoc,
1358 SourceLocation L, Expr *Init,
1359 SourceLocation R,
1360 VarDecl **Indices,
1361 unsigned NumIndices) {
1362 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001363 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001364 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001365 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1366 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001367}
1368
Sean Huntcbb67482011-01-08 20:30:50 +00001369TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001370 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001371 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001372 else
1373 return TypeLoc();
1374}
1375
Sean Huntcbb67482011-01-08 20:30:50 +00001376const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001377 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001378 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001379 else
1380 return 0;
1381}
1382
Sean Huntcbb67482011-01-08 20:30:50 +00001383SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001384 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001385 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001386
1387 if (isInClassMemberInitializer())
1388 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001389
Douglas Gregor76852c22011-11-01 01:16:03 +00001390 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1391 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1392
1393 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001394}
1395
Sean Huntcbb67482011-01-08 20:30:50 +00001396SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001397 if (isInClassMemberInitializer()) {
1398 FieldDecl *D = getAnyMember();
1399 if (Expr *I = D->getInClassInitializer())
1400 return I->getSourceRange();
1401 return SourceRange();
1402 }
1403
Douglas Gregor802ab452009-12-02 22:36:29 +00001404 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001405}
1406
Douglas Gregorb48fe382008-10-31 09:07:45 +00001407CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001408CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001409 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001410 QualType(), 0, false, false, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001411}
1412
1413CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001414CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001415 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001416 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001417 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001418 bool isExplicit, bool isInline,
1419 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001420 assert(NameInfo.getName().getNameKind()
1421 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001422 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001423 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001424 isExplicit, isInline, isImplicitlyDeclared,
1425 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001426}
1427
Douglas Gregor76852c22011-11-01 01:16:03 +00001428CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1429 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1430 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1431 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1432 return Construct->getConstructor();
1433
1434 return 0;
1435}
1436
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001437bool CXXConstructorDecl::isDefaultConstructor() const {
1438 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001439 // A default constructor for a class X is a constructor of class
1440 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001441 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001442 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001443}
1444
Mike Stump1eb44332009-09-09 15:08:12 +00001445bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001446CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001447 return isCopyOrMoveConstructor(TypeQuals) &&
1448 getParamDecl(0)->getType()->isLValueReferenceType();
1449}
1450
1451bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1452 return isCopyOrMoveConstructor(TypeQuals) &&
1453 getParamDecl(0)->getType()->isRValueReferenceType();
1454}
1455
1456/// \brief Determine whether this is a copy or move constructor.
1457bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001458 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001459 // A non-template constructor for class X is a copy constructor
1460 // if its first parameter is of type X&, const X&, volatile X& or
1461 // const volatile X&, and either there are no other parameters
1462 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001463 // C++0x [class.copy]p3:
1464 // A non-template constructor for class X is a move constructor if its
1465 // first parameter is of type X&&, const X&&, volatile X&&, or
1466 // const volatile X&&, and either there are no other parameters or else
1467 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001468 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001469 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001470 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001471 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001472 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001473
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001474 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001475
1476 // Do we have a reference type?
1477 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001478 if (!ParamRefType)
1479 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001480
Douglas Gregorfd476482009-11-13 23:59:09 +00001481 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001482 ASTContext &Context = getASTContext();
1483
Douglas Gregorfd476482009-11-13 23:59:09 +00001484 CanQualType PointeeType
1485 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001486 CanQualType ClassTy
1487 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001488 if (PointeeType.getUnqualifiedType() != ClassTy)
1489 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001490
John McCall0953e762009-09-24 19:53:00 +00001491 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001492
1493 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001494 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001495 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001496}
1497
Anders Carlssonfaccd722009-08-28 16:57:08 +00001498bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001499 // C++ [class.conv.ctor]p1:
1500 // A constructor declared without the function-specifier explicit
1501 // that can be called with a single parameter specifies a
1502 // conversion from the type of its first parameter to the type of
1503 // its class. Such a constructor is called a converting
1504 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001505 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001506 return false;
1507
Mike Stump1eb44332009-09-09 15:08:12 +00001508 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001509 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001510 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001511 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001512}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001513
Douglas Gregor6493cc52010-11-08 17:16:59 +00001514bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001515 if ((getNumParams() < 1) ||
1516 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1517 (getPrimaryTemplate() == 0) ||
1518 (getDescribedFunctionTemplate() != 0))
1519 return false;
1520
1521 const ParmVarDecl *Param = getParamDecl(0);
1522
1523 ASTContext &Context = getASTContext();
1524 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1525
Douglas Gregor66724ea2009-11-14 01:20:54 +00001526 // Is it the same as our our class type?
1527 CanQualType ClassTy
1528 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1529 if (ParamType.getUnqualifiedType() != ClassTy)
1530 return false;
1531
1532 return true;
1533}
1534
Sebastian Redlf677ea32011-02-05 19:23:19 +00001535const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1536 // Hack: we store the inherited constructor in the overridden method table
1537 method_iterator It = begin_overridden_methods();
1538 if (It == end_overridden_methods())
1539 return 0;
1540
1541 return cast<CXXConstructorDecl>(*It);
1542}
1543
1544void
1545CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1546 // Hack: we store the inherited constructor in the overridden method table
1547 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1548 addOverriddenMethod(BaseCtor);
1549}
1550
Douglas Gregor42a552f2008-11-05 20:51:48 +00001551CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001552CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001553 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001554 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001555}
1556
1557CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001558CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001559 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001560 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001561 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001562 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001563 assert(NameInfo.getName().getNameKind()
1564 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001565 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001566 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001567 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001568}
1569
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001570CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001571CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001572 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001573 QualType(), 0, false, false, false,
Douglas Gregorf5251602011-03-08 17:10:18 +00001574 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001575}
1576
1577CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001578CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001579 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001580 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001581 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001582 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001583 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001584 assert(NameInfo.getName().getNameKind()
1585 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001586 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001587 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001588 isInline, isExplicit, isConstexpr,
1589 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001590}
1591
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001592LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001593 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001594 SourceLocation ExternLoc,
1595 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001596 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001597 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001598 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001599}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001600
1601UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1602 SourceLocation L,
1603 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001604 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001605 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001606 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001607 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001608 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1609 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001610 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1611 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001612}
1613
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001614NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1615 if (NamespaceAliasDecl *NA =
1616 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1617 return NA->getNamespace();
1618 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1619}
1620
Mike Stump1eb44332009-09-09 15:08:12 +00001621NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001622 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001623 SourceLocation AliasLoc,
1624 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001625 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001626 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001627 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001628 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1629 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001630 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1631 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001632}
1633
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001634UsingDecl *UsingShadowDecl::getUsingDecl() const {
1635 const UsingShadowDecl *Shadow = this;
1636 while (const UsingShadowDecl *NextShadow =
1637 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1638 Shadow = NextShadow;
1639 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1640}
1641
1642void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1643 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1644 "declaration already in set");
1645 assert(S->getUsingDecl() == this);
1646
1647 if (FirstUsingShadow)
1648 S->UsingOrNextShadow = FirstUsingShadow;
1649 FirstUsingShadow = S;
1650}
1651
1652void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1653 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1654 "declaration not in set");
1655 assert(S->getUsingDecl() == this);
1656
1657 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1658
1659 if (FirstUsingShadow == S) {
1660 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1661 S->UsingOrNextShadow = this;
1662 return;
1663 }
1664
1665 UsingShadowDecl *Prev = FirstUsingShadow;
1666 while (Prev->UsingOrNextShadow != S)
1667 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1668 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1669 S->UsingOrNextShadow = this;
1670}
1671
Douglas Gregordc355712011-02-25 00:36:19 +00001672UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1673 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001674 const DeclarationNameInfo &NameInfo,
1675 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001676 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001677}
1678
John McCall7ba107a2009-11-18 02:36:19 +00001679UnresolvedUsingValueDecl *
1680UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1681 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001682 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001683 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001684 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001685 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001686}
1687
1688UnresolvedUsingTypenameDecl *
1689UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1690 SourceLocation UsingLoc,
1691 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001692 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001693 SourceLocation TargetNameLoc,
1694 DeclarationName TargetName) {
1695 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001696 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001697 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001698}
1699
Anders Carlssonfb311762009-03-14 00:25:26 +00001700StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001701 SourceLocation StaticAssertLoc,
1702 Expr *AssertExpr,
1703 StringLiteral *Message,
1704 SourceLocation RParenLoc) {
1705 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1706 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001707}
1708
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001709static const char *getAccessName(AccessSpecifier AS) {
1710 switch (AS) {
1711 default:
1712 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00001713 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001714 case AS_public:
1715 return "public";
1716 case AS_private:
1717 return "private";
1718 case AS_protected:
1719 return "protected";
1720 }
1721}
1722
1723const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1724 AccessSpecifier AS) {
1725 return DB << getAccessName(AS);
1726}