blob: aaa75dcad78f8e8db9562e8c10a8a0316a54af46 [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000017#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000018#include "clang/AST/CXXInheritance.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000020#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000021#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000022#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000023#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// Decl Allocation/Deallocation Method Implementations
28//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000029
John McCall86ff3082010-02-04 22:26:26 +000030CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
31 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000032 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
33 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000034 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000035 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000036 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Douglas Gregor2bb11012011-05-13 01:05:07 +000037 HasMutableFields(false), HasTrivialDefaultConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000038 HasConstExprNonCopyMoveConstructor(false), HasTrivialCopyConstructor(true),
39 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
40 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
41 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000042 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000043 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
44 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sean Hunt37b8c9e2011-05-09 21:45:35 +000045 DeclaredDestructor(false), NumBases(0), NumVBases(0), Bases(), VBases(),
46 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000047}
48
49CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000050 SourceLocation StartLoc, SourceLocation IdLoc,
51 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
52 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000053 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000054 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000055
Jay Foad4ba2a172011-01-12 09:06:06 +000056CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000057 DeclContext *DC, SourceLocation StartLoc,
58 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000059 CXXRecordDecl* PrevDecl,
60 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000061 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
62 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000063
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000064 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000065 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000066 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000067 return R;
68}
69
Jay Foad4ba2a172011-01-12 09:06:06 +000070CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000071 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
72 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000073}
74
Mike Stump1eb44332009-09-09 15:08:12 +000075void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000076CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000077 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000078 ASTContext &C = getASTContext();
79
Mike Stump1eb44332009-09-09 15:08:12 +000080 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000081 // An aggregate is an array or a class (clause 9) with [...]
82 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000083 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000084
Douglas Gregor7c789c12010-10-29 22:39:52 +000085 if (!data().Bases.isOffset() && data().NumBases > 0)
86 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000087
Anders Carlsson6f6de732010-03-29 05:13:12 +000088 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000089 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000090
91 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +000092 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000093
John McCall86ff3082010-02-04 22:26:26 +000094 data().Bases = new(C) CXXBaseSpecifier [NumBases];
95 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000096 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +000097 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000098 // Keep track of inherited vbases for this base class.
99 const CXXBaseSpecifier *Base = Bases[i];
100 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000101 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000102 if (BaseType->isDependentType())
103 continue;
104 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000105 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000106
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000107 // C++ [dcl.init.aggr]p1:
108 // An aggregate is [...] a class with [...] no base classes [...].
109 data().Aggregate = false;
110
111 // C++ [class]p4:
112 // A POD-struct is an aggregate class...
113 data().PlainOldData = false;
114
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000115 // A class with a non-empty base class is not empty.
116 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000117 if (!BaseClassDecl->isEmpty()) {
118 if (!data().Empty) {
119 // C++0x [class]p7:
120 // A standard-layout class is a class that:
121 // [...]
122 // -- either has no non-static data members in the most derived
123 // class and at most one base class with non-static data members,
124 // or has no base classes with non-static data members, and
125 // If this is the second non-empty base, then neither of these two
126 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000127 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000128 }
129
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000130 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000131 data().HasNoNonEmptyBases = false;
132 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000133
Douglas Gregor85606eb2010-09-28 20:50:54 +0000134 // C++ [class.virtual]p1:
135 // A class that declares or inherits a virtual function is called a
136 // polymorphic class.
137 if (BaseClassDecl->isPolymorphic())
138 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000139
Chandler Carrutha8225442011-04-30 09:17:45 +0000140 // C++0x [class]p7:
141 // A standard-layout class is a class that: [...]
142 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000143 if (!BaseClassDecl->isStandardLayout())
144 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000145
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000146 // Record if this base is the first non-literal field or base.
147 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
148 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000149
Anders Carlsson6f6de732010-03-29 05:13:12 +0000150 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000151 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000152 BaseClassDecl->vbases_begin(),
153 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000154 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000155 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000156 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000157 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000158
159 if (Base->isVirtual()) {
160 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000161 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000162 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000163
164 // C++0x [meta.unary.prop] is_empty:
165 // T is a class type, but not a union type, with ... no virtual base
166 // classes
167 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000168
169 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000170 // A default constructor is trivial [...] if:
171 // -- its class has [...] no virtual bases
172 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000173
174 // C++0x [class.copy]p13:
175 // A copy/move constructor for class X is trivial if it is neither
176 // user-provided nor deleted and if
177 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000178 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000179 data().HasTrivialMoveConstructor = false;
180
181 // C++0x [class.copy]p27:
182 // A copy/move assignment operator for class X is trivial if it is
183 // neither user-provided nor deleted and if
184 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000185 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000186 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000187
188 // C++0x [class]p7:
189 // A standard-layout class is a class that: [...]
190 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000191 data().IsStandardLayout = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000192 } else {
193 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000194 // A default constructor is trivial [...] if:
195 // -- all the direct base classes of its class have trivial default
196 // constructors.
197 if (!BaseClassDecl->hasTrivialDefaultConstructor())
198 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000199
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000200 // C++0x [class.copy]p13:
201 // A copy/move constructor for class X is trivial if [...]
202 // [...]
203 // -- the constructor selected to copy/move each direct base class
204 // subobject is trivial, and
205 // FIXME: C++0x: We need to only consider the selected constructor
206 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000207 if (!BaseClassDecl->hasTrivialCopyConstructor())
208 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000209 if (!BaseClassDecl->hasTrivialMoveConstructor())
210 data().HasTrivialMoveConstructor = false;
211
212 // C++0x [class.copy]p27:
213 // A copy/move assignment operator for class X is trivial if [...]
214 // [...]
215 // -- the assignment operator selected to copy/move each direct base
216 // class subobject is trivial, and
217 // FIXME: C++0x: We need to only consider the selected operator instead
218 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000219 if (!BaseClassDecl->hasTrivialCopyAssignment())
220 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000221 if (!BaseClassDecl->hasTrivialMoveAssignment())
222 data().HasTrivialMoveAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000223 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000224
225 // C++ [class.ctor]p3:
226 // A destructor is trivial if all the direct base classes of its class
227 // have trivial destructors.
228 if (!BaseClassDecl->hasTrivialDestructor())
229 data().HasTrivialDestructor = false;
Douglas Gregor2bb11012011-05-13 01:05:07 +0000230
John McCallf85e1932011-06-15 23:02:42 +0000231 // A class has an Objective-C object member if... or any of its bases
232 // has an Objective-C object member.
233 if (BaseClassDecl->hasObjectMember())
234 setHasObjectMember(true);
235
Douglas Gregor2bb11012011-05-13 01:05:07 +0000236 // Keep track of the presence of mutable fields.
237 if (BaseClassDecl->hasMutableFields())
238 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000239 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000240
241 if (VBases.empty())
242 return;
243
244 // Create base specifier for any direct or indirect virtual bases.
245 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
246 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000247 for (int I = 0, E = VBases.size(); I != E; ++I)
248 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000249}
250
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000251/// Callback function for CXXRecordDecl::forallBases that acknowledges
252/// that it saw a base class.
253static bool SawBase(const CXXRecordDecl *, void *) {
254 return true;
255}
256
257bool CXXRecordDecl::hasAnyDependentBases() const {
258 if (!isDependentContext())
259 return false;
260
261 return !forallBases(SawBase, 0);
262}
263
Sean Huntffe37fd2011-05-25 20:50:04 +0000264bool CXXRecordDecl::hasConstCopyConstructor() const {
265 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000266}
267
Chandler Carruthb7e95892011-04-23 10:47:28 +0000268bool CXXRecordDecl::isTriviallyCopyable() const {
269 // C++0x [class]p5:
270 // A trivially copyable class is a class that:
271 // -- has no non-trivial copy constructors,
272 if (!hasTrivialCopyConstructor()) return false;
273 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000274 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000275 // -- has no non-trivial copy assignment operators,
276 if (!hasTrivialCopyAssignment()) return false;
277 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000278 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000279 // -- has a trivial destructor.
280 if (!hasTrivialDestructor()) return false;
281
282 return true;
283}
284
Douglas Gregor0d405db2010-07-01 20:59:04 +0000285/// \brief Perform a simplistic form of overload resolution that only considers
286/// cv-qualifiers on a single parameter, and return the best overload candidate
287/// (if there is one).
288static CXXMethodDecl *
289GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000290 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000291 if (Cands.empty())
292 return 0;
293 if (Cands.size() == 1)
294 return Cands[0].first;
295
296 unsigned Best = 0, N = Cands.size();
297 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000298 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000299 Best = I;
300
301 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000302 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000303 return 0;
304
305 return Cands[Best].first;
306}
307
Sean Huntffe37fd2011-05-25 20:50:04 +0000308CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
309 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000310 QualType ClassType
311 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000312 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000313 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000314 Context.getCanonicalType(ClassType));
315 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000316 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000317 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000318 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000319 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000320 // C++ [class.copy]p2:
321 // A non-template constructor for class X is a copy constructor if [...]
322 if (isa<FunctionTemplateDecl>(*Con))
323 continue;
324
Douglas Gregor0d405db2010-07-01 20:59:04 +0000325 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
326 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000327 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
328 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000329 Found.push_back(std::make_pair(
330 const_cast<CXXConstructorDecl *>(Constructor),
331 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000332 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000333 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000334
335 return cast_or_null<CXXConstructorDecl>(
336 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000337}
338
Sean Huntffe37fd2011-05-25 20:50:04 +0000339CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
340 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
341 if (I->isMoveConstructor())
342 return *I;
343
344 return 0;
345}
346
Douglas Gregorb87786f2010-07-01 17:48:08 +0000347CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
348 ASTContext &Context = getASTContext();
349 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
350 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
351
Chris Lattner5f9e2722011-07-23 10:55:15 +0000352 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000353 DeclContext::lookup_const_iterator Op, OpEnd;
354 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
355 // C++ [class.copy]p9:
356 // A user-declared copy assignment operator is a non-static non-template
357 // member function of class X with exactly one parameter of type X, X&,
358 // const X&, volatile X& or const volatile X&.
359 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
360 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
361 continue;
362
363 const FunctionProtoType *FnType
364 = Method->getType()->getAs<FunctionProtoType>();
365 assert(FnType && "Overloaded operator has no prototype.");
366 // Don't assert on this; an invalid decl might have been left in the AST.
367 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
368 continue;
369
370 QualType ArgType = FnType->getArgType(0);
371 Qualifiers Quals;
372 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
373 ArgType = Ref->getPointeeType();
374 // If we have a const argument and we have a reference to a non-const,
375 // this function does not match.
376 if (ArgIsConst && !ArgType.isConstQualified())
377 continue;
378
379 Quals = ArgType.getQualifiers();
380 } else {
381 // By-value copy-assignment operators are treated like const X&
382 // copy-assignment operators.
383 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
384 }
385
386 if (!Context.hasSameUnqualifiedType(ArgType, Class))
387 continue;
388
389 // Save this copy-assignment operator. It might be "the one".
390 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
391 }
392
393 // Use a simplistic form of overload resolution to find the candidate.
394 return GetBestOverloadCandidateSimple(Found);
395}
396
Sean Huntffe37fd2011-05-25 20:50:04 +0000397CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
398 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
399 if (I->isMoveAssignmentOperator())
400 return *I;
401
402 return 0;
403}
404
Douglas Gregor21386642010-09-28 21:55:22 +0000405void CXXRecordDecl::markedVirtualFunctionPure() {
406 // C++ [class.abstract]p2:
407 // A class is abstract if it has at least one pure virtual function.
408 data().Abstract = true;
409}
410
411void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000412 // Ignore friends and invalid declarations.
413 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000414 return;
415
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000416 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
417 if (FunTmpl)
418 D = FunTmpl->getTemplatedDecl();
419
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000420 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
421 if (Method->isVirtual()) {
422 // C++ [dcl.init.aggr]p1:
423 // An aggregate is an array or a class with [...] no virtual functions.
424 data().Aggregate = false;
425
426 // C++ [class]p4:
427 // A POD-struct is an aggregate class...
428 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000429
430 // Virtual functions make the class non-empty.
431 // FIXME: Standard ref?
432 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000433
434 // C++ [class.virtual]p1:
435 // A class that declares or inherits a virtual function is called a
436 // polymorphic class.
437 data().Polymorphic = true;
438
Sean Hunt023df372011-05-09 18:22:59 +0000439 // C++0x [class.ctor]p5
440 // A default constructor is trivial [...] if:
441 // -- its class has no virtual functions [...]
442 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000443
444 // C++0x [class.copy]p13:
445 // A copy/move constructor for class X is trivial if [...]
446 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000447 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000448 data().HasTrivialMoveConstructor = false;
449
450 // C++0x [class.copy]p27:
451 // A copy/move assignment operator for class X is trivial if [...]
452 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000453 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000454 data().HasTrivialMoveAssignment = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000455 // FIXME: Destructor?
Chandler Carrutha8225442011-04-30 09:17:45 +0000456
457 // C++0x [class]p7:
458 // A standard-layout class is a class that: [...]
459 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000460 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000461 }
462 }
463
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000464 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000465 // Notify that an implicit member was added after the definition
466 // was completed.
467 if (!isBeingDefined())
468 if (ASTMutationListener *L = getASTMutationListener())
469 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000470
Sean Huntffe37fd2011-05-25 20:50:04 +0000471 // If this is a special member function, note that it was added and then
472 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000473 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000474 if (Constructor->isDefaultConstructor())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000475 data().DeclaredDefaultConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000476 else if (Constructor->isCopyConstructor())
477 data().DeclaredCopyConstructor = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000478 else if (Constructor->isMoveConstructor())
479 data().DeclaredMoveConstructor = true;
480 else
481 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000482 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000483 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000484 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000485 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000486 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
487 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000488 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000489 else if (Method->isMoveAssignmentOperator())
490 data().DeclaredMoveAssignment = true;
491 else
492 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000493 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000494 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000495
Sean Huntffe37fd2011-05-25 20:50:04 +0000496NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000497 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000498 }
499
500 // Handle (user-declared) constructors.
501 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
502 // Note that we have a user-declared constructor.
503 data().UserDeclaredConstructor = true;
504
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000505 // FIXME: Under C++0x, /only/ special member functions may be user-provided.
506 // This is probably a defect.
507 bool UserProvided = false;
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;
513 if (Constructor->isUserProvided()) {
514 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000515 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000516 UserProvided = true;
517 }
518 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000519
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000520 // Note when we have a user-declared copy or move constructor, which will
521 // suppress the implicit declaration of those constructors.
522 if (!FunTmpl) {
523 if (Constructor->isCopyConstructor()) {
524 data().UserDeclaredCopyConstructor = true;
525 data().DeclaredCopyConstructor = true;
526
527 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000528 // A copy/move constructor for class X is trivial if it is not
529 // user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000530 if (Constructor->isUserProvided()) {
Sean Hunt023df372011-05-09 18:22:59 +0000531 data().HasTrivialCopyConstructor = false;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000532 UserProvided = true;
533 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000534 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000535 data().UserDeclaredMoveConstructor = true;
536 data().DeclaredMoveConstructor = true;
537
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000538 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000539 // A copy/move constructor for class X is trivial if it is not
540 // user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000541 if (Constructor->isUserProvided()) {
Sean Hunt023df372011-05-09 18:22:59 +0000542 data().HasTrivialMoveConstructor = false;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000543 UserProvided = true;
544 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000545 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000546 }
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000547 if (Constructor->isConstExpr() &&
548 !Constructor->isCopyOrMoveConstructor()) {
549 // Record if we see any constexpr constructors which are niether copy
550 // nor move constructors.
551 data().HasConstExprNonCopyMoveConstructor = true;
552 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000553
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000554 // C++ [dcl.init.aggr]p1:
555 // An aggregate is an array or a class with no user-declared
556 // constructors [...].
557 // C++0x [dcl.init.aggr]p1:
558 // An aggregate is an array or a class with no user-provided
559 // constructors [...].
560 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
561 data().Aggregate = false;
562
563 // C++ [class]p4:
564 // A POD-struct is an aggregate class [...]
565 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
566 // type is technically an aggregate in C++0x since it wouldn't be in 03.
567 data().PlainOldData = false;
568
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000569 return;
570 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000571
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000572 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000573 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000574 data().DeclaredDestructor = true;
575 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000576
577 // C++ [class]p4:
578 // A POD-struct is an aggregate class that has [...] no user-defined
579 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000580 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000581 data().PlainOldData = false;
582
Sean Huntcf34e752011-05-16 22:41:40 +0000583 // C++0x [class.dtor]p5:
584 // A destructor is trivial if it is not user-provided and [...]
585 if (DD->isUserProvided())
586 data().HasTrivialDestructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000587
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000588 return;
589 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000590
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000591 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000592 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000593 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000594 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000595 // A POD-struct is an aggregate class that [...] has no user-defined
596 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000597 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000598 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000599
Sean Huntffe37fd2011-05-25 20:50:04 +0000600 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000601
Sean Huntffe37fd2011-05-25 20:50:04 +0000602 // Suppress the implicit declaration of a copy constructor.
603 data().UserDeclaredCopyAssignment = true;
604 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000605
Sean Huntffe37fd2011-05-25 20:50:04 +0000606 // C++0x [class.copy]p27:
607 // A copy/move assignment operator for class X is trivial if it is
608 // neither user-provided nor deleted [...]
609 if (Method->isUserProvided())
610 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000611
Sean Huntffe37fd2011-05-25 20:50:04 +0000612 return;
613 }
614
615 if (Method->isMoveAssignmentOperator()) {
616 // This is an extension in C++03 mode, but we'll keep consistency by
617 // taking a move assignment operator to induce non-POD-ness
618 data().PlainOldData = false;
619
620 // This is a move assignment operator.
621 data().UserDeclaredMoveAssignment = true;
622 data().DeclaredMoveAssignment = true;
623
624 // C++0x [class.copy]p27:
625 // A copy/move assignment operator for class X is trivial if it is
626 // neither user-provided nor deleted [...]
627 if (Method->isUserProvided())
628 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000629 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000630
Douglas Gregore80622f2010-09-29 04:25:11 +0000631 // Keep the list of conversion functions up-to-date.
632 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
633 // We don't record specializations.
634 if (Conversion->getPrimaryTemplate())
635 return;
636
637 // FIXME: We intentionally don't use the decl's access here because it
638 // hasn't been set yet. That's really just a misdesign in Sema.
639
640 if (FunTmpl) {
641 if (FunTmpl->getPreviousDeclaration())
642 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
643 FunTmpl);
644 else
645 data().Conversions.addDecl(FunTmpl);
646 } else {
647 if (Conversion->getPreviousDeclaration())
648 data().Conversions.replace(Conversion->getPreviousDeclaration(),
649 Conversion);
650 else
651 data().Conversions.addDecl(Conversion);
652 }
653 }
654
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000655 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000656 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000657
658 // Handle non-static data members.
659 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
660 // C++ [dcl.init.aggr]p1:
661 // An aggregate is an array or a class (clause 9) with [...] no
662 // private or protected non-static data members (clause 11).
663 //
664 // A POD must be an aggregate.
665 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
666 data().Aggregate = false;
667 data().PlainOldData = false;
668 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000669
670 // C++0x [class]p7:
671 // A standard-layout class is a class that:
672 // [...]
673 // -- has the same access control for all non-static data members,
674 switch (D->getAccess()) {
675 case AS_private: data().HasPrivateFields = true; break;
676 case AS_protected: data().HasProtectedFields = true; break;
677 case AS_public: data().HasPublicFields = true; break;
678 case AS_none: assert(0 && "Invalid access specifier");
679 };
680 if ((data().HasPrivateFields + data().HasProtectedFields +
681 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000682 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000683
Douglas Gregor2bb11012011-05-13 01:05:07 +0000684 // Keep track of the presence of mutable fields.
685 if (Field->isMutable())
686 data().HasMutableFields = true;
687
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000688 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000689 // A POD struct is a class that is both a trivial class and a
690 // standard-layout class, and has no non-static data members of type
691 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000692 //
693 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
694 // that does not explicitly have no lifetime makes the class a non-POD.
695 // However, we delay setting PlainOldData to false in this case so that
696 // Sema has a chance to diagnostic causes where the same class will be
697 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
698 // In this case, the class will become a non-POD class when we complete
699 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000700 ASTContext &Context = getASTContext();
701 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000702 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
703 if (!Context.getLangOptions().ObjCAutoRefCount ||
704 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
705 setHasObjectMember(true);
706 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000707 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000708
Chandler Carrutha8225442011-04-30 09:17:45 +0000709 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000710 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000711
Chandler Carrutha8225442011-04-30 09:17:45 +0000712 // C++0x [class]p7:
713 // A standard-layout class is a class that:
714 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000715 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000716 }
717
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000718 // Record if this field is the first non-literal field or base.
719 if (!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType())
720 data().HasNonLiteralTypeFieldsOrBases = true;
721
Richard Smith7a614d82011-06-11 17:19:42 +0000722 if (Field->hasInClassInitializer()) {
723 // C++0x [class]p5:
724 // A default constructor is trivial if [...] no non-static data member
725 // of its class has a brace-or-equal-initializer.
726 data().HasTrivialDefaultConstructor = false;
727
728 // C++0x [dcl.init.aggr]p1:
729 // An aggregate is a [...] class with [...] no
730 // brace-or-equal-initializers for non-static data members.
731 data().Aggregate = false;
732
733 // C++0x [class]p10:
734 // A POD struct is [...] a trivial class.
735 data().PlainOldData = false;
736 }
737
Douglas Gregor85606eb2010-09-28 20:50:54 +0000738 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
739 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
740 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000741 // C++0x [class.ctor]p5:
742 // A defulat constructor is trivial [...] if:
743 // -- for all the non-static data members of its class that are of
744 // class type (or array thereof), each such class has a trivial
745 // default constructor.
746 if (!FieldRec->hasTrivialDefaultConstructor())
747 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000748
749 // C++0x [class.copy]p13:
750 // A copy/move constructor for class X is trivial if [...]
751 // [...]
752 // -- for each non-static data member of X that is of class type (or
753 // an array thereof), the constructor selected to copy/move that
754 // member is trivial;
755 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000756 if (!FieldRec->hasTrivialCopyConstructor())
757 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000758 if (!FieldRec->hasTrivialMoveConstructor())
759 data().HasTrivialMoveConstructor = false;
760
761 // C++0x [class.copy]p27:
762 // A copy/move assignment operator for class X is trivial if [...]
763 // [...]
764 // -- for each non-static data member of X that is of class type (or
765 // an array thereof), the assignment operator selected to
766 // copy/move that member is trivial;
767 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000768 if (!FieldRec->hasTrivialCopyAssignment())
769 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000770 if (!FieldRec->hasTrivialMoveAssignment())
771 data().HasTrivialMoveAssignment = false;
772
Douglas Gregor85606eb2010-09-28 20:50:54 +0000773 if (!FieldRec->hasTrivialDestructor())
774 data().HasTrivialDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000775 if (FieldRec->hasObjectMember())
776 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000777
778 // C++0x [class]p7:
779 // A standard-layout class is a class that:
780 // -- has no non-static data members of type non-standard-layout
781 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000782 if (!FieldRec->isStandardLayout())
783 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000784
785 // C++0x [class]p7:
786 // A standard-layout class is a class that:
787 // [...]
788 // -- has no base classes of the same type as the first non-static
789 // data member.
790 // We don't want to expend bits in the state of the record decl
791 // tracking whether this is the first non-static data member so we
792 // cheat a bit and use some of the existing state: the empty bit.
793 // Virtual bases and virtual methods make a class non-empty, but they
794 // also make it non-standard-layout so we needn't check here.
795 // A non-empty base class may leave the class standard-layout, but not
796 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000797 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000798 // data member must come through here with Empty still true, and Empty
799 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000800 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000801 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
802 BE = bases_end();
803 BI != BE; ++BI) {
804 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000805 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000806 break;
807 }
808 }
809 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000810
811 // Keep track of the presence of mutable fields.
812 if (FieldRec->hasMutableFields())
813 data().HasMutableFields = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000814 }
815 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000816
817 // C++0x [class]p7:
818 // A standard-layout class is a class that:
819 // [...]
820 // -- either has no non-static data members in the most derived
821 // class and at most one base class with non-static data members,
822 // or has no base classes with non-static data members, and
823 // At this point we know that we have a non-static data member, so the last
824 // clause holds.
825 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000826 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000827
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000828 // If this is not a zero-length bit-field, then the class is not empty.
829 if (data().Empty) {
830 if (!Field->getBitWidth())
831 data().Empty = false;
832 else if (!Field->getBitWidth()->isTypeDependent() &&
833 !Field->getBitWidth()->isValueDependent()) {
834 llvm::APSInt Bits;
835 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
836 if (!!Bits)
837 data().Empty = false;
838 }
839 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000840 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000841
842 // Handle using declarations of conversion functions.
843 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
844 if (Shadow->getDeclName().getNameKind()
845 == DeclarationName::CXXConversionFunctionName)
846 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000847}
848
John McCallb05b5f32010-03-15 09:07:48 +0000849static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
850 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000851 if (isa<UsingShadowDecl>(Conv))
852 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000853 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
854 T = ConvTemp->getTemplatedDecl()->getResultType();
855 else
856 T = cast<CXXConversionDecl>(Conv)->getConversionType();
857 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000858}
859
John McCallb05b5f32010-03-15 09:07:48 +0000860/// Collect the visible conversions of a base class.
861///
862/// \param Base a base class of the class we're considering
863/// \param InVirtual whether this base class is a virtual base (or a base
864/// of a virtual base)
865/// \param Access the access along the inheritance path to this base
866/// \param ParentHiddenTypes the conversions provided by the inheritors
867/// of this base
868/// \param Output the set to which to add conversions from non-virtual bases
869/// \param VOutput the set to which to add conversions from virtual bases
870/// \param HiddenVBaseCs the set of conversions which were hidden in a
871/// virtual base along some inheritance path
872static void CollectVisibleConversions(ASTContext &Context,
873 CXXRecordDecl *Record,
874 bool InVirtual,
875 AccessSpecifier Access,
876 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
877 UnresolvedSetImpl &Output,
878 UnresolvedSetImpl &VOutput,
879 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
880 // The set of types which have conversions in this class or its
881 // subclasses. As an optimization, we don't copy the derived set
882 // unless it might change.
883 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
884 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
885
886 // Collect the direct conversions and figure out which conversions
887 // will be hidden in the subclasses.
888 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
889 if (!Cs.empty()) {
890 HiddenTypesBuffer = ParentHiddenTypes;
891 HiddenTypes = &HiddenTypesBuffer;
892
893 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
894 bool Hidden =
895 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
896
897 // If this conversion is hidden and we're in a virtual base,
898 // remember that it's hidden along some inheritance path.
899 if (Hidden && InVirtual)
900 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
901
902 // If this conversion isn't hidden, add it to the appropriate output.
903 else if (!Hidden) {
904 AccessSpecifier IAccess
905 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
906
907 if (InVirtual)
908 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000909 else
John McCallb05b5f32010-03-15 09:07:48 +0000910 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000911 }
912 }
913 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000914
John McCallb05b5f32010-03-15 09:07:48 +0000915 // Collect information recursively from any base classes.
916 for (CXXRecordDecl::base_class_iterator
917 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
918 const RecordType *RT = I->getType()->getAs<RecordType>();
919 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000920
John McCallb05b5f32010-03-15 09:07:48 +0000921 AccessSpecifier BaseAccess
922 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
923 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000924
John McCallb05b5f32010-03-15 09:07:48 +0000925 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
926 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
927 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000928 }
John McCallb05b5f32010-03-15 09:07:48 +0000929}
Sebastian Redl9994a342009-10-25 17:03:50 +0000930
John McCallb05b5f32010-03-15 09:07:48 +0000931/// Collect the visible conversions of a class.
932///
933/// This would be extremely straightforward if it weren't for virtual
934/// bases. It might be worth special-casing that, really.
935static void CollectVisibleConversions(ASTContext &Context,
936 CXXRecordDecl *Record,
937 UnresolvedSetImpl &Output) {
938 // The collection of all conversions in virtual bases that we've
939 // found. These will be added to the output as long as they don't
940 // appear in the hidden-conversions set.
941 UnresolvedSet<8> VBaseCs;
942
943 // The set of conversions in virtual bases that we've determined to
944 // be hidden.
945 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
946
947 // The set of types hidden by classes derived from this one.
948 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
949
950 // Go ahead and collect the direct conversions and add them to the
951 // hidden-types set.
952 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
953 Output.append(Cs.begin(), Cs.end());
954 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
955 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
956
957 // Recursively collect conversions from base classes.
958 for (CXXRecordDecl::base_class_iterator
959 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
960 const RecordType *RT = I->getType()->getAs<RecordType>();
961 if (!RT) continue;
962
963 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
964 I->isVirtual(), I->getAccessSpecifier(),
965 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
966 }
967
968 // Add any unhidden conversions provided by virtual bases.
969 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
970 I != E; ++I) {
971 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
972 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000973 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000974}
975
976/// getVisibleConversionFunctions - get all conversion functions visible
977/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000978const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000979 // If root class, all conversions are visible.
980 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000981 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000982 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000983 if (data().ComputedVisibleConversions)
984 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000985 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000986 data().ComputedVisibleConversions = true;
987 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000988}
989
John McCall32daa422010-03-31 01:36:47 +0000990void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
991 // This operation is O(N) but extremely rare. Sema only uses it to
992 // remove UsingShadowDecls in a class that were followed by a direct
993 // declaration, e.g.:
994 // class A : B {
995 // using B::operator int;
996 // operator int();
997 // };
998 // This is uncommon by itself and even more uncommon in conjunction
999 // with sufficiently large numbers of directly-declared conversions
1000 // that asymptotic behavior matters.
1001
1002 UnresolvedSetImpl &Convs = *getConversionFunctions();
1003 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1004 if (Convs[I].getDecl() == ConvDecl) {
1005 Convs.erase(I);
1006 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1007 && "conversion was found multiple times in unresolved set");
1008 return;
1009 }
1010 }
1011
1012 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001013}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001014
Douglas Gregorf6b11852009-10-08 15:14:33 +00001015CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001016 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001017 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1018
1019 return 0;
1020}
1021
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001022MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1023 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1024}
1025
Douglas Gregorf6b11852009-10-08 15:14:33 +00001026void
1027CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1028 TemplateSpecializationKind TSK) {
1029 assert(TemplateOrInstantiation.isNull() &&
1030 "Previous template or instantiation?");
1031 assert(!isa<ClassTemplateSpecializationDecl>(this));
1032 TemplateOrInstantiation
1033 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1034}
1035
Anders Carlssonb13e3572009-12-07 06:33:48 +00001036TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1037 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001038 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1039 return Spec->getSpecializationKind();
1040
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001041 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001042 return MSInfo->getTemplateSpecializationKind();
1043
1044 return TSK_Undeclared;
1045}
1046
1047void
1048CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1049 if (ClassTemplateSpecializationDecl *Spec
1050 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1051 Spec->setSpecializationKind(TSK);
1052 return;
1053 }
1054
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001055 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001056 MSInfo->setTemplateSpecializationKind(TSK);
1057 return;
1058 }
1059
1060 assert(false && "Not a class template or member class specialization");
1061}
1062
Douglas Gregor1d110e02010-07-01 14:13:13 +00001063CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1064 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001065 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001066
1067 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001068 = Context.DeclarationNames.getCXXDestructorName(
1069 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001070
John McCallc0bf4622010-02-23 00:48:20 +00001071 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001072 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001073 if (I == E)
1074 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001076 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001077 return Dtor;
1078}
1079
Douglas Gregorda2142f2011-02-19 18:51:44 +00001080void CXXRecordDecl::completeDefinition() {
1081 completeDefinition(0);
1082}
1083
1084void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1085 RecordDecl::completeDefinition();
1086
John McCallf85e1932011-06-15 23:02:42 +00001087 if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) {
1088 // Objective-C Automatic Reference Counting:
1089 // If a class has a non-static data member of Objective-C pointer
1090 // type (or array thereof), it is a non-POD type and its
1091 // default constructor (if any), copy constructor, copy assignment
1092 // operator, and destructor are non-trivial.
1093 struct DefinitionData &Data = data();
1094 Data.PlainOldData = false;
1095 Data.HasTrivialDefaultConstructor = false;
1096 Data.HasTrivialCopyConstructor = false;
1097 Data.HasTrivialCopyAssignment = false;
1098 Data.HasTrivialDestructor = false;
1099 }
1100
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001101 // If the class may be abstract (but hasn't been marked as such), check for
1102 // any pure final overriders.
1103 if (mayBeAbstract()) {
1104 CXXFinalOverriderMap MyFinalOverriders;
1105 if (!FinalOverriders) {
1106 getFinalOverriders(MyFinalOverriders);
1107 FinalOverriders = &MyFinalOverriders;
1108 }
1109
1110 bool Done = false;
1111 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1112 MEnd = FinalOverriders->end();
1113 M != MEnd && !Done; ++M) {
1114 for (OverridingMethods::iterator SO = M->second.begin(),
1115 SOEnd = M->second.end();
1116 SO != SOEnd && !Done; ++SO) {
1117 assert(SO->second.size() > 0 &&
1118 "All virtual functions have overridding virtual functions");
1119
1120 // C++ [class.abstract]p4:
1121 // A class is abstract if it contains or inherits at least one
1122 // pure virtual function for which the final overrider is pure
1123 // virtual.
1124 if (SO->second.front().Method->isPure()) {
1125 data().Abstract = true;
1126 Done = true;
1127 break;
1128 }
1129 }
1130 }
1131 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001132
1133 // Set access bits correctly on the directly-declared conversions.
1134 for (UnresolvedSetIterator I = data().Conversions.begin(),
1135 E = data().Conversions.end();
1136 I != E; ++I)
1137 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001138}
1139
1140bool CXXRecordDecl::mayBeAbstract() const {
1141 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1142 isDependentContext())
1143 return false;
1144
1145 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1146 BEnd = bases_end();
1147 B != BEnd; ++B) {
1148 CXXRecordDecl *BaseDecl
1149 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1150 if (BaseDecl->isAbstract())
1151 return true;
1152 }
1153
1154 return false;
1155}
1156
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001157CXXMethodDecl *
1158CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001159 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001160 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001161 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001162 bool isStatic, StorageClass SCAsWritten, bool isInline,
1163 SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001164 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001165 isStatic, SCAsWritten, isInline, EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001166}
1167
Douglas Gregor90916562009-09-29 18:16:17 +00001168bool CXXMethodDecl::isUsualDeallocationFunction() const {
1169 if (getOverloadedOperator() != OO_Delete &&
1170 getOverloadedOperator() != OO_Array_Delete)
1171 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001172
1173 // C++ [basic.stc.dynamic.deallocation]p2:
1174 // A template instance is never a usual deallocation function,
1175 // regardless of its signature.
1176 if (getPrimaryTemplate())
1177 return false;
1178
Douglas Gregor90916562009-09-29 18:16:17 +00001179 // C++ [basic.stc.dynamic.deallocation]p2:
1180 // If a class T has a member deallocation function named operator delete
1181 // with exactly one parameter, then that function is a usual (non-placement)
1182 // deallocation function. [...]
1183 if (getNumParams() == 1)
1184 return true;
1185
1186 // C++ [basic.stc.dynamic.deallocation]p2:
1187 // [...] If class T does not declare such an operator delete but does
1188 // declare a member deallocation function named operator delete with
1189 // exactly two parameters, the second of which has type std::size_t (18.1),
1190 // then this function is a usual deallocation function.
1191 ASTContext &Context = getASTContext();
1192 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001193 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1194 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001195 return false;
1196
1197 // This function is a usual deallocation function if there are no
1198 // single-parameter deallocation functions of the same kind.
1199 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1200 R.first != R.second; ++R.first) {
1201 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1202 if (FD->getNumParams() == 1)
1203 return false;
1204 }
1205
1206 return true;
1207}
1208
Douglas Gregor06a9f362010-05-01 20:49:11 +00001209bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001210 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001211 // A user-declared copy assignment operator X::operator= is a non-static
1212 // non-template member function of class X with exactly one parameter of
1213 // type X, X&, const X&, volatile X& or const volatile X&.
1214 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1215 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001216 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001217 return false;
1218
1219 QualType ParamType = getParamDecl(0)->getType();
1220 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1221 ParamType = Ref->getPointeeType();
1222
1223 ASTContext &Context = getASTContext();
1224 QualType ClassType
1225 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1226 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1227}
1228
Sean Huntffe37fd2011-05-25 20:50:04 +00001229bool CXXMethodDecl::isMoveAssignmentOperator() const {
1230 // C++0x [class.copy]p19:
1231 // A user-declared move assignment operator X::operator= is a non-static
1232 // non-template member function of class X with exactly one parameter of type
1233 // X&&, const X&&, volatile X&&, or const volatile X&&.
1234 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1235 getPrimaryTemplate() || getDescribedFunctionTemplate())
1236 return false;
1237
1238 QualType ParamType = getParamDecl(0)->getType();
1239 if (!isa<RValueReferenceType>(ParamType))
1240 return false;
1241 ParamType = ParamType->getPointeeType();
1242
1243 ASTContext &Context = getASTContext();
1244 QualType ClassType
1245 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1246 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1247}
1248
Anders Carlsson05eb2442009-05-16 23:58:37 +00001249void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001250 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001251 assert(!MD->getParent()->isDependentContext() &&
1252 "Can't add an overridden method to a class template!");
1253
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001254 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001255}
1256
1257CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001258 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001259}
1260
1261CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001262 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001263}
1264
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001265unsigned CXXMethodDecl::size_overridden_methods() const {
1266 return getASTContext().overridden_methods_size(this);
1267}
1268
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001269QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001270 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1271 // If the member function is declared const, the type of this is const X*,
1272 // if the member function is declared volatile, the type of this is
1273 // volatile X*, and if the member function is declared const volatile,
1274 // the type of this is const volatile X*.
1275
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001276 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001277
John McCall3cb0ebd2010-03-10 03:28:59 +00001278 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001279 ClassTy = C.getQualifiedType(ClassTy,
1280 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001281 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001282}
1283
Eli Friedmand7d7f672009-12-06 20:50:05 +00001284bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001285 // If this function is a template instantiation, look at the template from
1286 // which it was instantiated.
1287 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1288 if (!CheckFn)
1289 CheckFn = this;
1290
Eli Friedmand7d7f672009-12-06 20:50:05 +00001291 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001292 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001293}
1294
Sean Huntcbb67482011-01-08 20:30:50 +00001295CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1296 TypeSourceInfo *TInfo, bool IsVirtual,
1297 SourceLocation L, Expr *Init,
1298 SourceLocation R,
1299 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001300 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001301 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1302 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001303{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001304}
1305
Sean Huntcbb67482011-01-08 20:30:50 +00001306CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1307 FieldDecl *Member,
1308 SourceLocation MemberLoc,
1309 SourceLocation L, Expr *Init,
1310 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001311 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001312 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1313 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1314{
1315}
1316
Sean Huntcbb67482011-01-08 20:30:50 +00001317CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1318 IndirectFieldDecl *Member,
1319 SourceLocation MemberLoc,
1320 SourceLocation L, Expr *Init,
1321 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001322 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001323 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001324 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001325{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001326}
1327
Sean Huntcbb67482011-01-08 20:30:50 +00001328CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Hunt41717662011-02-26 19:13:13 +00001329 SourceLocation D, SourceLocation L,
1330 CXXConstructorDecl *Target, Expr *Init,
1331 SourceLocation R)
1332 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1333 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1334 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1335{
1336}
1337
1338CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001339 FieldDecl *Member,
1340 SourceLocation MemberLoc,
1341 SourceLocation L, Expr *Init,
1342 SourceLocation R,
1343 VarDecl **Indices,
1344 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001345 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001346 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001347 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001348{
1349 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1350 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1351}
1352
Sean Huntcbb67482011-01-08 20:30:50 +00001353CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1354 FieldDecl *Member,
1355 SourceLocation MemberLoc,
1356 SourceLocation L, Expr *Init,
1357 SourceLocation R,
1358 VarDecl **Indices,
1359 unsigned NumIndices) {
1360 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001361 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001362 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001363 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1364 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001365}
1366
Sean Huntcbb67482011-01-08 20:30:50 +00001367TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001368 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001369 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001370 else
1371 return TypeLoc();
1372}
1373
Sean Huntcbb67482011-01-08 20:30:50 +00001374const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001375 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001376 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001377 else
1378 return 0;
1379}
1380
Sean Huntcbb67482011-01-08 20:30:50 +00001381SourceLocation CXXCtorInitializer::getSourceLocation() const {
Sean Hunt41717662011-02-26 19:13:13 +00001382 if (isAnyMemberInitializer() || isDelegatingInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001383 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001384
1385 if (isInClassMemberInitializer())
1386 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001387
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001388 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001389}
1390
Sean Huntcbb67482011-01-08 20:30:50 +00001391SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001392 if (isInClassMemberInitializer()) {
1393 FieldDecl *D = getAnyMember();
1394 if (Expr *I = D->getInClassInitializer())
1395 return I->getSourceRange();
1396 return SourceRange();
1397 }
1398
Douglas Gregor802ab452009-12-02 22:36:29 +00001399 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001400}
1401
Douglas Gregorb48fe382008-10-31 09:07:45 +00001402CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001403CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001404 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Sean Hunt5f802e52011-05-06 00:11:07 +00001405 QualType(), 0, false, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001406}
1407
1408CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001409CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001410 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001411 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001412 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001413 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001414 bool isInline,
Sean Hunt5f802e52011-05-06 00:11:07 +00001415 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001416 assert(NameInfo.getName().getNameKind()
1417 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001418 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001419 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Sean Hunt5f802e52011-05-06 00:11:07 +00001420 isExplicit, isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001421}
1422
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001423bool CXXConstructorDecl::isDefaultConstructor() const {
1424 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001425 // A default constructor for a class X is a constructor of class
1426 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001427 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001428 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001429}
1430
Mike Stump1eb44332009-09-09 15:08:12 +00001431bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001432CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001433 return isCopyOrMoveConstructor(TypeQuals) &&
1434 getParamDecl(0)->getType()->isLValueReferenceType();
1435}
1436
1437bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1438 return isCopyOrMoveConstructor(TypeQuals) &&
1439 getParamDecl(0)->getType()->isRValueReferenceType();
1440}
1441
1442/// \brief Determine whether this is a copy or move constructor.
1443bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001444 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001445 // A non-template constructor for class X is a copy constructor
1446 // if its first parameter is of type X&, const X&, volatile X& or
1447 // const volatile X&, and either there are no other parameters
1448 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001449 // C++0x [class.copy]p3:
1450 // A non-template constructor for class X is a move constructor if its
1451 // first parameter is of type X&&, const X&&, volatile X&&, or
1452 // const volatile X&&, and either there are no other parameters or else
1453 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001454 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001455 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001456 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001457 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001458 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001459
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001460 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001461
1462 // Do we have a reference type?
1463 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001464 if (!ParamRefType)
1465 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001466
Douglas Gregorfd476482009-11-13 23:59:09 +00001467 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001468 ASTContext &Context = getASTContext();
1469
Douglas Gregorfd476482009-11-13 23:59:09 +00001470 CanQualType PointeeType
1471 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001472 CanQualType ClassTy
1473 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001474 if (PointeeType.getUnqualifiedType() != ClassTy)
1475 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001476
John McCall0953e762009-09-24 19:53:00 +00001477 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001478
1479 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001480 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001481 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001482}
1483
Anders Carlssonfaccd722009-08-28 16:57:08 +00001484bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001485 // C++ [class.conv.ctor]p1:
1486 // A constructor declared without the function-specifier explicit
1487 // that can be called with a single parameter specifies a
1488 // conversion from the type of its first parameter to the type of
1489 // its class. Such a constructor is called a converting
1490 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001491 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001492 return false;
1493
Mike Stump1eb44332009-09-09 15:08:12 +00001494 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001495 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001496 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001497 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001498}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001499
Douglas Gregor6493cc52010-11-08 17:16:59 +00001500bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001501 if ((getNumParams() < 1) ||
1502 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1503 (getPrimaryTemplate() == 0) ||
1504 (getDescribedFunctionTemplate() != 0))
1505 return false;
1506
1507 const ParmVarDecl *Param = getParamDecl(0);
1508
1509 ASTContext &Context = getASTContext();
1510 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1511
Douglas Gregor66724ea2009-11-14 01:20:54 +00001512 // Is it the same as our our class type?
1513 CanQualType ClassTy
1514 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1515 if (ParamType.getUnqualifiedType() != ClassTy)
1516 return false;
1517
1518 return true;
1519}
1520
Sebastian Redlf677ea32011-02-05 19:23:19 +00001521const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1522 // Hack: we store the inherited constructor in the overridden method table
1523 method_iterator It = begin_overridden_methods();
1524 if (It == end_overridden_methods())
1525 return 0;
1526
1527 return cast<CXXConstructorDecl>(*It);
1528}
1529
1530void
1531CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1532 // Hack: we store the inherited constructor in the overridden method table
1533 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1534 addOverriddenMethod(BaseCtor);
1535}
1536
Douglas Gregor42a552f2008-11-05 20:51:48 +00001537CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001538CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001539 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001540 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001541}
1542
1543CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001544CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001545 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001546 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001547 QualType T, TypeSourceInfo *TInfo,
1548 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001549 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001550 assert(NameInfo.getName().getNameKind()
1551 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001552 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001553 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001554 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001555}
1556
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001557CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001558CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001559 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001560 QualType(), 0, false, false,
1561 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001562}
1563
1564CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001565CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001566 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001567 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001568 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001569 bool isInline, bool isExplicit,
1570 SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001571 assert(NameInfo.getName().getNameKind()
1572 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001573 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001574 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001575 isInline, isExplicit, EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001576}
1577
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001578LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001579 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001580 SourceLocation ExternLoc,
1581 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001582 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001583 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001584 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001585}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001586
1587UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1588 SourceLocation L,
1589 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001590 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001591 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001592 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001593 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001594 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1595 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001596 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1597 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001598}
1599
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001600NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1601 if (NamespaceAliasDecl *NA =
1602 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1603 return NA->getNamespace();
1604 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1605}
1606
Mike Stump1eb44332009-09-09 15:08:12 +00001607NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001608 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001609 SourceLocation AliasLoc,
1610 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001611 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001612 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001613 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001614 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1615 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001616 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1617 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001618}
1619
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001620UsingDecl *UsingShadowDecl::getUsingDecl() const {
1621 const UsingShadowDecl *Shadow = this;
1622 while (const UsingShadowDecl *NextShadow =
1623 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1624 Shadow = NextShadow;
1625 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1626}
1627
1628void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1629 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1630 "declaration already in set");
1631 assert(S->getUsingDecl() == this);
1632
1633 if (FirstUsingShadow)
1634 S->UsingOrNextShadow = FirstUsingShadow;
1635 FirstUsingShadow = S;
1636}
1637
1638void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1639 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1640 "declaration not in set");
1641 assert(S->getUsingDecl() == this);
1642
1643 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1644
1645 if (FirstUsingShadow == S) {
1646 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1647 S->UsingOrNextShadow = this;
1648 return;
1649 }
1650
1651 UsingShadowDecl *Prev = FirstUsingShadow;
1652 while (Prev->UsingOrNextShadow != S)
1653 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1654 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1655 S->UsingOrNextShadow = this;
1656}
1657
Douglas Gregordc355712011-02-25 00:36:19 +00001658UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1659 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001660 const DeclarationNameInfo &NameInfo,
1661 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001662 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001663}
1664
John McCall7ba107a2009-11-18 02:36:19 +00001665UnresolvedUsingValueDecl *
1666UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1667 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001668 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001669 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001670 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001671 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001672}
1673
1674UnresolvedUsingTypenameDecl *
1675UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1676 SourceLocation UsingLoc,
1677 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001678 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001679 SourceLocation TargetNameLoc,
1680 DeclarationName TargetName) {
1681 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001682 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001683 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001684}
1685
Anders Carlssonfb311762009-03-14 00:25:26 +00001686StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001687 SourceLocation StaticAssertLoc,
1688 Expr *AssertExpr,
1689 StringLiteral *Message,
1690 SourceLocation RParenLoc) {
1691 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1692 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001693}
1694
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001695static const char *getAccessName(AccessSpecifier AS) {
1696 switch (AS) {
1697 default:
1698 case AS_none:
1699 assert("Invalid access specifier!");
1700 return 0;
1701 case AS_public:
1702 return "public";
1703 case AS_private:
1704 return "private";
1705 case AS_protected:
1706 return "protected";
1707 }
1708}
1709
1710const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1711 AccessSpecifier AS) {
1712 return DB << getAccessName(AS);
1713}