blob: 82e630acefba85553087c7d62b5d411dfe4aeed2 [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
Richard Smith93af2b82012-11-14 07:36:28 +0000243 // instead of all of them. For now, we treat a move constructor as being
244 // non-trivial if it calls anything other than a trivial move constructor.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000245 if (!BaseClassDecl->hasTrivialCopyConstructor())
246 data().HasTrivialCopyConstructor = false;
Richard Smith93af2b82012-11-14 07:36:28 +0000247 if (!BaseClassDecl->hasTrivialMoveConstructor() ||
248 !(BaseClassDecl->hasDeclaredMoveConstructor() ||
249 BaseClassDecl->needsImplicitMoveConstructor()))
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000250 data().HasTrivialMoveConstructor = false;
251
252 // C++0x [class.copy]p27:
253 // A copy/move assignment operator for class X is trivial if [...]
254 // [...]
255 // -- the assignment operator selected to copy/move each direct base
256 // class subobject is trivial, and
257 // FIXME: C++0x: We need to only consider the selected operator instead
258 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000259 if (!BaseClassDecl->hasTrivialCopyAssignment())
260 data().HasTrivialCopyAssignment = false;
Richard Smith93af2b82012-11-14 07:36:28 +0000261 if (!BaseClassDecl->hasTrivialMoveAssignment() ||
262 !(BaseClassDecl->hasDeclaredMoveAssignment() ||
263 BaseClassDecl->needsImplicitMoveAssignment()))
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000264 data().HasTrivialMoveAssignment = false;
Richard Smith61802452011-12-22 02:22:31 +0000265
266 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000267 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000268 // requirements of a constexpr constructor, the implicitly-defined
269 // default constructor is constexpr.
270 if (!BaseClassDecl->hasConstexprDefaultConstructor())
271 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000272 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000273
274 // C++ [class.ctor]p3:
275 // A destructor is trivial if all the direct base classes of its class
276 // have trivial destructors.
277 if (!BaseClassDecl->hasTrivialDestructor())
278 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000279
280 if (!BaseClassDecl->hasIrrelevantDestructor())
281 data().HasIrrelevantDestructor = false;
282
John McCallf85e1932011-06-15 23:02:42 +0000283 // A class has an Objective-C object member if... or any of its bases
284 // has an Objective-C object member.
285 if (BaseClassDecl->hasObjectMember())
286 setHasObjectMember(true);
287
Douglas Gregor2bb11012011-05-13 01:05:07 +0000288 // Keep track of the presence of mutable fields.
289 if (BaseClassDecl->hasMutableFields())
290 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000291 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000292
293 if (VBases.empty())
294 return;
295
296 // Create base specifier for any direct or indirect virtual bases.
297 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
298 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000299 for (int I = 0, E = VBases.size(); I != E; ++I)
300 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000301}
302
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000303/// Callback function for CXXRecordDecl::forallBases that acknowledges
304/// that it saw a base class.
305static bool SawBase(const CXXRecordDecl *, void *) {
306 return true;
307}
308
309bool CXXRecordDecl::hasAnyDependentBases() const {
310 if (!isDependentContext())
311 return false;
312
313 return !forallBases(SawBase, 0);
314}
315
Sean Huntffe37fd2011-05-25 20:50:04 +0000316bool CXXRecordDecl::hasConstCopyConstructor() const {
317 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000318}
319
Chandler Carruthb7e95892011-04-23 10:47:28 +0000320bool CXXRecordDecl::isTriviallyCopyable() const {
321 // C++0x [class]p5:
322 // A trivially copyable class is a class that:
323 // -- has no non-trivial copy constructors,
324 if (!hasTrivialCopyConstructor()) return false;
325 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000326 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000327 // -- has no non-trivial copy assignment operators,
328 if (!hasTrivialCopyAssignment()) return false;
329 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000330 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000331 // -- has a trivial destructor.
332 if (!hasTrivialDestructor()) return false;
333
334 return true;
335}
336
Douglas Gregor0d405db2010-07-01 20:59:04 +0000337/// \brief Perform a simplistic form of overload resolution that only considers
338/// cv-qualifiers on a single parameter, and return the best overload candidate
339/// (if there is one).
340static CXXMethodDecl *
341GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000342 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000343 if (Cands.empty())
344 return 0;
345 if (Cands.size() == 1)
346 return Cands[0].first;
347
348 unsigned Best = 0, N = Cands.size();
349 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000350 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000351 Best = I;
352
Benjamin Kramer2cd7f412012-07-30 15:53:26 +0000353 for (unsigned I = 0; I != N; ++I)
354 if (I != Best && Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000355 return 0;
356
357 return Cands[Best].first;
358}
359
Sean Huntffe37fd2011-05-25 20:50:04 +0000360CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
361 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000362 QualType ClassType
363 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000364 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000365 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000366 Context.getCanonicalType(ClassType));
367 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000368 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000369 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000370 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000371 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000372 // C++ [class.copy]p2:
373 // A non-template constructor for class X is a copy constructor if [...]
374 if (isa<FunctionTemplateDecl>(*Con))
375 continue;
376
Douglas Gregor0d405db2010-07-01 20:59:04 +0000377 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
378 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000379 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
380 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000381 Found.push_back(std::make_pair(
382 const_cast<CXXConstructorDecl *>(Constructor),
383 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000384 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000385 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000386
387 return cast_or_null<CXXConstructorDecl>(
388 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000389}
390
Sean Huntffe37fd2011-05-25 20:50:04 +0000391CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
392 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
393 if (I->isMoveConstructor())
David Blaikie581deb32012-06-06 20:45:41 +0000394 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000395
396 return 0;
397}
398
Douglas Gregorb87786f2010-07-01 17:48:08 +0000399CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
400 ASTContext &Context = getASTContext();
401 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
402 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
403
Chris Lattner5f9e2722011-07-23 10:55:15 +0000404 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000405 DeclContext::lookup_const_iterator Op, OpEnd;
406 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
407 // C++ [class.copy]p9:
408 // A user-declared copy assignment operator is a non-static non-template
409 // member function of class X with exactly one parameter of type X, X&,
410 // const X&, volatile X& or const volatile X&.
411 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
412 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
413 continue;
414
415 const FunctionProtoType *FnType
416 = Method->getType()->getAs<FunctionProtoType>();
417 assert(FnType && "Overloaded operator has no prototype.");
418 // Don't assert on this; an invalid decl might have been left in the AST.
419 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
420 continue;
421
422 QualType ArgType = FnType->getArgType(0);
423 Qualifiers Quals;
424 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
425 ArgType = Ref->getPointeeType();
426 // If we have a const argument and we have a reference to a non-const,
427 // this function does not match.
428 if (ArgIsConst && !ArgType.isConstQualified())
429 continue;
430
431 Quals = ArgType.getQualifiers();
432 } else {
433 // By-value copy-assignment operators are treated like const X&
434 // copy-assignment operators.
435 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
436 }
437
438 if (!Context.hasSameUnqualifiedType(ArgType, Class))
439 continue;
440
441 // Save this copy-assignment operator. It might be "the one".
442 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
443 }
444
445 // Use a simplistic form of overload resolution to find the candidate.
446 return GetBestOverloadCandidateSimple(Found);
447}
448
Sean Huntffe37fd2011-05-25 20:50:04 +0000449CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
450 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
451 if (I->isMoveAssignmentOperator())
David Blaikie581deb32012-06-06 20:45:41 +0000452 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000453
454 return 0;
455}
456
Douglas Gregor21386642010-09-28 21:55:22 +0000457void CXXRecordDecl::markedVirtualFunctionPure() {
458 // C++ [class.abstract]p2:
459 // A class is abstract if it has at least one pure virtual function.
460 data().Abstract = true;
461}
462
Richard Smith3f5f5582012-06-08 21:09:22 +0000463void CXXRecordDecl::markedConstructorConstexpr(CXXConstructorDecl *CD) {
Richard Smithd3861ce2012-06-10 07:07:24 +0000464 if (!CD->isCopyOrMoveConstructor())
Richard Smith3f5f5582012-06-08 21:09:22 +0000465 data().HasConstexprNonCopyMoveConstructor = true;
466
467 if (CD->isDefaultConstructor())
468 data().HasConstexprDefaultConstructor = true;
469}
470
Douglas Gregor21386642010-09-28 21:55:22 +0000471void CXXRecordDecl::addedMember(Decl *D) {
Joao Matos17d35c32012-08-31 22:18:20 +0000472 if (!D->isImplicit() &&
473 !isa<FieldDecl>(D) &&
474 !isa<IndirectFieldDecl>(D) &&
475 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
476 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
477 data().HasOnlyCMembers = false;
478
479 // Ignore friends and invalid declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000480 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000481 return;
482
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000483 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
484 if (FunTmpl)
485 D = FunTmpl->getTemplatedDecl();
486
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000487 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
488 if (Method->isVirtual()) {
489 // C++ [dcl.init.aggr]p1:
490 // An aggregate is an array or a class with [...] no virtual functions.
491 data().Aggregate = false;
492
493 // C++ [class]p4:
494 // A POD-struct is an aggregate class...
495 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000496
497 // Virtual functions make the class non-empty.
498 // FIXME: Standard ref?
499 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000500
501 // C++ [class.virtual]p1:
502 // A class that declares or inherits a virtual function is called a
503 // polymorphic class.
504 data().Polymorphic = true;
505
Sean Hunt023df372011-05-09 18:22:59 +0000506 // C++0x [class.ctor]p5
507 // A default constructor is trivial [...] if:
508 // -- its class has no virtual functions [...]
509 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000510
511 // C++0x [class.copy]p13:
512 // A copy/move constructor for class X is trivial if [...]
513 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000514 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000515 data().HasTrivialMoveConstructor = false;
516
517 // C++0x [class.copy]p27:
518 // A copy/move assignment operator for class X is trivial if [...]
519 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000520 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000521 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000522
Chandler Carrutha8225442011-04-30 09:17:45 +0000523 // C++0x [class]p7:
524 // A standard-layout class is a class that: [...]
525 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000526 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000527 }
528 }
529
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000530 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000531 // Notify that an implicit member was added after the definition
532 // was completed.
533 if (!isBeingDefined())
534 if (ASTMutationListener *L = getASTMutationListener())
535 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000536
Sean Huntffe37fd2011-05-25 20:50:04 +0000537 // If this is a special member function, note that it was added and then
538 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000539 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith61802452011-12-22 02:22:31 +0000540 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000541 data().DeclaredDefaultConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000542 if (Constructor->isConstexpr()) {
543 data().HasConstexprDefaultConstructor = true;
544 data().HasConstexprNonCopyMoveConstructor = true;
545 }
546 } else if (Constructor->isCopyConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000547 data().DeclaredCopyConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000548 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000549 data().DeclaredMoveConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000550 } else
Sean Huntffe37fd2011-05-25 20:50:04 +0000551 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000552 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000553 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000554 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000555 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000556 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
557 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000558 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000559 else if (Method->isMoveAssignmentOperator())
560 data().DeclaredMoveAssignment = true;
561 else
562 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000563 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000564 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000565
Sean Huntffe37fd2011-05-25 20:50:04 +0000566NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000567 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000568 }
569
570 // Handle (user-declared) constructors.
571 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
572 // Note that we have a user-declared constructor.
573 data().UserDeclaredConstructor = true;
574
Richard Smith017ab772011-09-05 02:13:09 +0000575 // Technically, "user-provided" is only defined for special member
576 // functions, but the intent of the standard is clearly that it should apply
577 // to all functions.
578 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000579
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000580 if (Constructor->isDefaultConstructor()) {
581 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000582 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000583 // C++0x [class.ctor]p5:
584 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000585 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000586 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000587 }
Richard Smith61802452011-12-22 02:22:31 +0000588 if (Constructor->isConstexpr()) {
589 data().HasConstexprDefaultConstructor = true;
590 data().HasConstexprNonCopyMoveConstructor = true;
591 }
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000592 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000593
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000594 // Note when we have a user-declared copy or move constructor, which will
595 // suppress the implicit declaration of those constructors.
596 if (!FunTmpl) {
597 if (Constructor->isCopyConstructor()) {
598 data().UserDeclaredCopyConstructor = true;
599 data().DeclaredCopyConstructor = true;
600
601 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000602 // A copy/move constructor for class X is trivial if it is not
603 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000604 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000605 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000606 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000607 data().UserDeclaredMoveConstructor = true;
608 data().DeclaredMoveConstructor = true;
609
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000610 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000611 // A copy/move constructor for class X is trivial if it is not
612 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000613 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000614 data().HasTrivialMoveConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000615 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000616 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000617 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
618 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000619 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000620 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000621 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000622
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000623 // C++ [dcl.init.aggr]p1:
624 // An aggregate is an array or a class with no user-declared
625 // constructors [...].
626 // C++0x [dcl.init.aggr]p1:
627 // An aggregate is an array or a class with no user-provided
628 // constructors [...].
David Blaikie4e4d0842012-03-11 07:00:24 +0000629 if (!getASTContext().getLangOpts().CPlusPlus0x || UserProvided)
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000630 data().Aggregate = false;
631
632 // C++ [class]p4:
633 // A POD-struct is an aggregate class [...]
634 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
635 // type is technically an aggregate in C++0x since it wouldn't be in 03.
636 data().PlainOldData = false;
637
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000638 return;
639 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000640
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000641 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000642 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000643 data().DeclaredDestructor = true;
644 data().UserDeclaredDestructor = true;
Richard Smithdfefb842012-02-25 07:33:38 +0000645 data().HasIrrelevantDestructor = false;
646
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000647 // C++ [class]p4:
648 // A POD-struct is an aggregate class that has [...] no user-defined
649 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000650 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000651 data().PlainOldData = false;
652
Douglas Gregor45fa5602011-11-07 20:56:01 +0000653 // C++11 [class.dtor]p5:
654 // A destructor is trivial if it is not user-provided and if
655 // -- the destructor is not virtual.
Richard Smithd3861ce2012-06-10 07:07:24 +0000656 if (DD->isUserProvided() || DD->isVirtual())
Sean Huntcf34e752011-05-16 22:41:40 +0000657 data().HasTrivialDestructor = false;
Richard Smithd3861ce2012-06-10 07:07:24 +0000658
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000659 return;
660 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000661
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000662 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000663 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000664 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000665 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000666 // A POD-struct is an aggregate class that [...] has no user-defined
667 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000668 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000669 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000670
Sean Huntffe37fd2011-05-25 20:50:04 +0000671 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000672
Sean Huntffe37fd2011-05-25 20:50:04 +0000673 // Suppress the implicit declaration of a copy constructor.
674 data().UserDeclaredCopyAssignment = true;
675 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000676
Sean Huntffe37fd2011-05-25 20:50:04 +0000677 // C++0x [class.copy]p27:
678 // A copy/move assignment operator for class X is trivial if it is
679 // neither user-provided nor deleted [...]
680 if (Method->isUserProvided())
681 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000682
Sean Huntffe37fd2011-05-25 20:50:04 +0000683 return;
684 }
685
686 if (Method->isMoveAssignmentOperator()) {
687 // This is an extension in C++03 mode, but we'll keep consistency by
688 // taking a move assignment operator to induce non-POD-ness
689 data().PlainOldData = false;
690
691 // This is a move assignment operator.
692 data().UserDeclaredMoveAssignment = true;
693 data().DeclaredMoveAssignment = true;
694
695 // C++0x [class.copy]p27:
696 // A copy/move assignment operator for class X is trivial if it is
697 // neither user-provided nor deleted [...]
698 if (Method->isUserProvided())
699 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000700 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000701
Douglas Gregore80622f2010-09-29 04:25:11 +0000702 // Keep the list of conversion functions up-to-date.
703 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
704 // We don't record specializations.
705 if (Conversion->getPrimaryTemplate())
706 return;
707
708 // FIXME: We intentionally don't use the decl's access here because it
709 // hasn't been set yet. That's really just a misdesign in Sema.
710
711 if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000712 if (FunTmpl->getPreviousDecl())
713 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000714 FunTmpl);
715 else
716 data().Conversions.addDecl(FunTmpl);
717 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000718 if (Conversion->getPreviousDecl())
719 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000720 Conversion);
721 else
722 data().Conversions.addDecl(Conversion);
723 }
724 }
725
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000726 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000727 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000728
729 // Handle non-static data members.
730 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000731 // C++ [class.bit]p2:
732 // A declaration for a bit-field that omits the identifier declares an
733 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
734 // initialized.
735 if (Field->isUnnamedBitfield())
736 return;
737
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000738 // C++ [dcl.init.aggr]p1:
739 // An aggregate is an array or a class (clause 9) with [...] no
740 // private or protected non-static data members (clause 11).
741 //
742 // A POD must be an aggregate.
743 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
744 data().Aggregate = false;
745 data().PlainOldData = false;
746 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000747
748 // C++0x [class]p7:
749 // A standard-layout class is a class that:
750 // [...]
751 // -- has the same access control for all non-static data members,
752 switch (D->getAccess()) {
753 case AS_private: data().HasPrivateFields = true; break;
754 case AS_protected: data().HasProtectedFields = true; break;
755 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000756 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000757 };
758 if ((data().HasPrivateFields + data().HasProtectedFields +
759 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000760 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000761
Douglas Gregor2bb11012011-05-13 01:05:07 +0000762 // Keep track of the presence of mutable fields.
763 if (Field->isMutable())
764 data().HasMutableFields = true;
765
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000766 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000767 // A POD struct is a class that is both a trivial class and a
768 // standard-layout class, and has no non-static data members of type
769 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000770 //
771 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
772 // that does not explicitly have no lifetime makes the class a non-POD.
773 // However, we delay setting PlainOldData to false in this case so that
774 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000775 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000776 // In this case, the class will become a non-POD class when we complete
777 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000778 ASTContext &Context = getASTContext();
779 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000780 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000781 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000782 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
783 setHasObjectMember(true);
784 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000785 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000786
Chandler Carrutha8225442011-04-30 09:17:45 +0000787 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000788 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000789
Chandler Carrutha8225442011-04-30 09:17:45 +0000790 // C++0x [class]p7:
791 // A standard-layout class is a class that:
792 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000793 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000794 }
795
Richard Smith86c3ae42012-02-13 03:54:03 +0000796 // Record if this field is the first non-literal or volatile field or base.
797 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000798 data().HasNonLiteralTypeFieldsOrBases = true;
799
Richard Smith7a614d82011-06-11 17:19:42 +0000800 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000801 data().HasInClassInitializer = true;
802
803 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000804 // A default constructor is trivial if [...] no non-static data member
805 // of its class has a brace-or-equal-initializer.
806 data().HasTrivialDefaultConstructor = false;
807
Richard Smithd079abf2012-05-07 01:07:30 +0000808 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000809 // An aggregate is a [...] class with [...] no
810 // brace-or-equal-initializers for non-static data members.
811 data().Aggregate = false;
812
Richard Smithd079abf2012-05-07 01:07:30 +0000813 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000814 // A POD struct is [...] a trivial class.
815 data().PlainOldData = false;
816 }
817
Douglas Gregor85606eb2010-09-28 20:50:54 +0000818 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
819 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
820 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000821 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000822 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000823 // -- for all the non-static data members of its class that are of
824 // class type (or array thereof), each such class has a trivial
825 // default constructor.
826 if (!FieldRec->hasTrivialDefaultConstructor())
827 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000828
829 // C++0x [class.copy]p13:
830 // A copy/move constructor for class X is trivial if [...]
831 // [...]
832 // -- for each non-static data member of X that is of class type (or
833 // an array thereof), the constructor selected to copy/move that
834 // member is trivial;
835 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000836 if (!FieldRec->hasTrivialCopyConstructor())
837 data().HasTrivialCopyConstructor = false;
Richard Smith93af2b82012-11-14 07:36:28 +0000838 if (!FieldRec->hasTrivialMoveConstructor() ||
839 !(FieldRec->hasDeclaredMoveConstructor() ||
840 FieldRec->needsImplicitMoveConstructor()))
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000841 data().HasTrivialMoveConstructor = false;
842
843 // C++0x [class.copy]p27:
844 // A copy/move assignment operator for class X is trivial if [...]
845 // [...]
846 // -- for each non-static data member of X that is of class type (or
847 // an array thereof), the assignment operator selected to
848 // copy/move that member is trivial;
849 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000850 if (!FieldRec->hasTrivialCopyAssignment())
851 data().HasTrivialCopyAssignment = false;
Richard Smith93af2b82012-11-14 07:36:28 +0000852 if (!FieldRec->hasTrivialMoveAssignment() ||
853 !(FieldRec->hasDeclaredMoveAssignment() ||
854 FieldRec->needsImplicitMoveAssignment()))
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000855 data().HasTrivialMoveAssignment = false;
856
Douglas Gregor85606eb2010-09-28 20:50:54 +0000857 if (!FieldRec->hasTrivialDestructor())
858 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000859 if (!FieldRec->hasIrrelevantDestructor())
860 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000861 if (FieldRec->hasObjectMember())
862 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000863
864 // C++0x [class]p7:
865 // A standard-layout class is a class that:
866 // -- has no non-static data members of type non-standard-layout
867 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000868 if (!FieldRec->isStandardLayout())
869 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000870
871 // C++0x [class]p7:
872 // A standard-layout class is a class that:
873 // [...]
874 // -- has no base classes of the same type as the first non-static
875 // data member.
876 // We don't want to expend bits in the state of the record decl
877 // tracking whether this is the first non-static data member so we
878 // cheat a bit and use some of the existing state: the empty bit.
879 // Virtual bases and virtual methods make a class non-empty, but they
880 // also make it non-standard-layout so we needn't check here.
881 // A non-empty base class may leave the class standard-layout, but not
882 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000883 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000884 // data member must come through here with Empty still true, and Empty
885 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000886 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000887 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
888 BE = bases_end();
889 BI != BE; ++BI) {
890 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000891 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000892 break;
893 }
894 }
895 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000896
897 // Keep track of the presence of mutable fields.
898 if (FieldRec->hasMutableFields())
899 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000900
901 // C++11 [class.copy]p13:
902 // If the implicitly-defined constructor would satisfy the
903 // requirements of a constexpr constructor, the implicitly-defined
904 // constructor is constexpr.
905 // C++11 [dcl.constexpr]p4:
906 // -- every constructor involved in initializing non-static data
907 // members [...] shall be a constexpr constructor
908 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000909 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000910 // The standard requires any in-class initializer to be a constant
911 // expression. We consider this to be a defect.
912 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000913 }
Richard Smith61802452011-12-22 02:22:31 +0000914 } else {
915 // Base element type of field is a non-class type.
Richard Smithd3861ce2012-06-10 07:07:24 +0000916 if (!T->isLiteralType() ||
917 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000918 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000919 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000920
921 // C++0x [class]p7:
922 // A standard-layout class is a class that:
923 // [...]
924 // -- either has no non-static data members in the most derived
925 // class and at most one base class with non-static data members,
926 // or has no base classes with non-static data members, and
927 // At this point we know that we have a non-static data member, so the last
928 // clause holds.
929 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000930 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000931
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000932 // If this is not a zero-length bit-field, then the class is not empty.
933 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000934 if (!Field->isBitField() ||
935 (!Field->getBitWidth()->isTypeDependent() &&
936 !Field->getBitWidth()->isValueDependent() &&
937 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000938 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000939 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000940 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000941
942 // Handle using declarations of conversion functions.
943 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
944 if (Shadow->getDeclName().getNameKind()
945 == DeclarationName::CXXConversionFunctionName)
946 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Joao Matos17d35c32012-08-31 22:18:20 +0000947}
948
949bool CXXRecordDecl::isCLike() const {
950 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
951 !TemplateOrInstantiation.isNull())
952 return false;
953 if (!hasDefinition())
954 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000955
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000956 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000957}
958
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000959void CXXRecordDecl::getCaptureFields(
960 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000961 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000962 Captures.clear();
963 ThisCapture = 0;
964
Douglas Gregorda8962a2012-02-13 15:44:47 +0000965 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000966 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000967 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000968 C != CEnd; ++C, ++Field) {
969 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +0000970 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000971 continue;
972 }
973
David Blaikie581deb32012-06-06 20:45:41 +0000974 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000975 }
976}
977
978
John McCallb05b5f32010-03-15 09:07:48 +0000979static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
980 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000981 if (isa<UsingShadowDecl>(Conv))
982 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000983 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
984 T = ConvTemp->getTemplatedDecl()->getResultType();
985 else
986 T = cast<CXXConversionDecl>(Conv)->getConversionType();
987 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000988}
989
John McCallb05b5f32010-03-15 09:07:48 +0000990/// Collect the visible conversions of a base class.
991///
James Dennetta1253502012-06-15 22:28:09 +0000992/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +0000993/// \param InVirtual whether this base class is a virtual base (or a base
994/// of a virtual base)
995/// \param Access the access along the inheritance path to this base
996/// \param ParentHiddenTypes the conversions provided by the inheritors
997/// of this base
998/// \param Output the set to which to add conversions from non-virtual bases
999/// \param VOutput the set to which to add conversions from virtual bases
1000/// \param HiddenVBaseCs the set of conversions which were hidden in a
1001/// virtual base along some inheritance path
1002static void CollectVisibleConversions(ASTContext &Context,
1003 CXXRecordDecl *Record,
1004 bool InVirtual,
1005 AccessSpecifier Access,
1006 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
1007 UnresolvedSetImpl &Output,
1008 UnresolvedSetImpl &VOutput,
1009 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1010 // The set of types which have conversions in this class or its
1011 // subclasses. As an optimization, we don't copy the derived set
1012 // unless it might change.
1013 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1014 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1015
1016 // Collect the direct conversions and figure out which conversions
1017 // will be hidden in the subclasses.
1018 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1019 if (!Cs.empty()) {
1020 HiddenTypesBuffer = ParentHiddenTypes;
1021 HiddenTypes = &HiddenTypesBuffer;
1022
1023 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +00001024 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1025 bool Hidden = ParentHiddenTypes.count(ConvType);
1026 if (!Hidden)
1027 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001028
1029 // If this conversion is hidden and we're in a virtual base,
1030 // remember that it's hidden along some inheritance path.
1031 if (Hidden && InVirtual)
1032 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1033
1034 // If this conversion isn't hidden, add it to the appropriate output.
1035 else if (!Hidden) {
1036 AccessSpecifier IAccess
1037 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1038
1039 if (InVirtual)
1040 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001041 else
John McCallb05b5f32010-03-15 09:07:48 +00001042 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001043 }
1044 }
1045 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001046
John McCallb05b5f32010-03-15 09:07:48 +00001047 // Collect information recursively from any base classes.
1048 for (CXXRecordDecl::base_class_iterator
1049 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1050 const RecordType *RT = I->getType()->getAs<RecordType>();
1051 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001052
John McCallb05b5f32010-03-15 09:07:48 +00001053 AccessSpecifier BaseAccess
1054 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1055 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001056
John McCallb05b5f32010-03-15 09:07:48 +00001057 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1058 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1059 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001060 }
John McCallb05b5f32010-03-15 09:07:48 +00001061}
Sebastian Redl9994a342009-10-25 17:03:50 +00001062
John McCallb05b5f32010-03-15 09:07:48 +00001063/// Collect the visible conversions of a class.
1064///
1065/// This would be extremely straightforward if it weren't for virtual
1066/// bases. It might be worth special-casing that, really.
1067static void CollectVisibleConversions(ASTContext &Context,
1068 CXXRecordDecl *Record,
1069 UnresolvedSetImpl &Output) {
1070 // The collection of all conversions in virtual bases that we've
1071 // found. These will be added to the output as long as they don't
1072 // appear in the hidden-conversions set.
1073 UnresolvedSet<8> VBaseCs;
1074
1075 // The set of conversions in virtual bases that we've determined to
1076 // be hidden.
1077 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1078
1079 // The set of types hidden by classes derived from this one.
1080 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1081
1082 // Go ahead and collect the direct conversions and add them to the
1083 // hidden-types set.
1084 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1085 Output.append(Cs.begin(), Cs.end());
1086 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1087 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1088
1089 // Recursively collect conversions from base classes.
1090 for (CXXRecordDecl::base_class_iterator
1091 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1092 const RecordType *RT = I->getType()->getAs<RecordType>();
1093 if (!RT) continue;
1094
1095 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1096 I->isVirtual(), I->getAccessSpecifier(),
1097 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1098 }
1099
1100 // Add any unhidden conversions provided by virtual bases.
1101 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1102 I != E; ++I) {
1103 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1104 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001105 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001106}
1107
1108/// getVisibleConversionFunctions - get all conversion functions visible
1109/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +00001110const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001111 // If root class, all conversions are visible.
1112 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +00001113 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +00001114 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +00001115 if (data().ComputedVisibleConversions)
1116 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +00001117 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001118 data().ComputedVisibleConversions = true;
1119 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001120}
1121
John McCall32daa422010-03-31 01:36:47 +00001122void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1123 // This operation is O(N) but extremely rare. Sema only uses it to
1124 // remove UsingShadowDecls in a class that were followed by a direct
1125 // declaration, e.g.:
1126 // class A : B {
1127 // using B::operator int;
1128 // operator int();
1129 // };
1130 // This is uncommon by itself and even more uncommon in conjunction
1131 // with sufficiently large numbers of directly-declared conversions
1132 // that asymptotic behavior matters.
1133
1134 UnresolvedSetImpl &Convs = *getConversionFunctions();
1135 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1136 if (Convs[I].getDecl() == ConvDecl) {
1137 Convs.erase(I);
1138 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1139 && "conversion was found multiple times in unresolved set");
1140 return;
1141 }
1142 }
1143
1144 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001145}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001146
Douglas Gregorf6b11852009-10-08 15:14:33 +00001147CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001148 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001149 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1150
1151 return 0;
1152}
1153
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001154MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1155 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1156}
1157
Douglas Gregorf6b11852009-10-08 15:14:33 +00001158void
1159CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1160 TemplateSpecializationKind TSK) {
1161 assert(TemplateOrInstantiation.isNull() &&
1162 "Previous template or instantiation?");
1163 assert(!isa<ClassTemplateSpecializationDecl>(this));
1164 TemplateOrInstantiation
1165 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1166}
1167
Anders Carlssonb13e3572009-12-07 06:33:48 +00001168TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1169 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001170 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1171 return Spec->getSpecializationKind();
1172
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001173 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001174 return MSInfo->getTemplateSpecializationKind();
1175
1176 return TSK_Undeclared;
1177}
1178
1179void
1180CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1181 if (ClassTemplateSpecializationDecl *Spec
1182 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1183 Spec->setSpecializationKind(TSK);
1184 return;
1185 }
1186
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001187 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001188 MSInfo->setTemplateSpecializationKind(TSK);
1189 return;
1190 }
1191
David Blaikieb219cfc2011-09-23 05:06:16 +00001192 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001193}
1194
Douglas Gregor1d110e02010-07-01 14:13:13 +00001195CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1196 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001197 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001198
1199 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001200 = Context.DeclarationNames.getCXXDestructorName(
1201 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001202
John McCallc0bf4622010-02-23 00:48:20 +00001203 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001204 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001205 if (I == E)
1206 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001208 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001209 return Dtor;
1210}
1211
Douglas Gregorda2142f2011-02-19 18:51:44 +00001212void CXXRecordDecl::completeDefinition() {
1213 completeDefinition(0);
1214}
1215
1216void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1217 RecordDecl::completeDefinition();
1218
David Blaikie4e4d0842012-03-11 07:00:24 +00001219 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001220 // Objective-C Automatic Reference Counting:
1221 // If a class has a non-static data member of Objective-C pointer
1222 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001223 // default constructor (if any), copy constructor, move constructor,
1224 // copy assignment operator, move assignment operator, and destructor are
1225 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001226 struct DefinitionData &Data = data();
1227 Data.PlainOldData = false;
1228 Data.HasTrivialDefaultConstructor = false;
1229 Data.HasTrivialCopyConstructor = false;
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001230 Data.HasTrivialMoveConstructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001231 Data.HasTrivialCopyAssignment = false;
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001232 Data.HasTrivialMoveAssignment = false;
John McCallf85e1932011-06-15 23:02:42 +00001233 Data.HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +00001234 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001235 }
1236
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001237 // If the class may be abstract (but hasn't been marked as such), check for
1238 // any pure final overriders.
1239 if (mayBeAbstract()) {
1240 CXXFinalOverriderMap MyFinalOverriders;
1241 if (!FinalOverriders) {
1242 getFinalOverriders(MyFinalOverriders);
1243 FinalOverriders = &MyFinalOverriders;
1244 }
1245
1246 bool Done = false;
1247 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1248 MEnd = FinalOverriders->end();
1249 M != MEnd && !Done; ++M) {
1250 for (OverridingMethods::iterator SO = M->second.begin(),
1251 SOEnd = M->second.end();
1252 SO != SOEnd && !Done; ++SO) {
1253 assert(SO->second.size() > 0 &&
1254 "All virtual functions have overridding virtual functions");
1255
1256 // C++ [class.abstract]p4:
1257 // A class is abstract if it contains or inherits at least one
1258 // pure virtual function for which the final overrider is pure
1259 // virtual.
1260 if (SO->second.front().Method->isPure()) {
1261 data().Abstract = true;
1262 Done = true;
1263 break;
1264 }
1265 }
1266 }
1267 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001268
1269 // Set access bits correctly on the directly-declared conversions.
1270 for (UnresolvedSetIterator I = data().Conversions.begin(),
1271 E = data().Conversions.end();
1272 I != E; ++I)
1273 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001274}
1275
1276bool CXXRecordDecl::mayBeAbstract() const {
1277 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1278 isDependentContext())
1279 return false;
1280
1281 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1282 BEnd = bases_end();
1283 B != BEnd; ++B) {
1284 CXXRecordDecl *BaseDecl
1285 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1286 if (BaseDecl->isAbstract())
1287 return true;
1288 }
1289
1290 return false;
1291}
1292
David Blaikie99ba9e32011-12-20 02:48:34 +00001293void CXXMethodDecl::anchor() { }
1294
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001295static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1296 const CXXMethodDecl *BaseMD) {
1297 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1298 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1299 const CXXMethodDecl *MD = *I;
1300 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1301 return true;
1302 if (recursivelyOverrides(MD, BaseMD))
1303 return true;
1304 }
1305 return false;
1306}
1307
1308CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001309CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1310 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001311 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1312 return this;
1313
1314 // Lookup doesn't work for destructors, so handle them separately.
1315 if (isa<CXXDestructorDecl>(this)) {
1316 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001317 if (MD) {
1318 if (recursivelyOverrides(MD, this))
1319 return MD;
1320 if (MayBeBase && recursivelyOverrides(this, MD))
1321 return MD;
1322 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001323 return NULL;
1324 }
1325
1326 lookup_const_result Candidates = RD->lookup(getDeclName());
1327 for (NamedDecl * const * I = Candidates.first; I != Candidates.second; ++I) {
1328 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1329 if (!MD)
1330 continue;
1331 if (recursivelyOverrides(MD, this))
1332 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001333 if (MayBeBase && recursivelyOverrides(this, MD))
1334 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001335 }
1336
1337 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1338 E = RD->bases_end(); I != E; ++I) {
1339 const RecordType *RT = I->getType()->getAs<RecordType>();
1340 if (!RT)
1341 continue;
1342 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1343 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1344 if (T)
1345 return T;
1346 }
1347
1348 return NULL;
1349}
1350
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001351CXXMethodDecl *
1352CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001353 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001354 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001355 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001356 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001357 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001358 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001359 isStatic, SCAsWritten, isInline, isConstexpr,
1360 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001361}
1362
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001363CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1364 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1365 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1366 DeclarationNameInfo(), QualType(),
1367 0, false, SC_None, false, false,
1368 SourceLocation());
1369}
1370
Douglas Gregor90916562009-09-29 18:16:17 +00001371bool CXXMethodDecl::isUsualDeallocationFunction() const {
1372 if (getOverloadedOperator() != OO_Delete &&
1373 getOverloadedOperator() != OO_Array_Delete)
1374 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001375
1376 // C++ [basic.stc.dynamic.deallocation]p2:
1377 // A template instance is never a usual deallocation function,
1378 // regardless of its signature.
1379 if (getPrimaryTemplate())
1380 return false;
1381
Douglas Gregor90916562009-09-29 18:16:17 +00001382 // C++ [basic.stc.dynamic.deallocation]p2:
1383 // If a class T has a member deallocation function named operator delete
1384 // with exactly one parameter, then that function is a usual (non-placement)
1385 // deallocation function. [...]
1386 if (getNumParams() == 1)
1387 return true;
1388
1389 // C++ [basic.stc.dynamic.deallocation]p2:
1390 // [...] If class T does not declare such an operator delete but does
1391 // declare a member deallocation function named operator delete with
1392 // exactly two parameters, the second of which has type std::size_t (18.1),
1393 // then this function is a usual deallocation function.
1394 ASTContext &Context = getASTContext();
1395 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001396 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1397 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001398 return false;
1399
1400 // This function is a usual deallocation function if there are no
1401 // single-parameter deallocation functions of the same kind.
1402 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1403 R.first != R.second; ++R.first) {
1404 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1405 if (FD->getNumParams() == 1)
1406 return false;
1407 }
1408
1409 return true;
1410}
1411
Douglas Gregor06a9f362010-05-01 20:49:11 +00001412bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001413 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001414 // A user-declared copy assignment operator X::operator= is a non-static
1415 // non-template member function of class X with exactly one parameter of
1416 // type X, X&, const X&, volatile X& or const volatile X&.
1417 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1418 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001419 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001420 return false;
1421
1422 QualType ParamType = getParamDecl(0)->getType();
1423 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1424 ParamType = Ref->getPointeeType();
1425
1426 ASTContext &Context = getASTContext();
1427 QualType ClassType
1428 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1429 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1430}
1431
Sean Huntffe37fd2011-05-25 20:50:04 +00001432bool CXXMethodDecl::isMoveAssignmentOperator() const {
1433 // C++0x [class.copy]p19:
1434 // A user-declared move assignment operator X::operator= is a non-static
1435 // non-template member function of class X with exactly one parameter of type
1436 // X&&, const X&&, volatile X&&, or const volatile X&&.
1437 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1438 getPrimaryTemplate() || getDescribedFunctionTemplate())
1439 return false;
1440
1441 QualType ParamType = getParamDecl(0)->getType();
1442 if (!isa<RValueReferenceType>(ParamType))
1443 return false;
1444 ParamType = ParamType->getPointeeType();
1445
1446 ASTContext &Context = getASTContext();
1447 QualType ClassType
1448 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1449 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1450}
1451
Anders Carlsson05eb2442009-05-16 23:58:37 +00001452void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001453 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001454 assert(!MD->getParent()->isDependentContext() &&
1455 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001456 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001457
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001458 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001459}
1460
1461CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001462 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001463 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001464}
1465
1466CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001467 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001468 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001469}
1470
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001471unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001472 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001473 return getASTContext().overridden_methods_size(this);
1474}
1475
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001476QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001477 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1478 // If the member function is declared const, the type of this is const X*,
1479 // if the member function is declared volatile, the type of this is
1480 // volatile X*, and if the member function is declared const volatile,
1481 // the type of this is const volatile X*.
1482
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001483 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001484
John McCall3cb0ebd2010-03-10 03:28:59 +00001485 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001486 ClassTy = C.getQualifiedType(ClassTy,
1487 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001488 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001489}
1490
Eli Friedmand7d7f672009-12-06 20:50:05 +00001491bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001492 // If this function is a template instantiation, look at the template from
1493 // which it was instantiated.
1494 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1495 if (!CheckFn)
1496 CheckFn = this;
1497
Eli Friedmand7d7f672009-12-06 20:50:05 +00001498 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001499 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001500}
1501
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001502bool CXXMethodDecl::isLambdaStaticInvoker() const {
1503 return getParent()->isLambda() &&
1504 getIdentifier() && getIdentifier()->getName() == "__invoke";
1505}
1506
1507
Sean Huntcbb67482011-01-08 20:30:50 +00001508CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1509 TypeSourceInfo *TInfo, bool IsVirtual,
1510 SourceLocation L, Expr *Init,
1511 SourceLocation R,
1512 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001513 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001514 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1515 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001516{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001517}
1518
Sean Huntcbb67482011-01-08 20:30:50 +00001519CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1520 FieldDecl *Member,
1521 SourceLocation MemberLoc,
1522 SourceLocation L, Expr *Init,
1523 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001524 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001525 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001526 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1527{
1528}
1529
Sean Huntcbb67482011-01-08 20:30:50 +00001530CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1531 IndirectFieldDecl *Member,
1532 SourceLocation MemberLoc,
1533 SourceLocation L, Expr *Init,
1534 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001535 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001536 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001537 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001538{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001539}
1540
Sean Huntcbb67482011-01-08 20:30:50 +00001541CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001542 TypeSourceInfo *TInfo,
1543 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001544 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001545 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1546 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001547 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1548{
1549}
1550
1551CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001552 FieldDecl *Member,
1553 SourceLocation MemberLoc,
1554 SourceLocation L, Expr *Init,
1555 SourceLocation R,
1556 VarDecl **Indices,
1557 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001558 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001559 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001560 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001561{
1562 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1563 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1564}
1565
Sean Huntcbb67482011-01-08 20:30:50 +00001566CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1567 FieldDecl *Member,
1568 SourceLocation MemberLoc,
1569 SourceLocation L, Expr *Init,
1570 SourceLocation R,
1571 VarDecl **Indices,
1572 unsigned NumIndices) {
1573 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001574 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001575 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001576 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1577 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001578}
1579
Sean Huntcbb67482011-01-08 20:30:50 +00001580TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001581 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001582 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001583 else
1584 return TypeLoc();
1585}
1586
Sean Huntcbb67482011-01-08 20:30:50 +00001587const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001588 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001589 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001590 else
1591 return 0;
1592}
1593
Sean Huntcbb67482011-01-08 20:30:50 +00001594SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001595 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001596 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001597
1598 if (isInClassMemberInitializer())
1599 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001600
Douglas Gregor76852c22011-11-01 01:16:03 +00001601 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1602 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1603
1604 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001605}
1606
Sean Huntcbb67482011-01-08 20:30:50 +00001607SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001608 if (isInClassMemberInitializer()) {
1609 FieldDecl *D = getAnyMember();
1610 if (Expr *I = D->getInClassInitializer())
1611 return I->getSourceRange();
1612 return SourceRange();
1613 }
1614
Douglas Gregor802ab452009-12-02 22:36:29 +00001615 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001616}
1617
David Blaikie99ba9e32011-12-20 02:48:34 +00001618void CXXConstructorDecl::anchor() { }
1619
Douglas Gregorb48fe382008-10-31 09:07:45 +00001620CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001621CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1622 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1623 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1624 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001625}
1626
1627CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001628CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001629 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001630 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001631 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001632 bool isExplicit, bool isInline,
1633 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001634 assert(NameInfo.getName().getNameKind()
1635 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001636 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001637 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001638 isExplicit, isInline, isImplicitlyDeclared,
1639 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001640}
1641
Douglas Gregor76852c22011-11-01 01:16:03 +00001642CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1643 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1644 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1645 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1646 return Construct->getConstructor();
1647
1648 return 0;
1649}
1650
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001651bool CXXConstructorDecl::isDefaultConstructor() const {
1652 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001653 // A default constructor for a class X is a constructor of class
1654 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001655 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001656 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001657}
1658
Mike Stump1eb44332009-09-09 15:08:12 +00001659bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001660CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001661 return isCopyOrMoveConstructor(TypeQuals) &&
1662 getParamDecl(0)->getType()->isLValueReferenceType();
1663}
1664
1665bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1666 return isCopyOrMoveConstructor(TypeQuals) &&
1667 getParamDecl(0)->getType()->isRValueReferenceType();
1668}
1669
1670/// \brief Determine whether this is a copy or move constructor.
1671bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001672 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001673 // A non-template constructor for class X is a copy constructor
1674 // if its first parameter is of type X&, const X&, volatile X& or
1675 // const volatile X&, and either there are no other parameters
1676 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001677 // C++0x [class.copy]p3:
1678 // A non-template constructor for class X is a move constructor if its
1679 // first parameter is of type X&&, const X&&, volatile X&&, or
1680 // const volatile X&&, and either there are no other parameters or else
1681 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001682 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001683 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001684 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001685 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001686 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001687
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001688 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001689
1690 // Do we have a reference type?
1691 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001692 if (!ParamRefType)
1693 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001694
Douglas Gregorfd476482009-11-13 23:59:09 +00001695 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001696 ASTContext &Context = getASTContext();
1697
Douglas Gregorfd476482009-11-13 23:59:09 +00001698 CanQualType PointeeType
1699 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001700 CanQualType ClassTy
1701 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001702 if (PointeeType.getUnqualifiedType() != ClassTy)
1703 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001704
John McCall0953e762009-09-24 19:53:00 +00001705 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001706
1707 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001708 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001709 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001710}
1711
Anders Carlssonfaccd722009-08-28 16:57:08 +00001712bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001713 // C++ [class.conv.ctor]p1:
1714 // A constructor declared without the function-specifier explicit
1715 // that can be called with a single parameter specifies a
1716 // conversion from the type of its first parameter to the type of
1717 // its class. Such a constructor is called a converting
1718 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001719 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001720 return false;
1721
Mike Stump1eb44332009-09-09 15:08:12 +00001722 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001723 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001724 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001725 (getNumParams() > 1 &&
1726 (getParamDecl(1)->hasDefaultArg() ||
1727 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001728}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001729
Douglas Gregor6493cc52010-11-08 17:16:59 +00001730bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001731 if ((getNumParams() < 1) ||
1732 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1733 (getPrimaryTemplate() == 0) ||
1734 (getDescribedFunctionTemplate() != 0))
1735 return false;
1736
1737 const ParmVarDecl *Param = getParamDecl(0);
1738
1739 ASTContext &Context = getASTContext();
1740 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1741
Douglas Gregor66724ea2009-11-14 01:20:54 +00001742 // Is it the same as our our class type?
1743 CanQualType ClassTy
1744 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1745 if (ParamType.getUnqualifiedType() != ClassTy)
1746 return false;
1747
1748 return true;
1749}
1750
Sebastian Redlf677ea32011-02-05 19:23:19 +00001751const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1752 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001753 method_iterator It = getASTContext().overridden_methods_begin(this);
1754 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001755 return 0;
1756
1757 return cast<CXXConstructorDecl>(*It);
1758}
1759
1760void
1761CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1762 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001763 assert(getASTContext().overridden_methods_size(this) == 0 &&
1764 "Base ctor already set.");
1765 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001766}
1767
David Blaikie99ba9e32011-12-20 02:48:34 +00001768void CXXDestructorDecl::anchor() { }
1769
Douglas Gregor42a552f2008-11-05 20:51:48 +00001770CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001771CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1772 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1773 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001774 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001775}
1776
1777CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001778CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001779 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001780 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001781 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001782 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001783 assert(NameInfo.getName().getNameKind()
1784 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001785 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001786 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001787 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001788}
1789
David Blaikie99ba9e32011-12-20 02:48:34 +00001790void CXXConversionDecl::anchor() { }
1791
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001792CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001793CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1794 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1795 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1796 QualType(), 0, false, false, false,
1797 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001798}
1799
1800CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001801CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001802 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001803 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001804 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001805 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001806 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001807 assert(NameInfo.getName().getNameKind()
1808 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001809 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001810 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001811 isInline, isExplicit, isConstexpr,
1812 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001813}
1814
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001815bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1816 return isImplicit() && getParent()->isLambda() &&
1817 getConversionType()->isBlockPointerType();
1818}
1819
David Blaikie99ba9e32011-12-20 02:48:34 +00001820void LinkageSpecDecl::anchor() { }
1821
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001822LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001823 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001824 SourceLocation ExternLoc,
1825 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001826 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001827 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001828 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001829}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001830
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001831LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1832 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1833 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1834 lang_c, SourceLocation());
1835}
1836
David Blaikie99ba9e32011-12-20 02:48:34 +00001837void UsingDirectiveDecl::anchor() { }
1838
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001839UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1840 SourceLocation L,
1841 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001842 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001843 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001844 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001845 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001846 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1847 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001848 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1849 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001850}
1851
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001852UsingDirectiveDecl *
1853UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1854 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1855 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1856 NestedNameSpecifierLoc(),
1857 SourceLocation(), 0, 0);
1858}
1859
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001860NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1861 if (NamespaceAliasDecl *NA =
1862 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1863 return NA->getNamespace();
1864 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1865}
1866
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001867void NamespaceDecl::anchor() { }
1868
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001869NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1870 SourceLocation StartLoc,
1871 SourceLocation IdLoc, IdentifierInfo *Id,
1872 NamespaceDecl *PrevDecl)
1873 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1874 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1875{
1876 setPreviousDeclaration(PrevDecl);
1877
1878 if (PrevDecl)
1879 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1880}
1881
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001882NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001883 bool Inline, SourceLocation StartLoc,
1884 SourceLocation IdLoc, IdentifierInfo *Id,
1885 NamespaceDecl *PrevDecl) {
1886 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001887}
1888
1889NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1890 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001891 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1892 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001893}
1894
David Blaikie99ba9e32011-12-20 02:48:34 +00001895void NamespaceAliasDecl::anchor() { }
1896
Mike Stump1eb44332009-09-09 15:08:12 +00001897NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001898 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001899 SourceLocation AliasLoc,
1900 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001901 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001902 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001903 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001904 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1905 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001906 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1907 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001908}
1909
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001910NamespaceAliasDecl *
1911NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1912 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1913 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1914 NestedNameSpecifierLoc(),
1915 SourceLocation(), 0);
1916}
1917
David Blaikie99ba9e32011-12-20 02:48:34 +00001918void UsingShadowDecl::anchor() { }
1919
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001920UsingShadowDecl *
1921UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1922 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1923 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1924}
1925
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001926UsingDecl *UsingShadowDecl::getUsingDecl() const {
1927 const UsingShadowDecl *Shadow = this;
1928 while (const UsingShadowDecl *NextShadow =
1929 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1930 Shadow = NextShadow;
1931 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1932}
1933
David Blaikie99ba9e32011-12-20 02:48:34 +00001934void UsingDecl::anchor() { }
1935
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001936void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1937 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1938 "declaration already in set");
1939 assert(S->getUsingDecl() == this);
1940
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001941 if (FirstUsingShadow.getPointer())
1942 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1943 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001944}
1945
1946void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1947 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1948 "declaration not in set");
1949 assert(S->getUsingDecl() == this);
1950
1951 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1952
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001953 if (FirstUsingShadow.getPointer() == S) {
1954 FirstUsingShadow.setPointer(
1955 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001956 S->UsingOrNextShadow = this;
1957 return;
1958 }
1959
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001960 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001961 while (Prev->UsingOrNextShadow != S)
1962 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1963 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1964 S->UsingOrNextShadow = this;
1965}
1966
Douglas Gregordc355712011-02-25 00:36:19 +00001967UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1968 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001969 const DeclarationNameInfo &NameInfo,
1970 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001971 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001972}
1973
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001974UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1975 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1976 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1977 DeclarationNameInfo(), false);
1978}
1979
David Blaikie99ba9e32011-12-20 02:48:34 +00001980void UnresolvedUsingValueDecl::anchor() { }
1981
John McCall7ba107a2009-11-18 02:36:19 +00001982UnresolvedUsingValueDecl *
1983UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1984 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001985 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001986 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001987 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001988 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001989}
1990
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001991UnresolvedUsingValueDecl *
1992UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1993 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1994 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1995 NestedNameSpecifierLoc(),
1996 DeclarationNameInfo());
1997}
1998
David Blaikie99ba9e32011-12-20 02:48:34 +00001999void UnresolvedUsingTypenameDecl::anchor() { }
2000
John McCall7ba107a2009-11-18 02:36:19 +00002001UnresolvedUsingTypenameDecl *
2002UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2003 SourceLocation UsingLoc,
2004 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002005 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002006 SourceLocation TargetNameLoc,
2007 DeclarationName TargetName) {
2008 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002009 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002010 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00002011}
2012
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002013UnresolvedUsingTypenameDecl *
2014UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2015 void *Mem = AllocateDeserializedDecl(C, ID,
2016 sizeof(UnresolvedUsingTypenameDecl));
2017 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2018 SourceLocation(),
2019 NestedNameSpecifierLoc(),
2020 SourceLocation(),
2021 0);
2022}
2023
David Blaikie99ba9e32011-12-20 02:48:34 +00002024void StaticAssertDecl::anchor() { }
2025
Anders Carlssonfb311762009-03-14 00:25:26 +00002026StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002027 SourceLocation StaticAssertLoc,
2028 Expr *AssertExpr,
2029 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002030 SourceLocation RParenLoc,
2031 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002032 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002033 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00002034}
2035
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002036StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2037 unsigned ID) {
2038 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00002039 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
2040 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002041}
2042
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002043static const char *getAccessName(AccessSpecifier AS) {
2044 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002045 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002046 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002047 case AS_public:
2048 return "public";
2049 case AS_private:
2050 return "private";
2051 case AS_protected:
2052 return "protected";
2053 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002054 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002055}
2056
2057const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2058 AccessSpecifier AS) {
2059 return DB << getAccessName(AS);
2060}
Richard Smithf15fda02012-02-02 01:16:57 +00002061
2062const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2063 AccessSpecifier AS) {
2064 return DB << getAccessName(AS);
2065}