blob: 712480655e2b30ee1d8381d1198b332a56715d47 [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"
15#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000016#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000017#include "clang/AST/CXXInheritance.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/AST/DeclTemplate.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)
Richard Smith7d04d3a2012-11-30 05:11:39 +000039 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
Eli Friedman97c134e2009-08-15 22:23:00 +000040 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000041 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000042 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000043 HasMutableFields(false), HasOnlyCMembers(true),
Richard Smithd5bc8672012-12-08 02:01:17 +000044 HasInClassInitializer(false), HasUninitializedReferenceMember(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000045 HasTrivialSpecialMembers(SMF_All),
Richard Smithac713512012-12-08 02:53:02 +000046 DeclaredNonTrivialSpecialMembers(0),
Richard Smith7d04d3a2012-11-30 05:11:39 +000047 HasIrrelevantDestructor(true),
Richard Smith61802452011-12-22 02:22:31 +000048 HasConstexprNonCopyMoveConstructor(false),
49 DefaultedDefaultConstructorIsConstexpr(true),
Richard Smith7d04d3a2012-11-30 05:11:39 +000050 HasConstexprDefaultConstructor(false),
Sean Hunt023df372011-05-09 18:22:59 +000051 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000052 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smithacf796b2012-11-28 06:23:12 +000053 ImplicitCopyConstructorHasConstParam(true),
54 ImplicitCopyAssignmentHasConstParam(true),
55 HasDeclaredCopyConstructorWithConstParam(false),
56 HasDeclaredCopyAssignmentWithConstParam(false),
57 FailedImplicitMoveConstructor(false), FailedImplicitMoveAssignment(false),
58 IsLambda(false), NumBases(0), NumVBases(0), Bases(), VBases(),
59 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000060}
61
Benjamin Krameree3096a2012-07-04 17:03:33 +000062CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
63 return Bases.get(Definition->getASTContext().getExternalSource());
64}
65
66CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
67 return VBases.get(Definition->getASTContext().getExternalSource());
68}
69
John McCall86ff3082010-02-04 22:26:26 +000070CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000071 SourceLocation StartLoc, SourceLocation IdLoc,
72 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
73 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000074 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000075 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000076
Jay Foad4ba2a172011-01-12 09:06:06 +000077CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000078 DeclContext *DC, SourceLocation StartLoc,
79 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000080 CXXRecordDecl* PrevDecl,
81 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000082 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
83 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000084
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000085 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000086 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000087 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000088 return R;
89}
90
Douglas Gregorda8962a2012-02-13 15:44:47 +000091CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Eli Friedman8da8a662012-09-19 01:18:11 +000092 TypeSourceInfo *Info, SourceLocation Loc,
93 bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +000094 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
95 0, 0);
96 R->IsBeingDefined = true;
Eli Friedman8da8a662012-09-19 01:18:11 +000097 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info, Dependent);
Douglas Gregorda8962a2012-02-13 15:44:47 +000098 C.getTypeDeclType(R, /*PrevDecl=*/0);
99 return R;
100}
101
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000102CXXRecordDecl *
103CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
104 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
105 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
106 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000107}
108
Mike Stump1eb44332009-09-09 15:08:12 +0000109void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000110CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000111 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000112 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000113
Douglas Gregor7c789c12010-10-29 22:39:52 +0000114 if (!data().Bases.isOffset() && data().NumBases > 0)
115 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Richard Smithdd677232011-10-18 20:08:55 +0000117 if (NumBases) {
118 // C++ [dcl.init.aggr]p1:
119 // An aggregate is [...] a class with [...] no base classes [...].
120 data().Aggregate = false;
121
122 // C++ [class]p4:
123 // A POD-struct is an aggregate class...
124 data().PlainOldData = false;
125 }
126
Anders Carlsson6f6de732010-03-29 05:13:12 +0000127 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000128 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000129
130 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000131 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
John McCall86ff3082010-02-04 22:26:26 +0000133 data().Bases = new(C) CXXBaseSpecifier [NumBases];
134 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000135 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000136 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000137 // Keep track of inherited vbases for this base class.
138 const CXXBaseSpecifier *Base = Bases[i];
139 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000140 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000141 if (BaseType->isDependentType())
142 continue;
143 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000144 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000145
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000146 // A class with a non-empty base class is not empty.
147 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000148 if (!BaseClassDecl->isEmpty()) {
149 if (!data().Empty) {
150 // C++0x [class]p7:
151 // A standard-layout class is a class that:
152 // [...]
153 // -- either has no non-static data members in the most derived
154 // class and at most one base class with non-static data members,
155 // or has no base classes with non-static data members, and
156 // If this is the second non-empty base, then neither of these two
157 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000158 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000159 }
160
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000161 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000162 data().HasNoNonEmptyBases = false;
163 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000164
Douglas Gregor85606eb2010-09-28 20:50:54 +0000165 // C++ [class.virtual]p1:
166 // A class that declares or inherits a virtual function is called a
167 // polymorphic class.
168 if (BaseClassDecl->isPolymorphic())
169 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000170
Chandler Carrutha8225442011-04-30 09:17:45 +0000171 // C++0x [class]p7:
172 // A standard-layout class is a class that: [...]
173 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000174 if (!BaseClassDecl->isStandardLayout())
175 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000176
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000177 // Record if this base is the first non-literal field or base.
178 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
179 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000180
Anders Carlsson6f6de732010-03-29 05:13:12 +0000181 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000182 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000183 BaseClassDecl->vbases_begin(),
184 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000185 // Add this base if it's not already in the list.
Richard Smithacf796b2012-11-28 06:23:12 +0000186 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000187 VBases.push_back(VBase);
Richard Smithacf796b2012-11-28 06:23:12 +0000188
189 // C++11 [class.copy]p8:
190 // The implicitly-declared copy constructor for a class X will have
191 // the form 'X::X(const X&)' if each [...] virtual base class B of X
192 // has a copy constructor whose first parameter is of type
193 // 'const B&' or 'const volatile B&' [...]
194 if (CXXRecordDecl *VBaseDecl = VBase->getType()->getAsCXXRecordDecl())
195 if (!VBaseDecl->hasCopyConstructorWithConstParam())
196 data().ImplicitCopyConstructorHasConstParam = false;
197 }
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000198 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000199
200 if (Base->isVirtual()) {
201 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000202 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Richard Smithacf796b2012-11-28 06:23:12 +0000203 VBases.push_back(Base);
204
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000205 // C++0x [meta.unary.prop] is_empty:
206 // T is a class type, but not a union type, with ... no virtual base
207 // classes
208 data().Empty = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000209
Richard Smith7d04d3a2012-11-30 05:11:39 +0000210 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
211 // A [default constructor, copy/move constructor, or copy/move assignment
212 // operator for a class X] is trivial [...] if:
213 // -- class X has [...] no virtual base classes
214 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carrutha8225442011-04-30 09:17:45 +0000215
216 // C++0x [class]p7:
217 // A standard-layout class is a class that: [...]
218 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000219 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000220
221 // C++11 [dcl.constexpr]p4:
222 // In the definition of a constexpr constructor [...]
223 // -- the class shall not have any virtual base classes
224 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000225 } else {
226 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000227 // A default constructor is trivial [...] if:
228 // -- all the direct base classes of its class have trivial default
229 // constructors.
230 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000231 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
232
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000233 // C++0x [class.copy]p13:
234 // A copy/move constructor for class X is trivial if [...]
235 // [...]
236 // -- the constructor selected to copy/move each direct base class
237 // subobject is trivial, and
238 // FIXME: C++0x: We need to only consider the selected constructor
Richard Smith93af2b82012-11-14 07:36:28 +0000239 // instead of all of them. For now, we treat a move constructor as being
240 // non-trivial if it calls anything other than a trivial move constructor.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000241 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000242 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith426391c2012-11-16 00:53:38 +0000243 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000244 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000245
246 // C++0x [class.copy]p27:
247 // A copy/move assignment operator for class X is trivial if [...]
248 // [...]
249 // -- the assignment operator selected to copy/move each direct base
250 // class subobject is trivial, and
251 // FIXME: C++0x: We need to only consider the selected operator instead
252 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000253 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000254 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith426391c2012-11-16 00:53:38 +0000255 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000256 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smith61802452011-12-22 02:22:31 +0000257
258 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000259 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000260 // requirements of a constexpr constructor, the implicitly-defined
261 // default constructor is constexpr.
262 if (!BaseClassDecl->hasConstexprDefaultConstructor())
263 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000264 }
Richard Smithac713512012-12-08 02:53:02 +0000265
Douglas Gregor85606eb2010-09-28 20:50:54 +0000266 // C++ [class.ctor]p3:
267 // A destructor is trivial if all the direct base classes of its class
268 // have trivial destructors.
269 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000270 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000271
272 if (!BaseClassDecl->hasIrrelevantDestructor())
273 data().HasIrrelevantDestructor = false;
274
Richard Smithacf796b2012-11-28 06:23:12 +0000275 // C++11 [class.copy]p18:
276 // The implicitly-declared copy assignment oeprator for a class X will
277 // have the form 'X& X::operator=(const X&)' if each direct base class B
278 // of X has a copy assignment operator whose parameter is of type 'const
279 // B&', 'const volatile B&', or 'B' [...]
280 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
281 data().ImplicitCopyAssignmentHasConstParam = false;
282
283 // C++11 [class.copy]p8:
284 // The implicitly-declared copy constructor for a class X will have
285 // the form 'X::X(const X&)' if each direct [...] base class B of X
286 // has a copy constructor whose first parameter is of type
287 // 'const B&' or 'const volatile B&' [...]
288 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
289 data().ImplicitCopyConstructorHasConstParam = false;
290
John McCallf85e1932011-06-15 23:02:42 +0000291 // A class has an Objective-C object member if... or any of its bases
292 // has an Objective-C object member.
293 if (BaseClassDecl->hasObjectMember())
294 setHasObjectMember(true);
295
Douglas Gregor2bb11012011-05-13 01:05:07 +0000296 // Keep track of the presence of mutable fields.
297 if (BaseClassDecl->hasMutableFields())
298 data().HasMutableFields = true;
Richard Smithd5bc8672012-12-08 02:01:17 +0000299
300 if (BaseClassDecl->hasUninitializedReferenceMember())
301 data().HasUninitializedReferenceMember = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000302 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000303
304 if (VBases.empty())
305 return;
306
307 // Create base specifier for any direct or indirect virtual bases.
308 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
309 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000310 for (int I = 0, E = VBases.size(); I != E; ++I)
311 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000312}
313
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000314/// Callback function for CXXRecordDecl::forallBases that acknowledges
315/// that it saw a base class.
316static bool SawBase(const CXXRecordDecl *, void *) {
317 return true;
318}
319
320bool CXXRecordDecl::hasAnyDependentBases() const {
321 if (!isDependentContext())
322 return false;
323
324 return !forallBases(SawBase, 0);
325}
326
Chandler Carruthb7e95892011-04-23 10:47:28 +0000327bool CXXRecordDecl::isTriviallyCopyable() const {
328 // C++0x [class]p5:
329 // A trivially copyable class is a class that:
330 // -- has no non-trivial copy constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000331 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000332 // -- has no non-trivial move constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000333 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000334 // -- has no non-trivial copy assignment operators,
Richard Smith426391c2012-11-16 00:53:38 +0000335 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000336 // -- has no non-trivial move assignment operators, and
Richard Smith426391c2012-11-16 00:53:38 +0000337 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000338 // -- has a trivial destructor.
339 if (!hasTrivialDestructor()) return false;
340
341 return true;
342}
343
Douglas Gregor21386642010-09-28 21:55:22 +0000344void CXXRecordDecl::markedVirtualFunctionPure() {
345 // C++ [class.abstract]p2:
346 // A class is abstract if it has at least one pure virtual function.
347 data().Abstract = true;
348}
349
350void CXXRecordDecl::addedMember(Decl *D) {
Joao Matos17d35c32012-08-31 22:18:20 +0000351 if (!D->isImplicit() &&
352 !isa<FieldDecl>(D) &&
353 !isa<IndirectFieldDecl>(D) &&
354 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
355 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
356 data().HasOnlyCMembers = false;
357
358 // Ignore friends and invalid declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000359 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000360 return;
361
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000362 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
363 if (FunTmpl)
364 D = FunTmpl->getTemplatedDecl();
365
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000366 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
367 if (Method->isVirtual()) {
368 // C++ [dcl.init.aggr]p1:
369 // An aggregate is an array or a class with [...] no virtual functions.
370 data().Aggregate = false;
371
372 // C++ [class]p4:
373 // A POD-struct is an aggregate class...
374 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000375
376 // Virtual functions make the class non-empty.
377 // FIXME: Standard ref?
378 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000379
380 // C++ [class.virtual]p1:
381 // A class that declares or inherits a virtual function is called a
382 // polymorphic class.
383 data().Polymorphic = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000384
Richard Smith7d04d3a2012-11-30 05:11:39 +0000385 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
386 // A [default constructor, copy/move constructor, or copy/move
387 // assignment operator for a class X] is trivial [...] if:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000388 // -- class X has no virtual functions [...]
Richard Smith7d04d3a2012-11-30 05:11:39 +0000389 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000390
Chandler Carrutha8225442011-04-30 09:17:45 +0000391 // C++0x [class]p7:
392 // A standard-layout class is a class that: [...]
393 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000394 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000395 }
396 }
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000397
Richard Smithacf796b2012-11-28 06:23:12 +0000398 // Notify the listener if an implicit member was added after the definition
399 // was completed.
400 if (!isBeingDefined() && D->isImplicit())
401 if (ASTMutationListener *L = getASTMutationListener())
402 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000403
Richard Smith7d04d3a2012-11-30 05:11:39 +0000404 // The kind of special member this declaration is, if any.
405 unsigned SMKind = 0;
406
Richard Smithacf796b2012-11-28 06:23:12 +0000407 // Handle constructors.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000408 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smithacf796b2012-11-28 06:23:12 +0000409 if (!Constructor->isImplicit()) {
410 // Note that we have a user-declared constructor.
411 data().UserDeclaredConstructor = true;
412
413 // C++ [class]p4:
414 // A POD-struct is an aggregate class [...]
415 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
416 // type is technically an aggregate in C++0x since it wouldn't be in 03.
417 data().PlainOldData = false;
418 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000419
Richard Smith017ab772011-09-05 02:13:09 +0000420 // Technically, "user-provided" is only defined for special member
421 // functions, but the intent of the standard is clearly that it should apply
422 // to all functions.
423 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000424
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000425 if (Constructor->isDefaultConstructor()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000426 SMKind |= SMF_DefaultConstructor;
427
428 if (UserProvided)
Sean Huntcdee3fe2011-05-11 22:34:38 +0000429 data().UserProvidedDefaultConstructor = true;
Richard Smithacf796b2012-11-28 06:23:12 +0000430 if (Constructor->isConstexpr())
Richard Smith61802452011-12-22 02:22:31 +0000431 data().HasConstexprDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000432 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000433
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000434 if (!FunTmpl) {
Richard Smithacf796b2012-11-28 06:23:12 +0000435 unsigned Quals;
436 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000437 SMKind |= SMF_CopyConstructor;
Richard Smithacf796b2012-11-28 06:23:12 +0000438
439 if (Quals & Qualifiers::Const)
440 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith7d04d3a2012-11-30 05:11:39 +0000441 } else if (Constructor->isMoveConstructor())
442 SMKind |= SMF_MoveConstructor;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000443 }
Richard Smithacf796b2012-11-28 06:23:12 +0000444
445 // Record if we see any constexpr constructors which are neither copy
446 // nor move constructors.
447 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith6b8bc072011-08-10 18:11:37 +0000448 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000449
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000450 // C++ [dcl.init.aggr]p1:
451 // An aggregate is an array or a class with no user-declared
452 // constructors [...].
453 // C++0x [dcl.init.aggr]p1:
454 // An aggregate is an array or a class with no user-provided
455 // constructors [...].
Richard Smithacf796b2012-11-28 06:23:12 +0000456 if (getASTContext().getLangOpts().CPlusPlus0x
457 ? UserProvided : !Constructor->isImplicit())
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000458 data().Aggregate = false;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000459 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000460
Richard Smithacf796b2012-11-28 06:23:12 +0000461 // Handle destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000462 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000463 SMKind |= SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000464
Richard Smith7d04d3a2012-11-30 05:11:39 +0000465 if (!DD->isImplicit())
Richard Smithacf796b2012-11-28 06:23:12 +0000466 data().HasIrrelevantDestructor = false;
467
Richard Smithacf796b2012-11-28 06:23:12 +0000468 // C++11 [class.dtor]p5:
Richard Smith7d04d3a2012-11-30 05:11:39 +0000469 // A destructor is trivial if [...] the destructor is not virtual.
470 if (DD->isVirtual())
471 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000472 }
Richard Smithacf796b2012-11-28 06:23:12 +0000473
474 // Handle member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000475 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000476 if (Method->isCopyAssignmentOperator()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000477 SMKind |= SMF_CopyAssignment;
Richard Smithacf796b2012-11-28 06:23:12 +0000478
479 const ReferenceType *ParamTy =
480 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
481 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
482 data().HasDeclaredCopyAssignmentWithConstParam = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000483 }
Sean Huntffe37fd2011-05-25 20:50:04 +0000484
Richard Smith7d04d3a2012-11-30 05:11:39 +0000485 if (Method->isMoveAssignmentOperator())
486 SMKind |= SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000487
Douglas Gregore80622f2010-09-29 04:25:11 +0000488 // Keep the list of conversion functions up-to-date.
489 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregore80622f2010-09-29 04:25:11 +0000490 // FIXME: We intentionally don't use the decl's access here because it
491 // hasn't been set yet. That's really just a misdesign in Sema.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000492 if (Conversion->getPrimaryTemplate()) {
493 // We don't record specializations.
494 } else if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000495 if (FunTmpl->getPreviousDecl())
496 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000497 FunTmpl);
498 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000499 data().Conversions.addDecl(getASTContext(), FunTmpl);
Douglas Gregore80622f2010-09-29 04:25:11 +0000500 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000501 if (Conversion->getPreviousDecl())
502 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000503 Conversion);
504 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000505 data().Conversions.addDecl(getASTContext(), Conversion);
Douglas Gregore80622f2010-09-29 04:25:11 +0000506 }
507 }
Richard Smithacf796b2012-11-28 06:23:12 +0000508
Richard Smith7d04d3a2012-11-30 05:11:39 +0000509 if (SMKind) {
Richard Smithac713512012-12-08 02:53:02 +0000510 // If this is the first declaration of a special member, we no longer have
511 // an implicit trivial special member.
512 data().HasTrivialSpecialMembers &=
513 data().DeclaredSpecialMembers | ~SMKind;
514
515 if (!Method->isImplicit() && !Method->isUserProvided()) {
516 // This method is user-declared but not user-provided. We can't work out
517 // whether it's trivial yet (not until we get to the end of the class).
518 // We'll handle this method in finishedDefaultedOrDeletedMember.
519 } else if (Method->isTrivial())
520 data().HasTrivialSpecialMembers |= SMKind;
521 else
522 data().DeclaredNonTrivialSpecialMembers |= SMKind;
523
Richard Smith7d04d3a2012-11-30 05:11:39 +0000524 // Note when we have declared a declared special member, and suppress the
525 // implicit declaration of this special member.
526 data().DeclaredSpecialMembers |= SMKind;
527
528 if (!Method->isImplicit()) {
529 data().UserDeclaredSpecialMembers |= SMKind;
530
531 // C++03 [class]p4:
532 // A POD-struct is an aggregate class that has [...] no user-defined
533 // copy assignment operator and no user-defined destructor.
534 //
535 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
536 // aggregates could not have any constructors, clear it even for an
537 // explicitly defaulted or deleted constructor.
538 // type is technically an aggregate in C++0x since it wouldn't be in 03.
539 //
540 // Also, a user-declared move assignment operator makes a class non-POD.
541 // This is an extension in C++03.
542 data().PlainOldData = false;
543 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000544 }
545
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000546 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000547 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000548
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000549 // Handle non-static data members.
550 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000551 // C++ [class.bit]p2:
552 // A declaration for a bit-field that omits the identifier declares an
553 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
554 // initialized.
555 if (Field->isUnnamedBitfield())
556 return;
557
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000558 // C++ [dcl.init.aggr]p1:
559 // An aggregate is an array or a class (clause 9) with [...] no
560 // private or protected non-static data members (clause 11).
561 //
562 // A POD must be an aggregate.
563 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
564 data().Aggregate = false;
565 data().PlainOldData = false;
566 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000567
568 // C++0x [class]p7:
569 // A standard-layout class is a class that:
570 // [...]
571 // -- has the same access control for all non-static data members,
572 switch (D->getAccess()) {
573 case AS_private: data().HasPrivateFields = true; break;
574 case AS_protected: data().HasProtectedFields = true; break;
575 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000576 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000577 };
578 if ((data().HasPrivateFields + data().HasProtectedFields +
579 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000580 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000581
Douglas Gregor2bb11012011-05-13 01:05:07 +0000582 // Keep track of the presence of mutable fields.
583 if (Field->isMutable())
584 data().HasMutableFields = true;
585
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000586 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000587 // A POD struct is a class that is both a trivial class and a
588 // standard-layout class, and has no non-static data members of type
589 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000590 //
591 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
592 // that does not explicitly have no lifetime makes the class a non-POD.
593 // However, we delay setting PlainOldData to false in this case so that
594 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000595 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000596 // In this case, the class will become a non-POD class when we complete
597 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000598 ASTContext &Context = getASTContext();
599 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000600 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000601 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000602 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
603 setHasObjectMember(true);
604 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000605 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000606
Chandler Carrutha8225442011-04-30 09:17:45 +0000607 if (T->isReferenceType()) {
Richard Smithd5bc8672012-12-08 02:01:17 +0000608 if (!Field->hasInClassInitializer())
609 data().HasUninitializedReferenceMember = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000610
Chandler Carrutha8225442011-04-30 09:17:45 +0000611 // C++0x [class]p7:
612 // A standard-layout class is a class that:
613 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000614 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000615 }
616
Richard Smith86c3ae42012-02-13 03:54:03 +0000617 // Record if this field is the first non-literal or volatile field or base.
618 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000619 data().HasNonLiteralTypeFieldsOrBases = true;
620
Richard Smith7a614d82011-06-11 17:19:42 +0000621 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000622 data().HasInClassInitializer = true;
623
624 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000625 // A default constructor is trivial if [...] no non-static data member
626 // of its class has a brace-or-equal-initializer.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000627 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith7a614d82011-06-11 17:19:42 +0000628
Richard Smithd079abf2012-05-07 01:07:30 +0000629 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000630 // An aggregate is a [...] class with [...] no
631 // brace-or-equal-initializers for non-static data members.
632 data().Aggregate = false;
633
Richard Smithd079abf2012-05-07 01:07:30 +0000634 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000635 // A POD struct is [...] a trivial class.
636 data().PlainOldData = false;
637 }
638
Douglas Gregor85606eb2010-09-28 20:50:54 +0000639 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
640 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
641 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000642 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000643 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000644 // -- for all the non-static data members of its class that are of
645 // class type (or array thereof), each such class has a trivial
646 // default constructor.
647 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000648 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000649
650 // C++0x [class.copy]p13:
651 // A copy/move constructor for class X is trivial if [...]
652 // [...]
653 // -- for each non-static data member of X that is of class type (or
654 // an array thereof), the constructor selected to copy/move that
655 // member is trivial;
656 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000657 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000658 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith426391c2012-11-16 00:53:38 +0000659 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000660 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000661
662 // C++0x [class.copy]p27:
663 // A copy/move assignment operator for class X is trivial if [...]
664 // [...]
665 // -- for each non-static data member of X that is of class type (or
666 // an array thereof), the assignment operator selected to
667 // copy/move that member is trivial;
668 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000669 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000670 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith426391c2012-11-16 00:53:38 +0000671 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000672 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000673
Douglas Gregor85606eb2010-09-28 20:50:54 +0000674 if (!FieldRec->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000675 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000676 if (!FieldRec->hasIrrelevantDestructor())
677 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000678 if (FieldRec->hasObjectMember())
679 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000680
681 // C++0x [class]p7:
682 // A standard-layout class is a class that:
683 // -- has no non-static data members of type non-standard-layout
684 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000685 if (!FieldRec->isStandardLayout())
686 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000687
688 // C++0x [class]p7:
689 // A standard-layout class is a class that:
690 // [...]
691 // -- has no base classes of the same type as the first non-static
692 // data member.
693 // We don't want to expend bits in the state of the record decl
694 // tracking whether this is the first non-static data member so we
695 // cheat a bit and use some of the existing state: the empty bit.
696 // Virtual bases and virtual methods make a class non-empty, but they
697 // also make it non-standard-layout so we needn't check here.
698 // A non-empty base class may leave the class standard-layout, but not
699 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000700 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000701 // data member must come through here with Empty still true, and Empty
702 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000703 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000704 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
705 BE = bases_end();
706 BI != BE; ++BI) {
707 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000708 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000709 break;
710 }
711 }
712 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000713
714 // Keep track of the presence of mutable fields.
715 if (FieldRec->hasMutableFields())
716 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000717
718 // C++11 [class.copy]p13:
719 // If the implicitly-defined constructor would satisfy the
720 // requirements of a constexpr constructor, the implicitly-defined
721 // constructor is constexpr.
722 // C++11 [dcl.constexpr]p4:
723 // -- every constructor involved in initializing non-static data
724 // members [...] shall be a constexpr constructor
725 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000726 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000727 // The standard requires any in-class initializer to be a constant
728 // expression. We consider this to be a defect.
729 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithacf796b2012-11-28 06:23:12 +0000730
731 // C++11 [class.copy]p8:
732 // The implicitly-declared copy constructor for a class X will have
733 // the form 'X::X(const X&)' if [...] for all the non-static data
734 // members of X that are of a class type M (or array thereof), each
735 // such class type has a copy constructor whose first parameter is
736 // of type 'const M&' or 'const volatile M&'.
737 if (!FieldRec->hasCopyConstructorWithConstParam())
738 data().ImplicitCopyConstructorHasConstParam = false;
739
740 // C++11 [class.copy]p18:
741 // The implicitly-declared copy assignment oeprator for a class X will
742 // have the form 'X& X::operator=(const X&)' if [...] for all the
743 // non-static data members of X that are of a class type M (or array
744 // thereof), each such class type has a copy assignment operator whose
745 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
746 if (!FieldRec->hasCopyAssignmentWithConstParam())
747 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smithd5bc8672012-12-08 02:01:17 +0000748
749 if (FieldRec->hasUninitializedReferenceMember() &&
750 !Field->hasInClassInitializer())
751 data().HasUninitializedReferenceMember = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000752 }
Richard Smith61802452011-12-22 02:22:31 +0000753 } else {
754 // Base element type of field is a non-class type.
Richard Smithd3861ce2012-06-10 07:07:24 +0000755 if (!T->isLiteralType() ||
756 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000757 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000758 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000759
760 // C++0x [class]p7:
761 // A standard-layout class is a class that:
762 // [...]
763 // -- either has no non-static data members in the most derived
764 // class and at most one base class with non-static data members,
765 // or has no base classes with non-static data members, and
766 // At this point we know that we have a non-static data member, so the last
767 // clause holds.
768 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000769 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000770
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000771 // If this is not a zero-length bit-field, then the class is not empty.
772 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000773 if (!Field->isBitField() ||
774 (!Field->getBitWidth()->isTypeDependent() &&
775 !Field->getBitWidth()->isValueDependent() &&
776 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000777 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000778 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000779 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000780
781 // Handle using declarations of conversion functions.
782 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
783 if (Shadow->getDeclName().getNameKind()
784 == DeclarationName::CXXConversionFunctionName)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000785 data().Conversions.addDecl(getASTContext(), Shadow, Shadow->getAccess());
Joao Matos17d35c32012-08-31 22:18:20 +0000786}
787
Richard Smithac713512012-12-08 02:53:02 +0000788void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
789 assert(!D->isImplicit() && !D->isUserProvided());
790
791 // The kind of special member this declaration is, if any.
792 unsigned SMKind = 0;
793
794 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
795 if (Constructor->isDefaultConstructor()) {
796 SMKind |= SMF_DefaultConstructor;
797 if (Constructor->isConstexpr())
798 data().HasConstexprDefaultConstructor = true;
799 }
800 if (Constructor->isCopyConstructor())
801 SMKind |= SMF_CopyConstructor;
802 else if (Constructor->isMoveConstructor())
803 SMKind |= SMF_MoveConstructor;
804 else if (Constructor->isConstexpr())
805 // We may now know that the constructor is constexpr.
806 data().HasConstexprNonCopyMoveConstructor = true;
807 } else if (isa<CXXDestructorDecl>(D))
808 SMKind |= SMF_Destructor;
809 else if (D->isCopyAssignmentOperator())
810 SMKind |= SMF_CopyAssignment;
811 else if (D->isMoveAssignmentOperator())
812 SMKind |= SMF_MoveAssignment;
813
814 // Update which trivial / non-trivial special members we have.
815 // addedMember will have skipped this step for this member.
816 if (D->isTrivial())
817 data().HasTrivialSpecialMembers |= SMKind;
818 else
819 data().DeclaredNonTrivialSpecialMembers |= SMKind;
820}
821
Joao Matos17d35c32012-08-31 22:18:20 +0000822bool CXXRecordDecl::isCLike() const {
823 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
824 !TemplateOrInstantiation.isNull())
825 return false;
826 if (!hasDefinition())
827 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000828
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000829 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000830}
831
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000832void CXXRecordDecl::getCaptureFields(
833 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000834 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000835 Captures.clear();
836 ThisCapture = 0;
837
Douglas Gregorda8962a2012-02-13 15:44:47 +0000838 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000839 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000840 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000841 C != CEnd; ++C, ++Field) {
842 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +0000843 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000844 continue;
845 }
846
David Blaikie581deb32012-06-06 20:45:41 +0000847 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000848 }
849}
850
851
John McCallb05b5f32010-03-15 09:07:48 +0000852static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
853 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000854 if (isa<UsingShadowDecl>(Conv))
855 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000856 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
857 T = ConvTemp->getTemplatedDecl()->getResultType();
858 else
859 T = cast<CXXConversionDecl>(Conv)->getConversionType();
860 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000861}
862
John McCallb05b5f32010-03-15 09:07:48 +0000863/// Collect the visible conversions of a base class.
864///
James Dennetta1253502012-06-15 22:28:09 +0000865/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +0000866/// \param InVirtual whether this base class is a virtual base (or a base
867/// of a virtual base)
868/// \param Access the access along the inheritance path to this base
869/// \param ParentHiddenTypes the conversions provided by the inheritors
870/// of this base
871/// \param Output the set to which to add conversions from non-virtual bases
872/// \param VOutput the set to which to add conversions from virtual bases
873/// \param HiddenVBaseCs the set of conversions which were hidden in a
874/// virtual base along some inheritance path
875static void CollectVisibleConversions(ASTContext &Context,
876 CXXRecordDecl *Record,
877 bool InVirtual,
878 AccessSpecifier Access,
879 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000880 ASTUnresolvedSet &Output,
John McCallb05b5f32010-03-15 09:07:48 +0000881 UnresolvedSetImpl &VOutput,
882 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
883 // The set of types which have conversions in this class or its
884 // subclasses. As an optimization, we don't copy the derived set
885 // unless it might change.
886 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
887 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
888
889 // Collect the direct conversions and figure out which conversions
890 // will be hidden in the subclasses.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000891 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
892 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
893 if (ConvI != ConvE) {
John McCallb05b5f32010-03-15 09:07:48 +0000894 HiddenTypesBuffer = ParentHiddenTypes;
895 HiddenTypes = &HiddenTypesBuffer;
896
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000897 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +0000898 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
899 bool Hidden = ParentHiddenTypes.count(ConvType);
900 if (!Hidden)
901 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +0000902
903 // If this conversion is hidden and we're in a virtual base,
904 // remember that it's hidden along some inheritance path.
905 if (Hidden && InVirtual)
906 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
907
908 // If this conversion isn't hidden, add it to the appropriate output.
909 else if (!Hidden) {
910 AccessSpecifier IAccess
911 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
912
913 if (InVirtual)
914 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000915 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000916 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000917 }
918 }
919 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000920
John McCallb05b5f32010-03-15 09:07:48 +0000921 // Collect information recursively from any base classes.
922 for (CXXRecordDecl::base_class_iterator
923 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
924 const RecordType *RT = I->getType()->getAs<RecordType>();
925 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000926
John McCallb05b5f32010-03-15 09:07:48 +0000927 AccessSpecifier BaseAccess
928 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
929 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000930
John McCallb05b5f32010-03-15 09:07:48 +0000931 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
932 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
933 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000934 }
John McCallb05b5f32010-03-15 09:07:48 +0000935}
Sebastian Redl9994a342009-10-25 17:03:50 +0000936
John McCallb05b5f32010-03-15 09:07:48 +0000937/// Collect the visible conversions of a class.
938///
939/// This would be extremely straightforward if it weren't for virtual
940/// bases. It might be worth special-casing that, really.
941static void CollectVisibleConversions(ASTContext &Context,
942 CXXRecordDecl *Record,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000943 ASTUnresolvedSet &Output) {
John McCallb05b5f32010-03-15 09:07:48 +0000944 // The collection of all conversions in virtual bases that we've
945 // found. These will be added to the output as long as they don't
946 // appear in the hidden-conversions set.
947 UnresolvedSet<8> VBaseCs;
948
949 // The set of conversions in virtual bases that we've determined to
950 // be hidden.
951 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
952
953 // The set of types hidden by classes derived from this one.
954 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
955
956 // Go ahead and collect the direct conversions and add them to the
957 // hidden-types set.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000958 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
959 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000960 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000961 for (; ConvI != ConvE; ++ConvI)
962 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCallb05b5f32010-03-15 09:07:48 +0000963
964 // Recursively collect conversions from base classes.
965 for (CXXRecordDecl::base_class_iterator
966 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
967 const RecordType *RT = I->getType()->getAs<RecordType>();
968 if (!RT) continue;
969
970 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
971 I->isVirtual(), I->getAccessSpecifier(),
972 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
973 }
974
975 // Add any unhidden conversions provided by virtual bases.
976 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
977 I != E; ++I) {
978 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000979 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000980 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000981}
982
983/// getVisibleConversionFunctions - get all conversion functions visible
984/// in current class; including conversion function templates.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000985std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
986CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000987 // If root class, all conversions are visible.
988 if (bases_begin() == bases_end())
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000989 return std::make_pair(data().Conversions.begin(), data().Conversions.end());
Fariborz Jahanian62509212009-09-12 18:26:03 +0000990 // If visible conversion list is already evaluated, return it.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000991 if (!data().ComputedVisibleConversions) {
992 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
993 data().ComputedVisibleConversions = true;
994 }
995 return std::make_pair(data().VisibleConversions.begin(),
996 data().VisibleConversions.end());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000997}
998
John McCall32daa422010-03-31 01:36:47 +0000999void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1000 // This operation is O(N) but extremely rare. Sema only uses it to
1001 // remove UsingShadowDecls in a class that were followed by a direct
1002 // declaration, e.g.:
1003 // class A : B {
1004 // using B::operator int;
1005 // operator int();
1006 // };
1007 // This is uncommon by itself and even more uncommon in conjunction
1008 // with sufficiently large numbers of directly-declared conversions
1009 // that asymptotic behavior matters.
1010
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001011 ASTUnresolvedSet &Convs = data().Conversions;
John McCall32daa422010-03-31 01:36:47 +00001012 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1013 if (Convs[I].getDecl() == ConvDecl) {
1014 Convs.erase(I);
1015 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1016 && "conversion was found multiple times in unresolved set");
1017 return;
1018 }
1019 }
1020
1021 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001022}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001023
Douglas Gregorf6b11852009-10-08 15:14:33 +00001024CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001025 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001026 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1027
1028 return 0;
1029}
1030
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001031MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1032 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1033}
1034
Douglas Gregorf6b11852009-10-08 15:14:33 +00001035void
1036CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1037 TemplateSpecializationKind TSK) {
1038 assert(TemplateOrInstantiation.isNull() &&
1039 "Previous template or instantiation?");
1040 assert(!isa<ClassTemplateSpecializationDecl>(this));
1041 TemplateOrInstantiation
1042 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1043}
1044
Anders Carlssonb13e3572009-12-07 06:33:48 +00001045TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1046 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001047 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1048 return Spec->getSpecializationKind();
1049
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001050 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001051 return MSInfo->getTemplateSpecializationKind();
1052
1053 return TSK_Undeclared;
1054}
1055
1056void
1057CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1058 if (ClassTemplateSpecializationDecl *Spec
1059 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1060 Spec->setSpecializationKind(TSK);
1061 return;
1062 }
1063
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001064 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001065 MSInfo->setTemplateSpecializationKind(TSK);
1066 return;
1067 }
1068
David Blaikieb219cfc2011-09-23 05:06:16 +00001069 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001070}
1071
Douglas Gregor1d110e02010-07-01 14:13:13 +00001072CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1073 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001074 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001075
1076 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001077 = Context.DeclarationNames.getCXXDestructorName(
1078 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001079
John McCallc0bf4622010-02-23 00:48:20 +00001080 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001081 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001082 if (I == E)
1083 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001085 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001086 return Dtor;
1087}
1088
Douglas Gregorda2142f2011-02-19 18:51:44 +00001089void CXXRecordDecl::completeDefinition() {
1090 completeDefinition(0);
1091}
1092
1093void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1094 RecordDecl::completeDefinition();
1095
David Blaikie4e4d0842012-03-11 07:00:24 +00001096 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001097 // Objective-C Automatic Reference Counting:
1098 // If a class has a non-static data member of Objective-C pointer
1099 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001100 // default constructor (if any), copy constructor, move constructor,
1101 // copy assignment operator, move assignment operator, and destructor are
1102 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001103 struct DefinitionData &Data = data();
1104 Data.PlainOldData = false;
Richard Smith7d04d3a2012-11-30 05:11:39 +00001105 Data.HasTrivialSpecialMembers = 0;
Richard Smithdfefb842012-02-25 07:33:38 +00001106 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001107 }
1108
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001109 // If the class may be abstract (but hasn't been marked as such), check for
1110 // any pure final overriders.
1111 if (mayBeAbstract()) {
1112 CXXFinalOverriderMap MyFinalOverriders;
1113 if (!FinalOverriders) {
1114 getFinalOverriders(MyFinalOverriders);
1115 FinalOverriders = &MyFinalOverriders;
1116 }
1117
1118 bool Done = false;
1119 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1120 MEnd = FinalOverriders->end();
1121 M != MEnd && !Done; ++M) {
1122 for (OverridingMethods::iterator SO = M->second.begin(),
1123 SOEnd = M->second.end();
1124 SO != SOEnd && !Done; ++SO) {
1125 assert(SO->second.size() > 0 &&
1126 "All virtual functions have overridding virtual functions");
1127
1128 // C++ [class.abstract]p4:
1129 // A class is abstract if it contains or inherits at least one
1130 // pure virtual function for which the final overrider is pure
1131 // virtual.
1132 if (SO->second.front().Method->isPure()) {
1133 data().Abstract = true;
1134 Done = true;
1135 break;
1136 }
1137 }
1138 }
1139 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001140
1141 // Set access bits correctly on the directly-declared conversions.
1142 for (UnresolvedSetIterator I = data().Conversions.begin(),
1143 E = data().Conversions.end();
1144 I != E; ++I)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001145 I.setAccess((*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001146}
1147
1148bool CXXRecordDecl::mayBeAbstract() const {
1149 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1150 isDependentContext())
1151 return false;
1152
1153 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1154 BEnd = bases_end();
1155 B != BEnd; ++B) {
1156 CXXRecordDecl *BaseDecl
1157 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1158 if (BaseDecl->isAbstract())
1159 return true;
1160 }
1161
1162 return false;
1163}
1164
David Blaikie99ba9e32011-12-20 02:48:34 +00001165void CXXMethodDecl::anchor() { }
1166
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001167static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1168 const CXXMethodDecl *BaseMD) {
1169 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1170 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1171 const CXXMethodDecl *MD = *I;
1172 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1173 return true;
1174 if (recursivelyOverrides(MD, BaseMD))
1175 return true;
1176 }
1177 return false;
1178}
1179
1180CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001181CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1182 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001183 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1184 return this;
1185
1186 // Lookup doesn't work for destructors, so handle them separately.
1187 if (isa<CXXDestructorDecl>(this)) {
1188 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001189 if (MD) {
1190 if (recursivelyOverrides(MD, this))
1191 return MD;
1192 if (MayBeBase && recursivelyOverrides(this, MD))
1193 return MD;
1194 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001195 return NULL;
1196 }
1197
1198 lookup_const_result Candidates = RD->lookup(getDeclName());
1199 for (NamedDecl * const * I = Candidates.first; I != Candidates.second; ++I) {
1200 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1201 if (!MD)
1202 continue;
1203 if (recursivelyOverrides(MD, this))
1204 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001205 if (MayBeBase && recursivelyOverrides(this, MD))
1206 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001207 }
1208
1209 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1210 E = RD->bases_end(); I != E; ++I) {
1211 const RecordType *RT = I->getType()->getAs<RecordType>();
1212 if (!RT)
1213 continue;
1214 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1215 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1216 if (T)
1217 return T;
1218 }
1219
1220 return NULL;
1221}
1222
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001223CXXMethodDecl *
1224CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001225 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001226 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001227 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001228 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001229 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001230 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001231 isStatic, SCAsWritten, isInline, isConstexpr,
1232 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001233}
1234
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001235CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1236 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1237 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1238 DeclarationNameInfo(), QualType(),
1239 0, false, SC_None, false, false,
1240 SourceLocation());
1241}
1242
Douglas Gregor90916562009-09-29 18:16:17 +00001243bool CXXMethodDecl::isUsualDeallocationFunction() const {
1244 if (getOverloadedOperator() != OO_Delete &&
1245 getOverloadedOperator() != OO_Array_Delete)
1246 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001247
1248 // C++ [basic.stc.dynamic.deallocation]p2:
1249 // A template instance is never a usual deallocation function,
1250 // regardless of its signature.
1251 if (getPrimaryTemplate())
1252 return false;
1253
Douglas Gregor90916562009-09-29 18:16:17 +00001254 // C++ [basic.stc.dynamic.deallocation]p2:
1255 // If a class T has a member deallocation function named operator delete
1256 // with exactly one parameter, then that function is a usual (non-placement)
1257 // deallocation function. [...]
1258 if (getNumParams() == 1)
1259 return true;
1260
1261 // C++ [basic.stc.dynamic.deallocation]p2:
1262 // [...] If class T does not declare such an operator delete but does
1263 // declare a member deallocation function named operator delete with
1264 // exactly two parameters, the second of which has type std::size_t (18.1),
1265 // then this function is a usual deallocation function.
1266 ASTContext &Context = getASTContext();
1267 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001268 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1269 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001270 return false;
1271
1272 // This function is a usual deallocation function if there are no
1273 // single-parameter deallocation functions of the same kind.
1274 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1275 R.first != R.second; ++R.first) {
1276 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1277 if (FD->getNumParams() == 1)
1278 return false;
1279 }
1280
1281 return true;
1282}
1283
Douglas Gregor06a9f362010-05-01 20:49:11 +00001284bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001285 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001286 // A user-declared copy assignment operator X::operator= is a non-static
1287 // non-template member function of class X with exactly one parameter of
1288 // type X, X&, const X&, volatile X& or const volatile X&.
1289 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1290 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001291 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001292 return false;
1293
1294 QualType ParamType = getParamDecl(0)->getType();
1295 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1296 ParamType = Ref->getPointeeType();
1297
1298 ASTContext &Context = getASTContext();
1299 QualType ClassType
1300 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1301 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1302}
1303
Sean Huntffe37fd2011-05-25 20:50:04 +00001304bool CXXMethodDecl::isMoveAssignmentOperator() const {
1305 // C++0x [class.copy]p19:
1306 // A user-declared move assignment operator X::operator= is a non-static
1307 // non-template member function of class X with exactly one parameter of type
1308 // X&&, const X&&, volatile X&&, or const volatile X&&.
1309 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1310 getPrimaryTemplate() || getDescribedFunctionTemplate())
1311 return false;
1312
1313 QualType ParamType = getParamDecl(0)->getType();
1314 if (!isa<RValueReferenceType>(ParamType))
1315 return false;
1316 ParamType = ParamType->getPointeeType();
1317
1318 ASTContext &Context = getASTContext();
1319 QualType ClassType
1320 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1321 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1322}
1323
Anders Carlsson05eb2442009-05-16 23:58:37 +00001324void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001325 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001326 assert(!MD->getParent()->isDependentContext() &&
1327 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001328 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001329
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001330 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001331}
1332
1333CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001334 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001335 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001336}
1337
1338CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001339 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001340 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001341}
1342
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001343unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001344 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001345 return getASTContext().overridden_methods_size(this);
1346}
1347
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001348QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001349 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1350 // If the member function is declared const, the type of this is const X*,
1351 // if the member function is declared volatile, the type of this is
1352 // volatile X*, and if the member function is declared const volatile,
1353 // the type of this is const volatile X*.
1354
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001355 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001356
John McCall3cb0ebd2010-03-10 03:28:59 +00001357 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001358 ClassTy = C.getQualifiedType(ClassTy,
1359 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001360 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001361}
1362
Eli Friedmand7d7f672009-12-06 20:50:05 +00001363bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001364 // If this function is a template instantiation, look at the template from
1365 // which it was instantiated.
1366 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1367 if (!CheckFn)
1368 CheckFn = this;
1369
Eli Friedmand7d7f672009-12-06 20:50:05 +00001370 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001371 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001372}
1373
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001374bool CXXMethodDecl::isLambdaStaticInvoker() const {
1375 return getParent()->isLambda() &&
1376 getIdentifier() && getIdentifier()->getName() == "__invoke";
1377}
1378
1379
Sean Huntcbb67482011-01-08 20:30:50 +00001380CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1381 TypeSourceInfo *TInfo, bool IsVirtual,
1382 SourceLocation L, Expr *Init,
1383 SourceLocation R,
1384 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001385 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001386 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1387 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001388{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001389}
1390
Sean Huntcbb67482011-01-08 20:30:50 +00001391CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1392 FieldDecl *Member,
1393 SourceLocation MemberLoc,
1394 SourceLocation L, Expr *Init,
1395 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001396 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001397 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001398 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1399{
1400}
1401
Sean Huntcbb67482011-01-08 20:30:50 +00001402CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1403 IndirectFieldDecl *Member,
1404 SourceLocation MemberLoc,
1405 SourceLocation L, Expr *Init,
1406 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001407 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001408 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001409 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001410{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001411}
1412
Sean Huntcbb67482011-01-08 20:30:50 +00001413CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001414 TypeSourceInfo *TInfo,
1415 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001416 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001417 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1418 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001419 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1420{
1421}
1422
1423CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001424 FieldDecl *Member,
1425 SourceLocation MemberLoc,
1426 SourceLocation L, Expr *Init,
1427 SourceLocation R,
1428 VarDecl **Indices,
1429 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001430 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001431 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001432 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001433{
1434 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1435 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1436}
1437
Sean Huntcbb67482011-01-08 20:30:50 +00001438CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1439 FieldDecl *Member,
1440 SourceLocation MemberLoc,
1441 SourceLocation L, Expr *Init,
1442 SourceLocation R,
1443 VarDecl **Indices,
1444 unsigned NumIndices) {
1445 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001446 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001447 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001448 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1449 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001450}
1451
Sean Huntcbb67482011-01-08 20:30:50 +00001452TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001453 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001454 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001455 else
1456 return TypeLoc();
1457}
1458
Sean Huntcbb67482011-01-08 20:30:50 +00001459const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001460 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001461 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001462 else
1463 return 0;
1464}
1465
Sean Huntcbb67482011-01-08 20:30:50 +00001466SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001467 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001468 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001469
1470 if (isInClassMemberInitializer())
1471 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001472
Douglas Gregor76852c22011-11-01 01:16:03 +00001473 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1474 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1475
1476 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001477}
1478
Sean Huntcbb67482011-01-08 20:30:50 +00001479SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001480 if (isInClassMemberInitializer()) {
1481 FieldDecl *D = getAnyMember();
1482 if (Expr *I = D->getInClassInitializer())
1483 return I->getSourceRange();
1484 return SourceRange();
1485 }
1486
Douglas Gregor802ab452009-12-02 22:36:29 +00001487 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001488}
1489
David Blaikie99ba9e32011-12-20 02:48:34 +00001490void CXXConstructorDecl::anchor() { }
1491
Douglas Gregorb48fe382008-10-31 09:07:45 +00001492CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001493CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1494 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1495 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1496 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001497}
1498
1499CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001500CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001501 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001502 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001503 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001504 bool isExplicit, bool isInline,
1505 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001506 assert(NameInfo.getName().getNameKind()
1507 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001508 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001509 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001510 isExplicit, isInline, isImplicitlyDeclared,
1511 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001512}
1513
Douglas Gregor76852c22011-11-01 01:16:03 +00001514CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1515 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1516 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1517 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1518 return Construct->getConstructor();
1519
1520 return 0;
1521}
1522
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001523bool CXXConstructorDecl::isDefaultConstructor() const {
1524 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001525 // A default constructor for a class X is a constructor of class
1526 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001527 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001528 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001529}
1530
Mike Stump1eb44332009-09-09 15:08:12 +00001531bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001532CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001533 return isCopyOrMoveConstructor(TypeQuals) &&
1534 getParamDecl(0)->getType()->isLValueReferenceType();
1535}
1536
1537bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1538 return isCopyOrMoveConstructor(TypeQuals) &&
1539 getParamDecl(0)->getType()->isRValueReferenceType();
1540}
1541
1542/// \brief Determine whether this is a copy or move constructor.
1543bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001544 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001545 // A non-template constructor for class X is a copy constructor
1546 // if its first parameter is of type X&, const X&, volatile X& or
1547 // const volatile X&, and either there are no other parameters
1548 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001549 // C++0x [class.copy]p3:
1550 // A non-template constructor for class X is a move constructor if its
1551 // first parameter is of type X&&, const X&&, volatile X&&, or
1552 // const volatile X&&, and either there are no other parameters or else
1553 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001554 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001555 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001556 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001557 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001558 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001559
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001560 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001561
1562 // Do we have a reference type?
1563 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001564 if (!ParamRefType)
1565 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001566
Douglas Gregorfd476482009-11-13 23:59:09 +00001567 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001568 ASTContext &Context = getASTContext();
1569
Douglas Gregorfd476482009-11-13 23:59:09 +00001570 CanQualType PointeeType
1571 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001572 CanQualType ClassTy
1573 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001574 if (PointeeType.getUnqualifiedType() != ClassTy)
1575 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001576
John McCall0953e762009-09-24 19:53:00 +00001577 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001578
1579 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001580 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001581 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001582}
1583
Anders Carlssonfaccd722009-08-28 16:57:08 +00001584bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001585 // C++ [class.conv.ctor]p1:
1586 // A constructor declared without the function-specifier explicit
1587 // that can be called with a single parameter specifies a
1588 // conversion from the type of its first parameter to the type of
1589 // its class. Such a constructor is called a converting
1590 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001591 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001592 return false;
1593
Mike Stump1eb44332009-09-09 15:08:12 +00001594 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001595 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001596 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001597 (getNumParams() > 1 &&
1598 (getParamDecl(1)->hasDefaultArg() ||
1599 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001600}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001601
Douglas Gregor6493cc52010-11-08 17:16:59 +00001602bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001603 if ((getNumParams() < 1) ||
1604 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1605 (getPrimaryTemplate() == 0) ||
1606 (getDescribedFunctionTemplate() != 0))
1607 return false;
1608
1609 const ParmVarDecl *Param = getParamDecl(0);
1610
1611 ASTContext &Context = getASTContext();
1612 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1613
Douglas Gregor66724ea2009-11-14 01:20:54 +00001614 // Is it the same as our our class type?
1615 CanQualType ClassTy
1616 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1617 if (ParamType.getUnqualifiedType() != ClassTy)
1618 return false;
1619
1620 return true;
1621}
1622
Sebastian Redlf677ea32011-02-05 19:23:19 +00001623const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1624 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001625 method_iterator It = getASTContext().overridden_methods_begin(this);
1626 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001627 return 0;
1628
1629 return cast<CXXConstructorDecl>(*It);
1630}
1631
1632void
1633CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1634 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001635 assert(getASTContext().overridden_methods_size(this) == 0 &&
1636 "Base ctor already set.");
1637 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001638}
1639
David Blaikie99ba9e32011-12-20 02:48:34 +00001640void CXXDestructorDecl::anchor() { }
1641
Douglas Gregor42a552f2008-11-05 20:51:48 +00001642CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001643CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1644 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1645 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001646 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001647}
1648
1649CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001650CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001651 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001652 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001653 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001654 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001655 assert(NameInfo.getName().getNameKind()
1656 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001657 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001658 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001659 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001660}
1661
David Blaikie99ba9e32011-12-20 02:48:34 +00001662void CXXConversionDecl::anchor() { }
1663
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001664CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001665CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1666 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1667 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1668 QualType(), 0, false, false, false,
1669 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001670}
1671
1672CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001673CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001674 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001675 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001676 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001677 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001678 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001679 assert(NameInfo.getName().getNameKind()
1680 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001681 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001682 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001683 isInline, isExplicit, isConstexpr,
1684 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001685}
1686
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001687bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1688 return isImplicit() && getParent()->isLambda() &&
1689 getConversionType()->isBlockPointerType();
1690}
1691
David Blaikie99ba9e32011-12-20 02:48:34 +00001692void LinkageSpecDecl::anchor() { }
1693
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001694LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001695 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001696 SourceLocation ExternLoc,
1697 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001698 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001699 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001700 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001701}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001702
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001703LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1704 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1705 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1706 lang_c, SourceLocation());
1707}
1708
David Blaikie99ba9e32011-12-20 02:48:34 +00001709void UsingDirectiveDecl::anchor() { }
1710
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001711UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1712 SourceLocation L,
1713 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001714 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001715 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001716 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001717 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001718 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1719 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001720 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1721 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001722}
1723
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001724UsingDirectiveDecl *
1725UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1726 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1727 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1728 NestedNameSpecifierLoc(),
1729 SourceLocation(), 0, 0);
1730}
1731
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001732NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1733 if (NamespaceAliasDecl *NA =
1734 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1735 return NA->getNamespace();
1736 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1737}
1738
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001739void NamespaceDecl::anchor() { }
1740
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001741NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1742 SourceLocation StartLoc,
1743 SourceLocation IdLoc, IdentifierInfo *Id,
1744 NamespaceDecl *PrevDecl)
1745 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1746 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1747{
1748 setPreviousDeclaration(PrevDecl);
1749
1750 if (PrevDecl)
1751 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1752}
1753
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001754NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001755 bool Inline, SourceLocation StartLoc,
1756 SourceLocation IdLoc, IdentifierInfo *Id,
1757 NamespaceDecl *PrevDecl) {
1758 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001759}
1760
1761NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1762 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001763 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1764 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001765}
1766
David Blaikie99ba9e32011-12-20 02:48:34 +00001767void NamespaceAliasDecl::anchor() { }
1768
Mike Stump1eb44332009-09-09 15:08:12 +00001769NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001770 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001771 SourceLocation AliasLoc,
1772 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001773 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001774 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001775 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001776 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1777 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001778 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1779 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001780}
1781
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001782NamespaceAliasDecl *
1783NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1784 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1785 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1786 NestedNameSpecifierLoc(),
1787 SourceLocation(), 0);
1788}
1789
David Blaikie99ba9e32011-12-20 02:48:34 +00001790void UsingShadowDecl::anchor() { }
1791
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001792UsingShadowDecl *
1793UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1794 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1795 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1796}
1797
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001798UsingDecl *UsingShadowDecl::getUsingDecl() const {
1799 const UsingShadowDecl *Shadow = this;
1800 while (const UsingShadowDecl *NextShadow =
1801 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1802 Shadow = NextShadow;
1803 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1804}
1805
David Blaikie99ba9e32011-12-20 02:48:34 +00001806void UsingDecl::anchor() { }
1807
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001808void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1809 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1810 "declaration already in set");
1811 assert(S->getUsingDecl() == this);
1812
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001813 if (FirstUsingShadow.getPointer())
1814 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1815 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001816}
1817
1818void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1819 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1820 "declaration not in set");
1821 assert(S->getUsingDecl() == this);
1822
1823 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1824
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001825 if (FirstUsingShadow.getPointer() == S) {
1826 FirstUsingShadow.setPointer(
1827 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001828 S->UsingOrNextShadow = this;
1829 return;
1830 }
1831
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001832 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001833 while (Prev->UsingOrNextShadow != S)
1834 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1835 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1836 S->UsingOrNextShadow = this;
1837}
1838
Douglas Gregordc355712011-02-25 00:36:19 +00001839UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1840 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001841 const DeclarationNameInfo &NameInfo,
1842 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001843 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001844}
1845
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001846UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1847 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1848 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1849 DeclarationNameInfo(), false);
1850}
1851
David Blaikie99ba9e32011-12-20 02:48:34 +00001852void UnresolvedUsingValueDecl::anchor() { }
1853
John McCall7ba107a2009-11-18 02:36:19 +00001854UnresolvedUsingValueDecl *
1855UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1856 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001857 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001858 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001859 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001860 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001861}
1862
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001863UnresolvedUsingValueDecl *
1864UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1865 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1866 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1867 NestedNameSpecifierLoc(),
1868 DeclarationNameInfo());
1869}
1870
David Blaikie99ba9e32011-12-20 02:48:34 +00001871void UnresolvedUsingTypenameDecl::anchor() { }
1872
John McCall7ba107a2009-11-18 02:36:19 +00001873UnresolvedUsingTypenameDecl *
1874UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1875 SourceLocation UsingLoc,
1876 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001877 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001878 SourceLocation TargetNameLoc,
1879 DeclarationName TargetName) {
1880 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001881 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001882 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001883}
1884
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001885UnresolvedUsingTypenameDecl *
1886UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1887 void *Mem = AllocateDeserializedDecl(C, ID,
1888 sizeof(UnresolvedUsingTypenameDecl));
1889 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
1890 SourceLocation(),
1891 NestedNameSpecifierLoc(),
1892 SourceLocation(),
1893 0);
1894}
1895
David Blaikie99ba9e32011-12-20 02:48:34 +00001896void StaticAssertDecl::anchor() { }
1897
Anders Carlssonfb311762009-03-14 00:25:26 +00001898StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001899 SourceLocation StaticAssertLoc,
1900 Expr *AssertExpr,
1901 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00001902 SourceLocation RParenLoc,
1903 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001904 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00001905 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00001906}
1907
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001908StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
1909 unsigned ID) {
1910 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00001911 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
1912 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001913}
1914
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001915static const char *getAccessName(AccessSpecifier AS) {
1916 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001917 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00001918 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001919 case AS_public:
1920 return "public";
1921 case AS_private:
1922 return "private";
1923 case AS_protected:
1924 return "protected";
1925 }
David Blaikie561d3ab2012-01-17 02:30:50 +00001926 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001927}
1928
1929const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1930 AccessSpecifier AS) {
1931 return DB << getAccessName(AS);
1932}
Richard Smithf15fda02012-02-02 01:16:57 +00001933
1934const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
1935 AccessSpecifier AS) {
1936 return DB << getAccessName(AS);
1937}