blob: 2c7c4d335d1c71cc0784226215bbcd4143b2fe4e [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
15#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000016#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000017#include "clang/AST/CXXInheritance.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/AST/DeclTemplate.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregor76852c22011-11-01 01:16:03 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000021#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000022#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000023#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Decl Allocation/Deallocation Method Implementations
29//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000030
David Blaikie99ba9e32011-12-20 02:48:34 +000031void AccessSpecDecl::anchor() { }
32
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000033AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
34 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(AccessSpecDecl));
35 return new (Mem) AccessSpecDecl(EmptyShell());
36}
37
John McCall86ff3082010-02-04 22:26:26 +000038CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Richard Smith7d04d3a2012-11-30 05:11:39 +000039 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
Eli Friedman97c134e2009-08-15 22:23:00 +000040 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000041 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000042 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000043 HasMutableFields(false), HasOnlyCMembers(true),
Richard Smithd5bc8672012-12-08 02:01:17 +000044 HasInClassInitializer(false), HasUninitializedReferenceMember(false),
Richard Smithbc2a35d2012-12-08 08:32:28 +000045 NeedOverloadResolutionForMoveConstructor(false),
46 NeedOverloadResolutionForMoveAssignment(false),
47 NeedOverloadResolutionForDestructor(false),
48 DefaultedMoveConstructorIsDeleted(false),
49 DefaultedMoveAssignmentIsDeleted(false),
50 DefaultedDestructorIsDeleted(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000051 HasTrivialSpecialMembers(SMF_All),
Richard Smithac713512012-12-08 02:53:02 +000052 DeclaredNonTrivialSpecialMembers(0),
Richard Smith7d04d3a2012-11-30 05:11:39 +000053 HasIrrelevantDestructor(true),
Richard Smith61802452011-12-22 02:22:31 +000054 HasConstexprNonCopyMoveConstructor(false),
55 DefaultedDefaultConstructorIsConstexpr(true),
Richard Smith7d04d3a2012-11-30 05:11:39 +000056 HasConstexprDefaultConstructor(false),
Sean Hunt023df372011-05-09 18:22:59 +000057 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000058 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smithacf796b2012-11-28 06:23:12 +000059 ImplicitCopyConstructorHasConstParam(true),
60 ImplicitCopyAssignmentHasConstParam(true),
61 HasDeclaredCopyConstructorWithConstParam(false),
62 HasDeclaredCopyAssignmentWithConstParam(false),
63 FailedImplicitMoveConstructor(false), FailedImplicitMoveAssignment(false),
64 IsLambda(false), NumBases(0), NumVBases(0), Bases(), VBases(),
65 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000066}
67
Benjamin Krameree3096a2012-07-04 17:03:33 +000068CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
69 return Bases.get(Definition->getASTContext().getExternalSource());
70}
71
72CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
73 return VBases.get(Definition->getASTContext().getExternalSource());
74}
75
John McCall86ff3082010-02-04 22:26:26 +000076CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000077 SourceLocation StartLoc, SourceLocation IdLoc,
78 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
79 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000080 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000081 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000082
Jay Foad4ba2a172011-01-12 09:06:06 +000083CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000084 DeclContext *DC, SourceLocation StartLoc,
85 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000086 CXXRecordDecl* PrevDecl,
87 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000088 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
89 Id, PrevDecl);
Douglas Gregor6bd99292013-02-09 01:35:03 +000090 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump1eb44332009-09-09 15:08:12 +000091
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000092 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000093 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000094 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000095 return R;
96}
97
Douglas Gregorda8962a2012-02-13 15:44:47 +000098CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Eli Friedman8da8a662012-09-19 01:18:11 +000099 TypeSourceInfo *Info, SourceLocation Loc,
100 bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +0000101 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
102 0, 0);
103 R->IsBeingDefined = true;
Eli Friedman8da8a662012-09-19 01:18:11 +0000104 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info, Dependent);
Douglas Gregor6bd99292013-02-09 01:35:03 +0000105 R->MayHaveOutOfDateDef = false;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000106 C.getTypeDeclType(R, /*PrevDecl=*/0);
107 return R;
108}
109
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000110CXXRecordDecl *
111CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
112 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +0000113 CXXRecordDecl *R = new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0,
114 SourceLocation(), SourceLocation(),
115 0, 0);
116 R->MayHaveOutOfDateDef = false;
117 return R;
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000118}
119
Mike Stump1eb44332009-09-09 15:08:12 +0000120void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000121CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000122 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000123 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000124
Douglas Gregor7c789c12010-10-29 22:39:52 +0000125 if (!data().Bases.isOffset() && data().NumBases > 0)
126 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Richard Smithdd677232011-10-18 20:08:55 +0000128 if (NumBases) {
129 // C++ [dcl.init.aggr]p1:
130 // An aggregate is [...] a class with [...] no base classes [...].
131 data().Aggregate = false;
132
133 // C++ [class]p4:
134 // A POD-struct is an aggregate class...
135 data().PlainOldData = false;
136 }
137
Anders Carlsson6f6de732010-03-29 05:13:12 +0000138 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000139 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000140
141 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000142 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000143
John McCall86ff3082010-02-04 22:26:26 +0000144 data().Bases = new(C) CXXBaseSpecifier [NumBases];
145 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000146 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000147 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000148 // Keep track of inherited vbases for this base class.
149 const CXXBaseSpecifier *Base = Bases[i];
150 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000151 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000152 if (BaseType->isDependentType())
153 continue;
154 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000155 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000156
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000157 // A class with a non-empty base class is not empty.
158 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000159 if (!BaseClassDecl->isEmpty()) {
160 if (!data().Empty) {
161 // C++0x [class]p7:
162 // A standard-layout class is a class that:
163 // [...]
164 // -- either has no non-static data members in the most derived
165 // class and at most one base class with non-static data members,
166 // or has no base classes with non-static data members, and
167 // If this is the second non-empty base, then neither of these two
168 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000169 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000170 }
171
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000172 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000173 data().HasNoNonEmptyBases = false;
174 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000175
Douglas Gregor85606eb2010-09-28 20:50:54 +0000176 // C++ [class.virtual]p1:
177 // A class that declares or inherits a virtual function is called a
178 // polymorphic class.
179 if (BaseClassDecl->isPolymorphic())
180 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000181
Chandler Carrutha8225442011-04-30 09:17:45 +0000182 // C++0x [class]p7:
183 // A standard-layout class is a class that: [...]
184 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000185 if (!BaseClassDecl->isStandardLayout())
186 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000187
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000188 // Record if this base is the first non-literal field or base.
189 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
190 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000191
Anders Carlsson6f6de732010-03-29 05:13:12 +0000192 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000193 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000194 BaseClassDecl->vbases_begin(),
195 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000196 // Add this base if it's not already in the list.
Richard Smithacf796b2012-11-28 06:23:12 +0000197 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000198 VBases.push_back(VBase);
Richard Smithacf796b2012-11-28 06:23:12 +0000199
200 // C++11 [class.copy]p8:
201 // The implicitly-declared copy constructor for a class X will have
202 // the form 'X::X(const X&)' if each [...] virtual base class B of X
203 // has a copy constructor whose first parameter is of type
204 // 'const B&' or 'const volatile B&' [...]
205 if (CXXRecordDecl *VBaseDecl = VBase->getType()->getAsCXXRecordDecl())
206 if (!VBaseDecl->hasCopyConstructorWithConstParam())
207 data().ImplicitCopyConstructorHasConstParam = false;
208 }
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000209 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000210
211 if (Base->isVirtual()) {
212 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000213 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Richard Smithacf796b2012-11-28 06:23:12 +0000214 VBases.push_back(Base);
215
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000216 // C++0x [meta.unary.prop] is_empty:
217 // T is a class type, but not a union type, with ... no virtual base
218 // classes
219 data().Empty = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000220
Richard Smith7d04d3a2012-11-30 05:11:39 +0000221 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
222 // A [default constructor, copy/move constructor, or copy/move assignment
223 // operator for a class X] is trivial [...] if:
224 // -- class X has [...] no virtual base classes
225 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carrutha8225442011-04-30 09:17:45 +0000226
227 // C++0x [class]p7:
228 // A standard-layout class is a class that: [...]
229 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000230 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000231
232 // C++11 [dcl.constexpr]p4:
233 // In the definition of a constexpr constructor [...]
234 // -- the class shall not have any virtual base classes
235 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000236 } else {
237 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000238 // A default constructor is trivial [...] if:
239 // -- all the direct base classes of its class have trivial default
240 // constructors.
241 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000242 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
243
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000244 // C++0x [class.copy]p13:
245 // A copy/move constructor for class X is trivial if [...]
246 // [...]
247 // -- the constructor selected to copy/move each direct base class
248 // subobject is trivial, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000249 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000250 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000251 // If the base class doesn't have a simple move constructor, we'll eagerly
252 // declare it and perform overload resolution to determine which function
253 // it actually calls. If it does have a simple move constructor, this
254 // check is correct.
Richard Smith426391c2012-11-16 00:53:38 +0000255 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000256 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000257
258 // C++0x [class.copy]p27:
259 // A copy/move assignment operator for class X is trivial if [...]
260 // [...]
261 // -- the assignment operator selected to copy/move each direct base
262 // class subobject is trivial, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000263 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000264 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000265 // If the base class doesn't have a simple move assignment, we'll eagerly
266 // declare it and perform overload resolution to determine which function
267 // it actually calls. If it does have a simple move assignment, this
268 // check is correct.
Richard Smith426391c2012-11-16 00:53:38 +0000269 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000270 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smith61802452011-12-22 02:22:31 +0000271
272 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000273 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000274 // requirements of a constexpr constructor, the implicitly-defined
275 // default constructor is constexpr.
276 if (!BaseClassDecl->hasConstexprDefaultConstructor())
277 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000278 }
Richard Smithac713512012-12-08 02:53:02 +0000279
Douglas Gregor85606eb2010-09-28 20:50:54 +0000280 // C++ [class.ctor]p3:
281 // A destructor is trivial if all the direct base classes of its class
282 // have trivial destructors.
283 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000284 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000285
286 if (!BaseClassDecl->hasIrrelevantDestructor())
287 data().HasIrrelevantDestructor = false;
288
Richard Smithacf796b2012-11-28 06:23:12 +0000289 // C++11 [class.copy]p18:
290 // The implicitly-declared copy assignment oeprator for a class X will
291 // have the form 'X& X::operator=(const X&)' if each direct base class B
292 // of X has a copy assignment operator whose parameter is of type 'const
293 // B&', 'const volatile B&', or 'B' [...]
294 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
295 data().ImplicitCopyAssignmentHasConstParam = false;
296
297 // C++11 [class.copy]p8:
298 // The implicitly-declared copy constructor for a class X will have
299 // the form 'X::X(const X&)' if each direct [...] base class B of X
300 // has a copy constructor whose first parameter is of type
301 // 'const B&' or 'const volatile B&' [...]
302 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
303 data().ImplicitCopyConstructorHasConstParam = false;
304
John McCallf85e1932011-06-15 23:02:42 +0000305 // A class has an Objective-C object member if... or any of its bases
306 // has an Objective-C object member.
307 if (BaseClassDecl->hasObjectMember())
308 setHasObjectMember(true);
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +0000309
310 if (BaseClassDecl->hasVolatileMember())
311 setHasVolatileMember(true);
John McCallf85e1932011-06-15 23:02:42 +0000312
Douglas Gregor2bb11012011-05-13 01:05:07 +0000313 // Keep track of the presence of mutable fields.
314 if (BaseClassDecl->hasMutableFields())
315 data().HasMutableFields = true;
Richard Smithd5bc8672012-12-08 02:01:17 +0000316
317 if (BaseClassDecl->hasUninitializedReferenceMember())
318 data().HasUninitializedReferenceMember = true;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000319
320 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000321 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000322
323 if (VBases.empty())
324 return;
325
326 // Create base specifier for any direct or indirect virtual bases.
327 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
328 data().NumVBases = VBases.size();
Richard Smithbc2a35d2012-12-08 08:32:28 +0000329 for (int I = 0, E = VBases.size(); I != E; ++I) {
330 QualType Type = VBases[I]->getType();
331 if (!Type->isDependentType())
332 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000333 data().getVBases()[I] = *VBases[I];
Richard Smithbc2a35d2012-12-08 08:32:28 +0000334 }
335}
336
337void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
338 // C++11 [class.copy]p11:
339 // A defaulted copy/move constructor for a class X is defined as
340 // deleted if X has:
341 // -- a direct or virtual base class B that cannot be copied/moved [...]
342 // -- a non-static data member of class type M (or array thereof)
343 // that cannot be copied or moved [...]
344 if (!Subobj->hasSimpleMoveConstructor())
345 data().NeedOverloadResolutionForMoveConstructor = true;
346
347 // C++11 [class.copy]p23:
348 // A defaulted copy/move assignment operator for a class X is defined as
349 // deleted if X has:
350 // -- a direct or virtual base class B that cannot be copied/moved [...]
351 // -- a non-static data member of class type M (or array thereof)
352 // that cannot be copied or moved [...]
353 if (!Subobj->hasSimpleMoveAssignment())
354 data().NeedOverloadResolutionForMoveAssignment = true;
355
356 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
357 // A defaulted [ctor or dtor] for a class X is defined as
358 // deleted if X has:
359 // -- any direct or virtual base class [...] has a type with a destructor
360 // that is deleted or inaccessible from the defaulted [ctor or dtor].
361 // -- any non-static data member has a type with a destructor
362 // that is deleted or inaccessible from the defaulted [ctor or dtor].
363 if (!Subobj->hasSimpleDestructor()) {
364 data().NeedOverloadResolutionForMoveConstructor = true;
365 data().NeedOverloadResolutionForDestructor = true;
366 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000367}
368
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000369/// Callback function for CXXRecordDecl::forallBases that acknowledges
370/// that it saw a base class.
371static bool SawBase(const CXXRecordDecl *, void *) {
372 return true;
373}
374
375bool CXXRecordDecl::hasAnyDependentBases() const {
376 if (!isDependentContext())
377 return false;
378
379 return !forallBases(SawBase, 0);
380}
381
Chandler Carruthb7e95892011-04-23 10:47:28 +0000382bool CXXRecordDecl::isTriviallyCopyable() const {
383 // C++0x [class]p5:
384 // A trivially copyable class is a class that:
385 // -- has no non-trivial copy constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000386 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000387 // -- has no non-trivial move constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000388 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000389 // -- has no non-trivial copy assignment operators,
Richard Smith426391c2012-11-16 00:53:38 +0000390 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000391 // -- has no non-trivial move assignment operators, and
Richard Smith426391c2012-11-16 00:53:38 +0000392 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000393 // -- has a trivial destructor.
394 if (!hasTrivialDestructor()) return false;
395
396 return true;
397}
398
Douglas Gregor21386642010-09-28 21:55:22 +0000399void CXXRecordDecl::markedVirtualFunctionPure() {
400 // C++ [class.abstract]p2:
401 // A class is abstract if it has at least one pure virtual function.
402 data().Abstract = true;
403}
404
405void CXXRecordDecl::addedMember(Decl *D) {
Joao Matos17d35c32012-08-31 22:18:20 +0000406 if (!D->isImplicit() &&
407 !isa<FieldDecl>(D) &&
408 !isa<IndirectFieldDecl>(D) &&
409 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
410 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
411 data().HasOnlyCMembers = false;
412
413 // Ignore friends and invalid declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000414 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000415 return;
416
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000417 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
418 if (FunTmpl)
419 D = FunTmpl->getTemplatedDecl();
420
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000421 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
422 if (Method->isVirtual()) {
423 // C++ [dcl.init.aggr]p1:
424 // An aggregate is an array or a class with [...] no virtual functions.
425 data().Aggregate = false;
426
427 // C++ [class]p4:
428 // A POD-struct is an aggregate class...
429 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000430
431 // Virtual functions make the class non-empty.
432 // FIXME: Standard ref?
433 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000434
435 // C++ [class.virtual]p1:
436 // A class that declares or inherits a virtual function is called a
437 // polymorphic class.
438 data().Polymorphic = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000439
Richard Smith7d04d3a2012-11-30 05:11:39 +0000440 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
441 // A [default constructor, copy/move constructor, or copy/move
442 // assignment operator for a class X] is trivial [...] if:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000443 // -- class X has no virtual functions [...]
Richard Smith7d04d3a2012-11-30 05:11:39 +0000444 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000445
Chandler Carrutha8225442011-04-30 09:17:45 +0000446 // C++0x [class]p7:
447 // A standard-layout class is a class that: [...]
448 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000449 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000450 }
451 }
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000452
Richard Smithacf796b2012-11-28 06:23:12 +0000453 // Notify the listener if an implicit member was added after the definition
454 // was completed.
455 if (!isBeingDefined() && D->isImplicit())
456 if (ASTMutationListener *L = getASTMutationListener())
457 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000458
Richard Smith7d04d3a2012-11-30 05:11:39 +0000459 // The kind of special member this declaration is, if any.
460 unsigned SMKind = 0;
461
Richard Smithacf796b2012-11-28 06:23:12 +0000462 // Handle constructors.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000463 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smithacf796b2012-11-28 06:23:12 +0000464 if (!Constructor->isImplicit()) {
465 // Note that we have a user-declared constructor.
466 data().UserDeclaredConstructor = true;
467
468 // C++ [class]p4:
469 // A POD-struct is an aggregate class [...]
470 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
471 // type is technically an aggregate in C++0x since it wouldn't be in 03.
472 data().PlainOldData = false;
473 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000474
Richard Smith017ab772011-09-05 02:13:09 +0000475 // Technically, "user-provided" is only defined for special member
476 // functions, but the intent of the standard is clearly that it should apply
477 // to all functions.
478 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000479
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000480 if (Constructor->isDefaultConstructor()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000481 SMKind |= SMF_DefaultConstructor;
482
483 if (UserProvided)
Sean Huntcdee3fe2011-05-11 22:34:38 +0000484 data().UserProvidedDefaultConstructor = true;
Richard Smithacf796b2012-11-28 06:23:12 +0000485 if (Constructor->isConstexpr())
Richard Smith61802452011-12-22 02:22:31 +0000486 data().HasConstexprDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000487 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000488
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000489 if (!FunTmpl) {
Richard Smithacf796b2012-11-28 06:23:12 +0000490 unsigned Quals;
491 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000492 SMKind |= SMF_CopyConstructor;
Richard Smithacf796b2012-11-28 06:23:12 +0000493
494 if (Quals & Qualifiers::Const)
495 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith7d04d3a2012-11-30 05:11:39 +0000496 } else if (Constructor->isMoveConstructor())
497 SMKind |= SMF_MoveConstructor;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000498 }
Richard Smithacf796b2012-11-28 06:23:12 +0000499
500 // Record if we see any constexpr constructors which are neither copy
501 // nor move constructors.
502 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith6b8bc072011-08-10 18:11:37 +0000503 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000504
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000505 // C++ [dcl.init.aggr]p1:
506 // An aggregate is an array or a class with no user-declared
507 // constructors [...].
508 // C++0x [dcl.init.aggr]p1:
509 // An aggregate is an array or a class with no user-provided
510 // constructors [...].
Richard Smith80ad52f2013-01-02 11:42:31 +0000511 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smithacf796b2012-11-28 06:23:12 +0000512 ? UserProvided : !Constructor->isImplicit())
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000513 data().Aggregate = false;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000514 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000515
Richard Smithacf796b2012-11-28 06:23:12 +0000516 // Handle destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000517 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000518 SMKind |= SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000519
Richard Smith7d04d3a2012-11-30 05:11:39 +0000520 if (!DD->isImplicit())
Richard Smithacf796b2012-11-28 06:23:12 +0000521 data().HasIrrelevantDestructor = false;
522
Richard Smithacf796b2012-11-28 06:23:12 +0000523 // C++11 [class.dtor]p5:
Richard Smith7d04d3a2012-11-30 05:11:39 +0000524 // A destructor is trivial if [...] the destructor is not virtual.
525 if (DD->isVirtual())
526 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000527 }
Richard Smithacf796b2012-11-28 06:23:12 +0000528
529 // Handle member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000530 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000531 if (Method->isCopyAssignmentOperator()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000532 SMKind |= SMF_CopyAssignment;
Richard Smithacf796b2012-11-28 06:23:12 +0000533
534 const ReferenceType *ParamTy =
535 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
536 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
537 data().HasDeclaredCopyAssignmentWithConstParam = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000538 }
Sean Huntffe37fd2011-05-25 20:50:04 +0000539
Richard Smith7d04d3a2012-11-30 05:11:39 +0000540 if (Method->isMoveAssignmentOperator())
541 SMKind |= SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000542
Douglas Gregore80622f2010-09-29 04:25:11 +0000543 // Keep the list of conversion functions up-to-date.
544 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor109f5fc2013-04-08 17:12:58 +0000545 // FIXME: We use the 'unsafe' accessor for the access specifier here,
546 // because Sema may not have set it yet. That's really just a misdesign
547 // in Sema. However, LLDB *will* have set the access specifier correctly,
548 // and adds declarations after the class is technically completed,
549 // so completeDefinition()'s overriding of the access specifiers doesn't
550 // work.
551 AccessSpecifier AS = Conversion->getAccessUnsafe();
552
Richard Smith7d04d3a2012-11-30 05:11:39 +0000553 if (Conversion->getPrimaryTemplate()) {
554 // We don't record specializations.
555 } else if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000556 if (FunTmpl->getPreviousDecl())
557 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregor109f5fc2013-04-08 17:12:58 +0000558 FunTmpl, AS);
Douglas Gregore80622f2010-09-29 04:25:11 +0000559 else
Douglas Gregor109f5fc2013-04-08 17:12:58 +0000560 data().Conversions.addDecl(getASTContext(), FunTmpl, AS);
Douglas Gregore80622f2010-09-29 04:25:11 +0000561 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000562 if (Conversion->getPreviousDecl())
563 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregor109f5fc2013-04-08 17:12:58 +0000564 Conversion, AS);
Douglas Gregore80622f2010-09-29 04:25:11 +0000565 else
Douglas Gregor109f5fc2013-04-08 17:12:58 +0000566 data().Conversions.addDecl(getASTContext(), Conversion, AS);
Douglas Gregore80622f2010-09-29 04:25:11 +0000567 }
568 }
Richard Smithacf796b2012-11-28 06:23:12 +0000569
Richard Smith7d04d3a2012-11-30 05:11:39 +0000570 if (SMKind) {
Richard Smithac713512012-12-08 02:53:02 +0000571 // If this is the first declaration of a special member, we no longer have
572 // an implicit trivial special member.
573 data().HasTrivialSpecialMembers &=
574 data().DeclaredSpecialMembers | ~SMKind;
575
576 if (!Method->isImplicit() && !Method->isUserProvided()) {
577 // This method is user-declared but not user-provided. We can't work out
578 // whether it's trivial yet (not until we get to the end of the class).
579 // We'll handle this method in finishedDefaultedOrDeletedMember.
580 } else if (Method->isTrivial())
581 data().HasTrivialSpecialMembers |= SMKind;
582 else
583 data().DeclaredNonTrivialSpecialMembers |= SMKind;
584
Richard Smith7d04d3a2012-11-30 05:11:39 +0000585 // Note when we have declared a declared special member, and suppress the
586 // implicit declaration of this special member.
587 data().DeclaredSpecialMembers |= SMKind;
588
589 if (!Method->isImplicit()) {
590 data().UserDeclaredSpecialMembers |= SMKind;
591
592 // C++03 [class]p4:
593 // A POD-struct is an aggregate class that has [...] no user-defined
594 // copy assignment operator and no user-defined destructor.
595 //
596 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
597 // aggregates could not have any constructors, clear it even for an
598 // explicitly defaulted or deleted constructor.
599 // type is technically an aggregate in C++0x since it wouldn't be in 03.
600 //
601 // Also, a user-declared move assignment operator makes a class non-POD.
602 // This is an extension in C++03.
603 data().PlainOldData = false;
604 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000605 }
606
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000607 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000608 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000609
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000610 // Handle non-static data members.
611 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000612 // C++ [class.bit]p2:
613 // A declaration for a bit-field that omits the identifier declares an
614 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
615 // initialized.
616 if (Field->isUnnamedBitfield())
617 return;
618
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000619 // C++ [dcl.init.aggr]p1:
620 // An aggregate is an array or a class (clause 9) with [...] no
621 // private or protected non-static data members (clause 11).
622 //
623 // A POD must be an aggregate.
624 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
625 data().Aggregate = false;
626 data().PlainOldData = false;
627 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000628
629 // C++0x [class]p7:
630 // A standard-layout class is a class that:
631 // [...]
632 // -- has the same access control for all non-static data members,
633 switch (D->getAccess()) {
634 case AS_private: data().HasPrivateFields = true; break;
635 case AS_protected: data().HasProtectedFields = true; break;
636 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000637 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000638 };
639 if ((data().HasPrivateFields + data().HasProtectedFields +
640 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000641 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000642
Douglas Gregor2bb11012011-05-13 01:05:07 +0000643 // Keep track of the presence of mutable fields.
644 if (Field->isMutable())
645 data().HasMutableFields = true;
646
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000647 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000648 // A POD struct is a class that is both a trivial class and a
649 // standard-layout class, and has no non-static data members of type
650 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000651 //
652 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
653 // that does not explicitly have no lifetime makes the class a non-POD.
654 // However, we delay setting PlainOldData to false in this case so that
655 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000656 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000657 // In this case, the class will become a non-POD class when we complete
658 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000659 ASTContext &Context = getASTContext();
660 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000661 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000662 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000663 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
664 setHasObjectMember(true);
665 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000666 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000667
Chandler Carrutha8225442011-04-30 09:17:45 +0000668 if (T->isReferenceType()) {
Richard Smithd5bc8672012-12-08 02:01:17 +0000669 if (!Field->hasInClassInitializer())
670 data().HasUninitializedReferenceMember = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000671
Chandler Carrutha8225442011-04-30 09:17:45 +0000672 // C++0x [class]p7:
673 // A standard-layout class is a class that:
674 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000675 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000676 }
677
Richard Smith86c3ae42012-02-13 03:54:03 +0000678 // Record if this field is the first non-literal or volatile field or base.
679 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000680 data().HasNonLiteralTypeFieldsOrBases = true;
681
Richard Smith7a614d82011-06-11 17:19:42 +0000682 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000683 data().HasInClassInitializer = true;
684
685 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000686 // A default constructor is trivial if [...] no non-static data member
687 // of its class has a brace-or-equal-initializer.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000688 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith7a614d82011-06-11 17:19:42 +0000689
Richard Smithd079abf2012-05-07 01:07:30 +0000690 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000691 // An aggregate is a [...] class with [...] no
692 // brace-or-equal-initializers for non-static data members.
693 data().Aggregate = false;
694
Richard Smithd079abf2012-05-07 01:07:30 +0000695 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000696 // A POD struct is [...] a trivial class.
697 data().PlainOldData = false;
698 }
699
Richard Smithbc2a35d2012-12-08 08:32:28 +0000700 // C++11 [class.copy]p23:
701 // A defaulted copy/move assignment operator for a class X is defined
702 // as deleted if X has:
703 // -- a non-static data member of reference type
704 if (T->isReferenceType())
705 data().DefaultedMoveAssignmentIsDeleted = true;
706
Douglas Gregor85606eb2010-09-28 20:50:54 +0000707 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
708 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
709 if (FieldRec->getDefinition()) {
Richard Smithbc2a35d2012-12-08 08:32:28 +0000710 addedClassSubobject(FieldRec);
711
712 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
713 // A defaulted [special member] for a class X is defined as
714 // deleted if:
715 // -- X is a union-like class that has a variant member with a
716 // non-trivial [corresponding special member]
717 if (isUnion()) {
718 if (FieldRec->hasNonTrivialMoveConstructor())
719 data().DefaultedMoveConstructorIsDeleted = true;
720 if (FieldRec->hasNonTrivialMoveAssignment())
721 data().DefaultedMoveAssignmentIsDeleted = true;
722 if (FieldRec->hasNonTrivialDestructor())
723 data().DefaultedDestructorIsDeleted = true;
724 }
725
Sean Hunt023df372011-05-09 18:22:59 +0000726 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000727 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000728 // -- for all the non-static data members of its class that are of
729 // class type (or array thereof), each such class has a trivial
730 // default constructor.
731 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000732 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000733
734 // C++0x [class.copy]p13:
735 // A copy/move constructor for class X is trivial if [...]
736 // [...]
737 // -- for each non-static data member of X that is of class type (or
738 // an array thereof), the constructor selected to copy/move that
739 // member is trivial;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000740 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000741 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000742 // If the field doesn't have a simple move constructor, we'll eagerly
743 // declare the move constructor for this class and we'll decide whether
744 // it's trivial then.
Richard Smith426391c2012-11-16 00:53:38 +0000745 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000746 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000747
748 // C++0x [class.copy]p27:
749 // A copy/move assignment operator for class X is trivial if [...]
750 // [...]
751 // -- for each non-static data member of X that is of class type (or
752 // an array thereof), the assignment operator selected to
753 // copy/move that member is trivial;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000754 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000755 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000756 // If the field doesn't have a simple move assignment, we'll eagerly
757 // declare the move assignment for this class and we'll decide whether
758 // it's trivial then.
Richard Smith426391c2012-11-16 00:53:38 +0000759 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000760 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000761
Douglas Gregor85606eb2010-09-28 20:50:54 +0000762 if (!FieldRec->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000763 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000764 if (!FieldRec->hasIrrelevantDestructor())
765 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000766 if (FieldRec->hasObjectMember())
767 setHasObjectMember(true);
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +0000768 if (FieldRec->hasVolatileMember())
769 setHasVolatileMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000770
771 // C++0x [class]p7:
772 // A standard-layout class is a class that:
773 // -- has no non-static data members of type non-standard-layout
774 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000775 if (!FieldRec->isStandardLayout())
776 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000777
778 // C++0x [class]p7:
779 // A standard-layout class is a class that:
780 // [...]
781 // -- has no base classes of the same type as the first non-static
782 // data member.
783 // We don't want to expend bits in the state of the record decl
784 // tracking whether this is the first non-static data member so we
785 // cheat a bit and use some of the existing state: the empty bit.
786 // Virtual bases and virtual methods make a class non-empty, but they
787 // also make it non-standard-layout so we needn't check here.
788 // A non-empty base class may leave the class standard-layout, but not
789 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000790 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000791 // data member must come through here with Empty still true, and Empty
792 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000793 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000794 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
795 BE = bases_end();
796 BI != BE; ++BI) {
797 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000798 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000799 break;
800 }
801 }
802 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000803
804 // Keep track of the presence of mutable fields.
805 if (FieldRec->hasMutableFields())
806 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000807
808 // C++11 [class.copy]p13:
809 // If the implicitly-defined constructor would satisfy the
810 // requirements of a constexpr constructor, the implicitly-defined
811 // constructor is constexpr.
812 // C++11 [dcl.constexpr]p4:
813 // -- every constructor involved in initializing non-static data
814 // members [...] shall be a constexpr constructor
815 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000816 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000817 // The standard requires any in-class initializer to be a constant
818 // expression. We consider this to be a defect.
819 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithacf796b2012-11-28 06:23:12 +0000820
821 // C++11 [class.copy]p8:
822 // The implicitly-declared copy constructor for a class X will have
823 // the form 'X::X(const X&)' if [...] for all the non-static data
824 // members of X that are of a class type M (or array thereof), each
825 // such class type has a copy constructor whose first parameter is
826 // of type 'const M&' or 'const volatile M&'.
827 if (!FieldRec->hasCopyConstructorWithConstParam())
828 data().ImplicitCopyConstructorHasConstParam = false;
829
830 // C++11 [class.copy]p18:
831 // The implicitly-declared copy assignment oeprator for a class X will
832 // have the form 'X& X::operator=(const X&)' if [...] for all the
833 // non-static data members of X that are of a class type M (or array
834 // thereof), each such class type has a copy assignment operator whose
835 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
836 if (!FieldRec->hasCopyAssignmentWithConstParam())
837 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smithd5bc8672012-12-08 02:01:17 +0000838
839 if (FieldRec->hasUninitializedReferenceMember() &&
840 !Field->hasInClassInitializer())
841 data().HasUninitializedReferenceMember = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000842 }
Richard Smith61802452011-12-22 02:22:31 +0000843 } else {
844 // Base element type of field is a non-class type.
Richard Smithd3861ce2012-06-10 07:07:24 +0000845 if (!T->isLiteralType() ||
846 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000847 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000848
849 // C++11 [class.copy]p23:
850 // A defaulted copy/move assignment operator for a class X is defined
851 // as deleted if X has:
852 // -- a non-static data member of const non-class type (or array
853 // thereof)
854 if (T.isConstQualified())
855 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000856 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000857
858 // C++0x [class]p7:
859 // A standard-layout class is a class that:
860 // [...]
861 // -- either has no non-static data members in the most derived
862 // class and at most one base class with non-static data members,
863 // or has no base classes with non-static data members, and
864 // At this point we know that we have a non-static data member, so the last
865 // clause holds.
866 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000867 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000868
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000869 // If this is not a zero-length bit-field, then the class is not empty.
870 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000871 if (!Field->isBitField() ||
872 (!Field->getBitWidth()->isTypeDependent() &&
873 !Field->getBitWidth()->isValueDependent() &&
874 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000875 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000876 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000877 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000878
879 // Handle using declarations of conversion functions.
880 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
881 if (Shadow->getDeclName().getNameKind()
882 == DeclarationName::CXXConversionFunctionName)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000883 data().Conversions.addDecl(getASTContext(), Shadow, Shadow->getAccess());
Joao Matos17d35c32012-08-31 22:18:20 +0000884}
885
Richard Smithac713512012-12-08 02:53:02 +0000886void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
887 assert(!D->isImplicit() && !D->isUserProvided());
888
889 // The kind of special member this declaration is, if any.
890 unsigned SMKind = 0;
891
892 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
893 if (Constructor->isDefaultConstructor()) {
894 SMKind |= SMF_DefaultConstructor;
895 if (Constructor->isConstexpr())
896 data().HasConstexprDefaultConstructor = true;
897 }
898 if (Constructor->isCopyConstructor())
899 SMKind |= SMF_CopyConstructor;
900 else if (Constructor->isMoveConstructor())
901 SMKind |= SMF_MoveConstructor;
902 else if (Constructor->isConstexpr())
903 // We may now know that the constructor is constexpr.
904 data().HasConstexprNonCopyMoveConstructor = true;
905 } else if (isa<CXXDestructorDecl>(D))
906 SMKind |= SMF_Destructor;
907 else if (D->isCopyAssignmentOperator())
908 SMKind |= SMF_CopyAssignment;
909 else if (D->isMoveAssignmentOperator())
910 SMKind |= SMF_MoveAssignment;
911
912 // Update which trivial / non-trivial special members we have.
913 // addedMember will have skipped this step for this member.
914 if (D->isTrivial())
915 data().HasTrivialSpecialMembers |= SMKind;
916 else
917 data().DeclaredNonTrivialSpecialMembers |= SMKind;
918}
919
Joao Matos17d35c32012-08-31 22:18:20 +0000920bool CXXRecordDecl::isCLike() const {
921 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
922 !TemplateOrInstantiation.isNull())
923 return false;
924 if (!hasDefinition())
925 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000926
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000927 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000928}
929
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000930void CXXRecordDecl::getCaptureFields(
931 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000932 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000933 Captures.clear();
934 ThisCapture = 0;
935
Douglas Gregorda8962a2012-02-13 15:44:47 +0000936 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000937 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000938 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000939 C != CEnd; ++C, ++Field) {
940 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +0000941 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000942 continue;
943 }
944
David Blaikie581deb32012-06-06 20:45:41 +0000945 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000946 }
947}
948
949
John McCallb05b5f32010-03-15 09:07:48 +0000950static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
951 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000952 if (isa<UsingShadowDecl>(Conv))
953 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000954 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
955 T = ConvTemp->getTemplatedDecl()->getResultType();
956 else
957 T = cast<CXXConversionDecl>(Conv)->getConversionType();
958 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000959}
960
John McCallb05b5f32010-03-15 09:07:48 +0000961/// Collect the visible conversions of a base class.
962///
James Dennetta1253502012-06-15 22:28:09 +0000963/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +0000964/// \param InVirtual whether this base class is a virtual base (or a base
965/// of a virtual base)
966/// \param Access the access along the inheritance path to this base
967/// \param ParentHiddenTypes the conversions provided by the inheritors
968/// of this base
969/// \param Output the set to which to add conversions from non-virtual bases
970/// \param VOutput the set to which to add conversions from virtual bases
971/// \param HiddenVBaseCs the set of conversions which were hidden in a
972/// virtual base along some inheritance path
973static void CollectVisibleConversions(ASTContext &Context,
974 CXXRecordDecl *Record,
975 bool InVirtual,
976 AccessSpecifier Access,
977 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000978 ASTUnresolvedSet &Output,
John McCallb05b5f32010-03-15 09:07:48 +0000979 UnresolvedSetImpl &VOutput,
980 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
981 // The set of types which have conversions in this class or its
982 // subclasses. As an optimization, we don't copy the derived set
983 // unless it might change.
984 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
985 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
986
987 // Collect the direct conversions and figure out which conversions
988 // will be hidden in the subclasses.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000989 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
990 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
991 if (ConvI != ConvE) {
John McCallb05b5f32010-03-15 09:07:48 +0000992 HiddenTypesBuffer = ParentHiddenTypes;
993 HiddenTypes = &HiddenTypesBuffer;
994
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000995 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +0000996 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
997 bool Hidden = ParentHiddenTypes.count(ConvType);
998 if (!Hidden)
999 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001000
1001 // If this conversion is hidden and we're in a virtual base,
1002 // remember that it's hidden along some inheritance path.
1003 if (Hidden && InVirtual)
1004 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1005
1006 // If this conversion isn't hidden, add it to the appropriate output.
1007 else if (!Hidden) {
1008 AccessSpecifier IAccess
1009 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1010
1011 if (InVirtual)
1012 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001013 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001014 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001015 }
1016 }
1017 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001018
John McCallb05b5f32010-03-15 09:07:48 +00001019 // Collect information recursively from any base classes.
1020 for (CXXRecordDecl::base_class_iterator
1021 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1022 const RecordType *RT = I->getType()->getAs<RecordType>();
1023 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001024
John McCallb05b5f32010-03-15 09:07:48 +00001025 AccessSpecifier BaseAccess
1026 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1027 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001028
John McCallb05b5f32010-03-15 09:07:48 +00001029 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1030 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1031 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001032 }
John McCallb05b5f32010-03-15 09:07:48 +00001033}
Sebastian Redl9994a342009-10-25 17:03:50 +00001034
John McCallb05b5f32010-03-15 09:07:48 +00001035/// Collect the visible conversions of a class.
1036///
1037/// This would be extremely straightforward if it weren't for virtual
1038/// bases. It might be worth special-casing that, really.
1039static void CollectVisibleConversions(ASTContext &Context,
1040 CXXRecordDecl *Record,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001041 ASTUnresolvedSet &Output) {
John McCallb05b5f32010-03-15 09:07:48 +00001042 // The collection of all conversions in virtual bases that we've
1043 // found. These will be added to the output as long as they don't
1044 // appear in the hidden-conversions set.
1045 UnresolvedSet<8> VBaseCs;
1046
1047 // The set of conversions in virtual bases that we've determined to
1048 // be hidden.
1049 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1050
1051 // The set of types hidden by classes derived from this one.
1052 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1053
1054 // Go ahead and collect the direct conversions and add them to the
1055 // hidden-types set.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001056 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1057 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001058 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001059 for (; ConvI != ConvE; ++ConvI)
1060 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCallb05b5f32010-03-15 09:07:48 +00001061
1062 // Recursively collect conversions from base classes.
1063 for (CXXRecordDecl::base_class_iterator
1064 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1065 const RecordType *RT = I->getType()->getAs<RecordType>();
1066 if (!RT) continue;
1067
1068 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1069 I->isVirtual(), I->getAccessSpecifier(),
1070 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1071 }
1072
1073 // Add any unhidden conversions provided by virtual bases.
1074 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1075 I != E; ++I) {
1076 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001077 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001078 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001079}
1080
1081/// getVisibleConversionFunctions - get all conversion functions visible
1082/// in current class; including conversion function templates.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001083std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1084CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001085 // If root class, all conversions are visible.
1086 if (bases_begin() == bases_end())
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001087 return std::make_pair(data().Conversions.begin(), data().Conversions.end());
Fariborz Jahanian62509212009-09-12 18:26:03 +00001088 // If visible conversion list is already evaluated, return it.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001089 if (!data().ComputedVisibleConversions) {
1090 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
1091 data().ComputedVisibleConversions = true;
1092 }
1093 return std::make_pair(data().VisibleConversions.begin(),
1094 data().VisibleConversions.end());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001095}
1096
John McCall32daa422010-03-31 01:36:47 +00001097void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1098 // This operation is O(N) but extremely rare. Sema only uses it to
1099 // remove UsingShadowDecls in a class that were followed by a direct
1100 // declaration, e.g.:
1101 // class A : B {
1102 // using B::operator int;
1103 // operator int();
1104 // };
1105 // This is uncommon by itself and even more uncommon in conjunction
1106 // with sufficiently large numbers of directly-declared conversions
1107 // that asymptotic behavior matters.
1108
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001109 ASTUnresolvedSet &Convs = data().Conversions;
John McCall32daa422010-03-31 01:36:47 +00001110 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1111 if (Convs[I].getDecl() == ConvDecl) {
1112 Convs.erase(I);
1113 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1114 && "conversion was found multiple times in unresolved set");
1115 return;
1116 }
1117 }
1118
1119 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001120}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001121
Douglas Gregorf6b11852009-10-08 15:14:33 +00001122CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001123 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001124 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1125
1126 return 0;
1127}
1128
1129void
1130CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1131 TemplateSpecializationKind TSK) {
1132 assert(TemplateOrInstantiation.isNull() &&
1133 "Previous template or instantiation?");
1134 assert(!isa<ClassTemplateSpecializationDecl>(this));
1135 TemplateOrInstantiation
1136 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1137}
1138
Anders Carlssonb13e3572009-12-07 06:33:48 +00001139TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1140 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001141 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1142 return Spec->getSpecializationKind();
1143
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001144 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001145 return MSInfo->getTemplateSpecializationKind();
1146
1147 return TSK_Undeclared;
1148}
1149
1150void
1151CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1152 if (ClassTemplateSpecializationDecl *Spec
1153 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1154 Spec->setSpecializationKind(TSK);
1155 return;
1156 }
1157
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001158 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001159 MSInfo->setTemplateSpecializationKind(TSK);
1160 return;
1161 }
1162
David Blaikieb219cfc2011-09-23 05:06:16 +00001163 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001164}
1165
Douglas Gregor1d110e02010-07-01 14:13:13 +00001166CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1167 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001168 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001169
1170 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001171 = Context.DeclarationNames.getCXXDestructorName(
1172 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001173
David Blaikie3bc93e32012-12-19 00:45:41 +00001174 DeclContext::lookup_const_result R = lookup(Name);
1175 if (R.empty())
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001176 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001177
David Blaikie3bc93e32012-12-19 00:45:41 +00001178 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson7267c162009-05-29 21:03:38 +00001179 return Dtor;
1180}
1181
Douglas Gregorda2142f2011-02-19 18:51:44 +00001182void CXXRecordDecl::completeDefinition() {
1183 completeDefinition(0);
1184}
1185
1186void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1187 RecordDecl::completeDefinition();
1188
David Blaikie4e4d0842012-03-11 07:00:24 +00001189 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001190 // Objective-C Automatic Reference Counting:
1191 // If a class has a non-static data member of Objective-C pointer
1192 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001193 // default constructor (if any), copy constructor, move constructor,
1194 // copy assignment operator, move assignment operator, and destructor are
1195 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001196 struct DefinitionData &Data = data();
1197 Data.PlainOldData = false;
Richard Smith7d04d3a2012-11-30 05:11:39 +00001198 Data.HasTrivialSpecialMembers = 0;
Richard Smithdfefb842012-02-25 07:33:38 +00001199 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001200 }
1201
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001202 // If the class may be abstract (but hasn't been marked as such), check for
1203 // any pure final overriders.
1204 if (mayBeAbstract()) {
1205 CXXFinalOverriderMap MyFinalOverriders;
1206 if (!FinalOverriders) {
1207 getFinalOverriders(MyFinalOverriders);
1208 FinalOverriders = &MyFinalOverriders;
1209 }
1210
1211 bool Done = false;
1212 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1213 MEnd = FinalOverriders->end();
1214 M != MEnd && !Done; ++M) {
1215 for (OverridingMethods::iterator SO = M->second.begin(),
1216 SOEnd = M->second.end();
1217 SO != SOEnd && !Done; ++SO) {
1218 assert(SO->second.size() > 0 &&
1219 "All virtual functions have overridding virtual functions");
1220
1221 // C++ [class.abstract]p4:
1222 // A class is abstract if it contains or inherits at least one
1223 // pure virtual function for which the final overrider is pure
1224 // virtual.
1225 if (SO->second.front().Method->isPure()) {
1226 data().Abstract = true;
1227 Done = true;
1228 break;
1229 }
1230 }
1231 }
1232 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001233
1234 // Set access bits correctly on the directly-declared conversions.
1235 for (UnresolvedSetIterator I = data().Conversions.begin(),
1236 E = data().Conversions.end();
1237 I != E; ++I)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001238 I.setAccess((*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001239}
1240
1241bool CXXRecordDecl::mayBeAbstract() const {
1242 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1243 isDependentContext())
1244 return false;
1245
1246 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1247 BEnd = bases_end();
1248 B != BEnd; ++B) {
1249 CXXRecordDecl *BaseDecl
1250 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1251 if (BaseDecl->isAbstract())
1252 return true;
1253 }
1254
1255 return false;
1256}
1257
David Blaikie99ba9e32011-12-20 02:48:34 +00001258void CXXMethodDecl::anchor() { }
1259
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001260bool CXXMethodDecl::isStatic() const {
Rafael Espindola72fdc892013-04-15 12:38:20 +00001261 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001262
1263 if (MD->getStorageClass() == SC_Static)
1264 return true;
1265
1266 DeclarationName Name = getDeclName();
1267 // [class.free]p1:
1268 // Any allocation function for a class T is a static member
1269 // (even if not explicitly declared static).
1270 if (Name.getCXXOverloadedOperator() == OO_New ||
1271 Name.getCXXOverloadedOperator() == OO_Array_New)
1272 return true;
1273
1274 // [class.free]p6 Any deallocation function for a class X is a static member
1275 // (even if not explicitly declared static).
1276 if (Name.getCXXOverloadedOperator() == OO_Delete ||
1277 Name.getCXXOverloadedOperator() == OO_Array_Delete)
1278 return true;
1279
1280 return false;
1281}
1282
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001283static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1284 const CXXMethodDecl *BaseMD) {
1285 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1286 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1287 const CXXMethodDecl *MD = *I;
1288 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1289 return true;
1290 if (recursivelyOverrides(MD, BaseMD))
1291 return true;
1292 }
1293 return false;
1294}
1295
1296CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001297CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1298 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001299 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1300 return this;
1301
1302 // Lookup doesn't work for destructors, so handle them separately.
1303 if (isa<CXXDestructorDecl>(this)) {
1304 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001305 if (MD) {
1306 if (recursivelyOverrides(MD, this))
1307 return MD;
1308 if (MayBeBase && recursivelyOverrides(this, MD))
1309 return MD;
1310 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001311 return NULL;
1312 }
1313
1314 lookup_const_result Candidates = RD->lookup(getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +00001315 for (NamedDecl * const * I = Candidates.begin(); I != Candidates.end(); ++I) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001316 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1317 if (!MD)
1318 continue;
1319 if (recursivelyOverrides(MD, this))
1320 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001321 if (MayBeBase && recursivelyOverrides(this, MD))
1322 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001323 }
1324
1325 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1326 E = RD->bases_end(); I != E; ++I) {
1327 const RecordType *RT = I->getType()->getAs<RecordType>();
1328 if (!RT)
1329 continue;
1330 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1331 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1332 if (T)
1333 return T;
1334 }
1335
1336 return NULL;
1337}
1338
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001339CXXMethodDecl *
1340CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001341 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001342 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001343 QualType T, TypeSourceInfo *TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001344 StorageClass SC, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001345 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001346 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001347 SC, isInline, isConstexpr,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001348 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001349}
1350
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001351CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1352 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1353 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1354 DeclarationNameInfo(), QualType(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001355 0, SC_None, false, false,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001356 SourceLocation());
1357}
1358
Douglas Gregor90916562009-09-29 18:16:17 +00001359bool CXXMethodDecl::isUsualDeallocationFunction() const {
1360 if (getOverloadedOperator() != OO_Delete &&
1361 getOverloadedOperator() != OO_Array_Delete)
1362 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001363
1364 // C++ [basic.stc.dynamic.deallocation]p2:
1365 // A template instance is never a usual deallocation function,
1366 // regardless of its signature.
1367 if (getPrimaryTemplate())
1368 return false;
1369
Douglas Gregor90916562009-09-29 18:16:17 +00001370 // C++ [basic.stc.dynamic.deallocation]p2:
1371 // If a class T has a member deallocation function named operator delete
1372 // with exactly one parameter, then that function is a usual (non-placement)
1373 // deallocation function. [...]
1374 if (getNumParams() == 1)
1375 return true;
1376
1377 // C++ [basic.stc.dynamic.deallocation]p2:
1378 // [...] If class T does not declare such an operator delete but does
1379 // declare a member deallocation function named operator delete with
1380 // exactly two parameters, the second of which has type std::size_t (18.1),
1381 // then this function is a usual deallocation function.
1382 ASTContext &Context = getASTContext();
1383 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001384 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1385 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001386 return false;
1387
1388 // This function is a usual deallocation function if there are no
1389 // single-parameter deallocation functions of the same kind.
David Blaikie3bc93e32012-12-19 00:45:41 +00001390 DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1391 for (DeclContext::lookup_const_result::iterator I = R.begin(), E = R.end();
1392 I != E; ++I) {
1393 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregor90916562009-09-29 18:16:17 +00001394 if (FD->getNumParams() == 1)
1395 return false;
1396 }
1397
1398 return true;
1399}
1400
Douglas Gregor06a9f362010-05-01 20:49:11 +00001401bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001402 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001403 // A user-declared copy assignment operator X::operator= is a non-static
1404 // non-template member function of class X with exactly one parameter of
1405 // type X, X&, const X&, volatile X& or const volatile X&.
1406 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1407 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001408 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001409 return false;
1410
1411 QualType ParamType = getParamDecl(0)->getType();
1412 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1413 ParamType = Ref->getPointeeType();
1414
1415 ASTContext &Context = getASTContext();
1416 QualType ClassType
1417 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1418 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1419}
1420
Sean Huntffe37fd2011-05-25 20:50:04 +00001421bool CXXMethodDecl::isMoveAssignmentOperator() const {
1422 // C++0x [class.copy]p19:
1423 // A user-declared move assignment operator X::operator= is a non-static
1424 // non-template member function of class X with exactly one parameter of type
1425 // X&&, const X&&, volatile X&&, or const volatile X&&.
1426 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1427 getPrimaryTemplate() || getDescribedFunctionTemplate())
1428 return false;
1429
1430 QualType ParamType = getParamDecl(0)->getType();
1431 if (!isa<RValueReferenceType>(ParamType))
1432 return false;
1433 ParamType = ParamType->getPointeeType();
1434
1435 ASTContext &Context = getASTContext();
1436 QualType ClassType
1437 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1438 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1439}
1440
Anders Carlsson05eb2442009-05-16 23:58:37 +00001441void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001442 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001443 assert(!MD->getParent()->isDependentContext() &&
1444 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001445 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001446
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001447 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001448}
1449
1450CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001451 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001452 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001453}
1454
1455CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001456 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001457 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001458}
1459
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001460unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001461 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001462 return getASTContext().overridden_methods_size(this);
1463}
1464
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001465QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001466 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1467 // If the member function is declared const, the type of this is const X*,
1468 // if the member function is declared volatile, the type of this is
1469 // volatile X*, and if the member function is declared const volatile,
1470 // the type of this is const volatile X*.
1471
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001472 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001473
John McCall3cb0ebd2010-03-10 03:28:59 +00001474 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001475 ClassTy = C.getQualifiedType(ClassTy,
1476 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001477 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001478}
1479
Eli Friedmand7d7f672009-12-06 20:50:05 +00001480bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001481 // If this function is a template instantiation, look at the template from
1482 // which it was instantiated.
1483 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1484 if (!CheckFn)
1485 CheckFn = this;
1486
Eli Friedmand7d7f672009-12-06 20:50:05 +00001487 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001488 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001489}
1490
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001491bool CXXMethodDecl::isLambdaStaticInvoker() const {
1492 return getParent()->isLambda() &&
1493 getIdentifier() && getIdentifier()->getName() == "__invoke";
1494}
1495
1496
Sean Huntcbb67482011-01-08 20:30:50 +00001497CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1498 TypeSourceInfo *TInfo, bool IsVirtual,
1499 SourceLocation L, Expr *Init,
1500 SourceLocation R,
1501 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001502 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001503 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1504 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001505{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001506}
1507
Sean Huntcbb67482011-01-08 20:30:50 +00001508CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1509 FieldDecl *Member,
1510 SourceLocation MemberLoc,
1511 SourceLocation L, Expr *Init,
1512 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001513 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001514 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001515 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1516{
1517}
1518
Sean Huntcbb67482011-01-08 20:30:50 +00001519CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1520 IndirectFieldDecl *Member,
1521 SourceLocation MemberLoc,
1522 SourceLocation L, Expr *Init,
1523 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001524 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001525 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001526 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001527{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001528}
1529
Sean Huntcbb67482011-01-08 20:30:50 +00001530CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001531 TypeSourceInfo *TInfo,
1532 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001533 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001534 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1535 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001536 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1537{
1538}
1539
1540CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001541 FieldDecl *Member,
1542 SourceLocation MemberLoc,
1543 SourceLocation L, Expr *Init,
1544 SourceLocation R,
1545 VarDecl **Indices,
1546 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001547 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001548 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001549 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001550{
1551 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1552 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1553}
1554
Sean Huntcbb67482011-01-08 20:30:50 +00001555CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1556 FieldDecl *Member,
1557 SourceLocation MemberLoc,
1558 SourceLocation L, Expr *Init,
1559 SourceLocation R,
1560 VarDecl **Indices,
1561 unsigned NumIndices) {
1562 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001563 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001564 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001565 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1566 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001567}
1568
Sean Huntcbb67482011-01-08 20:30:50 +00001569TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001570 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001571 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001572 else
1573 return TypeLoc();
1574}
1575
Sean Huntcbb67482011-01-08 20:30:50 +00001576const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001577 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001578 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001579 else
1580 return 0;
1581}
1582
Sean Huntcbb67482011-01-08 20:30:50 +00001583SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001584 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001585 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001586
1587 if (isInClassMemberInitializer())
1588 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001589
Douglas Gregor76852c22011-11-01 01:16:03 +00001590 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1591 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1592
1593 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001594}
1595
Sean Huntcbb67482011-01-08 20:30:50 +00001596SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001597 if (isInClassMemberInitializer()) {
1598 FieldDecl *D = getAnyMember();
1599 if (Expr *I = D->getInClassInitializer())
1600 return I->getSourceRange();
1601 return SourceRange();
1602 }
1603
Douglas Gregor802ab452009-12-02 22:36:29 +00001604 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001605}
1606
David Blaikie99ba9e32011-12-20 02:48:34 +00001607void CXXConstructorDecl::anchor() { }
1608
Douglas Gregorb48fe382008-10-31 09:07:45 +00001609CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001610CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1611 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1612 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1613 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001614}
1615
1616CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001617CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001618 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001619 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001620 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001621 bool isExplicit, bool isInline,
1622 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001623 assert(NameInfo.getName().getNameKind()
1624 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001625 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001626 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001627 isExplicit, isInline, isImplicitlyDeclared,
1628 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001629}
1630
Douglas Gregor76852c22011-11-01 01:16:03 +00001631CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1632 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1633 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1634 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1635 return Construct->getConstructor();
1636
1637 return 0;
1638}
1639
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001640bool CXXConstructorDecl::isDefaultConstructor() const {
1641 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001642 // A default constructor for a class X is a constructor of class
1643 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001644 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001645 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001646}
1647
Mike Stump1eb44332009-09-09 15:08:12 +00001648bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001649CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001650 return isCopyOrMoveConstructor(TypeQuals) &&
1651 getParamDecl(0)->getType()->isLValueReferenceType();
1652}
1653
1654bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1655 return isCopyOrMoveConstructor(TypeQuals) &&
1656 getParamDecl(0)->getType()->isRValueReferenceType();
1657}
1658
1659/// \brief Determine whether this is a copy or move constructor.
1660bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001661 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001662 // A non-template constructor for class X is a copy constructor
1663 // if its first parameter is of type X&, const X&, volatile X& or
1664 // const volatile X&, and either there are no other parameters
1665 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001666 // C++0x [class.copy]p3:
1667 // A non-template constructor for class X is a move constructor if its
1668 // first parameter is of type X&&, const X&&, volatile X&&, or
1669 // const volatile X&&, and either there are no other parameters or else
1670 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001671 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001672 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001673 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001674 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001675 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001676
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001677 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001678
1679 // Do we have a reference type?
1680 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001681 if (!ParamRefType)
1682 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001683
Douglas Gregorfd476482009-11-13 23:59:09 +00001684 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001685 ASTContext &Context = getASTContext();
1686
Douglas Gregorfd476482009-11-13 23:59:09 +00001687 CanQualType PointeeType
1688 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001689 CanQualType ClassTy
1690 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001691 if (PointeeType.getUnqualifiedType() != ClassTy)
1692 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001693
John McCall0953e762009-09-24 19:53:00 +00001694 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001695
1696 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001697 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001698 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001699}
1700
Anders Carlssonfaccd722009-08-28 16:57:08 +00001701bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001702 // C++ [class.conv.ctor]p1:
1703 // A constructor declared without the function-specifier explicit
1704 // that can be called with a single parameter specifies a
1705 // conversion from the type of its first parameter to the type of
1706 // its class. Such a constructor is called a converting
1707 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001708 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001709 return false;
1710
Mike Stump1eb44332009-09-09 15:08:12 +00001711 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001712 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001713 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001714 (getNumParams() > 1 &&
1715 (getParamDecl(1)->hasDefaultArg() ||
1716 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001717}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001718
Douglas Gregor6493cc52010-11-08 17:16:59 +00001719bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001720 if ((getNumParams() < 1) ||
1721 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1722 (getPrimaryTemplate() == 0) ||
1723 (getDescribedFunctionTemplate() != 0))
1724 return false;
1725
1726 const ParmVarDecl *Param = getParamDecl(0);
1727
1728 ASTContext &Context = getASTContext();
1729 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1730
Douglas Gregor66724ea2009-11-14 01:20:54 +00001731 // Is it the same as our our class type?
1732 CanQualType ClassTy
1733 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1734 if (ParamType.getUnqualifiedType() != ClassTy)
1735 return false;
1736
1737 return true;
1738}
1739
Sebastian Redlf677ea32011-02-05 19:23:19 +00001740const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1741 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001742 method_iterator It = getASTContext().overridden_methods_begin(this);
1743 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001744 return 0;
1745
1746 return cast<CXXConstructorDecl>(*It);
1747}
1748
1749void
1750CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1751 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001752 assert(getASTContext().overridden_methods_size(this) == 0 &&
1753 "Base ctor already set.");
1754 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001755}
1756
David Blaikie99ba9e32011-12-20 02:48:34 +00001757void CXXDestructorDecl::anchor() { }
1758
Douglas Gregor42a552f2008-11-05 20:51:48 +00001759CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001760CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1761 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1762 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001763 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001764}
1765
1766CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001767CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001768 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001769 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001770 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001771 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001772 assert(NameInfo.getName().getNameKind()
1773 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001774 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001775 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001776 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001777}
1778
David Blaikie99ba9e32011-12-20 02:48:34 +00001779void CXXConversionDecl::anchor() { }
1780
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001781CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001782CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1783 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1784 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1785 QualType(), 0, false, false, false,
1786 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001787}
1788
1789CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001790CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001791 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001792 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001793 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001794 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001795 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001796 assert(NameInfo.getName().getNameKind()
1797 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001798 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001799 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001800 isInline, isExplicit, isConstexpr,
1801 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001802}
1803
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001804bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1805 return isImplicit() && getParent()->isLambda() &&
1806 getConversionType()->isBlockPointerType();
1807}
1808
David Blaikie99ba9e32011-12-20 02:48:34 +00001809void LinkageSpecDecl::anchor() { }
1810
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001811LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001812 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001813 SourceLocation ExternLoc,
1814 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001815 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001816 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001817 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001818}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001819
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001820LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1821 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1822 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1823 lang_c, SourceLocation());
1824}
1825
David Blaikie99ba9e32011-12-20 02:48:34 +00001826void UsingDirectiveDecl::anchor() { }
1827
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001828UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1829 SourceLocation L,
1830 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001831 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001832 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001833 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001834 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001835 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1836 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001837 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1838 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001839}
1840
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001841UsingDirectiveDecl *
1842UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1843 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1844 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1845 NestedNameSpecifierLoc(),
1846 SourceLocation(), 0, 0);
1847}
1848
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001849NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1850 if (NamespaceAliasDecl *NA =
1851 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1852 return NA->getNamespace();
1853 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1854}
1855
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001856void NamespaceDecl::anchor() { }
1857
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001858NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1859 SourceLocation StartLoc,
1860 SourceLocation IdLoc, IdentifierInfo *Id,
1861 NamespaceDecl *PrevDecl)
1862 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1863 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1864{
1865 setPreviousDeclaration(PrevDecl);
1866
1867 if (PrevDecl)
1868 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1869}
1870
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001871NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001872 bool Inline, SourceLocation StartLoc,
1873 SourceLocation IdLoc, IdentifierInfo *Id,
1874 NamespaceDecl *PrevDecl) {
1875 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001876}
1877
1878NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1879 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001880 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1881 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001882}
1883
David Blaikie99ba9e32011-12-20 02:48:34 +00001884void NamespaceAliasDecl::anchor() { }
1885
Mike Stump1eb44332009-09-09 15:08:12 +00001886NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001887 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001888 SourceLocation AliasLoc,
1889 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001890 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001891 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001892 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001893 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1894 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001895 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1896 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001897}
1898
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001899NamespaceAliasDecl *
1900NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1901 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1902 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1903 NestedNameSpecifierLoc(),
1904 SourceLocation(), 0);
1905}
1906
David Blaikie99ba9e32011-12-20 02:48:34 +00001907void UsingShadowDecl::anchor() { }
1908
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001909UsingShadowDecl *
1910UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1911 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1912 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1913}
1914
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001915UsingDecl *UsingShadowDecl::getUsingDecl() const {
1916 const UsingShadowDecl *Shadow = this;
1917 while (const UsingShadowDecl *NextShadow =
1918 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1919 Shadow = NextShadow;
1920 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1921}
1922
David Blaikie99ba9e32011-12-20 02:48:34 +00001923void UsingDecl::anchor() { }
1924
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001925void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1926 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1927 "declaration already in set");
1928 assert(S->getUsingDecl() == this);
1929
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001930 if (FirstUsingShadow.getPointer())
1931 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1932 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001933}
1934
1935void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1936 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1937 "declaration not in set");
1938 assert(S->getUsingDecl() == this);
1939
1940 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1941
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001942 if (FirstUsingShadow.getPointer() == S) {
1943 FirstUsingShadow.setPointer(
1944 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001945 S->UsingOrNextShadow = this;
1946 return;
1947 }
1948
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001949 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001950 while (Prev->UsingOrNextShadow != S)
1951 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1952 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1953 S->UsingOrNextShadow = this;
1954}
1955
Douglas Gregordc355712011-02-25 00:36:19 +00001956UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1957 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001958 const DeclarationNameInfo &NameInfo,
1959 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001960 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001961}
1962
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001963UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1964 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1965 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1966 DeclarationNameInfo(), false);
1967}
1968
David Blaikie99ba9e32011-12-20 02:48:34 +00001969void UnresolvedUsingValueDecl::anchor() { }
1970
John McCall7ba107a2009-11-18 02:36:19 +00001971UnresolvedUsingValueDecl *
1972UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1973 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001974 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001975 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001976 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001977 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001978}
1979
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001980UnresolvedUsingValueDecl *
1981UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1982 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1983 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1984 NestedNameSpecifierLoc(),
1985 DeclarationNameInfo());
1986}
1987
David Blaikie99ba9e32011-12-20 02:48:34 +00001988void UnresolvedUsingTypenameDecl::anchor() { }
1989
John McCall7ba107a2009-11-18 02:36:19 +00001990UnresolvedUsingTypenameDecl *
1991UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1992 SourceLocation UsingLoc,
1993 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001994 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001995 SourceLocation TargetNameLoc,
1996 DeclarationName TargetName) {
1997 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001998 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001999 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00002000}
2001
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002002UnresolvedUsingTypenameDecl *
2003UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2004 void *Mem = AllocateDeserializedDecl(C, ID,
2005 sizeof(UnresolvedUsingTypenameDecl));
2006 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2007 SourceLocation(),
2008 NestedNameSpecifierLoc(),
2009 SourceLocation(),
2010 0);
2011}
2012
David Blaikie99ba9e32011-12-20 02:48:34 +00002013void StaticAssertDecl::anchor() { }
2014
Anders Carlssonfb311762009-03-14 00:25:26 +00002015StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002016 SourceLocation StaticAssertLoc,
2017 Expr *AssertExpr,
2018 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002019 SourceLocation RParenLoc,
2020 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002021 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002022 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00002023}
2024
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002025StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2026 unsigned ID) {
2027 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00002028 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
2029 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002030}
2031
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002032static const char *getAccessName(AccessSpecifier AS) {
2033 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002034 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002035 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002036 case AS_public:
2037 return "public";
2038 case AS_private:
2039 return "private";
2040 case AS_protected:
2041 return "protected";
2042 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002043 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002044}
2045
2046const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2047 AccessSpecifier AS) {
2048 return DB << getAccessName(AS);
2049}
Richard Smithf15fda02012-02-02 01:16:57 +00002050
2051const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2052 AccessSpecifier AS) {
2053 return DB << getAccessName(AS);
2054}