blob: 4d963cf4b3c6624d7174858810731d656ad4cf82 [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
Richard Smithc2d77572013-08-30 04:46:40 +000038void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
39 ExternalASTSource *Source = C.getExternalSource();
40 assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
41 assert(Source && "getFromExternalSource with no external source");
42
43 for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
44 I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
45 reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
46 Impl.Decls.setLazy(false);
47}
48
John McCall86ff3082010-02-04 22:26:26 +000049CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Richard Smith7d04d3a2012-11-30 05:11:39 +000050 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
Eli Friedman97c134e2009-08-15 22:23:00 +000051 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000052 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000053 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000054 HasMutableFields(false), HasOnlyCMembers(true),
Richard Smithd5bc8672012-12-08 02:01:17 +000055 HasInClassInitializer(false), HasUninitializedReferenceMember(false),
Richard Smithbc2a35d2012-12-08 08:32:28 +000056 NeedOverloadResolutionForMoveConstructor(false),
57 NeedOverloadResolutionForMoveAssignment(false),
58 NeedOverloadResolutionForDestructor(false),
59 DefaultedMoveConstructorIsDeleted(false),
60 DefaultedMoveAssignmentIsDeleted(false),
61 DefaultedDestructorIsDeleted(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000062 HasTrivialSpecialMembers(SMF_All),
Richard Smithac713512012-12-08 02:53:02 +000063 DeclaredNonTrivialSpecialMembers(0),
Richard Smith7d04d3a2012-11-30 05:11:39 +000064 HasIrrelevantDestructor(true),
Richard Smith61802452011-12-22 02:22:31 +000065 HasConstexprNonCopyMoveConstructor(false),
66 DefaultedDefaultConstructorIsConstexpr(true),
Richard Smith7d04d3a2012-11-30 05:11:39 +000067 HasConstexprDefaultConstructor(false),
Sean Hunt023df372011-05-09 18:22:59 +000068 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000069 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smithacf796b2012-11-28 06:23:12 +000070 ImplicitCopyConstructorHasConstParam(true),
71 ImplicitCopyAssignmentHasConstParam(true),
72 HasDeclaredCopyConstructorWithConstParam(false),
73 HasDeclaredCopyAssignmentWithConstParam(false),
74 FailedImplicitMoveConstructor(false), FailedImplicitMoveAssignment(false),
75 IsLambda(false), NumBases(0), NumVBases(0), Bases(), VBases(),
Richard Smith4fc50892013-06-26 02:41:25 +000076 Definition(D), FirstFriend() {
John McCall86ff3082010-02-04 22:26:26 +000077}
78
Benjamin Krameree3096a2012-07-04 17:03:33 +000079CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
80 return Bases.get(Definition->getASTContext().getExternalSource());
81}
82
83CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
84 return VBases.get(Definition->getASTContext().getExternalSource());
85}
86
John McCall86ff3082010-02-04 22:26:26 +000087CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000088 SourceLocation StartLoc, SourceLocation IdLoc,
89 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
90 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000091 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000092 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000093
Jay Foad4ba2a172011-01-12 09:06:06 +000094CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000095 DeclContext *DC, SourceLocation StartLoc,
96 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000097 CXXRecordDecl* PrevDecl,
98 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000099 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
100 Id, PrevDecl);
Douglas Gregor6bd99292013-02-09 01:35:03 +0000101 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000103 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000104 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +0000105 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000106 return R;
107}
108
Douglas Gregorda8962a2012-02-13 15:44:47 +0000109CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Eli Friedman8da8a662012-09-19 01:18:11 +0000110 TypeSourceInfo *Info, SourceLocation Loc,
111 bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +0000112 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
113 0, 0);
114 R->IsBeingDefined = true;
Eli Friedman8da8a662012-09-19 01:18:11 +0000115 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info, Dependent);
Douglas Gregor6bd99292013-02-09 01:35:03 +0000116 R->MayHaveOutOfDateDef = false;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000117 C.getTypeDeclType(R, /*PrevDecl=*/0);
118 return R;
119}
120
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000121CXXRecordDecl *
122CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
123 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +0000124 CXXRecordDecl *R = new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0,
125 SourceLocation(), SourceLocation(),
126 0, 0);
127 R->MayHaveOutOfDateDef = false;
128 return R;
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000129}
130
Mike Stump1eb44332009-09-09 15:08:12 +0000131void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000132CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000133 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000134 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000135
Douglas Gregor7c789c12010-10-29 22:39:52 +0000136 if (!data().Bases.isOffset() && data().NumBases > 0)
137 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Richard Smithdd677232011-10-18 20:08:55 +0000139 if (NumBases) {
140 // C++ [dcl.init.aggr]p1:
141 // An aggregate is [...] a class with [...] no base classes [...].
142 data().Aggregate = false;
143
144 // C++ [class]p4:
145 // A POD-struct is an aggregate class...
146 data().PlainOldData = false;
147 }
148
Anders Carlsson6f6de732010-03-29 05:13:12 +0000149 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000150 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000151
152 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000153 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000154
John McCall86ff3082010-02-04 22:26:26 +0000155 data().Bases = new(C) CXXBaseSpecifier [NumBases];
156 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000157 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000158 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000159 // Keep track of inherited vbases for this base class.
160 const CXXBaseSpecifier *Base = Bases[i];
161 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000162 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000163 if (BaseType->isDependentType())
164 continue;
165 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000166 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000167
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000168 // A class with a non-empty base class is not empty.
169 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000170 if (!BaseClassDecl->isEmpty()) {
171 if (!data().Empty) {
172 // C++0x [class]p7:
173 // A standard-layout class is a class that:
174 // [...]
175 // -- either has no non-static data members in the most derived
176 // class and at most one base class with non-static data members,
177 // or has no base classes with non-static data members, and
178 // If this is the second non-empty base, then neither of these two
179 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000180 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000181 }
182
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000183 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000184 data().HasNoNonEmptyBases = false;
185 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000186
Douglas Gregor85606eb2010-09-28 20:50:54 +0000187 // C++ [class.virtual]p1:
188 // A class that declares or inherits a virtual function is called a
189 // polymorphic class.
190 if (BaseClassDecl->isPolymorphic())
191 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000192
Chandler Carrutha8225442011-04-30 09:17:45 +0000193 // C++0x [class]p7:
194 // A standard-layout class is a class that: [...]
195 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000196 if (!BaseClassDecl->isStandardLayout())
197 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000198
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000199 // Record if this base is the first non-literal field or base.
Richard Smitha10b9782013-04-22 15:31:51 +0000200 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000201 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000202
Anders Carlsson6f6de732010-03-29 05:13:12 +0000203 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000204 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000205 BaseClassDecl->vbases_begin(),
206 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000207 // Add this base if it's not already in the list.
Richard Smithacf796b2012-11-28 06:23:12 +0000208 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000209 VBases.push_back(VBase);
Richard Smithacf796b2012-11-28 06:23:12 +0000210
211 // C++11 [class.copy]p8:
212 // The implicitly-declared copy constructor for a class X will have
213 // the form 'X::X(const X&)' if each [...] virtual base class B of X
214 // has a copy constructor whose first parameter is of type
215 // 'const B&' or 'const volatile B&' [...]
216 if (CXXRecordDecl *VBaseDecl = VBase->getType()->getAsCXXRecordDecl())
217 if (!VBaseDecl->hasCopyConstructorWithConstParam())
218 data().ImplicitCopyConstructorHasConstParam = false;
219 }
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000220 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000221
222 if (Base->isVirtual()) {
223 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000224 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Richard Smithacf796b2012-11-28 06:23:12 +0000225 VBases.push_back(Base);
226
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000227 // C++0x [meta.unary.prop] is_empty:
228 // T is a class type, but not a union type, with ... no virtual base
229 // classes
230 data().Empty = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000231
Richard Smith7d04d3a2012-11-30 05:11:39 +0000232 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
233 // A [default constructor, copy/move constructor, or copy/move assignment
234 // operator for a class X] is trivial [...] if:
235 // -- class X has [...] no virtual base classes
236 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carrutha8225442011-04-30 09:17:45 +0000237
238 // C++0x [class]p7:
239 // A standard-layout class is a class that: [...]
240 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000241 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000242
243 // C++11 [dcl.constexpr]p4:
244 // In the definition of a constexpr constructor [...]
245 // -- the class shall not have any virtual base classes
246 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000247 } else {
248 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000249 // A default constructor is trivial [...] if:
250 // -- all the direct base classes of its class have trivial default
251 // constructors.
252 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000253 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
254
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000255 // C++0x [class.copy]p13:
256 // A copy/move constructor for class X is trivial if [...]
257 // [...]
258 // -- the constructor selected to copy/move each direct base class
259 // subobject is trivial, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000260 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000261 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000262 // If the base class doesn't have a simple move constructor, we'll eagerly
263 // declare it and perform overload resolution to determine which function
264 // it actually calls. If it does have a simple move constructor, this
265 // check is correct.
Richard Smith426391c2012-11-16 00:53:38 +0000266 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000267 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000268
269 // C++0x [class.copy]p27:
270 // A copy/move assignment operator for class X is trivial if [...]
271 // [...]
272 // -- the assignment operator selected to copy/move each direct base
273 // class subobject is trivial, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000274 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000275 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000276 // If the base class doesn't have a simple move assignment, we'll eagerly
277 // declare it and perform overload resolution to determine which function
278 // it actually calls. If it does have a simple move assignment, this
279 // check is correct.
Richard Smith426391c2012-11-16 00:53:38 +0000280 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000281 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smith61802452011-12-22 02:22:31 +0000282
283 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000284 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000285 // requirements of a constexpr constructor, the implicitly-defined
286 // default constructor is constexpr.
287 if (!BaseClassDecl->hasConstexprDefaultConstructor())
288 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000289 }
Richard Smithac713512012-12-08 02:53:02 +0000290
Douglas Gregor85606eb2010-09-28 20:50:54 +0000291 // C++ [class.ctor]p3:
292 // A destructor is trivial if all the direct base classes of its class
293 // have trivial destructors.
294 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000295 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000296
297 if (!BaseClassDecl->hasIrrelevantDestructor())
298 data().HasIrrelevantDestructor = false;
299
Richard Smithacf796b2012-11-28 06:23:12 +0000300 // C++11 [class.copy]p18:
301 // The implicitly-declared copy assignment oeprator for a class X will
302 // have the form 'X& X::operator=(const X&)' if each direct base class B
303 // of X has a copy assignment operator whose parameter is of type 'const
304 // B&', 'const volatile B&', or 'B' [...]
305 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
306 data().ImplicitCopyAssignmentHasConstParam = false;
307
308 // C++11 [class.copy]p8:
309 // The implicitly-declared copy constructor for a class X will have
310 // the form 'X::X(const X&)' if each direct [...] base class B of X
311 // has a copy constructor whose first parameter is of type
312 // 'const B&' or 'const volatile B&' [...]
313 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
314 data().ImplicitCopyConstructorHasConstParam = false;
315
John McCallf85e1932011-06-15 23:02:42 +0000316 // A class has an Objective-C object member if... or any of its bases
317 // has an Objective-C object member.
318 if (BaseClassDecl->hasObjectMember())
319 setHasObjectMember(true);
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +0000320
321 if (BaseClassDecl->hasVolatileMember())
322 setHasVolatileMember(true);
John McCallf85e1932011-06-15 23:02:42 +0000323
Douglas Gregor2bb11012011-05-13 01:05:07 +0000324 // Keep track of the presence of mutable fields.
325 if (BaseClassDecl->hasMutableFields())
326 data().HasMutableFields = true;
Richard Smithd5bc8672012-12-08 02:01:17 +0000327
328 if (BaseClassDecl->hasUninitializedReferenceMember())
329 data().HasUninitializedReferenceMember = true;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000330
331 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000332 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000333
334 if (VBases.empty())
335 return;
336
337 // Create base specifier for any direct or indirect virtual bases.
338 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
339 data().NumVBases = VBases.size();
Richard Smithbc2a35d2012-12-08 08:32:28 +0000340 for (int I = 0, E = VBases.size(); I != E; ++I) {
341 QualType Type = VBases[I]->getType();
342 if (!Type->isDependentType())
343 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000344 data().getVBases()[I] = *VBases[I];
Richard Smithbc2a35d2012-12-08 08:32:28 +0000345 }
346}
347
348void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
349 // C++11 [class.copy]p11:
350 // A defaulted copy/move constructor for a class X is defined as
351 // deleted if X has:
352 // -- a direct or virtual base class B that cannot be copied/moved [...]
353 // -- a non-static data member of class type M (or array thereof)
354 // that cannot be copied or moved [...]
355 if (!Subobj->hasSimpleMoveConstructor())
356 data().NeedOverloadResolutionForMoveConstructor = true;
357
358 // C++11 [class.copy]p23:
359 // A defaulted copy/move assignment operator for a class X is defined as
360 // deleted if X has:
361 // -- a direct or virtual base class B that cannot be copied/moved [...]
362 // -- a non-static data member of class type M (or array thereof)
363 // that cannot be copied or moved [...]
364 if (!Subobj->hasSimpleMoveAssignment())
365 data().NeedOverloadResolutionForMoveAssignment = true;
366
367 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
368 // A defaulted [ctor or dtor] for a class X is defined as
369 // deleted if X has:
370 // -- any direct or virtual base class [...] has a type with a destructor
371 // that is deleted or inaccessible from the defaulted [ctor or dtor].
372 // -- any non-static data member has a type with a destructor
373 // that is deleted or inaccessible from the defaulted [ctor or dtor].
374 if (!Subobj->hasSimpleDestructor()) {
375 data().NeedOverloadResolutionForMoveConstructor = true;
376 data().NeedOverloadResolutionForDestructor = true;
377 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000378}
379
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000380/// Callback function for CXXRecordDecl::forallBases that acknowledges
381/// that it saw a base class.
382static bool SawBase(const CXXRecordDecl *, void *) {
383 return true;
384}
385
386bool CXXRecordDecl::hasAnyDependentBases() const {
387 if (!isDependentContext())
388 return false;
389
390 return !forallBases(SawBase, 0);
391}
392
Chandler Carruthb7e95892011-04-23 10:47:28 +0000393bool CXXRecordDecl::isTriviallyCopyable() const {
394 // C++0x [class]p5:
395 // A trivially copyable class is a class that:
396 // -- has no non-trivial copy constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000397 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000398 // -- has no non-trivial move constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000399 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000400 // -- has no non-trivial copy assignment operators,
Richard Smith426391c2012-11-16 00:53:38 +0000401 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000402 // -- has no non-trivial move assignment operators, and
Richard Smith426391c2012-11-16 00:53:38 +0000403 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000404 // -- has a trivial destructor.
405 if (!hasTrivialDestructor()) return false;
406
407 return true;
408}
409
Douglas Gregor21386642010-09-28 21:55:22 +0000410void CXXRecordDecl::markedVirtualFunctionPure() {
411 // C++ [class.abstract]p2:
412 // A class is abstract if it has at least one pure virtual function.
413 data().Abstract = true;
414}
415
416void CXXRecordDecl::addedMember(Decl *D) {
Joao Matos17d35c32012-08-31 22:18:20 +0000417 if (!D->isImplicit() &&
418 !isa<FieldDecl>(D) &&
419 !isa<IndirectFieldDecl>(D) &&
420 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
421 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
422 data().HasOnlyCMembers = false;
423
424 // Ignore friends and invalid declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000425 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000426 return;
427
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000428 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
429 if (FunTmpl)
430 D = FunTmpl->getTemplatedDecl();
431
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000432 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
433 if (Method->isVirtual()) {
434 // C++ [dcl.init.aggr]p1:
435 // An aggregate is an array or a class with [...] no virtual functions.
436 data().Aggregate = false;
437
438 // C++ [class]p4:
439 // A POD-struct is an aggregate class...
440 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000441
442 // Virtual functions make the class non-empty.
443 // FIXME: Standard ref?
444 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000445
446 // C++ [class.virtual]p1:
447 // A class that declares or inherits a virtual function is called a
448 // polymorphic class.
449 data().Polymorphic = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000450
Richard Smith7d04d3a2012-11-30 05:11:39 +0000451 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
452 // A [default constructor, copy/move constructor, or copy/move
453 // assignment operator for a class X] is trivial [...] if:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000454 // -- class X has no virtual functions [...]
Richard Smith7d04d3a2012-11-30 05:11:39 +0000455 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000456
Chandler Carrutha8225442011-04-30 09:17:45 +0000457 // C++0x [class]p7:
458 // A standard-layout class is a class that: [...]
459 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000460 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000461 }
462 }
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000463
Richard Smithacf796b2012-11-28 06:23:12 +0000464 // Notify the listener if an implicit member was added after the definition
465 // was completed.
466 if (!isBeingDefined() && D->isImplicit())
467 if (ASTMutationListener *L = getASTMutationListener())
468 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000469
Richard Smith7d04d3a2012-11-30 05:11:39 +0000470 // The kind of special member this declaration is, if any.
471 unsigned SMKind = 0;
472
Richard Smithacf796b2012-11-28 06:23:12 +0000473 // Handle constructors.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000474 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smithacf796b2012-11-28 06:23:12 +0000475 if (!Constructor->isImplicit()) {
476 // Note that we have a user-declared constructor.
477 data().UserDeclaredConstructor = true;
478
479 // C++ [class]p4:
480 // A POD-struct is an aggregate class [...]
481 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
482 // type is technically an aggregate in C++0x since it wouldn't be in 03.
483 data().PlainOldData = false;
484 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000485
Richard Smith017ab772011-09-05 02:13:09 +0000486 // Technically, "user-provided" is only defined for special member
487 // functions, but the intent of the standard is clearly that it should apply
488 // to all functions.
489 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000490
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000491 if (Constructor->isDefaultConstructor()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000492 SMKind |= SMF_DefaultConstructor;
493
494 if (UserProvided)
Sean Huntcdee3fe2011-05-11 22:34:38 +0000495 data().UserProvidedDefaultConstructor = true;
Richard Smithacf796b2012-11-28 06:23:12 +0000496 if (Constructor->isConstexpr())
Richard Smith61802452011-12-22 02:22:31 +0000497 data().HasConstexprDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000498 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000499
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000500 if (!FunTmpl) {
Richard Smithacf796b2012-11-28 06:23:12 +0000501 unsigned Quals;
502 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000503 SMKind |= SMF_CopyConstructor;
Richard Smithacf796b2012-11-28 06:23:12 +0000504
505 if (Quals & Qualifiers::Const)
506 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith7d04d3a2012-11-30 05:11:39 +0000507 } else if (Constructor->isMoveConstructor())
508 SMKind |= SMF_MoveConstructor;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000509 }
Richard Smithacf796b2012-11-28 06:23:12 +0000510
511 // Record if we see any constexpr constructors which are neither copy
512 // nor move constructors.
513 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith6b8bc072011-08-10 18:11:37 +0000514 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000515
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000516 // C++ [dcl.init.aggr]p1:
517 // An aggregate is an array or a class with no user-declared
518 // constructors [...].
Richard Smithc3bf52c2013-04-20 22:23:05 +0000519 // C++11 [dcl.init.aggr]p1:
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000520 // An aggregate is an array or a class with no user-provided
521 // constructors [...].
Richard Smith80ad52f2013-01-02 11:42:31 +0000522 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smithacf796b2012-11-28 06:23:12 +0000523 ? UserProvided : !Constructor->isImplicit())
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000524 data().Aggregate = false;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000525 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000526
Richard Smithacf796b2012-11-28 06:23:12 +0000527 // Handle destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000528 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000529 SMKind |= SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000530
Richard Smith7d04d3a2012-11-30 05:11:39 +0000531 if (!DD->isImplicit())
Richard Smithacf796b2012-11-28 06:23:12 +0000532 data().HasIrrelevantDestructor = false;
533
Richard Smithacf796b2012-11-28 06:23:12 +0000534 // C++11 [class.dtor]p5:
Richard Smith7d04d3a2012-11-30 05:11:39 +0000535 // A destructor is trivial if [...] the destructor is not virtual.
536 if (DD->isVirtual())
537 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000538 }
Richard Smithacf796b2012-11-28 06:23:12 +0000539
540 // Handle member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000541 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000542 if (Method->isCopyAssignmentOperator()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000543 SMKind |= SMF_CopyAssignment;
Richard Smithacf796b2012-11-28 06:23:12 +0000544
545 const ReferenceType *ParamTy =
546 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
547 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
548 data().HasDeclaredCopyAssignmentWithConstParam = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000549 }
Sean Huntffe37fd2011-05-25 20:50:04 +0000550
Richard Smith7d04d3a2012-11-30 05:11:39 +0000551 if (Method->isMoveAssignmentOperator())
552 SMKind |= SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000553
Douglas Gregore80622f2010-09-29 04:25:11 +0000554 // Keep the list of conversion functions up-to-date.
555 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor109f5fc2013-04-08 17:12:58 +0000556 // FIXME: We use the 'unsafe' accessor for the access specifier here,
557 // because Sema may not have set it yet. That's really just a misdesign
558 // in Sema. However, LLDB *will* have set the access specifier correctly,
559 // and adds declarations after the class is technically completed,
560 // so completeDefinition()'s overriding of the access specifiers doesn't
561 // work.
562 AccessSpecifier AS = Conversion->getAccessUnsafe();
563
Richard Smith7d04d3a2012-11-30 05:11:39 +0000564 if (Conversion->getPrimaryTemplate()) {
565 // We don't record specializations.
Douglas Gregore80622f2010-09-29 04:25:11 +0000566 } else {
Richard Smithc2d77572013-08-30 04:46:40 +0000567 ASTContext &Ctx = getASTContext();
568 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
569 NamedDecl *Primary =
570 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
571 if (Primary->getPreviousDecl())
572 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
573 Primary, AS);
Douglas Gregore80622f2010-09-29 04:25:11 +0000574 else
Richard Smithc2d77572013-08-30 04:46:40 +0000575 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregore80622f2010-09-29 04:25:11 +0000576 }
577 }
Richard Smithacf796b2012-11-28 06:23:12 +0000578
Richard Smith7d04d3a2012-11-30 05:11:39 +0000579 if (SMKind) {
Richard Smithac713512012-12-08 02:53:02 +0000580 // If this is the first declaration of a special member, we no longer have
581 // an implicit trivial special member.
582 data().HasTrivialSpecialMembers &=
583 data().DeclaredSpecialMembers | ~SMKind;
584
585 if (!Method->isImplicit() && !Method->isUserProvided()) {
586 // This method is user-declared but not user-provided. We can't work out
587 // whether it's trivial yet (not until we get to the end of the class).
588 // We'll handle this method in finishedDefaultedOrDeletedMember.
589 } else if (Method->isTrivial())
590 data().HasTrivialSpecialMembers |= SMKind;
591 else
592 data().DeclaredNonTrivialSpecialMembers |= SMKind;
593
Richard Smith7d04d3a2012-11-30 05:11:39 +0000594 // Note when we have declared a declared special member, and suppress the
595 // implicit declaration of this special member.
596 data().DeclaredSpecialMembers |= SMKind;
597
598 if (!Method->isImplicit()) {
599 data().UserDeclaredSpecialMembers |= SMKind;
600
601 // C++03 [class]p4:
602 // A POD-struct is an aggregate class that has [...] no user-defined
603 // copy assignment operator and no user-defined destructor.
604 //
605 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
606 // aggregates could not have any constructors, clear it even for an
607 // explicitly defaulted or deleted constructor.
608 // type is technically an aggregate in C++0x since it wouldn't be in 03.
609 //
610 // Also, a user-declared move assignment operator makes a class non-POD.
611 // This is an extension in C++03.
612 data().PlainOldData = false;
613 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000614 }
615
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000616 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000617 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000618
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000619 // Handle non-static data members.
620 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000621 // C++ [class.bit]p2:
622 // A declaration for a bit-field that omits the identifier declares an
623 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
624 // initialized.
625 if (Field->isUnnamedBitfield())
626 return;
627
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000628 // C++ [dcl.init.aggr]p1:
629 // An aggregate is an array or a class (clause 9) with [...] no
630 // private or protected non-static data members (clause 11).
631 //
632 // A POD must be an aggregate.
633 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
634 data().Aggregate = false;
635 data().PlainOldData = false;
636 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000637
638 // C++0x [class]p7:
639 // A standard-layout class is a class that:
640 // [...]
641 // -- has the same access control for all non-static data members,
642 switch (D->getAccess()) {
643 case AS_private: data().HasPrivateFields = true; break;
644 case AS_protected: data().HasProtectedFields = true; break;
645 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000646 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000647 };
648 if ((data().HasPrivateFields + data().HasProtectedFields +
649 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000650 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000651
Douglas Gregor2bb11012011-05-13 01:05:07 +0000652 // Keep track of the presence of mutable fields.
653 if (Field->isMutable())
654 data().HasMutableFields = true;
655
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000656 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000657 // A POD struct is a class that is both a trivial class and a
658 // standard-layout class, and has no non-static data members of type
659 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000660 //
661 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
662 // that does not explicitly have no lifetime makes the class a non-POD.
663 // However, we delay setting PlainOldData to false in this case so that
664 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000665 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000666 // In this case, the class will become a non-POD class when we complete
667 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000668 ASTContext &Context = getASTContext();
669 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000670 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000671 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000672 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
673 setHasObjectMember(true);
Eli Friedman65400522013-07-20 01:06:31 +0000674 } else if (!T.isCXX98PODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000675 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000676
Chandler Carrutha8225442011-04-30 09:17:45 +0000677 if (T->isReferenceType()) {
Richard Smithd5bc8672012-12-08 02:01:17 +0000678 if (!Field->hasInClassInitializer())
679 data().HasUninitializedReferenceMember = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000680
Chandler Carrutha8225442011-04-30 09:17:45 +0000681 // C++0x [class]p7:
682 // A standard-layout class is a class that:
683 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000684 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000685 }
686
Richard Smith86c3ae42012-02-13 03:54:03 +0000687 // Record if this field is the first non-literal or volatile field or base.
Richard Smitha10b9782013-04-22 15:31:51 +0000688 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000689 data().HasNonLiteralTypeFieldsOrBases = true;
690
Richard Smith7a614d82011-06-11 17:19:42 +0000691 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000692 data().HasInClassInitializer = true;
693
694 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000695 // A default constructor is trivial if [...] no non-static data member
696 // of its class has a brace-or-equal-initializer.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000697 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith7a614d82011-06-11 17:19:42 +0000698
Richard Smithd079abf2012-05-07 01:07:30 +0000699 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000700 // An aggregate is a [...] class with [...] no
701 // brace-or-equal-initializers for non-static data members.
Richard Smithc3bf52c2013-04-20 22:23:05 +0000702 //
703 // This rule was removed in C++1y.
704 if (!getASTContext().getLangOpts().CPlusPlus1y)
705 data().Aggregate = false;
Richard Smith7a614d82011-06-11 17:19:42 +0000706
Richard Smithd079abf2012-05-07 01:07:30 +0000707 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000708 // A POD struct is [...] a trivial class.
709 data().PlainOldData = false;
710 }
711
Richard Smithbc2a35d2012-12-08 08:32:28 +0000712 // C++11 [class.copy]p23:
713 // A defaulted copy/move assignment operator for a class X is defined
714 // as deleted if X has:
715 // -- a non-static data member of reference type
716 if (T->isReferenceType())
717 data().DefaultedMoveAssignmentIsDeleted = true;
718
Douglas Gregor85606eb2010-09-28 20:50:54 +0000719 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
720 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
721 if (FieldRec->getDefinition()) {
Richard Smithbc2a35d2012-12-08 08:32:28 +0000722 addedClassSubobject(FieldRec);
723
724 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
725 // A defaulted [special member] for a class X is defined as
726 // deleted if:
727 // -- X is a union-like class that has a variant member with a
728 // non-trivial [corresponding special member]
729 if (isUnion()) {
730 if (FieldRec->hasNonTrivialMoveConstructor())
731 data().DefaultedMoveConstructorIsDeleted = true;
732 if (FieldRec->hasNonTrivialMoveAssignment())
733 data().DefaultedMoveAssignmentIsDeleted = true;
734 if (FieldRec->hasNonTrivialDestructor())
735 data().DefaultedDestructorIsDeleted = true;
736 }
737
Sean Hunt023df372011-05-09 18:22:59 +0000738 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000739 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000740 // -- for all the non-static data members of its class that are of
741 // class type (or array thereof), each such class has a trivial
742 // default constructor.
743 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000744 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000745
746 // C++0x [class.copy]p13:
747 // A copy/move constructor for class X is trivial if [...]
748 // [...]
749 // -- for each non-static data member of X that is of class type (or
750 // an array thereof), the constructor selected to copy/move that
751 // member is trivial;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000752 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000753 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000754 // If the field doesn't have a simple move constructor, we'll eagerly
755 // declare the move constructor for this class and we'll decide whether
756 // it's trivial then.
Richard Smith426391c2012-11-16 00:53:38 +0000757 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000758 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000759
760 // C++0x [class.copy]p27:
761 // A copy/move assignment operator for class X is trivial if [...]
762 // [...]
763 // -- for each non-static data member of X that is of class type (or
764 // an array thereof), the assignment operator selected to
765 // copy/move that member is trivial;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000766 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000767 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000768 // If the field doesn't have a simple move assignment, we'll eagerly
769 // declare the move assignment for this class and we'll decide whether
770 // it's trivial then.
Richard Smith426391c2012-11-16 00:53:38 +0000771 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000772 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000773
Douglas Gregor85606eb2010-09-28 20:50:54 +0000774 if (!FieldRec->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000775 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000776 if (!FieldRec->hasIrrelevantDestructor())
777 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000778 if (FieldRec->hasObjectMember())
779 setHasObjectMember(true);
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +0000780 if (FieldRec->hasVolatileMember())
781 setHasVolatileMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000782
783 // C++0x [class]p7:
784 // A standard-layout class is a class that:
785 // -- has no non-static data members of type non-standard-layout
786 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000787 if (!FieldRec->isStandardLayout())
788 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000789
790 // C++0x [class]p7:
791 // A standard-layout class is a class that:
792 // [...]
793 // -- has no base classes of the same type as the first non-static
794 // data member.
795 // We don't want to expend bits in the state of the record decl
796 // tracking whether this is the first non-static data member so we
797 // cheat a bit and use some of the existing state: the empty bit.
798 // Virtual bases and virtual methods make a class non-empty, but they
799 // also make it non-standard-layout so we needn't check here.
800 // A non-empty base class may leave the class standard-layout, but not
801 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000802 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000803 // data member must come through here with Empty still true, and Empty
804 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000805 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000806 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
807 BE = bases_end();
808 BI != BE; ++BI) {
809 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000810 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000811 break;
812 }
813 }
814 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000815
816 // Keep track of the presence of mutable fields.
817 if (FieldRec->hasMutableFields())
818 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000819
820 // C++11 [class.copy]p13:
821 // If the implicitly-defined constructor would satisfy the
822 // requirements of a constexpr constructor, the implicitly-defined
823 // constructor is constexpr.
824 // C++11 [dcl.constexpr]p4:
825 // -- every constructor involved in initializing non-static data
826 // members [...] shall be a constexpr constructor
827 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000828 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000829 // The standard requires any in-class initializer to be a constant
830 // expression. We consider this to be a defect.
831 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithacf796b2012-11-28 06:23:12 +0000832
833 // C++11 [class.copy]p8:
834 // The implicitly-declared copy constructor for a class X will have
835 // the form 'X::X(const X&)' if [...] for all the non-static data
836 // members of X that are of a class type M (or array thereof), each
837 // such class type has a copy constructor whose first parameter is
838 // of type 'const M&' or 'const volatile M&'.
839 if (!FieldRec->hasCopyConstructorWithConstParam())
840 data().ImplicitCopyConstructorHasConstParam = false;
841
842 // C++11 [class.copy]p18:
843 // The implicitly-declared copy assignment oeprator for a class X will
844 // have the form 'X& X::operator=(const X&)' if [...] for all the
845 // non-static data members of X that are of a class type M (or array
846 // thereof), each such class type has a copy assignment operator whose
847 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
848 if (!FieldRec->hasCopyAssignmentWithConstParam())
849 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smithd5bc8672012-12-08 02:01:17 +0000850
851 if (FieldRec->hasUninitializedReferenceMember() &&
852 !Field->hasInClassInitializer())
853 data().HasUninitializedReferenceMember = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000854 }
Richard Smith61802452011-12-22 02:22:31 +0000855 } else {
856 // Base element type of field is a non-class type.
Richard Smitha10b9782013-04-22 15:31:51 +0000857 if (!T->isLiteralType(Context) ||
Richard Smithd3861ce2012-06-10 07:07:24 +0000858 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000859 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000860
861 // C++11 [class.copy]p23:
862 // A defaulted copy/move assignment operator for a class X is defined
863 // as deleted if X has:
864 // -- a non-static data member of const non-class type (or array
865 // thereof)
866 if (T.isConstQualified())
867 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000868 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000869
870 // C++0x [class]p7:
871 // A standard-layout class is a class that:
872 // [...]
873 // -- either has no non-static data members in the most derived
874 // class and at most one base class with non-static data members,
875 // or has no base classes with non-static data members, and
876 // At this point we know that we have a non-static data member, so the last
877 // clause holds.
878 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000879 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000880
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000881 // If this is not a zero-length bit-field, then the class is not empty.
882 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000883 if (!Field->isBitField() ||
884 (!Field->getBitWidth()->isTypeDependent() &&
885 !Field->getBitWidth()->isValueDependent() &&
886 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000887 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000888 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000889 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000890
891 // Handle using declarations of conversion functions.
Richard Smithc2d77572013-08-30 04:46:40 +0000892 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregore80622f2010-09-29 04:25:11 +0000893 if (Shadow->getDeclName().getNameKind()
Richard Smithc2d77572013-08-30 04:46:40 +0000894 == DeclarationName::CXXConversionFunctionName) {
895 ASTContext &Ctx = getASTContext();
896 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
897 }
898 }
Joao Matos17d35c32012-08-31 22:18:20 +0000899}
900
Richard Smithac713512012-12-08 02:53:02 +0000901void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
902 assert(!D->isImplicit() && !D->isUserProvided());
903
904 // The kind of special member this declaration is, if any.
905 unsigned SMKind = 0;
906
907 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
908 if (Constructor->isDefaultConstructor()) {
909 SMKind |= SMF_DefaultConstructor;
910 if (Constructor->isConstexpr())
911 data().HasConstexprDefaultConstructor = true;
912 }
913 if (Constructor->isCopyConstructor())
914 SMKind |= SMF_CopyConstructor;
915 else if (Constructor->isMoveConstructor())
916 SMKind |= SMF_MoveConstructor;
917 else if (Constructor->isConstexpr())
918 // We may now know that the constructor is constexpr.
919 data().HasConstexprNonCopyMoveConstructor = true;
920 } else if (isa<CXXDestructorDecl>(D))
921 SMKind |= SMF_Destructor;
922 else if (D->isCopyAssignmentOperator())
923 SMKind |= SMF_CopyAssignment;
924 else if (D->isMoveAssignmentOperator())
925 SMKind |= SMF_MoveAssignment;
926
927 // Update which trivial / non-trivial special members we have.
928 // addedMember will have skipped this step for this member.
929 if (D->isTrivial())
930 data().HasTrivialSpecialMembers |= SMKind;
931 else
932 data().DeclaredNonTrivialSpecialMembers |= SMKind;
933}
934
Joao Matos17d35c32012-08-31 22:18:20 +0000935bool CXXRecordDecl::isCLike() const {
936 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
937 !TemplateOrInstantiation.isNull())
938 return false;
939 if (!hasDefinition())
940 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000941
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000942 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000943}
944
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000945void CXXRecordDecl::getCaptureFields(
946 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000947 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000948 Captures.clear();
949 ThisCapture = 0;
950
Douglas Gregorda8962a2012-02-13 15:44:47 +0000951 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000952 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000953 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000954 C != CEnd; ++C, ++Field) {
Richard Smith0d8e9642013-05-16 06:20:58 +0000955 if (C->capturesThis())
David Blaikie581deb32012-06-06 20:45:41 +0000956 ThisCapture = *Field;
Richard Smith0d8e9642013-05-16 06:20:58 +0000957 else if (C->capturesVariable())
958 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000959 }
960}
961
962
John McCallb05b5f32010-03-15 09:07:48 +0000963static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
964 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000965 if (isa<UsingShadowDecl>(Conv))
966 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000967 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
968 T = ConvTemp->getTemplatedDecl()->getResultType();
969 else
970 T = cast<CXXConversionDecl>(Conv)->getConversionType();
971 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000972}
973
John McCallb05b5f32010-03-15 09:07:48 +0000974/// Collect the visible conversions of a base class.
975///
James Dennetta1253502012-06-15 22:28:09 +0000976/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +0000977/// \param InVirtual whether this base class is a virtual base (or a base
978/// of a virtual base)
979/// \param Access the access along the inheritance path to this base
980/// \param ParentHiddenTypes the conversions provided by the inheritors
981/// of this base
982/// \param Output the set to which to add conversions from non-virtual bases
983/// \param VOutput the set to which to add conversions from virtual bases
984/// \param HiddenVBaseCs the set of conversions which were hidden in a
985/// virtual base along some inheritance path
986static void CollectVisibleConversions(ASTContext &Context,
987 CXXRecordDecl *Record,
988 bool InVirtual,
989 AccessSpecifier Access,
990 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000991 ASTUnresolvedSet &Output,
John McCallb05b5f32010-03-15 09:07:48 +0000992 UnresolvedSetImpl &VOutput,
993 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
994 // The set of types which have conversions in this class or its
995 // subclasses. As an optimization, we don't copy the derived set
996 // unless it might change.
997 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
998 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
999
1000 // Collect the direct conversions and figure out which conversions
1001 // will be hidden in the subclasses.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001002 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1003 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1004 if (ConvI != ConvE) {
John McCallb05b5f32010-03-15 09:07:48 +00001005 HiddenTypesBuffer = ParentHiddenTypes;
1006 HiddenTypes = &HiddenTypesBuffer;
1007
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001008 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +00001009 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1010 bool Hidden = ParentHiddenTypes.count(ConvType);
1011 if (!Hidden)
1012 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001013
1014 // If this conversion is hidden and we're in a virtual base,
1015 // remember that it's hidden along some inheritance path.
1016 if (Hidden && InVirtual)
1017 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1018
1019 // If this conversion isn't hidden, add it to the appropriate output.
1020 else if (!Hidden) {
1021 AccessSpecifier IAccess
1022 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1023
1024 if (InVirtual)
1025 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001026 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001027 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001028 }
1029 }
1030 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001031
John McCallb05b5f32010-03-15 09:07:48 +00001032 // Collect information recursively from any base classes.
1033 for (CXXRecordDecl::base_class_iterator
1034 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1035 const RecordType *RT = I->getType()->getAs<RecordType>();
1036 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001037
John McCallb05b5f32010-03-15 09:07:48 +00001038 AccessSpecifier BaseAccess
1039 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1040 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001041
John McCallb05b5f32010-03-15 09:07:48 +00001042 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1043 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1044 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001045 }
John McCallb05b5f32010-03-15 09:07:48 +00001046}
Sebastian Redl9994a342009-10-25 17:03:50 +00001047
John McCallb05b5f32010-03-15 09:07:48 +00001048/// Collect the visible conversions of a class.
1049///
1050/// This would be extremely straightforward if it weren't for virtual
1051/// bases. It might be worth special-casing that, really.
1052static void CollectVisibleConversions(ASTContext &Context,
1053 CXXRecordDecl *Record,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001054 ASTUnresolvedSet &Output) {
John McCallb05b5f32010-03-15 09:07:48 +00001055 // The collection of all conversions in virtual bases that we've
1056 // found. These will be added to the output as long as they don't
1057 // appear in the hidden-conversions set.
1058 UnresolvedSet<8> VBaseCs;
1059
1060 // The set of conversions in virtual bases that we've determined to
1061 // be hidden.
1062 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1063
1064 // The set of types hidden by classes derived from this one.
1065 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1066
1067 // Go ahead and collect the direct conversions and add them to the
1068 // hidden-types set.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001069 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1070 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001071 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001072 for (; ConvI != ConvE; ++ConvI)
1073 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCallb05b5f32010-03-15 09:07:48 +00001074
1075 // Recursively collect conversions from base classes.
1076 for (CXXRecordDecl::base_class_iterator
1077 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1078 const RecordType *RT = I->getType()->getAs<RecordType>();
1079 if (!RT) continue;
1080
1081 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1082 I->isVirtual(), I->getAccessSpecifier(),
1083 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1084 }
1085
1086 // Add any unhidden conversions provided by virtual bases.
1087 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1088 I != E; ++I) {
1089 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001090 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001091 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001092}
1093
1094/// getVisibleConversionFunctions - get all conversion functions visible
1095/// in current class; including conversion function templates.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001096std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1097CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smithc2d77572013-08-30 04:46:40 +00001098 ASTContext &Ctx = getASTContext();
1099
1100 ASTUnresolvedSet *Set;
1101 if (bases_begin() == bases_end()) {
1102 // If root class, all conversions are visible.
1103 Set = &data().Conversions.get(Ctx);
1104 } else {
1105 Set = &data().VisibleConversions.get(Ctx);
1106 // If visible conversion list is not evaluated, evaluate it.
1107 if (!data().ComputedVisibleConversions) {
1108 CollectVisibleConversions(Ctx, this, *Set);
1109 data().ComputedVisibleConversions = true;
1110 }
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001111 }
Richard Smithc2d77572013-08-30 04:46:40 +00001112 return std::make_pair(Set->begin(), Set->end());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001113}
1114
John McCall32daa422010-03-31 01:36:47 +00001115void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1116 // This operation is O(N) but extremely rare. Sema only uses it to
1117 // remove UsingShadowDecls in a class that were followed by a direct
1118 // declaration, e.g.:
1119 // class A : B {
1120 // using B::operator int;
1121 // operator int();
1122 // };
1123 // This is uncommon by itself and even more uncommon in conjunction
1124 // with sufficiently large numbers of directly-declared conversions
1125 // that asymptotic behavior matters.
1126
Richard Smithc2d77572013-08-30 04:46:40 +00001127 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCall32daa422010-03-31 01:36:47 +00001128 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1129 if (Convs[I].getDecl() == ConvDecl) {
1130 Convs.erase(I);
1131 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1132 && "conversion was found multiple times in unresolved set");
1133 return;
1134 }
1135 }
1136
1137 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001138}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001139
Douglas Gregorf6b11852009-10-08 15:14:33 +00001140CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001141 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001142 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1143
1144 return 0;
1145}
1146
1147void
1148CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1149 TemplateSpecializationKind TSK) {
1150 assert(TemplateOrInstantiation.isNull() &&
1151 "Previous template or instantiation?");
1152 assert(!isa<ClassTemplateSpecializationDecl>(this));
1153 TemplateOrInstantiation
1154 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1155}
1156
Anders Carlssonb13e3572009-12-07 06:33:48 +00001157TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1158 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001159 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1160 return Spec->getSpecializationKind();
1161
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001162 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001163 return MSInfo->getTemplateSpecializationKind();
1164
1165 return TSK_Undeclared;
1166}
1167
1168void
1169CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1170 if (ClassTemplateSpecializationDecl *Spec
1171 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1172 Spec->setSpecializationKind(TSK);
1173 return;
1174 }
1175
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001176 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001177 MSInfo->setTemplateSpecializationKind(TSK);
1178 return;
1179 }
1180
David Blaikieb219cfc2011-09-23 05:06:16 +00001181 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001182}
1183
Douglas Gregor1d110e02010-07-01 14:13:13 +00001184CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1185 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001186 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001187
1188 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001189 = Context.DeclarationNames.getCXXDestructorName(
1190 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001191
David Blaikie3bc93e32012-12-19 00:45:41 +00001192 DeclContext::lookup_const_result R = lookup(Name);
1193 if (R.empty())
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001194 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001195
David Blaikie3bc93e32012-12-19 00:45:41 +00001196 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson7267c162009-05-29 21:03:38 +00001197 return Dtor;
1198}
1199
Douglas Gregorda2142f2011-02-19 18:51:44 +00001200void CXXRecordDecl::completeDefinition() {
1201 completeDefinition(0);
1202}
1203
1204void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1205 RecordDecl::completeDefinition();
1206
David Blaikie4e4d0842012-03-11 07:00:24 +00001207 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001208 // Objective-C Automatic Reference Counting:
1209 // If a class has a non-static data member of Objective-C pointer
1210 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001211 // default constructor (if any), copy constructor, move constructor,
1212 // copy assignment operator, move assignment operator, and destructor are
1213 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001214 struct DefinitionData &Data = data();
1215 Data.PlainOldData = false;
Richard Smith7d04d3a2012-11-30 05:11:39 +00001216 Data.HasTrivialSpecialMembers = 0;
Richard Smithdfefb842012-02-25 07:33:38 +00001217 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001218 }
1219
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001220 // If the class may be abstract (but hasn't been marked as such), check for
1221 // any pure final overriders.
1222 if (mayBeAbstract()) {
1223 CXXFinalOverriderMap MyFinalOverriders;
1224 if (!FinalOverriders) {
1225 getFinalOverriders(MyFinalOverriders);
1226 FinalOverriders = &MyFinalOverriders;
1227 }
1228
1229 bool Done = false;
1230 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1231 MEnd = FinalOverriders->end();
1232 M != MEnd && !Done; ++M) {
1233 for (OverridingMethods::iterator SO = M->second.begin(),
1234 SOEnd = M->second.end();
1235 SO != SOEnd && !Done; ++SO) {
1236 assert(SO->second.size() > 0 &&
1237 "All virtual functions have overridding virtual functions");
1238
1239 // C++ [class.abstract]p4:
1240 // A class is abstract if it contains or inherits at least one
1241 // pure virtual function for which the final overrider is pure
1242 // virtual.
1243 if (SO->second.front().Method->isPure()) {
1244 data().Abstract = true;
1245 Done = true;
1246 break;
1247 }
1248 }
1249 }
1250 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001251
1252 // Set access bits correctly on the directly-declared conversions.
Richard Smithc2d77572013-08-30 04:46:40 +00001253 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregore80622f2010-09-29 04:25:11 +00001254 I != E; ++I)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001255 I.setAccess((*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001256}
1257
1258bool CXXRecordDecl::mayBeAbstract() const {
1259 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1260 isDependentContext())
1261 return false;
1262
1263 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1264 BEnd = bases_end();
1265 B != BEnd; ++B) {
1266 CXXRecordDecl *BaseDecl
1267 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1268 if (BaseDecl->isAbstract())
1269 return true;
1270 }
1271
1272 return false;
1273}
1274
David Blaikie99ba9e32011-12-20 02:48:34 +00001275void CXXMethodDecl::anchor() { }
1276
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001277bool CXXMethodDecl::isStatic() const {
Rafael Espindola72fdc892013-04-15 12:38:20 +00001278 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001279
1280 if (MD->getStorageClass() == SC_Static)
1281 return true;
1282
1283 DeclarationName Name = getDeclName();
1284 // [class.free]p1:
1285 // Any allocation function for a class T is a static member
1286 // (even if not explicitly declared static).
1287 if (Name.getCXXOverloadedOperator() == OO_New ||
1288 Name.getCXXOverloadedOperator() == OO_Array_New)
1289 return true;
1290
1291 // [class.free]p6 Any deallocation function for a class X is a static member
1292 // (even if not explicitly declared static).
1293 if (Name.getCXXOverloadedOperator() == OO_Delete ||
1294 Name.getCXXOverloadedOperator() == OO_Array_Delete)
1295 return true;
1296
1297 return false;
1298}
1299
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001300static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1301 const CXXMethodDecl *BaseMD) {
1302 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1303 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1304 const CXXMethodDecl *MD = *I;
1305 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1306 return true;
1307 if (recursivelyOverrides(MD, BaseMD))
1308 return true;
1309 }
1310 return false;
1311}
1312
1313CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001314CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1315 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001316 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1317 return this;
1318
1319 // Lookup doesn't work for destructors, so handle them separately.
1320 if (isa<CXXDestructorDecl>(this)) {
1321 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001322 if (MD) {
1323 if (recursivelyOverrides(MD, this))
1324 return MD;
1325 if (MayBeBase && recursivelyOverrides(this, MD))
1326 return MD;
1327 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001328 return NULL;
1329 }
1330
1331 lookup_const_result Candidates = RD->lookup(getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +00001332 for (NamedDecl * const * I = Candidates.begin(); I != Candidates.end(); ++I) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001333 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1334 if (!MD)
1335 continue;
1336 if (recursivelyOverrides(MD, this))
1337 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001338 if (MayBeBase && recursivelyOverrides(this, MD))
1339 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001340 }
1341
1342 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1343 E = RD->bases_end(); I != E; ++I) {
1344 const RecordType *RT = I->getType()->getAs<RecordType>();
1345 if (!RT)
1346 continue;
1347 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1348 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1349 if (T)
1350 return T;
1351 }
1352
1353 return NULL;
1354}
1355
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001356CXXMethodDecl *
1357CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001358 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001359 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001360 QualType T, TypeSourceInfo *TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001361 StorageClass SC, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001362 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001363 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001364 SC, isInline, isConstexpr,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001365 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001366}
1367
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001368CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1369 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1370 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1371 DeclarationNameInfo(), QualType(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001372 0, SC_None, false, false,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001373 SourceLocation());
1374}
1375
Douglas Gregor90916562009-09-29 18:16:17 +00001376bool CXXMethodDecl::isUsualDeallocationFunction() const {
1377 if (getOverloadedOperator() != OO_Delete &&
1378 getOverloadedOperator() != OO_Array_Delete)
1379 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001380
1381 // C++ [basic.stc.dynamic.deallocation]p2:
1382 // A template instance is never a usual deallocation function,
1383 // regardless of its signature.
1384 if (getPrimaryTemplate())
1385 return false;
1386
Douglas Gregor90916562009-09-29 18:16:17 +00001387 // C++ [basic.stc.dynamic.deallocation]p2:
1388 // If a class T has a member deallocation function named operator delete
1389 // with exactly one parameter, then that function is a usual (non-placement)
1390 // deallocation function. [...]
1391 if (getNumParams() == 1)
1392 return true;
1393
1394 // C++ [basic.stc.dynamic.deallocation]p2:
1395 // [...] If class T does not declare such an operator delete but does
1396 // declare a member deallocation function named operator delete with
1397 // exactly two parameters, the second of which has type std::size_t (18.1),
1398 // then this function is a usual deallocation function.
1399 ASTContext &Context = getASTContext();
1400 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001401 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1402 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001403 return false;
1404
1405 // This function is a usual deallocation function if there are no
1406 // single-parameter deallocation functions of the same kind.
David Blaikie3bc93e32012-12-19 00:45:41 +00001407 DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1408 for (DeclContext::lookup_const_result::iterator I = R.begin(), E = R.end();
1409 I != E; ++I) {
1410 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregor90916562009-09-29 18:16:17 +00001411 if (FD->getNumParams() == 1)
1412 return false;
1413 }
1414
1415 return true;
1416}
1417
Douglas Gregor06a9f362010-05-01 20:49:11 +00001418bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001419 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001420 // A user-declared copy assignment operator X::operator= is a non-static
1421 // non-template member function of class X with exactly one parameter of
1422 // type X, X&, const X&, volatile X& or const volatile X&.
1423 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1424 /*non-static*/ isStatic() ||
Eli Friedmanfcb5a252013-07-11 23:55:07 +00001425 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1426 getNumParams() != 1)
Douglas Gregor06a9f362010-05-01 20:49:11 +00001427 return false;
1428
1429 QualType ParamType = getParamDecl(0)->getType();
1430 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1431 ParamType = Ref->getPointeeType();
1432
1433 ASTContext &Context = getASTContext();
1434 QualType ClassType
1435 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1436 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1437}
1438
Sean Huntffe37fd2011-05-25 20:50:04 +00001439bool CXXMethodDecl::isMoveAssignmentOperator() const {
1440 // C++0x [class.copy]p19:
1441 // A user-declared move assignment operator X::operator= is a non-static
1442 // non-template member function of class X with exactly one parameter of type
1443 // X&&, const X&&, volatile X&&, or const volatile X&&.
1444 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedmanfcb5a252013-07-11 23:55:07 +00001445 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1446 getNumParams() != 1)
Sean Huntffe37fd2011-05-25 20:50:04 +00001447 return false;
1448
1449 QualType ParamType = getParamDecl(0)->getType();
1450 if (!isa<RValueReferenceType>(ParamType))
1451 return false;
1452 ParamType = ParamType->getPointeeType();
1453
1454 ASTContext &Context = getASTContext();
1455 QualType ClassType
1456 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1457 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1458}
1459
Anders Carlsson05eb2442009-05-16 23:58:37 +00001460void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001461 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001462 assert(!MD->getParent()->isDependentContext() &&
1463 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001464 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001465
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001466 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001467}
1468
1469CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001470 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001471 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001472}
1473
1474CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001475 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001476 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001477}
1478
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001479unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001480 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001481 return getASTContext().overridden_methods_size(this);
1482}
1483
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001484QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001485 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1486 // If the member function is declared const, the type of this is const X*,
1487 // if the member function is declared volatile, the type of this is
1488 // volatile X*, and if the member function is declared const volatile,
1489 // the type of this is const volatile X*.
1490
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001491 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001492
John McCall3cb0ebd2010-03-10 03:28:59 +00001493 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001494 ClassTy = C.getQualifiedType(ClassTy,
1495 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001496 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001497}
1498
Eli Friedmand7d7f672009-12-06 20:50:05 +00001499bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001500 // If this function is a template instantiation, look at the template from
1501 // which it was instantiated.
1502 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1503 if (!CheckFn)
1504 CheckFn = this;
1505
Eli Friedmand7d7f672009-12-06 20:50:05 +00001506 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001507 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001508}
1509
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001510bool CXXMethodDecl::isLambdaStaticInvoker() const {
1511 return getParent()->isLambda() &&
Manuel Klimek152b4e42013-08-22 12:12:24 +00001512 getIdentifier() && getIdentifier()->getName() == "__invoke";
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001513}
1514
1515
Sean Huntcbb67482011-01-08 20:30:50 +00001516CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1517 TypeSourceInfo *TInfo, bool IsVirtual,
1518 SourceLocation L, Expr *Init,
1519 SourceLocation R,
1520 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001521 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001522 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1523 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001524{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001525}
1526
Sean Huntcbb67482011-01-08 20:30:50 +00001527CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1528 FieldDecl *Member,
1529 SourceLocation MemberLoc,
1530 SourceLocation L, Expr *Init,
1531 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001532 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001533 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001534 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1535{
1536}
1537
Sean Huntcbb67482011-01-08 20:30:50 +00001538CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1539 IndirectFieldDecl *Member,
1540 SourceLocation MemberLoc,
1541 SourceLocation L, Expr *Init,
1542 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001543 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001544 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001545 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001546{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001547}
1548
Sean Huntcbb67482011-01-08 20:30:50 +00001549CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001550 TypeSourceInfo *TInfo,
1551 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001552 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001553 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1554 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001555 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1556{
1557}
1558
1559CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001560 FieldDecl *Member,
1561 SourceLocation MemberLoc,
1562 SourceLocation L, Expr *Init,
1563 SourceLocation R,
1564 VarDecl **Indices,
1565 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001566 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001567 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001568 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001569{
1570 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1571 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1572}
1573
Sean Huntcbb67482011-01-08 20:30:50 +00001574CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1575 FieldDecl *Member,
1576 SourceLocation MemberLoc,
1577 SourceLocation L, Expr *Init,
1578 SourceLocation R,
1579 VarDecl **Indices,
1580 unsigned NumIndices) {
1581 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001582 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001583 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001584 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1585 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001586}
1587
Sean Huntcbb67482011-01-08 20:30:50 +00001588TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001589 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001590 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001591 else
1592 return TypeLoc();
1593}
1594
Sean Huntcbb67482011-01-08 20:30:50 +00001595const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001596 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001597 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001598 else
1599 return 0;
1600}
1601
Sean Huntcbb67482011-01-08 20:30:50 +00001602SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001603 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001604 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001605
1606 if (isInClassMemberInitializer())
1607 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001608
Douglas Gregor76852c22011-11-01 01:16:03 +00001609 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1610 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1611
1612 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001613}
1614
Sean Huntcbb67482011-01-08 20:30:50 +00001615SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001616 if (isInClassMemberInitializer()) {
1617 FieldDecl *D = getAnyMember();
1618 if (Expr *I = D->getInClassInitializer())
1619 return I->getSourceRange();
1620 return SourceRange();
1621 }
1622
Douglas Gregor802ab452009-12-02 22:36:29 +00001623 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001624}
1625
David Blaikie99ba9e32011-12-20 02:48:34 +00001626void CXXConstructorDecl::anchor() { }
1627
Douglas Gregorb48fe382008-10-31 09:07:45 +00001628CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001629CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1630 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1631 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1632 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001633}
1634
1635CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001636CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001637 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001638 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001639 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001640 bool isExplicit, bool isInline,
1641 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001642 assert(NameInfo.getName().getNameKind()
1643 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001644 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001645 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001646 isExplicit, isInline, isImplicitlyDeclared,
1647 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001648}
1649
Douglas Gregor76852c22011-11-01 01:16:03 +00001650CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1651 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1652 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1653 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1654 return Construct->getConstructor();
1655
1656 return 0;
1657}
1658
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001659bool CXXConstructorDecl::isDefaultConstructor() const {
1660 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001661 // A default constructor for a class X is a constructor of class
1662 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001663 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001664 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001665}
1666
Mike Stump1eb44332009-09-09 15:08:12 +00001667bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001668CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001669 return isCopyOrMoveConstructor(TypeQuals) &&
1670 getParamDecl(0)->getType()->isLValueReferenceType();
1671}
1672
1673bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1674 return isCopyOrMoveConstructor(TypeQuals) &&
1675 getParamDecl(0)->getType()->isRValueReferenceType();
1676}
1677
1678/// \brief Determine whether this is a copy or move constructor.
1679bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001680 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001681 // A non-template constructor for class X is a copy constructor
1682 // if its first parameter is of type X&, const X&, volatile X& or
1683 // const volatile X&, and either there are no other parameters
1684 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001685 // C++0x [class.copy]p3:
1686 // A non-template constructor for class X is a move constructor if its
1687 // first parameter is of type X&&, const X&&, volatile X&&, or
1688 // const volatile X&&, and either there are no other parameters or else
1689 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001690 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001691 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001692 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001693 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001694 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001695
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001696 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001697
1698 // Do we have a reference type?
1699 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001700 if (!ParamRefType)
1701 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001702
Douglas Gregorfd476482009-11-13 23:59:09 +00001703 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001704 ASTContext &Context = getASTContext();
1705
Douglas Gregorfd476482009-11-13 23:59:09 +00001706 CanQualType PointeeType
1707 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001708 CanQualType ClassTy
1709 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001710 if (PointeeType.getUnqualifiedType() != ClassTy)
1711 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001712
John McCall0953e762009-09-24 19:53:00 +00001713 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001714
1715 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001716 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001717 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001718}
1719
Anders Carlssonfaccd722009-08-28 16:57:08 +00001720bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001721 // C++ [class.conv.ctor]p1:
1722 // A constructor declared without the function-specifier explicit
1723 // that can be called with a single parameter specifies a
1724 // conversion from the type of its first parameter to the type of
1725 // its class. Such a constructor is called a converting
1726 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001727 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001728 return false;
1729
Mike Stump1eb44332009-09-09 15:08:12 +00001730 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001731 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001732 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001733 (getNumParams() > 1 &&
1734 (getParamDecl(1)->hasDefaultArg() ||
1735 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001736}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001737
Douglas Gregor6493cc52010-11-08 17:16:59 +00001738bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001739 if ((getNumParams() < 1) ||
1740 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1741 (getPrimaryTemplate() == 0) ||
1742 (getDescribedFunctionTemplate() != 0))
1743 return false;
1744
1745 const ParmVarDecl *Param = getParamDecl(0);
1746
1747 ASTContext &Context = getASTContext();
1748 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1749
Douglas Gregor66724ea2009-11-14 01:20:54 +00001750 // Is it the same as our our class type?
1751 CanQualType ClassTy
1752 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1753 if (ParamType.getUnqualifiedType() != ClassTy)
1754 return false;
1755
1756 return true;
1757}
1758
Sebastian Redlf677ea32011-02-05 19:23:19 +00001759const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1760 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001761 method_iterator It = getASTContext().overridden_methods_begin(this);
1762 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001763 return 0;
1764
1765 return cast<CXXConstructorDecl>(*It);
1766}
1767
1768void
1769CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1770 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001771 assert(getASTContext().overridden_methods_size(this) == 0 &&
1772 "Base ctor already set.");
1773 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001774}
1775
David Blaikie99ba9e32011-12-20 02:48:34 +00001776void CXXDestructorDecl::anchor() { }
1777
Douglas Gregor42a552f2008-11-05 20:51:48 +00001778CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001779CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1780 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1781 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001782 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001783}
1784
1785CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001786CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001787 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001788 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001789 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001790 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001791 assert(NameInfo.getName().getNameKind()
1792 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001793 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001794 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001795 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001796}
1797
David Blaikie99ba9e32011-12-20 02:48:34 +00001798void CXXConversionDecl::anchor() { }
1799
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001800CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001801CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1802 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1803 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1804 QualType(), 0, false, false, false,
1805 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001806}
1807
1808CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001809CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001810 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001811 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001812 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001813 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001814 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001815 assert(NameInfo.getName().getNameKind()
1816 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001817 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001818 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001819 isInline, isExplicit, isConstexpr,
1820 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001821}
1822
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001823bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1824 return isImplicit() && getParent()->isLambda() &&
1825 getConversionType()->isBlockPointerType();
1826}
1827
David Blaikie99ba9e32011-12-20 02:48:34 +00001828void LinkageSpecDecl::anchor() { }
1829
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001830LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001831 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001832 SourceLocation ExternLoc,
1833 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001834 LanguageIDs Lang,
Rafael Espindolae5e575d2013-04-26 01:30:23 +00001835 bool HasBraces) {
1836 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001837}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001838
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001839LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1840 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1841 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
Rafael Espindolae5e575d2013-04-26 01:30:23 +00001842 lang_c, false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001843}
1844
David Blaikie99ba9e32011-12-20 02:48:34 +00001845void UsingDirectiveDecl::anchor() { }
1846
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001847UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1848 SourceLocation L,
1849 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001850 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001851 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001852 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001853 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001854 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1855 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001856 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1857 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001858}
1859
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001860UsingDirectiveDecl *
1861UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1862 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1863 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1864 NestedNameSpecifierLoc(),
1865 SourceLocation(), 0, 0);
1866}
1867
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001868NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1869 if (NamespaceAliasDecl *NA =
1870 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1871 return NA->getNamespace();
1872 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1873}
1874
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001875void NamespaceDecl::anchor() { }
1876
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001877NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1878 SourceLocation StartLoc,
1879 SourceLocation IdLoc, IdentifierInfo *Id,
1880 NamespaceDecl *PrevDecl)
1881 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1882 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1883{
1884 setPreviousDeclaration(PrevDecl);
1885
1886 if (PrevDecl)
1887 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1888}
1889
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001890NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001891 bool Inline, SourceLocation StartLoc,
1892 SourceLocation IdLoc, IdentifierInfo *Id,
1893 NamespaceDecl *PrevDecl) {
1894 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001895}
1896
1897NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1898 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001899 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1900 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001901}
1902
David Blaikie99ba9e32011-12-20 02:48:34 +00001903void NamespaceAliasDecl::anchor() { }
1904
Mike Stump1eb44332009-09-09 15:08:12 +00001905NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001906 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001907 SourceLocation AliasLoc,
1908 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001909 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001910 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001911 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001912 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1913 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001914 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1915 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001916}
1917
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001918NamespaceAliasDecl *
1919NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1920 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1921 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1922 NestedNameSpecifierLoc(),
1923 SourceLocation(), 0);
1924}
1925
David Blaikie99ba9e32011-12-20 02:48:34 +00001926void UsingShadowDecl::anchor() { }
1927
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001928UsingShadowDecl *
1929UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1930 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1931 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1932}
1933
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001934UsingDecl *UsingShadowDecl::getUsingDecl() const {
1935 const UsingShadowDecl *Shadow = this;
1936 while (const UsingShadowDecl *NextShadow =
1937 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1938 Shadow = NextShadow;
1939 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1940}
1941
David Blaikie99ba9e32011-12-20 02:48:34 +00001942void UsingDecl::anchor() { }
1943
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001944void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1945 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1946 "declaration already in set");
1947 assert(S->getUsingDecl() == this);
1948
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001949 if (FirstUsingShadow.getPointer())
1950 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1951 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001952}
1953
1954void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1955 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1956 "declaration not in set");
1957 assert(S->getUsingDecl() == this);
1958
1959 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1960
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001961 if (FirstUsingShadow.getPointer() == S) {
1962 FirstUsingShadow.setPointer(
1963 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001964 S->UsingOrNextShadow = this;
1965 return;
1966 }
1967
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001968 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001969 while (Prev->UsingOrNextShadow != S)
1970 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1971 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1972 S->UsingOrNextShadow = this;
1973}
1974
Douglas Gregordc355712011-02-25 00:36:19 +00001975UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1976 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001977 const DeclarationNameInfo &NameInfo,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00001978 bool HasTypename) {
1979 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001980}
1981
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001982UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1983 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1984 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1985 DeclarationNameInfo(), false);
1986}
1987
Enea Zaffanellad4de59d2013-07-17 17:28:56 +00001988SourceRange UsingDecl::getSourceRange() const {
1989 SourceLocation Begin = isAccessDeclaration()
1990 ? getQualifierLoc().getBeginLoc() : UsingLocation;
1991 return SourceRange(Begin, getNameInfo().getEndLoc());
1992}
1993
David Blaikie99ba9e32011-12-20 02:48:34 +00001994void UnresolvedUsingValueDecl::anchor() { }
1995
John McCall7ba107a2009-11-18 02:36:19 +00001996UnresolvedUsingValueDecl *
1997UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1998 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001999 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002000 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00002001 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002002 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00002003}
2004
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002005UnresolvedUsingValueDecl *
2006UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2007 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
2008 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
2009 NestedNameSpecifierLoc(),
2010 DeclarationNameInfo());
2011}
2012
Enea Zaffanellad4de59d2013-07-17 17:28:56 +00002013SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2014 SourceLocation Begin = isAccessDeclaration()
2015 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2016 return SourceRange(Begin, getNameInfo().getEndLoc());
2017}
2018
David Blaikie99ba9e32011-12-20 02:48:34 +00002019void UnresolvedUsingTypenameDecl::anchor() { }
2020
John McCall7ba107a2009-11-18 02:36:19 +00002021UnresolvedUsingTypenameDecl *
2022UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2023 SourceLocation UsingLoc,
2024 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002025 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002026 SourceLocation TargetNameLoc,
2027 DeclarationName TargetName) {
2028 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002029 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002030 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00002031}
2032
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002033UnresolvedUsingTypenameDecl *
2034UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2035 void *Mem = AllocateDeserializedDecl(C, ID,
2036 sizeof(UnresolvedUsingTypenameDecl));
2037 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2038 SourceLocation(),
2039 NestedNameSpecifierLoc(),
2040 SourceLocation(),
2041 0);
2042}
2043
David Blaikie99ba9e32011-12-20 02:48:34 +00002044void StaticAssertDecl::anchor() { }
2045
Anders Carlssonfb311762009-03-14 00:25:26 +00002046StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002047 SourceLocation StaticAssertLoc,
2048 Expr *AssertExpr,
2049 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002050 SourceLocation RParenLoc,
2051 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002052 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002053 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00002054}
2055
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002056StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2057 unsigned ID) {
2058 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00002059 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
2060 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002061}
2062
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002063static const char *getAccessName(AccessSpecifier AS) {
2064 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002065 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002066 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002067 case AS_public:
2068 return "public";
2069 case AS_private:
2070 return "private";
2071 case AS_protected:
2072 return "protected";
2073 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002074 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002075}
2076
2077const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2078 AccessSpecifier AS) {
2079 return DB << getAccessName(AS);
2080}
Richard Smithf15fda02012-02-02 01:16:57 +00002081
2082const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2083 AccessSpecifier AS) {
2084 return DB << getAccessName(AS);
2085}