blob: d3a0c1ad4c45bacc9b0770aaa605e6904572973a [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
15#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000016#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000017#include "clang/AST/CXXInheritance.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/AST/DeclTemplate.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregor76852c22011-11-01 01:16:03 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000021#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000022#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000023#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Decl Allocation/Deallocation Method Implementations
29//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000030
David Blaikie99ba9e32011-12-20 02:48:34 +000031void AccessSpecDecl::anchor() { }
32
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000033AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
34 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(AccessSpecDecl));
35 return new (Mem) AccessSpecDecl(EmptyShell());
36}
37
John McCall86ff3082010-02-04 22:26:26 +000038CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Richard Smith7d04d3a2012-11-30 05:11:39 +000039 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
Eli Friedman97c134e2009-08-15 22:23:00 +000040 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000041 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000042 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000043 HasMutableFields(false), HasOnlyCMembers(true),
Richard Smithd5bc8672012-12-08 02:01:17 +000044 HasInClassInitializer(false), HasUninitializedReferenceMember(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000045 HasTrivialSpecialMembers(SMF_All),
46 HasIrrelevantDestructor(true),
Richard Smith61802452011-12-22 02:22:31 +000047 HasConstexprNonCopyMoveConstructor(false),
48 DefaultedDefaultConstructorIsConstexpr(true),
Richard Smith7d04d3a2012-11-30 05:11:39 +000049 HasConstexprDefaultConstructor(false),
Sean Hunt023df372011-05-09 18:22:59 +000050 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000051 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smithacf796b2012-11-28 06:23:12 +000052 ImplicitCopyConstructorHasConstParam(true),
53 ImplicitCopyAssignmentHasConstParam(true),
54 HasDeclaredCopyConstructorWithConstParam(false),
55 HasDeclaredCopyAssignmentWithConstParam(false),
56 FailedImplicitMoveConstructor(false), FailedImplicitMoveAssignment(false),
57 IsLambda(false), NumBases(0), NumVBases(0), Bases(), VBases(),
58 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000059}
60
Benjamin Krameree3096a2012-07-04 17:03:33 +000061CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
62 return Bases.get(Definition->getASTContext().getExternalSource());
63}
64
65CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
66 return VBases.get(Definition->getASTContext().getExternalSource());
67}
68
John McCall86ff3082010-02-04 22:26:26 +000069CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000070 SourceLocation StartLoc, SourceLocation IdLoc,
71 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
72 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000073 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000074 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000075
Jay Foad4ba2a172011-01-12 09:06:06 +000076CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000077 DeclContext *DC, SourceLocation StartLoc,
78 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000079 CXXRecordDecl* PrevDecl,
80 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000081 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
82 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000083
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000084 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000085 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000086 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000087 return R;
88}
89
Douglas Gregorda8962a2012-02-13 15:44:47 +000090CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Eli Friedman8da8a662012-09-19 01:18:11 +000091 TypeSourceInfo *Info, SourceLocation Loc,
92 bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +000093 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
94 0, 0);
95 R->IsBeingDefined = true;
Eli Friedman8da8a662012-09-19 01:18:11 +000096 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info, Dependent);
Douglas Gregorda8962a2012-02-13 15:44:47 +000097 C.getTypeDeclType(R, /*PrevDecl=*/0);
98 return R;
99}
100
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000101CXXRecordDecl *
102CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
103 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
104 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
105 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000106}
107
Mike Stump1eb44332009-09-09 15:08:12 +0000108void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000109CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000110 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000111 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000112
Douglas Gregor7c789c12010-10-29 22:39:52 +0000113 if (!data().Bases.isOffset() && data().NumBases > 0)
114 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Richard Smithdd677232011-10-18 20:08:55 +0000116 if (NumBases) {
117 // C++ [dcl.init.aggr]p1:
118 // An aggregate is [...] a class with [...] no base classes [...].
119 data().Aggregate = false;
120
121 // C++ [class]p4:
122 // A POD-struct is an aggregate class...
123 data().PlainOldData = false;
124 }
125
Anders Carlsson6f6de732010-03-29 05:13:12 +0000126 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000127 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000128
129 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000130 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
John McCall86ff3082010-02-04 22:26:26 +0000132 data().Bases = new(C) CXXBaseSpecifier [NumBases];
133 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000134 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000135 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000136 // Keep track of inherited vbases for this base class.
137 const CXXBaseSpecifier *Base = Bases[i];
138 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000139 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000140 if (BaseType->isDependentType())
141 continue;
142 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000143 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000144
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000145 // A class with a non-empty base class is not empty.
146 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000147 if (!BaseClassDecl->isEmpty()) {
148 if (!data().Empty) {
149 // C++0x [class]p7:
150 // A standard-layout class is a class that:
151 // [...]
152 // -- either has no non-static data members in the most derived
153 // class and at most one base class with non-static data members,
154 // or has no base classes with non-static data members, and
155 // If this is the second non-empty base, then neither of these two
156 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000157 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000158 }
159
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000160 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000161 data().HasNoNonEmptyBases = false;
162 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000163
Douglas Gregor85606eb2010-09-28 20:50:54 +0000164 // C++ [class.virtual]p1:
165 // A class that declares or inherits a virtual function is called a
166 // polymorphic class.
167 if (BaseClassDecl->isPolymorphic())
168 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000169
Chandler Carrutha8225442011-04-30 09:17:45 +0000170 // C++0x [class]p7:
171 // A standard-layout class is a class that: [...]
172 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000173 if (!BaseClassDecl->isStandardLayout())
174 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000175
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000176 // Record if this base is the first non-literal field or base.
177 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
178 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000179
Anders Carlsson6f6de732010-03-29 05:13:12 +0000180 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000181 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000182 BaseClassDecl->vbases_begin(),
183 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000184 // Add this base if it's not already in the list.
Richard Smithacf796b2012-11-28 06:23:12 +0000185 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000186 VBases.push_back(VBase);
Richard Smithacf796b2012-11-28 06:23:12 +0000187
188 // C++11 [class.copy]p8:
189 // The implicitly-declared copy constructor for a class X will have
190 // the form 'X::X(const X&)' if each [...] virtual base class B of X
191 // has a copy constructor whose first parameter is of type
192 // 'const B&' or 'const volatile B&' [...]
193 if (CXXRecordDecl *VBaseDecl = VBase->getType()->getAsCXXRecordDecl())
194 if (!VBaseDecl->hasCopyConstructorWithConstParam())
195 data().ImplicitCopyConstructorHasConstParam = false;
196 }
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000197 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000198
199 if (Base->isVirtual()) {
200 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000201 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Richard Smithacf796b2012-11-28 06:23:12 +0000202 VBases.push_back(Base);
203
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000204 // C++0x [meta.unary.prop] is_empty:
205 // T is a class type, but not a union type, with ... no virtual base
206 // classes
207 data().Empty = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000208
Richard Smith7d04d3a2012-11-30 05:11:39 +0000209 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
210 // A [default constructor, copy/move constructor, or copy/move assignment
211 // operator for a class X] is trivial [...] if:
212 // -- class X has [...] no virtual base classes
213 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carrutha8225442011-04-30 09:17:45 +0000214
215 // C++0x [class]p7:
216 // A standard-layout class is a class that: [...]
217 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000218 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000219
220 // C++11 [dcl.constexpr]p4:
221 // In the definition of a constexpr constructor [...]
222 // -- the class shall not have any virtual base classes
223 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000224 } else {
225 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000226 // A default constructor is trivial [...] if:
227 // -- all the direct base classes of its class have trivial default
228 // constructors.
229 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000230 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
231
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000232 // C++0x [class.copy]p13:
233 // A copy/move constructor for class X is trivial if [...]
234 // [...]
235 // -- the constructor selected to copy/move each direct base class
236 // subobject is trivial, and
237 // FIXME: C++0x: We need to only consider the selected constructor
Richard Smith93af2b82012-11-14 07:36:28 +0000238 // instead of all of them. For now, we treat a move constructor as being
239 // non-trivial if it calls anything other than a trivial move constructor.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000240 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000241 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith426391c2012-11-16 00:53:38 +0000242 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000243 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000244
245 // C++0x [class.copy]p27:
246 // A copy/move assignment operator for class X is trivial if [...]
247 // [...]
248 // -- the assignment operator selected to copy/move each direct base
249 // class subobject is trivial, and
250 // FIXME: C++0x: We need to only consider the selected operator instead
251 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000252 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000253 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith426391c2012-11-16 00:53:38 +0000254 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000255 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smith61802452011-12-22 02:22:31 +0000256
257 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000258 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000259 // requirements of a constexpr constructor, the implicitly-defined
260 // default constructor is constexpr.
261 if (!BaseClassDecl->hasConstexprDefaultConstructor())
262 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000263 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000264
265 // C++ [class.ctor]p3:
266 // A destructor is trivial if all the direct base classes of its class
267 // have trivial destructors.
268 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000269 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000270
271 if (!BaseClassDecl->hasIrrelevantDestructor())
272 data().HasIrrelevantDestructor = false;
273
Richard Smithacf796b2012-11-28 06:23:12 +0000274 // C++11 [class.copy]p18:
275 // The implicitly-declared copy assignment oeprator for a class X will
276 // have the form 'X& X::operator=(const X&)' if each direct base class B
277 // of X has a copy assignment operator whose parameter is of type 'const
278 // B&', 'const volatile B&', or 'B' [...]
279 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
280 data().ImplicitCopyAssignmentHasConstParam = false;
281
282 // C++11 [class.copy]p8:
283 // The implicitly-declared copy constructor for a class X will have
284 // the form 'X::X(const X&)' if each direct [...] base class B of X
285 // has a copy constructor whose first parameter is of type
286 // 'const B&' or 'const volatile B&' [...]
287 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
288 data().ImplicitCopyConstructorHasConstParam = false;
289
John McCallf85e1932011-06-15 23:02:42 +0000290 // A class has an Objective-C object member if... or any of its bases
291 // has an Objective-C object member.
292 if (BaseClassDecl->hasObjectMember())
293 setHasObjectMember(true);
294
Douglas Gregor2bb11012011-05-13 01:05:07 +0000295 // Keep track of the presence of mutable fields.
296 if (BaseClassDecl->hasMutableFields())
297 data().HasMutableFields = true;
Richard Smithd5bc8672012-12-08 02:01:17 +0000298
299 if (BaseClassDecl->hasUninitializedReferenceMember())
300 data().HasUninitializedReferenceMember = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000301 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000302
303 if (VBases.empty())
304 return;
305
306 // Create base specifier for any direct or indirect virtual bases.
307 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
308 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000309 for (int I = 0, E = VBases.size(); I != E; ++I)
310 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000311}
312
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000313/// Callback function for CXXRecordDecl::forallBases that acknowledges
314/// that it saw a base class.
315static bool SawBase(const CXXRecordDecl *, void *) {
316 return true;
317}
318
319bool CXXRecordDecl::hasAnyDependentBases() const {
320 if (!isDependentContext())
321 return false;
322
323 return !forallBases(SawBase, 0);
324}
325
Sean Huntffe37fd2011-05-25 20:50:04 +0000326bool CXXRecordDecl::hasConstCopyConstructor() const {
327 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000328}
329
Chandler Carruthb7e95892011-04-23 10:47:28 +0000330bool CXXRecordDecl::isTriviallyCopyable() const {
331 // C++0x [class]p5:
332 // A trivially copyable class is a class that:
333 // -- has no non-trivial copy constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000334 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000335 // -- has no non-trivial move constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000336 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000337 // -- has no non-trivial copy assignment operators,
Richard Smith426391c2012-11-16 00:53:38 +0000338 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000339 // -- has no non-trivial move assignment operators, and
Richard Smith426391c2012-11-16 00:53:38 +0000340 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000341 // -- has a trivial destructor.
342 if (!hasTrivialDestructor()) return false;
343
344 return true;
345}
346
Douglas Gregor0d405db2010-07-01 20:59:04 +0000347/// \brief Perform a simplistic form of overload resolution that only considers
348/// cv-qualifiers on a single parameter, and return the best overload candidate
349/// (if there is one).
350static CXXMethodDecl *
351GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000352 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000353 if (Cands.empty())
354 return 0;
355 if (Cands.size() == 1)
356 return Cands[0].first;
357
358 unsigned Best = 0, N = Cands.size();
359 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000360 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000361 Best = I;
362
Benjamin Kramer2cd7f412012-07-30 15:53:26 +0000363 for (unsigned I = 0; I != N; ++I)
364 if (I != Best && Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000365 return 0;
366
367 return Cands[Best].first;
368}
369
Sean Huntffe37fd2011-05-25 20:50:04 +0000370CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
371 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000372 QualType ClassType
373 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000374 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000375 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000376 Context.getCanonicalType(ClassType));
377 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000378 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000379 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000380 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000381 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000382 // C++ [class.copy]p2:
383 // A non-template constructor for class X is a copy constructor if [...]
384 if (isa<FunctionTemplateDecl>(*Con))
385 continue;
386
Douglas Gregor0d405db2010-07-01 20:59:04 +0000387 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
388 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000389 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
390 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000391 Found.push_back(std::make_pair(
392 const_cast<CXXConstructorDecl *>(Constructor),
393 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000394 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000395 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000396
397 return cast_or_null<CXXConstructorDecl>(
398 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000399}
400
Sean Huntffe37fd2011-05-25 20:50:04 +0000401CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
402 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
403 if (I->isMoveConstructor())
David Blaikie581deb32012-06-06 20:45:41 +0000404 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000405
406 return 0;
407}
408
Douglas Gregorb87786f2010-07-01 17:48:08 +0000409CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
410 ASTContext &Context = getASTContext();
411 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
412 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
413
Chris Lattner5f9e2722011-07-23 10:55:15 +0000414 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000415 DeclContext::lookup_const_iterator Op, OpEnd;
416 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
417 // C++ [class.copy]p9:
418 // A user-declared copy assignment operator is a non-static non-template
419 // member function of class X with exactly one parameter of type X, X&,
420 // const X&, volatile X& or const volatile X&.
421 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
422 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
423 continue;
424
425 const FunctionProtoType *FnType
426 = Method->getType()->getAs<FunctionProtoType>();
427 assert(FnType && "Overloaded operator has no prototype.");
428 // Don't assert on this; an invalid decl might have been left in the AST.
429 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
430 continue;
431
432 QualType ArgType = FnType->getArgType(0);
433 Qualifiers Quals;
434 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
435 ArgType = Ref->getPointeeType();
436 // If we have a const argument and we have a reference to a non-const,
437 // this function does not match.
438 if (ArgIsConst && !ArgType.isConstQualified())
439 continue;
440
441 Quals = ArgType.getQualifiers();
442 } else {
443 // By-value copy-assignment operators are treated like const X&
444 // copy-assignment operators.
445 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
446 }
447
448 if (!Context.hasSameUnqualifiedType(ArgType, Class))
449 continue;
450
451 // Save this copy-assignment operator. It might be "the one".
452 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
453 }
454
455 // Use a simplistic form of overload resolution to find the candidate.
456 return GetBestOverloadCandidateSimple(Found);
457}
458
Sean Huntffe37fd2011-05-25 20:50:04 +0000459CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
460 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
461 if (I->isMoveAssignmentOperator())
David Blaikie581deb32012-06-06 20:45:41 +0000462 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000463
464 return 0;
465}
466
Douglas Gregor21386642010-09-28 21:55:22 +0000467void CXXRecordDecl::markedVirtualFunctionPure() {
468 // C++ [class.abstract]p2:
469 // A class is abstract if it has at least one pure virtual function.
470 data().Abstract = true;
471}
472
Richard Smith3f5f5582012-06-08 21:09:22 +0000473void CXXRecordDecl::markedConstructorConstexpr(CXXConstructorDecl *CD) {
Richard Smithd3861ce2012-06-10 07:07:24 +0000474 if (!CD->isCopyOrMoveConstructor())
Richard Smith3f5f5582012-06-08 21:09:22 +0000475 data().HasConstexprNonCopyMoveConstructor = true;
476
477 if (CD->isDefaultConstructor())
478 data().HasConstexprDefaultConstructor = true;
479}
480
Douglas Gregor21386642010-09-28 21:55:22 +0000481void CXXRecordDecl::addedMember(Decl *D) {
Joao Matos17d35c32012-08-31 22:18:20 +0000482 if (!D->isImplicit() &&
483 !isa<FieldDecl>(D) &&
484 !isa<IndirectFieldDecl>(D) &&
485 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
486 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
487 data().HasOnlyCMembers = false;
488
489 // Ignore friends and invalid declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000490 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000491 return;
492
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000493 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
494 if (FunTmpl)
495 D = FunTmpl->getTemplatedDecl();
496
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000497 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
498 if (Method->isVirtual()) {
499 // C++ [dcl.init.aggr]p1:
500 // An aggregate is an array or a class with [...] no virtual functions.
501 data().Aggregate = false;
502
503 // C++ [class]p4:
504 // A POD-struct is an aggregate class...
505 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000506
507 // Virtual functions make the class non-empty.
508 // FIXME: Standard ref?
509 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000510
511 // C++ [class.virtual]p1:
512 // A class that declares or inherits a virtual function is called a
513 // polymorphic class.
514 data().Polymorphic = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000515
Richard Smith7d04d3a2012-11-30 05:11:39 +0000516 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
517 // A [default constructor, copy/move constructor, or copy/move
518 // assignment operator for a class X] is trivial [...] if:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000519 // -- class X has no virtual functions [...]
Richard Smith7d04d3a2012-11-30 05:11:39 +0000520 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000521
Chandler Carrutha8225442011-04-30 09:17:45 +0000522 // C++0x [class]p7:
523 // A standard-layout class is a class that: [...]
524 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000525 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000526 }
527 }
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000528
Richard Smithacf796b2012-11-28 06:23:12 +0000529 // Notify the listener if an implicit member was added after the definition
530 // was completed.
531 if (!isBeingDefined() && D->isImplicit())
532 if (ASTMutationListener *L = getASTMutationListener())
533 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000534
Richard Smith7d04d3a2012-11-30 05:11:39 +0000535 // The kind of special member this declaration is, if any.
536 unsigned SMKind = 0;
537
Richard Smithacf796b2012-11-28 06:23:12 +0000538 // Handle constructors.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000539 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smithacf796b2012-11-28 06:23:12 +0000540 if (!Constructor->isImplicit()) {
541 // Note that we have a user-declared constructor.
542 data().UserDeclaredConstructor = true;
543
544 // C++ [class]p4:
545 // A POD-struct is an aggregate class [...]
546 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
547 // type is technically an aggregate in C++0x since it wouldn't be in 03.
548 data().PlainOldData = false;
549 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000550
Richard Smith017ab772011-09-05 02:13:09 +0000551 // Technically, "user-provided" is only defined for special member
552 // functions, but the intent of the standard is clearly that it should apply
553 // to all functions.
554 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000555
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000556 if (Constructor->isDefaultConstructor()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000557 SMKind |= SMF_DefaultConstructor;
558
559 if (UserProvided)
Sean Huntcdee3fe2011-05-11 22:34:38 +0000560 data().UserProvidedDefaultConstructor = true;
Richard Smithacf796b2012-11-28 06:23:12 +0000561 if (Constructor->isConstexpr())
Richard Smith61802452011-12-22 02:22:31 +0000562 data().HasConstexprDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000563 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000564
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000565 if (!FunTmpl) {
Richard Smithacf796b2012-11-28 06:23:12 +0000566 unsigned Quals;
567 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000568 SMKind |= SMF_CopyConstructor;
Richard Smithacf796b2012-11-28 06:23:12 +0000569
570 if (Quals & Qualifiers::Const)
571 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith7d04d3a2012-11-30 05:11:39 +0000572 } else if (Constructor->isMoveConstructor())
573 SMKind |= SMF_MoveConstructor;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000574 }
Richard Smithacf796b2012-11-28 06:23:12 +0000575
576 // Record if we see any constexpr constructors which are neither copy
577 // nor move constructors.
578 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith6b8bc072011-08-10 18:11:37 +0000579 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000580
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000581 // C++ [dcl.init.aggr]p1:
582 // An aggregate is an array or a class with no user-declared
583 // constructors [...].
584 // C++0x [dcl.init.aggr]p1:
585 // An aggregate is an array or a class with no user-provided
586 // constructors [...].
Richard Smithacf796b2012-11-28 06:23:12 +0000587 if (getASTContext().getLangOpts().CPlusPlus0x
588 ? UserProvided : !Constructor->isImplicit())
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000589 data().Aggregate = false;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000590 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000591
Richard Smithacf796b2012-11-28 06:23:12 +0000592 // Handle destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000593 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000594 SMKind |= SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000595
Richard Smith7d04d3a2012-11-30 05:11:39 +0000596 if (!DD->isImplicit())
Richard Smithacf796b2012-11-28 06:23:12 +0000597 data().HasIrrelevantDestructor = false;
598
Richard Smithacf796b2012-11-28 06:23:12 +0000599 // C++11 [class.dtor]p5:
Richard Smith7d04d3a2012-11-30 05:11:39 +0000600 // A destructor is trivial if [...] the destructor is not virtual.
601 if (DD->isVirtual())
602 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000603 }
Richard Smithacf796b2012-11-28 06:23:12 +0000604
605 // Handle member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000606 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000607 if (Method->isCopyAssignmentOperator()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000608 SMKind |= SMF_CopyAssignment;
Richard Smithacf796b2012-11-28 06:23:12 +0000609
610 const ReferenceType *ParamTy =
611 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
612 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
613 data().HasDeclaredCopyAssignmentWithConstParam = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000614 }
Sean Huntffe37fd2011-05-25 20:50:04 +0000615
Richard Smith7d04d3a2012-11-30 05:11:39 +0000616 if (Method->isMoveAssignmentOperator())
617 SMKind |= SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000618
Douglas Gregore80622f2010-09-29 04:25:11 +0000619 // Keep the list of conversion functions up-to-date.
620 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregore80622f2010-09-29 04:25:11 +0000621 // FIXME: We intentionally don't use the decl's access here because it
622 // hasn't been set yet. That's really just a misdesign in Sema.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000623 if (Conversion->getPrimaryTemplate()) {
624 // We don't record specializations.
625 } else if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000626 if (FunTmpl->getPreviousDecl())
627 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000628 FunTmpl);
629 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000630 data().Conversions.addDecl(getASTContext(), FunTmpl);
Douglas Gregore80622f2010-09-29 04:25:11 +0000631 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000632 if (Conversion->getPreviousDecl())
633 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000634 Conversion);
635 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000636 data().Conversions.addDecl(getASTContext(), Conversion);
Douglas Gregore80622f2010-09-29 04:25:11 +0000637 }
638 }
Richard Smithacf796b2012-11-28 06:23:12 +0000639
Richard Smith7d04d3a2012-11-30 05:11:39 +0000640 if (SMKind) {
641 // Note when we have declared a declared special member, and suppress the
642 // implicit declaration of this special member.
643 data().DeclaredSpecialMembers |= SMKind;
644
645 if (!Method->isImplicit()) {
646 data().UserDeclaredSpecialMembers |= SMKind;
647
648 // C++03 [class]p4:
649 // A POD-struct is an aggregate class that has [...] no user-defined
650 // copy assignment operator and no user-defined destructor.
651 //
652 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
653 // aggregates could not have any constructors, clear it even for an
654 // explicitly defaulted or deleted constructor.
655 // type is technically an aggregate in C++0x since it wouldn't be in 03.
656 //
657 // Also, a user-declared move assignment operator makes a class non-POD.
658 // This is an extension in C++03.
659 data().PlainOldData = false;
660 }
661
662 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25,
663 // C++11 [class.dtor]p5:
664 // A [special member] is trivial if it is not user-provided [...]
665 // FIXME: This is bogus. A class can have both (say) a trivial copy
666 // constructor *and* a user-provided copy constructor.
667 if (Method->isUserProvided())
668 data().HasTrivialSpecialMembers &= ~SMKind;
669 }
670
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000671 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000672 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000673
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000674 // Handle non-static data members.
675 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000676 // C++ [class.bit]p2:
677 // A declaration for a bit-field that omits the identifier declares an
678 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
679 // initialized.
680 if (Field->isUnnamedBitfield())
681 return;
682
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000683 // C++ [dcl.init.aggr]p1:
684 // An aggregate is an array or a class (clause 9) with [...] no
685 // private or protected non-static data members (clause 11).
686 //
687 // A POD must be an aggregate.
688 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
689 data().Aggregate = false;
690 data().PlainOldData = false;
691 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000692
693 // C++0x [class]p7:
694 // A standard-layout class is a class that:
695 // [...]
696 // -- has the same access control for all non-static data members,
697 switch (D->getAccess()) {
698 case AS_private: data().HasPrivateFields = true; break;
699 case AS_protected: data().HasProtectedFields = true; break;
700 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000701 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000702 };
703 if ((data().HasPrivateFields + data().HasProtectedFields +
704 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000705 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000706
Douglas Gregor2bb11012011-05-13 01:05:07 +0000707 // Keep track of the presence of mutable fields.
708 if (Field->isMutable())
709 data().HasMutableFields = true;
710
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000711 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000712 // A POD struct is a class that is both a trivial class and a
713 // standard-layout class, and has no non-static data members of type
714 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000715 //
716 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
717 // that does not explicitly have no lifetime makes the class a non-POD.
718 // However, we delay setting PlainOldData to false in this case so that
719 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000720 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000721 // In this case, the class will become a non-POD class when we complete
722 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000723 ASTContext &Context = getASTContext();
724 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000725 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000726 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000727 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
728 setHasObjectMember(true);
729 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000730 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000731
Chandler Carrutha8225442011-04-30 09:17:45 +0000732 if (T->isReferenceType()) {
Richard Smithd5bc8672012-12-08 02:01:17 +0000733 if (!Field->hasInClassInitializer())
734 data().HasUninitializedReferenceMember = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000735
Chandler Carrutha8225442011-04-30 09:17:45 +0000736 // C++0x [class]p7:
737 // A standard-layout class is a class that:
738 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000739 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000740 }
741
Richard Smith86c3ae42012-02-13 03:54:03 +0000742 // Record if this field is the first non-literal or volatile field or base.
743 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000744 data().HasNonLiteralTypeFieldsOrBases = true;
745
Richard Smith7a614d82011-06-11 17:19:42 +0000746 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000747 data().HasInClassInitializer = true;
748
749 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000750 // A default constructor is trivial if [...] no non-static data member
751 // of its class has a brace-or-equal-initializer.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000752 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith7a614d82011-06-11 17:19:42 +0000753
Richard Smithd079abf2012-05-07 01:07:30 +0000754 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000755 // An aggregate is a [...] class with [...] no
756 // brace-or-equal-initializers for non-static data members.
757 data().Aggregate = false;
758
Richard Smithd079abf2012-05-07 01:07:30 +0000759 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000760 // A POD struct is [...] a trivial class.
761 data().PlainOldData = false;
762 }
763
Douglas Gregor85606eb2010-09-28 20:50:54 +0000764 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
765 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
766 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000767 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000768 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000769 // -- for all the non-static data members of its class that are of
770 // class type (or array thereof), each such class has a trivial
771 // default constructor.
772 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000773 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000774
775 // C++0x [class.copy]p13:
776 // A copy/move constructor for class X is trivial if [...]
777 // [...]
778 // -- for each non-static data member of X that is of class type (or
779 // an array thereof), the constructor selected to copy/move that
780 // member is trivial;
781 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000782 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000783 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith426391c2012-11-16 00:53:38 +0000784 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000785 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000786
787 // C++0x [class.copy]p27:
788 // A copy/move assignment operator for class X is trivial if [...]
789 // [...]
790 // -- for each non-static data member of X that is of class type (or
791 // an array thereof), the assignment operator selected to
792 // copy/move that member is trivial;
793 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000794 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000795 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith426391c2012-11-16 00:53:38 +0000796 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000797 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000798
Douglas Gregor85606eb2010-09-28 20:50:54 +0000799 if (!FieldRec->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000800 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000801 if (!FieldRec->hasIrrelevantDestructor())
802 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000803 if (FieldRec->hasObjectMember())
804 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000805
806 // C++0x [class]p7:
807 // A standard-layout class is a class that:
808 // -- has no non-static data members of type non-standard-layout
809 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000810 if (!FieldRec->isStandardLayout())
811 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000812
813 // C++0x [class]p7:
814 // A standard-layout class is a class that:
815 // [...]
816 // -- has no base classes of the same type as the first non-static
817 // data member.
818 // We don't want to expend bits in the state of the record decl
819 // tracking whether this is the first non-static data member so we
820 // cheat a bit and use some of the existing state: the empty bit.
821 // Virtual bases and virtual methods make a class non-empty, but they
822 // also make it non-standard-layout so we needn't check here.
823 // A non-empty base class may leave the class standard-layout, but not
824 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000825 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000826 // data member must come through here with Empty still true, and Empty
827 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000828 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000829 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
830 BE = bases_end();
831 BI != BE; ++BI) {
832 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000833 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000834 break;
835 }
836 }
837 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000838
839 // Keep track of the presence of mutable fields.
840 if (FieldRec->hasMutableFields())
841 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000842
843 // C++11 [class.copy]p13:
844 // If the implicitly-defined constructor would satisfy the
845 // requirements of a constexpr constructor, the implicitly-defined
846 // constructor is constexpr.
847 // C++11 [dcl.constexpr]p4:
848 // -- every constructor involved in initializing non-static data
849 // members [...] shall be a constexpr constructor
850 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000851 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000852 // The standard requires any in-class initializer to be a constant
853 // expression. We consider this to be a defect.
854 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithacf796b2012-11-28 06:23:12 +0000855
856 // C++11 [class.copy]p8:
857 // The implicitly-declared copy constructor for a class X will have
858 // the form 'X::X(const X&)' if [...] for all the non-static data
859 // members of X that are of a class type M (or array thereof), each
860 // such class type has a copy constructor whose first parameter is
861 // of type 'const M&' or 'const volatile M&'.
862 if (!FieldRec->hasCopyConstructorWithConstParam())
863 data().ImplicitCopyConstructorHasConstParam = false;
864
865 // C++11 [class.copy]p18:
866 // The implicitly-declared copy assignment oeprator for a class X will
867 // have the form 'X& X::operator=(const X&)' if [...] for all the
868 // non-static data members of X that are of a class type M (or array
869 // thereof), each such class type has a copy assignment operator whose
870 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
871 if (!FieldRec->hasCopyAssignmentWithConstParam())
872 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smithd5bc8672012-12-08 02:01:17 +0000873
874 if (FieldRec->hasUninitializedReferenceMember() &&
875 !Field->hasInClassInitializer())
876 data().HasUninitializedReferenceMember = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000877 }
Richard Smith61802452011-12-22 02:22:31 +0000878 } else {
879 // Base element type of field is a non-class type.
Richard Smithd3861ce2012-06-10 07:07:24 +0000880 if (!T->isLiteralType() ||
881 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000882 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000883 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000884
885 // C++0x [class]p7:
886 // A standard-layout class is a class that:
887 // [...]
888 // -- either has no non-static data members in the most derived
889 // class and at most one base class with non-static data members,
890 // or has no base classes with non-static data members, and
891 // At this point we know that we have a non-static data member, so the last
892 // clause holds.
893 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000894 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000895
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000896 // If this is not a zero-length bit-field, then the class is not empty.
897 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000898 if (!Field->isBitField() ||
899 (!Field->getBitWidth()->isTypeDependent() &&
900 !Field->getBitWidth()->isValueDependent() &&
901 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000902 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000903 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000904 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000905
906 // Handle using declarations of conversion functions.
907 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
908 if (Shadow->getDeclName().getNameKind()
909 == DeclarationName::CXXConversionFunctionName)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000910 data().Conversions.addDecl(getASTContext(), Shadow, Shadow->getAccess());
Joao Matos17d35c32012-08-31 22:18:20 +0000911}
912
913bool CXXRecordDecl::isCLike() const {
914 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
915 !TemplateOrInstantiation.isNull())
916 return false;
917 if (!hasDefinition())
918 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000919
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000920 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000921}
922
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000923void CXXRecordDecl::getCaptureFields(
924 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000925 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000926 Captures.clear();
927 ThisCapture = 0;
928
Douglas Gregorda8962a2012-02-13 15:44:47 +0000929 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000930 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000931 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000932 C != CEnd; ++C, ++Field) {
933 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +0000934 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000935 continue;
936 }
937
David Blaikie581deb32012-06-06 20:45:41 +0000938 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000939 }
940}
941
942
John McCallb05b5f32010-03-15 09:07:48 +0000943static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
944 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000945 if (isa<UsingShadowDecl>(Conv))
946 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000947 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
948 T = ConvTemp->getTemplatedDecl()->getResultType();
949 else
950 T = cast<CXXConversionDecl>(Conv)->getConversionType();
951 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000952}
953
John McCallb05b5f32010-03-15 09:07:48 +0000954/// Collect the visible conversions of a base class.
955///
James Dennetta1253502012-06-15 22:28:09 +0000956/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +0000957/// \param InVirtual whether this base class is a virtual base (or a base
958/// of a virtual base)
959/// \param Access the access along the inheritance path to this base
960/// \param ParentHiddenTypes the conversions provided by the inheritors
961/// of this base
962/// \param Output the set to which to add conversions from non-virtual bases
963/// \param VOutput the set to which to add conversions from virtual bases
964/// \param HiddenVBaseCs the set of conversions which were hidden in a
965/// virtual base along some inheritance path
966static void CollectVisibleConversions(ASTContext &Context,
967 CXXRecordDecl *Record,
968 bool InVirtual,
969 AccessSpecifier Access,
970 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000971 ASTUnresolvedSet &Output,
John McCallb05b5f32010-03-15 09:07:48 +0000972 UnresolvedSetImpl &VOutput,
973 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
974 // The set of types which have conversions in this class or its
975 // subclasses. As an optimization, we don't copy the derived set
976 // unless it might change.
977 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
978 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
979
980 // Collect the direct conversions and figure out which conversions
981 // will be hidden in the subclasses.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000982 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
983 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
984 if (ConvI != ConvE) {
John McCallb05b5f32010-03-15 09:07:48 +0000985 HiddenTypesBuffer = ParentHiddenTypes;
986 HiddenTypes = &HiddenTypesBuffer;
987
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000988 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +0000989 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
990 bool Hidden = ParentHiddenTypes.count(ConvType);
991 if (!Hidden)
992 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +0000993
994 // If this conversion is hidden and we're in a virtual base,
995 // remember that it's hidden along some inheritance path.
996 if (Hidden && InVirtual)
997 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
998
999 // If this conversion isn't hidden, add it to the appropriate output.
1000 else if (!Hidden) {
1001 AccessSpecifier IAccess
1002 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1003
1004 if (InVirtual)
1005 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001006 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001007 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001008 }
1009 }
1010 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001011
John McCallb05b5f32010-03-15 09:07:48 +00001012 // Collect information recursively from any base classes.
1013 for (CXXRecordDecl::base_class_iterator
1014 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1015 const RecordType *RT = I->getType()->getAs<RecordType>();
1016 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001017
John McCallb05b5f32010-03-15 09:07:48 +00001018 AccessSpecifier BaseAccess
1019 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1020 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001021
John McCallb05b5f32010-03-15 09:07:48 +00001022 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1023 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1024 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001025 }
John McCallb05b5f32010-03-15 09:07:48 +00001026}
Sebastian Redl9994a342009-10-25 17:03:50 +00001027
John McCallb05b5f32010-03-15 09:07:48 +00001028/// Collect the visible conversions of a class.
1029///
1030/// This would be extremely straightforward if it weren't for virtual
1031/// bases. It might be worth special-casing that, really.
1032static void CollectVisibleConversions(ASTContext &Context,
1033 CXXRecordDecl *Record,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001034 ASTUnresolvedSet &Output) {
John McCallb05b5f32010-03-15 09:07:48 +00001035 // The collection of all conversions in virtual bases that we've
1036 // found. These will be added to the output as long as they don't
1037 // appear in the hidden-conversions set.
1038 UnresolvedSet<8> VBaseCs;
1039
1040 // The set of conversions in virtual bases that we've determined to
1041 // be hidden.
1042 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1043
1044 // The set of types hidden by classes derived from this one.
1045 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1046
1047 // Go ahead and collect the direct conversions and add them to the
1048 // hidden-types set.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001049 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1050 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001051 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001052 for (; ConvI != ConvE; ++ConvI)
1053 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCallb05b5f32010-03-15 09:07:48 +00001054
1055 // Recursively collect conversions from base classes.
1056 for (CXXRecordDecl::base_class_iterator
1057 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1058 const RecordType *RT = I->getType()->getAs<RecordType>();
1059 if (!RT) continue;
1060
1061 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1062 I->isVirtual(), I->getAccessSpecifier(),
1063 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1064 }
1065
1066 // Add any unhidden conversions provided by virtual bases.
1067 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1068 I != E; ++I) {
1069 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001070 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001071 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001072}
1073
1074/// getVisibleConversionFunctions - get all conversion functions visible
1075/// in current class; including conversion function templates.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001076std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1077CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001078 // If root class, all conversions are visible.
1079 if (bases_begin() == bases_end())
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001080 return std::make_pair(data().Conversions.begin(), data().Conversions.end());
Fariborz Jahanian62509212009-09-12 18:26:03 +00001081 // If visible conversion list is already evaluated, return it.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001082 if (!data().ComputedVisibleConversions) {
1083 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
1084 data().ComputedVisibleConversions = true;
1085 }
1086 return std::make_pair(data().VisibleConversions.begin(),
1087 data().VisibleConversions.end());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001088}
1089
John McCall32daa422010-03-31 01:36:47 +00001090void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1091 // This operation is O(N) but extremely rare. Sema only uses it to
1092 // remove UsingShadowDecls in a class that were followed by a direct
1093 // declaration, e.g.:
1094 // class A : B {
1095 // using B::operator int;
1096 // operator int();
1097 // };
1098 // This is uncommon by itself and even more uncommon in conjunction
1099 // with sufficiently large numbers of directly-declared conversions
1100 // that asymptotic behavior matters.
1101
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001102 ASTUnresolvedSet &Convs = data().Conversions;
John McCall32daa422010-03-31 01:36:47 +00001103 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1104 if (Convs[I].getDecl() == ConvDecl) {
1105 Convs.erase(I);
1106 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1107 && "conversion was found multiple times in unresolved set");
1108 return;
1109 }
1110 }
1111
1112 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001113}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001114
Douglas Gregorf6b11852009-10-08 15:14:33 +00001115CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001116 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001117 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1118
1119 return 0;
1120}
1121
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001122MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1123 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1124}
1125
Douglas Gregorf6b11852009-10-08 15:14:33 +00001126void
1127CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1128 TemplateSpecializationKind TSK) {
1129 assert(TemplateOrInstantiation.isNull() &&
1130 "Previous template or instantiation?");
1131 assert(!isa<ClassTemplateSpecializationDecl>(this));
1132 TemplateOrInstantiation
1133 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1134}
1135
Anders Carlssonb13e3572009-12-07 06:33:48 +00001136TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1137 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001138 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1139 return Spec->getSpecializationKind();
1140
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001141 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001142 return MSInfo->getTemplateSpecializationKind();
1143
1144 return TSK_Undeclared;
1145}
1146
1147void
1148CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1149 if (ClassTemplateSpecializationDecl *Spec
1150 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1151 Spec->setSpecializationKind(TSK);
1152 return;
1153 }
1154
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001155 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001156 MSInfo->setTemplateSpecializationKind(TSK);
1157 return;
1158 }
1159
David Blaikieb219cfc2011-09-23 05:06:16 +00001160 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001161}
1162
Douglas Gregor1d110e02010-07-01 14:13:13 +00001163CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1164 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001165 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001166
1167 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001168 = Context.DeclarationNames.getCXXDestructorName(
1169 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001170
John McCallc0bf4622010-02-23 00:48:20 +00001171 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001172 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001173 if (I == E)
1174 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001176 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001177 return Dtor;
1178}
1179
Douglas Gregorda2142f2011-02-19 18:51:44 +00001180void CXXRecordDecl::completeDefinition() {
1181 completeDefinition(0);
1182}
1183
1184void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1185 RecordDecl::completeDefinition();
1186
David Blaikie4e4d0842012-03-11 07:00:24 +00001187 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001188 // Objective-C Automatic Reference Counting:
1189 // If a class has a non-static data member of Objective-C pointer
1190 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001191 // default constructor (if any), copy constructor, move constructor,
1192 // copy assignment operator, move assignment operator, and destructor are
1193 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001194 struct DefinitionData &Data = data();
1195 Data.PlainOldData = false;
Richard Smith7d04d3a2012-11-30 05:11:39 +00001196 Data.HasTrivialSpecialMembers = 0;
Richard Smithdfefb842012-02-25 07:33:38 +00001197 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001198 }
1199
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001200 // If the class may be abstract (but hasn't been marked as such), check for
1201 // any pure final overriders.
1202 if (mayBeAbstract()) {
1203 CXXFinalOverriderMap MyFinalOverriders;
1204 if (!FinalOverriders) {
1205 getFinalOverriders(MyFinalOverriders);
1206 FinalOverriders = &MyFinalOverriders;
1207 }
1208
1209 bool Done = false;
1210 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1211 MEnd = FinalOverriders->end();
1212 M != MEnd && !Done; ++M) {
1213 for (OverridingMethods::iterator SO = M->second.begin(),
1214 SOEnd = M->second.end();
1215 SO != SOEnd && !Done; ++SO) {
1216 assert(SO->second.size() > 0 &&
1217 "All virtual functions have overridding virtual functions");
1218
1219 // C++ [class.abstract]p4:
1220 // A class is abstract if it contains or inherits at least one
1221 // pure virtual function for which the final overrider is pure
1222 // virtual.
1223 if (SO->second.front().Method->isPure()) {
1224 data().Abstract = true;
1225 Done = true;
1226 break;
1227 }
1228 }
1229 }
1230 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001231
1232 // Set access bits correctly on the directly-declared conversions.
1233 for (UnresolvedSetIterator I = data().Conversions.begin(),
1234 E = data().Conversions.end();
1235 I != E; ++I)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001236 I.setAccess((*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001237}
1238
1239bool CXXRecordDecl::mayBeAbstract() const {
1240 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1241 isDependentContext())
1242 return false;
1243
1244 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1245 BEnd = bases_end();
1246 B != BEnd; ++B) {
1247 CXXRecordDecl *BaseDecl
1248 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1249 if (BaseDecl->isAbstract())
1250 return true;
1251 }
1252
1253 return false;
1254}
1255
David Blaikie99ba9e32011-12-20 02:48:34 +00001256void CXXMethodDecl::anchor() { }
1257
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001258static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1259 const CXXMethodDecl *BaseMD) {
1260 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1261 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1262 const CXXMethodDecl *MD = *I;
1263 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1264 return true;
1265 if (recursivelyOverrides(MD, BaseMD))
1266 return true;
1267 }
1268 return false;
1269}
1270
1271CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001272CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1273 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001274 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1275 return this;
1276
1277 // Lookup doesn't work for destructors, so handle them separately.
1278 if (isa<CXXDestructorDecl>(this)) {
1279 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001280 if (MD) {
1281 if (recursivelyOverrides(MD, this))
1282 return MD;
1283 if (MayBeBase && recursivelyOverrides(this, MD))
1284 return MD;
1285 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001286 return NULL;
1287 }
1288
1289 lookup_const_result Candidates = RD->lookup(getDeclName());
1290 for (NamedDecl * const * I = Candidates.first; I != Candidates.second; ++I) {
1291 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1292 if (!MD)
1293 continue;
1294 if (recursivelyOverrides(MD, this))
1295 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001296 if (MayBeBase && recursivelyOverrides(this, MD))
1297 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001298 }
1299
1300 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1301 E = RD->bases_end(); I != E; ++I) {
1302 const RecordType *RT = I->getType()->getAs<RecordType>();
1303 if (!RT)
1304 continue;
1305 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1306 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1307 if (T)
1308 return T;
1309 }
1310
1311 return NULL;
1312}
1313
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001314CXXMethodDecl *
1315CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001316 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001317 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001318 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001319 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001320 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001321 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001322 isStatic, SCAsWritten, isInline, isConstexpr,
1323 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001324}
1325
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001326CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1327 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1328 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1329 DeclarationNameInfo(), QualType(),
1330 0, false, SC_None, false, false,
1331 SourceLocation());
1332}
1333
Douglas Gregor90916562009-09-29 18:16:17 +00001334bool CXXMethodDecl::isUsualDeallocationFunction() const {
1335 if (getOverloadedOperator() != OO_Delete &&
1336 getOverloadedOperator() != OO_Array_Delete)
1337 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001338
1339 // C++ [basic.stc.dynamic.deallocation]p2:
1340 // A template instance is never a usual deallocation function,
1341 // regardless of its signature.
1342 if (getPrimaryTemplate())
1343 return false;
1344
Douglas Gregor90916562009-09-29 18:16:17 +00001345 // C++ [basic.stc.dynamic.deallocation]p2:
1346 // If a class T has a member deallocation function named operator delete
1347 // with exactly one parameter, then that function is a usual (non-placement)
1348 // deallocation function. [...]
1349 if (getNumParams() == 1)
1350 return true;
1351
1352 // C++ [basic.stc.dynamic.deallocation]p2:
1353 // [...] If class T does not declare such an operator delete but does
1354 // declare a member deallocation function named operator delete with
1355 // exactly two parameters, the second of which has type std::size_t (18.1),
1356 // then this function is a usual deallocation function.
1357 ASTContext &Context = getASTContext();
1358 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001359 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1360 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001361 return false;
1362
1363 // This function is a usual deallocation function if there are no
1364 // single-parameter deallocation functions of the same kind.
1365 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1366 R.first != R.second; ++R.first) {
1367 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1368 if (FD->getNumParams() == 1)
1369 return false;
1370 }
1371
1372 return true;
1373}
1374
Douglas Gregor06a9f362010-05-01 20:49:11 +00001375bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001376 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001377 // A user-declared copy assignment operator X::operator= is a non-static
1378 // non-template member function of class X with exactly one parameter of
1379 // type X, X&, const X&, volatile X& or const volatile X&.
1380 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1381 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001382 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001383 return false;
1384
1385 QualType ParamType = getParamDecl(0)->getType();
1386 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1387 ParamType = Ref->getPointeeType();
1388
1389 ASTContext &Context = getASTContext();
1390 QualType ClassType
1391 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1392 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1393}
1394
Sean Huntffe37fd2011-05-25 20:50:04 +00001395bool CXXMethodDecl::isMoveAssignmentOperator() const {
1396 // C++0x [class.copy]p19:
1397 // A user-declared move assignment operator X::operator= is a non-static
1398 // non-template member function of class X with exactly one parameter of type
1399 // X&&, const X&&, volatile X&&, or const volatile X&&.
1400 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1401 getPrimaryTemplate() || getDescribedFunctionTemplate())
1402 return false;
1403
1404 QualType ParamType = getParamDecl(0)->getType();
1405 if (!isa<RValueReferenceType>(ParamType))
1406 return false;
1407 ParamType = ParamType->getPointeeType();
1408
1409 ASTContext &Context = getASTContext();
1410 QualType ClassType
1411 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1412 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1413}
1414
Anders Carlsson05eb2442009-05-16 23:58:37 +00001415void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001416 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001417 assert(!MD->getParent()->isDependentContext() &&
1418 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001419 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001420
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001421 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001422}
1423
1424CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001425 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001426 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001427}
1428
1429CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001430 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001431 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001432}
1433
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001434unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001435 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001436 return getASTContext().overridden_methods_size(this);
1437}
1438
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001439QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001440 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1441 // If the member function is declared const, the type of this is const X*,
1442 // if the member function is declared volatile, the type of this is
1443 // volatile X*, and if the member function is declared const volatile,
1444 // the type of this is const volatile X*.
1445
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001446 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001447
John McCall3cb0ebd2010-03-10 03:28:59 +00001448 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001449 ClassTy = C.getQualifiedType(ClassTy,
1450 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001451 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001452}
1453
Eli Friedmand7d7f672009-12-06 20:50:05 +00001454bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001455 // If this function is a template instantiation, look at the template from
1456 // which it was instantiated.
1457 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1458 if (!CheckFn)
1459 CheckFn = this;
1460
Eli Friedmand7d7f672009-12-06 20:50:05 +00001461 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001462 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001463}
1464
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001465bool CXXMethodDecl::isLambdaStaticInvoker() const {
1466 return getParent()->isLambda() &&
1467 getIdentifier() && getIdentifier()->getName() == "__invoke";
1468}
1469
1470
Sean Huntcbb67482011-01-08 20:30:50 +00001471CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1472 TypeSourceInfo *TInfo, bool IsVirtual,
1473 SourceLocation L, Expr *Init,
1474 SourceLocation R,
1475 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001476 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001477 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1478 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001479{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001480}
1481
Sean Huntcbb67482011-01-08 20:30:50 +00001482CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1483 FieldDecl *Member,
1484 SourceLocation MemberLoc,
1485 SourceLocation L, Expr *Init,
1486 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001487 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001488 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001489 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1490{
1491}
1492
Sean Huntcbb67482011-01-08 20:30:50 +00001493CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1494 IndirectFieldDecl *Member,
1495 SourceLocation MemberLoc,
1496 SourceLocation L, Expr *Init,
1497 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001498 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001499 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001500 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001501{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001502}
1503
Sean Huntcbb67482011-01-08 20:30:50 +00001504CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001505 TypeSourceInfo *TInfo,
1506 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001507 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001508 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1509 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001510 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1511{
1512}
1513
1514CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001515 FieldDecl *Member,
1516 SourceLocation MemberLoc,
1517 SourceLocation L, Expr *Init,
1518 SourceLocation R,
1519 VarDecl **Indices,
1520 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001521 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001522 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001523 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001524{
1525 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1526 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1527}
1528
Sean Huntcbb67482011-01-08 20:30:50 +00001529CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1530 FieldDecl *Member,
1531 SourceLocation MemberLoc,
1532 SourceLocation L, Expr *Init,
1533 SourceLocation R,
1534 VarDecl **Indices,
1535 unsigned NumIndices) {
1536 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001537 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001538 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001539 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1540 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001541}
1542
Sean Huntcbb67482011-01-08 20:30:50 +00001543TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001544 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001545 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001546 else
1547 return TypeLoc();
1548}
1549
Sean Huntcbb67482011-01-08 20:30:50 +00001550const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001551 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001552 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001553 else
1554 return 0;
1555}
1556
Sean Huntcbb67482011-01-08 20:30:50 +00001557SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001558 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001559 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001560
1561 if (isInClassMemberInitializer())
1562 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001563
Douglas Gregor76852c22011-11-01 01:16:03 +00001564 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1565 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1566
1567 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001568}
1569
Sean Huntcbb67482011-01-08 20:30:50 +00001570SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001571 if (isInClassMemberInitializer()) {
1572 FieldDecl *D = getAnyMember();
1573 if (Expr *I = D->getInClassInitializer())
1574 return I->getSourceRange();
1575 return SourceRange();
1576 }
1577
Douglas Gregor802ab452009-12-02 22:36:29 +00001578 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001579}
1580
David Blaikie99ba9e32011-12-20 02:48:34 +00001581void CXXConstructorDecl::anchor() { }
1582
Douglas Gregorb48fe382008-10-31 09:07:45 +00001583CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001584CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1585 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1586 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1587 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001588}
1589
1590CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001591CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001592 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001593 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001594 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001595 bool isExplicit, bool isInline,
1596 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001597 assert(NameInfo.getName().getNameKind()
1598 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001599 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001600 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001601 isExplicit, isInline, isImplicitlyDeclared,
1602 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001603}
1604
Douglas Gregor76852c22011-11-01 01:16:03 +00001605CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1606 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1607 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1608 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1609 return Construct->getConstructor();
1610
1611 return 0;
1612}
1613
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001614bool CXXConstructorDecl::isDefaultConstructor() const {
1615 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001616 // A default constructor for a class X is a constructor of class
1617 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001618 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001619 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001620}
1621
Mike Stump1eb44332009-09-09 15:08:12 +00001622bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001623CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001624 return isCopyOrMoveConstructor(TypeQuals) &&
1625 getParamDecl(0)->getType()->isLValueReferenceType();
1626}
1627
1628bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1629 return isCopyOrMoveConstructor(TypeQuals) &&
1630 getParamDecl(0)->getType()->isRValueReferenceType();
1631}
1632
1633/// \brief Determine whether this is a copy or move constructor.
1634bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001635 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001636 // A non-template constructor for class X is a copy constructor
1637 // if its first parameter is of type X&, const X&, volatile X& or
1638 // const volatile X&, and either there are no other parameters
1639 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001640 // C++0x [class.copy]p3:
1641 // A non-template constructor for class X is a move constructor if its
1642 // first parameter is of type X&&, const X&&, volatile X&&, or
1643 // const volatile X&&, and either there are no other parameters or else
1644 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001645 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001646 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001647 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001648 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001649 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001650
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001651 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001652
1653 // Do we have a reference type?
1654 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001655 if (!ParamRefType)
1656 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001657
Douglas Gregorfd476482009-11-13 23:59:09 +00001658 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001659 ASTContext &Context = getASTContext();
1660
Douglas Gregorfd476482009-11-13 23:59:09 +00001661 CanQualType PointeeType
1662 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001663 CanQualType ClassTy
1664 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001665 if (PointeeType.getUnqualifiedType() != ClassTy)
1666 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001667
John McCall0953e762009-09-24 19:53:00 +00001668 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001669
1670 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001671 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001672 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001673}
1674
Anders Carlssonfaccd722009-08-28 16:57:08 +00001675bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001676 // C++ [class.conv.ctor]p1:
1677 // A constructor declared without the function-specifier explicit
1678 // that can be called with a single parameter specifies a
1679 // conversion from the type of its first parameter to the type of
1680 // its class. Such a constructor is called a converting
1681 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001682 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001683 return false;
1684
Mike Stump1eb44332009-09-09 15:08:12 +00001685 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001686 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001687 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001688 (getNumParams() > 1 &&
1689 (getParamDecl(1)->hasDefaultArg() ||
1690 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001691}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001692
Douglas Gregor6493cc52010-11-08 17:16:59 +00001693bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001694 if ((getNumParams() < 1) ||
1695 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1696 (getPrimaryTemplate() == 0) ||
1697 (getDescribedFunctionTemplate() != 0))
1698 return false;
1699
1700 const ParmVarDecl *Param = getParamDecl(0);
1701
1702 ASTContext &Context = getASTContext();
1703 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1704
Douglas Gregor66724ea2009-11-14 01:20:54 +00001705 // Is it the same as our our class type?
1706 CanQualType ClassTy
1707 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1708 if (ParamType.getUnqualifiedType() != ClassTy)
1709 return false;
1710
1711 return true;
1712}
1713
Sebastian Redlf677ea32011-02-05 19:23:19 +00001714const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1715 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001716 method_iterator It = getASTContext().overridden_methods_begin(this);
1717 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001718 return 0;
1719
1720 return cast<CXXConstructorDecl>(*It);
1721}
1722
1723void
1724CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1725 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001726 assert(getASTContext().overridden_methods_size(this) == 0 &&
1727 "Base ctor already set.");
1728 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001729}
1730
David Blaikie99ba9e32011-12-20 02:48:34 +00001731void CXXDestructorDecl::anchor() { }
1732
Douglas Gregor42a552f2008-11-05 20:51:48 +00001733CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001734CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1735 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1736 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001737 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001738}
1739
1740CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001741CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001742 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001743 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001744 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001745 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001746 assert(NameInfo.getName().getNameKind()
1747 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001748 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001749 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001750 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001751}
1752
David Blaikie99ba9e32011-12-20 02:48:34 +00001753void CXXConversionDecl::anchor() { }
1754
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001755CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001756CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1757 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1758 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1759 QualType(), 0, false, false, false,
1760 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001761}
1762
1763CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001764CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001765 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001766 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001767 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001768 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001769 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001770 assert(NameInfo.getName().getNameKind()
1771 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001772 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001773 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001774 isInline, isExplicit, isConstexpr,
1775 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001776}
1777
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001778bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1779 return isImplicit() && getParent()->isLambda() &&
1780 getConversionType()->isBlockPointerType();
1781}
1782
David Blaikie99ba9e32011-12-20 02:48:34 +00001783void LinkageSpecDecl::anchor() { }
1784
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001785LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001786 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001787 SourceLocation ExternLoc,
1788 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001789 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001790 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001791 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001792}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001793
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001794LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1795 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1796 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1797 lang_c, SourceLocation());
1798}
1799
David Blaikie99ba9e32011-12-20 02:48:34 +00001800void UsingDirectiveDecl::anchor() { }
1801
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001802UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1803 SourceLocation L,
1804 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001805 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001806 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001807 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001808 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001809 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1810 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001811 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1812 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001813}
1814
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001815UsingDirectiveDecl *
1816UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1817 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1818 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1819 NestedNameSpecifierLoc(),
1820 SourceLocation(), 0, 0);
1821}
1822
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001823NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1824 if (NamespaceAliasDecl *NA =
1825 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1826 return NA->getNamespace();
1827 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1828}
1829
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001830void NamespaceDecl::anchor() { }
1831
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001832NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1833 SourceLocation StartLoc,
1834 SourceLocation IdLoc, IdentifierInfo *Id,
1835 NamespaceDecl *PrevDecl)
1836 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1837 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1838{
1839 setPreviousDeclaration(PrevDecl);
1840
1841 if (PrevDecl)
1842 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1843}
1844
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001845NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001846 bool Inline, SourceLocation StartLoc,
1847 SourceLocation IdLoc, IdentifierInfo *Id,
1848 NamespaceDecl *PrevDecl) {
1849 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001850}
1851
1852NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1853 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001854 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1855 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001856}
1857
David Blaikie99ba9e32011-12-20 02:48:34 +00001858void NamespaceAliasDecl::anchor() { }
1859
Mike Stump1eb44332009-09-09 15:08:12 +00001860NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001861 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001862 SourceLocation AliasLoc,
1863 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001864 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001865 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001866 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001867 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1868 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001869 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1870 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001871}
1872
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001873NamespaceAliasDecl *
1874NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1875 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1876 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1877 NestedNameSpecifierLoc(),
1878 SourceLocation(), 0);
1879}
1880
David Blaikie99ba9e32011-12-20 02:48:34 +00001881void UsingShadowDecl::anchor() { }
1882
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001883UsingShadowDecl *
1884UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1885 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1886 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1887}
1888
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001889UsingDecl *UsingShadowDecl::getUsingDecl() const {
1890 const UsingShadowDecl *Shadow = this;
1891 while (const UsingShadowDecl *NextShadow =
1892 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1893 Shadow = NextShadow;
1894 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1895}
1896
David Blaikie99ba9e32011-12-20 02:48:34 +00001897void UsingDecl::anchor() { }
1898
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001899void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1900 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1901 "declaration already in set");
1902 assert(S->getUsingDecl() == this);
1903
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001904 if (FirstUsingShadow.getPointer())
1905 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1906 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001907}
1908
1909void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1910 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1911 "declaration not in set");
1912 assert(S->getUsingDecl() == this);
1913
1914 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1915
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001916 if (FirstUsingShadow.getPointer() == S) {
1917 FirstUsingShadow.setPointer(
1918 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001919 S->UsingOrNextShadow = this;
1920 return;
1921 }
1922
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001923 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001924 while (Prev->UsingOrNextShadow != S)
1925 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1926 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1927 S->UsingOrNextShadow = this;
1928}
1929
Douglas Gregordc355712011-02-25 00:36:19 +00001930UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1931 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001932 const DeclarationNameInfo &NameInfo,
1933 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001934 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001935}
1936
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001937UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1938 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1939 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1940 DeclarationNameInfo(), false);
1941}
1942
David Blaikie99ba9e32011-12-20 02:48:34 +00001943void UnresolvedUsingValueDecl::anchor() { }
1944
John McCall7ba107a2009-11-18 02:36:19 +00001945UnresolvedUsingValueDecl *
1946UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1947 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001948 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001949 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001950 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001951 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001952}
1953
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001954UnresolvedUsingValueDecl *
1955UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1956 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1957 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1958 NestedNameSpecifierLoc(),
1959 DeclarationNameInfo());
1960}
1961
David Blaikie99ba9e32011-12-20 02:48:34 +00001962void UnresolvedUsingTypenameDecl::anchor() { }
1963
John McCall7ba107a2009-11-18 02:36:19 +00001964UnresolvedUsingTypenameDecl *
1965UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1966 SourceLocation UsingLoc,
1967 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001968 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001969 SourceLocation TargetNameLoc,
1970 DeclarationName TargetName) {
1971 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001972 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001973 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001974}
1975
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001976UnresolvedUsingTypenameDecl *
1977UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1978 void *Mem = AllocateDeserializedDecl(C, ID,
1979 sizeof(UnresolvedUsingTypenameDecl));
1980 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
1981 SourceLocation(),
1982 NestedNameSpecifierLoc(),
1983 SourceLocation(),
1984 0);
1985}
1986
David Blaikie99ba9e32011-12-20 02:48:34 +00001987void StaticAssertDecl::anchor() { }
1988
Anders Carlssonfb311762009-03-14 00:25:26 +00001989StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001990 SourceLocation StaticAssertLoc,
1991 Expr *AssertExpr,
1992 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00001993 SourceLocation RParenLoc,
1994 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001995 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00001996 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00001997}
1998
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001999StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2000 unsigned ID) {
2001 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00002002 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
2003 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002004}
2005
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002006static const char *getAccessName(AccessSpecifier AS) {
2007 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002008 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002009 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002010 case AS_public:
2011 return "public";
2012 case AS_private:
2013 return "private";
2014 case AS_protected:
2015 return "protected";
2016 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002017 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002018}
2019
2020const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2021 AccessSpecifier AS) {
2022 return DB << getAccessName(AS);
2023}
Richard Smithf15fda02012-02-02 01:16:57 +00002024
2025const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2026 AccessSpecifier AS) {
2027 return DB << getAccessName(AS);
2028}