blob: a0ff6508ce2318788cab915fb0fd592b7ad83379 [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 Smith426391c2012-11-16 00:53:38 +0000247 if (!BaseClassDecl->hasTrivialMoveConstructor())
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000248 data().HasTrivialMoveConstructor = false;
249
250 // C++0x [class.copy]p27:
251 // A copy/move assignment operator for class X is trivial if [...]
252 // [...]
253 // -- the assignment operator selected to copy/move each direct base
254 // class subobject is trivial, and
255 // FIXME: C++0x: We need to only consider the selected operator instead
256 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000257 if (!BaseClassDecl->hasTrivialCopyAssignment())
258 data().HasTrivialCopyAssignment = false;
Richard Smith426391c2012-11-16 00:53:38 +0000259 if (!BaseClassDecl->hasTrivialMoveAssignment())
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000260 data().HasTrivialMoveAssignment = false;
Richard Smith61802452011-12-22 02:22:31 +0000261
262 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000263 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000264 // requirements of a constexpr constructor, the implicitly-defined
265 // default constructor is constexpr.
266 if (!BaseClassDecl->hasConstexprDefaultConstructor())
267 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000268 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000269
270 // C++ [class.ctor]p3:
271 // A destructor is trivial if all the direct base classes of its class
272 // have trivial destructors.
273 if (!BaseClassDecl->hasTrivialDestructor())
274 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000275
276 if (!BaseClassDecl->hasIrrelevantDestructor())
277 data().HasIrrelevantDestructor = false;
278
John McCallf85e1932011-06-15 23:02:42 +0000279 // A class has an Objective-C object member if... or any of its bases
280 // has an Objective-C object member.
281 if (BaseClassDecl->hasObjectMember())
282 setHasObjectMember(true);
283
Douglas Gregor2bb11012011-05-13 01:05:07 +0000284 // Keep track of the presence of mutable fields.
285 if (BaseClassDecl->hasMutableFields())
286 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000287 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000288
289 if (VBases.empty())
290 return;
291
292 // Create base specifier for any direct or indirect virtual bases.
293 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
294 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000295 for (int I = 0, E = VBases.size(); I != E; ++I)
296 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000297}
298
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000299/// Callback function for CXXRecordDecl::forallBases that acknowledges
300/// that it saw a base class.
301static bool SawBase(const CXXRecordDecl *, void *) {
302 return true;
303}
304
305bool CXXRecordDecl::hasAnyDependentBases() const {
306 if (!isDependentContext())
307 return false;
308
309 return !forallBases(SawBase, 0);
310}
311
Sean Huntffe37fd2011-05-25 20:50:04 +0000312bool CXXRecordDecl::hasConstCopyConstructor() const {
313 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000314}
315
Chandler Carruthb7e95892011-04-23 10:47:28 +0000316bool CXXRecordDecl::isTriviallyCopyable() const {
317 // C++0x [class]p5:
318 // A trivially copyable class is a class that:
319 // -- has no non-trivial copy constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000320 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000321 // -- has no non-trivial move constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000322 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000323 // -- has no non-trivial copy assignment operators,
Richard Smith426391c2012-11-16 00:53:38 +0000324 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000325 // -- has no non-trivial move assignment operators, and
Richard Smith426391c2012-11-16 00:53:38 +0000326 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000327 // -- has a trivial destructor.
328 if (!hasTrivialDestructor()) return false;
329
330 return true;
331}
332
Douglas Gregor0d405db2010-07-01 20:59:04 +0000333/// \brief Perform a simplistic form of overload resolution that only considers
334/// cv-qualifiers on a single parameter, and return the best overload candidate
335/// (if there is one).
336static CXXMethodDecl *
337GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000338 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000339 if (Cands.empty())
340 return 0;
341 if (Cands.size() == 1)
342 return Cands[0].first;
343
344 unsigned Best = 0, N = Cands.size();
345 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000346 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000347 Best = I;
348
Benjamin Kramer2cd7f412012-07-30 15:53:26 +0000349 for (unsigned I = 0; I != N; ++I)
350 if (I != Best && Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000351 return 0;
352
353 return Cands[Best].first;
354}
355
Sean Huntffe37fd2011-05-25 20:50:04 +0000356CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
357 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000358 QualType ClassType
359 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000360 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000361 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000362 Context.getCanonicalType(ClassType));
363 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000364 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000365 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000366 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000367 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000368 // C++ [class.copy]p2:
369 // A non-template constructor for class X is a copy constructor if [...]
370 if (isa<FunctionTemplateDecl>(*Con))
371 continue;
372
Douglas Gregor0d405db2010-07-01 20:59:04 +0000373 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
374 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000375 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
376 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000377 Found.push_back(std::make_pair(
378 const_cast<CXXConstructorDecl *>(Constructor),
379 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000380 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000381 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000382
383 return cast_or_null<CXXConstructorDecl>(
384 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000385}
386
Sean Huntffe37fd2011-05-25 20:50:04 +0000387CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
388 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
389 if (I->isMoveConstructor())
David Blaikie581deb32012-06-06 20:45:41 +0000390 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000391
392 return 0;
393}
394
Douglas Gregorb87786f2010-07-01 17:48:08 +0000395CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
396 ASTContext &Context = getASTContext();
397 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
398 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
399
Chris Lattner5f9e2722011-07-23 10:55:15 +0000400 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000401 DeclContext::lookup_const_iterator Op, OpEnd;
402 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
403 // C++ [class.copy]p9:
404 // A user-declared copy assignment operator is a non-static non-template
405 // member function of class X with exactly one parameter of type X, X&,
406 // const X&, volatile X& or const volatile X&.
407 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
408 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
409 continue;
410
411 const FunctionProtoType *FnType
412 = Method->getType()->getAs<FunctionProtoType>();
413 assert(FnType && "Overloaded operator has no prototype.");
414 // Don't assert on this; an invalid decl might have been left in the AST.
415 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
416 continue;
417
418 QualType ArgType = FnType->getArgType(0);
419 Qualifiers Quals;
420 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
421 ArgType = Ref->getPointeeType();
422 // If we have a const argument and we have a reference to a non-const,
423 // this function does not match.
424 if (ArgIsConst && !ArgType.isConstQualified())
425 continue;
426
427 Quals = ArgType.getQualifiers();
428 } else {
429 // By-value copy-assignment operators are treated like const X&
430 // copy-assignment operators.
431 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
432 }
433
434 if (!Context.hasSameUnqualifiedType(ArgType, Class))
435 continue;
436
437 // Save this copy-assignment operator. It might be "the one".
438 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
439 }
440
441 // Use a simplistic form of overload resolution to find the candidate.
442 return GetBestOverloadCandidateSimple(Found);
443}
444
Sean Huntffe37fd2011-05-25 20:50:04 +0000445CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
446 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
447 if (I->isMoveAssignmentOperator())
David Blaikie581deb32012-06-06 20:45:41 +0000448 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000449
450 return 0;
451}
452
Douglas Gregor21386642010-09-28 21:55:22 +0000453void CXXRecordDecl::markedVirtualFunctionPure() {
454 // C++ [class.abstract]p2:
455 // A class is abstract if it has at least one pure virtual function.
456 data().Abstract = true;
457}
458
Richard Smith3f5f5582012-06-08 21:09:22 +0000459void CXXRecordDecl::markedConstructorConstexpr(CXXConstructorDecl *CD) {
Richard Smithd3861ce2012-06-10 07:07:24 +0000460 if (!CD->isCopyOrMoveConstructor())
Richard Smith3f5f5582012-06-08 21:09:22 +0000461 data().HasConstexprNonCopyMoveConstructor = true;
462
463 if (CD->isDefaultConstructor())
464 data().HasConstexprDefaultConstructor = true;
465}
466
Douglas Gregor21386642010-09-28 21:55:22 +0000467void CXXRecordDecl::addedMember(Decl *D) {
Joao Matos17d35c32012-08-31 22:18:20 +0000468 if (!D->isImplicit() &&
469 !isa<FieldDecl>(D) &&
470 !isa<IndirectFieldDecl>(D) &&
471 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
472 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
473 data().HasOnlyCMembers = false;
474
475 // Ignore friends and invalid declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000476 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000477 return;
478
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000479 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
480 if (FunTmpl)
481 D = FunTmpl->getTemplatedDecl();
482
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000483 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
484 if (Method->isVirtual()) {
485 // C++ [dcl.init.aggr]p1:
486 // An aggregate is an array or a class with [...] no virtual functions.
487 data().Aggregate = false;
488
489 // C++ [class]p4:
490 // A POD-struct is an aggregate class...
491 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000492
493 // Virtual functions make the class non-empty.
494 // FIXME: Standard ref?
495 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000496
497 // C++ [class.virtual]p1:
498 // A class that declares or inherits a virtual function is called a
499 // polymorphic class.
500 data().Polymorphic = true;
501
Sean Hunt023df372011-05-09 18:22:59 +0000502 // C++0x [class.ctor]p5
503 // A default constructor is trivial [...] if:
504 // -- its class has no virtual functions [...]
505 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000506
507 // C++0x [class.copy]p13:
508 // A copy/move constructor for class X is trivial if [...]
509 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000510 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000511 data().HasTrivialMoveConstructor = false;
512
513 // C++0x [class.copy]p27:
514 // A copy/move assignment operator for class X is trivial if [...]
515 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000516 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000517 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000518
Chandler Carrutha8225442011-04-30 09:17:45 +0000519 // C++0x [class]p7:
520 // A standard-layout class is a class that: [...]
521 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000522 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000523 }
524 }
525
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000526 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000527 // Notify that an implicit member was added after the definition
528 // was completed.
529 if (!isBeingDefined())
530 if (ASTMutationListener *L = getASTMutationListener())
531 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000532
Sean Huntffe37fd2011-05-25 20:50:04 +0000533 // If this is a special member function, note that it was added and then
534 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000535 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith61802452011-12-22 02:22:31 +0000536 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000537 data().DeclaredDefaultConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000538 if (Constructor->isConstexpr()) {
539 data().HasConstexprDefaultConstructor = true;
540 data().HasConstexprNonCopyMoveConstructor = true;
541 }
542 } else if (Constructor->isCopyConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000543 data().DeclaredCopyConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000544 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000545 data().DeclaredMoveConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000546 } else
Sean Huntffe37fd2011-05-25 20:50:04 +0000547 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000548 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000549 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000550 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000551 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000552 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
553 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000554 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000555 else if (Method->isMoveAssignmentOperator())
556 data().DeclaredMoveAssignment = true;
557 else
558 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000559 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000560 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000561
Sean Huntffe37fd2011-05-25 20:50:04 +0000562NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000563 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000564 }
565
566 // Handle (user-declared) constructors.
567 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
568 // Note that we have a user-declared constructor.
569 data().UserDeclaredConstructor = true;
570
Richard Smith017ab772011-09-05 02:13:09 +0000571 // Technically, "user-provided" is only defined for special member
572 // functions, but the intent of the standard is clearly that it should apply
573 // to all functions.
574 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000575
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000576 if (Constructor->isDefaultConstructor()) {
577 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000578 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000579 // C++0x [class.ctor]p5:
580 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000581 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000582 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000583 }
Richard Smith61802452011-12-22 02:22:31 +0000584 if (Constructor->isConstexpr()) {
585 data().HasConstexprDefaultConstructor = true;
586 data().HasConstexprNonCopyMoveConstructor = true;
587 }
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000588 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000589
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000590 // Note when we have a user-declared copy or move constructor, which will
591 // suppress the implicit declaration of those constructors.
592 if (!FunTmpl) {
593 if (Constructor->isCopyConstructor()) {
594 data().UserDeclaredCopyConstructor = true;
595 data().DeclaredCopyConstructor = true;
596
597 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000598 // A copy/move constructor for class X is trivial if it is not
599 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000600 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000601 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000602 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000603 data().UserDeclaredMoveConstructor = true;
604 data().DeclaredMoveConstructor = true;
605
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000606 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000607 // A copy/move constructor for class X is trivial if it is not
608 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000609 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000610 data().HasTrivialMoveConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000611 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000612 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000613 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
614 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000615 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000616 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000617 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000618
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000619 // C++ [dcl.init.aggr]p1:
620 // An aggregate is an array or a class with no user-declared
621 // constructors [...].
622 // C++0x [dcl.init.aggr]p1:
623 // An aggregate is an array or a class with no user-provided
624 // constructors [...].
David Blaikie4e4d0842012-03-11 07:00:24 +0000625 if (!getASTContext().getLangOpts().CPlusPlus0x || UserProvided)
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000626 data().Aggregate = false;
627
628 // C++ [class]p4:
629 // A POD-struct is an aggregate class [...]
630 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
631 // type is technically an aggregate in C++0x since it wouldn't be in 03.
632 data().PlainOldData = false;
633
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000634 return;
635 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000636
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000637 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000638 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000639 data().DeclaredDestructor = true;
640 data().UserDeclaredDestructor = true;
Richard Smithdfefb842012-02-25 07:33:38 +0000641 data().HasIrrelevantDestructor = false;
642
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000643 // C++ [class]p4:
644 // A POD-struct is an aggregate class that has [...] no user-defined
645 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000646 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000647 data().PlainOldData = false;
648
Douglas Gregor45fa5602011-11-07 20:56:01 +0000649 // C++11 [class.dtor]p5:
650 // A destructor is trivial if it is not user-provided and if
651 // -- the destructor is not virtual.
Richard Smithd3861ce2012-06-10 07:07:24 +0000652 if (DD->isUserProvided() || DD->isVirtual())
Sean Huntcf34e752011-05-16 22:41:40 +0000653 data().HasTrivialDestructor = false;
Richard Smithd3861ce2012-06-10 07:07:24 +0000654
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000655 return;
656 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000657
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000658 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000659 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000660 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000661 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000662 // A POD-struct is an aggregate class that [...] has no user-defined
663 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000664 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000665 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000666
Sean Huntffe37fd2011-05-25 20:50:04 +0000667 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000668
Sean Huntffe37fd2011-05-25 20:50:04 +0000669 // Suppress the implicit declaration of a copy constructor.
670 data().UserDeclaredCopyAssignment = true;
671 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000672
Sean Huntffe37fd2011-05-25 20:50:04 +0000673 // C++0x [class.copy]p27:
674 // A copy/move assignment operator for class X is trivial if it is
675 // neither user-provided nor deleted [...]
676 if (Method->isUserProvided())
677 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000678
Sean Huntffe37fd2011-05-25 20:50:04 +0000679 return;
680 }
681
682 if (Method->isMoveAssignmentOperator()) {
683 // This is an extension in C++03 mode, but we'll keep consistency by
684 // taking a move assignment operator to induce non-POD-ness
685 data().PlainOldData = false;
686
687 // This is a move assignment operator.
688 data().UserDeclaredMoveAssignment = true;
689 data().DeclaredMoveAssignment = true;
690
691 // C++0x [class.copy]p27:
692 // A copy/move assignment operator for class X is trivial if it is
693 // neither user-provided nor deleted [...]
694 if (Method->isUserProvided())
695 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000696 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000697
Douglas Gregore80622f2010-09-29 04:25:11 +0000698 // Keep the list of conversion functions up-to-date.
699 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
700 // We don't record specializations.
701 if (Conversion->getPrimaryTemplate())
702 return;
703
704 // FIXME: We intentionally don't use the decl's access here because it
705 // hasn't been set yet. That's really just a misdesign in Sema.
706
707 if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000708 if (FunTmpl->getPreviousDecl())
709 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000710 FunTmpl);
711 else
712 data().Conversions.addDecl(FunTmpl);
713 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000714 if (Conversion->getPreviousDecl())
715 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000716 Conversion);
717 else
718 data().Conversions.addDecl(Conversion);
719 }
720 }
721
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000722 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000723 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000724
725 // Handle non-static data members.
726 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000727 // C++ [class.bit]p2:
728 // A declaration for a bit-field that omits the identifier declares an
729 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
730 // initialized.
731 if (Field->isUnnamedBitfield())
732 return;
733
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000734 // C++ [dcl.init.aggr]p1:
735 // An aggregate is an array or a class (clause 9) with [...] no
736 // private or protected non-static data members (clause 11).
737 //
738 // A POD must be an aggregate.
739 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
740 data().Aggregate = false;
741 data().PlainOldData = false;
742 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000743
744 // C++0x [class]p7:
745 // A standard-layout class is a class that:
746 // [...]
747 // -- has the same access control for all non-static data members,
748 switch (D->getAccess()) {
749 case AS_private: data().HasPrivateFields = true; break;
750 case AS_protected: data().HasProtectedFields = true; break;
751 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000752 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000753 };
754 if ((data().HasPrivateFields + data().HasProtectedFields +
755 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000756 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000757
Douglas Gregor2bb11012011-05-13 01:05:07 +0000758 // Keep track of the presence of mutable fields.
759 if (Field->isMutable())
760 data().HasMutableFields = true;
761
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000762 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000763 // A POD struct is a class that is both a trivial class and a
764 // standard-layout class, and has no non-static data members of type
765 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000766 //
767 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
768 // that does not explicitly have no lifetime makes the class a non-POD.
769 // However, we delay setting PlainOldData to false in this case so that
770 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000771 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000772 // In this case, the class will become a non-POD class when we complete
773 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000774 ASTContext &Context = getASTContext();
775 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000776 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000777 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000778 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
779 setHasObjectMember(true);
780 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000781 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000782
Chandler Carrutha8225442011-04-30 09:17:45 +0000783 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000784 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000785
Chandler Carrutha8225442011-04-30 09:17:45 +0000786 // C++0x [class]p7:
787 // A standard-layout class is a class that:
788 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000789 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000790 }
791
Richard Smith86c3ae42012-02-13 03:54:03 +0000792 // Record if this field is the first non-literal or volatile field or base.
793 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000794 data().HasNonLiteralTypeFieldsOrBases = true;
795
Richard Smith7a614d82011-06-11 17:19:42 +0000796 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000797 data().HasInClassInitializer = true;
798
799 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000800 // A default constructor is trivial if [...] no non-static data member
801 // of its class has a brace-or-equal-initializer.
802 data().HasTrivialDefaultConstructor = false;
803
Richard Smithd079abf2012-05-07 01:07:30 +0000804 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000805 // An aggregate is a [...] class with [...] no
806 // brace-or-equal-initializers for non-static data members.
807 data().Aggregate = false;
808
Richard Smithd079abf2012-05-07 01:07:30 +0000809 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000810 // A POD struct is [...] a trivial class.
811 data().PlainOldData = false;
812 }
813
Douglas Gregor85606eb2010-09-28 20:50:54 +0000814 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
815 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
816 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000817 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000818 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000819 // -- for all the non-static data members of its class that are of
820 // class type (or array thereof), each such class has a trivial
821 // default constructor.
822 if (!FieldRec->hasTrivialDefaultConstructor())
823 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000824
825 // C++0x [class.copy]p13:
826 // A copy/move constructor for class X is trivial if [...]
827 // [...]
828 // -- for each non-static data member of X that is of class type (or
829 // an array thereof), the constructor selected to copy/move that
830 // member is trivial;
831 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000832 if (!FieldRec->hasTrivialCopyConstructor())
833 data().HasTrivialCopyConstructor = false;
Richard Smith426391c2012-11-16 00:53:38 +0000834 if (!FieldRec->hasTrivialMoveConstructor())
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000835 data().HasTrivialMoveConstructor = false;
836
837 // C++0x [class.copy]p27:
838 // A copy/move assignment operator for class X is trivial if [...]
839 // [...]
840 // -- for each non-static data member of X that is of class type (or
841 // an array thereof), the assignment operator selected to
842 // copy/move that member is trivial;
843 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000844 if (!FieldRec->hasTrivialCopyAssignment())
845 data().HasTrivialCopyAssignment = false;
Richard Smith426391c2012-11-16 00:53:38 +0000846 if (!FieldRec->hasTrivialMoveAssignment())
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000847 data().HasTrivialMoveAssignment = false;
848
Douglas Gregor85606eb2010-09-28 20:50:54 +0000849 if (!FieldRec->hasTrivialDestructor())
850 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000851 if (!FieldRec->hasIrrelevantDestructor())
852 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000853 if (FieldRec->hasObjectMember())
854 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000855
856 // C++0x [class]p7:
857 // A standard-layout class is a class that:
858 // -- has no non-static data members of type non-standard-layout
859 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000860 if (!FieldRec->isStandardLayout())
861 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000862
863 // C++0x [class]p7:
864 // A standard-layout class is a class that:
865 // [...]
866 // -- has no base classes of the same type as the first non-static
867 // data member.
868 // We don't want to expend bits in the state of the record decl
869 // tracking whether this is the first non-static data member so we
870 // cheat a bit and use some of the existing state: the empty bit.
871 // Virtual bases and virtual methods make a class non-empty, but they
872 // also make it non-standard-layout so we needn't check here.
873 // A non-empty base class may leave the class standard-layout, but not
874 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000875 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000876 // data member must come through here with Empty still true, and Empty
877 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000878 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000879 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
880 BE = bases_end();
881 BI != BE; ++BI) {
882 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000883 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000884 break;
885 }
886 }
887 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000888
889 // Keep track of the presence of mutable fields.
890 if (FieldRec->hasMutableFields())
891 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000892
893 // C++11 [class.copy]p13:
894 // If the implicitly-defined constructor would satisfy the
895 // requirements of a constexpr constructor, the implicitly-defined
896 // constructor is constexpr.
897 // C++11 [dcl.constexpr]p4:
898 // -- every constructor involved in initializing non-static data
899 // members [...] shall be a constexpr constructor
900 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000901 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000902 // The standard requires any in-class initializer to be a constant
903 // expression. We consider this to be a defect.
904 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000905 }
Richard Smith61802452011-12-22 02:22:31 +0000906 } else {
907 // Base element type of field is a non-class type.
Richard Smithd3861ce2012-06-10 07:07:24 +0000908 if (!T->isLiteralType() ||
909 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000910 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000911 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000912
913 // C++0x [class]p7:
914 // A standard-layout class is a class that:
915 // [...]
916 // -- either has no non-static data members in the most derived
917 // class and at most one base class with non-static data members,
918 // or has no base classes with non-static data members, and
919 // At this point we know that we have a non-static data member, so the last
920 // clause holds.
921 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000922 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000923
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000924 // If this is not a zero-length bit-field, then the class is not empty.
925 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000926 if (!Field->isBitField() ||
927 (!Field->getBitWidth()->isTypeDependent() &&
928 !Field->getBitWidth()->isValueDependent() &&
929 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000930 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000931 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000932 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000933
934 // Handle using declarations of conversion functions.
935 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
936 if (Shadow->getDeclName().getNameKind()
937 == DeclarationName::CXXConversionFunctionName)
938 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Joao Matos17d35c32012-08-31 22:18:20 +0000939}
940
941bool CXXRecordDecl::isCLike() const {
942 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
943 !TemplateOrInstantiation.isNull())
944 return false;
945 if (!hasDefinition())
946 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000947
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000948 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000949}
950
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000951void CXXRecordDecl::getCaptureFields(
952 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000953 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000954 Captures.clear();
955 ThisCapture = 0;
956
Douglas Gregorda8962a2012-02-13 15:44:47 +0000957 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000958 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000959 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000960 C != CEnd; ++C, ++Field) {
961 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +0000962 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000963 continue;
964 }
965
David Blaikie581deb32012-06-06 20:45:41 +0000966 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000967 }
968}
969
970
John McCallb05b5f32010-03-15 09:07:48 +0000971static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
972 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000973 if (isa<UsingShadowDecl>(Conv))
974 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000975 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
976 T = ConvTemp->getTemplatedDecl()->getResultType();
977 else
978 T = cast<CXXConversionDecl>(Conv)->getConversionType();
979 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000980}
981
John McCallb05b5f32010-03-15 09:07:48 +0000982/// Collect the visible conversions of a base class.
983///
James Dennetta1253502012-06-15 22:28:09 +0000984/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +0000985/// \param InVirtual whether this base class is a virtual base (or a base
986/// of a virtual base)
987/// \param Access the access along the inheritance path to this base
988/// \param ParentHiddenTypes the conversions provided by the inheritors
989/// of this base
990/// \param Output the set to which to add conversions from non-virtual bases
991/// \param VOutput the set to which to add conversions from virtual bases
992/// \param HiddenVBaseCs the set of conversions which were hidden in a
993/// virtual base along some inheritance path
994static void CollectVisibleConversions(ASTContext &Context,
995 CXXRecordDecl *Record,
996 bool InVirtual,
997 AccessSpecifier Access,
998 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
999 UnresolvedSetImpl &Output,
1000 UnresolvedSetImpl &VOutput,
1001 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1002 // The set of types which have conversions in this class or its
1003 // subclasses. As an optimization, we don't copy the derived set
1004 // unless it might change.
1005 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1006 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1007
1008 // Collect the direct conversions and figure out which conversions
1009 // will be hidden in the subclasses.
1010 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1011 if (!Cs.empty()) {
1012 HiddenTypesBuffer = ParentHiddenTypes;
1013 HiddenTypes = &HiddenTypesBuffer;
1014
1015 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +00001016 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1017 bool Hidden = ParentHiddenTypes.count(ConvType);
1018 if (!Hidden)
1019 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001020
1021 // If this conversion is hidden and we're in a virtual base,
1022 // remember that it's hidden along some inheritance path.
1023 if (Hidden && InVirtual)
1024 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1025
1026 // If this conversion isn't hidden, add it to the appropriate output.
1027 else if (!Hidden) {
1028 AccessSpecifier IAccess
1029 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1030
1031 if (InVirtual)
1032 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001033 else
John McCallb05b5f32010-03-15 09:07:48 +00001034 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001035 }
1036 }
1037 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001038
John McCallb05b5f32010-03-15 09:07:48 +00001039 // Collect information recursively from any base classes.
1040 for (CXXRecordDecl::base_class_iterator
1041 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1042 const RecordType *RT = I->getType()->getAs<RecordType>();
1043 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001044
John McCallb05b5f32010-03-15 09:07:48 +00001045 AccessSpecifier BaseAccess
1046 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1047 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001048
John McCallb05b5f32010-03-15 09:07:48 +00001049 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1050 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1051 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001052 }
John McCallb05b5f32010-03-15 09:07:48 +00001053}
Sebastian Redl9994a342009-10-25 17:03:50 +00001054
John McCallb05b5f32010-03-15 09:07:48 +00001055/// Collect the visible conversions of a class.
1056///
1057/// This would be extremely straightforward if it weren't for virtual
1058/// bases. It might be worth special-casing that, really.
1059static void CollectVisibleConversions(ASTContext &Context,
1060 CXXRecordDecl *Record,
1061 UnresolvedSetImpl &Output) {
1062 // The collection of all conversions in virtual bases that we've
1063 // found. These will be added to the output as long as they don't
1064 // appear in the hidden-conversions set.
1065 UnresolvedSet<8> VBaseCs;
1066
1067 // The set of conversions in virtual bases that we've determined to
1068 // be hidden.
1069 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1070
1071 // The set of types hidden by classes derived from this one.
1072 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1073
1074 // Go ahead and collect the direct conversions and add them to the
1075 // hidden-types set.
1076 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1077 Output.append(Cs.begin(), Cs.end());
1078 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1079 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1080
1081 // Recursively collect conversions from base classes.
1082 for (CXXRecordDecl::base_class_iterator
1083 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1084 const RecordType *RT = I->getType()->getAs<RecordType>();
1085 if (!RT) continue;
1086
1087 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1088 I->isVirtual(), I->getAccessSpecifier(),
1089 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1090 }
1091
1092 // Add any unhidden conversions provided by virtual bases.
1093 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1094 I != E; ++I) {
1095 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1096 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001097 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001098}
1099
1100/// getVisibleConversionFunctions - get all conversion functions visible
1101/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +00001102const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001103 // If root class, all conversions are visible.
1104 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +00001105 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +00001106 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +00001107 if (data().ComputedVisibleConversions)
1108 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +00001109 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001110 data().ComputedVisibleConversions = true;
1111 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001112}
1113
John McCall32daa422010-03-31 01:36:47 +00001114void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1115 // This operation is O(N) but extremely rare. Sema only uses it to
1116 // remove UsingShadowDecls in a class that were followed by a direct
1117 // declaration, e.g.:
1118 // class A : B {
1119 // using B::operator int;
1120 // operator int();
1121 // };
1122 // This is uncommon by itself and even more uncommon in conjunction
1123 // with sufficiently large numbers of directly-declared conversions
1124 // that asymptotic behavior matters.
1125
1126 UnresolvedSetImpl &Convs = *getConversionFunctions();
1127 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1128 if (Convs[I].getDecl() == ConvDecl) {
1129 Convs.erase(I);
1130 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1131 && "conversion was found multiple times in unresolved set");
1132 return;
1133 }
1134 }
1135
1136 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001137}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001138
Douglas Gregorf6b11852009-10-08 15:14:33 +00001139CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001140 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001141 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1142
1143 return 0;
1144}
1145
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001146MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1147 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1148}
1149
Douglas Gregorf6b11852009-10-08 15:14:33 +00001150void
1151CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1152 TemplateSpecializationKind TSK) {
1153 assert(TemplateOrInstantiation.isNull() &&
1154 "Previous template or instantiation?");
1155 assert(!isa<ClassTemplateSpecializationDecl>(this));
1156 TemplateOrInstantiation
1157 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1158}
1159
Anders Carlssonb13e3572009-12-07 06:33:48 +00001160TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1161 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001162 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1163 return Spec->getSpecializationKind();
1164
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001165 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001166 return MSInfo->getTemplateSpecializationKind();
1167
1168 return TSK_Undeclared;
1169}
1170
1171void
1172CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1173 if (ClassTemplateSpecializationDecl *Spec
1174 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1175 Spec->setSpecializationKind(TSK);
1176 return;
1177 }
1178
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001179 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001180 MSInfo->setTemplateSpecializationKind(TSK);
1181 return;
1182 }
1183
David Blaikieb219cfc2011-09-23 05:06:16 +00001184 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001185}
1186
Douglas Gregor1d110e02010-07-01 14:13:13 +00001187CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1188 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001189 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001190
1191 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001192 = Context.DeclarationNames.getCXXDestructorName(
1193 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001194
John McCallc0bf4622010-02-23 00:48:20 +00001195 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001196 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001197 if (I == E)
1198 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001200 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001201 return Dtor;
1202}
1203
Douglas Gregorda2142f2011-02-19 18:51:44 +00001204void CXXRecordDecl::completeDefinition() {
1205 completeDefinition(0);
1206}
1207
1208void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1209 RecordDecl::completeDefinition();
1210
David Blaikie4e4d0842012-03-11 07:00:24 +00001211 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001212 // Objective-C Automatic Reference Counting:
1213 // If a class has a non-static data member of Objective-C pointer
1214 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001215 // default constructor (if any), copy constructor, move constructor,
1216 // copy assignment operator, move assignment operator, and destructor are
1217 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001218 struct DefinitionData &Data = data();
1219 Data.PlainOldData = false;
1220 Data.HasTrivialDefaultConstructor = false;
1221 Data.HasTrivialCopyConstructor = false;
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001222 Data.HasTrivialMoveConstructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001223 Data.HasTrivialCopyAssignment = false;
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001224 Data.HasTrivialMoveAssignment = false;
John McCallf85e1932011-06-15 23:02:42 +00001225 Data.HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +00001226 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001227 }
1228
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001229 // If the class may be abstract (but hasn't been marked as such), check for
1230 // any pure final overriders.
1231 if (mayBeAbstract()) {
1232 CXXFinalOverriderMap MyFinalOverriders;
1233 if (!FinalOverriders) {
1234 getFinalOverriders(MyFinalOverriders);
1235 FinalOverriders = &MyFinalOverriders;
1236 }
1237
1238 bool Done = false;
1239 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1240 MEnd = FinalOverriders->end();
1241 M != MEnd && !Done; ++M) {
1242 for (OverridingMethods::iterator SO = M->second.begin(),
1243 SOEnd = M->second.end();
1244 SO != SOEnd && !Done; ++SO) {
1245 assert(SO->second.size() > 0 &&
1246 "All virtual functions have overridding virtual functions");
1247
1248 // C++ [class.abstract]p4:
1249 // A class is abstract if it contains or inherits at least one
1250 // pure virtual function for which the final overrider is pure
1251 // virtual.
1252 if (SO->second.front().Method->isPure()) {
1253 data().Abstract = true;
1254 Done = true;
1255 break;
1256 }
1257 }
1258 }
1259 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001260
1261 // Set access bits correctly on the directly-declared conversions.
1262 for (UnresolvedSetIterator I = data().Conversions.begin(),
1263 E = data().Conversions.end();
1264 I != E; ++I)
1265 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001266}
1267
1268bool CXXRecordDecl::mayBeAbstract() const {
1269 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1270 isDependentContext())
1271 return false;
1272
1273 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1274 BEnd = bases_end();
1275 B != BEnd; ++B) {
1276 CXXRecordDecl *BaseDecl
1277 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1278 if (BaseDecl->isAbstract())
1279 return true;
1280 }
1281
1282 return false;
1283}
1284
David Blaikie99ba9e32011-12-20 02:48:34 +00001285void CXXMethodDecl::anchor() { }
1286
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001287static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1288 const CXXMethodDecl *BaseMD) {
1289 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1290 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1291 const CXXMethodDecl *MD = *I;
1292 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1293 return true;
1294 if (recursivelyOverrides(MD, BaseMD))
1295 return true;
1296 }
1297 return false;
1298}
1299
1300CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001301CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1302 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001303 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1304 return this;
1305
1306 // Lookup doesn't work for destructors, so handle them separately.
1307 if (isa<CXXDestructorDecl>(this)) {
1308 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001309 if (MD) {
1310 if (recursivelyOverrides(MD, this))
1311 return MD;
1312 if (MayBeBase && recursivelyOverrides(this, MD))
1313 return MD;
1314 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001315 return NULL;
1316 }
1317
1318 lookup_const_result Candidates = RD->lookup(getDeclName());
1319 for (NamedDecl * const * I = Candidates.first; I != Candidates.second; ++I) {
1320 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1321 if (!MD)
1322 continue;
1323 if (recursivelyOverrides(MD, this))
1324 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001325 if (MayBeBase && recursivelyOverrides(this, MD))
1326 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001327 }
1328
1329 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1330 E = RD->bases_end(); I != E; ++I) {
1331 const RecordType *RT = I->getType()->getAs<RecordType>();
1332 if (!RT)
1333 continue;
1334 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1335 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1336 if (T)
1337 return T;
1338 }
1339
1340 return NULL;
1341}
1342
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001343CXXMethodDecl *
1344CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001345 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001346 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001347 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001348 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001349 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001350 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001351 isStatic, SCAsWritten, isInline, isConstexpr,
1352 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001353}
1354
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001355CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1356 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1357 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1358 DeclarationNameInfo(), QualType(),
1359 0, false, SC_None, false, false,
1360 SourceLocation());
1361}
1362
Douglas Gregor90916562009-09-29 18:16:17 +00001363bool CXXMethodDecl::isUsualDeallocationFunction() const {
1364 if (getOverloadedOperator() != OO_Delete &&
1365 getOverloadedOperator() != OO_Array_Delete)
1366 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001367
1368 // C++ [basic.stc.dynamic.deallocation]p2:
1369 // A template instance is never a usual deallocation function,
1370 // regardless of its signature.
1371 if (getPrimaryTemplate())
1372 return false;
1373
Douglas Gregor90916562009-09-29 18:16:17 +00001374 // C++ [basic.stc.dynamic.deallocation]p2:
1375 // If a class T has a member deallocation function named operator delete
1376 // with exactly one parameter, then that function is a usual (non-placement)
1377 // deallocation function. [...]
1378 if (getNumParams() == 1)
1379 return true;
1380
1381 // C++ [basic.stc.dynamic.deallocation]p2:
1382 // [...] If class T does not declare such an operator delete but does
1383 // declare a member deallocation function named operator delete with
1384 // exactly two parameters, the second of which has type std::size_t (18.1),
1385 // then this function is a usual deallocation function.
1386 ASTContext &Context = getASTContext();
1387 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001388 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1389 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001390 return false;
1391
1392 // This function is a usual deallocation function if there are no
1393 // single-parameter deallocation functions of the same kind.
1394 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1395 R.first != R.second; ++R.first) {
1396 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1397 if (FD->getNumParams() == 1)
1398 return false;
1399 }
1400
1401 return true;
1402}
1403
Douglas Gregor06a9f362010-05-01 20:49:11 +00001404bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001405 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001406 // A user-declared copy assignment operator X::operator= is a non-static
1407 // non-template member function of class X with exactly one parameter of
1408 // type X, X&, const X&, volatile X& or const volatile X&.
1409 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1410 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001411 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001412 return false;
1413
1414 QualType ParamType = getParamDecl(0)->getType();
1415 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1416 ParamType = Ref->getPointeeType();
1417
1418 ASTContext &Context = getASTContext();
1419 QualType ClassType
1420 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1421 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1422}
1423
Sean Huntffe37fd2011-05-25 20:50:04 +00001424bool CXXMethodDecl::isMoveAssignmentOperator() const {
1425 // C++0x [class.copy]p19:
1426 // A user-declared move assignment operator X::operator= is a non-static
1427 // non-template member function of class X with exactly one parameter of type
1428 // X&&, const X&&, volatile X&&, or const volatile X&&.
1429 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1430 getPrimaryTemplate() || getDescribedFunctionTemplate())
1431 return false;
1432
1433 QualType ParamType = getParamDecl(0)->getType();
1434 if (!isa<RValueReferenceType>(ParamType))
1435 return false;
1436 ParamType = ParamType->getPointeeType();
1437
1438 ASTContext &Context = getASTContext();
1439 QualType ClassType
1440 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1441 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1442}
1443
Anders Carlsson05eb2442009-05-16 23:58:37 +00001444void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001445 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001446 assert(!MD->getParent()->isDependentContext() &&
1447 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001448 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001449
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001450 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001451}
1452
1453CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001454 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001455 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001456}
1457
1458CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001459 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001460 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001461}
1462
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001463unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001464 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001465 return getASTContext().overridden_methods_size(this);
1466}
1467
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001468QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001469 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1470 // If the member function is declared const, the type of this is const X*,
1471 // if the member function is declared volatile, the type of this is
1472 // volatile X*, and if the member function is declared const volatile,
1473 // the type of this is const volatile X*.
1474
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001475 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001476
John McCall3cb0ebd2010-03-10 03:28:59 +00001477 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001478 ClassTy = C.getQualifiedType(ClassTy,
1479 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001480 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001481}
1482
Eli Friedmand7d7f672009-12-06 20:50:05 +00001483bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001484 // If this function is a template instantiation, look at the template from
1485 // which it was instantiated.
1486 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1487 if (!CheckFn)
1488 CheckFn = this;
1489
Eli Friedmand7d7f672009-12-06 20:50:05 +00001490 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001491 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001492}
1493
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001494bool CXXMethodDecl::isLambdaStaticInvoker() const {
1495 return getParent()->isLambda() &&
1496 getIdentifier() && getIdentifier()->getName() == "__invoke";
1497}
1498
1499
Sean Huntcbb67482011-01-08 20:30:50 +00001500CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1501 TypeSourceInfo *TInfo, bool IsVirtual,
1502 SourceLocation L, Expr *Init,
1503 SourceLocation R,
1504 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001505 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001506 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1507 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001508{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001509}
1510
Sean Huntcbb67482011-01-08 20:30:50 +00001511CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1512 FieldDecl *Member,
1513 SourceLocation MemberLoc,
1514 SourceLocation L, Expr *Init,
1515 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001516 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001517 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001518 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1519{
1520}
1521
Sean Huntcbb67482011-01-08 20:30:50 +00001522CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1523 IndirectFieldDecl *Member,
1524 SourceLocation MemberLoc,
1525 SourceLocation L, Expr *Init,
1526 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001527 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001528 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001529 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001530{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001531}
1532
Sean Huntcbb67482011-01-08 20:30:50 +00001533CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001534 TypeSourceInfo *TInfo,
1535 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001536 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001537 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1538 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001539 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1540{
1541}
1542
1543CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001544 FieldDecl *Member,
1545 SourceLocation MemberLoc,
1546 SourceLocation L, Expr *Init,
1547 SourceLocation R,
1548 VarDecl **Indices,
1549 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001550 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001551 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001552 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001553{
1554 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1555 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1556}
1557
Sean Huntcbb67482011-01-08 20:30:50 +00001558CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1559 FieldDecl *Member,
1560 SourceLocation MemberLoc,
1561 SourceLocation L, Expr *Init,
1562 SourceLocation R,
1563 VarDecl **Indices,
1564 unsigned NumIndices) {
1565 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001566 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001567 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001568 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1569 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001570}
1571
Sean Huntcbb67482011-01-08 20:30:50 +00001572TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001573 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001574 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001575 else
1576 return TypeLoc();
1577}
1578
Sean Huntcbb67482011-01-08 20:30:50 +00001579const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001580 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001581 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001582 else
1583 return 0;
1584}
1585
Sean Huntcbb67482011-01-08 20:30:50 +00001586SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001587 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001588 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001589
1590 if (isInClassMemberInitializer())
1591 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001592
Douglas Gregor76852c22011-11-01 01:16:03 +00001593 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1594 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1595
1596 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001597}
1598
Sean Huntcbb67482011-01-08 20:30:50 +00001599SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001600 if (isInClassMemberInitializer()) {
1601 FieldDecl *D = getAnyMember();
1602 if (Expr *I = D->getInClassInitializer())
1603 return I->getSourceRange();
1604 return SourceRange();
1605 }
1606
Douglas Gregor802ab452009-12-02 22:36:29 +00001607 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001608}
1609
David Blaikie99ba9e32011-12-20 02:48:34 +00001610void CXXConstructorDecl::anchor() { }
1611
Douglas Gregorb48fe382008-10-31 09:07:45 +00001612CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001613CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1614 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1615 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1616 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001617}
1618
1619CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001620CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001621 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001622 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001623 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001624 bool isExplicit, bool isInline,
1625 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001626 assert(NameInfo.getName().getNameKind()
1627 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001628 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001629 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001630 isExplicit, isInline, isImplicitlyDeclared,
1631 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001632}
1633
Douglas Gregor76852c22011-11-01 01:16:03 +00001634CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1635 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1636 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1637 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1638 return Construct->getConstructor();
1639
1640 return 0;
1641}
1642
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001643bool CXXConstructorDecl::isDefaultConstructor() const {
1644 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001645 // A default constructor for a class X is a constructor of class
1646 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001647 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001648 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001649}
1650
Mike Stump1eb44332009-09-09 15:08:12 +00001651bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001652CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001653 return isCopyOrMoveConstructor(TypeQuals) &&
1654 getParamDecl(0)->getType()->isLValueReferenceType();
1655}
1656
1657bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1658 return isCopyOrMoveConstructor(TypeQuals) &&
1659 getParamDecl(0)->getType()->isRValueReferenceType();
1660}
1661
1662/// \brief Determine whether this is a copy or move constructor.
1663bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001664 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001665 // A non-template constructor for class X is a copy constructor
1666 // if its first parameter is of type X&, const X&, volatile X& or
1667 // const volatile X&, and either there are no other parameters
1668 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001669 // C++0x [class.copy]p3:
1670 // A non-template constructor for class X is a move constructor if its
1671 // first parameter is of type X&&, const X&&, volatile X&&, or
1672 // const volatile X&&, and either there are no other parameters or else
1673 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001674 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001675 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001676 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001677 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001678 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001679
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001680 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001681
1682 // Do we have a reference type?
1683 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001684 if (!ParamRefType)
1685 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001686
Douglas Gregorfd476482009-11-13 23:59:09 +00001687 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001688 ASTContext &Context = getASTContext();
1689
Douglas Gregorfd476482009-11-13 23:59:09 +00001690 CanQualType PointeeType
1691 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001692 CanQualType ClassTy
1693 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001694 if (PointeeType.getUnqualifiedType() != ClassTy)
1695 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001696
John McCall0953e762009-09-24 19:53:00 +00001697 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001698
1699 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001700 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001701 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001702}
1703
Anders Carlssonfaccd722009-08-28 16:57:08 +00001704bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001705 // C++ [class.conv.ctor]p1:
1706 // A constructor declared without the function-specifier explicit
1707 // that can be called with a single parameter specifies a
1708 // conversion from the type of its first parameter to the type of
1709 // its class. Such a constructor is called a converting
1710 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001711 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001712 return false;
1713
Mike Stump1eb44332009-09-09 15:08:12 +00001714 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001715 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001716 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001717 (getNumParams() > 1 &&
1718 (getParamDecl(1)->hasDefaultArg() ||
1719 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001720}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001721
Douglas Gregor6493cc52010-11-08 17:16:59 +00001722bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001723 if ((getNumParams() < 1) ||
1724 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1725 (getPrimaryTemplate() == 0) ||
1726 (getDescribedFunctionTemplate() != 0))
1727 return false;
1728
1729 const ParmVarDecl *Param = getParamDecl(0);
1730
1731 ASTContext &Context = getASTContext();
1732 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1733
Douglas Gregor66724ea2009-11-14 01:20:54 +00001734 // Is it the same as our our class type?
1735 CanQualType ClassTy
1736 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1737 if (ParamType.getUnqualifiedType() != ClassTy)
1738 return false;
1739
1740 return true;
1741}
1742
Sebastian Redlf677ea32011-02-05 19:23:19 +00001743const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1744 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001745 method_iterator It = getASTContext().overridden_methods_begin(this);
1746 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001747 return 0;
1748
1749 return cast<CXXConstructorDecl>(*It);
1750}
1751
1752void
1753CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1754 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001755 assert(getASTContext().overridden_methods_size(this) == 0 &&
1756 "Base ctor already set.");
1757 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001758}
1759
David Blaikie99ba9e32011-12-20 02:48:34 +00001760void CXXDestructorDecl::anchor() { }
1761
Douglas Gregor42a552f2008-11-05 20:51:48 +00001762CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001763CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1764 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1765 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001766 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001767}
1768
1769CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001770CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001771 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001772 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001773 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001774 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001775 assert(NameInfo.getName().getNameKind()
1776 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001777 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001778 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001779 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001780}
1781
David Blaikie99ba9e32011-12-20 02:48:34 +00001782void CXXConversionDecl::anchor() { }
1783
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001784CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001785CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1786 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1787 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1788 QualType(), 0, false, false, false,
1789 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001790}
1791
1792CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001793CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001794 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001795 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001796 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001797 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001798 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001799 assert(NameInfo.getName().getNameKind()
1800 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001801 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001802 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001803 isInline, isExplicit, isConstexpr,
1804 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001805}
1806
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001807bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1808 return isImplicit() && getParent()->isLambda() &&
1809 getConversionType()->isBlockPointerType();
1810}
1811
David Blaikie99ba9e32011-12-20 02:48:34 +00001812void LinkageSpecDecl::anchor() { }
1813
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001814LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001815 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001816 SourceLocation ExternLoc,
1817 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001818 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001819 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001820 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001821}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001822
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001823LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1824 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1825 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1826 lang_c, SourceLocation());
1827}
1828
David Blaikie99ba9e32011-12-20 02:48:34 +00001829void UsingDirectiveDecl::anchor() { }
1830
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001831UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1832 SourceLocation L,
1833 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001834 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001835 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001836 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001837 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001838 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1839 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001840 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1841 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001842}
1843
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001844UsingDirectiveDecl *
1845UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1846 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1847 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1848 NestedNameSpecifierLoc(),
1849 SourceLocation(), 0, 0);
1850}
1851
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001852NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1853 if (NamespaceAliasDecl *NA =
1854 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1855 return NA->getNamespace();
1856 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1857}
1858
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001859void NamespaceDecl::anchor() { }
1860
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001861NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1862 SourceLocation StartLoc,
1863 SourceLocation IdLoc, IdentifierInfo *Id,
1864 NamespaceDecl *PrevDecl)
1865 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1866 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1867{
1868 setPreviousDeclaration(PrevDecl);
1869
1870 if (PrevDecl)
1871 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1872}
1873
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001874NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001875 bool Inline, SourceLocation StartLoc,
1876 SourceLocation IdLoc, IdentifierInfo *Id,
1877 NamespaceDecl *PrevDecl) {
1878 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001879}
1880
1881NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1882 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001883 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1884 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001885}
1886
David Blaikie99ba9e32011-12-20 02:48:34 +00001887void NamespaceAliasDecl::anchor() { }
1888
Mike Stump1eb44332009-09-09 15:08:12 +00001889NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001890 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001891 SourceLocation AliasLoc,
1892 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001893 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001894 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001895 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001896 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1897 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001898 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1899 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001900}
1901
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001902NamespaceAliasDecl *
1903NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1904 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1905 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1906 NestedNameSpecifierLoc(),
1907 SourceLocation(), 0);
1908}
1909
David Blaikie99ba9e32011-12-20 02:48:34 +00001910void UsingShadowDecl::anchor() { }
1911
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001912UsingShadowDecl *
1913UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1914 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1915 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1916}
1917
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001918UsingDecl *UsingShadowDecl::getUsingDecl() const {
1919 const UsingShadowDecl *Shadow = this;
1920 while (const UsingShadowDecl *NextShadow =
1921 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1922 Shadow = NextShadow;
1923 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1924}
1925
David Blaikie99ba9e32011-12-20 02:48:34 +00001926void UsingDecl::anchor() { }
1927
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001928void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1929 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1930 "declaration already in set");
1931 assert(S->getUsingDecl() == this);
1932
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001933 if (FirstUsingShadow.getPointer())
1934 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1935 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001936}
1937
1938void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1939 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1940 "declaration not in set");
1941 assert(S->getUsingDecl() == this);
1942
1943 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1944
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001945 if (FirstUsingShadow.getPointer() == S) {
1946 FirstUsingShadow.setPointer(
1947 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001948 S->UsingOrNextShadow = this;
1949 return;
1950 }
1951
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001952 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001953 while (Prev->UsingOrNextShadow != S)
1954 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1955 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1956 S->UsingOrNextShadow = this;
1957}
1958
Douglas Gregordc355712011-02-25 00:36:19 +00001959UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1960 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001961 const DeclarationNameInfo &NameInfo,
1962 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001963 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001964}
1965
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001966UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1967 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1968 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1969 DeclarationNameInfo(), false);
1970}
1971
David Blaikie99ba9e32011-12-20 02:48:34 +00001972void UnresolvedUsingValueDecl::anchor() { }
1973
John McCall7ba107a2009-11-18 02:36:19 +00001974UnresolvedUsingValueDecl *
1975UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1976 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001977 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001978 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001979 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001980 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001981}
1982
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001983UnresolvedUsingValueDecl *
1984UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1985 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1986 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1987 NestedNameSpecifierLoc(),
1988 DeclarationNameInfo());
1989}
1990
David Blaikie99ba9e32011-12-20 02:48:34 +00001991void UnresolvedUsingTypenameDecl::anchor() { }
1992
John McCall7ba107a2009-11-18 02:36:19 +00001993UnresolvedUsingTypenameDecl *
1994UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1995 SourceLocation UsingLoc,
1996 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001997 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001998 SourceLocation TargetNameLoc,
1999 DeclarationName TargetName) {
2000 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002001 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002002 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00002003}
2004
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002005UnresolvedUsingTypenameDecl *
2006UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2007 void *Mem = AllocateDeserializedDecl(C, ID,
2008 sizeof(UnresolvedUsingTypenameDecl));
2009 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2010 SourceLocation(),
2011 NestedNameSpecifierLoc(),
2012 SourceLocation(),
2013 0);
2014}
2015
David Blaikie99ba9e32011-12-20 02:48:34 +00002016void StaticAssertDecl::anchor() { }
2017
Anders Carlssonfb311762009-03-14 00:25:26 +00002018StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002019 SourceLocation StaticAssertLoc,
2020 Expr *AssertExpr,
2021 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002022 SourceLocation RParenLoc,
2023 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002024 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002025 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00002026}
2027
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002028StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2029 unsigned ID) {
2030 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00002031 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
2032 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002033}
2034
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002035static const char *getAccessName(AccessSpecifier AS) {
2036 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002037 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002038 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002039 case AS_public:
2040 return "public";
2041 case AS_private:
2042 return "private";
2043 case AS_protected:
2044 return "protected";
2045 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002046 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002047}
2048
2049const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2050 AccessSpecifier AS) {
2051 return DB << getAccessName(AS);
2052}
Richard Smithf15fda02012-02-02 01:16:57 +00002053
2054const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2055 AccessSpecifier AS) {
2056 return DB << getAccessName(AS);
2057}