blob: df8a52675be5414e0416afd8bff46e685bd0b41d [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.
Richard Smitha10b9782013-04-22 15:31:51 +0000189 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000190 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 [...].
Richard Smithc3bf52c2013-04-20 22:23:05 +0000508 // C++11 [dcl.init.aggr]p1:
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000509 // 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.
Richard Smitha10b9782013-04-22 15:31:51 +0000679 if (!T->isLiteralType(Context) || 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.
Richard Smithc3bf52c2013-04-20 22:23:05 +0000693 //
694 // This rule was removed in C++1y.
695 if (!getASTContext().getLangOpts().CPlusPlus1y)
696 data().Aggregate = false;
Richard Smith7a614d82011-06-11 17:19:42 +0000697
Richard Smithd079abf2012-05-07 01:07:30 +0000698 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000699 // A POD struct is [...] a trivial class.
700 data().PlainOldData = false;
701 }
702
Richard Smithbc2a35d2012-12-08 08:32:28 +0000703 // C++11 [class.copy]p23:
704 // A defaulted copy/move assignment operator for a class X is defined
705 // as deleted if X has:
706 // -- a non-static data member of reference type
707 if (T->isReferenceType())
708 data().DefaultedMoveAssignmentIsDeleted = true;
709
Douglas Gregor85606eb2010-09-28 20:50:54 +0000710 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
711 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
712 if (FieldRec->getDefinition()) {
Richard Smithbc2a35d2012-12-08 08:32:28 +0000713 addedClassSubobject(FieldRec);
714
715 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
716 // A defaulted [special member] for a class X is defined as
717 // deleted if:
718 // -- X is a union-like class that has a variant member with a
719 // non-trivial [corresponding special member]
720 if (isUnion()) {
721 if (FieldRec->hasNonTrivialMoveConstructor())
722 data().DefaultedMoveConstructorIsDeleted = true;
723 if (FieldRec->hasNonTrivialMoveAssignment())
724 data().DefaultedMoveAssignmentIsDeleted = true;
725 if (FieldRec->hasNonTrivialDestructor())
726 data().DefaultedDestructorIsDeleted = true;
727 }
728
Sean Hunt023df372011-05-09 18:22:59 +0000729 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000730 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000731 // -- for all the non-static data members of its class that are of
732 // class type (or array thereof), each such class has a trivial
733 // default constructor.
734 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000735 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000736
737 // C++0x [class.copy]p13:
738 // A copy/move constructor for class X is trivial if [...]
739 // [...]
740 // -- for each non-static data member of X that is of class type (or
741 // an array thereof), the constructor selected to copy/move that
742 // member is trivial;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000743 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000744 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000745 // If the field doesn't have a simple move constructor, we'll eagerly
746 // declare the move constructor for this class and we'll decide whether
747 // it's trivial then.
Richard Smith426391c2012-11-16 00:53:38 +0000748 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000749 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000750
751 // C++0x [class.copy]p27:
752 // A copy/move assignment operator for class X is trivial if [...]
753 // [...]
754 // -- for each non-static data member of X that is of class type (or
755 // an array thereof), the assignment operator selected to
756 // copy/move that member is trivial;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000757 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000758 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000759 // If the field doesn't have a simple move assignment, we'll eagerly
760 // declare the move assignment for this class and we'll decide whether
761 // it's trivial then.
Richard Smith426391c2012-11-16 00:53:38 +0000762 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000763 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000764
Douglas Gregor85606eb2010-09-28 20:50:54 +0000765 if (!FieldRec->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000766 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000767 if (!FieldRec->hasIrrelevantDestructor())
768 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000769 if (FieldRec->hasObjectMember())
770 setHasObjectMember(true);
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +0000771 if (FieldRec->hasVolatileMember())
772 setHasVolatileMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000773
774 // C++0x [class]p7:
775 // A standard-layout class is a class that:
776 // -- has no non-static data members of type non-standard-layout
777 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000778 if (!FieldRec->isStandardLayout())
779 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000780
781 // C++0x [class]p7:
782 // A standard-layout class is a class that:
783 // [...]
784 // -- has no base classes of the same type as the first non-static
785 // data member.
786 // We don't want to expend bits in the state of the record decl
787 // tracking whether this is the first non-static data member so we
788 // cheat a bit and use some of the existing state: the empty bit.
789 // Virtual bases and virtual methods make a class non-empty, but they
790 // also make it non-standard-layout so we needn't check here.
791 // A non-empty base class may leave the class standard-layout, but not
792 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000793 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000794 // data member must come through here with Empty still true, and Empty
795 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000796 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000797 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
798 BE = bases_end();
799 BI != BE; ++BI) {
800 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000801 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000802 break;
803 }
804 }
805 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000806
807 // Keep track of the presence of mutable fields.
808 if (FieldRec->hasMutableFields())
809 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000810
811 // C++11 [class.copy]p13:
812 // If the implicitly-defined constructor would satisfy the
813 // requirements of a constexpr constructor, the implicitly-defined
814 // constructor is constexpr.
815 // C++11 [dcl.constexpr]p4:
816 // -- every constructor involved in initializing non-static data
817 // members [...] shall be a constexpr constructor
818 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000819 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000820 // The standard requires any in-class initializer to be a constant
821 // expression. We consider this to be a defect.
822 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithacf796b2012-11-28 06:23:12 +0000823
824 // C++11 [class.copy]p8:
825 // The implicitly-declared copy constructor for a class X will have
826 // the form 'X::X(const X&)' if [...] for all the non-static data
827 // members of X that are of a class type M (or array thereof), each
828 // such class type has a copy constructor whose first parameter is
829 // of type 'const M&' or 'const volatile M&'.
830 if (!FieldRec->hasCopyConstructorWithConstParam())
831 data().ImplicitCopyConstructorHasConstParam = false;
832
833 // C++11 [class.copy]p18:
834 // The implicitly-declared copy assignment oeprator for a class X will
835 // have the form 'X& X::operator=(const X&)' if [...] for all the
836 // non-static data members of X that are of a class type M (or array
837 // thereof), each such class type has a copy assignment operator whose
838 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
839 if (!FieldRec->hasCopyAssignmentWithConstParam())
840 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smithd5bc8672012-12-08 02:01:17 +0000841
842 if (FieldRec->hasUninitializedReferenceMember() &&
843 !Field->hasInClassInitializer())
844 data().HasUninitializedReferenceMember = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000845 }
Richard Smith61802452011-12-22 02:22:31 +0000846 } else {
847 // Base element type of field is a non-class type.
Richard Smitha10b9782013-04-22 15:31:51 +0000848 if (!T->isLiteralType(Context) ||
Richard Smithd3861ce2012-06-10 07:07:24 +0000849 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000850 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000851
852 // C++11 [class.copy]p23:
853 // A defaulted copy/move assignment operator for a class X is defined
854 // as deleted if X has:
855 // -- a non-static data member of const non-class type (or array
856 // thereof)
857 if (T.isConstQualified())
858 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000859 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000860
861 // C++0x [class]p7:
862 // A standard-layout class is a class that:
863 // [...]
864 // -- either has no non-static data members in the most derived
865 // class and at most one base class with non-static data members,
866 // or has no base classes with non-static data members, and
867 // At this point we know that we have a non-static data member, so the last
868 // clause holds.
869 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000870 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000871
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000872 // If this is not a zero-length bit-field, then the class is not empty.
873 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000874 if (!Field->isBitField() ||
875 (!Field->getBitWidth()->isTypeDependent() &&
876 !Field->getBitWidth()->isValueDependent() &&
877 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000878 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000879 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000880 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000881
882 // Handle using declarations of conversion functions.
883 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
884 if (Shadow->getDeclName().getNameKind()
885 == DeclarationName::CXXConversionFunctionName)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000886 data().Conversions.addDecl(getASTContext(), Shadow, Shadow->getAccess());
Joao Matos17d35c32012-08-31 22:18:20 +0000887}
888
Richard Smithac713512012-12-08 02:53:02 +0000889void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
890 assert(!D->isImplicit() && !D->isUserProvided());
891
892 // The kind of special member this declaration is, if any.
893 unsigned SMKind = 0;
894
895 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
896 if (Constructor->isDefaultConstructor()) {
897 SMKind |= SMF_DefaultConstructor;
898 if (Constructor->isConstexpr())
899 data().HasConstexprDefaultConstructor = true;
900 }
901 if (Constructor->isCopyConstructor())
902 SMKind |= SMF_CopyConstructor;
903 else if (Constructor->isMoveConstructor())
904 SMKind |= SMF_MoveConstructor;
905 else if (Constructor->isConstexpr())
906 // We may now know that the constructor is constexpr.
907 data().HasConstexprNonCopyMoveConstructor = true;
908 } else if (isa<CXXDestructorDecl>(D))
909 SMKind |= SMF_Destructor;
910 else if (D->isCopyAssignmentOperator())
911 SMKind |= SMF_CopyAssignment;
912 else if (D->isMoveAssignmentOperator())
913 SMKind |= SMF_MoveAssignment;
914
915 // Update which trivial / non-trivial special members we have.
916 // addedMember will have skipped this step for this member.
917 if (D->isTrivial())
918 data().HasTrivialSpecialMembers |= SMKind;
919 else
920 data().DeclaredNonTrivialSpecialMembers |= SMKind;
921}
922
Joao Matos17d35c32012-08-31 22:18:20 +0000923bool CXXRecordDecl::isCLike() const {
924 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
925 !TemplateOrInstantiation.isNull())
926 return false;
927 if (!hasDefinition())
928 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000929
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000930 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000931}
932
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000933void CXXRecordDecl::getCaptureFields(
934 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000935 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000936 Captures.clear();
937 ThisCapture = 0;
938
Douglas Gregorda8962a2012-02-13 15:44:47 +0000939 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000940 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000941 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000942 C != CEnd; ++C, ++Field) {
Richard Smith0d8e9642013-05-16 06:20:58 +0000943 if (C->capturesThis())
David Blaikie581deb32012-06-06 20:45:41 +0000944 ThisCapture = *Field;
Richard Smith0d8e9642013-05-16 06:20:58 +0000945 else if (C->capturesVariable())
946 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000947 }
948}
949
950
John McCallb05b5f32010-03-15 09:07:48 +0000951static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
952 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000953 if (isa<UsingShadowDecl>(Conv))
954 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000955 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
956 T = ConvTemp->getTemplatedDecl()->getResultType();
957 else
958 T = cast<CXXConversionDecl>(Conv)->getConversionType();
959 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000960}
961
John McCallb05b5f32010-03-15 09:07:48 +0000962/// Collect the visible conversions of a base class.
963///
James Dennetta1253502012-06-15 22:28:09 +0000964/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +0000965/// \param InVirtual whether this base class is a virtual base (or a base
966/// of a virtual base)
967/// \param Access the access along the inheritance path to this base
968/// \param ParentHiddenTypes the conversions provided by the inheritors
969/// of this base
970/// \param Output the set to which to add conversions from non-virtual bases
971/// \param VOutput the set to which to add conversions from virtual bases
972/// \param HiddenVBaseCs the set of conversions which were hidden in a
973/// virtual base along some inheritance path
974static void CollectVisibleConversions(ASTContext &Context,
975 CXXRecordDecl *Record,
976 bool InVirtual,
977 AccessSpecifier Access,
978 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000979 ASTUnresolvedSet &Output,
John McCallb05b5f32010-03-15 09:07:48 +0000980 UnresolvedSetImpl &VOutput,
981 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
982 // The set of types which have conversions in this class or its
983 // subclasses. As an optimization, we don't copy the derived set
984 // unless it might change.
985 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
986 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
987
988 // Collect the direct conversions and figure out which conversions
989 // will be hidden in the subclasses.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000990 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
991 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
992 if (ConvI != ConvE) {
John McCallb05b5f32010-03-15 09:07:48 +0000993 HiddenTypesBuffer = ParentHiddenTypes;
994 HiddenTypes = &HiddenTypesBuffer;
995
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000996 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +0000997 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
998 bool Hidden = ParentHiddenTypes.count(ConvType);
999 if (!Hidden)
1000 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001001
1002 // If this conversion is hidden and we're in a virtual base,
1003 // remember that it's hidden along some inheritance path.
1004 if (Hidden && InVirtual)
1005 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1006
1007 // If this conversion isn't hidden, add it to the appropriate output.
1008 else if (!Hidden) {
1009 AccessSpecifier IAccess
1010 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1011
1012 if (InVirtual)
1013 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001014 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001015 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001016 }
1017 }
1018 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001019
John McCallb05b5f32010-03-15 09:07:48 +00001020 // Collect information recursively from any base classes.
1021 for (CXXRecordDecl::base_class_iterator
1022 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1023 const RecordType *RT = I->getType()->getAs<RecordType>();
1024 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001025
John McCallb05b5f32010-03-15 09:07:48 +00001026 AccessSpecifier BaseAccess
1027 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1028 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001029
John McCallb05b5f32010-03-15 09:07:48 +00001030 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1031 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1032 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001033 }
John McCallb05b5f32010-03-15 09:07:48 +00001034}
Sebastian Redl9994a342009-10-25 17:03:50 +00001035
John McCallb05b5f32010-03-15 09:07:48 +00001036/// Collect the visible conversions of a class.
1037///
1038/// This would be extremely straightforward if it weren't for virtual
1039/// bases. It might be worth special-casing that, really.
1040static void CollectVisibleConversions(ASTContext &Context,
1041 CXXRecordDecl *Record,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001042 ASTUnresolvedSet &Output) {
John McCallb05b5f32010-03-15 09:07:48 +00001043 // The collection of all conversions in virtual bases that we've
1044 // found. These will be added to the output as long as they don't
1045 // appear in the hidden-conversions set.
1046 UnresolvedSet<8> VBaseCs;
1047
1048 // The set of conversions in virtual bases that we've determined to
1049 // be hidden.
1050 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1051
1052 // The set of types hidden by classes derived from this one.
1053 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1054
1055 // Go ahead and collect the direct conversions and add them to the
1056 // hidden-types set.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001057 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1058 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001059 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001060 for (; ConvI != ConvE; ++ConvI)
1061 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCallb05b5f32010-03-15 09:07:48 +00001062
1063 // Recursively collect conversions from base classes.
1064 for (CXXRecordDecl::base_class_iterator
1065 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1066 const RecordType *RT = I->getType()->getAs<RecordType>();
1067 if (!RT) continue;
1068
1069 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1070 I->isVirtual(), I->getAccessSpecifier(),
1071 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1072 }
1073
1074 // Add any unhidden conversions provided by virtual bases.
1075 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1076 I != E; ++I) {
1077 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001078 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001079 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001080}
1081
1082/// getVisibleConversionFunctions - get all conversion functions visible
1083/// in current class; including conversion function templates.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001084std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1085CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001086 // If root class, all conversions are visible.
1087 if (bases_begin() == bases_end())
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001088 return std::make_pair(data().Conversions.begin(), data().Conversions.end());
Fariborz Jahanian62509212009-09-12 18:26:03 +00001089 // If visible conversion list is already evaluated, return it.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001090 if (!data().ComputedVisibleConversions) {
1091 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
1092 data().ComputedVisibleConversions = true;
1093 }
1094 return std::make_pair(data().VisibleConversions.begin(),
1095 data().VisibleConversions.end());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001096}
1097
John McCall32daa422010-03-31 01:36:47 +00001098void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1099 // This operation is O(N) but extremely rare. Sema only uses it to
1100 // remove UsingShadowDecls in a class that were followed by a direct
1101 // declaration, e.g.:
1102 // class A : B {
1103 // using B::operator int;
1104 // operator int();
1105 // };
1106 // This is uncommon by itself and even more uncommon in conjunction
1107 // with sufficiently large numbers of directly-declared conversions
1108 // that asymptotic behavior matters.
1109
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001110 ASTUnresolvedSet &Convs = data().Conversions;
John McCall32daa422010-03-31 01:36:47 +00001111 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1112 if (Convs[I].getDecl() == ConvDecl) {
1113 Convs.erase(I);
1114 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1115 && "conversion was found multiple times in unresolved set");
1116 return;
1117 }
1118 }
1119
1120 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001121}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001122
Douglas Gregorf6b11852009-10-08 15:14:33 +00001123CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001124 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001125 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1126
1127 return 0;
1128}
1129
1130void
1131CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1132 TemplateSpecializationKind TSK) {
1133 assert(TemplateOrInstantiation.isNull() &&
1134 "Previous template or instantiation?");
1135 assert(!isa<ClassTemplateSpecializationDecl>(this));
1136 TemplateOrInstantiation
1137 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1138}
1139
Anders Carlssonb13e3572009-12-07 06:33:48 +00001140TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1141 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001142 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1143 return Spec->getSpecializationKind();
1144
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001145 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001146 return MSInfo->getTemplateSpecializationKind();
1147
1148 return TSK_Undeclared;
1149}
1150
1151void
1152CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1153 if (ClassTemplateSpecializationDecl *Spec
1154 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1155 Spec->setSpecializationKind(TSK);
1156 return;
1157 }
1158
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001159 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001160 MSInfo->setTemplateSpecializationKind(TSK);
1161 return;
1162 }
1163
David Blaikieb219cfc2011-09-23 05:06:16 +00001164 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001165}
1166
Douglas Gregor1d110e02010-07-01 14:13:13 +00001167CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1168 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001169 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001170
1171 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001172 = Context.DeclarationNames.getCXXDestructorName(
1173 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001174
David Blaikie3bc93e32012-12-19 00:45:41 +00001175 DeclContext::lookup_const_result R = lookup(Name);
1176 if (R.empty())
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001177 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001178
David Blaikie3bc93e32012-12-19 00:45:41 +00001179 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson7267c162009-05-29 21:03:38 +00001180 return Dtor;
1181}
1182
Douglas Gregorda2142f2011-02-19 18:51:44 +00001183void CXXRecordDecl::completeDefinition() {
1184 completeDefinition(0);
1185}
1186
1187void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1188 RecordDecl::completeDefinition();
1189
David Blaikie4e4d0842012-03-11 07:00:24 +00001190 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001191 // Objective-C Automatic Reference Counting:
1192 // If a class has a non-static data member of Objective-C pointer
1193 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001194 // default constructor (if any), copy constructor, move constructor,
1195 // copy assignment operator, move assignment operator, and destructor are
1196 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001197 struct DefinitionData &Data = data();
1198 Data.PlainOldData = false;
Richard Smith7d04d3a2012-11-30 05:11:39 +00001199 Data.HasTrivialSpecialMembers = 0;
Richard Smithdfefb842012-02-25 07:33:38 +00001200 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001201 }
1202
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001203 // If the class may be abstract (but hasn't been marked as such), check for
1204 // any pure final overriders.
1205 if (mayBeAbstract()) {
1206 CXXFinalOverriderMap MyFinalOverriders;
1207 if (!FinalOverriders) {
1208 getFinalOverriders(MyFinalOverriders);
1209 FinalOverriders = &MyFinalOverriders;
1210 }
1211
1212 bool Done = false;
1213 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1214 MEnd = FinalOverriders->end();
1215 M != MEnd && !Done; ++M) {
1216 for (OverridingMethods::iterator SO = M->second.begin(),
1217 SOEnd = M->second.end();
1218 SO != SOEnd && !Done; ++SO) {
1219 assert(SO->second.size() > 0 &&
1220 "All virtual functions have overridding virtual functions");
1221
1222 // C++ [class.abstract]p4:
1223 // A class is abstract if it contains or inherits at least one
1224 // pure virtual function for which the final overrider is pure
1225 // virtual.
1226 if (SO->second.front().Method->isPure()) {
1227 data().Abstract = true;
1228 Done = true;
1229 break;
1230 }
1231 }
1232 }
1233 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001234
1235 // Set access bits correctly on the directly-declared conversions.
1236 for (UnresolvedSetIterator I = data().Conversions.begin(),
1237 E = data().Conversions.end();
1238 I != E; ++I)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001239 I.setAccess((*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001240}
1241
1242bool CXXRecordDecl::mayBeAbstract() const {
1243 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1244 isDependentContext())
1245 return false;
1246
1247 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1248 BEnd = bases_end();
1249 B != BEnd; ++B) {
1250 CXXRecordDecl *BaseDecl
1251 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1252 if (BaseDecl->isAbstract())
1253 return true;
1254 }
1255
1256 return false;
1257}
1258
David Blaikie99ba9e32011-12-20 02:48:34 +00001259void CXXMethodDecl::anchor() { }
1260
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001261bool CXXMethodDecl::isStatic() const {
Rafael Espindola72fdc892013-04-15 12:38:20 +00001262 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001263
1264 if (MD->getStorageClass() == SC_Static)
1265 return true;
1266
1267 DeclarationName Name = getDeclName();
1268 // [class.free]p1:
1269 // Any allocation function for a class T is a static member
1270 // (even if not explicitly declared static).
1271 if (Name.getCXXOverloadedOperator() == OO_New ||
1272 Name.getCXXOverloadedOperator() == OO_Array_New)
1273 return true;
1274
1275 // [class.free]p6 Any deallocation function for a class X is a static member
1276 // (even if not explicitly declared static).
1277 if (Name.getCXXOverloadedOperator() == OO_Delete ||
1278 Name.getCXXOverloadedOperator() == OO_Array_Delete)
1279 return true;
1280
1281 return false;
1282}
1283
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001284static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1285 const CXXMethodDecl *BaseMD) {
1286 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1287 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1288 const CXXMethodDecl *MD = *I;
1289 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1290 return true;
1291 if (recursivelyOverrides(MD, BaseMD))
1292 return true;
1293 }
1294 return false;
1295}
1296
1297CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001298CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1299 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001300 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1301 return this;
1302
1303 // Lookup doesn't work for destructors, so handle them separately.
1304 if (isa<CXXDestructorDecl>(this)) {
1305 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001306 if (MD) {
1307 if (recursivelyOverrides(MD, this))
1308 return MD;
1309 if (MayBeBase && recursivelyOverrides(this, MD))
1310 return MD;
1311 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001312 return NULL;
1313 }
1314
1315 lookup_const_result Candidates = RD->lookup(getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +00001316 for (NamedDecl * const * I = Candidates.begin(); I != Candidates.end(); ++I) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001317 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1318 if (!MD)
1319 continue;
1320 if (recursivelyOverrides(MD, this))
1321 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001322 if (MayBeBase && recursivelyOverrides(this, MD))
1323 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001324 }
1325
1326 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1327 E = RD->bases_end(); I != E; ++I) {
1328 const RecordType *RT = I->getType()->getAs<RecordType>();
1329 if (!RT)
1330 continue;
1331 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1332 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1333 if (T)
1334 return T;
1335 }
1336
1337 return NULL;
1338}
1339
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001340CXXMethodDecl *
1341CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001342 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001343 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001344 QualType T, TypeSourceInfo *TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001345 StorageClass SC, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001346 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001347 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001348 SC, isInline, isConstexpr,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001349 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001350}
1351
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001352CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1353 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1354 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1355 DeclarationNameInfo(), QualType(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001356 0, SC_None, false, false,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001357 SourceLocation());
1358}
1359
Douglas Gregor90916562009-09-29 18:16:17 +00001360bool CXXMethodDecl::isUsualDeallocationFunction() const {
1361 if (getOverloadedOperator() != OO_Delete &&
1362 getOverloadedOperator() != OO_Array_Delete)
1363 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001364
1365 // C++ [basic.stc.dynamic.deallocation]p2:
1366 // A template instance is never a usual deallocation function,
1367 // regardless of its signature.
1368 if (getPrimaryTemplate())
1369 return false;
1370
Douglas Gregor90916562009-09-29 18:16:17 +00001371 // C++ [basic.stc.dynamic.deallocation]p2:
1372 // If a class T has a member deallocation function named operator delete
1373 // with exactly one parameter, then that function is a usual (non-placement)
1374 // deallocation function. [...]
1375 if (getNumParams() == 1)
1376 return true;
1377
1378 // C++ [basic.stc.dynamic.deallocation]p2:
1379 // [...] If class T does not declare such an operator delete but does
1380 // declare a member deallocation function named operator delete with
1381 // exactly two parameters, the second of which has type std::size_t (18.1),
1382 // then this function is a usual deallocation function.
1383 ASTContext &Context = getASTContext();
1384 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001385 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1386 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001387 return false;
1388
1389 // This function is a usual deallocation function if there are no
1390 // single-parameter deallocation functions of the same kind.
David Blaikie3bc93e32012-12-19 00:45:41 +00001391 DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1392 for (DeclContext::lookup_const_result::iterator I = R.begin(), E = R.end();
1393 I != E; ++I) {
1394 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregor90916562009-09-29 18:16:17 +00001395 if (FD->getNumParams() == 1)
1396 return false;
1397 }
1398
1399 return true;
1400}
1401
Douglas Gregor06a9f362010-05-01 20:49:11 +00001402bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001403 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001404 // A user-declared copy assignment operator X::operator= is a non-static
1405 // non-template member function of class X with exactly one parameter of
1406 // type X, X&, const X&, volatile X& or const volatile X&.
1407 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1408 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001409 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001410 return false;
1411
1412 QualType ParamType = getParamDecl(0)->getType();
1413 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1414 ParamType = Ref->getPointeeType();
1415
1416 ASTContext &Context = getASTContext();
1417 QualType ClassType
1418 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1419 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1420}
1421
Sean Huntffe37fd2011-05-25 20:50:04 +00001422bool CXXMethodDecl::isMoveAssignmentOperator() const {
1423 // C++0x [class.copy]p19:
1424 // A user-declared move assignment operator X::operator= is a non-static
1425 // non-template member function of class X with exactly one parameter of type
1426 // X&&, const X&&, volatile X&&, or const volatile X&&.
1427 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1428 getPrimaryTemplate() || getDescribedFunctionTemplate())
1429 return false;
1430
1431 QualType ParamType = getParamDecl(0)->getType();
1432 if (!isa<RValueReferenceType>(ParamType))
1433 return false;
1434 ParamType = ParamType->getPointeeType();
1435
1436 ASTContext &Context = getASTContext();
1437 QualType ClassType
1438 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1439 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1440}
1441
Anders Carlsson05eb2442009-05-16 23:58:37 +00001442void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001443 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001444 assert(!MD->getParent()->isDependentContext() &&
1445 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001446 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001447
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001448 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001449}
1450
1451CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001452 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001453 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001454}
1455
1456CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001457 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001458 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001459}
1460
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001461unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001462 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001463 return getASTContext().overridden_methods_size(this);
1464}
1465
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001466QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001467 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1468 // If the member function is declared const, the type of this is const X*,
1469 // if the member function is declared volatile, the type of this is
1470 // volatile X*, and if the member function is declared const volatile,
1471 // the type of this is const volatile X*.
1472
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001473 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001474
John McCall3cb0ebd2010-03-10 03:28:59 +00001475 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001476 ClassTy = C.getQualifiedType(ClassTy,
1477 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001478 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001479}
1480
Eli Friedmand7d7f672009-12-06 20:50:05 +00001481bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001482 // If this function is a template instantiation, look at the template from
1483 // which it was instantiated.
1484 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1485 if (!CheckFn)
1486 CheckFn = this;
1487
Eli Friedmand7d7f672009-12-06 20:50:05 +00001488 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001489 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001490}
1491
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001492bool CXXMethodDecl::isLambdaStaticInvoker() const {
1493 return getParent()->isLambda() &&
1494 getIdentifier() && getIdentifier()->getName() == "__invoke";
1495}
1496
1497
Sean Huntcbb67482011-01-08 20:30:50 +00001498CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1499 TypeSourceInfo *TInfo, bool IsVirtual,
1500 SourceLocation L, Expr *Init,
1501 SourceLocation R,
1502 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001503 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001504 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1505 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001506{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001507}
1508
Sean Huntcbb67482011-01-08 20:30:50 +00001509CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1510 FieldDecl *Member,
1511 SourceLocation MemberLoc,
1512 SourceLocation L, Expr *Init,
1513 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001514 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001515 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001516 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1517{
1518}
1519
Sean Huntcbb67482011-01-08 20:30:50 +00001520CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1521 IndirectFieldDecl *Member,
1522 SourceLocation MemberLoc,
1523 SourceLocation L, Expr *Init,
1524 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001525 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001526 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001527 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001528{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001529}
1530
Sean Huntcbb67482011-01-08 20:30:50 +00001531CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001532 TypeSourceInfo *TInfo,
1533 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001534 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001535 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1536 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001537 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1538{
1539}
1540
1541CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001542 FieldDecl *Member,
1543 SourceLocation MemberLoc,
1544 SourceLocation L, Expr *Init,
1545 SourceLocation R,
1546 VarDecl **Indices,
1547 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001548 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001549 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001550 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001551{
1552 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1553 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1554}
1555
Sean Huntcbb67482011-01-08 20:30:50 +00001556CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1557 FieldDecl *Member,
1558 SourceLocation MemberLoc,
1559 SourceLocation L, Expr *Init,
1560 SourceLocation R,
1561 VarDecl **Indices,
1562 unsigned NumIndices) {
1563 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001564 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001565 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001566 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1567 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001568}
1569
Sean Huntcbb67482011-01-08 20:30:50 +00001570TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001571 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001572 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001573 else
1574 return TypeLoc();
1575}
1576
Sean Huntcbb67482011-01-08 20:30:50 +00001577const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001578 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001579 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001580 else
1581 return 0;
1582}
1583
Sean Huntcbb67482011-01-08 20:30:50 +00001584SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001585 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001586 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001587
1588 if (isInClassMemberInitializer())
1589 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001590
Douglas Gregor76852c22011-11-01 01:16:03 +00001591 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1592 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1593
1594 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001595}
1596
Sean Huntcbb67482011-01-08 20:30:50 +00001597SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001598 if (isInClassMemberInitializer()) {
1599 FieldDecl *D = getAnyMember();
1600 if (Expr *I = D->getInClassInitializer())
1601 return I->getSourceRange();
1602 return SourceRange();
1603 }
1604
Douglas Gregor802ab452009-12-02 22:36:29 +00001605 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001606}
1607
David Blaikie99ba9e32011-12-20 02:48:34 +00001608void CXXConstructorDecl::anchor() { }
1609
Douglas Gregorb48fe382008-10-31 09:07:45 +00001610CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001611CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1612 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1613 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1614 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001615}
1616
1617CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001618CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001619 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001620 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001621 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001622 bool isExplicit, bool isInline,
1623 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001624 assert(NameInfo.getName().getNameKind()
1625 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001626 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001627 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001628 isExplicit, isInline, isImplicitlyDeclared,
1629 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001630}
1631
Douglas Gregor76852c22011-11-01 01:16:03 +00001632CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1633 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1634 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1635 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1636 return Construct->getConstructor();
1637
1638 return 0;
1639}
1640
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001641bool CXXConstructorDecl::isDefaultConstructor() const {
1642 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001643 // A default constructor for a class X is a constructor of class
1644 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001645 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001646 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001647}
1648
Mike Stump1eb44332009-09-09 15:08:12 +00001649bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001650CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001651 return isCopyOrMoveConstructor(TypeQuals) &&
1652 getParamDecl(0)->getType()->isLValueReferenceType();
1653}
1654
1655bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1656 return isCopyOrMoveConstructor(TypeQuals) &&
1657 getParamDecl(0)->getType()->isRValueReferenceType();
1658}
1659
1660/// \brief Determine whether this is a copy or move constructor.
1661bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001662 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001663 // A non-template constructor for class X is a copy constructor
1664 // if its first parameter is of type X&, const X&, volatile X& or
1665 // const volatile X&, and either there are no other parameters
1666 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001667 // C++0x [class.copy]p3:
1668 // A non-template constructor for class X is a move constructor if its
1669 // first parameter is of type X&&, const X&&, volatile X&&, or
1670 // const volatile X&&, and either there are no other parameters or else
1671 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001672 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001673 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001674 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001675 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001676 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001677
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001678 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001679
1680 // Do we have a reference type?
1681 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001682 if (!ParamRefType)
1683 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001684
Douglas Gregorfd476482009-11-13 23:59:09 +00001685 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001686 ASTContext &Context = getASTContext();
1687
Douglas Gregorfd476482009-11-13 23:59:09 +00001688 CanQualType PointeeType
1689 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001690 CanQualType ClassTy
1691 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001692 if (PointeeType.getUnqualifiedType() != ClassTy)
1693 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001694
John McCall0953e762009-09-24 19:53:00 +00001695 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001696
1697 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001698 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001699 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001700}
1701
Anders Carlssonfaccd722009-08-28 16:57:08 +00001702bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001703 // C++ [class.conv.ctor]p1:
1704 // A constructor declared without the function-specifier explicit
1705 // that can be called with a single parameter specifies a
1706 // conversion from the type of its first parameter to the type of
1707 // its class. Such a constructor is called a converting
1708 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001709 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001710 return false;
1711
Mike Stump1eb44332009-09-09 15:08:12 +00001712 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001713 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001714 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001715 (getNumParams() > 1 &&
1716 (getParamDecl(1)->hasDefaultArg() ||
1717 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001718}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001719
Douglas Gregor6493cc52010-11-08 17:16:59 +00001720bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001721 if ((getNumParams() < 1) ||
1722 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1723 (getPrimaryTemplate() == 0) ||
1724 (getDescribedFunctionTemplate() != 0))
1725 return false;
1726
1727 const ParmVarDecl *Param = getParamDecl(0);
1728
1729 ASTContext &Context = getASTContext();
1730 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1731
Douglas Gregor66724ea2009-11-14 01:20:54 +00001732 // Is it the same as our our class type?
1733 CanQualType ClassTy
1734 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1735 if (ParamType.getUnqualifiedType() != ClassTy)
1736 return false;
1737
1738 return true;
1739}
1740
Sebastian Redlf677ea32011-02-05 19:23:19 +00001741const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1742 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001743 method_iterator It = getASTContext().overridden_methods_begin(this);
1744 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001745 return 0;
1746
1747 return cast<CXXConstructorDecl>(*It);
1748}
1749
1750void
1751CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1752 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001753 assert(getASTContext().overridden_methods_size(this) == 0 &&
1754 "Base ctor already set.");
1755 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001756}
1757
David Blaikie99ba9e32011-12-20 02:48:34 +00001758void CXXDestructorDecl::anchor() { }
1759
Douglas Gregor42a552f2008-11-05 20:51:48 +00001760CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001761CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1762 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1763 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001764 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001765}
1766
1767CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001768CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001769 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001770 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001771 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001772 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001773 assert(NameInfo.getName().getNameKind()
1774 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001775 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001776 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001777 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001778}
1779
David Blaikie99ba9e32011-12-20 02:48:34 +00001780void CXXConversionDecl::anchor() { }
1781
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001782CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001783CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1784 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1785 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1786 QualType(), 0, false, false, false,
1787 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001788}
1789
1790CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001791CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001792 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001793 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001794 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001795 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001796 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001797 assert(NameInfo.getName().getNameKind()
1798 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001799 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001800 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001801 isInline, isExplicit, isConstexpr,
1802 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001803}
1804
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001805bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1806 return isImplicit() && getParent()->isLambda() &&
1807 getConversionType()->isBlockPointerType();
1808}
1809
David Blaikie99ba9e32011-12-20 02:48:34 +00001810void LinkageSpecDecl::anchor() { }
1811
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001812LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001813 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001814 SourceLocation ExternLoc,
1815 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001816 LanguageIDs Lang,
Rafael Espindolae5e575d2013-04-26 01:30:23 +00001817 bool HasBraces) {
1818 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001819}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001820
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001821LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1822 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1823 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
Rafael Espindolae5e575d2013-04-26 01:30:23 +00001824 lang_c, false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001825}
1826
David Blaikie99ba9e32011-12-20 02:48:34 +00001827void UsingDirectiveDecl::anchor() { }
1828
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001829UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1830 SourceLocation L,
1831 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001832 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001833 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001834 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001835 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001836 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1837 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001838 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1839 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001840}
1841
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001842UsingDirectiveDecl *
1843UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1844 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1845 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1846 NestedNameSpecifierLoc(),
1847 SourceLocation(), 0, 0);
1848}
1849
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001850NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1851 if (NamespaceAliasDecl *NA =
1852 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1853 return NA->getNamespace();
1854 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1855}
1856
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001857void NamespaceDecl::anchor() { }
1858
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001859NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1860 SourceLocation StartLoc,
1861 SourceLocation IdLoc, IdentifierInfo *Id,
1862 NamespaceDecl *PrevDecl)
1863 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1864 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1865{
1866 setPreviousDeclaration(PrevDecl);
1867
1868 if (PrevDecl)
1869 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1870}
1871
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001872NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001873 bool Inline, SourceLocation StartLoc,
1874 SourceLocation IdLoc, IdentifierInfo *Id,
1875 NamespaceDecl *PrevDecl) {
1876 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001877}
1878
1879NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1880 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001881 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1882 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001883}
1884
David Blaikie99ba9e32011-12-20 02:48:34 +00001885void NamespaceAliasDecl::anchor() { }
1886
Mike Stump1eb44332009-09-09 15:08:12 +00001887NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001888 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001889 SourceLocation AliasLoc,
1890 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001891 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001892 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001893 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001894 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1895 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001896 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1897 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001898}
1899
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001900NamespaceAliasDecl *
1901NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1902 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1903 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1904 NestedNameSpecifierLoc(),
1905 SourceLocation(), 0);
1906}
1907
David Blaikie99ba9e32011-12-20 02:48:34 +00001908void UsingShadowDecl::anchor() { }
1909
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001910UsingShadowDecl *
1911UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1912 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1913 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1914}
1915
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001916UsingDecl *UsingShadowDecl::getUsingDecl() const {
1917 const UsingShadowDecl *Shadow = this;
1918 while (const UsingShadowDecl *NextShadow =
1919 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1920 Shadow = NextShadow;
1921 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1922}
1923
David Blaikie99ba9e32011-12-20 02:48:34 +00001924void UsingDecl::anchor() { }
1925
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001926void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1927 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1928 "declaration already in set");
1929 assert(S->getUsingDecl() == this);
1930
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001931 if (FirstUsingShadow.getPointer())
1932 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1933 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001934}
1935
1936void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1937 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1938 "declaration not in set");
1939 assert(S->getUsingDecl() == this);
1940
1941 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1942
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001943 if (FirstUsingShadow.getPointer() == S) {
1944 FirstUsingShadow.setPointer(
1945 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001946 S->UsingOrNextShadow = this;
1947 return;
1948 }
1949
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001950 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001951 while (Prev->UsingOrNextShadow != S)
1952 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1953 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1954 S->UsingOrNextShadow = this;
1955}
1956
Douglas Gregordc355712011-02-25 00:36:19 +00001957UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1958 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001959 const DeclarationNameInfo &NameInfo,
1960 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001961 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001962}
1963
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001964UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1965 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1966 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1967 DeclarationNameInfo(), false);
1968}
1969
David Blaikie99ba9e32011-12-20 02:48:34 +00001970void UnresolvedUsingValueDecl::anchor() { }
1971
John McCall7ba107a2009-11-18 02:36:19 +00001972UnresolvedUsingValueDecl *
1973UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1974 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001975 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001976 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001977 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001978 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001979}
1980
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001981UnresolvedUsingValueDecl *
1982UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1983 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1984 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1985 NestedNameSpecifierLoc(),
1986 DeclarationNameInfo());
1987}
1988
David Blaikie99ba9e32011-12-20 02:48:34 +00001989void UnresolvedUsingTypenameDecl::anchor() { }
1990
John McCall7ba107a2009-11-18 02:36:19 +00001991UnresolvedUsingTypenameDecl *
1992UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1993 SourceLocation UsingLoc,
1994 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001995 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001996 SourceLocation TargetNameLoc,
1997 DeclarationName TargetName) {
1998 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001999 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002000 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00002001}
2002
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002003UnresolvedUsingTypenameDecl *
2004UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2005 void *Mem = AllocateDeserializedDecl(C, ID,
2006 sizeof(UnresolvedUsingTypenameDecl));
2007 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2008 SourceLocation(),
2009 NestedNameSpecifierLoc(),
2010 SourceLocation(),
2011 0);
2012}
2013
David Blaikie99ba9e32011-12-20 02:48:34 +00002014void StaticAssertDecl::anchor() { }
2015
Anders Carlssonfb311762009-03-14 00:25:26 +00002016StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002017 SourceLocation StaticAssertLoc,
2018 Expr *AssertExpr,
2019 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002020 SourceLocation RParenLoc,
2021 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002022 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002023 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00002024}
2025
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002026StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2027 unsigned ID) {
2028 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00002029 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
2030 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002031}
2032
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002033static const char *getAccessName(AccessSpecifier AS) {
2034 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002035 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002036 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002037 case AS_public:
2038 return "public";
2039 case AS_private:
2040 return "private";
2041 case AS_protected:
2042 return "protected";
2043 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002044 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002045}
2046
2047const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2048 AccessSpecifier AS) {
2049 return DB << getAccessName(AS);
2050}
Richard Smithf15fda02012-02-02 01:16:57 +00002051
2052const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2053 AccessSpecifier AS) {
2054 return DB << getAccessName(AS);
2055}