blob: 9db33357a6099f33af030af82ccb3a9f0faae3ed [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,
Eli Friedman8da8a662012-09-19 01:18:11 +000093 TypeSourceInfo *Info, SourceLocation Loc,
94 bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +000095 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
96 0, 0);
97 R->IsBeingDefined = true;
Eli Friedman8da8a662012-09-19 01:18:11 +000098 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info, Dependent);
Douglas Gregorda8962a2012-02-13 15:44:47 +000099 C.getTypeDeclType(R, /*PrevDecl=*/0);
100 return R;
101}
102
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000103CXXRecordDecl *
104CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
105 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
106 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
107 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000108}
109
Mike Stump1eb44332009-09-09 15:08:12 +0000110void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000111CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000112 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000113 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000114
Douglas Gregor7c789c12010-10-29 22:39:52 +0000115 if (!data().Bases.isOffset() && data().NumBases > 0)
116 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Richard Smithdd677232011-10-18 20:08:55 +0000118 if (NumBases) {
119 // C++ [dcl.init.aggr]p1:
120 // An aggregate is [...] a class with [...] no base classes [...].
121 data().Aggregate = false;
122
123 // C++ [class]p4:
124 // A POD-struct is an aggregate class...
125 data().PlainOldData = false;
126 }
127
Anders Carlsson6f6de732010-03-29 05:13:12 +0000128 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000129 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000130
131 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000132 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000133
John McCall86ff3082010-02-04 22:26:26 +0000134 data().Bases = new(C) CXXBaseSpecifier [NumBases];
135 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000136 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000137 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000138 // Keep track of inherited vbases for this base class.
139 const CXXBaseSpecifier *Base = Bases[i];
140 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000141 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000142 if (BaseType->isDependentType())
143 continue;
144 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000145 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000146
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000147 // A class with a non-empty base class is not empty.
148 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000149 if (!BaseClassDecl->isEmpty()) {
150 if (!data().Empty) {
151 // C++0x [class]p7:
152 // A standard-layout class is a class that:
153 // [...]
154 // -- either has no non-static data members in the most derived
155 // class and at most one base class with non-static data members,
156 // or has no base classes with non-static data members, and
157 // If this is the second non-empty base, then neither of these two
158 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000159 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000160 }
161
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000162 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000163 data().HasNoNonEmptyBases = false;
164 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000165
Douglas Gregor85606eb2010-09-28 20:50:54 +0000166 // C++ [class.virtual]p1:
167 // A class that declares or inherits a virtual function is called a
168 // polymorphic class.
169 if (BaseClassDecl->isPolymorphic())
170 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000171
Chandler Carrutha8225442011-04-30 09:17:45 +0000172 // C++0x [class]p7:
173 // A standard-layout class is a class that: [...]
174 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000175 if (!BaseClassDecl->isStandardLayout())
176 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000177
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000178 // Record if this base is the first non-literal field or base.
179 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
180 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000181
Anders Carlsson6f6de732010-03-29 05:13:12 +0000182 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000183 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000184 BaseClassDecl->vbases_begin(),
185 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000186 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000187 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000188 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000189 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000190
191 if (Base->isVirtual()) {
192 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000193 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000194 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000195
196 // C++0x [meta.unary.prop] is_empty:
197 // T is a class type, but not a union type, with ... no virtual base
198 // classes
199 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000200
201 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000202 // A default constructor is trivial [...] if:
203 // -- its class has [...] no virtual bases
204 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000205
206 // C++0x [class.copy]p13:
207 // A copy/move constructor for class X is trivial if it is neither
208 // user-provided nor deleted and if
209 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000210 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000211 data().HasTrivialMoveConstructor = false;
212
213 // C++0x [class.copy]p27:
214 // A copy/move assignment operator for class X is trivial if it is
215 // neither user-provided nor deleted and if
216 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000217 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000218 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000219
220 // C++0x [class]p7:
221 // A standard-layout class is a class that: [...]
222 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000223 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000224
225 // C++11 [dcl.constexpr]p4:
226 // In the definition of a constexpr constructor [...]
227 // -- the class shall not have any virtual base classes
228 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000229 } else {
230 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000231 // A default constructor is trivial [...] if:
232 // -- all the direct base classes of its class have trivial default
233 // constructors.
234 if (!BaseClassDecl->hasTrivialDefaultConstructor())
235 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000236
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000237 // C++0x [class.copy]p13:
238 // A copy/move constructor for class X is trivial if [...]
239 // [...]
240 // -- the constructor selected to copy/move each direct base class
241 // subobject is trivial, and
242 // FIXME: C++0x: We need to only consider the selected constructor
243 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000244 if (!BaseClassDecl->hasTrivialCopyConstructor())
245 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000246 if (!BaseClassDecl->hasTrivialMoveConstructor())
247 data().HasTrivialMoveConstructor = false;
248
249 // C++0x [class.copy]p27:
250 // A copy/move assignment operator for class X is trivial if [...]
251 // [...]
252 // -- the assignment operator selected to copy/move each direct base
253 // class subobject is trivial, and
254 // FIXME: C++0x: We need to only consider the selected operator instead
255 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000256 if (!BaseClassDecl->hasTrivialCopyAssignment())
257 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000258 if (!BaseClassDecl->hasTrivialMoveAssignment())
259 data().HasTrivialMoveAssignment = false;
Richard Smith61802452011-12-22 02:22:31 +0000260
261 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000262 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000263 // requirements of a constexpr constructor, the implicitly-defined
264 // default constructor is constexpr.
265 if (!BaseClassDecl->hasConstexprDefaultConstructor())
266 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000267 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000268
269 // C++ [class.ctor]p3:
270 // A destructor is trivial if all the direct base classes of its class
271 // have trivial destructors.
272 if (!BaseClassDecl->hasTrivialDestructor())
273 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000274
275 if (!BaseClassDecl->hasIrrelevantDestructor())
276 data().HasIrrelevantDestructor = false;
277
John McCallf85e1932011-06-15 23:02:42 +0000278 // A class has an Objective-C object member if... or any of its bases
279 // has an Objective-C object member.
280 if (BaseClassDecl->hasObjectMember())
281 setHasObjectMember(true);
282
Douglas Gregor2bb11012011-05-13 01:05:07 +0000283 // Keep track of the presence of mutable fields.
284 if (BaseClassDecl->hasMutableFields())
285 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000286 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000287
288 if (VBases.empty())
289 return;
290
291 // Create base specifier for any direct or indirect virtual bases.
292 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
293 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000294 for (int I = 0, E = VBases.size(); I != E; ++I)
295 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000296}
297
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000298/// Callback function for CXXRecordDecl::forallBases that acknowledges
299/// that it saw a base class.
300static bool SawBase(const CXXRecordDecl *, void *) {
301 return true;
302}
303
304bool CXXRecordDecl::hasAnyDependentBases() const {
305 if (!isDependentContext())
306 return false;
307
308 return !forallBases(SawBase, 0);
309}
310
Sean Huntffe37fd2011-05-25 20:50:04 +0000311bool CXXRecordDecl::hasConstCopyConstructor() const {
312 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000313}
314
Chandler Carruthb7e95892011-04-23 10:47:28 +0000315bool CXXRecordDecl::isTriviallyCopyable() const {
316 // C++0x [class]p5:
317 // A trivially copyable class is a class that:
318 // -- has no non-trivial copy constructors,
319 if (!hasTrivialCopyConstructor()) return false;
320 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000321 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000322 // -- has no non-trivial copy assignment operators,
323 if (!hasTrivialCopyAssignment()) return false;
324 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000325 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000326 // -- has a trivial destructor.
327 if (!hasTrivialDestructor()) return false;
328
329 return true;
330}
331
Douglas Gregor0d405db2010-07-01 20:59:04 +0000332/// \brief Perform a simplistic form of overload resolution that only considers
333/// cv-qualifiers on a single parameter, and return the best overload candidate
334/// (if there is one).
335static CXXMethodDecl *
336GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000337 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000338 if (Cands.empty())
339 return 0;
340 if (Cands.size() == 1)
341 return Cands[0].first;
342
343 unsigned Best = 0, N = Cands.size();
344 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000345 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000346 Best = I;
347
Benjamin Kramer2cd7f412012-07-30 15:53:26 +0000348 for (unsigned I = 0; I != N; ++I)
349 if (I != Best && Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000350 return 0;
351
352 return Cands[Best].first;
353}
354
Sean Huntffe37fd2011-05-25 20:50:04 +0000355CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
356 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000357 QualType ClassType
358 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000359 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000360 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000361 Context.getCanonicalType(ClassType));
362 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000363 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000364 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000365 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000366 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000367 // C++ [class.copy]p2:
368 // A non-template constructor for class X is a copy constructor if [...]
369 if (isa<FunctionTemplateDecl>(*Con))
370 continue;
371
Douglas Gregor0d405db2010-07-01 20:59:04 +0000372 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
373 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000374 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
375 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000376 Found.push_back(std::make_pair(
377 const_cast<CXXConstructorDecl *>(Constructor),
378 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000379 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000380 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000381
382 return cast_or_null<CXXConstructorDecl>(
383 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000384}
385
Sean Huntffe37fd2011-05-25 20:50:04 +0000386CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
387 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
388 if (I->isMoveConstructor())
David Blaikie581deb32012-06-06 20:45:41 +0000389 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000390
391 return 0;
392}
393
Douglas Gregorb87786f2010-07-01 17:48:08 +0000394CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
395 ASTContext &Context = getASTContext();
396 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
397 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
398
Chris Lattner5f9e2722011-07-23 10:55:15 +0000399 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000400 DeclContext::lookup_const_iterator Op, OpEnd;
401 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
402 // C++ [class.copy]p9:
403 // A user-declared copy assignment operator is a non-static non-template
404 // member function of class X with exactly one parameter of type X, X&,
405 // const X&, volatile X& or const volatile X&.
406 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
407 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
408 continue;
409
410 const FunctionProtoType *FnType
411 = Method->getType()->getAs<FunctionProtoType>();
412 assert(FnType && "Overloaded operator has no prototype.");
413 // Don't assert on this; an invalid decl might have been left in the AST.
414 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
415 continue;
416
417 QualType ArgType = FnType->getArgType(0);
418 Qualifiers Quals;
419 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
420 ArgType = Ref->getPointeeType();
421 // If we have a const argument and we have a reference to a non-const,
422 // this function does not match.
423 if (ArgIsConst && !ArgType.isConstQualified())
424 continue;
425
426 Quals = ArgType.getQualifiers();
427 } else {
428 // By-value copy-assignment operators are treated like const X&
429 // copy-assignment operators.
430 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
431 }
432
433 if (!Context.hasSameUnqualifiedType(ArgType, Class))
434 continue;
435
436 // Save this copy-assignment operator. It might be "the one".
437 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
438 }
439
440 // Use a simplistic form of overload resolution to find the candidate.
441 return GetBestOverloadCandidateSimple(Found);
442}
443
Sean Huntffe37fd2011-05-25 20:50:04 +0000444CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
445 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
446 if (I->isMoveAssignmentOperator())
David Blaikie581deb32012-06-06 20:45:41 +0000447 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000448
449 return 0;
450}
451
Douglas Gregor21386642010-09-28 21:55:22 +0000452void CXXRecordDecl::markedVirtualFunctionPure() {
453 // C++ [class.abstract]p2:
454 // A class is abstract if it has at least one pure virtual function.
455 data().Abstract = true;
456}
457
Richard Smith3f5f5582012-06-08 21:09:22 +0000458void CXXRecordDecl::markedConstructorConstexpr(CXXConstructorDecl *CD) {
Richard Smithd3861ce2012-06-10 07:07:24 +0000459 if (!CD->isCopyOrMoveConstructor())
Richard Smith3f5f5582012-06-08 21:09:22 +0000460 data().HasConstexprNonCopyMoveConstructor = true;
461
462 if (CD->isDefaultConstructor())
463 data().HasConstexprDefaultConstructor = true;
464}
465
Douglas Gregor21386642010-09-28 21:55:22 +0000466void CXXRecordDecl::addedMember(Decl *D) {
Joao Matos17d35c32012-08-31 22:18:20 +0000467 if (!D->isImplicit() &&
468 !isa<FieldDecl>(D) &&
469 !isa<IndirectFieldDecl>(D) &&
470 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
471 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
472 data().HasOnlyCMembers = false;
473
474 // Ignore friends and invalid declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000475 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000476 return;
477
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000478 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
479 if (FunTmpl)
480 D = FunTmpl->getTemplatedDecl();
481
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000482 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
483 if (Method->isVirtual()) {
484 // C++ [dcl.init.aggr]p1:
485 // An aggregate is an array or a class with [...] no virtual functions.
486 data().Aggregate = false;
487
488 // C++ [class]p4:
489 // A POD-struct is an aggregate class...
490 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000491
492 // Virtual functions make the class non-empty.
493 // FIXME: Standard ref?
494 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000495
496 // C++ [class.virtual]p1:
497 // A class that declares or inherits a virtual function is called a
498 // polymorphic class.
499 data().Polymorphic = true;
500
Sean Hunt023df372011-05-09 18:22:59 +0000501 // C++0x [class.ctor]p5
502 // A default constructor is trivial [...] if:
503 // -- its class has no virtual functions [...]
504 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000505
506 // C++0x [class.copy]p13:
507 // A copy/move constructor for class X is trivial if [...]
508 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000509 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000510 data().HasTrivialMoveConstructor = false;
511
512 // C++0x [class.copy]p27:
513 // A copy/move assignment operator for class X is trivial if [...]
514 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000515 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000516 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000517
Chandler Carrutha8225442011-04-30 09:17:45 +0000518 // C++0x [class]p7:
519 // A standard-layout class is a class that: [...]
520 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000521 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000522 }
523 }
524
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000525 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000526 // Notify that an implicit member was added after the definition
527 // was completed.
528 if (!isBeingDefined())
529 if (ASTMutationListener *L = getASTMutationListener())
530 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000531
Sean Huntffe37fd2011-05-25 20:50:04 +0000532 // If this is a special member function, note that it was added and then
533 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000534 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith61802452011-12-22 02:22:31 +0000535 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000536 data().DeclaredDefaultConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000537 if (Constructor->isConstexpr()) {
538 data().HasConstexprDefaultConstructor = true;
539 data().HasConstexprNonCopyMoveConstructor = true;
540 }
541 } else if (Constructor->isCopyConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000542 data().DeclaredCopyConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000543 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000544 data().DeclaredMoveConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000545 } else
Sean Huntffe37fd2011-05-25 20:50:04 +0000546 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000547 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000548 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000549 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000550 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000551 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
552 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000553 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000554 else if (Method->isMoveAssignmentOperator())
555 data().DeclaredMoveAssignment = true;
556 else
557 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000558 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000559 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000560
Sean Huntffe37fd2011-05-25 20:50:04 +0000561NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000562 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000563 }
564
565 // Handle (user-declared) constructors.
566 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
567 // Note that we have a user-declared constructor.
568 data().UserDeclaredConstructor = true;
569
Richard Smith017ab772011-09-05 02:13:09 +0000570 // Technically, "user-provided" is only defined for special member
571 // functions, but the intent of the standard is clearly that it should apply
572 // to all functions.
573 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000574
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000575 if (Constructor->isDefaultConstructor()) {
576 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000577 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000578 // C++0x [class.ctor]p5:
579 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000580 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000581 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000582 }
Richard Smith61802452011-12-22 02:22:31 +0000583 if (Constructor->isConstexpr()) {
584 data().HasConstexprDefaultConstructor = true;
585 data().HasConstexprNonCopyMoveConstructor = true;
586 }
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000587 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000588
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000589 // Note when we have a user-declared copy or move constructor, which will
590 // suppress the implicit declaration of those constructors.
591 if (!FunTmpl) {
592 if (Constructor->isCopyConstructor()) {
593 data().UserDeclaredCopyConstructor = true;
594 data().DeclaredCopyConstructor = true;
595
596 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000597 // A copy/move constructor for class X is trivial if it is not
598 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000599 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000600 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000601 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000602 data().UserDeclaredMoveConstructor = true;
603 data().DeclaredMoveConstructor = true;
604
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000605 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000606 // A copy/move constructor for class X is trivial if it is not
607 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000608 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000609 data().HasTrivialMoveConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000610 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000611 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000612 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
613 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000614 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000615 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000616 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000617
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000618 // C++ [dcl.init.aggr]p1:
619 // An aggregate is an array or a class with no user-declared
620 // constructors [...].
621 // C++0x [dcl.init.aggr]p1:
622 // An aggregate is an array or a class with no user-provided
623 // constructors [...].
David Blaikie4e4d0842012-03-11 07:00:24 +0000624 if (!getASTContext().getLangOpts().CPlusPlus0x || UserProvided)
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000625 data().Aggregate = false;
626
627 // C++ [class]p4:
628 // A POD-struct is an aggregate class [...]
629 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
630 // type is technically an aggregate in C++0x since it wouldn't be in 03.
631 data().PlainOldData = false;
632
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000633 return;
634 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000635
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000636 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000637 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000638 data().DeclaredDestructor = true;
639 data().UserDeclaredDestructor = true;
Richard Smithdfefb842012-02-25 07:33:38 +0000640 data().HasIrrelevantDestructor = false;
641
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000642 // C++ [class]p4:
643 // A POD-struct is an aggregate class that has [...] no user-defined
644 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000645 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000646 data().PlainOldData = false;
647
Douglas Gregor45fa5602011-11-07 20:56:01 +0000648 // C++11 [class.dtor]p5:
649 // A destructor is trivial if it is not user-provided and if
650 // -- the destructor is not virtual.
Richard Smithd3861ce2012-06-10 07:07:24 +0000651 if (DD->isUserProvided() || DD->isVirtual())
Sean Huntcf34e752011-05-16 22:41:40 +0000652 data().HasTrivialDestructor = false;
Richard Smithd3861ce2012-06-10 07:07:24 +0000653
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000654 return;
655 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000656
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000657 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000658 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000659 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000660 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000661 // A POD-struct is an aggregate class that [...] has no user-defined
662 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000663 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000664 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000665
Sean Huntffe37fd2011-05-25 20:50:04 +0000666 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000667
Sean Huntffe37fd2011-05-25 20:50:04 +0000668 // Suppress the implicit declaration of a copy constructor.
669 data().UserDeclaredCopyAssignment = true;
670 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000671
Sean Huntffe37fd2011-05-25 20:50:04 +0000672 // C++0x [class.copy]p27:
673 // A copy/move assignment operator for class X is trivial if it is
674 // neither user-provided nor deleted [...]
675 if (Method->isUserProvided())
676 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000677
Sean Huntffe37fd2011-05-25 20:50:04 +0000678 return;
679 }
680
681 if (Method->isMoveAssignmentOperator()) {
682 // This is an extension in C++03 mode, but we'll keep consistency by
683 // taking a move assignment operator to induce non-POD-ness
684 data().PlainOldData = false;
685
686 // This is a move assignment operator.
687 data().UserDeclaredMoveAssignment = true;
688 data().DeclaredMoveAssignment = true;
689
690 // C++0x [class.copy]p27:
691 // A copy/move assignment operator for class X is trivial if it is
692 // neither user-provided nor deleted [...]
693 if (Method->isUserProvided())
694 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000695 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000696
Douglas Gregore80622f2010-09-29 04:25:11 +0000697 // Keep the list of conversion functions up-to-date.
698 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
699 // We don't record specializations.
700 if (Conversion->getPrimaryTemplate())
701 return;
702
703 // FIXME: We intentionally don't use the decl's access here because it
704 // hasn't been set yet. That's really just a misdesign in Sema.
705
706 if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000707 if (FunTmpl->getPreviousDecl())
708 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000709 FunTmpl);
710 else
711 data().Conversions.addDecl(FunTmpl);
712 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000713 if (Conversion->getPreviousDecl())
714 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000715 Conversion);
716 else
717 data().Conversions.addDecl(Conversion);
718 }
719 }
720
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000721 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000722 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000723
724 // Handle non-static data members.
725 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000726 // C++ [class.bit]p2:
727 // A declaration for a bit-field that omits the identifier declares an
728 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
729 // initialized.
730 if (Field->isUnnamedBitfield())
731 return;
732
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000733 // C++ [dcl.init.aggr]p1:
734 // An aggregate is an array or a class (clause 9) with [...] no
735 // private or protected non-static data members (clause 11).
736 //
737 // A POD must be an aggregate.
738 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
739 data().Aggregate = false;
740 data().PlainOldData = false;
741 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000742
743 // C++0x [class]p7:
744 // A standard-layout class is a class that:
745 // [...]
746 // -- has the same access control for all non-static data members,
747 switch (D->getAccess()) {
748 case AS_private: data().HasPrivateFields = true; break;
749 case AS_protected: data().HasProtectedFields = true; break;
750 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000751 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000752 };
753 if ((data().HasPrivateFields + data().HasProtectedFields +
754 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000755 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000756
Douglas Gregor2bb11012011-05-13 01:05:07 +0000757 // Keep track of the presence of mutable fields.
758 if (Field->isMutable())
759 data().HasMutableFields = true;
760
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000761 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000762 // A POD struct is a class that is both a trivial class and a
763 // standard-layout class, and has no non-static data members of type
764 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000765 //
766 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
767 // that does not explicitly have no lifetime makes the class a non-POD.
768 // However, we delay setting PlainOldData to false in this case so that
769 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000770 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000771 // In this case, the class will become a non-POD class when we complete
772 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000773 ASTContext &Context = getASTContext();
774 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000775 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000776 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000777 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
778 setHasObjectMember(true);
779 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000780 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000781
Chandler Carrutha8225442011-04-30 09:17:45 +0000782 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000783 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000784
Chandler Carrutha8225442011-04-30 09:17:45 +0000785 // C++0x [class]p7:
786 // A standard-layout class is a class that:
787 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000788 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000789 }
790
Richard Smith86c3ae42012-02-13 03:54:03 +0000791 // Record if this field is the first non-literal or volatile field or base.
792 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000793 data().HasNonLiteralTypeFieldsOrBases = true;
794
Richard Smith7a614d82011-06-11 17:19:42 +0000795 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000796 data().HasInClassInitializer = true;
797
798 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000799 // A default constructor is trivial if [...] no non-static data member
800 // of its class has a brace-or-equal-initializer.
801 data().HasTrivialDefaultConstructor = false;
802
Richard Smithd079abf2012-05-07 01:07:30 +0000803 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000804 // An aggregate is a [...] class with [...] no
805 // brace-or-equal-initializers for non-static data members.
806 data().Aggregate = false;
807
Richard Smithd079abf2012-05-07 01:07:30 +0000808 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000809 // A POD struct is [...] a trivial class.
810 data().PlainOldData = false;
811 }
812
Douglas Gregor85606eb2010-09-28 20:50:54 +0000813 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
814 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
815 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000816 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000817 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000818 // -- for all the non-static data members of its class that are of
819 // class type (or array thereof), each such class has a trivial
820 // default constructor.
821 if (!FieldRec->hasTrivialDefaultConstructor())
822 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000823
824 // C++0x [class.copy]p13:
825 // A copy/move constructor for class X is trivial if [...]
826 // [...]
827 // -- for each non-static data member of X that is of class type (or
828 // an array thereof), the constructor selected to copy/move that
829 // member is trivial;
830 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000831 if (!FieldRec->hasTrivialCopyConstructor())
832 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000833 if (!FieldRec->hasTrivialMoveConstructor())
834 data().HasTrivialMoveConstructor = false;
835
836 // C++0x [class.copy]p27:
837 // A copy/move assignment operator for class X is trivial if [...]
838 // [...]
839 // -- for each non-static data member of X that is of class type (or
840 // an array thereof), the assignment operator selected to
841 // copy/move that member is trivial;
842 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000843 if (!FieldRec->hasTrivialCopyAssignment())
844 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000845 if (!FieldRec->hasTrivialMoveAssignment())
846 data().HasTrivialMoveAssignment = false;
847
Douglas Gregor85606eb2010-09-28 20:50:54 +0000848 if (!FieldRec->hasTrivialDestructor())
849 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000850 if (!FieldRec->hasIrrelevantDestructor())
851 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000852 if (FieldRec->hasObjectMember())
853 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000854
855 // C++0x [class]p7:
856 // A standard-layout class is a class that:
857 // -- has no non-static data members of type non-standard-layout
858 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000859 if (!FieldRec->isStandardLayout())
860 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000861
862 // C++0x [class]p7:
863 // A standard-layout class is a class that:
864 // [...]
865 // -- has no base classes of the same type as the first non-static
866 // data member.
867 // We don't want to expend bits in the state of the record decl
868 // tracking whether this is the first non-static data member so we
869 // cheat a bit and use some of the existing state: the empty bit.
870 // Virtual bases and virtual methods make a class non-empty, but they
871 // also make it non-standard-layout so we needn't check here.
872 // A non-empty base class may leave the class standard-layout, but not
873 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000874 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000875 // data member must come through here with Empty still true, and Empty
876 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000877 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000878 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
879 BE = bases_end();
880 BI != BE; ++BI) {
881 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000882 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000883 break;
884 }
885 }
886 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000887
888 // Keep track of the presence of mutable fields.
889 if (FieldRec->hasMutableFields())
890 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000891
892 // C++11 [class.copy]p13:
893 // If the implicitly-defined constructor would satisfy the
894 // requirements of a constexpr constructor, the implicitly-defined
895 // constructor is constexpr.
896 // C++11 [dcl.constexpr]p4:
897 // -- every constructor involved in initializing non-static data
898 // members [...] shall be a constexpr constructor
899 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000900 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000901 // The standard requires any in-class initializer to be a constant
902 // expression. We consider this to be a defect.
903 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000904 }
Richard Smith61802452011-12-22 02:22:31 +0000905 } else {
906 // Base element type of field is a non-class type.
Richard Smithd3861ce2012-06-10 07:07:24 +0000907 if (!T->isLiteralType() ||
908 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000909 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000910 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000911
912 // C++0x [class]p7:
913 // A standard-layout class is a class that:
914 // [...]
915 // -- either has no non-static data members in the most derived
916 // class and at most one base class with non-static data members,
917 // or has no base classes with non-static data members, and
918 // At this point we know that we have a non-static data member, so the last
919 // clause holds.
920 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000921 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000922
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000923 // If this is not a zero-length bit-field, then the class is not empty.
924 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000925 if (!Field->isBitField() ||
926 (!Field->getBitWidth()->isTypeDependent() &&
927 !Field->getBitWidth()->isValueDependent() &&
928 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000929 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000930 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000931 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000932
933 // Handle using declarations of conversion functions.
934 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
935 if (Shadow->getDeclName().getNameKind()
936 == DeclarationName::CXXConversionFunctionName)
937 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Joao Matos17d35c32012-08-31 22:18:20 +0000938}
939
940bool CXXRecordDecl::isCLike() const {
941 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
942 !TemplateOrInstantiation.isNull())
943 return false;
944 if (!hasDefinition())
945 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000946
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000947 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000948}
949
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000950void CXXRecordDecl::getCaptureFields(
951 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000952 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000953 Captures.clear();
954 ThisCapture = 0;
955
Douglas Gregorda8962a2012-02-13 15:44:47 +0000956 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000957 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000958 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000959 C != CEnd; ++C, ++Field) {
960 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +0000961 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000962 continue;
963 }
964
David Blaikie581deb32012-06-06 20:45:41 +0000965 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000966 }
967}
968
969
John McCallb05b5f32010-03-15 09:07:48 +0000970static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
971 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000972 if (isa<UsingShadowDecl>(Conv))
973 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000974 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
975 T = ConvTemp->getTemplatedDecl()->getResultType();
976 else
977 T = cast<CXXConversionDecl>(Conv)->getConversionType();
978 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000979}
980
John McCallb05b5f32010-03-15 09:07:48 +0000981/// Collect the visible conversions of a base class.
982///
James Dennetta1253502012-06-15 22:28:09 +0000983/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +0000984/// \param InVirtual whether this base class is a virtual base (or a base
985/// of a virtual base)
986/// \param Access the access along the inheritance path to this base
987/// \param ParentHiddenTypes the conversions provided by the inheritors
988/// of this base
989/// \param Output the set to which to add conversions from non-virtual bases
990/// \param VOutput the set to which to add conversions from virtual bases
991/// \param HiddenVBaseCs the set of conversions which were hidden in a
992/// virtual base along some inheritance path
993static void CollectVisibleConversions(ASTContext &Context,
994 CXXRecordDecl *Record,
995 bool InVirtual,
996 AccessSpecifier Access,
997 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
998 UnresolvedSetImpl &Output,
999 UnresolvedSetImpl &VOutput,
1000 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1001 // The set of types which have conversions in this class or its
1002 // subclasses. As an optimization, we don't copy the derived set
1003 // unless it might change.
1004 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1005 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1006
1007 // Collect the direct conversions and figure out which conversions
1008 // will be hidden in the subclasses.
1009 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1010 if (!Cs.empty()) {
1011 HiddenTypesBuffer = ParentHiddenTypes;
1012 HiddenTypes = &HiddenTypesBuffer;
1013
1014 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +00001015 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1016 bool Hidden = ParentHiddenTypes.count(ConvType);
1017 if (!Hidden)
1018 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001019
1020 // If this conversion is hidden and we're in a virtual base,
1021 // remember that it's hidden along some inheritance path.
1022 if (Hidden && InVirtual)
1023 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1024
1025 // If this conversion isn't hidden, add it to the appropriate output.
1026 else if (!Hidden) {
1027 AccessSpecifier IAccess
1028 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1029
1030 if (InVirtual)
1031 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001032 else
John McCallb05b5f32010-03-15 09:07:48 +00001033 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001034 }
1035 }
1036 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001037
John McCallb05b5f32010-03-15 09:07:48 +00001038 // Collect information recursively from any base classes.
1039 for (CXXRecordDecl::base_class_iterator
1040 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1041 const RecordType *RT = I->getType()->getAs<RecordType>();
1042 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001043
John McCallb05b5f32010-03-15 09:07:48 +00001044 AccessSpecifier BaseAccess
1045 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1046 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001047
John McCallb05b5f32010-03-15 09:07:48 +00001048 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1049 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1050 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001051 }
John McCallb05b5f32010-03-15 09:07:48 +00001052}
Sebastian Redl9994a342009-10-25 17:03:50 +00001053
John McCallb05b5f32010-03-15 09:07:48 +00001054/// Collect the visible conversions of a class.
1055///
1056/// This would be extremely straightforward if it weren't for virtual
1057/// bases. It might be worth special-casing that, really.
1058static void CollectVisibleConversions(ASTContext &Context,
1059 CXXRecordDecl *Record,
1060 UnresolvedSetImpl &Output) {
1061 // The collection of all conversions in virtual bases that we've
1062 // found. These will be added to the output as long as they don't
1063 // appear in the hidden-conversions set.
1064 UnresolvedSet<8> VBaseCs;
1065
1066 // The set of conversions in virtual bases that we've determined to
1067 // be hidden.
1068 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1069
1070 // The set of types hidden by classes derived from this one.
1071 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1072
1073 // Go ahead and collect the direct conversions and add them to the
1074 // hidden-types set.
1075 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1076 Output.append(Cs.begin(), Cs.end());
1077 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1078 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1079
1080 // Recursively collect conversions from base classes.
1081 for (CXXRecordDecl::base_class_iterator
1082 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1083 const RecordType *RT = I->getType()->getAs<RecordType>();
1084 if (!RT) continue;
1085
1086 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1087 I->isVirtual(), I->getAccessSpecifier(),
1088 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1089 }
1090
1091 // Add any unhidden conversions provided by virtual bases.
1092 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1093 I != E; ++I) {
1094 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1095 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001096 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001097}
1098
1099/// getVisibleConversionFunctions - get all conversion functions visible
1100/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +00001101const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001102 // If root class, all conversions are visible.
1103 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +00001104 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +00001105 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +00001106 if (data().ComputedVisibleConversions)
1107 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +00001108 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001109 data().ComputedVisibleConversions = true;
1110 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001111}
1112
John McCall32daa422010-03-31 01:36:47 +00001113void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1114 // This operation is O(N) but extremely rare. Sema only uses it to
1115 // remove UsingShadowDecls in a class that were followed by a direct
1116 // declaration, e.g.:
1117 // class A : B {
1118 // using B::operator int;
1119 // operator int();
1120 // };
1121 // This is uncommon by itself and even more uncommon in conjunction
1122 // with sufficiently large numbers of directly-declared conversions
1123 // that asymptotic behavior matters.
1124
1125 UnresolvedSetImpl &Convs = *getConversionFunctions();
1126 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1127 if (Convs[I].getDecl() == ConvDecl) {
1128 Convs.erase(I);
1129 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1130 && "conversion was found multiple times in unresolved set");
1131 return;
1132 }
1133 }
1134
1135 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001136}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001137
Douglas Gregorf6b11852009-10-08 15:14:33 +00001138CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001139 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001140 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1141
1142 return 0;
1143}
1144
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001145MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1146 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1147}
1148
Douglas Gregorf6b11852009-10-08 15:14:33 +00001149void
1150CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1151 TemplateSpecializationKind TSK) {
1152 assert(TemplateOrInstantiation.isNull() &&
1153 "Previous template or instantiation?");
1154 assert(!isa<ClassTemplateSpecializationDecl>(this));
1155 TemplateOrInstantiation
1156 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1157}
1158
Anders Carlssonb13e3572009-12-07 06:33:48 +00001159TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1160 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001161 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1162 return Spec->getSpecializationKind();
1163
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001164 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001165 return MSInfo->getTemplateSpecializationKind();
1166
1167 return TSK_Undeclared;
1168}
1169
1170void
1171CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1172 if (ClassTemplateSpecializationDecl *Spec
1173 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1174 Spec->setSpecializationKind(TSK);
1175 return;
1176 }
1177
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001178 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001179 MSInfo->setTemplateSpecializationKind(TSK);
1180 return;
1181 }
1182
David Blaikieb219cfc2011-09-23 05:06:16 +00001183 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001184}
1185
Douglas Gregor1d110e02010-07-01 14:13:13 +00001186CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1187 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001188 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001189
1190 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001191 = Context.DeclarationNames.getCXXDestructorName(
1192 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001193
John McCallc0bf4622010-02-23 00:48:20 +00001194 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001195 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001196 if (I == E)
1197 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001199 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001200 return Dtor;
1201}
1202
Douglas Gregorda2142f2011-02-19 18:51:44 +00001203void CXXRecordDecl::completeDefinition() {
1204 completeDefinition(0);
1205}
1206
1207void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1208 RecordDecl::completeDefinition();
1209
David Blaikie4e4d0842012-03-11 07:00:24 +00001210 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001211 // Objective-C Automatic Reference Counting:
1212 // If a class has a non-static data member of Objective-C pointer
1213 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001214 // default constructor (if any), copy constructor, move constructor,
1215 // copy assignment operator, move assignment operator, and destructor are
1216 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001217 struct DefinitionData &Data = data();
1218 Data.PlainOldData = false;
1219 Data.HasTrivialDefaultConstructor = false;
1220 Data.HasTrivialCopyConstructor = false;
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001221 Data.HasTrivialMoveConstructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001222 Data.HasTrivialCopyAssignment = false;
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001223 Data.HasTrivialMoveAssignment = false;
John McCallf85e1932011-06-15 23:02:42 +00001224 Data.HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +00001225 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001226 }
1227
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001228 // If the class may be abstract (but hasn't been marked as such), check for
1229 // any pure final overriders.
1230 if (mayBeAbstract()) {
1231 CXXFinalOverriderMap MyFinalOverriders;
1232 if (!FinalOverriders) {
1233 getFinalOverriders(MyFinalOverriders);
1234 FinalOverriders = &MyFinalOverriders;
1235 }
1236
1237 bool Done = false;
1238 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1239 MEnd = FinalOverriders->end();
1240 M != MEnd && !Done; ++M) {
1241 for (OverridingMethods::iterator SO = M->second.begin(),
1242 SOEnd = M->second.end();
1243 SO != SOEnd && !Done; ++SO) {
1244 assert(SO->second.size() > 0 &&
1245 "All virtual functions have overridding virtual functions");
1246
1247 // C++ [class.abstract]p4:
1248 // A class is abstract if it contains or inherits at least one
1249 // pure virtual function for which the final overrider is pure
1250 // virtual.
1251 if (SO->second.front().Method->isPure()) {
1252 data().Abstract = true;
1253 Done = true;
1254 break;
1255 }
1256 }
1257 }
1258 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001259
1260 // Set access bits correctly on the directly-declared conversions.
1261 for (UnresolvedSetIterator I = data().Conversions.begin(),
1262 E = data().Conversions.end();
1263 I != E; ++I)
1264 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001265}
1266
1267bool CXXRecordDecl::mayBeAbstract() const {
1268 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1269 isDependentContext())
1270 return false;
1271
1272 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1273 BEnd = bases_end();
1274 B != BEnd; ++B) {
1275 CXXRecordDecl *BaseDecl
1276 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1277 if (BaseDecl->isAbstract())
1278 return true;
1279 }
1280
1281 return false;
1282}
1283
David Blaikie99ba9e32011-12-20 02:48:34 +00001284void CXXMethodDecl::anchor() { }
1285
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001286static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1287 const CXXMethodDecl *BaseMD) {
1288 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1289 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1290 const CXXMethodDecl *MD = *I;
1291 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1292 return true;
1293 if (recursivelyOverrides(MD, BaseMD))
1294 return true;
1295 }
1296 return false;
1297}
1298
1299CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001300CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1301 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001302 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1303 return this;
1304
1305 // Lookup doesn't work for destructors, so handle them separately.
1306 if (isa<CXXDestructorDecl>(this)) {
1307 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001308 if (MD) {
1309 if (recursivelyOverrides(MD, this))
1310 return MD;
1311 if (MayBeBase && recursivelyOverrides(this, MD))
1312 return MD;
1313 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001314 return NULL;
1315 }
1316
1317 lookup_const_result Candidates = RD->lookup(getDeclName());
1318 for (NamedDecl * const * I = Candidates.first; I != Candidates.second; ++I) {
1319 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1320 if (!MD)
1321 continue;
1322 if (recursivelyOverrides(MD, this))
1323 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001324 if (MayBeBase && recursivelyOverrides(this, MD))
1325 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001326 }
1327
1328 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1329 E = RD->bases_end(); I != E; ++I) {
1330 const RecordType *RT = I->getType()->getAs<RecordType>();
1331 if (!RT)
1332 continue;
1333 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1334 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1335 if (T)
1336 return T;
1337 }
1338
1339 return NULL;
1340}
1341
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001342CXXMethodDecl *
1343CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001344 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001345 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001346 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001347 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001348 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001349 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001350 isStatic, SCAsWritten, isInline, isConstexpr,
1351 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001352}
1353
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001354CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1355 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1356 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1357 DeclarationNameInfo(), QualType(),
1358 0, false, SC_None, false, false,
1359 SourceLocation());
1360}
1361
Douglas Gregor90916562009-09-29 18:16:17 +00001362bool CXXMethodDecl::isUsualDeallocationFunction() const {
1363 if (getOverloadedOperator() != OO_Delete &&
1364 getOverloadedOperator() != OO_Array_Delete)
1365 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001366
1367 // C++ [basic.stc.dynamic.deallocation]p2:
1368 // A template instance is never a usual deallocation function,
1369 // regardless of its signature.
1370 if (getPrimaryTemplate())
1371 return false;
1372
Douglas Gregor90916562009-09-29 18:16:17 +00001373 // C++ [basic.stc.dynamic.deallocation]p2:
1374 // If a class T has a member deallocation function named operator delete
1375 // with exactly one parameter, then that function is a usual (non-placement)
1376 // deallocation function. [...]
1377 if (getNumParams() == 1)
1378 return true;
1379
1380 // C++ [basic.stc.dynamic.deallocation]p2:
1381 // [...] If class T does not declare such an operator delete but does
1382 // declare a member deallocation function named operator delete with
1383 // exactly two parameters, the second of which has type std::size_t (18.1),
1384 // then this function is a usual deallocation function.
1385 ASTContext &Context = getASTContext();
1386 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001387 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1388 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001389 return false;
1390
1391 // This function is a usual deallocation function if there are no
1392 // single-parameter deallocation functions of the same kind.
1393 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1394 R.first != R.second; ++R.first) {
1395 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1396 if (FD->getNumParams() == 1)
1397 return false;
1398 }
1399
1400 return true;
1401}
1402
Douglas Gregor06a9f362010-05-01 20:49:11 +00001403bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001404 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001405 // A user-declared copy assignment operator X::operator= is a non-static
1406 // non-template member function of class X with exactly one parameter of
1407 // type X, X&, const X&, volatile X& or const volatile X&.
1408 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1409 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001410 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001411 return false;
1412
1413 QualType ParamType = getParamDecl(0)->getType();
1414 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1415 ParamType = Ref->getPointeeType();
1416
1417 ASTContext &Context = getASTContext();
1418 QualType ClassType
1419 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1420 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1421}
1422
Sean Huntffe37fd2011-05-25 20:50:04 +00001423bool CXXMethodDecl::isMoveAssignmentOperator() const {
1424 // C++0x [class.copy]p19:
1425 // A user-declared move assignment operator X::operator= is a non-static
1426 // non-template member function of class X with exactly one parameter of type
1427 // X&&, const X&&, volatile X&&, or const volatile X&&.
1428 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1429 getPrimaryTemplate() || getDescribedFunctionTemplate())
1430 return false;
1431
1432 QualType ParamType = getParamDecl(0)->getType();
1433 if (!isa<RValueReferenceType>(ParamType))
1434 return false;
1435 ParamType = ParamType->getPointeeType();
1436
1437 ASTContext &Context = getASTContext();
1438 QualType ClassType
1439 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1440 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1441}
1442
Anders Carlsson05eb2442009-05-16 23:58:37 +00001443void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001444 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001445 assert(!MD->getParent()->isDependentContext() &&
1446 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001447 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001448
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001449 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001450}
1451
1452CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001453 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001454 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001455}
1456
1457CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001458 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001459 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001460}
1461
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001462unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001463 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001464 return getASTContext().overridden_methods_size(this);
1465}
1466
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001467QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001468 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1469 // If the member function is declared const, the type of this is const X*,
1470 // if the member function is declared volatile, the type of this is
1471 // volatile X*, and if the member function is declared const volatile,
1472 // the type of this is const volatile X*.
1473
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001474 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001475
John McCall3cb0ebd2010-03-10 03:28:59 +00001476 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001477 ClassTy = C.getQualifiedType(ClassTy,
1478 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001479 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001480}
1481
Eli Friedmand7d7f672009-12-06 20:50:05 +00001482bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001483 // If this function is a template instantiation, look at the template from
1484 // which it was instantiated.
1485 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1486 if (!CheckFn)
1487 CheckFn = this;
1488
Eli Friedmand7d7f672009-12-06 20:50:05 +00001489 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001490 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001491}
1492
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001493bool CXXMethodDecl::isLambdaStaticInvoker() const {
1494 return getParent()->isLambda() &&
1495 getIdentifier() && getIdentifier()->getName() == "__invoke";
1496}
1497
1498
Sean Huntcbb67482011-01-08 20:30:50 +00001499CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1500 TypeSourceInfo *TInfo, bool IsVirtual,
1501 SourceLocation L, Expr *Init,
1502 SourceLocation R,
1503 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001504 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001505 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1506 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001507{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001508}
1509
Sean Huntcbb67482011-01-08 20:30:50 +00001510CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1511 FieldDecl *Member,
1512 SourceLocation MemberLoc,
1513 SourceLocation L, Expr *Init,
1514 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001515 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001516 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001517 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1518{
1519}
1520
Sean Huntcbb67482011-01-08 20:30:50 +00001521CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1522 IndirectFieldDecl *Member,
1523 SourceLocation MemberLoc,
1524 SourceLocation L, Expr *Init,
1525 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001526 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001527 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001528 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001529{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001530}
1531
Sean Huntcbb67482011-01-08 20:30:50 +00001532CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001533 TypeSourceInfo *TInfo,
1534 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001535 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001536 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1537 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001538 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1539{
1540}
1541
1542CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001543 FieldDecl *Member,
1544 SourceLocation MemberLoc,
1545 SourceLocation L, Expr *Init,
1546 SourceLocation R,
1547 VarDecl **Indices,
1548 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001549 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001550 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001551 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001552{
1553 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1554 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1555}
1556
Sean Huntcbb67482011-01-08 20:30:50 +00001557CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1558 FieldDecl *Member,
1559 SourceLocation MemberLoc,
1560 SourceLocation L, Expr *Init,
1561 SourceLocation R,
1562 VarDecl **Indices,
1563 unsigned NumIndices) {
1564 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001565 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001566 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001567 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1568 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001569}
1570
Sean Huntcbb67482011-01-08 20:30:50 +00001571TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001572 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001573 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001574 else
1575 return TypeLoc();
1576}
1577
Sean Huntcbb67482011-01-08 20:30:50 +00001578const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001579 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001580 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001581 else
1582 return 0;
1583}
1584
Sean Huntcbb67482011-01-08 20:30:50 +00001585SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001586 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001587 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001588
1589 if (isInClassMemberInitializer())
1590 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001591
Douglas Gregor76852c22011-11-01 01:16:03 +00001592 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1593 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1594
1595 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001596}
1597
Sean Huntcbb67482011-01-08 20:30:50 +00001598SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001599 if (isInClassMemberInitializer()) {
1600 FieldDecl *D = getAnyMember();
1601 if (Expr *I = D->getInClassInitializer())
1602 return I->getSourceRange();
1603 return SourceRange();
1604 }
1605
Douglas Gregor802ab452009-12-02 22:36:29 +00001606 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001607}
1608
David Blaikie99ba9e32011-12-20 02:48:34 +00001609void CXXConstructorDecl::anchor() { }
1610
Douglas Gregorb48fe382008-10-31 09:07:45 +00001611CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001612CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1613 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1614 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1615 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001616}
1617
1618CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001619CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001620 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001621 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001622 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001623 bool isExplicit, bool isInline,
1624 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001625 assert(NameInfo.getName().getNameKind()
1626 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001627 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001628 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001629 isExplicit, isInline, isImplicitlyDeclared,
1630 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001631}
1632
Douglas Gregor76852c22011-11-01 01:16:03 +00001633CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1634 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1635 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1636 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1637 return Construct->getConstructor();
1638
1639 return 0;
1640}
1641
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001642bool CXXConstructorDecl::isDefaultConstructor() const {
1643 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001644 // A default constructor for a class X is a constructor of class
1645 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001646 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001647 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001648}
1649
Mike Stump1eb44332009-09-09 15:08:12 +00001650bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001651CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001652 return isCopyOrMoveConstructor(TypeQuals) &&
1653 getParamDecl(0)->getType()->isLValueReferenceType();
1654}
1655
1656bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1657 return isCopyOrMoveConstructor(TypeQuals) &&
1658 getParamDecl(0)->getType()->isRValueReferenceType();
1659}
1660
1661/// \brief Determine whether this is a copy or move constructor.
1662bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001663 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001664 // A non-template constructor for class X is a copy constructor
1665 // if its first parameter is of type X&, const X&, volatile X& or
1666 // const volatile X&, and either there are no other parameters
1667 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001668 // C++0x [class.copy]p3:
1669 // A non-template constructor for class X is a move constructor if its
1670 // first parameter is of type X&&, const X&&, volatile X&&, or
1671 // const volatile X&&, and either there are no other parameters or else
1672 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001673 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001674 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001675 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001676 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001677 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001678
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001679 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001680
1681 // Do we have a reference type?
1682 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001683 if (!ParamRefType)
1684 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001685
Douglas Gregorfd476482009-11-13 23:59:09 +00001686 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001687 ASTContext &Context = getASTContext();
1688
Douglas Gregorfd476482009-11-13 23:59:09 +00001689 CanQualType PointeeType
1690 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001691 CanQualType ClassTy
1692 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001693 if (PointeeType.getUnqualifiedType() != ClassTy)
1694 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001695
John McCall0953e762009-09-24 19:53:00 +00001696 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001697
1698 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001699 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001700 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001701}
1702
Anders Carlssonfaccd722009-08-28 16:57:08 +00001703bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001704 // C++ [class.conv.ctor]p1:
1705 // A constructor declared without the function-specifier explicit
1706 // that can be called with a single parameter specifies a
1707 // conversion from the type of its first parameter to the type of
1708 // its class. Such a constructor is called a converting
1709 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001710 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001711 return false;
1712
Mike Stump1eb44332009-09-09 15:08:12 +00001713 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001714 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001715 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001716 (getNumParams() > 1 &&
1717 (getParamDecl(1)->hasDefaultArg() ||
1718 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001719}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001720
Douglas Gregor6493cc52010-11-08 17:16:59 +00001721bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001722 if ((getNumParams() < 1) ||
1723 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1724 (getPrimaryTemplate() == 0) ||
1725 (getDescribedFunctionTemplate() != 0))
1726 return false;
1727
1728 const ParmVarDecl *Param = getParamDecl(0);
1729
1730 ASTContext &Context = getASTContext();
1731 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1732
Douglas Gregor66724ea2009-11-14 01:20:54 +00001733 // Is it the same as our our class type?
1734 CanQualType ClassTy
1735 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1736 if (ParamType.getUnqualifiedType() != ClassTy)
1737 return false;
1738
1739 return true;
1740}
1741
Sebastian Redlf677ea32011-02-05 19:23:19 +00001742const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1743 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001744 method_iterator It = getASTContext().overridden_methods_begin(this);
1745 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001746 return 0;
1747
1748 return cast<CXXConstructorDecl>(*It);
1749}
1750
1751void
1752CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1753 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001754 assert(getASTContext().overridden_methods_size(this) == 0 &&
1755 "Base ctor already set.");
1756 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001757}
1758
David Blaikie99ba9e32011-12-20 02:48:34 +00001759void CXXDestructorDecl::anchor() { }
1760
Douglas Gregor42a552f2008-11-05 20:51:48 +00001761CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001762CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1763 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1764 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001765 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001766}
1767
1768CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001769CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001770 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001771 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001772 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001773 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001774 assert(NameInfo.getName().getNameKind()
1775 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001776 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001777 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001778 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001779}
1780
David Blaikie99ba9e32011-12-20 02:48:34 +00001781void CXXConversionDecl::anchor() { }
1782
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001783CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001784CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1785 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1786 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1787 QualType(), 0, false, false, false,
1788 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001789}
1790
1791CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001792CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001793 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001794 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001795 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001796 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001797 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001798 assert(NameInfo.getName().getNameKind()
1799 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001800 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001801 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001802 isInline, isExplicit, isConstexpr,
1803 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001804}
1805
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001806bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1807 return isImplicit() && getParent()->isLambda() &&
1808 getConversionType()->isBlockPointerType();
1809}
1810
David Blaikie99ba9e32011-12-20 02:48:34 +00001811void LinkageSpecDecl::anchor() { }
1812
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001813LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001814 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001815 SourceLocation ExternLoc,
1816 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001817 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001818 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001819 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001820}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001821
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001822LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1823 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1824 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1825 lang_c, SourceLocation());
1826}
1827
David Blaikie99ba9e32011-12-20 02:48:34 +00001828void UsingDirectiveDecl::anchor() { }
1829
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001830UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1831 SourceLocation L,
1832 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001833 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001834 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001835 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001836 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001837 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1838 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001839 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1840 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001841}
1842
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001843UsingDirectiveDecl *
1844UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1845 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1846 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1847 NestedNameSpecifierLoc(),
1848 SourceLocation(), 0, 0);
1849}
1850
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001851NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1852 if (NamespaceAliasDecl *NA =
1853 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1854 return NA->getNamespace();
1855 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1856}
1857
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001858void NamespaceDecl::anchor() { }
1859
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001860NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1861 SourceLocation StartLoc,
1862 SourceLocation IdLoc, IdentifierInfo *Id,
1863 NamespaceDecl *PrevDecl)
1864 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1865 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1866{
1867 setPreviousDeclaration(PrevDecl);
1868
1869 if (PrevDecl)
1870 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1871}
1872
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001873NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001874 bool Inline, SourceLocation StartLoc,
1875 SourceLocation IdLoc, IdentifierInfo *Id,
1876 NamespaceDecl *PrevDecl) {
1877 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001878}
1879
1880NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1881 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001882 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1883 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001884}
1885
David Blaikie99ba9e32011-12-20 02:48:34 +00001886void NamespaceAliasDecl::anchor() { }
1887
Mike Stump1eb44332009-09-09 15:08:12 +00001888NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001889 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001890 SourceLocation AliasLoc,
1891 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001892 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001893 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001894 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001895 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1896 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001897 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1898 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001899}
1900
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001901NamespaceAliasDecl *
1902NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1903 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1904 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1905 NestedNameSpecifierLoc(),
1906 SourceLocation(), 0);
1907}
1908
David Blaikie99ba9e32011-12-20 02:48:34 +00001909void UsingShadowDecl::anchor() { }
1910
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001911UsingShadowDecl *
1912UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1913 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1914 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1915}
1916
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001917UsingDecl *UsingShadowDecl::getUsingDecl() const {
1918 const UsingShadowDecl *Shadow = this;
1919 while (const UsingShadowDecl *NextShadow =
1920 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1921 Shadow = NextShadow;
1922 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1923}
1924
David Blaikie99ba9e32011-12-20 02:48:34 +00001925void UsingDecl::anchor() { }
1926
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001927void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1928 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1929 "declaration already in set");
1930 assert(S->getUsingDecl() == this);
1931
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001932 if (FirstUsingShadow.getPointer())
1933 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1934 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001935}
1936
1937void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1938 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1939 "declaration not in set");
1940 assert(S->getUsingDecl() == this);
1941
1942 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1943
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001944 if (FirstUsingShadow.getPointer() == S) {
1945 FirstUsingShadow.setPointer(
1946 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001947 S->UsingOrNextShadow = this;
1948 return;
1949 }
1950
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001951 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001952 while (Prev->UsingOrNextShadow != S)
1953 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1954 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1955 S->UsingOrNextShadow = this;
1956}
1957
Douglas Gregordc355712011-02-25 00:36:19 +00001958UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1959 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001960 const DeclarationNameInfo &NameInfo,
1961 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001962 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001963}
1964
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001965UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1966 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1967 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1968 DeclarationNameInfo(), false);
1969}
1970
David Blaikie99ba9e32011-12-20 02:48:34 +00001971void UnresolvedUsingValueDecl::anchor() { }
1972
John McCall7ba107a2009-11-18 02:36:19 +00001973UnresolvedUsingValueDecl *
1974UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1975 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001976 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001977 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001978 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001979 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001980}
1981
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001982UnresolvedUsingValueDecl *
1983UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1984 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1985 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1986 NestedNameSpecifierLoc(),
1987 DeclarationNameInfo());
1988}
1989
David Blaikie99ba9e32011-12-20 02:48:34 +00001990void UnresolvedUsingTypenameDecl::anchor() { }
1991
John McCall7ba107a2009-11-18 02:36:19 +00001992UnresolvedUsingTypenameDecl *
1993UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1994 SourceLocation UsingLoc,
1995 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001996 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001997 SourceLocation TargetNameLoc,
1998 DeclarationName TargetName) {
1999 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002000 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002001 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00002002}
2003
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002004UnresolvedUsingTypenameDecl *
2005UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2006 void *Mem = AllocateDeserializedDecl(C, ID,
2007 sizeof(UnresolvedUsingTypenameDecl));
2008 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2009 SourceLocation(),
2010 NestedNameSpecifierLoc(),
2011 SourceLocation(),
2012 0);
2013}
2014
David Blaikie99ba9e32011-12-20 02:48:34 +00002015void StaticAssertDecl::anchor() { }
2016
Anders Carlssonfb311762009-03-14 00:25:26 +00002017StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002018 SourceLocation StaticAssertLoc,
2019 Expr *AssertExpr,
2020 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002021 SourceLocation RParenLoc,
2022 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002023 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002024 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00002025}
2026
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002027StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2028 unsigned ID) {
2029 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00002030 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
2031 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002032}
2033
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002034static const char *getAccessName(AccessSpecifier AS) {
2035 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002036 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002037 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002038 case AS_public:
2039 return "public";
2040 case AS_private:
2041 return "private";
2042 case AS_protected:
2043 return "protected";
2044 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002045 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002046}
2047
2048const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2049 AccessSpecifier AS) {
2050 return DB << getAccessName(AS);
2051}
Richard Smithf15fda02012-02-02 01:16:57 +00002052
2053const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2054 AccessSpecifier AS) {
2055 return DB << getAccessName(AS);
2056}