blob: 518210a3f7f37bef709c8f955d53f01e82d131e6 [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000017#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000018#include "clang/AST/CXXInheritance.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000020#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000021#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000022#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000023#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// Decl Allocation/Deallocation Method Implementations
28//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000029
John McCall86ff3082010-02-04 22:26:26 +000030CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
31 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000032 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
33 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000034 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000035 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000036 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Douglas Gregor2bb11012011-05-13 01:05:07 +000037 HasMutableFields(false), HasTrivialDefaultConstructor(true),
Richard Smith6b8bc072011-08-10 18:11:37 +000038 HasConstexprNonCopyMoveConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000039 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
40 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
41 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000042 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000043 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
44 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
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 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000547 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
548 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000549 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000550 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000551 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000552
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000553 // C++ [dcl.init.aggr]p1:
554 // An aggregate is an array or a class with no user-declared
555 // constructors [...].
556 // C++0x [dcl.init.aggr]p1:
557 // An aggregate is an array or a class with no user-provided
558 // constructors [...].
559 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
560 data().Aggregate = false;
561
562 // C++ [class]p4:
563 // A POD-struct is an aggregate class [...]
564 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
565 // type is technically an aggregate in C++0x since it wouldn't be in 03.
566 data().PlainOldData = false;
567
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000568 return;
569 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000570
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000571 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000572 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000573 data().DeclaredDestructor = true;
574 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000575
576 // C++ [class]p4:
577 // A POD-struct is an aggregate class that has [...] no user-defined
578 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000579 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000580 data().PlainOldData = false;
581
Sean Huntcf34e752011-05-16 22:41:40 +0000582 // C++0x [class.dtor]p5:
583 // A destructor is trivial if it is not user-provided and [...]
584 if (DD->isUserProvided())
585 data().HasTrivialDestructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000586
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000587 return;
588 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000589
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000590 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000591 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000592 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000593 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000594 // A POD-struct is an aggregate class that [...] has no user-defined
595 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000596 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000597 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000598
Sean Huntffe37fd2011-05-25 20:50:04 +0000599 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000600
Sean Huntffe37fd2011-05-25 20:50:04 +0000601 // Suppress the implicit declaration of a copy constructor.
602 data().UserDeclaredCopyAssignment = true;
603 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000604
Sean Huntffe37fd2011-05-25 20:50:04 +0000605 // C++0x [class.copy]p27:
606 // A copy/move assignment operator for class X is trivial if it is
607 // neither user-provided nor deleted [...]
608 if (Method->isUserProvided())
609 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000610
Sean Huntffe37fd2011-05-25 20:50:04 +0000611 return;
612 }
613
614 if (Method->isMoveAssignmentOperator()) {
615 // This is an extension in C++03 mode, but we'll keep consistency by
616 // taking a move assignment operator to induce non-POD-ness
617 data().PlainOldData = false;
618
619 // This is a move assignment operator.
620 data().UserDeclaredMoveAssignment = true;
621 data().DeclaredMoveAssignment = true;
622
623 // C++0x [class.copy]p27:
624 // A copy/move assignment operator for class X is trivial if it is
625 // neither user-provided nor deleted [...]
626 if (Method->isUserProvided())
627 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000628 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000629
Douglas Gregore80622f2010-09-29 04:25:11 +0000630 // Keep the list of conversion functions up-to-date.
631 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
632 // We don't record specializations.
633 if (Conversion->getPrimaryTemplate())
634 return;
635
636 // FIXME: We intentionally don't use the decl's access here because it
637 // hasn't been set yet. That's really just a misdesign in Sema.
638
639 if (FunTmpl) {
640 if (FunTmpl->getPreviousDeclaration())
641 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
642 FunTmpl);
643 else
644 data().Conversions.addDecl(FunTmpl);
645 } else {
646 if (Conversion->getPreviousDeclaration())
647 data().Conversions.replace(Conversion->getPreviousDeclaration(),
648 Conversion);
649 else
650 data().Conversions.addDecl(Conversion);
651 }
652 }
653
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000654 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000655 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000656
657 // Handle non-static data members.
658 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
659 // C++ [dcl.init.aggr]p1:
660 // An aggregate is an array or a class (clause 9) with [...] no
661 // private or protected non-static data members (clause 11).
662 //
663 // A POD must be an aggregate.
664 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
665 data().Aggregate = false;
666 data().PlainOldData = false;
667 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000668
669 // C++0x [class]p7:
670 // A standard-layout class is a class that:
671 // [...]
672 // -- has the same access control for all non-static data members,
673 switch (D->getAccess()) {
674 case AS_private: data().HasPrivateFields = true; break;
675 case AS_protected: data().HasProtectedFields = true; break;
676 case AS_public: data().HasPublicFields = true; break;
677 case AS_none: assert(0 && "Invalid access specifier");
678 };
679 if ((data().HasPrivateFields + data().HasProtectedFields +
680 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000681 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000682
Douglas Gregor2bb11012011-05-13 01:05:07 +0000683 // Keep track of the presence of mutable fields.
684 if (Field->isMutable())
685 data().HasMutableFields = true;
686
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000687 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000688 // A POD struct is a class that is both a trivial class and a
689 // standard-layout class, and has no non-static data members of type
690 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000691 //
692 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
693 // that does not explicitly have no lifetime makes the class a non-POD.
694 // However, we delay setting PlainOldData to false in this case so that
695 // Sema has a chance to diagnostic causes where the same class will be
696 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
697 // In this case, the class will become a non-POD class when we complete
698 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000699 ASTContext &Context = getASTContext();
700 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000701 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
702 if (!Context.getLangOptions().ObjCAutoRefCount ||
703 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
704 setHasObjectMember(true);
705 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000706 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000707
Chandler Carrutha8225442011-04-30 09:17:45 +0000708 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000709 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000710
Chandler Carrutha8225442011-04-30 09:17:45 +0000711 // C++0x [class]p7:
712 // A standard-layout class is a class that:
713 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000714 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000715 }
716
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000717 // Record if this field is the first non-literal field or base.
718 if (!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType())
719 data().HasNonLiteralTypeFieldsOrBases = true;
720
Richard Smith7a614d82011-06-11 17:19:42 +0000721 if (Field->hasInClassInitializer()) {
722 // C++0x [class]p5:
723 // A default constructor is trivial if [...] no non-static data member
724 // of its class has a brace-or-equal-initializer.
725 data().HasTrivialDefaultConstructor = false;
726
727 // C++0x [dcl.init.aggr]p1:
728 // An aggregate is a [...] class with [...] no
729 // brace-or-equal-initializers for non-static data members.
730 data().Aggregate = false;
731
732 // C++0x [class]p10:
733 // A POD struct is [...] a trivial class.
734 data().PlainOldData = false;
735 }
736
Douglas Gregor85606eb2010-09-28 20:50:54 +0000737 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
738 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
739 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000740 // C++0x [class.ctor]p5:
741 // A defulat constructor is trivial [...] if:
742 // -- for all the non-static data members of its class that are of
743 // class type (or array thereof), each such class has a trivial
744 // default constructor.
745 if (!FieldRec->hasTrivialDefaultConstructor())
746 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000747
748 // C++0x [class.copy]p13:
749 // A copy/move constructor for class X is trivial if [...]
750 // [...]
751 // -- for each non-static data member of X that is of class type (or
752 // an array thereof), the constructor selected to copy/move that
753 // member is trivial;
754 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000755 if (!FieldRec->hasTrivialCopyConstructor())
756 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000757 if (!FieldRec->hasTrivialMoveConstructor())
758 data().HasTrivialMoveConstructor = false;
759
760 // C++0x [class.copy]p27:
761 // A copy/move assignment operator for class X is trivial if [...]
762 // [...]
763 // -- for each non-static data member of X that is of class type (or
764 // an array thereof), the assignment operator selected to
765 // copy/move that member is trivial;
766 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000767 if (!FieldRec->hasTrivialCopyAssignment())
768 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000769 if (!FieldRec->hasTrivialMoveAssignment())
770 data().HasTrivialMoveAssignment = false;
771
Douglas Gregor85606eb2010-09-28 20:50:54 +0000772 if (!FieldRec->hasTrivialDestructor())
773 data().HasTrivialDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000774 if (FieldRec->hasObjectMember())
775 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000776
777 // C++0x [class]p7:
778 // A standard-layout class is a class that:
779 // -- has no non-static data members of type non-standard-layout
780 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000781 if (!FieldRec->isStandardLayout())
782 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000783
784 // C++0x [class]p7:
785 // A standard-layout class is a class that:
786 // [...]
787 // -- has no base classes of the same type as the first non-static
788 // data member.
789 // We don't want to expend bits in the state of the record decl
790 // tracking whether this is the first non-static data member so we
791 // cheat a bit and use some of the existing state: the empty bit.
792 // Virtual bases and virtual methods make a class non-empty, but they
793 // also make it non-standard-layout so we needn't check here.
794 // A non-empty base class may leave the class standard-layout, but not
795 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000796 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000797 // data member must come through here with Empty still true, and Empty
798 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000799 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000800 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
801 BE = bases_end();
802 BI != BE; ++BI) {
803 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000804 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000805 break;
806 }
807 }
808 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000809
810 // Keep track of the presence of mutable fields.
811 if (FieldRec->hasMutableFields())
812 data().HasMutableFields = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000813 }
814 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000815
816 // C++0x [class]p7:
817 // A standard-layout class is a class that:
818 // [...]
819 // -- either has no non-static data members in the most derived
820 // class and at most one base class with non-static data members,
821 // or has no base classes with non-static data members, and
822 // At this point we know that we have a non-static data member, so the last
823 // clause holds.
824 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000825 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000826
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000827 // If this is not a zero-length bit-field, then the class is not empty.
828 if (data().Empty) {
829 if (!Field->getBitWidth())
830 data().Empty = false;
831 else if (!Field->getBitWidth()->isTypeDependent() &&
832 !Field->getBitWidth()->isValueDependent()) {
833 llvm::APSInt Bits;
834 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
835 if (!!Bits)
836 data().Empty = false;
837 }
838 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000839 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000840
841 // Handle using declarations of conversion functions.
842 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
843 if (Shadow->getDeclName().getNameKind()
844 == DeclarationName::CXXConversionFunctionName)
845 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000846}
847
John McCallb05b5f32010-03-15 09:07:48 +0000848static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
849 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000850 if (isa<UsingShadowDecl>(Conv))
851 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000852 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
853 T = ConvTemp->getTemplatedDecl()->getResultType();
854 else
855 T = cast<CXXConversionDecl>(Conv)->getConversionType();
856 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000857}
858
John McCallb05b5f32010-03-15 09:07:48 +0000859/// Collect the visible conversions of a base class.
860///
861/// \param Base a base class of the class we're considering
862/// \param InVirtual whether this base class is a virtual base (or a base
863/// of a virtual base)
864/// \param Access the access along the inheritance path to this base
865/// \param ParentHiddenTypes the conversions provided by the inheritors
866/// of this base
867/// \param Output the set to which to add conversions from non-virtual bases
868/// \param VOutput the set to which to add conversions from virtual bases
869/// \param HiddenVBaseCs the set of conversions which were hidden in a
870/// virtual base along some inheritance path
871static void CollectVisibleConversions(ASTContext &Context,
872 CXXRecordDecl *Record,
873 bool InVirtual,
874 AccessSpecifier Access,
875 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
876 UnresolvedSetImpl &Output,
877 UnresolvedSetImpl &VOutput,
878 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
879 // The set of types which have conversions in this class or its
880 // subclasses. As an optimization, we don't copy the derived set
881 // unless it might change.
882 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
883 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
884
885 // Collect the direct conversions and figure out which conversions
886 // will be hidden in the subclasses.
887 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
888 if (!Cs.empty()) {
889 HiddenTypesBuffer = ParentHiddenTypes;
890 HiddenTypes = &HiddenTypesBuffer;
891
892 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
893 bool Hidden =
894 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
895
896 // If this conversion is hidden and we're in a virtual base,
897 // remember that it's hidden along some inheritance path.
898 if (Hidden && InVirtual)
899 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
900
901 // If this conversion isn't hidden, add it to the appropriate output.
902 else if (!Hidden) {
903 AccessSpecifier IAccess
904 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
905
906 if (InVirtual)
907 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000908 else
John McCallb05b5f32010-03-15 09:07:48 +0000909 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000910 }
911 }
912 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000913
John McCallb05b5f32010-03-15 09:07:48 +0000914 // Collect information recursively from any base classes.
915 for (CXXRecordDecl::base_class_iterator
916 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
917 const RecordType *RT = I->getType()->getAs<RecordType>();
918 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000919
John McCallb05b5f32010-03-15 09:07:48 +0000920 AccessSpecifier BaseAccess
921 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
922 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000923
John McCallb05b5f32010-03-15 09:07:48 +0000924 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
925 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
926 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000927 }
John McCallb05b5f32010-03-15 09:07:48 +0000928}
Sebastian Redl9994a342009-10-25 17:03:50 +0000929
John McCallb05b5f32010-03-15 09:07:48 +0000930/// Collect the visible conversions of a class.
931///
932/// This would be extremely straightforward if it weren't for virtual
933/// bases. It might be worth special-casing that, really.
934static void CollectVisibleConversions(ASTContext &Context,
935 CXXRecordDecl *Record,
936 UnresolvedSetImpl &Output) {
937 // The collection of all conversions in virtual bases that we've
938 // found. These will be added to the output as long as they don't
939 // appear in the hidden-conversions set.
940 UnresolvedSet<8> VBaseCs;
941
942 // The set of conversions in virtual bases that we've determined to
943 // be hidden.
944 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
945
946 // The set of types hidden by classes derived from this one.
947 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
948
949 // Go ahead and collect the direct conversions and add them to the
950 // hidden-types set.
951 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
952 Output.append(Cs.begin(), Cs.end());
953 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
954 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
955
956 // Recursively collect conversions from base classes.
957 for (CXXRecordDecl::base_class_iterator
958 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
959 const RecordType *RT = I->getType()->getAs<RecordType>();
960 if (!RT) continue;
961
962 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
963 I->isVirtual(), I->getAccessSpecifier(),
964 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
965 }
966
967 // Add any unhidden conversions provided by virtual bases.
968 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
969 I != E; ++I) {
970 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
971 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000972 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000973}
974
975/// getVisibleConversionFunctions - get all conversion functions visible
976/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000977const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000978 // If root class, all conversions are visible.
979 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000980 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000981 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000982 if (data().ComputedVisibleConversions)
983 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000984 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000985 data().ComputedVisibleConversions = true;
986 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000987}
988
John McCall32daa422010-03-31 01:36:47 +0000989void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
990 // This operation is O(N) but extremely rare. Sema only uses it to
991 // remove UsingShadowDecls in a class that were followed by a direct
992 // declaration, e.g.:
993 // class A : B {
994 // using B::operator int;
995 // operator int();
996 // };
997 // This is uncommon by itself and even more uncommon in conjunction
998 // with sufficiently large numbers of directly-declared conversions
999 // that asymptotic behavior matters.
1000
1001 UnresolvedSetImpl &Convs = *getConversionFunctions();
1002 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1003 if (Convs[I].getDecl() == ConvDecl) {
1004 Convs.erase(I);
1005 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1006 && "conversion was found multiple times in unresolved set");
1007 return;
1008 }
1009 }
1010
1011 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001012}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001013
Douglas Gregorf6b11852009-10-08 15:14:33 +00001014CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001015 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001016 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1017
1018 return 0;
1019}
1020
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001021MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1022 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1023}
1024
Douglas Gregorf6b11852009-10-08 15:14:33 +00001025void
1026CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1027 TemplateSpecializationKind TSK) {
1028 assert(TemplateOrInstantiation.isNull() &&
1029 "Previous template or instantiation?");
1030 assert(!isa<ClassTemplateSpecializationDecl>(this));
1031 TemplateOrInstantiation
1032 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1033}
1034
Anders Carlssonb13e3572009-12-07 06:33:48 +00001035TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1036 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001037 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1038 return Spec->getSpecializationKind();
1039
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001040 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001041 return MSInfo->getTemplateSpecializationKind();
1042
1043 return TSK_Undeclared;
1044}
1045
1046void
1047CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1048 if (ClassTemplateSpecializationDecl *Spec
1049 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1050 Spec->setSpecializationKind(TSK);
1051 return;
1052 }
1053
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001054 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001055 MSInfo->setTemplateSpecializationKind(TSK);
1056 return;
1057 }
1058
1059 assert(false && "Not a class template or member class specialization");
1060}
1061
Douglas Gregor1d110e02010-07-01 14:13:13 +00001062CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1063 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001064 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001065
1066 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001067 = Context.DeclarationNames.getCXXDestructorName(
1068 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001069
John McCallc0bf4622010-02-23 00:48:20 +00001070 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001071 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001072 if (I == E)
1073 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001075 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001076 return Dtor;
1077}
1078
Douglas Gregorda2142f2011-02-19 18:51:44 +00001079void CXXRecordDecl::completeDefinition() {
1080 completeDefinition(0);
1081}
1082
1083void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1084 RecordDecl::completeDefinition();
1085
John McCallf85e1932011-06-15 23:02:42 +00001086 if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) {
1087 // Objective-C Automatic Reference Counting:
1088 // If a class has a non-static data member of Objective-C pointer
1089 // type (or array thereof), it is a non-POD type and its
1090 // default constructor (if any), copy constructor, copy assignment
1091 // operator, and destructor are non-trivial.
1092 struct DefinitionData &Data = data();
1093 Data.PlainOldData = false;
1094 Data.HasTrivialDefaultConstructor = false;
1095 Data.HasTrivialCopyConstructor = false;
1096 Data.HasTrivialCopyAssignment = false;
1097 Data.HasTrivialDestructor = false;
1098 }
1099
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001100 // If the class may be abstract (but hasn't been marked as such), check for
1101 // any pure final overriders.
1102 if (mayBeAbstract()) {
1103 CXXFinalOverriderMap MyFinalOverriders;
1104 if (!FinalOverriders) {
1105 getFinalOverriders(MyFinalOverriders);
1106 FinalOverriders = &MyFinalOverriders;
1107 }
1108
1109 bool Done = false;
1110 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1111 MEnd = FinalOverriders->end();
1112 M != MEnd && !Done; ++M) {
1113 for (OverridingMethods::iterator SO = M->second.begin(),
1114 SOEnd = M->second.end();
1115 SO != SOEnd && !Done; ++SO) {
1116 assert(SO->second.size() > 0 &&
1117 "All virtual functions have overridding virtual functions");
1118
1119 // C++ [class.abstract]p4:
1120 // A class is abstract if it contains or inherits at least one
1121 // pure virtual function for which the final overrider is pure
1122 // virtual.
1123 if (SO->second.front().Method->isPure()) {
1124 data().Abstract = true;
1125 Done = true;
1126 break;
1127 }
1128 }
1129 }
1130 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001131
1132 // Set access bits correctly on the directly-declared conversions.
1133 for (UnresolvedSetIterator I = data().Conversions.begin(),
1134 E = data().Conversions.end();
1135 I != E; ++I)
1136 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001137}
1138
1139bool CXXRecordDecl::mayBeAbstract() const {
1140 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1141 isDependentContext())
1142 return false;
1143
1144 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1145 BEnd = bases_end();
1146 B != BEnd; ++B) {
1147 CXXRecordDecl *BaseDecl
1148 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1149 if (BaseDecl->isAbstract())
1150 return true;
1151 }
1152
1153 return false;
1154}
1155
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001156CXXMethodDecl *
1157CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001158 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001159 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001160 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001161 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001162 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001163 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001164 isStatic, SCAsWritten, isInline, isConstexpr,
1165 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(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001405 QualType(), 0, false, 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,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001413 bool isExplicit, bool isInline,
1414 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001415 assert(NameInfo.getName().getNameKind()
1416 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001417 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001418 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001419 isExplicit, isInline, isImplicitlyDeclared,
1420 isConstexpr);
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,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001548 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001549 assert(NameInfo.getName().getNameKind()
1550 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001551 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001552 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001553 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001554}
1555
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001556CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001557CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001558 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001559 QualType(), 0, false, false, false,
Douglas Gregorf5251602011-03-08 17:10:18 +00001560 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001561}
1562
1563CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001564CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001565 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001566 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001567 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001568 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001569 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001570 assert(NameInfo.getName().getNameKind()
1571 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001572 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001573 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001574 isInline, isExplicit, isConstexpr,
1575 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}