blob: 2f21e4cbd652e536830af707ff7114a943de7ed8 [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000017#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000018#include "clang/AST/CXXInheritance.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregor76852c22011-11-01 01:16:03 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000021#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000022#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000023#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Decl Allocation/Deallocation Method Implementations
29//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000030
David Blaikie99ba9e32011-12-20 02:48:34 +000031void AccessSpecDecl::anchor() { }
32
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000033AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
34 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(AccessSpecDecl));
35 return new (Mem) AccessSpecDecl(EmptyShell());
36}
37
John McCall86ff3082010-02-04 22:26:26 +000038CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
39 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000040 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
41 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000042 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000043 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000044 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000045 HasMutableFields(false), HasOnlyCMembers(true),
Richard Smithd079abf2012-05-07 01:07:30 +000046 HasInClassInitializer(false),
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +000047 HasTrivialDefaultConstructor(true),
Richard Smith61802452011-12-22 02:22:31 +000048 HasConstexprNonCopyMoveConstructor(false),
49 DefaultedDefaultConstructorIsConstexpr(true),
Richard Smithd3861ce2012-06-10 07:07:24 +000050 HasConstexprDefaultConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000051 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
52 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
Richard Smithdfefb842012-02-25 07:33:38 +000053 HasIrrelevantDestructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000054 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000055 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000056 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
57 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sebastian Redl85ea7aa2011-08-30 19:58:05 +000058 DeclaredDestructor(false), FailedImplicitMoveConstructor(false),
Eli Friedman72899c32012-01-07 04:59:52 +000059 FailedImplicitMoveAssignment(false), IsLambda(false), NumBases(0),
60 NumVBases(0), Bases(), VBases(), Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000061}
62
Benjamin Krameree3096a2012-07-04 17:03:33 +000063CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
64 return Bases.get(Definition->getASTContext().getExternalSource());
65}
66
67CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
68 return VBases.get(Definition->getASTContext().getExternalSource());
69}
70
John McCall86ff3082010-02-04 22:26:26 +000071CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000072 SourceLocation StartLoc, SourceLocation IdLoc,
73 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
74 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000075 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000076 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000077
Jay Foad4ba2a172011-01-12 09:06:06 +000078CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000079 DeclContext *DC, SourceLocation StartLoc,
80 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000081 CXXRecordDecl* PrevDecl,
82 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000083 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
84 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000085
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000086 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000087 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000088 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000089 return R;
90}
91
Douglas Gregorda8962a2012-02-13 15:44:47 +000092CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Douglas Gregorf4b7de12012-02-21 19:11:17 +000093 SourceLocation Loc, bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +000094 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
95 0, 0);
96 R->IsBeingDefined = true;
Douglas Gregorf4b7de12012-02-21 19:11:17 +000097 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Dependent);
Douglas Gregorda8962a2012-02-13 15:44:47 +000098 C.getTypeDeclType(R, /*PrevDecl=*/0);
99 return R;
100}
101
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000102CXXRecordDecl *
103CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
104 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
105 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
106 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000107}
108
Mike Stump1eb44332009-09-09 15:08:12 +0000109void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000110CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000111 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000112 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000113
Douglas Gregor7c789c12010-10-29 22:39:52 +0000114 if (!data().Bases.isOffset() && data().NumBases > 0)
115 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Richard Smithdd677232011-10-18 20:08:55 +0000117 if (NumBases) {
118 // C++ [dcl.init.aggr]p1:
119 // An aggregate is [...] a class with [...] no base classes [...].
120 data().Aggregate = false;
121
122 // C++ [class]p4:
123 // A POD-struct is an aggregate class...
124 data().PlainOldData = false;
125 }
126
Anders Carlsson6f6de732010-03-29 05:13:12 +0000127 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000128 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000129
130 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000131 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
John McCall86ff3082010-02-04 22:26:26 +0000133 data().Bases = new(C) CXXBaseSpecifier [NumBases];
134 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000135 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000136 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000137 // Keep track of inherited vbases for this base class.
138 const CXXBaseSpecifier *Base = Bases[i];
139 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000140 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000141 if (BaseType->isDependentType())
142 continue;
143 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000144 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000145
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000146 // A class with a non-empty base class is not empty.
147 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000148 if (!BaseClassDecl->isEmpty()) {
149 if (!data().Empty) {
150 // C++0x [class]p7:
151 // A standard-layout class is a class that:
152 // [...]
153 // -- either has no non-static data members in the most derived
154 // class and at most one base class with non-static data members,
155 // or has no base classes with non-static data members, and
156 // If this is the second non-empty base, then neither of these two
157 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000158 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000159 }
160
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000161 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000162 data().HasNoNonEmptyBases = false;
163 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000164
Douglas Gregor85606eb2010-09-28 20:50:54 +0000165 // C++ [class.virtual]p1:
166 // A class that declares or inherits a virtual function is called a
167 // polymorphic class.
168 if (BaseClassDecl->isPolymorphic())
169 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000170
Chandler Carrutha8225442011-04-30 09:17:45 +0000171 // C++0x [class]p7:
172 // A standard-layout class is a class that: [...]
173 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000174 if (!BaseClassDecl->isStandardLayout())
175 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000176
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000177 // Record if this base is the first non-literal field or base.
178 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
179 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000180
Anders Carlsson6f6de732010-03-29 05:13:12 +0000181 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000182 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000183 BaseClassDecl->vbases_begin(),
184 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000185 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000186 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000187 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000188 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000189
190 if (Base->isVirtual()) {
191 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000192 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000193 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000194
195 // C++0x [meta.unary.prop] is_empty:
196 // T is a class type, but not a union type, with ... no virtual base
197 // classes
198 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000199
200 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000201 // A default constructor is trivial [...] if:
202 // -- its class has [...] no virtual bases
203 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000204
205 // C++0x [class.copy]p13:
206 // A copy/move constructor for class X is trivial if it is neither
207 // user-provided nor deleted and if
208 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000209 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000210 data().HasTrivialMoveConstructor = false;
211
212 // C++0x [class.copy]p27:
213 // A copy/move assignment operator for class X is trivial if it is
214 // neither user-provided nor deleted and if
215 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000216 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000217 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000218
219 // C++0x [class]p7:
220 // A standard-layout class is a class that: [...]
221 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000222 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000223
224 // C++11 [dcl.constexpr]p4:
225 // In the definition of a constexpr constructor [...]
226 // -- the class shall not have any virtual base classes
227 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000228 } else {
229 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000230 // A default constructor is trivial [...] if:
231 // -- all the direct base classes of its class have trivial default
232 // constructors.
233 if (!BaseClassDecl->hasTrivialDefaultConstructor())
234 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000235
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000236 // C++0x [class.copy]p13:
237 // A copy/move constructor for class X is trivial if [...]
238 // [...]
239 // -- the constructor selected to copy/move each direct base class
240 // subobject is trivial, and
241 // FIXME: C++0x: We need to only consider the selected constructor
242 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000243 if (!BaseClassDecl->hasTrivialCopyConstructor())
244 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000245 if (!BaseClassDecl->hasTrivialMoveConstructor())
246 data().HasTrivialMoveConstructor = false;
247
248 // C++0x [class.copy]p27:
249 // A copy/move assignment operator for class X is trivial if [...]
250 // [...]
251 // -- the assignment operator selected to copy/move each direct base
252 // class subobject is trivial, and
253 // FIXME: C++0x: We need to only consider the selected operator instead
254 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000255 if (!BaseClassDecl->hasTrivialCopyAssignment())
256 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000257 if (!BaseClassDecl->hasTrivialMoveAssignment())
258 data().HasTrivialMoveAssignment = false;
Richard Smith61802452011-12-22 02:22:31 +0000259
260 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000261 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000262 // requirements of a constexpr constructor, the implicitly-defined
263 // default constructor is constexpr.
264 if (!BaseClassDecl->hasConstexprDefaultConstructor())
265 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000266 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000267
268 // C++ [class.ctor]p3:
269 // A destructor is trivial if all the direct base classes of its class
270 // have trivial destructors.
271 if (!BaseClassDecl->hasTrivialDestructor())
272 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000273
274 if (!BaseClassDecl->hasIrrelevantDestructor())
275 data().HasIrrelevantDestructor = false;
276
John McCallf85e1932011-06-15 23:02:42 +0000277 // A class has an Objective-C object member if... or any of its bases
278 // has an Objective-C object member.
279 if (BaseClassDecl->hasObjectMember())
280 setHasObjectMember(true);
281
Douglas Gregor2bb11012011-05-13 01:05:07 +0000282 // Keep track of the presence of mutable fields.
283 if (BaseClassDecl->hasMutableFields())
284 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000285 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000286
287 if (VBases.empty())
288 return;
289
290 // Create base specifier for any direct or indirect virtual bases.
291 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
292 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000293 for (int I = 0, E = VBases.size(); I != E; ++I)
294 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000295}
296
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000297/// Callback function for CXXRecordDecl::forallBases that acknowledges
298/// that it saw a base class.
299static bool SawBase(const CXXRecordDecl *, void *) {
300 return true;
301}
302
303bool CXXRecordDecl::hasAnyDependentBases() const {
304 if (!isDependentContext())
305 return false;
306
307 return !forallBases(SawBase, 0);
308}
309
Sean Huntffe37fd2011-05-25 20:50:04 +0000310bool CXXRecordDecl::hasConstCopyConstructor() const {
311 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000312}
313
Chandler Carruthb7e95892011-04-23 10:47:28 +0000314bool CXXRecordDecl::isTriviallyCopyable() const {
315 // C++0x [class]p5:
316 // A trivially copyable class is a class that:
317 // -- has no non-trivial copy constructors,
318 if (!hasTrivialCopyConstructor()) return false;
319 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000320 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000321 // -- has no non-trivial copy assignment operators,
322 if (!hasTrivialCopyAssignment()) return false;
323 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000324 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000325 // -- has a trivial destructor.
326 if (!hasTrivialDestructor()) return false;
327
328 return true;
329}
330
Douglas Gregor0d405db2010-07-01 20:59:04 +0000331/// \brief Perform a simplistic form of overload resolution that only considers
332/// cv-qualifiers on a single parameter, and return the best overload candidate
333/// (if there is one).
334static CXXMethodDecl *
335GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000336 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000337 if (Cands.empty())
338 return 0;
339 if (Cands.size() == 1)
340 return Cands[0].first;
341
342 unsigned Best = 0, N = Cands.size();
343 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000344 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000345 Best = I;
346
Benjamin Kramer2cd7f412012-07-30 15:53:26 +0000347 for (unsigned I = 0; I != N; ++I)
348 if (I != Best && Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000349 return 0;
350
351 return Cands[Best].first;
352}
353
Sean Huntffe37fd2011-05-25 20:50:04 +0000354CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
355 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000356 QualType ClassType
357 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000358 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000359 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000360 Context.getCanonicalType(ClassType));
361 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000362 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000363 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000364 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000365 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000366 // C++ [class.copy]p2:
367 // A non-template constructor for class X is a copy constructor if [...]
368 if (isa<FunctionTemplateDecl>(*Con))
369 continue;
370
Douglas Gregor0d405db2010-07-01 20:59:04 +0000371 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
372 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000373 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
374 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000375 Found.push_back(std::make_pair(
376 const_cast<CXXConstructorDecl *>(Constructor),
377 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000378 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000379 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000380
381 return cast_or_null<CXXConstructorDecl>(
382 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000383}
384
Sean Huntffe37fd2011-05-25 20:50:04 +0000385CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
386 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
387 if (I->isMoveConstructor())
David Blaikie581deb32012-06-06 20:45:41 +0000388 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000389
390 return 0;
391}
392
Douglas Gregorb87786f2010-07-01 17:48:08 +0000393CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
394 ASTContext &Context = getASTContext();
395 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
396 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
397
Chris Lattner5f9e2722011-07-23 10:55:15 +0000398 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000399 DeclContext::lookup_const_iterator Op, OpEnd;
400 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
401 // C++ [class.copy]p9:
402 // A user-declared copy assignment operator is a non-static non-template
403 // member function of class X with exactly one parameter of type X, X&,
404 // const X&, volatile X& or const volatile X&.
405 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
406 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
407 continue;
408
409 const FunctionProtoType *FnType
410 = Method->getType()->getAs<FunctionProtoType>();
411 assert(FnType && "Overloaded operator has no prototype.");
412 // Don't assert on this; an invalid decl might have been left in the AST.
413 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
414 continue;
415
416 QualType ArgType = FnType->getArgType(0);
417 Qualifiers Quals;
418 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
419 ArgType = Ref->getPointeeType();
420 // If we have a const argument and we have a reference to a non-const,
421 // this function does not match.
422 if (ArgIsConst && !ArgType.isConstQualified())
423 continue;
424
425 Quals = ArgType.getQualifiers();
426 } else {
427 // By-value copy-assignment operators are treated like const X&
428 // copy-assignment operators.
429 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
430 }
431
432 if (!Context.hasSameUnqualifiedType(ArgType, Class))
433 continue;
434
435 // Save this copy-assignment operator. It might be "the one".
436 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
437 }
438
439 // Use a simplistic form of overload resolution to find the candidate.
440 return GetBestOverloadCandidateSimple(Found);
441}
442
Sean Huntffe37fd2011-05-25 20:50:04 +0000443CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
444 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
445 if (I->isMoveAssignmentOperator())
David Blaikie581deb32012-06-06 20:45:41 +0000446 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000447
448 return 0;
449}
450
Douglas Gregor21386642010-09-28 21:55:22 +0000451void CXXRecordDecl::markedVirtualFunctionPure() {
452 // C++ [class.abstract]p2:
453 // A class is abstract if it has at least one pure virtual function.
454 data().Abstract = true;
455}
456
Richard Smith3f5f5582012-06-08 21:09:22 +0000457void CXXRecordDecl::markedConstructorConstexpr(CXXConstructorDecl *CD) {
Richard Smithd3861ce2012-06-10 07:07:24 +0000458 if (!CD->isCopyOrMoveConstructor())
Richard Smith3f5f5582012-06-08 21:09:22 +0000459 data().HasConstexprNonCopyMoveConstructor = true;
460
461 if (CD->isDefaultConstructor())
462 data().HasConstexprDefaultConstructor = true;
463}
464
Douglas Gregor21386642010-09-28 21:55:22 +0000465void CXXRecordDecl::addedMember(Decl *D) {
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +0000466 if (!D->isImplicit() &&
467 !isa<FieldDecl>(D) &&
468 !isa<IndirectFieldDecl>(D) &&
469 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class))
470 data().HasOnlyCMembers = false;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000471
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000472 // Ignore friends and invalid declarations.
473 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000474 return;
475
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000476 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
477 if (FunTmpl)
478 D = FunTmpl->getTemplatedDecl();
479
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000480 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
481 if (Method->isVirtual()) {
482 // C++ [dcl.init.aggr]p1:
483 // An aggregate is an array or a class with [...] no virtual functions.
484 data().Aggregate = false;
485
486 // C++ [class]p4:
487 // A POD-struct is an aggregate class...
488 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000489
490 // Virtual functions make the class non-empty.
491 // FIXME: Standard ref?
492 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000493
494 // C++ [class.virtual]p1:
495 // A class that declares or inherits a virtual function is called a
496 // polymorphic class.
497 data().Polymorphic = true;
498
Sean Hunt023df372011-05-09 18:22:59 +0000499 // C++0x [class.ctor]p5
500 // A default constructor is trivial [...] if:
501 // -- its class has no virtual functions [...]
502 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000503
504 // C++0x [class.copy]p13:
505 // A copy/move constructor for class X is trivial if [...]
506 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000507 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000508 data().HasTrivialMoveConstructor = false;
509
510 // C++0x [class.copy]p27:
511 // A copy/move assignment operator for class X is trivial if [...]
512 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000513 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000514 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000515
Chandler Carrutha8225442011-04-30 09:17:45 +0000516 // C++0x [class]p7:
517 // A standard-layout class is a class that: [...]
518 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000519 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000520 }
521 }
522
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000523 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000524 // Notify that an implicit member was added after the definition
525 // was completed.
526 if (!isBeingDefined())
527 if (ASTMutationListener *L = getASTMutationListener())
528 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000529
Sean Huntffe37fd2011-05-25 20:50:04 +0000530 // If this is a special member function, note that it was added and then
531 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000532 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith61802452011-12-22 02:22:31 +0000533 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000534 data().DeclaredDefaultConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000535 if (Constructor->isConstexpr()) {
536 data().HasConstexprDefaultConstructor = true;
537 data().HasConstexprNonCopyMoveConstructor = true;
538 }
539 } else if (Constructor->isCopyConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000540 data().DeclaredCopyConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000541 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000542 data().DeclaredMoveConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000543 } else
Sean Huntffe37fd2011-05-25 20:50:04 +0000544 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000545 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000546 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000547 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000548 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000549 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
550 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000551 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000552 else if (Method->isMoveAssignmentOperator())
553 data().DeclaredMoveAssignment = true;
554 else
555 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000556 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000557 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000558
Sean Huntffe37fd2011-05-25 20:50:04 +0000559NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000560 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000561 }
562
563 // Handle (user-declared) constructors.
564 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
565 // Note that we have a user-declared constructor.
566 data().UserDeclaredConstructor = true;
567
Richard Smith017ab772011-09-05 02:13:09 +0000568 // Technically, "user-provided" is only defined for special member
569 // functions, but the intent of the standard is clearly that it should apply
570 // to all functions.
571 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000572
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000573 if (Constructor->isDefaultConstructor()) {
574 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000575 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000576 // C++0x [class.ctor]p5:
577 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000578 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000579 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000580 }
Richard Smith61802452011-12-22 02:22:31 +0000581 if (Constructor->isConstexpr()) {
582 data().HasConstexprDefaultConstructor = true;
583 data().HasConstexprNonCopyMoveConstructor = true;
584 }
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000585 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000586
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000587 // Note when we have a user-declared copy or move constructor, which will
588 // suppress the implicit declaration of those constructors.
589 if (!FunTmpl) {
590 if (Constructor->isCopyConstructor()) {
591 data().UserDeclaredCopyConstructor = true;
592 data().DeclaredCopyConstructor = true;
593
594 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000595 // A copy/move constructor for class X is trivial if it is not
596 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000597 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000598 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000599 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000600 data().UserDeclaredMoveConstructor = true;
601 data().DeclaredMoveConstructor = true;
602
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000603 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000604 // A copy/move constructor for class X is trivial if it is not
605 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000606 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000607 data().HasTrivialMoveConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000608 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000609 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000610 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
611 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000612 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000613 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000614 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000615
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000616 // C++ [dcl.init.aggr]p1:
617 // An aggregate is an array or a class with no user-declared
618 // constructors [...].
619 // C++0x [dcl.init.aggr]p1:
620 // An aggregate is an array or a class with no user-provided
621 // constructors [...].
David Blaikie4e4d0842012-03-11 07:00:24 +0000622 if (!getASTContext().getLangOpts().CPlusPlus0x || UserProvided)
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000623 data().Aggregate = false;
624
625 // C++ [class]p4:
626 // A POD-struct is an aggregate class [...]
627 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
628 // type is technically an aggregate in C++0x since it wouldn't be in 03.
629 data().PlainOldData = false;
630
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000631 return;
632 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000633
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000634 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000635 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000636 data().DeclaredDestructor = true;
637 data().UserDeclaredDestructor = true;
Richard Smithdfefb842012-02-25 07:33:38 +0000638 data().HasIrrelevantDestructor = false;
639
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000640 // C++ [class]p4:
641 // A POD-struct is an aggregate class that has [...] no user-defined
642 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000643 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000644 data().PlainOldData = false;
645
Douglas Gregor45fa5602011-11-07 20:56:01 +0000646 // C++11 [class.dtor]p5:
647 // A destructor is trivial if it is not user-provided and if
648 // -- the destructor is not virtual.
Richard Smithd3861ce2012-06-10 07:07:24 +0000649 if (DD->isUserProvided() || DD->isVirtual())
Sean Huntcf34e752011-05-16 22:41:40 +0000650 data().HasTrivialDestructor = false;
Richard Smithd3861ce2012-06-10 07:07:24 +0000651
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000652 return;
653 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000654
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000655 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000656 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000657 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000658 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000659 // A POD-struct is an aggregate class that [...] has no user-defined
660 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000661 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000662 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000663
Sean Huntffe37fd2011-05-25 20:50:04 +0000664 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000665
Sean Huntffe37fd2011-05-25 20:50:04 +0000666 // Suppress the implicit declaration of a copy constructor.
667 data().UserDeclaredCopyAssignment = true;
668 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000669
Sean Huntffe37fd2011-05-25 20:50:04 +0000670 // C++0x [class.copy]p27:
671 // A copy/move assignment operator for class X is trivial if it is
672 // neither user-provided nor deleted [...]
673 if (Method->isUserProvided())
674 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000675
Sean Huntffe37fd2011-05-25 20:50:04 +0000676 return;
677 }
678
679 if (Method->isMoveAssignmentOperator()) {
680 // This is an extension in C++03 mode, but we'll keep consistency by
681 // taking a move assignment operator to induce non-POD-ness
682 data().PlainOldData = false;
683
684 // This is a move assignment operator.
685 data().UserDeclaredMoveAssignment = true;
686 data().DeclaredMoveAssignment = true;
687
688 // C++0x [class.copy]p27:
689 // A copy/move assignment operator for class X is trivial if it is
690 // neither user-provided nor deleted [...]
691 if (Method->isUserProvided())
692 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000693 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000694
Douglas Gregore80622f2010-09-29 04:25:11 +0000695 // Keep the list of conversion functions up-to-date.
696 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
697 // We don't record specializations.
698 if (Conversion->getPrimaryTemplate())
699 return;
700
701 // FIXME: We intentionally don't use the decl's access here because it
702 // hasn't been set yet. That's really just a misdesign in Sema.
703
704 if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000705 if (FunTmpl->getPreviousDecl())
706 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000707 FunTmpl);
708 else
709 data().Conversions.addDecl(FunTmpl);
710 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000711 if (Conversion->getPreviousDecl())
712 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000713 Conversion);
714 else
715 data().Conversions.addDecl(Conversion);
716 }
717 }
718
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000719 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000720 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000721
722 // Handle non-static data members.
723 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000724 // C++ [class.bit]p2:
725 // A declaration for a bit-field that omits the identifier declares an
726 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
727 // initialized.
728 if (Field->isUnnamedBitfield())
729 return;
730
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000731 // C++ [dcl.init.aggr]p1:
732 // An aggregate is an array or a class (clause 9) with [...] no
733 // private or protected non-static data members (clause 11).
734 //
735 // A POD must be an aggregate.
736 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
737 data().Aggregate = false;
738 data().PlainOldData = false;
739 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000740
741 // C++0x [class]p7:
742 // A standard-layout class is a class that:
743 // [...]
744 // -- has the same access control for all non-static data members,
745 switch (D->getAccess()) {
746 case AS_private: data().HasPrivateFields = true; break;
747 case AS_protected: data().HasProtectedFields = true; break;
748 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000749 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000750 };
751 if ((data().HasPrivateFields + data().HasProtectedFields +
752 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000753 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000754
Douglas Gregor2bb11012011-05-13 01:05:07 +0000755 // Keep track of the presence of mutable fields.
756 if (Field->isMutable())
757 data().HasMutableFields = true;
758
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000759 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000760 // A POD struct is a class that is both a trivial class and a
761 // standard-layout class, and has no non-static data members of type
762 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000763 //
764 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
765 // that does not explicitly have no lifetime makes the class a non-POD.
766 // However, we delay setting PlainOldData to false in this case so that
767 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000768 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000769 // In this case, the class will become a non-POD class when we complete
770 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000771 ASTContext &Context = getASTContext();
772 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000773 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000774 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000775 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
776 setHasObjectMember(true);
777 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000778 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000779
Chandler Carrutha8225442011-04-30 09:17:45 +0000780 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000781 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000782
Chandler Carrutha8225442011-04-30 09:17:45 +0000783 // C++0x [class]p7:
784 // A standard-layout class is a class that:
785 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000786 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000787 }
788
Richard Smith86c3ae42012-02-13 03:54:03 +0000789 // Record if this field is the first non-literal or volatile field or base.
790 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000791 data().HasNonLiteralTypeFieldsOrBases = true;
792
Richard Smith7a614d82011-06-11 17:19:42 +0000793 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000794 data().HasInClassInitializer = true;
795
796 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000797 // A default constructor is trivial if [...] no non-static data member
798 // of its class has a brace-or-equal-initializer.
799 data().HasTrivialDefaultConstructor = false;
800
Richard Smithd079abf2012-05-07 01:07:30 +0000801 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000802 // An aggregate is a [...] class with [...] no
803 // brace-or-equal-initializers for non-static data members.
804 data().Aggregate = false;
805
Richard Smithd079abf2012-05-07 01:07:30 +0000806 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000807 // A POD struct is [...] a trivial class.
808 data().PlainOldData = false;
809 }
810
Douglas Gregor85606eb2010-09-28 20:50:54 +0000811 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
812 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
813 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000814 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000815 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000816 // -- for all the non-static data members of its class that are of
817 // class type (or array thereof), each such class has a trivial
818 // default constructor.
819 if (!FieldRec->hasTrivialDefaultConstructor())
820 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000821
822 // C++0x [class.copy]p13:
823 // A copy/move constructor for class X is trivial if [...]
824 // [...]
825 // -- for each non-static data member of X that is of class type (or
826 // an array thereof), the constructor selected to copy/move that
827 // member is trivial;
828 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000829 if (!FieldRec->hasTrivialCopyConstructor())
830 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000831 if (!FieldRec->hasTrivialMoveConstructor())
832 data().HasTrivialMoveConstructor = false;
833
834 // C++0x [class.copy]p27:
835 // A copy/move assignment operator for class X is trivial if [...]
836 // [...]
837 // -- for each non-static data member of X that is of class type (or
838 // an array thereof), the assignment operator selected to
839 // copy/move that member is trivial;
840 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000841 if (!FieldRec->hasTrivialCopyAssignment())
842 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000843 if (!FieldRec->hasTrivialMoveAssignment())
844 data().HasTrivialMoveAssignment = false;
845
Douglas Gregor85606eb2010-09-28 20:50:54 +0000846 if (!FieldRec->hasTrivialDestructor())
847 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000848 if (!FieldRec->hasIrrelevantDestructor())
849 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000850 if (FieldRec->hasObjectMember())
851 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000852
853 // C++0x [class]p7:
854 // A standard-layout class is a class that:
855 // -- has no non-static data members of type non-standard-layout
856 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000857 if (!FieldRec->isStandardLayout())
858 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000859
860 // C++0x [class]p7:
861 // A standard-layout class is a class that:
862 // [...]
863 // -- has no base classes of the same type as the first non-static
864 // data member.
865 // We don't want to expend bits in the state of the record decl
866 // tracking whether this is the first non-static data member so we
867 // cheat a bit and use some of the existing state: the empty bit.
868 // Virtual bases and virtual methods make a class non-empty, but they
869 // also make it non-standard-layout so we needn't check here.
870 // A non-empty base class may leave the class standard-layout, but not
871 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000872 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000873 // data member must come through here with Empty still true, and Empty
874 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000875 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000876 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
877 BE = bases_end();
878 BI != BE; ++BI) {
879 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000880 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000881 break;
882 }
883 }
884 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000885
886 // Keep track of the presence of mutable fields.
887 if (FieldRec->hasMutableFields())
888 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000889
890 // C++11 [class.copy]p13:
891 // If the implicitly-defined constructor would satisfy the
892 // requirements of a constexpr constructor, the implicitly-defined
893 // constructor is constexpr.
894 // C++11 [dcl.constexpr]p4:
895 // -- every constructor involved in initializing non-static data
896 // members [...] shall be a constexpr constructor
897 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000898 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000899 // The standard requires any in-class initializer to be a constant
900 // expression. We consider this to be a defect.
901 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000902 }
Richard Smith61802452011-12-22 02:22:31 +0000903 } else {
904 // Base element type of field is a non-class type.
Richard Smithd3861ce2012-06-10 07:07:24 +0000905 if (!T->isLiteralType() ||
906 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000907 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000908 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000909
910 // C++0x [class]p7:
911 // A standard-layout class is a class that:
912 // [...]
913 // -- either has no non-static data members in the most derived
914 // class and at most one base class with non-static data members,
915 // or has no base classes with non-static data members, and
916 // At this point we know that we have a non-static data member, so the last
917 // clause holds.
918 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000919 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000920
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000921 // If this is not a zero-length bit-field, then the class is not empty.
922 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000923 if (!Field->isBitField() ||
924 (!Field->getBitWidth()->isTypeDependent() &&
925 !Field->getBitWidth()->isValueDependent() &&
926 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000927 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000928 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000929 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000930
931 // Handle using declarations of conversion functions.
932 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
933 if (Shadow->getDeclName().getNameKind()
934 == DeclarationName::CXXConversionFunctionName)
935 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000936}
937
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000938bool CXXRecordDecl::isCLike() const {
939 if (getTagKind() == TTK_Class || !TemplateOrInstantiation.isNull())
940 return false;
941 if (!hasDefinition())
942 return true;
943
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000944 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000945}
946
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000947void CXXRecordDecl::getCaptureFields(
948 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000949 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000950 Captures.clear();
951 ThisCapture = 0;
952
Douglas Gregorda8962a2012-02-13 15:44:47 +0000953 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000954 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000955 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000956 C != CEnd; ++C, ++Field) {
957 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +0000958 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000959 continue;
960 }
961
David Blaikie581deb32012-06-06 20:45:41 +0000962 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000963 }
964}
965
966
John McCallb05b5f32010-03-15 09:07:48 +0000967static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
968 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000969 if (isa<UsingShadowDecl>(Conv))
970 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000971 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
972 T = ConvTemp->getTemplatedDecl()->getResultType();
973 else
974 T = cast<CXXConversionDecl>(Conv)->getConversionType();
975 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000976}
977
John McCallb05b5f32010-03-15 09:07:48 +0000978/// Collect the visible conversions of a base class.
979///
James Dennetta1253502012-06-15 22:28:09 +0000980/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +0000981/// \param InVirtual whether this base class is a virtual base (or a base
982/// of a virtual base)
983/// \param Access the access along the inheritance path to this base
984/// \param ParentHiddenTypes the conversions provided by the inheritors
985/// of this base
986/// \param Output the set to which to add conversions from non-virtual bases
987/// \param VOutput the set to which to add conversions from virtual bases
988/// \param HiddenVBaseCs the set of conversions which were hidden in a
989/// virtual base along some inheritance path
990static void CollectVisibleConversions(ASTContext &Context,
991 CXXRecordDecl *Record,
992 bool InVirtual,
993 AccessSpecifier Access,
994 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
995 UnresolvedSetImpl &Output,
996 UnresolvedSetImpl &VOutput,
997 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
998 // The set of types which have conversions in this class or its
999 // subclasses. As an optimization, we don't copy the derived set
1000 // unless it might change.
1001 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1002 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1003
1004 // Collect the direct conversions and figure out which conversions
1005 // will be hidden in the subclasses.
1006 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1007 if (!Cs.empty()) {
1008 HiddenTypesBuffer = ParentHiddenTypes;
1009 HiddenTypes = &HiddenTypesBuffer;
1010
1011 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +00001012 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1013 bool Hidden = ParentHiddenTypes.count(ConvType);
1014 if (!Hidden)
1015 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001016
1017 // If this conversion is hidden and we're in a virtual base,
1018 // remember that it's hidden along some inheritance path.
1019 if (Hidden && InVirtual)
1020 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1021
1022 // If this conversion isn't hidden, add it to the appropriate output.
1023 else if (!Hidden) {
1024 AccessSpecifier IAccess
1025 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1026
1027 if (InVirtual)
1028 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001029 else
John McCallb05b5f32010-03-15 09:07:48 +00001030 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001031 }
1032 }
1033 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001034
John McCallb05b5f32010-03-15 09:07:48 +00001035 // Collect information recursively from any base classes.
1036 for (CXXRecordDecl::base_class_iterator
1037 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1038 const RecordType *RT = I->getType()->getAs<RecordType>();
1039 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001040
John McCallb05b5f32010-03-15 09:07:48 +00001041 AccessSpecifier BaseAccess
1042 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1043 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001044
John McCallb05b5f32010-03-15 09:07:48 +00001045 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1046 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1047 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001048 }
John McCallb05b5f32010-03-15 09:07:48 +00001049}
Sebastian Redl9994a342009-10-25 17:03:50 +00001050
John McCallb05b5f32010-03-15 09:07:48 +00001051/// Collect the visible conversions of a class.
1052///
1053/// This would be extremely straightforward if it weren't for virtual
1054/// bases. It might be worth special-casing that, really.
1055static void CollectVisibleConversions(ASTContext &Context,
1056 CXXRecordDecl *Record,
1057 UnresolvedSetImpl &Output) {
1058 // The collection of all conversions in virtual bases that we've
1059 // found. These will be added to the output as long as they don't
1060 // appear in the hidden-conversions set.
1061 UnresolvedSet<8> VBaseCs;
1062
1063 // The set of conversions in virtual bases that we've determined to
1064 // be hidden.
1065 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1066
1067 // The set of types hidden by classes derived from this one.
1068 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1069
1070 // Go ahead and collect the direct conversions and add them to the
1071 // hidden-types set.
1072 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1073 Output.append(Cs.begin(), Cs.end());
1074 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1075 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1076
1077 // Recursively collect conversions from base classes.
1078 for (CXXRecordDecl::base_class_iterator
1079 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1080 const RecordType *RT = I->getType()->getAs<RecordType>();
1081 if (!RT) continue;
1082
1083 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1084 I->isVirtual(), I->getAccessSpecifier(),
1085 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1086 }
1087
1088 // Add any unhidden conversions provided by virtual bases.
1089 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1090 I != E; ++I) {
1091 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1092 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001093 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001094}
1095
1096/// getVisibleConversionFunctions - get all conversion functions visible
1097/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +00001098const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001099 // If root class, all conversions are visible.
1100 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +00001101 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +00001102 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +00001103 if (data().ComputedVisibleConversions)
1104 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +00001105 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001106 data().ComputedVisibleConversions = true;
1107 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001108}
1109
John McCall32daa422010-03-31 01:36:47 +00001110void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1111 // This operation is O(N) but extremely rare. Sema only uses it to
1112 // remove UsingShadowDecls in a class that were followed by a direct
1113 // declaration, e.g.:
1114 // class A : B {
1115 // using B::operator int;
1116 // operator int();
1117 // };
1118 // This is uncommon by itself and even more uncommon in conjunction
1119 // with sufficiently large numbers of directly-declared conversions
1120 // that asymptotic behavior matters.
1121
1122 UnresolvedSetImpl &Convs = *getConversionFunctions();
1123 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1124 if (Convs[I].getDecl() == ConvDecl) {
1125 Convs.erase(I);
1126 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1127 && "conversion was found multiple times in unresolved set");
1128 return;
1129 }
1130 }
1131
1132 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001133}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001134
Douglas Gregorf6b11852009-10-08 15:14:33 +00001135CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001136 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001137 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1138
1139 return 0;
1140}
1141
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001142MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1143 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1144}
1145
Douglas Gregorf6b11852009-10-08 15:14:33 +00001146void
1147CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1148 TemplateSpecializationKind TSK) {
1149 assert(TemplateOrInstantiation.isNull() &&
1150 "Previous template or instantiation?");
1151 assert(!isa<ClassTemplateSpecializationDecl>(this));
1152 TemplateOrInstantiation
1153 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1154}
1155
Anders Carlssonb13e3572009-12-07 06:33:48 +00001156TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1157 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001158 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1159 return Spec->getSpecializationKind();
1160
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001161 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001162 return MSInfo->getTemplateSpecializationKind();
1163
1164 return TSK_Undeclared;
1165}
1166
1167void
1168CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1169 if (ClassTemplateSpecializationDecl *Spec
1170 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1171 Spec->setSpecializationKind(TSK);
1172 return;
1173 }
1174
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001175 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001176 MSInfo->setTemplateSpecializationKind(TSK);
1177 return;
1178 }
1179
David Blaikieb219cfc2011-09-23 05:06:16 +00001180 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001181}
1182
Douglas Gregor1d110e02010-07-01 14:13:13 +00001183CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1184 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001185 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001186
1187 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001188 = Context.DeclarationNames.getCXXDestructorName(
1189 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001190
John McCallc0bf4622010-02-23 00:48:20 +00001191 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001192 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001193 if (I == E)
1194 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001196 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001197 return Dtor;
1198}
1199
Douglas Gregorda2142f2011-02-19 18:51:44 +00001200void CXXRecordDecl::completeDefinition() {
1201 completeDefinition(0);
1202}
1203
1204void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1205 RecordDecl::completeDefinition();
1206
David Blaikie4e4d0842012-03-11 07:00:24 +00001207 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001208 // Objective-C Automatic Reference Counting:
1209 // If a class has a non-static data member of Objective-C pointer
1210 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001211 // default constructor (if any), copy constructor, move constructor,
1212 // copy assignment operator, move assignment operator, and destructor are
1213 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001214 struct DefinitionData &Data = data();
1215 Data.PlainOldData = false;
1216 Data.HasTrivialDefaultConstructor = false;
1217 Data.HasTrivialCopyConstructor = false;
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001218 Data.HasTrivialMoveConstructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001219 Data.HasTrivialCopyAssignment = false;
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001220 Data.HasTrivialMoveAssignment = false;
John McCallf85e1932011-06-15 23:02:42 +00001221 Data.HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +00001222 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001223 }
1224
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001225 // If the class may be abstract (but hasn't been marked as such), check for
1226 // any pure final overriders.
1227 if (mayBeAbstract()) {
1228 CXXFinalOverriderMap MyFinalOverriders;
1229 if (!FinalOverriders) {
1230 getFinalOverriders(MyFinalOverriders);
1231 FinalOverriders = &MyFinalOverriders;
1232 }
1233
1234 bool Done = false;
1235 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1236 MEnd = FinalOverriders->end();
1237 M != MEnd && !Done; ++M) {
1238 for (OverridingMethods::iterator SO = M->second.begin(),
1239 SOEnd = M->second.end();
1240 SO != SOEnd && !Done; ++SO) {
1241 assert(SO->second.size() > 0 &&
1242 "All virtual functions have overridding virtual functions");
1243
1244 // C++ [class.abstract]p4:
1245 // A class is abstract if it contains or inherits at least one
1246 // pure virtual function for which the final overrider is pure
1247 // virtual.
1248 if (SO->second.front().Method->isPure()) {
1249 data().Abstract = true;
1250 Done = true;
1251 break;
1252 }
1253 }
1254 }
1255 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001256
1257 // Set access bits correctly on the directly-declared conversions.
1258 for (UnresolvedSetIterator I = data().Conversions.begin(),
1259 E = data().Conversions.end();
1260 I != E; ++I)
1261 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001262}
1263
1264bool CXXRecordDecl::mayBeAbstract() const {
1265 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1266 isDependentContext())
1267 return false;
1268
1269 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1270 BEnd = bases_end();
1271 B != BEnd; ++B) {
1272 CXXRecordDecl *BaseDecl
1273 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1274 if (BaseDecl->isAbstract())
1275 return true;
1276 }
1277
1278 return false;
1279}
1280
David Blaikie99ba9e32011-12-20 02:48:34 +00001281void CXXMethodDecl::anchor() { }
1282
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001283static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1284 const CXXMethodDecl *BaseMD) {
1285 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1286 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1287 const CXXMethodDecl *MD = *I;
1288 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1289 return true;
1290 if (recursivelyOverrides(MD, BaseMD))
1291 return true;
1292 }
1293 return false;
1294}
1295
1296CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001297CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1298 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001299 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1300 return this;
1301
1302 // Lookup doesn't work for destructors, so handle them separately.
1303 if (isa<CXXDestructorDecl>(this)) {
1304 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001305 if (MD) {
1306 if (recursivelyOverrides(MD, this))
1307 return MD;
1308 if (MayBeBase && recursivelyOverrides(this, MD))
1309 return MD;
1310 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001311 return NULL;
1312 }
1313
1314 lookup_const_result Candidates = RD->lookup(getDeclName());
1315 for (NamedDecl * const * I = Candidates.first; I != Candidates.second; ++I) {
1316 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1317 if (!MD)
1318 continue;
1319 if (recursivelyOverrides(MD, this))
1320 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001321 if (MayBeBase && recursivelyOverrides(this, MD))
1322 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001323 }
1324
1325 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1326 E = RD->bases_end(); I != E; ++I) {
1327 const RecordType *RT = I->getType()->getAs<RecordType>();
1328 if (!RT)
1329 continue;
1330 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1331 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1332 if (T)
1333 return T;
1334 }
1335
1336 return NULL;
1337}
1338
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001339CXXMethodDecl *
1340CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001341 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001342 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001343 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001344 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001345 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001346 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001347 isStatic, SCAsWritten, isInline, isConstexpr,
1348 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001349}
1350
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001351CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1352 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1353 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1354 DeclarationNameInfo(), QualType(),
1355 0, false, SC_None, false, false,
1356 SourceLocation());
1357}
1358
Douglas Gregor90916562009-09-29 18:16:17 +00001359bool CXXMethodDecl::isUsualDeallocationFunction() const {
1360 if (getOverloadedOperator() != OO_Delete &&
1361 getOverloadedOperator() != OO_Array_Delete)
1362 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001363
1364 // C++ [basic.stc.dynamic.deallocation]p2:
1365 // A template instance is never a usual deallocation function,
1366 // regardless of its signature.
1367 if (getPrimaryTemplate())
1368 return false;
1369
Douglas Gregor90916562009-09-29 18:16:17 +00001370 // C++ [basic.stc.dynamic.deallocation]p2:
1371 // If a class T has a member deallocation function named operator delete
1372 // with exactly one parameter, then that function is a usual (non-placement)
1373 // deallocation function. [...]
1374 if (getNumParams() == 1)
1375 return true;
1376
1377 // C++ [basic.stc.dynamic.deallocation]p2:
1378 // [...] If class T does not declare such an operator delete but does
1379 // declare a member deallocation function named operator delete with
1380 // exactly two parameters, the second of which has type std::size_t (18.1),
1381 // then this function is a usual deallocation function.
1382 ASTContext &Context = getASTContext();
1383 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001384 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1385 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001386 return false;
1387
1388 // This function is a usual deallocation function if there are no
1389 // single-parameter deallocation functions of the same kind.
1390 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1391 R.first != R.second; ++R.first) {
1392 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1393 if (FD->getNumParams() == 1)
1394 return false;
1395 }
1396
1397 return true;
1398}
1399
Douglas Gregor06a9f362010-05-01 20:49:11 +00001400bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001401 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001402 // A user-declared copy assignment operator X::operator= is a non-static
1403 // non-template member function of class X with exactly one parameter of
1404 // type X, X&, const X&, volatile X& or const volatile X&.
1405 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1406 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001407 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001408 return false;
1409
1410 QualType ParamType = getParamDecl(0)->getType();
1411 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1412 ParamType = Ref->getPointeeType();
1413
1414 ASTContext &Context = getASTContext();
1415 QualType ClassType
1416 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1417 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1418}
1419
Sean Huntffe37fd2011-05-25 20:50:04 +00001420bool CXXMethodDecl::isMoveAssignmentOperator() const {
1421 // C++0x [class.copy]p19:
1422 // A user-declared move assignment operator X::operator= is a non-static
1423 // non-template member function of class X with exactly one parameter of type
1424 // X&&, const X&&, volatile X&&, or const volatile X&&.
1425 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1426 getPrimaryTemplate() || getDescribedFunctionTemplate())
1427 return false;
1428
1429 QualType ParamType = getParamDecl(0)->getType();
1430 if (!isa<RValueReferenceType>(ParamType))
1431 return false;
1432 ParamType = ParamType->getPointeeType();
1433
1434 ASTContext &Context = getASTContext();
1435 QualType ClassType
1436 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1437 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1438}
1439
Anders Carlsson05eb2442009-05-16 23:58:37 +00001440void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001441 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001442 assert(!MD->getParent()->isDependentContext() &&
1443 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001444 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001445
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001446 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001447}
1448
1449CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001450 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001451 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001452}
1453
1454CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001455 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001456 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001457}
1458
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001459unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001460 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001461 return getASTContext().overridden_methods_size(this);
1462}
1463
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001464QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001465 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1466 // If the member function is declared const, the type of this is const X*,
1467 // if the member function is declared volatile, the type of this is
1468 // volatile X*, and if the member function is declared const volatile,
1469 // the type of this is const volatile X*.
1470
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001471 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001472
John McCall3cb0ebd2010-03-10 03:28:59 +00001473 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001474 ClassTy = C.getQualifiedType(ClassTy,
1475 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001476 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001477}
1478
Eli Friedmand7d7f672009-12-06 20:50:05 +00001479bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001480 // If this function is a template instantiation, look at the template from
1481 // which it was instantiated.
1482 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1483 if (!CheckFn)
1484 CheckFn = this;
1485
Eli Friedmand7d7f672009-12-06 20:50:05 +00001486 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001487 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001488}
1489
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001490bool CXXMethodDecl::isLambdaStaticInvoker() const {
1491 return getParent()->isLambda() &&
1492 getIdentifier() && getIdentifier()->getName() == "__invoke";
1493}
1494
1495
Sean Huntcbb67482011-01-08 20:30:50 +00001496CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1497 TypeSourceInfo *TInfo, bool IsVirtual,
1498 SourceLocation L, Expr *Init,
1499 SourceLocation R,
1500 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001501 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001502 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1503 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001504{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001505}
1506
Sean Huntcbb67482011-01-08 20:30:50 +00001507CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1508 FieldDecl *Member,
1509 SourceLocation MemberLoc,
1510 SourceLocation L, Expr *Init,
1511 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001512 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001513 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001514 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1515{
1516}
1517
Sean Huntcbb67482011-01-08 20:30:50 +00001518CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1519 IndirectFieldDecl *Member,
1520 SourceLocation MemberLoc,
1521 SourceLocation L, Expr *Init,
1522 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001523 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001524 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001525 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001526{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001527}
1528
Sean Huntcbb67482011-01-08 20:30:50 +00001529CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001530 TypeSourceInfo *TInfo,
1531 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001532 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001533 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1534 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001535 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1536{
1537}
1538
1539CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001540 FieldDecl *Member,
1541 SourceLocation MemberLoc,
1542 SourceLocation L, Expr *Init,
1543 SourceLocation R,
1544 VarDecl **Indices,
1545 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001546 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001547 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001548 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001549{
1550 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1551 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1552}
1553
Sean Huntcbb67482011-01-08 20:30:50 +00001554CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1555 FieldDecl *Member,
1556 SourceLocation MemberLoc,
1557 SourceLocation L, Expr *Init,
1558 SourceLocation R,
1559 VarDecl **Indices,
1560 unsigned NumIndices) {
1561 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001562 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001563 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001564 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1565 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001566}
1567
Sean Huntcbb67482011-01-08 20:30:50 +00001568TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001569 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001570 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001571 else
1572 return TypeLoc();
1573}
1574
Sean Huntcbb67482011-01-08 20:30:50 +00001575const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001576 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001577 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001578 else
1579 return 0;
1580}
1581
Sean Huntcbb67482011-01-08 20:30:50 +00001582SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001583 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001584 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001585
1586 if (isInClassMemberInitializer())
1587 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001588
Douglas Gregor76852c22011-11-01 01:16:03 +00001589 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1590 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1591
1592 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001593}
1594
Sean Huntcbb67482011-01-08 20:30:50 +00001595SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001596 if (isInClassMemberInitializer()) {
1597 FieldDecl *D = getAnyMember();
1598 if (Expr *I = D->getInClassInitializer())
1599 return I->getSourceRange();
1600 return SourceRange();
1601 }
1602
Douglas Gregor802ab452009-12-02 22:36:29 +00001603 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001604}
1605
David Blaikie99ba9e32011-12-20 02:48:34 +00001606void CXXConstructorDecl::anchor() { }
1607
Douglas Gregorb48fe382008-10-31 09:07:45 +00001608CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001609CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1610 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1611 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1612 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001613}
1614
1615CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001616CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001617 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001618 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001619 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001620 bool isExplicit, bool isInline,
1621 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001622 assert(NameInfo.getName().getNameKind()
1623 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001624 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001625 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001626 isExplicit, isInline, isImplicitlyDeclared,
1627 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001628}
1629
Douglas Gregor76852c22011-11-01 01:16:03 +00001630CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1631 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1632 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1633 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1634 return Construct->getConstructor();
1635
1636 return 0;
1637}
1638
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001639bool CXXConstructorDecl::isDefaultConstructor() const {
1640 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001641 // A default constructor for a class X is a constructor of class
1642 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001643 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001644 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001645}
1646
Mike Stump1eb44332009-09-09 15:08:12 +00001647bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001648CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001649 return isCopyOrMoveConstructor(TypeQuals) &&
1650 getParamDecl(0)->getType()->isLValueReferenceType();
1651}
1652
1653bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1654 return isCopyOrMoveConstructor(TypeQuals) &&
1655 getParamDecl(0)->getType()->isRValueReferenceType();
1656}
1657
1658/// \brief Determine whether this is a copy or move constructor.
1659bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001660 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001661 // A non-template constructor for class X is a copy constructor
1662 // if its first parameter is of type X&, const X&, volatile X& or
1663 // const volatile X&, and either there are no other parameters
1664 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001665 // C++0x [class.copy]p3:
1666 // A non-template constructor for class X is a move constructor if its
1667 // first parameter is of type X&&, const X&&, volatile X&&, or
1668 // const volatile X&&, and either there are no other parameters or else
1669 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001670 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001671 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001672 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001673 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001674 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001675
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001676 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001677
1678 // Do we have a reference type?
1679 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001680 if (!ParamRefType)
1681 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001682
Douglas Gregorfd476482009-11-13 23:59:09 +00001683 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001684 ASTContext &Context = getASTContext();
1685
Douglas Gregorfd476482009-11-13 23:59:09 +00001686 CanQualType PointeeType
1687 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001688 CanQualType ClassTy
1689 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001690 if (PointeeType.getUnqualifiedType() != ClassTy)
1691 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001692
John McCall0953e762009-09-24 19:53:00 +00001693 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001694
1695 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001696 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001697 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001698}
1699
Anders Carlssonfaccd722009-08-28 16:57:08 +00001700bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001701 // C++ [class.conv.ctor]p1:
1702 // A constructor declared without the function-specifier explicit
1703 // that can be called with a single parameter specifies a
1704 // conversion from the type of its first parameter to the type of
1705 // its class. Such a constructor is called a converting
1706 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001707 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001708 return false;
1709
Mike Stump1eb44332009-09-09 15:08:12 +00001710 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001711 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001712 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001713 (getNumParams() > 1 &&
1714 (getParamDecl(1)->hasDefaultArg() ||
1715 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001716}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001717
Douglas Gregor6493cc52010-11-08 17:16:59 +00001718bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001719 if ((getNumParams() < 1) ||
1720 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1721 (getPrimaryTemplate() == 0) ||
1722 (getDescribedFunctionTemplate() != 0))
1723 return false;
1724
1725 const ParmVarDecl *Param = getParamDecl(0);
1726
1727 ASTContext &Context = getASTContext();
1728 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1729
Douglas Gregor66724ea2009-11-14 01:20:54 +00001730 // Is it the same as our our class type?
1731 CanQualType ClassTy
1732 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1733 if (ParamType.getUnqualifiedType() != ClassTy)
1734 return false;
1735
1736 return true;
1737}
1738
Sebastian Redlf677ea32011-02-05 19:23:19 +00001739const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1740 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001741 method_iterator It = getASTContext().overridden_methods_begin(this);
1742 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001743 return 0;
1744
1745 return cast<CXXConstructorDecl>(*It);
1746}
1747
1748void
1749CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1750 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001751 assert(getASTContext().overridden_methods_size(this) == 0 &&
1752 "Base ctor already set.");
1753 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001754}
1755
David Blaikie99ba9e32011-12-20 02:48:34 +00001756void CXXDestructorDecl::anchor() { }
1757
Douglas Gregor42a552f2008-11-05 20:51:48 +00001758CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001759CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1760 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1761 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001762 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001763}
1764
1765CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001766CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001767 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001768 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001769 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001770 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001771 assert(NameInfo.getName().getNameKind()
1772 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001773 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001774 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001775 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001776}
1777
David Blaikie99ba9e32011-12-20 02:48:34 +00001778void CXXConversionDecl::anchor() { }
1779
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001780CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001781CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1782 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1783 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1784 QualType(), 0, false, false, false,
1785 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001786}
1787
1788CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001789CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001790 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001791 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001792 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001793 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001794 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001795 assert(NameInfo.getName().getNameKind()
1796 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001797 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001798 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001799 isInline, isExplicit, isConstexpr,
1800 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001801}
1802
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001803bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1804 return isImplicit() && getParent()->isLambda() &&
1805 getConversionType()->isBlockPointerType();
1806}
1807
David Blaikie99ba9e32011-12-20 02:48:34 +00001808void LinkageSpecDecl::anchor() { }
1809
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001810LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001811 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001812 SourceLocation ExternLoc,
1813 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001814 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001815 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001816 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001817}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001818
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001819LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1820 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1821 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1822 lang_c, SourceLocation());
1823}
1824
David Blaikie99ba9e32011-12-20 02:48:34 +00001825void UsingDirectiveDecl::anchor() { }
1826
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001827UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1828 SourceLocation L,
1829 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001830 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001831 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001832 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001833 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001834 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1835 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001836 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1837 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001838}
1839
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001840UsingDirectiveDecl *
1841UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1842 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1843 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1844 NestedNameSpecifierLoc(),
1845 SourceLocation(), 0, 0);
1846}
1847
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001848NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1849 if (NamespaceAliasDecl *NA =
1850 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1851 return NA->getNamespace();
1852 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1853}
1854
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001855void NamespaceDecl::anchor() { }
1856
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001857NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1858 SourceLocation StartLoc,
1859 SourceLocation IdLoc, IdentifierInfo *Id,
1860 NamespaceDecl *PrevDecl)
1861 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1862 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1863{
1864 setPreviousDeclaration(PrevDecl);
1865
1866 if (PrevDecl)
1867 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1868}
1869
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001870NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001871 bool Inline, SourceLocation StartLoc,
1872 SourceLocation IdLoc, IdentifierInfo *Id,
1873 NamespaceDecl *PrevDecl) {
1874 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001875}
1876
1877NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1878 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001879 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1880 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001881}
1882
David Blaikie99ba9e32011-12-20 02:48:34 +00001883void NamespaceAliasDecl::anchor() { }
1884
Mike Stump1eb44332009-09-09 15:08:12 +00001885NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001886 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001887 SourceLocation AliasLoc,
1888 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001889 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001890 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001891 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001892 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1893 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001894 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1895 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001896}
1897
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001898NamespaceAliasDecl *
1899NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1900 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1901 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1902 NestedNameSpecifierLoc(),
1903 SourceLocation(), 0);
1904}
1905
David Blaikie99ba9e32011-12-20 02:48:34 +00001906void UsingShadowDecl::anchor() { }
1907
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001908UsingShadowDecl *
1909UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1910 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1911 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1912}
1913
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001914UsingDecl *UsingShadowDecl::getUsingDecl() const {
1915 const UsingShadowDecl *Shadow = this;
1916 while (const UsingShadowDecl *NextShadow =
1917 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1918 Shadow = NextShadow;
1919 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1920}
1921
David Blaikie99ba9e32011-12-20 02:48:34 +00001922void UsingDecl::anchor() { }
1923
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001924void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1925 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1926 "declaration already in set");
1927 assert(S->getUsingDecl() == this);
1928
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001929 if (FirstUsingShadow.getPointer())
1930 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1931 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001932}
1933
1934void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1935 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1936 "declaration not in set");
1937 assert(S->getUsingDecl() == this);
1938
1939 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1940
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001941 if (FirstUsingShadow.getPointer() == S) {
1942 FirstUsingShadow.setPointer(
1943 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001944 S->UsingOrNextShadow = this;
1945 return;
1946 }
1947
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001948 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001949 while (Prev->UsingOrNextShadow != S)
1950 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1951 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1952 S->UsingOrNextShadow = this;
1953}
1954
Douglas Gregordc355712011-02-25 00:36:19 +00001955UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1956 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001957 const DeclarationNameInfo &NameInfo,
1958 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001959 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001960}
1961
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001962UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1963 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1964 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1965 DeclarationNameInfo(), false);
1966}
1967
David Blaikie99ba9e32011-12-20 02:48:34 +00001968void UnresolvedUsingValueDecl::anchor() { }
1969
John McCall7ba107a2009-11-18 02:36:19 +00001970UnresolvedUsingValueDecl *
1971UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1972 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001973 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001974 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001975 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001976 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001977}
1978
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001979UnresolvedUsingValueDecl *
1980UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1981 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1982 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1983 NestedNameSpecifierLoc(),
1984 DeclarationNameInfo());
1985}
1986
David Blaikie99ba9e32011-12-20 02:48:34 +00001987void UnresolvedUsingTypenameDecl::anchor() { }
1988
John McCall7ba107a2009-11-18 02:36:19 +00001989UnresolvedUsingTypenameDecl *
1990UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1991 SourceLocation UsingLoc,
1992 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001993 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001994 SourceLocation TargetNameLoc,
1995 DeclarationName TargetName) {
1996 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001997 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001998 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001999}
2000
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002001UnresolvedUsingTypenameDecl *
2002UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2003 void *Mem = AllocateDeserializedDecl(C, ID,
2004 sizeof(UnresolvedUsingTypenameDecl));
2005 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2006 SourceLocation(),
2007 NestedNameSpecifierLoc(),
2008 SourceLocation(),
2009 0);
2010}
2011
David Blaikie99ba9e32011-12-20 02:48:34 +00002012void StaticAssertDecl::anchor() { }
2013
Anders Carlssonfb311762009-03-14 00:25:26 +00002014StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002015 SourceLocation StaticAssertLoc,
2016 Expr *AssertExpr,
2017 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002018 SourceLocation RParenLoc,
2019 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002020 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002021 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00002022}
2023
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002024StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2025 unsigned ID) {
2026 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00002027 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
2028 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002029}
2030
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002031static const char *getAccessName(AccessSpecifier AS) {
2032 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002033 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002034 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002035 case AS_public:
2036 return "public";
2037 case AS_private:
2038 return "private";
2039 case AS_protected:
2040 return "protected";
2041 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002042 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002043}
2044
2045const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2046 AccessSpecifier AS) {
2047 return DB << getAccessName(AS);
2048}
Richard Smithf15fda02012-02-02 01:16:57 +00002049
2050const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2051 AccessSpecifier AS) {
2052 return DB << getAccessName(AS);
2053}