blob: b50aa9337fe8d1e1587e899e483db08539cdbda9 [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"
Faisal Valiecb58192013-08-22 01:49:11 +000023#include "clang/Sema/SemaLambda.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000024#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000025#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// Decl Allocation/Deallocation Method Implementations
30//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000031
David Blaikie99ba9e32011-12-20 02:48:34 +000032void AccessSpecDecl::anchor() { }
33
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000034AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
35 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(AccessSpecDecl));
36 return new (Mem) AccessSpecDecl(EmptyShell());
37}
38
John McCall86ff3082010-02-04 22:26:26 +000039CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Richard Smith7d04d3a2012-11-30 05:11:39 +000040 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
Eli Friedman97c134e2009-08-15 22:23:00 +000041 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000042 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000043 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000044 HasMutableFields(false), HasOnlyCMembers(true),
Richard Smithd5bc8672012-12-08 02:01:17 +000045 HasInClassInitializer(false), HasUninitializedReferenceMember(false),
Richard Smithbc2a35d2012-12-08 08:32:28 +000046 NeedOverloadResolutionForMoveConstructor(false),
47 NeedOverloadResolutionForMoveAssignment(false),
48 NeedOverloadResolutionForDestructor(false),
49 DefaultedMoveConstructorIsDeleted(false),
50 DefaultedMoveAssignmentIsDeleted(false),
51 DefaultedDestructorIsDeleted(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000052 HasTrivialSpecialMembers(SMF_All),
Richard Smithac713512012-12-08 02:53:02 +000053 DeclaredNonTrivialSpecialMembers(0),
Richard Smith7d04d3a2012-11-30 05:11:39 +000054 HasIrrelevantDestructor(true),
Richard Smith61802452011-12-22 02:22:31 +000055 HasConstexprNonCopyMoveConstructor(false),
56 DefaultedDefaultConstructorIsConstexpr(true),
Richard Smith7d04d3a2012-11-30 05:11:39 +000057 HasConstexprDefaultConstructor(false),
Sean Hunt023df372011-05-09 18:22:59 +000058 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000059 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smithacf796b2012-11-28 06:23:12 +000060 ImplicitCopyConstructorHasConstParam(true),
61 ImplicitCopyAssignmentHasConstParam(true),
62 HasDeclaredCopyConstructorWithConstParam(false),
63 HasDeclaredCopyAssignmentWithConstParam(false),
64 FailedImplicitMoveConstructor(false), FailedImplicitMoveAssignment(false),
65 IsLambda(false), NumBases(0), NumVBases(0), Bases(), VBases(),
Richard Smith4fc50892013-06-26 02:41:25 +000066 Definition(D), FirstFriend() {
John McCall86ff3082010-02-04 22:26:26 +000067}
68
Benjamin Krameree3096a2012-07-04 17:03:33 +000069CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
70 return Bases.get(Definition->getASTContext().getExternalSource());
71}
72
73CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
74 return VBases.get(Definition->getASTContext().getExternalSource());
75}
76
John McCall86ff3082010-02-04 22:26:26 +000077CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000078 SourceLocation StartLoc, SourceLocation IdLoc,
79 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
80 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000081 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000082 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000083
Jay Foad4ba2a172011-01-12 09:06:06 +000084CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000085 DeclContext *DC, SourceLocation StartLoc,
86 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000087 CXXRecordDecl* PrevDecl,
88 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000089 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
90 Id, PrevDecl);
Douglas Gregor6bd99292013-02-09 01:35:03 +000091 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump1eb44332009-09-09 15:08:12 +000092
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000093 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000094 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000095 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000096 return R;
97}
98
Douglas Gregorda8962a2012-02-13 15:44:47 +000099CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Eli Friedman8da8a662012-09-19 01:18:11 +0000100 TypeSourceInfo *Info, SourceLocation Loc,
101 bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +0000102 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
103 0, 0);
104 R->IsBeingDefined = true;
Eli Friedman8da8a662012-09-19 01:18:11 +0000105 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info, Dependent);
Douglas Gregor6bd99292013-02-09 01:35:03 +0000106 R->MayHaveOutOfDateDef = false;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000107 C.getTypeDeclType(R, /*PrevDecl=*/0);
108 return R;
109}
110
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000111CXXRecordDecl *
112CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
113 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +0000114 CXXRecordDecl *R = new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0,
115 SourceLocation(), SourceLocation(),
116 0, 0);
117 R->MayHaveOutOfDateDef = false;
118 return R;
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000119}
120
Mike Stump1eb44332009-09-09 15:08:12 +0000121void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000122CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000123 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000124 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000125
Douglas Gregor7c789c12010-10-29 22:39:52 +0000126 if (!data().Bases.isOffset() && data().NumBases > 0)
127 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Richard Smithdd677232011-10-18 20:08:55 +0000129 if (NumBases) {
130 // C++ [dcl.init.aggr]p1:
131 // An aggregate is [...] a class with [...] no base classes [...].
132 data().Aggregate = false;
133
134 // C++ [class]p4:
135 // A POD-struct is an aggregate class...
136 data().PlainOldData = false;
137 }
138
Anders Carlsson6f6de732010-03-29 05:13:12 +0000139 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000140 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000141
142 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000143 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000144
John McCall86ff3082010-02-04 22:26:26 +0000145 data().Bases = new(C) CXXBaseSpecifier [NumBases];
146 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000147 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000148 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000149 // Keep track of inherited vbases for this base class.
150 const CXXBaseSpecifier *Base = Bases[i];
151 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000152 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000153 if (BaseType->isDependentType())
154 continue;
155 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000156 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000157
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000158 // A class with a non-empty base class is not empty.
159 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000160 if (!BaseClassDecl->isEmpty()) {
161 if (!data().Empty) {
162 // C++0x [class]p7:
163 // A standard-layout class is a class that:
164 // [...]
165 // -- either has no non-static data members in the most derived
166 // class and at most one base class with non-static data members,
167 // or has no base classes with non-static data members, and
168 // If this is the second non-empty base, then neither of these two
169 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000170 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000171 }
172
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000173 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000174 data().HasNoNonEmptyBases = false;
175 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000176
Douglas Gregor85606eb2010-09-28 20:50:54 +0000177 // C++ [class.virtual]p1:
178 // A class that declares or inherits a virtual function is called a
179 // polymorphic class.
180 if (BaseClassDecl->isPolymorphic())
181 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000182
Chandler Carrutha8225442011-04-30 09:17:45 +0000183 // C++0x [class]p7:
184 // A standard-layout class is a class that: [...]
185 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000186 if (!BaseClassDecl->isStandardLayout())
187 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000188
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000189 // Record if this base is the first non-literal field or base.
Richard Smitha10b9782013-04-22 15:31:51 +0000190 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000191 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000192
Anders Carlsson6f6de732010-03-29 05:13:12 +0000193 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000194 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000195 BaseClassDecl->vbases_begin(),
196 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000197 // Add this base if it's not already in the list.
Richard Smithacf796b2012-11-28 06:23:12 +0000198 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000199 VBases.push_back(VBase);
Richard Smithacf796b2012-11-28 06:23:12 +0000200
201 // C++11 [class.copy]p8:
202 // The implicitly-declared copy constructor for a class X will have
203 // the form 'X::X(const X&)' if each [...] virtual base class B of X
204 // has a copy constructor whose first parameter is of type
205 // 'const B&' or 'const volatile B&' [...]
206 if (CXXRecordDecl *VBaseDecl = VBase->getType()->getAsCXXRecordDecl())
207 if (!VBaseDecl->hasCopyConstructorWithConstParam())
208 data().ImplicitCopyConstructorHasConstParam = false;
209 }
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000210 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000211
212 if (Base->isVirtual()) {
213 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000214 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Richard Smithacf796b2012-11-28 06:23:12 +0000215 VBases.push_back(Base);
216
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000217 // C++0x [meta.unary.prop] is_empty:
218 // T is a class type, but not a union type, with ... no virtual base
219 // classes
220 data().Empty = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000221
Richard Smith7d04d3a2012-11-30 05:11:39 +0000222 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
223 // A [default constructor, copy/move constructor, or copy/move assignment
224 // operator for a class X] is trivial [...] if:
225 // -- class X has [...] no virtual base classes
226 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carrutha8225442011-04-30 09:17:45 +0000227
228 // C++0x [class]p7:
229 // A standard-layout class is a class that: [...]
230 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000231 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000232
233 // C++11 [dcl.constexpr]p4:
234 // In the definition of a constexpr constructor [...]
235 // -- the class shall not have any virtual base classes
236 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000237 } else {
238 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000239 // A default constructor is trivial [...] if:
240 // -- all the direct base classes of its class have trivial default
241 // constructors.
242 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000243 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
244
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000245 // C++0x [class.copy]p13:
246 // A copy/move constructor for class X is trivial if [...]
247 // [...]
248 // -- the constructor selected to copy/move each direct base class
249 // subobject is trivial, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000250 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000251 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000252 // If the base class doesn't have a simple move constructor, we'll eagerly
253 // declare it and perform overload resolution to determine which function
254 // it actually calls. If it does have a simple move constructor, this
255 // check is correct.
Richard Smith426391c2012-11-16 00:53:38 +0000256 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000257 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000258
259 // C++0x [class.copy]p27:
260 // A copy/move assignment operator for class X is trivial if [...]
261 // [...]
262 // -- the assignment operator selected to copy/move each direct base
263 // class subobject is trivial, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000264 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000265 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000266 // If the base class doesn't have a simple move assignment, we'll eagerly
267 // declare it and perform overload resolution to determine which function
268 // it actually calls. If it does have a simple move assignment, this
269 // check is correct.
Richard Smith426391c2012-11-16 00:53:38 +0000270 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000271 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smith61802452011-12-22 02:22:31 +0000272
273 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000274 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000275 // requirements of a constexpr constructor, the implicitly-defined
276 // default constructor is constexpr.
277 if (!BaseClassDecl->hasConstexprDefaultConstructor())
278 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000279 }
Richard Smithac713512012-12-08 02:53:02 +0000280
Douglas Gregor85606eb2010-09-28 20:50:54 +0000281 // C++ [class.ctor]p3:
282 // A destructor is trivial if all the direct base classes of its class
283 // have trivial destructors.
284 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000285 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000286
287 if (!BaseClassDecl->hasIrrelevantDestructor())
288 data().HasIrrelevantDestructor = false;
289
Richard Smithacf796b2012-11-28 06:23:12 +0000290 // C++11 [class.copy]p18:
291 // The implicitly-declared copy assignment oeprator for a class X will
292 // have the form 'X& X::operator=(const X&)' if each direct base class B
293 // of X has a copy assignment operator whose parameter is of type 'const
294 // B&', 'const volatile B&', or 'B' [...]
295 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
296 data().ImplicitCopyAssignmentHasConstParam = false;
297
298 // C++11 [class.copy]p8:
299 // The implicitly-declared copy constructor for a class X will have
300 // the form 'X::X(const X&)' if each direct [...] base class B of X
301 // has a copy constructor whose first parameter is of type
302 // 'const B&' or 'const volatile B&' [...]
303 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
304 data().ImplicitCopyConstructorHasConstParam = false;
305
John McCallf85e1932011-06-15 23:02:42 +0000306 // A class has an Objective-C object member if... or any of its bases
307 // has an Objective-C object member.
308 if (BaseClassDecl->hasObjectMember())
309 setHasObjectMember(true);
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +0000310
311 if (BaseClassDecl->hasVolatileMember())
312 setHasVolatileMember(true);
John McCallf85e1932011-06-15 23:02:42 +0000313
Douglas Gregor2bb11012011-05-13 01:05:07 +0000314 // Keep track of the presence of mutable fields.
315 if (BaseClassDecl->hasMutableFields())
316 data().HasMutableFields = true;
Richard Smithd5bc8672012-12-08 02:01:17 +0000317
318 if (BaseClassDecl->hasUninitializedReferenceMember())
319 data().HasUninitializedReferenceMember = true;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000320
321 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000322 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000323
324 if (VBases.empty())
325 return;
326
327 // Create base specifier for any direct or indirect virtual bases.
328 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
329 data().NumVBases = VBases.size();
Richard Smithbc2a35d2012-12-08 08:32:28 +0000330 for (int I = 0, E = VBases.size(); I != E; ++I) {
331 QualType Type = VBases[I]->getType();
332 if (!Type->isDependentType())
333 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000334 data().getVBases()[I] = *VBases[I];
Richard Smithbc2a35d2012-12-08 08:32:28 +0000335 }
336}
337
338void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
339 // C++11 [class.copy]p11:
340 // A defaulted copy/move constructor for a class X is defined as
341 // deleted if X has:
342 // -- a direct or virtual base class B that cannot be copied/moved [...]
343 // -- a non-static data member of class type M (or array thereof)
344 // that cannot be copied or moved [...]
345 if (!Subobj->hasSimpleMoveConstructor())
346 data().NeedOverloadResolutionForMoveConstructor = true;
347
348 // C++11 [class.copy]p23:
349 // A defaulted copy/move assignment operator for a class X is defined as
350 // deleted if X has:
351 // -- a direct or virtual base class B that cannot be copied/moved [...]
352 // -- a non-static data member of class type M (or array thereof)
353 // that cannot be copied or moved [...]
354 if (!Subobj->hasSimpleMoveAssignment())
355 data().NeedOverloadResolutionForMoveAssignment = true;
356
357 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
358 // A defaulted [ctor or dtor] for a class X is defined as
359 // deleted if X has:
360 // -- any direct or virtual base class [...] has a type with a destructor
361 // that is deleted or inaccessible from the defaulted [ctor or dtor].
362 // -- any non-static data member has a type with a destructor
363 // that is deleted or inaccessible from the defaulted [ctor or dtor].
364 if (!Subobj->hasSimpleDestructor()) {
365 data().NeedOverloadResolutionForMoveConstructor = true;
366 data().NeedOverloadResolutionForDestructor = true;
367 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000368}
369
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000370/// Callback function for CXXRecordDecl::forallBases that acknowledges
371/// that it saw a base class.
372static bool SawBase(const CXXRecordDecl *, void *) {
373 return true;
374}
375
376bool CXXRecordDecl::hasAnyDependentBases() const {
377 if (!isDependentContext())
378 return false;
379
380 return !forallBases(SawBase, 0);
381}
382
Chandler Carruthb7e95892011-04-23 10:47:28 +0000383bool CXXRecordDecl::isTriviallyCopyable() const {
384 // C++0x [class]p5:
385 // A trivially copyable class is a class that:
386 // -- has no non-trivial copy constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000387 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000388 // -- has no non-trivial move constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000389 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000390 // -- has no non-trivial copy assignment operators,
Richard Smith426391c2012-11-16 00:53:38 +0000391 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000392 // -- has no non-trivial move assignment operators, and
Richard Smith426391c2012-11-16 00:53:38 +0000393 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000394 // -- has a trivial destructor.
395 if (!hasTrivialDestructor()) return false;
396
397 return true;
398}
399
Douglas Gregor21386642010-09-28 21:55:22 +0000400void CXXRecordDecl::markedVirtualFunctionPure() {
401 // C++ [class.abstract]p2:
402 // A class is abstract if it has at least one pure virtual function.
403 data().Abstract = true;
404}
405
406void CXXRecordDecl::addedMember(Decl *D) {
Joao Matos17d35c32012-08-31 22:18:20 +0000407 if (!D->isImplicit() &&
408 !isa<FieldDecl>(D) &&
409 !isa<IndirectFieldDecl>(D) &&
410 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
411 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
412 data().HasOnlyCMembers = false;
413
414 // Ignore friends and invalid declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000415 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000416 return;
417
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000418 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
419 if (FunTmpl)
420 D = FunTmpl->getTemplatedDecl();
421
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000422 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
423 if (Method->isVirtual()) {
424 // C++ [dcl.init.aggr]p1:
425 // An aggregate is an array or a class with [...] no virtual functions.
426 data().Aggregate = false;
427
428 // C++ [class]p4:
429 // A POD-struct is an aggregate class...
430 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000431
432 // Virtual functions make the class non-empty.
433 // FIXME: Standard ref?
434 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000435
436 // C++ [class.virtual]p1:
437 // A class that declares or inherits a virtual function is called a
438 // polymorphic class.
439 data().Polymorphic = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000440
Richard Smith7d04d3a2012-11-30 05:11:39 +0000441 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
442 // A [default constructor, copy/move constructor, or copy/move
443 // assignment operator for a class X] is trivial [...] if:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000444 // -- class X has no virtual functions [...]
Richard Smith7d04d3a2012-11-30 05:11:39 +0000445 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000446
Chandler Carrutha8225442011-04-30 09:17:45 +0000447 // C++0x [class]p7:
448 // A standard-layout class is a class that: [...]
449 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000450 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000451 }
452 }
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000453
Richard Smithacf796b2012-11-28 06:23:12 +0000454 // Notify the listener if an implicit member was added after the definition
455 // was completed.
456 if (!isBeingDefined() && D->isImplicit())
457 if (ASTMutationListener *L = getASTMutationListener())
458 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000459
Richard Smith7d04d3a2012-11-30 05:11:39 +0000460 // The kind of special member this declaration is, if any.
461 unsigned SMKind = 0;
462
Richard Smithacf796b2012-11-28 06:23:12 +0000463 // Handle constructors.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000464 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smithacf796b2012-11-28 06:23:12 +0000465 if (!Constructor->isImplicit()) {
466 // Note that we have a user-declared constructor.
467 data().UserDeclaredConstructor = true;
468
469 // C++ [class]p4:
470 // A POD-struct is an aggregate class [...]
471 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
472 // type is technically an aggregate in C++0x since it wouldn't be in 03.
473 data().PlainOldData = false;
474 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000475
Richard Smith017ab772011-09-05 02:13:09 +0000476 // Technically, "user-provided" is only defined for special member
477 // functions, but the intent of the standard is clearly that it should apply
478 // to all functions.
479 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000480
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000481 if (Constructor->isDefaultConstructor()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000482 SMKind |= SMF_DefaultConstructor;
483
484 if (UserProvided)
Sean Huntcdee3fe2011-05-11 22:34:38 +0000485 data().UserProvidedDefaultConstructor = true;
Richard Smithacf796b2012-11-28 06:23:12 +0000486 if (Constructor->isConstexpr())
Richard Smith61802452011-12-22 02:22:31 +0000487 data().HasConstexprDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000488 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000489
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000490 if (!FunTmpl) {
Richard Smithacf796b2012-11-28 06:23:12 +0000491 unsigned Quals;
492 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000493 SMKind |= SMF_CopyConstructor;
Richard Smithacf796b2012-11-28 06:23:12 +0000494
495 if (Quals & Qualifiers::Const)
496 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith7d04d3a2012-11-30 05:11:39 +0000497 } else if (Constructor->isMoveConstructor())
498 SMKind |= SMF_MoveConstructor;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000499 }
Richard Smithacf796b2012-11-28 06:23:12 +0000500
501 // Record if we see any constexpr constructors which are neither copy
502 // nor move constructors.
503 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith6b8bc072011-08-10 18:11:37 +0000504 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000505
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000506 // C++ [dcl.init.aggr]p1:
507 // An aggregate is an array or a class with no user-declared
508 // constructors [...].
Richard Smithc3bf52c2013-04-20 22:23:05 +0000509 // C++11 [dcl.init.aggr]p1:
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000510 // An aggregate is an array or a class with no user-provided
511 // constructors [...].
Richard Smith80ad52f2013-01-02 11:42:31 +0000512 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smithacf796b2012-11-28 06:23:12 +0000513 ? UserProvided : !Constructor->isImplicit())
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000514 data().Aggregate = false;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000515 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000516
Richard Smithacf796b2012-11-28 06:23:12 +0000517 // Handle destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000518 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000519 SMKind |= SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000520
Richard Smith7d04d3a2012-11-30 05:11:39 +0000521 if (!DD->isImplicit())
Richard Smithacf796b2012-11-28 06:23:12 +0000522 data().HasIrrelevantDestructor = false;
523
Richard Smithacf796b2012-11-28 06:23:12 +0000524 // C++11 [class.dtor]p5:
Richard Smith7d04d3a2012-11-30 05:11:39 +0000525 // A destructor is trivial if [...] the destructor is not virtual.
526 if (DD->isVirtual())
527 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000528 }
Richard Smithacf796b2012-11-28 06:23:12 +0000529
530 // Handle member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000531 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000532 if (Method->isCopyAssignmentOperator()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000533 SMKind |= SMF_CopyAssignment;
Richard Smithacf796b2012-11-28 06:23:12 +0000534
535 const ReferenceType *ParamTy =
536 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
537 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
538 data().HasDeclaredCopyAssignmentWithConstParam = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000539 }
Sean Huntffe37fd2011-05-25 20:50:04 +0000540
Richard Smith7d04d3a2012-11-30 05:11:39 +0000541 if (Method->isMoveAssignmentOperator())
542 SMKind |= SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000543
Douglas Gregore80622f2010-09-29 04:25:11 +0000544 // Keep the list of conversion functions up-to-date.
545 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor109f5fc2013-04-08 17:12:58 +0000546 // FIXME: We use the 'unsafe' accessor for the access specifier here,
547 // because Sema may not have set it yet. That's really just a misdesign
548 // in Sema. However, LLDB *will* have set the access specifier correctly,
549 // and adds declarations after the class is technically completed,
550 // so completeDefinition()'s overriding of the access specifiers doesn't
551 // work.
552 AccessSpecifier AS = Conversion->getAccessUnsafe();
553
Richard Smith7d04d3a2012-11-30 05:11:39 +0000554 if (Conversion->getPrimaryTemplate()) {
555 // We don't record specializations.
556 } else if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000557 if (FunTmpl->getPreviousDecl())
558 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregor109f5fc2013-04-08 17:12:58 +0000559 FunTmpl, AS);
Douglas Gregore80622f2010-09-29 04:25:11 +0000560 else
Douglas Gregor109f5fc2013-04-08 17:12:58 +0000561 data().Conversions.addDecl(getASTContext(), FunTmpl, AS);
Douglas Gregore80622f2010-09-29 04:25:11 +0000562 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000563 if (Conversion->getPreviousDecl())
564 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregor109f5fc2013-04-08 17:12:58 +0000565 Conversion, AS);
Douglas Gregore80622f2010-09-29 04:25:11 +0000566 else
Douglas Gregor109f5fc2013-04-08 17:12:58 +0000567 data().Conversions.addDecl(getASTContext(), Conversion, AS);
Douglas Gregore80622f2010-09-29 04:25:11 +0000568 }
569 }
Richard Smithacf796b2012-11-28 06:23:12 +0000570
Richard Smith7d04d3a2012-11-30 05:11:39 +0000571 if (SMKind) {
Richard Smithac713512012-12-08 02:53:02 +0000572 // If this is the first declaration of a special member, we no longer have
573 // an implicit trivial special member.
574 data().HasTrivialSpecialMembers &=
575 data().DeclaredSpecialMembers | ~SMKind;
576
577 if (!Method->isImplicit() && !Method->isUserProvided()) {
578 // This method is user-declared but not user-provided. We can't work out
579 // whether it's trivial yet (not until we get to the end of the class).
580 // We'll handle this method in finishedDefaultedOrDeletedMember.
581 } else if (Method->isTrivial())
582 data().HasTrivialSpecialMembers |= SMKind;
583 else
584 data().DeclaredNonTrivialSpecialMembers |= SMKind;
585
Richard Smith7d04d3a2012-11-30 05:11:39 +0000586 // Note when we have declared a declared special member, and suppress the
587 // implicit declaration of this special member.
588 data().DeclaredSpecialMembers |= SMKind;
589
590 if (!Method->isImplicit()) {
591 data().UserDeclaredSpecialMembers |= SMKind;
592
593 // C++03 [class]p4:
594 // A POD-struct is an aggregate class that has [...] no user-defined
595 // copy assignment operator and no user-defined destructor.
596 //
597 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
598 // aggregates could not have any constructors, clear it even for an
599 // explicitly defaulted or deleted constructor.
600 // type is technically an aggregate in C++0x since it wouldn't be in 03.
601 //
602 // Also, a user-declared move assignment operator makes a class non-POD.
603 // This is an extension in C++03.
604 data().PlainOldData = false;
605 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000606 }
607
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000608 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000609 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000610
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000611 // Handle non-static data members.
612 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000613 // C++ [class.bit]p2:
614 // A declaration for a bit-field that omits the identifier declares an
615 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
616 // initialized.
617 if (Field->isUnnamedBitfield())
618 return;
619
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000620 // C++ [dcl.init.aggr]p1:
621 // An aggregate is an array or a class (clause 9) with [...] no
622 // private or protected non-static data members (clause 11).
623 //
624 // A POD must be an aggregate.
625 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
626 data().Aggregate = false;
627 data().PlainOldData = false;
628 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000629
630 // C++0x [class]p7:
631 // A standard-layout class is a class that:
632 // [...]
633 // -- has the same access control for all non-static data members,
634 switch (D->getAccess()) {
635 case AS_private: data().HasPrivateFields = true; break;
636 case AS_protected: data().HasProtectedFields = true; break;
637 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000638 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000639 };
640 if ((data().HasPrivateFields + data().HasProtectedFields +
641 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000642 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000643
Douglas Gregor2bb11012011-05-13 01:05:07 +0000644 // Keep track of the presence of mutable fields.
645 if (Field->isMutable())
646 data().HasMutableFields = true;
647
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000648 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000649 // A POD struct is a class that is both a trivial class and a
650 // standard-layout class, and has no non-static data members of type
651 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000652 //
653 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
654 // that does not explicitly have no lifetime makes the class a non-POD.
655 // However, we delay setting PlainOldData to false in this case so that
656 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000657 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000658 // In this case, the class will become a non-POD class when we complete
659 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000660 ASTContext &Context = getASTContext();
661 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000662 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000663 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000664 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
665 setHasObjectMember(true);
Eli Friedman65400522013-07-20 01:06:31 +0000666 } else if (!T.isCXX98PODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000667 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000668
Chandler Carrutha8225442011-04-30 09:17:45 +0000669 if (T->isReferenceType()) {
Richard Smithd5bc8672012-12-08 02:01:17 +0000670 if (!Field->hasInClassInitializer())
671 data().HasUninitializedReferenceMember = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000672
Chandler Carrutha8225442011-04-30 09:17:45 +0000673 // C++0x [class]p7:
674 // A standard-layout class is a class that:
675 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000676 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000677 }
678
Richard Smith86c3ae42012-02-13 03:54:03 +0000679 // Record if this field is the first non-literal or volatile field or base.
Richard Smitha10b9782013-04-22 15:31:51 +0000680 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000681 data().HasNonLiteralTypeFieldsOrBases = true;
682
Richard Smith7a614d82011-06-11 17:19:42 +0000683 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000684 data().HasInClassInitializer = true;
685
686 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000687 // A default constructor is trivial if [...] no non-static data member
688 // of its class has a brace-or-equal-initializer.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000689 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith7a614d82011-06-11 17:19:42 +0000690
Richard Smithd079abf2012-05-07 01:07:30 +0000691 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000692 // An aggregate is a [...] class with [...] no
693 // brace-or-equal-initializers for non-static data members.
Richard Smithc3bf52c2013-04-20 22:23:05 +0000694 //
695 // This rule was removed in C++1y.
696 if (!getASTContext().getLangOpts().CPlusPlus1y)
697 data().Aggregate = false;
Richard Smith7a614d82011-06-11 17:19:42 +0000698
Richard Smithd079abf2012-05-07 01:07:30 +0000699 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000700 // A POD struct is [...] a trivial class.
701 data().PlainOldData = false;
702 }
703
Richard Smithbc2a35d2012-12-08 08:32:28 +0000704 // C++11 [class.copy]p23:
705 // A defaulted copy/move assignment operator for a class X is defined
706 // as deleted if X has:
707 // -- a non-static data member of reference type
708 if (T->isReferenceType())
709 data().DefaultedMoveAssignmentIsDeleted = true;
710
Douglas Gregor85606eb2010-09-28 20:50:54 +0000711 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
712 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
713 if (FieldRec->getDefinition()) {
Richard Smithbc2a35d2012-12-08 08:32:28 +0000714 addedClassSubobject(FieldRec);
715
716 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
717 // A defaulted [special member] for a class X is defined as
718 // deleted if:
719 // -- X is a union-like class that has a variant member with a
720 // non-trivial [corresponding special member]
721 if (isUnion()) {
722 if (FieldRec->hasNonTrivialMoveConstructor())
723 data().DefaultedMoveConstructorIsDeleted = true;
724 if (FieldRec->hasNonTrivialMoveAssignment())
725 data().DefaultedMoveAssignmentIsDeleted = true;
726 if (FieldRec->hasNonTrivialDestructor())
727 data().DefaultedDestructorIsDeleted = true;
728 }
729
Sean Hunt023df372011-05-09 18:22:59 +0000730 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000731 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000732 // -- for all the non-static data members of its class that are of
733 // class type (or array thereof), each such class has a trivial
734 // default constructor.
735 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000736 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000737
738 // C++0x [class.copy]p13:
739 // A copy/move constructor for class X is trivial if [...]
740 // [...]
741 // -- for each non-static data member of X that is of class type (or
742 // an array thereof), the constructor selected to copy/move that
743 // member is trivial;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000744 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000745 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000746 // If the field doesn't have a simple move constructor, we'll eagerly
747 // declare the move constructor for this class and we'll decide whether
748 // it's trivial then.
Richard Smith426391c2012-11-16 00:53:38 +0000749 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000750 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000751
752 // C++0x [class.copy]p27:
753 // A copy/move assignment operator for class X is trivial if [...]
754 // [...]
755 // -- for each non-static data member of X that is of class type (or
756 // an array thereof), the assignment operator selected to
757 // copy/move that member is trivial;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000758 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000759 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000760 // If the field doesn't have a simple move assignment, we'll eagerly
761 // declare the move assignment for this class and we'll decide whether
762 // it's trivial then.
Richard Smith426391c2012-11-16 00:53:38 +0000763 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000764 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000765
Douglas Gregor85606eb2010-09-28 20:50:54 +0000766 if (!FieldRec->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000767 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000768 if (!FieldRec->hasIrrelevantDestructor())
769 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000770 if (FieldRec->hasObjectMember())
771 setHasObjectMember(true);
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +0000772 if (FieldRec->hasVolatileMember())
773 setHasVolatileMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000774
775 // C++0x [class]p7:
776 // A standard-layout class is a class that:
777 // -- has no non-static data members of type non-standard-layout
778 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000779 if (!FieldRec->isStandardLayout())
780 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000781
782 // C++0x [class]p7:
783 // A standard-layout class is a class that:
784 // [...]
785 // -- has no base classes of the same type as the first non-static
786 // data member.
787 // We don't want to expend bits in the state of the record decl
788 // tracking whether this is the first non-static data member so we
789 // cheat a bit and use some of the existing state: the empty bit.
790 // Virtual bases and virtual methods make a class non-empty, but they
791 // also make it non-standard-layout so we needn't check here.
792 // A non-empty base class may leave the class standard-layout, but not
793 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000794 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000795 // data member must come through here with Empty still true, and Empty
796 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000797 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000798 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
799 BE = bases_end();
800 BI != BE; ++BI) {
801 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000802 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000803 break;
804 }
805 }
806 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000807
808 // Keep track of the presence of mutable fields.
809 if (FieldRec->hasMutableFields())
810 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000811
812 // C++11 [class.copy]p13:
813 // If the implicitly-defined constructor would satisfy the
814 // requirements of a constexpr constructor, the implicitly-defined
815 // constructor is constexpr.
816 // C++11 [dcl.constexpr]p4:
817 // -- every constructor involved in initializing non-static data
818 // members [...] shall be a constexpr constructor
819 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000820 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000821 // The standard requires any in-class initializer to be a constant
822 // expression. We consider this to be a defect.
823 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithacf796b2012-11-28 06:23:12 +0000824
825 // C++11 [class.copy]p8:
826 // The implicitly-declared copy constructor for a class X will have
827 // the form 'X::X(const X&)' if [...] for all the non-static data
828 // members of X that are of a class type M (or array thereof), each
829 // such class type has a copy constructor whose first parameter is
830 // of type 'const M&' or 'const volatile M&'.
831 if (!FieldRec->hasCopyConstructorWithConstParam())
832 data().ImplicitCopyConstructorHasConstParam = false;
833
834 // C++11 [class.copy]p18:
835 // The implicitly-declared copy assignment oeprator for a class X will
836 // have the form 'X& X::operator=(const X&)' if [...] for all the
837 // non-static data members of X that are of a class type M (or array
838 // thereof), each such class type has a copy assignment operator whose
839 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
840 if (!FieldRec->hasCopyAssignmentWithConstParam())
841 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smithd5bc8672012-12-08 02:01:17 +0000842
843 if (FieldRec->hasUninitializedReferenceMember() &&
844 !Field->hasInClassInitializer())
845 data().HasUninitializedReferenceMember = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000846 }
Richard Smith61802452011-12-22 02:22:31 +0000847 } else {
848 // Base element type of field is a non-class type.
Richard Smitha10b9782013-04-22 15:31:51 +0000849 if (!T->isLiteralType(Context) ||
Richard Smithd3861ce2012-06-10 07:07:24 +0000850 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000851 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000852
853 // C++11 [class.copy]p23:
854 // A defaulted copy/move assignment operator for a class X is defined
855 // as deleted if X has:
856 // -- a non-static data member of const non-class type (or array
857 // thereof)
858 if (T.isConstQualified())
859 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000860 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000861
862 // C++0x [class]p7:
863 // A standard-layout class is a class that:
864 // [...]
865 // -- either has no non-static data members in the most derived
866 // class and at most one base class with non-static data members,
867 // or has no base classes with non-static data members, and
868 // At this point we know that we have a non-static data member, so the last
869 // clause holds.
870 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000871 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000872
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000873 // If this is not a zero-length bit-field, then the class is not empty.
874 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000875 if (!Field->isBitField() ||
876 (!Field->getBitWidth()->isTypeDependent() &&
877 !Field->getBitWidth()->isValueDependent() &&
878 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000879 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000880 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000881 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000882
883 // Handle using declarations of conversion functions.
884 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
885 if (Shadow->getDeclName().getNameKind()
886 == DeclarationName::CXXConversionFunctionName)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000887 data().Conversions.addDecl(getASTContext(), Shadow, Shadow->getAccess());
Joao Matos17d35c32012-08-31 22:18:20 +0000888}
889
Richard Smithac713512012-12-08 02:53:02 +0000890void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
891 assert(!D->isImplicit() && !D->isUserProvided());
892
893 // The kind of special member this declaration is, if any.
894 unsigned SMKind = 0;
895
896 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
897 if (Constructor->isDefaultConstructor()) {
898 SMKind |= SMF_DefaultConstructor;
899 if (Constructor->isConstexpr())
900 data().HasConstexprDefaultConstructor = true;
901 }
902 if (Constructor->isCopyConstructor())
903 SMKind |= SMF_CopyConstructor;
904 else if (Constructor->isMoveConstructor())
905 SMKind |= SMF_MoveConstructor;
906 else if (Constructor->isConstexpr())
907 // We may now know that the constructor is constexpr.
908 data().HasConstexprNonCopyMoveConstructor = true;
909 } else if (isa<CXXDestructorDecl>(D))
910 SMKind |= SMF_Destructor;
911 else if (D->isCopyAssignmentOperator())
912 SMKind |= SMF_CopyAssignment;
913 else if (D->isMoveAssignmentOperator())
914 SMKind |= SMF_MoveAssignment;
915
916 // Update which trivial / non-trivial special members we have.
917 // addedMember will have skipped this step for this member.
918 if (D->isTrivial())
919 data().HasTrivialSpecialMembers |= SMKind;
920 else
921 data().DeclaredNonTrivialSpecialMembers |= SMKind;
922}
923
Joao Matos17d35c32012-08-31 22:18:20 +0000924bool CXXRecordDecl::isCLike() const {
925 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
926 !TemplateOrInstantiation.isNull())
927 return false;
928 if (!hasDefinition())
929 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000930
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000931 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000932}
933
Faisal Valiecb58192013-08-22 01:49:11 +0000934bool CXXRecordDecl::isGenericLambda() const {
935 return isLambda() &&
936 getLambdaCallOperator()->getDescribedFunctionTemplate();
937}
938
939CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
940 if (!isLambda()) return 0;
941 DeclarationName Name =
942 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
943 DeclContext::lookup_const_result Calls = lookup(Name);
944
945 assert(!Calls.empty() && "Missing lambda call operator!");
946 assert(Calls.size() == 1 && "More than one lambda call operator!");
947
948 NamedDecl *CallOp = Calls.front();
949 if (FunctionTemplateDecl *CallOpTmpl =
950 dyn_cast<FunctionTemplateDecl>(CallOp))
951 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
952
953 return cast<CXXMethodDecl>(CallOp);
954}
955
956CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
957 if (!isLambda()) return 0;
958 DeclarationName Name =
959 &getASTContext().Idents.get(getLambdaStaticInvokerName());
960 DeclContext::lookup_const_result Invoker = lookup(Name);
961 if (Invoker.empty()) return 0;
962 assert(Invoker.size() == 1 && "More than one static invoker operator!");
963 CXXMethodDecl *Result = cast<CXXMethodDecl>(Invoker.front());
964 return Result;
965
966}
967
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000968void CXXRecordDecl::getCaptureFields(
969 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000970 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000971 Captures.clear();
972 ThisCapture = 0;
973
Douglas Gregorda8962a2012-02-13 15:44:47 +0000974 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000975 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000976 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000977 C != CEnd; ++C, ++Field) {
Richard Smith0d8e9642013-05-16 06:20:58 +0000978 if (C->capturesThis())
David Blaikie581deb32012-06-06 20:45:41 +0000979 ThisCapture = *Field;
Richard Smith0d8e9642013-05-16 06:20:58 +0000980 else if (C->capturesVariable())
981 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000982 }
983}
984
Faisal Valiecb58192013-08-22 01:49:11 +0000985TemplateParameterList*
986 CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
987 if (!isLambda()) return 0;
988 CXXMethodDecl *CallOp = getLambdaCallOperator();
989 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
990 return Tmpl->getTemplateParameters();
991 return 0;
992}
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000993
John McCallb05b5f32010-03-15 09:07:48 +0000994static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
995 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000996 if (isa<UsingShadowDecl>(Conv))
997 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000998 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
999 T = ConvTemp->getTemplatedDecl()->getResultType();
1000 else
1001 T = cast<CXXConversionDecl>(Conv)->getConversionType();
1002 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +00001003}
1004
John McCallb05b5f32010-03-15 09:07:48 +00001005/// Collect the visible conversions of a base class.
1006///
James Dennetta1253502012-06-15 22:28:09 +00001007/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +00001008/// \param InVirtual whether this base class is a virtual base (or a base
1009/// of a virtual base)
1010/// \param Access the access along the inheritance path to this base
1011/// \param ParentHiddenTypes the conversions provided by the inheritors
1012/// of this base
1013/// \param Output the set to which to add conversions from non-virtual bases
1014/// \param VOutput the set to which to add conversions from virtual bases
1015/// \param HiddenVBaseCs the set of conversions which were hidden in a
1016/// virtual base along some inheritance path
1017static void CollectVisibleConversions(ASTContext &Context,
1018 CXXRecordDecl *Record,
1019 bool InVirtual,
1020 AccessSpecifier Access,
1021 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001022 ASTUnresolvedSet &Output,
John McCallb05b5f32010-03-15 09:07:48 +00001023 UnresolvedSetImpl &VOutput,
1024 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1025 // The set of types which have conversions in this class or its
1026 // subclasses. As an optimization, we don't copy the derived set
1027 // unless it might change.
1028 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1029 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1030
1031 // Collect the direct conversions and figure out which conversions
1032 // will be hidden in the subclasses.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001033 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1034 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1035 if (ConvI != ConvE) {
John McCallb05b5f32010-03-15 09:07:48 +00001036 HiddenTypesBuffer = ParentHiddenTypes;
1037 HiddenTypes = &HiddenTypesBuffer;
1038
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001039 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +00001040 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1041 bool Hidden = ParentHiddenTypes.count(ConvType);
1042 if (!Hidden)
1043 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001044
1045 // If this conversion is hidden and we're in a virtual base,
1046 // remember that it's hidden along some inheritance path.
1047 if (Hidden && InVirtual)
1048 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1049
1050 // If this conversion isn't hidden, add it to the appropriate output.
1051 else if (!Hidden) {
1052 AccessSpecifier IAccess
1053 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1054
1055 if (InVirtual)
1056 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001057 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001058 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001059 }
1060 }
1061 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001062
John McCallb05b5f32010-03-15 09:07:48 +00001063 // Collect information recursively from any base classes.
1064 for (CXXRecordDecl::base_class_iterator
1065 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1066 const RecordType *RT = I->getType()->getAs<RecordType>();
1067 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001068
John McCallb05b5f32010-03-15 09:07:48 +00001069 AccessSpecifier BaseAccess
1070 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1071 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001072
John McCallb05b5f32010-03-15 09:07:48 +00001073 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1074 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1075 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001076 }
John McCallb05b5f32010-03-15 09:07:48 +00001077}
Sebastian Redl9994a342009-10-25 17:03:50 +00001078
John McCallb05b5f32010-03-15 09:07:48 +00001079/// Collect the visible conversions of a class.
1080///
1081/// This would be extremely straightforward if it weren't for virtual
1082/// bases. It might be worth special-casing that, really.
1083static void CollectVisibleConversions(ASTContext &Context,
1084 CXXRecordDecl *Record,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001085 ASTUnresolvedSet &Output) {
John McCallb05b5f32010-03-15 09:07:48 +00001086 // The collection of all conversions in virtual bases that we've
1087 // found. These will be added to the output as long as they don't
1088 // appear in the hidden-conversions set.
1089 UnresolvedSet<8> VBaseCs;
1090
1091 // The set of conversions in virtual bases that we've determined to
1092 // be hidden.
1093 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1094
1095 // The set of types hidden by classes derived from this one.
1096 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1097
1098 // Go ahead and collect the direct conversions and add them to the
1099 // hidden-types set.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001100 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1101 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001102 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001103 for (; ConvI != ConvE; ++ConvI)
1104 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCallb05b5f32010-03-15 09:07:48 +00001105
1106 // Recursively collect conversions from base classes.
1107 for (CXXRecordDecl::base_class_iterator
1108 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1109 const RecordType *RT = I->getType()->getAs<RecordType>();
1110 if (!RT) continue;
1111
1112 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1113 I->isVirtual(), I->getAccessSpecifier(),
1114 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1115 }
1116
1117 // Add any unhidden conversions provided by virtual bases.
1118 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1119 I != E; ++I) {
1120 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001121 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001122 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001123}
1124
1125/// getVisibleConversionFunctions - get all conversion functions visible
1126/// in current class; including conversion function templates.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001127std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1128CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001129 // If root class, all conversions are visible.
1130 if (bases_begin() == bases_end())
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001131 return std::make_pair(data().Conversions.begin(), data().Conversions.end());
Fariborz Jahanian62509212009-09-12 18:26:03 +00001132 // If visible conversion list is already evaluated, return it.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001133 if (!data().ComputedVisibleConversions) {
1134 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
1135 data().ComputedVisibleConversions = true;
1136 }
1137 return std::make_pair(data().VisibleConversions.begin(),
1138 data().VisibleConversions.end());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001139}
1140
John McCall32daa422010-03-31 01:36:47 +00001141void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1142 // This operation is O(N) but extremely rare. Sema only uses it to
1143 // remove UsingShadowDecls in a class that were followed by a direct
1144 // declaration, e.g.:
1145 // class A : B {
1146 // using B::operator int;
1147 // operator int();
1148 // };
1149 // This is uncommon by itself and even more uncommon in conjunction
1150 // with sufficiently large numbers of directly-declared conversions
1151 // that asymptotic behavior matters.
1152
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001153 ASTUnresolvedSet &Convs = data().Conversions;
John McCall32daa422010-03-31 01:36:47 +00001154 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1155 if (Convs[I].getDecl() == ConvDecl) {
1156 Convs.erase(I);
1157 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1158 && "conversion was found multiple times in unresolved set");
1159 return;
1160 }
1161 }
1162
1163 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001164}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001165
Douglas Gregorf6b11852009-10-08 15:14:33 +00001166CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001167 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001168 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1169
1170 return 0;
1171}
1172
1173void
1174CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1175 TemplateSpecializationKind TSK) {
1176 assert(TemplateOrInstantiation.isNull() &&
1177 "Previous template or instantiation?");
1178 assert(!isa<ClassTemplateSpecializationDecl>(this));
1179 TemplateOrInstantiation
1180 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1181}
1182
Anders Carlssonb13e3572009-12-07 06:33:48 +00001183TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1184 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001185 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1186 return Spec->getSpecializationKind();
1187
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001188 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001189 return MSInfo->getTemplateSpecializationKind();
1190
1191 return TSK_Undeclared;
1192}
1193
1194void
1195CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1196 if (ClassTemplateSpecializationDecl *Spec
1197 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1198 Spec->setSpecializationKind(TSK);
1199 return;
1200 }
1201
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001202 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001203 MSInfo->setTemplateSpecializationKind(TSK);
1204 return;
1205 }
1206
David Blaikieb219cfc2011-09-23 05:06:16 +00001207 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001208}
1209
Douglas Gregor1d110e02010-07-01 14:13:13 +00001210CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1211 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001212 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001213
1214 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001215 = Context.DeclarationNames.getCXXDestructorName(
1216 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001217
David Blaikie3bc93e32012-12-19 00:45:41 +00001218 DeclContext::lookup_const_result R = lookup(Name);
1219 if (R.empty())
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001220 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001221
David Blaikie3bc93e32012-12-19 00:45:41 +00001222 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson7267c162009-05-29 21:03:38 +00001223 return Dtor;
1224}
1225
Douglas Gregorda2142f2011-02-19 18:51:44 +00001226void CXXRecordDecl::completeDefinition() {
1227 completeDefinition(0);
1228}
1229
1230void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1231 RecordDecl::completeDefinition();
1232
David Blaikie4e4d0842012-03-11 07:00:24 +00001233 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001234 // Objective-C Automatic Reference Counting:
1235 // If a class has a non-static data member of Objective-C pointer
1236 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001237 // default constructor (if any), copy constructor, move constructor,
1238 // copy assignment operator, move assignment operator, and destructor are
1239 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001240 struct DefinitionData &Data = data();
1241 Data.PlainOldData = false;
Richard Smith7d04d3a2012-11-30 05:11:39 +00001242 Data.HasTrivialSpecialMembers = 0;
Richard Smithdfefb842012-02-25 07:33:38 +00001243 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001244 }
1245
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001246 // If the class may be abstract (but hasn't been marked as such), check for
1247 // any pure final overriders.
1248 if (mayBeAbstract()) {
1249 CXXFinalOverriderMap MyFinalOverriders;
1250 if (!FinalOverriders) {
1251 getFinalOverriders(MyFinalOverriders);
1252 FinalOverriders = &MyFinalOverriders;
1253 }
1254
1255 bool Done = false;
1256 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1257 MEnd = FinalOverriders->end();
1258 M != MEnd && !Done; ++M) {
1259 for (OverridingMethods::iterator SO = M->second.begin(),
1260 SOEnd = M->second.end();
1261 SO != SOEnd && !Done; ++SO) {
1262 assert(SO->second.size() > 0 &&
1263 "All virtual functions have overridding virtual functions");
1264
1265 // C++ [class.abstract]p4:
1266 // A class is abstract if it contains or inherits at least one
1267 // pure virtual function for which the final overrider is pure
1268 // virtual.
1269 if (SO->second.front().Method->isPure()) {
1270 data().Abstract = true;
1271 Done = true;
1272 break;
1273 }
1274 }
1275 }
1276 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001277
1278 // Set access bits correctly on the directly-declared conversions.
1279 for (UnresolvedSetIterator I = data().Conversions.begin(),
1280 E = data().Conversions.end();
1281 I != E; ++I)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001282 I.setAccess((*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001283}
1284
1285bool CXXRecordDecl::mayBeAbstract() const {
1286 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1287 isDependentContext())
1288 return false;
1289
1290 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1291 BEnd = bases_end();
1292 B != BEnd; ++B) {
1293 CXXRecordDecl *BaseDecl
1294 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1295 if (BaseDecl->isAbstract())
1296 return true;
1297 }
1298
1299 return false;
1300}
1301
David Blaikie99ba9e32011-12-20 02:48:34 +00001302void CXXMethodDecl::anchor() { }
1303
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001304bool CXXMethodDecl::isStatic() const {
Rafael Espindola72fdc892013-04-15 12:38:20 +00001305 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001306
1307 if (MD->getStorageClass() == SC_Static)
1308 return true;
1309
1310 DeclarationName Name = getDeclName();
1311 // [class.free]p1:
1312 // Any allocation function for a class T is a static member
1313 // (even if not explicitly declared static).
1314 if (Name.getCXXOverloadedOperator() == OO_New ||
1315 Name.getCXXOverloadedOperator() == OO_Array_New)
1316 return true;
1317
1318 // [class.free]p6 Any deallocation function for a class X is a static member
1319 // (even if not explicitly declared static).
1320 if (Name.getCXXOverloadedOperator() == OO_Delete ||
1321 Name.getCXXOverloadedOperator() == OO_Array_Delete)
1322 return true;
1323
1324 return false;
1325}
1326
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001327static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1328 const CXXMethodDecl *BaseMD) {
1329 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1330 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1331 const CXXMethodDecl *MD = *I;
1332 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1333 return true;
1334 if (recursivelyOverrides(MD, BaseMD))
1335 return true;
1336 }
1337 return false;
1338}
1339
1340CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001341CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1342 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001343 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1344 return this;
1345
1346 // Lookup doesn't work for destructors, so handle them separately.
1347 if (isa<CXXDestructorDecl>(this)) {
1348 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001349 if (MD) {
1350 if (recursivelyOverrides(MD, this))
1351 return MD;
1352 if (MayBeBase && recursivelyOverrides(this, MD))
1353 return MD;
1354 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001355 return NULL;
1356 }
1357
1358 lookup_const_result Candidates = RD->lookup(getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +00001359 for (NamedDecl * const * I = Candidates.begin(); I != Candidates.end(); ++I) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001360 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1361 if (!MD)
1362 continue;
1363 if (recursivelyOverrides(MD, this))
1364 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001365 if (MayBeBase && recursivelyOverrides(this, MD))
1366 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001367 }
1368
1369 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1370 E = RD->bases_end(); I != E; ++I) {
1371 const RecordType *RT = I->getType()->getAs<RecordType>();
1372 if (!RT)
1373 continue;
1374 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1375 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1376 if (T)
1377 return T;
1378 }
1379
1380 return NULL;
1381}
1382
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001383CXXMethodDecl *
1384CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001385 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001386 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001387 QualType T, TypeSourceInfo *TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001388 StorageClass SC, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001389 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001390 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001391 SC, isInline, isConstexpr,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001392 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001393}
1394
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001395CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1396 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1397 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1398 DeclarationNameInfo(), QualType(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001399 0, SC_None, false, false,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001400 SourceLocation());
1401}
1402
Douglas Gregor90916562009-09-29 18:16:17 +00001403bool CXXMethodDecl::isUsualDeallocationFunction() const {
1404 if (getOverloadedOperator() != OO_Delete &&
1405 getOverloadedOperator() != OO_Array_Delete)
1406 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001407
1408 // C++ [basic.stc.dynamic.deallocation]p2:
1409 // A template instance is never a usual deallocation function,
1410 // regardless of its signature.
1411 if (getPrimaryTemplate())
1412 return false;
1413
Douglas Gregor90916562009-09-29 18:16:17 +00001414 // C++ [basic.stc.dynamic.deallocation]p2:
1415 // If a class T has a member deallocation function named operator delete
1416 // with exactly one parameter, then that function is a usual (non-placement)
1417 // deallocation function. [...]
1418 if (getNumParams() == 1)
1419 return true;
1420
1421 // C++ [basic.stc.dynamic.deallocation]p2:
1422 // [...] If class T does not declare such an operator delete but does
1423 // declare a member deallocation function named operator delete with
1424 // exactly two parameters, the second of which has type std::size_t (18.1),
1425 // then this function is a usual deallocation function.
1426 ASTContext &Context = getASTContext();
1427 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001428 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1429 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001430 return false;
1431
1432 // This function is a usual deallocation function if there are no
1433 // single-parameter deallocation functions of the same kind.
David Blaikie3bc93e32012-12-19 00:45:41 +00001434 DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1435 for (DeclContext::lookup_const_result::iterator I = R.begin(), E = R.end();
1436 I != E; ++I) {
1437 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregor90916562009-09-29 18:16:17 +00001438 if (FD->getNumParams() == 1)
1439 return false;
1440 }
1441
1442 return true;
1443}
1444
Douglas Gregor06a9f362010-05-01 20:49:11 +00001445bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001446 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001447 // A user-declared copy assignment operator X::operator= is a non-static
1448 // non-template member function of class X with exactly one parameter of
1449 // type X, X&, const X&, volatile X& or const volatile X&.
1450 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1451 /*non-static*/ isStatic() ||
Eli Friedmanfcb5a252013-07-11 23:55:07 +00001452 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1453 getNumParams() != 1)
Douglas Gregor06a9f362010-05-01 20:49:11 +00001454 return false;
1455
1456 QualType ParamType = getParamDecl(0)->getType();
1457 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1458 ParamType = Ref->getPointeeType();
1459
1460 ASTContext &Context = getASTContext();
1461 QualType ClassType
1462 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1463 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1464}
1465
Sean Huntffe37fd2011-05-25 20:50:04 +00001466bool CXXMethodDecl::isMoveAssignmentOperator() const {
1467 // C++0x [class.copy]p19:
1468 // A user-declared move assignment operator X::operator= is a non-static
1469 // non-template member function of class X with exactly one parameter of type
1470 // X&&, const X&&, volatile X&&, or const volatile X&&.
1471 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedmanfcb5a252013-07-11 23:55:07 +00001472 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1473 getNumParams() != 1)
Sean Huntffe37fd2011-05-25 20:50:04 +00001474 return false;
1475
1476 QualType ParamType = getParamDecl(0)->getType();
1477 if (!isa<RValueReferenceType>(ParamType))
1478 return false;
1479 ParamType = ParamType->getPointeeType();
1480
1481 ASTContext &Context = getASTContext();
1482 QualType ClassType
1483 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1484 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1485}
1486
Anders Carlsson05eb2442009-05-16 23:58:37 +00001487void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001488 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001489 assert(!MD->getParent()->isDependentContext() &&
1490 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001491 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001492
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001493 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001494}
1495
1496CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001497 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001498 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001499}
1500
1501CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001502 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001503 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001504}
1505
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001506unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001507 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001508 return getASTContext().overridden_methods_size(this);
1509}
1510
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001511QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001512 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1513 // If the member function is declared const, the type of this is const X*,
1514 // if the member function is declared volatile, the type of this is
1515 // volatile X*, and if the member function is declared const volatile,
1516 // the type of this is const volatile X*.
1517
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001518 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001519
John McCall3cb0ebd2010-03-10 03:28:59 +00001520 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001521 ClassTy = C.getQualifiedType(ClassTy,
1522 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001523 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001524}
1525
Eli Friedmand7d7f672009-12-06 20:50:05 +00001526bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001527 // If this function is a template instantiation, look at the template from
1528 // which it was instantiated.
1529 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1530 if (!CheckFn)
1531 CheckFn = this;
1532
Eli Friedmand7d7f672009-12-06 20:50:05 +00001533 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001534 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001535}
1536
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001537bool CXXMethodDecl::isLambdaStaticInvoker() const {
1538 return getParent()->isLambda() &&
Faisal Valiecb58192013-08-22 01:49:11 +00001539 getParent()->getLambdaStaticInvoker() == this;
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001540}
1541
1542
Sean Huntcbb67482011-01-08 20:30:50 +00001543CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1544 TypeSourceInfo *TInfo, bool IsVirtual,
1545 SourceLocation L, Expr *Init,
1546 SourceLocation R,
1547 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001548 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001549 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1550 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001551{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001552}
1553
Sean Huntcbb67482011-01-08 20:30:50 +00001554CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1555 FieldDecl *Member,
1556 SourceLocation MemberLoc,
1557 SourceLocation L, Expr *Init,
1558 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001559 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001560 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001561 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1562{
1563}
1564
Sean Huntcbb67482011-01-08 20:30:50 +00001565CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1566 IndirectFieldDecl *Member,
1567 SourceLocation MemberLoc,
1568 SourceLocation L, Expr *Init,
1569 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001570 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001571 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001572 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001573{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001574}
1575
Sean Huntcbb67482011-01-08 20:30:50 +00001576CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001577 TypeSourceInfo *TInfo,
1578 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001579 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001580 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1581 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001582 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1583{
1584}
1585
1586CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001587 FieldDecl *Member,
1588 SourceLocation MemberLoc,
1589 SourceLocation L, Expr *Init,
1590 SourceLocation R,
1591 VarDecl **Indices,
1592 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001593 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001594 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001595 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001596{
1597 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1598 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1599}
1600
Sean Huntcbb67482011-01-08 20:30:50 +00001601CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1602 FieldDecl *Member,
1603 SourceLocation MemberLoc,
1604 SourceLocation L, Expr *Init,
1605 SourceLocation R,
1606 VarDecl **Indices,
1607 unsigned NumIndices) {
1608 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001609 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001610 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001611 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1612 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001613}
1614
Sean Huntcbb67482011-01-08 20:30:50 +00001615TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001616 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001617 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001618 else
1619 return TypeLoc();
1620}
1621
Sean Huntcbb67482011-01-08 20:30:50 +00001622const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001623 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001624 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001625 else
1626 return 0;
1627}
1628
Sean Huntcbb67482011-01-08 20:30:50 +00001629SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001630 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001631 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001632
1633 if (isInClassMemberInitializer())
1634 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001635
Douglas Gregor76852c22011-11-01 01:16:03 +00001636 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1637 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1638
1639 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001640}
1641
Sean Huntcbb67482011-01-08 20:30:50 +00001642SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001643 if (isInClassMemberInitializer()) {
1644 FieldDecl *D = getAnyMember();
1645 if (Expr *I = D->getInClassInitializer())
1646 return I->getSourceRange();
1647 return SourceRange();
1648 }
1649
Douglas Gregor802ab452009-12-02 22:36:29 +00001650 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001651}
1652
David Blaikie99ba9e32011-12-20 02:48:34 +00001653void CXXConstructorDecl::anchor() { }
1654
Douglas Gregorb48fe382008-10-31 09:07:45 +00001655CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001656CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1657 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1658 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1659 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001660}
1661
1662CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001663CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001664 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001665 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001666 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001667 bool isExplicit, bool isInline,
1668 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001669 assert(NameInfo.getName().getNameKind()
1670 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001671 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001672 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001673 isExplicit, isInline, isImplicitlyDeclared,
1674 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001675}
1676
Douglas Gregor76852c22011-11-01 01:16:03 +00001677CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1678 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1679 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1680 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1681 return Construct->getConstructor();
1682
1683 return 0;
1684}
1685
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001686bool CXXConstructorDecl::isDefaultConstructor() const {
1687 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001688 // A default constructor for a class X is a constructor of class
1689 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001690 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001691 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001692}
1693
Mike Stump1eb44332009-09-09 15:08:12 +00001694bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001695CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001696 return isCopyOrMoveConstructor(TypeQuals) &&
1697 getParamDecl(0)->getType()->isLValueReferenceType();
1698}
1699
1700bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1701 return isCopyOrMoveConstructor(TypeQuals) &&
1702 getParamDecl(0)->getType()->isRValueReferenceType();
1703}
1704
1705/// \brief Determine whether this is a copy or move constructor.
1706bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001707 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001708 // A non-template constructor for class X is a copy constructor
1709 // if its first parameter is of type X&, const X&, volatile X& or
1710 // const volatile X&, and either there are no other parameters
1711 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001712 // C++0x [class.copy]p3:
1713 // A non-template constructor for class X is a move constructor if its
1714 // first parameter is of type X&&, const X&&, volatile X&&, or
1715 // const volatile X&&, and either there are no other parameters or else
1716 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001717 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001718 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001719 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001720 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001721 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001722
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001723 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001724
1725 // Do we have a reference type?
1726 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001727 if (!ParamRefType)
1728 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001729
Douglas Gregorfd476482009-11-13 23:59:09 +00001730 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001731 ASTContext &Context = getASTContext();
1732
Douglas Gregorfd476482009-11-13 23:59:09 +00001733 CanQualType PointeeType
1734 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001735 CanQualType ClassTy
1736 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001737 if (PointeeType.getUnqualifiedType() != ClassTy)
1738 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001739
John McCall0953e762009-09-24 19:53:00 +00001740 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001741
1742 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001743 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001744 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001745}
1746
Anders Carlssonfaccd722009-08-28 16:57:08 +00001747bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001748 // C++ [class.conv.ctor]p1:
1749 // A constructor declared without the function-specifier explicit
1750 // that can be called with a single parameter specifies a
1751 // conversion from the type of its first parameter to the type of
1752 // its class. Such a constructor is called a converting
1753 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001754 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001755 return false;
1756
Mike Stump1eb44332009-09-09 15:08:12 +00001757 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001758 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001759 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001760 (getNumParams() > 1 &&
1761 (getParamDecl(1)->hasDefaultArg() ||
1762 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001763}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001764
Douglas Gregor6493cc52010-11-08 17:16:59 +00001765bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001766 if ((getNumParams() < 1) ||
1767 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1768 (getPrimaryTemplate() == 0) ||
1769 (getDescribedFunctionTemplate() != 0))
1770 return false;
1771
1772 const ParmVarDecl *Param = getParamDecl(0);
1773
1774 ASTContext &Context = getASTContext();
1775 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1776
Douglas Gregor66724ea2009-11-14 01:20:54 +00001777 // Is it the same as our our class type?
1778 CanQualType ClassTy
1779 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1780 if (ParamType.getUnqualifiedType() != ClassTy)
1781 return false;
1782
1783 return true;
1784}
1785
Sebastian Redlf677ea32011-02-05 19:23:19 +00001786const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1787 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001788 method_iterator It = getASTContext().overridden_methods_begin(this);
1789 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001790 return 0;
1791
1792 return cast<CXXConstructorDecl>(*It);
1793}
1794
1795void
1796CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1797 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001798 assert(getASTContext().overridden_methods_size(this) == 0 &&
1799 "Base ctor already set.");
1800 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001801}
1802
David Blaikie99ba9e32011-12-20 02:48:34 +00001803void CXXDestructorDecl::anchor() { }
1804
Douglas Gregor42a552f2008-11-05 20:51:48 +00001805CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001806CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1807 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1808 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001809 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001810}
1811
1812CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001813CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001814 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001815 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001816 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001817 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001818 assert(NameInfo.getName().getNameKind()
1819 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001820 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001821 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001822 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001823}
1824
David Blaikie99ba9e32011-12-20 02:48:34 +00001825void CXXConversionDecl::anchor() { }
1826
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001827CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001828CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1829 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1830 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1831 QualType(), 0, false, false, false,
1832 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001833}
1834
1835CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001836CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001837 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001838 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001839 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001840 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001841 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001842 assert(NameInfo.getName().getNameKind()
1843 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001844 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001845 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001846 isInline, isExplicit, isConstexpr,
1847 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001848}
1849
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001850bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1851 return isImplicit() && getParent()->isLambda() &&
1852 getConversionType()->isBlockPointerType();
1853}
1854
David Blaikie99ba9e32011-12-20 02:48:34 +00001855void LinkageSpecDecl::anchor() { }
1856
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001857LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001858 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001859 SourceLocation ExternLoc,
1860 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001861 LanguageIDs Lang,
Rafael Espindolae5e575d2013-04-26 01:30:23 +00001862 bool HasBraces) {
1863 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001864}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001865
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001866LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1867 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1868 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
Rafael Espindolae5e575d2013-04-26 01:30:23 +00001869 lang_c, false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001870}
1871
David Blaikie99ba9e32011-12-20 02:48:34 +00001872void UsingDirectiveDecl::anchor() { }
1873
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001874UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1875 SourceLocation L,
1876 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001877 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001878 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001879 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001880 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001881 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1882 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001883 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1884 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001885}
1886
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001887UsingDirectiveDecl *
1888UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1889 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1890 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1891 NestedNameSpecifierLoc(),
1892 SourceLocation(), 0, 0);
1893}
1894
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001895NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1896 if (NamespaceAliasDecl *NA =
1897 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1898 return NA->getNamespace();
1899 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1900}
1901
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001902void NamespaceDecl::anchor() { }
1903
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001904NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1905 SourceLocation StartLoc,
1906 SourceLocation IdLoc, IdentifierInfo *Id,
1907 NamespaceDecl *PrevDecl)
1908 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1909 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1910{
1911 setPreviousDeclaration(PrevDecl);
1912
1913 if (PrevDecl)
1914 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1915}
1916
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001917NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001918 bool Inline, SourceLocation StartLoc,
1919 SourceLocation IdLoc, IdentifierInfo *Id,
1920 NamespaceDecl *PrevDecl) {
1921 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001922}
1923
1924NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1925 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001926 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1927 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001928}
1929
David Blaikie99ba9e32011-12-20 02:48:34 +00001930void NamespaceAliasDecl::anchor() { }
1931
Mike Stump1eb44332009-09-09 15:08:12 +00001932NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001933 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001934 SourceLocation AliasLoc,
1935 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001936 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001937 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001938 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001939 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1940 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001941 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1942 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001943}
1944
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001945NamespaceAliasDecl *
1946NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1947 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1948 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1949 NestedNameSpecifierLoc(),
1950 SourceLocation(), 0);
1951}
1952
David Blaikie99ba9e32011-12-20 02:48:34 +00001953void UsingShadowDecl::anchor() { }
1954
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001955UsingShadowDecl *
1956UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1957 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1958 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1959}
1960
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001961UsingDecl *UsingShadowDecl::getUsingDecl() const {
1962 const UsingShadowDecl *Shadow = this;
1963 while (const UsingShadowDecl *NextShadow =
1964 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1965 Shadow = NextShadow;
1966 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1967}
1968
David Blaikie99ba9e32011-12-20 02:48:34 +00001969void UsingDecl::anchor() { }
1970
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001971void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1972 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1973 "declaration already in set");
1974 assert(S->getUsingDecl() == this);
1975
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001976 if (FirstUsingShadow.getPointer())
1977 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1978 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001979}
1980
1981void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1982 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1983 "declaration not in set");
1984 assert(S->getUsingDecl() == this);
1985
1986 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1987
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001988 if (FirstUsingShadow.getPointer() == S) {
1989 FirstUsingShadow.setPointer(
1990 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001991 S->UsingOrNextShadow = this;
1992 return;
1993 }
1994
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001995 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001996 while (Prev->UsingOrNextShadow != S)
1997 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1998 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1999 S->UsingOrNextShadow = this;
2000}
2001
Douglas Gregordc355712011-02-25 00:36:19 +00002002UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2003 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002004 const DeclarationNameInfo &NameInfo,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00002005 bool HasTypename) {
2006 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002007}
2008
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002009UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2010 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
2011 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
2012 DeclarationNameInfo(), false);
2013}
2014
Enea Zaffanellad4de59d2013-07-17 17:28:56 +00002015SourceRange UsingDecl::getSourceRange() const {
2016 SourceLocation Begin = isAccessDeclaration()
2017 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2018 return SourceRange(Begin, getNameInfo().getEndLoc());
2019}
2020
David Blaikie99ba9e32011-12-20 02:48:34 +00002021void UnresolvedUsingValueDecl::anchor() { }
2022
John McCall7ba107a2009-11-18 02:36:19 +00002023UnresolvedUsingValueDecl *
2024UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2025 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002026 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002027 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00002028 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002029 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00002030}
2031
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002032UnresolvedUsingValueDecl *
2033UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2034 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
2035 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
2036 NestedNameSpecifierLoc(),
2037 DeclarationNameInfo());
2038}
2039
Enea Zaffanellad4de59d2013-07-17 17:28:56 +00002040SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2041 SourceLocation Begin = isAccessDeclaration()
2042 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2043 return SourceRange(Begin, getNameInfo().getEndLoc());
2044}
2045
David Blaikie99ba9e32011-12-20 02:48:34 +00002046void UnresolvedUsingTypenameDecl::anchor() { }
2047
John McCall7ba107a2009-11-18 02:36:19 +00002048UnresolvedUsingTypenameDecl *
2049UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2050 SourceLocation UsingLoc,
2051 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002052 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002053 SourceLocation TargetNameLoc,
2054 DeclarationName TargetName) {
2055 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002056 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002057 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00002058}
2059
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002060UnresolvedUsingTypenameDecl *
2061UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2062 void *Mem = AllocateDeserializedDecl(C, ID,
2063 sizeof(UnresolvedUsingTypenameDecl));
2064 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2065 SourceLocation(),
2066 NestedNameSpecifierLoc(),
2067 SourceLocation(),
2068 0);
2069}
2070
David Blaikie99ba9e32011-12-20 02:48:34 +00002071void StaticAssertDecl::anchor() { }
2072
Anders Carlssonfb311762009-03-14 00:25:26 +00002073StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002074 SourceLocation StaticAssertLoc,
2075 Expr *AssertExpr,
2076 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002077 SourceLocation RParenLoc,
2078 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002079 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002080 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00002081}
2082
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002083StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2084 unsigned ID) {
2085 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00002086 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
2087 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002088}
2089
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002090static const char *getAccessName(AccessSpecifier AS) {
2091 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002092 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002093 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002094 case AS_public:
2095 return "public";
2096 case AS_private:
2097 return "private";
2098 case AS_protected:
2099 return "protected";
2100 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002101 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002102}
2103
2104const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2105 AccessSpecifier AS) {
2106 return DB << getAccessName(AS);
2107}
Richard Smithf15fda02012-02-02 01:16:57 +00002108
2109const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2110 AccessSpecifier AS) {
2111 return DB << getAccessName(AS);
2112}