blob: c10e6c40d39f7eee394bf228f9b17624ba925b6c [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.
92 llvm::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();
247 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000248 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
249
Anders Carlsson6f6de732010-03-29 05:13:12 +0000250 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000251 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000252 continue;
253
Nick Lewycky56062202010-07-26 16:56:01 +0000254 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
255 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000256
Douglas Gregor7c789c12010-10-29 22:39:52 +0000257 data().getVBases()[I] =
Anders Carlsson6f6de732010-03-29 05:13:12 +0000258 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000259 VBaseClassDecl->getTagKind() == TTK_Class,
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000260 VBases[I]->getAccessSpecifier(), VBaseTypeInfo,
261 SourceLocation());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000262 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000263}
264
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000265/// Callback function for CXXRecordDecl::forallBases that acknowledges
266/// that it saw a base class.
267static bool SawBase(const CXXRecordDecl *, void *) {
268 return true;
269}
270
271bool CXXRecordDecl::hasAnyDependentBases() const {
272 if (!isDependentContext())
273 return false;
274
275 return !forallBases(SawBase, 0);
276}
277
Sean Huntffe37fd2011-05-25 20:50:04 +0000278bool CXXRecordDecl::hasConstCopyConstructor() const {
279 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000280}
281
Chandler Carruthb7e95892011-04-23 10:47:28 +0000282bool CXXRecordDecl::isTriviallyCopyable() const {
283 // C++0x [class]p5:
284 // A trivially copyable class is a class that:
285 // -- has no non-trivial copy constructors,
286 if (!hasTrivialCopyConstructor()) return false;
287 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000288 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000289 // -- has no non-trivial copy assignment operators,
290 if (!hasTrivialCopyAssignment()) return false;
291 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000292 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000293 // -- has a trivial destructor.
294 if (!hasTrivialDestructor()) return false;
295
296 return true;
297}
298
Douglas Gregor0d405db2010-07-01 20:59:04 +0000299/// \brief Perform a simplistic form of overload resolution that only considers
300/// cv-qualifiers on a single parameter, and return the best overload candidate
301/// (if there is one).
302static CXXMethodDecl *
303GetBestOverloadCandidateSimple(
304 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
305 if (Cands.empty())
306 return 0;
307 if (Cands.size() == 1)
308 return Cands[0].first;
309
310 unsigned Best = 0, N = Cands.size();
311 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000312 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000313 Best = I;
314
315 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000316 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000317 return 0;
318
319 return Cands[Best].first;
320}
321
Sean Huntffe37fd2011-05-25 20:50:04 +0000322CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
323 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000324 QualType ClassType
325 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000326 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000327 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000328 Context.getCanonicalType(ClassType));
329 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000330 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000331 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000332 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000333 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000334 // C++ [class.copy]p2:
335 // A non-template constructor for class X is a copy constructor if [...]
336 if (isa<FunctionTemplateDecl>(*Con))
337 continue;
338
Douglas Gregor0d405db2010-07-01 20:59:04 +0000339 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
340 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000341 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
342 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000343 Found.push_back(std::make_pair(
344 const_cast<CXXConstructorDecl *>(Constructor),
345 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000346 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000347 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000348
349 return cast_or_null<CXXConstructorDecl>(
350 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000351}
352
Sean Huntffe37fd2011-05-25 20:50:04 +0000353CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
354 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
355 if (I->isMoveConstructor())
356 return *I;
357
358 return 0;
359}
360
Douglas Gregorb87786f2010-07-01 17:48:08 +0000361CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
362 ASTContext &Context = getASTContext();
363 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
364 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
365
366 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
367 DeclContext::lookup_const_iterator Op, OpEnd;
368 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
369 // C++ [class.copy]p9:
370 // A user-declared copy assignment operator is a non-static non-template
371 // member function of class X with exactly one parameter of type X, X&,
372 // const X&, volatile X& or const volatile X&.
373 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
374 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
375 continue;
376
377 const FunctionProtoType *FnType
378 = Method->getType()->getAs<FunctionProtoType>();
379 assert(FnType && "Overloaded operator has no prototype.");
380 // Don't assert on this; an invalid decl might have been left in the AST.
381 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
382 continue;
383
384 QualType ArgType = FnType->getArgType(0);
385 Qualifiers Quals;
386 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
387 ArgType = Ref->getPointeeType();
388 // If we have a const argument and we have a reference to a non-const,
389 // this function does not match.
390 if (ArgIsConst && !ArgType.isConstQualified())
391 continue;
392
393 Quals = ArgType.getQualifiers();
394 } else {
395 // By-value copy-assignment operators are treated like const X&
396 // copy-assignment operators.
397 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
398 }
399
400 if (!Context.hasSameUnqualifiedType(ArgType, Class))
401 continue;
402
403 // Save this copy-assignment operator. It might be "the one".
404 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
405 }
406
407 // Use a simplistic form of overload resolution to find the candidate.
408 return GetBestOverloadCandidateSimple(Found);
409}
410
Sean Huntffe37fd2011-05-25 20:50:04 +0000411CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
412 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
413 if (I->isMoveAssignmentOperator())
414 return *I;
415
416 return 0;
417}
418
Douglas Gregor21386642010-09-28 21:55:22 +0000419void CXXRecordDecl::markedVirtualFunctionPure() {
420 // C++ [class.abstract]p2:
421 // A class is abstract if it has at least one pure virtual function.
422 data().Abstract = true;
423}
424
425void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000426 // Ignore friends and invalid declarations.
427 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000428 return;
429
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000430 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
431 if (FunTmpl)
432 D = FunTmpl->getTemplatedDecl();
433
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000434 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
435 if (Method->isVirtual()) {
436 // C++ [dcl.init.aggr]p1:
437 // An aggregate is an array or a class with [...] no virtual functions.
438 data().Aggregate = false;
439
440 // C++ [class]p4:
441 // A POD-struct is an aggregate class...
442 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000443
444 // Virtual functions make the class non-empty.
445 // FIXME: Standard ref?
446 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000447
448 // C++ [class.virtual]p1:
449 // A class that declares or inherits a virtual function is called a
450 // polymorphic class.
451 data().Polymorphic = true;
452
Sean Hunt023df372011-05-09 18:22:59 +0000453 // C++0x [class.ctor]p5
454 // A default constructor is trivial [...] if:
455 // -- its class has no virtual functions [...]
456 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000457
458 // C++0x [class.copy]p13:
459 // A copy/move constructor for class X is trivial if [...]
460 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000461 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000462 data().HasTrivialMoveConstructor = false;
463
464 // C++0x [class.copy]p27:
465 // A copy/move assignment operator for class X is trivial if [...]
466 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000467 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000468 data().HasTrivialMoveAssignment = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000469 // FIXME: Destructor?
Chandler Carrutha8225442011-04-30 09:17:45 +0000470
471 // C++0x [class]p7:
472 // A standard-layout class is a class that: [...]
473 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000474 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000475 }
476 }
477
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000478 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000479 // Notify that an implicit member was added after the definition
480 // was completed.
481 if (!isBeingDefined())
482 if (ASTMutationListener *L = getASTMutationListener())
483 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000484
Sean Huntffe37fd2011-05-25 20:50:04 +0000485 // If this is a special member function, note that it was added and then
486 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000487 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000488 if (Constructor->isDefaultConstructor())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000489 data().DeclaredDefaultConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000490 else if (Constructor->isCopyConstructor())
491 data().DeclaredCopyConstructor = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000492 else if (Constructor->isMoveConstructor())
493 data().DeclaredMoveConstructor = true;
494 else
495 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000496 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000497 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000498 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000499 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000500 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
501 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000502 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000503 else if (Method->isMoveAssignmentOperator())
504 data().DeclaredMoveAssignment = true;
505 else
506 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000507 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000508 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000509
Sean Huntffe37fd2011-05-25 20:50:04 +0000510NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000511 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000512 }
513
514 // Handle (user-declared) constructors.
515 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
516 // Note that we have a user-declared constructor.
517 data().UserDeclaredConstructor = true;
518
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000519 // FIXME: Under C++0x, /only/ special member functions may be user-provided.
520 // This is probably a defect.
521 bool UserProvided = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000522
Sean Hunt023df372011-05-09 18:22:59 +0000523 // C++0x [class.ctor]p5:
524 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000525 if (Constructor->isDefaultConstructor()) {
526 data().DeclaredDefaultConstructor = true;
527 if (Constructor->isUserProvided()) {
528 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000529 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000530 UserProvided = true;
531 }
532 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000533
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000534 // Note when we have a user-declared copy or move constructor, which will
535 // suppress the implicit declaration of those constructors.
536 if (!FunTmpl) {
537 if (Constructor->isCopyConstructor()) {
538 data().UserDeclaredCopyConstructor = true;
539 data().DeclaredCopyConstructor = true;
540
541 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000542 // A copy/move constructor for class X is trivial if it is not
543 // user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000544 if (Constructor->isUserProvided()) {
Sean Hunt023df372011-05-09 18:22:59 +0000545 data().HasTrivialCopyConstructor = false;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000546 UserProvided = true;
547 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000548 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000549 data().UserDeclaredMoveConstructor = true;
550 data().DeclaredMoveConstructor = true;
551
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000552 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000553 // A copy/move constructor for class X is trivial if it is not
554 // user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000555 if (Constructor->isUserProvided()) {
Sean Hunt023df372011-05-09 18:22:59 +0000556 data().HasTrivialMoveConstructor = false;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000557 UserProvided = true;
558 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000559 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000560 }
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000561 if (Constructor->isConstExpr() &&
562 !Constructor->isCopyOrMoveConstructor()) {
563 // Record if we see any constexpr constructors which are niether copy
564 // nor move constructors.
565 data().HasConstExprNonCopyMoveConstructor = true;
566 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000567
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000568 // C++ [dcl.init.aggr]p1:
569 // An aggregate is an array or a class with no user-declared
570 // constructors [...].
571 // C++0x [dcl.init.aggr]p1:
572 // An aggregate is an array or a class with no user-provided
573 // constructors [...].
574 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
575 data().Aggregate = false;
576
577 // C++ [class]p4:
578 // A POD-struct is an aggregate class [...]
579 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
580 // type is technically an aggregate in C++0x since it wouldn't be in 03.
581 data().PlainOldData = false;
582
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000583 return;
584 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000585
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000586 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000587 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000588 data().DeclaredDestructor = true;
589 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000590
591 // C++ [class]p4:
592 // A POD-struct is an aggregate class that has [...] no user-defined
593 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000594 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000595 data().PlainOldData = false;
596
Sean Huntcf34e752011-05-16 22:41:40 +0000597 // C++0x [class.dtor]p5:
598 // A destructor is trivial if it is not user-provided and [...]
599 if (DD->isUserProvided())
600 data().HasTrivialDestructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000601
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000602 return;
603 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000604
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000605 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000606 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000607 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000608 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000609 // A POD-struct is an aggregate class that [...] has no user-defined
610 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000611 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000612 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000613
Sean Huntffe37fd2011-05-25 20:50:04 +0000614 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000615
Sean Huntffe37fd2011-05-25 20:50:04 +0000616 // Suppress the implicit declaration of a copy constructor.
617 data().UserDeclaredCopyAssignment = true;
618 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000619
Sean Huntffe37fd2011-05-25 20:50:04 +0000620 // C++0x [class.copy]p27:
621 // A copy/move assignment operator for class X is trivial if it is
622 // neither user-provided nor deleted [...]
623 if (Method->isUserProvided())
624 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000625
Sean Huntffe37fd2011-05-25 20:50:04 +0000626 return;
627 }
628
629 if (Method->isMoveAssignmentOperator()) {
630 // This is an extension in C++03 mode, but we'll keep consistency by
631 // taking a move assignment operator to induce non-POD-ness
632 data().PlainOldData = false;
633
634 // This is a move assignment operator.
635 data().UserDeclaredMoveAssignment = true;
636 data().DeclaredMoveAssignment = true;
637
638 // C++0x [class.copy]p27:
639 // A copy/move assignment operator for class X is trivial if it is
640 // neither user-provided nor deleted [...]
641 if (Method->isUserProvided())
642 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000643 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000644
Douglas Gregore80622f2010-09-29 04:25:11 +0000645 // Keep the list of conversion functions up-to-date.
646 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
647 // We don't record specializations.
648 if (Conversion->getPrimaryTemplate())
649 return;
650
651 // FIXME: We intentionally don't use the decl's access here because it
652 // hasn't been set yet. That's really just a misdesign in Sema.
653
654 if (FunTmpl) {
655 if (FunTmpl->getPreviousDeclaration())
656 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
657 FunTmpl);
658 else
659 data().Conversions.addDecl(FunTmpl);
660 } else {
661 if (Conversion->getPreviousDeclaration())
662 data().Conversions.replace(Conversion->getPreviousDeclaration(),
663 Conversion);
664 else
665 data().Conversions.addDecl(Conversion);
666 }
667 }
668
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000669 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000670 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000671
672 // Handle non-static data members.
673 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
674 // C++ [dcl.init.aggr]p1:
675 // An aggregate is an array or a class (clause 9) with [...] no
676 // private or protected non-static data members (clause 11).
677 //
678 // A POD must be an aggregate.
679 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
680 data().Aggregate = false;
681 data().PlainOldData = false;
682 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000683
684 // C++0x [class]p7:
685 // A standard-layout class is a class that:
686 // [...]
687 // -- has the same access control for all non-static data members,
688 switch (D->getAccess()) {
689 case AS_private: data().HasPrivateFields = true; break;
690 case AS_protected: data().HasProtectedFields = true; break;
691 case AS_public: data().HasPublicFields = true; break;
692 case AS_none: assert(0 && "Invalid access specifier");
693 };
694 if ((data().HasPrivateFields + data().HasProtectedFields +
695 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000696 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000697
Douglas Gregor2bb11012011-05-13 01:05:07 +0000698 // Keep track of the presence of mutable fields.
699 if (Field->isMutable())
700 data().HasMutableFields = true;
701
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000702 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000703 // A POD struct is a class that is both a trivial class and a
704 // standard-layout class, and has no non-static data members of type
705 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000706 //
707 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
708 // that does not explicitly have no lifetime makes the class a non-POD.
709 // However, we delay setting PlainOldData to false in this case so that
710 // Sema has a chance to diagnostic causes where the same class will be
711 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
712 // In this case, the class will become a non-POD class when we complete
713 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000714 ASTContext &Context = getASTContext();
715 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000716 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
717 if (!Context.getLangOptions().ObjCAutoRefCount ||
718 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
719 setHasObjectMember(true);
720 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000721 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000722
Chandler Carrutha8225442011-04-30 09:17:45 +0000723 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000724 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000725
Chandler Carrutha8225442011-04-30 09:17:45 +0000726 // C++0x [class]p7:
727 // A standard-layout class is a class that:
728 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000729 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000730 }
731
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000732 // Record if this field is the first non-literal field or base.
733 if (!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType())
734 data().HasNonLiteralTypeFieldsOrBases = true;
735
Richard Smith7a614d82011-06-11 17:19:42 +0000736 if (Field->hasInClassInitializer()) {
737 // C++0x [class]p5:
738 // A default constructor is trivial if [...] no non-static data member
739 // of its class has a brace-or-equal-initializer.
740 data().HasTrivialDefaultConstructor = false;
741
742 // C++0x [dcl.init.aggr]p1:
743 // An aggregate is a [...] class with [...] no
744 // brace-or-equal-initializers for non-static data members.
745 data().Aggregate = false;
746
747 // C++0x [class]p10:
748 // A POD struct is [...] a trivial class.
749 data().PlainOldData = false;
750 }
751
Douglas Gregor85606eb2010-09-28 20:50:54 +0000752 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
753 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
754 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000755 // C++0x [class.ctor]p5:
756 // A defulat constructor is trivial [...] if:
757 // -- for all the non-static data members of its class that are of
758 // class type (or array thereof), each such class has a trivial
759 // default constructor.
760 if (!FieldRec->hasTrivialDefaultConstructor())
761 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000762
763 // C++0x [class.copy]p13:
764 // A copy/move constructor for class X is trivial if [...]
765 // [...]
766 // -- for each non-static data member of X that is of class type (or
767 // an array thereof), the constructor selected to copy/move that
768 // member is trivial;
769 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000770 if (!FieldRec->hasTrivialCopyConstructor())
771 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000772 if (!FieldRec->hasTrivialMoveConstructor())
773 data().HasTrivialMoveConstructor = false;
774
775 // C++0x [class.copy]p27:
776 // A copy/move assignment operator for class X is trivial if [...]
777 // [...]
778 // -- for each non-static data member of X that is of class type (or
779 // an array thereof), the assignment operator selected to
780 // copy/move that member is trivial;
781 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000782 if (!FieldRec->hasTrivialCopyAssignment())
783 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000784 if (!FieldRec->hasTrivialMoveAssignment())
785 data().HasTrivialMoveAssignment = false;
786
Douglas Gregor85606eb2010-09-28 20:50:54 +0000787 if (!FieldRec->hasTrivialDestructor())
788 data().HasTrivialDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000789 if (FieldRec->hasObjectMember())
790 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000791
792 // C++0x [class]p7:
793 // A standard-layout class is a class that:
794 // -- has no non-static data members of type non-standard-layout
795 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000796 if (!FieldRec->isStandardLayout())
797 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000798
799 // C++0x [class]p7:
800 // A standard-layout class is a class that:
801 // [...]
802 // -- has no base classes of the same type as the first non-static
803 // data member.
804 // We don't want to expend bits in the state of the record decl
805 // tracking whether this is the first non-static data member so we
806 // cheat a bit and use some of the existing state: the empty bit.
807 // Virtual bases and virtual methods make a class non-empty, but they
808 // also make it non-standard-layout so we needn't check here.
809 // A non-empty base class may leave the class standard-layout, but not
810 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000811 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000812 // data member must come through here with Empty still true, and Empty
813 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000814 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000815 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
816 BE = bases_end();
817 BI != BE; ++BI) {
818 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000819 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000820 break;
821 }
822 }
823 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000824
825 // Keep track of the presence of mutable fields.
826 if (FieldRec->hasMutableFields())
827 data().HasMutableFields = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000828 }
829 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000830
831 // C++0x [class]p7:
832 // A standard-layout class is a class that:
833 // [...]
834 // -- either has no non-static data members in the most derived
835 // class and at most one base class with non-static data members,
836 // or has no base classes with non-static data members, and
837 // At this point we know that we have a non-static data member, so the last
838 // clause holds.
839 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000840 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000841
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000842 // If this is not a zero-length bit-field, then the class is not empty.
843 if (data().Empty) {
844 if (!Field->getBitWidth())
845 data().Empty = false;
846 else if (!Field->getBitWidth()->isTypeDependent() &&
847 !Field->getBitWidth()->isValueDependent()) {
848 llvm::APSInt Bits;
849 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
850 if (!!Bits)
851 data().Empty = false;
852 }
853 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000854 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000855
856 // Handle using declarations of conversion functions.
857 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
858 if (Shadow->getDeclName().getNameKind()
859 == DeclarationName::CXXConversionFunctionName)
860 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000861}
862
John McCallb05b5f32010-03-15 09:07:48 +0000863static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
864 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000865 if (isa<UsingShadowDecl>(Conv))
866 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000867 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
868 T = ConvTemp->getTemplatedDecl()->getResultType();
869 else
870 T = cast<CXXConversionDecl>(Conv)->getConversionType();
871 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000872}
873
John McCallb05b5f32010-03-15 09:07:48 +0000874/// Collect the visible conversions of a base class.
875///
876/// \param Base a base class of the class we're considering
877/// \param InVirtual whether this base class is a virtual base (or a base
878/// of a virtual base)
879/// \param Access the access along the inheritance path to this base
880/// \param ParentHiddenTypes the conversions provided by the inheritors
881/// of this base
882/// \param Output the set to which to add conversions from non-virtual bases
883/// \param VOutput the set to which to add conversions from virtual bases
884/// \param HiddenVBaseCs the set of conversions which were hidden in a
885/// virtual base along some inheritance path
886static void CollectVisibleConversions(ASTContext &Context,
887 CXXRecordDecl *Record,
888 bool InVirtual,
889 AccessSpecifier Access,
890 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
891 UnresolvedSetImpl &Output,
892 UnresolvedSetImpl &VOutput,
893 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
894 // The set of types which have conversions in this class or its
895 // subclasses. As an optimization, we don't copy the derived set
896 // unless it might change.
897 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
898 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
899
900 // Collect the direct conversions and figure out which conversions
901 // will be hidden in the subclasses.
902 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
903 if (!Cs.empty()) {
904 HiddenTypesBuffer = ParentHiddenTypes;
905 HiddenTypes = &HiddenTypesBuffer;
906
907 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
908 bool Hidden =
909 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
910
911 // If this conversion is hidden and we're in a virtual base,
912 // remember that it's hidden along some inheritance path.
913 if (Hidden && InVirtual)
914 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
915
916 // If this conversion isn't hidden, add it to the appropriate output.
917 else if (!Hidden) {
918 AccessSpecifier IAccess
919 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
920
921 if (InVirtual)
922 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000923 else
John McCallb05b5f32010-03-15 09:07:48 +0000924 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000925 }
926 }
927 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000928
John McCallb05b5f32010-03-15 09:07:48 +0000929 // Collect information recursively from any base classes.
930 for (CXXRecordDecl::base_class_iterator
931 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
932 const RecordType *RT = I->getType()->getAs<RecordType>();
933 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000934
John McCallb05b5f32010-03-15 09:07:48 +0000935 AccessSpecifier BaseAccess
936 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
937 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000938
John McCallb05b5f32010-03-15 09:07:48 +0000939 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
940 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
941 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000942 }
John McCallb05b5f32010-03-15 09:07:48 +0000943}
Sebastian Redl9994a342009-10-25 17:03:50 +0000944
John McCallb05b5f32010-03-15 09:07:48 +0000945/// Collect the visible conversions of a class.
946///
947/// This would be extremely straightforward if it weren't for virtual
948/// bases. It might be worth special-casing that, really.
949static void CollectVisibleConversions(ASTContext &Context,
950 CXXRecordDecl *Record,
951 UnresolvedSetImpl &Output) {
952 // The collection of all conversions in virtual bases that we've
953 // found. These will be added to the output as long as they don't
954 // appear in the hidden-conversions set.
955 UnresolvedSet<8> VBaseCs;
956
957 // The set of conversions in virtual bases that we've determined to
958 // be hidden.
959 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
960
961 // The set of types hidden by classes derived from this one.
962 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
963
964 // Go ahead and collect the direct conversions and add them to the
965 // hidden-types set.
966 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
967 Output.append(Cs.begin(), Cs.end());
968 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
969 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
970
971 // Recursively collect conversions from base classes.
972 for (CXXRecordDecl::base_class_iterator
973 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
974 const RecordType *RT = I->getType()->getAs<RecordType>();
975 if (!RT) continue;
976
977 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
978 I->isVirtual(), I->getAccessSpecifier(),
979 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
980 }
981
982 // Add any unhidden conversions provided by virtual bases.
983 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
984 I != E; ++I) {
985 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
986 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000987 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000988}
989
990/// getVisibleConversionFunctions - get all conversion functions visible
991/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000992const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000993 // If root class, all conversions are visible.
994 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000995 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000996 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000997 if (data().ComputedVisibleConversions)
998 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000999 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001000 data().ComputedVisibleConversions = true;
1001 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001002}
1003
John McCall32daa422010-03-31 01:36:47 +00001004void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1005 // This operation is O(N) but extremely rare. Sema only uses it to
1006 // remove UsingShadowDecls in a class that were followed by a direct
1007 // declaration, e.g.:
1008 // class A : B {
1009 // using B::operator int;
1010 // operator int();
1011 // };
1012 // This is uncommon by itself and even more uncommon in conjunction
1013 // with sufficiently large numbers of directly-declared conversions
1014 // that asymptotic behavior matters.
1015
1016 UnresolvedSetImpl &Convs = *getConversionFunctions();
1017 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1018 if (Convs[I].getDecl() == ConvDecl) {
1019 Convs.erase(I);
1020 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1021 && "conversion was found multiple times in unresolved set");
1022 return;
1023 }
1024 }
1025
1026 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001027}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001028
Douglas Gregorf6b11852009-10-08 15:14:33 +00001029CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001030 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001031 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1032
1033 return 0;
1034}
1035
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001036MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1037 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1038}
1039
Douglas Gregorf6b11852009-10-08 15:14:33 +00001040void
1041CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1042 TemplateSpecializationKind TSK) {
1043 assert(TemplateOrInstantiation.isNull() &&
1044 "Previous template or instantiation?");
1045 assert(!isa<ClassTemplateSpecializationDecl>(this));
1046 TemplateOrInstantiation
1047 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1048}
1049
Anders Carlssonb13e3572009-12-07 06:33:48 +00001050TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1051 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001052 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1053 return Spec->getSpecializationKind();
1054
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001055 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001056 return MSInfo->getTemplateSpecializationKind();
1057
1058 return TSK_Undeclared;
1059}
1060
1061void
1062CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1063 if (ClassTemplateSpecializationDecl *Spec
1064 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1065 Spec->setSpecializationKind(TSK);
1066 return;
1067 }
1068
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001069 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001070 MSInfo->setTemplateSpecializationKind(TSK);
1071 return;
1072 }
1073
1074 assert(false && "Not a class template or member class specialization");
1075}
1076
Douglas Gregor1d110e02010-07-01 14:13:13 +00001077CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1078 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001079 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001080
1081 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001082 = Context.DeclarationNames.getCXXDestructorName(
1083 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001084
John McCallc0bf4622010-02-23 00:48:20 +00001085 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001086 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001087 if (I == E)
1088 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001090 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001091 return Dtor;
1092}
1093
Douglas Gregorda2142f2011-02-19 18:51:44 +00001094void CXXRecordDecl::completeDefinition() {
1095 completeDefinition(0);
1096}
1097
1098void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1099 RecordDecl::completeDefinition();
1100
John McCallf85e1932011-06-15 23:02:42 +00001101 if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) {
1102 // Objective-C Automatic Reference Counting:
1103 // If a class has a non-static data member of Objective-C pointer
1104 // type (or array thereof), it is a non-POD type and its
1105 // default constructor (if any), copy constructor, copy assignment
1106 // operator, and destructor are non-trivial.
1107 struct DefinitionData &Data = data();
1108 Data.PlainOldData = false;
1109 Data.HasTrivialDefaultConstructor = false;
1110 Data.HasTrivialCopyConstructor = false;
1111 Data.HasTrivialCopyAssignment = false;
1112 Data.HasTrivialDestructor = false;
1113 }
1114
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001115 // If the class may be abstract (but hasn't been marked as such), check for
1116 // any pure final overriders.
1117 if (mayBeAbstract()) {
1118 CXXFinalOverriderMap MyFinalOverriders;
1119 if (!FinalOverriders) {
1120 getFinalOverriders(MyFinalOverriders);
1121 FinalOverriders = &MyFinalOverriders;
1122 }
1123
1124 bool Done = false;
1125 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1126 MEnd = FinalOverriders->end();
1127 M != MEnd && !Done; ++M) {
1128 for (OverridingMethods::iterator SO = M->second.begin(),
1129 SOEnd = M->second.end();
1130 SO != SOEnd && !Done; ++SO) {
1131 assert(SO->second.size() > 0 &&
1132 "All virtual functions have overridding virtual functions");
1133
1134 // C++ [class.abstract]p4:
1135 // A class is abstract if it contains or inherits at least one
1136 // pure virtual function for which the final overrider is pure
1137 // virtual.
1138 if (SO->second.front().Method->isPure()) {
1139 data().Abstract = true;
1140 Done = true;
1141 break;
1142 }
1143 }
1144 }
1145 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001146
1147 // Set access bits correctly on the directly-declared conversions.
1148 for (UnresolvedSetIterator I = data().Conversions.begin(),
1149 E = data().Conversions.end();
1150 I != E; ++I)
1151 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001152}
1153
1154bool CXXRecordDecl::mayBeAbstract() const {
1155 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1156 isDependentContext())
1157 return false;
1158
1159 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1160 BEnd = bases_end();
1161 B != BEnd; ++B) {
1162 CXXRecordDecl *BaseDecl
1163 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1164 if (BaseDecl->isAbstract())
1165 return true;
1166 }
1167
1168 return false;
1169}
1170
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001171CXXMethodDecl *
1172CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001173 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001174 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001175 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001176 bool isStatic, StorageClass SCAsWritten, bool isInline,
1177 SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001178 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001179 isStatic, SCAsWritten, isInline, EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001180}
1181
Douglas Gregor90916562009-09-29 18:16:17 +00001182bool CXXMethodDecl::isUsualDeallocationFunction() const {
1183 if (getOverloadedOperator() != OO_Delete &&
1184 getOverloadedOperator() != OO_Array_Delete)
1185 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001186
1187 // C++ [basic.stc.dynamic.deallocation]p2:
1188 // A template instance is never a usual deallocation function,
1189 // regardless of its signature.
1190 if (getPrimaryTemplate())
1191 return false;
1192
Douglas Gregor90916562009-09-29 18:16:17 +00001193 // C++ [basic.stc.dynamic.deallocation]p2:
1194 // If a class T has a member deallocation function named operator delete
1195 // with exactly one parameter, then that function is a usual (non-placement)
1196 // deallocation function. [...]
1197 if (getNumParams() == 1)
1198 return true;
1199
1200 // C++ [basic.stc.dynamic.deallocation]p2:
1201 // [...] If class T does not declare such an operator delete but does
1202 // declare a member deallocation function named operator delete with
1203 // exactly two parameters, the second of which has type std::size_t (18.1),
1204 // then this function is a usual deallocation function.
1205 ASTContext &Context = getASTContext();
1206 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001207 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1208 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001209 return false;
1210
1211 // This function is a usual deallocation function if there are no
1212 // single-parameter deallocation functions of the same kind.
1213 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1214 R.first != R.second; ++R.first) {
1215 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1216 if (FD->getNumParams() == 1)
1217 return false;
1218 }
1219
1220 return true;
1221}
1222
Douglas Gregor06a9f362010-05-01 20:49:11 +00001223bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001224 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001225 // A user-declared copy assignment operator X::operator= is a non-static
1226 // non-template member function of class X with exactly one parameter of
1227 // type X, X&, const X&, volatile X& or const volatile X&.
1228 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1229 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001230 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001231 return false;
1232
1233 QualType ParamType = getParamDecl(0)->getType();
1234 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1235 ParamType = Ref->getPointeeType();
1236
1237 ASTContext &Context = getASTContext();
1238 QualType ClassType
1239 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1240 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1241}
1242
Sean Huntffe37fd2011-05-25 20:50:04 +00001243bool CXXMethodDecl::isMoveAssignmentOperator() const {
1244 // C++0x [class.copy]p19:
1245 // A user-declared move assignment operator X::operator= is a non-static
1246 // non-template member function of class X with exactly one parameter of type
1247 // X&&, const X&&, volatile X&&, or const volatile X&&.
1248 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1249 getPrimaryTemplate() || getDescribedFunctionTemplate())
1250 return false;
1251
1252 QualType ParamType = getParamDecl(0)->getType();
1253 if (!isa<RValueReferenceType>(ParamType))
1254 return false;
1255 ParamType = ParamType->getPointeeType();
1256
1257 ASTContext &Context = getASTContext();
1258 QualType ClassType
1259 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1260 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1261}
1262
Anders Carlsson05eb2442009-05-16 23:58:37 +00001263void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001264 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001265 assert(!MD->getParent()->isDependentContext() &&
1266 "Can't add an overridden method to a class template!");
1267
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001268 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001269}
1270
1271CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001272 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001273}
1274
1275CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001276 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001277}
1278
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001279unsigned CXXMethodDecl::size_overridden_methods() const {
1280 return getASTContext().overridden_methods_size(this);
1281}
1282
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001283QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001284 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1285 // If the member function is declared const, the type of this is const X*,
1286 // if the member function is declared volatile, the type of this is
1287 // volatile X*, and if the member function is declared const volatile,
1288 // the type of this is const volatile X*.
1289
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001290 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001291
John McCall3cb0ebd2010-03-10 03:28:59 +00001292 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001293 ClassTy = C.getQualifiedType(ClassTy,
1294 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001295 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001296}
1297
Eli Friedmand7d7f672009-12-06 20:50:05 +00001298bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001299 // If this function is a template instantiation, look at the template from
1300 // which it was instantiated.
1301 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1302 if (!CheckFn)
1303 CheckFn = this;
1304
Eli Friedmand7d7f672009-12-06 20:50:05 +00001305 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001306 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001307}
1308
Sean Huntcbb67482011-01-08 20:30:50 +00001309CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1310 TypeSourceInfo *TInfo, bool IsVirtual,
1311 SourceLocation L, Expr *Init,
1312 SourceLocation R,
1313 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001314 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001315 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1316 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001317{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001318}
1319
Sean Huntcbb67482011-01-08 20:30:50 +00001320CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1321 FieldDecl *Member,
1322 SourceLocation MemberLoc,
1323 SourceLocation L, Expr *Init,
1324 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001325 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001326 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1327 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1328{
1329}
1330
Sean Huntcbb67482011-01-08 20:30:50 +00001331CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1332 IndirectFieldDecl *Member,
1333 SourceLocation MemberLoc,
1334 SourceLocation L, Expr *Init,
1335 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001336 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001337 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001338 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001339{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001340}
1341
Sean Huntcbb67482011-01-08 20:30:50 +00001342CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Hunt41717662011-02-26 19:13:13 +00001343 SourceLocation D, SourceLocation L,
1344 CXXConstructorDecl *Target, Expr *Init,
1345 SourceLocation R)
1346 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1347 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1348 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1349{
1350}
1351
1352CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001353 FieldDecl *Member,
1354 SourceLocation MemberLoc,
1355 SourceLocation L, Expr *Init,
1356 SourceLocation R,
1357 VarDecl **Indices,
1358 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001359 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001360 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001361 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001362{
1363 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1364 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1365}
1366
Sean Huntcbb67482011-01-08 20:30:50 +00001367CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1368 FieldDecl *Member,
1369 SourceLocation MemberLoc,
1370 SourceLocation L, Expr *Init,
1371 SourceLocation R,
1372 VarDecl **Indices,
1373 unsigned NumIndices) {
1374 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001375 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001376 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001377 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1378 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001379}
1380
Sean Huntcbb67482011-01-08 20:30:50 +00001381TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001382 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001383 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001384 else
1385 return TypeLoc();
1386}
1387
Sean Huntcbb67482011-01-08 20:30:50 +00001388const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001389 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001390 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001391 else
1392 return 0;
1393}
1394
Sean Huntcbb67482011-01-08 20:30:50 +00001395SourceLocation CXXCtorInitializer::getSourceLocation() const {
Sean Hunt41717662011-02-26 19:13:13 +00001396 if (isAnyMemberInitializer() || isDelegatingInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001397 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001398
1399 if (isInClassMemberInitializer())
1400 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001401
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001402 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001403}
1404
Sean Huntcbb67482011-01-08 20:30:50 +00001405SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001406 if (isInClassMemberInitializer()) {
1407 FieldDecl *D = getAnyMember();
1408 if (Expr *I = D->getInClassInitializer())
1409 return I->getSourceRange();
1410 return SourceRange();
1411 }
1412
Douglas Gregor802ab452009-12-02 22:36:29 +00001413 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001414}
1415
Douglas Gregorb48fe382008-10-31 09:07:45 +00001416CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001417CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001418 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Sean Hunt5f802e52011-05-06 00:11:07 +00001419 QualType(), 0, false, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001420}
1421
1422CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001423CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001424 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001425 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001426 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001427 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001428 bool isInline,
Sean Hunt5f802e52011-05-06 00:11:07 +00001429 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001430 assert(NameInfo.getName().getNameKind()
1431 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001432 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001433 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Sean Hunt5f802e52011-05-06 00:11:07 +00001434 isExplicit, isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001435}
1436
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001437bool CXXConstructorDecl::isDefaultConstructor() const {
1438 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001439 // A default constructor for a class X is a constructor of class
1440 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001441 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001442 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001443}
1444
Mike Stump1eb44332009-09-09 15:08:12 +00001445bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001446CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001447 return isCopyOrMoveConstructor(TypeQuals) &&
1448 getParamDecl(0)->getType()->isLValueReferenceType();
1449}
1450
1451bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1452 return isCopyOrMoveConstructor(TypeQuals) &&
1453 getParamDecl(0)->getType()->isRValueReferenceType();
1454}
1455
1456/// \brief Determine whether this is a copy or move constructor.
1457bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001458 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001459 // A non-template constructor for class X is a copy constructor
1460 // if its first parameter is of type X&, const X&, volatile X& or
1461 // const volatile X&, and either there are no other parameters
1462 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001463 // C++0x [class.copy]p3:
1464 // A non-template constructor for class X is a move constructor if its
1465 // first parameter is of type X&&, const X&&, volatile X&&, or
1466 // const volatile X&&, and either there are no other parameters or else
1467 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001468 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001469 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001470 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001471 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001472 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001473
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001474 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001475
1476 // Do we have a reference type?
1477 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001478 if (!ParamRefType)
1479 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001480
Douglas Gregorfd476482009-11-13 23:59:09 +00001481 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001482 ASTContext &Context = getASTContext();
1483
Douglas Gregorfd476482009-11-13 23:59:09 +00001484 CanQualType PointeeType
1485 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001486 CanQualType ClassTy
1487 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001488 if (PointeeType.getUnqualifiedType() != ClassTy)
1489 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001490
John McCall0953e762009-09-24 19:53:00 +00001491 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001492
1493 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001494 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001495 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001496}
1497
Anders Carlssonfaccd722009-08-28 16:57:08 +00001498bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001499 // C++ [class.conv.ctor]p1:
1500 // A constructor declared without the function-specifier explicit
1501 // that can be called with a single parameter specifies a
1502 // conversion from the type of its first parameter to the type of
1503 // its class. Such a constructor is called a converting
1504 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001505 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001506 return false;
1507
Mike Stump1eb44332009-09-09 15:08:12 +00001508 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001509 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001510 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001511 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001512}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001513
Douglas Gregor6493cc52010-11-08 17:16:59 +00001514bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001515 if ((getNumParams() < 1) ||
1516 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1517 (getPrimaryTemplate() == 0) ||
1518 (getDescribedFunctionTemplate() != 0))
1519 return false;
1520
1521 const ParmVarDecl *Param = getParamDecl(0);
1522
1523 ASTContext &Context = getASTContext();
1524 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1525
Douglas Gregor66724ea2009-11-14 01:20:54 +00001526 // Is it the same as our our class type?
1527 CanQualType ClassTy
1528 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1529 if (ParamType.getUnqualifiedType() != ClassTy)
1530 return false;
1531
1532 return true;
1533}
1534
Sebastian Redlf677ea32011-02-05 19:23:19 +00001535const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1536 // Hack: we store the inherited constructor in the overridden method table
1537 method_iterator It = begin_overridden_methods();
1538 if (It == end_overridden_methods())
1539 return 0;
1540
1541 return cast<CXXConstructorDecl>(*It);
1542}
1543
1544void
1545CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1546 // Hack: we store the inherited constructor in the overridden method table
1547 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1548 addOverriddenMethod(BaseCtor);
1549}
1550
Douglas Gregor42a552f2008-11-05 20:51:48 +00001551CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001552CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001553 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001554 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001555}
1556
1557CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001558CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001559 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001560 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001561 QualType T, TypeSourceInfo *TInfo,
1562 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001563 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001564 assert(NameInfo.getName().getNameKind()
1565 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001566 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001567 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001568 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001569}
1570
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001571CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001572CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001573 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001574 QualType(), 0, false, false,
1575 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001576}
1577
1578CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001579CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001580 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001581 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001582 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001583 bool isInline, bool isExplicit,
1584 SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001585 assert(NameInfo.getName().getNameKind()
1586 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001587 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001588 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001589 isInline, isExplicit, EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001590}
1591
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001592LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001593 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001594 SourceLocation ExternLoc,
1595 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001596 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001597 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001598 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001599}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001600
1601UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1602 SourceLocation L,
1603 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001604 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001605 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001606 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001607 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001608 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1609 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001610 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1611 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001612}
1613
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001614NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1615 if (NamespaceAliasDecl *NA =
1616 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1617 return NA->getNamespace();
1618 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1619}
1620
Mike Stump1eb44332009-09-09 15:08:12 +00001621NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001622 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001623 SourceLocation AliasLoc,
1624 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001625 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001626 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001627 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001628 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1629 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001630 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1631 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001632}
1633
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001634UsingDecl *UsingShadowDecl::getUsingDecl() const {
1635 const UsingShadowDecl *Shadow = this;
1636 while (const UsingShadowDecl *NextShadow =
1637 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1638 Shadow = NextShadow;
1639 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1640}
1641
1642void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1643 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1644 "declaration already in set");
1645 assert(S->getUsingDecl() == this);
1646
1647 if (FirstUsingShadow)
1648 S->UsingOrNextShadow = FirstUsingShadow;
1649 FirstUsingShadow = S;
1650}
1651
1652void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1653 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1654 "declaration not in set");
1655 assert(S->getUsingDecl() == this);
1656
1657 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1658
1659 if (FirstUsingShadow == S) {
1660 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1661 S->UsingOrNextShadow = this;
1662 return;
1663 }
1664
1665 UsingShadowDecl *Prev = FirstUsingShadow;
1666 while (Prev->UsingOrNextShadow != S)
1667 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1668 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1669 S->UsingOrNextShadow = this;
1670}
1671
Douglas Gregordc355712011-02-25 00:36:19 +00001672UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1673 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001674 const DeclarationNameInfo &NameInfo,
1675 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001676 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001677}
1678
John McCall7ba107a2009-11-18 02:36:19 +00001679UnresolvedUsingValueDecl *
1680UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1681 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001682 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001683 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001684 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001685 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001686}
1687
1688UnresolvedUsingTypenameDecl *
1689UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1690 SourceLocation UsingLoc,
1691 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001692 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001693 SourceLocation TargetNameLoc,
1694 DeclarationName TargetName) {
1695 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001696 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001697 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001698}
1699
Anders Carlssonfb311762009-03-14 00:25:26 +00001700StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001701 SourceLocation StaticAssertLoc,
1702 Expr *AssertExpr,
1703 StringLiteral *Message,
1704 SourceLocation RParenLoc) {
1705 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1706 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001707}
1708
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001709static const char *getAccessName(AccessSpecifier AS) {
1710 switch (AS) {
1711 default:
1712 case AS_none:
1713 assert("Invalid access specifier!");
1714 return 0;
1715 case AS_public:
1716 return "public";
1717 case AS_private:
1718 return "private";
1719 case AS_protected:
1720 return "protected";
1721 }
1722}
1723
1724const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1725 AccessSpecifier AS) {
1726 return DB << getAccessName(AS);
1727}