blob: ba877649315d7905c6c47d3f865af4e32e893d2d [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"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000017#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000018#include "clang/AST/CXXInheritance.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 Smithd079abf2012-05-07 01:07:30 +000044 HasInClassInitializer(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;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000298 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000299
300 if (VBases.empty())
301 return;
302
303 // Create base specifier for any direct or indirect virtual bases.
304 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
305 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000306 for (int I = 0, E = VBases.size(); I != E; ++I)
307 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000308}
309
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000310/// Callback function for CXXRecordDecl::forallBases that acknowledges
311/// that it saw a base class.
312static bool SawBase(const CXXRecordDecl *, void *) {
313 return true;
314}
315
316bool CXXRecordDecl::hasAnyDependentBases() const {
317 if (!isDependentContext())
318 return false;
319
320 return !forallBases(SawBase, 0);
321}
322
Sean Huntffe37fd2011-05-25 20:50:04 +0000323bool CXXRecordDecl::hasConstCopyConstructor() const {
324 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000325}
326
Chandler Carruthb7e95892011-04-23 10:47:28 +0000327bool CXXRecordDecl::isTriviallyCopyable() const {
328 // C++0x [class]p5:
329 // A trivially copyable class is a class that:
330 // -- has no non-trivial copy constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000331 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000332 // -- has no non-trivial move constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000333 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000334 // -- has no non-trivial copy assignment operators,
Richard Smith426391c2012-11-16 00:53:38 +0000335 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000336 // -- has no non-trivial move assignment operators, and
Richard Smith426391c2012-11-16 00:53:38 +0000337 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000338 // -- has a trivial destructor.
339 if (!hasTrivialDestructor()) return false;
340
341 return true;
342}
343
Douglas Gregor0d405db2010-07-01 20:59:04 +0000344/// \brief Perform a simplistic form of overload resolution that only considers
345/// cv-qualifiers on a single parameter, and return the best overload candidate
346/// (if there is one).
347static CXXMethodDecl *
348GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000349 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000350 if (Cands.empty())
351 return 0;
352 if (Cands.size() == 1)
353 return Cands[0].first;
354
355 unsigned Best = 0, N = Cands.size();
356 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000357 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000358 Best = I;
359
Benjamin Kramer2cd7f412012-07-30 15:53:26 +0000360 for (unsigned I = 0; I != N; ++I)
361 if (I != Best && Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000362 return 0;
363
364 return Cands[Best].first;
365}
366
Sean Huntffe37fd2011-05-25 20:50:04 +0000367CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
368 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000369 QualType ClassType
370 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000371 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000372 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000373 Context.getCanonicalType(ClassType));
374 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000375 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000376 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000377 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000378 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000379 // C++ [class.copy]p2:
380 // A non-template constructor for class X is a copy constructor if [...]
381 if (isa<FunctionTemplateDecl>(*Con))
382 continue;
383
Douglas Gregor0d405db2010-07-01 20:59:04 +0000384 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
385 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000386 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
387 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000388 Found.push_back(std::make_pair(
389 const_cast<CXXConstructorDecl *>(Constructor),
390 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000391 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000392 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000393
394 return cast_or_null<CXXConstructorDecl>(
395 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000396}
397
Sean Huntffe37fd2011-05-25 20:50:04 +0000398CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
399 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
400 if (I->isMoveConstructor())
David Blaikie581deb32012-06-06 20:45:41 +0000401 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000402
403 return 0;
404}
405
Douglas Gregorb87786f2010-07-01 17:48:08 +0000406CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
407 ASTContext &Context = getASTContext();
408 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
409 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
410
Chris Lattner5f9e2722011-07-23 10:55:15 +0000411 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000412 DeclContext::lookup_const_iterator Op, OpEnd;
413 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
414 // C++ [class.copy]p9:
415 // A user-declared copy assignment operator is a non-static non-template
416 // member function of class X with exactly one parameter of type X, X&,
417 // const X&, volatile X& or const volatile X&.
418 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
419 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
420 continue;
421
422 const FunctionProtoType *FnType
423 = Method->getType()->getAs<FunctionProtoType>();
424 assert(FnType && "Overloaded operator has no prototype.");
425 // Don't assert on this; an invalid decl might have been left in the AST.
426 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
427 continue;
428
429 QualType ArgType = FnType->getArgType(0);
430 Qualifiers Quals;
431 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
432 ArgType = Ref->getPointeeType();
433 // If we have a const argument and we have a reference to a non-const,
434 // this function does not match.
435 if (ArgIsConst && !ArgType.isConstQualified())
436 continue;
437
438 Quals = ArgType.getQualifiers();
439 } else {
440 // By-value copy-assignment operators are treated like const X&
441 // copy-assignment operators.
442 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
443 }
444
445 if (!Context.hasSameUnqualifiedType(ArgType, Class))
446 continue;
447
448 // Save this copy-assignment operator. It might be "the one".
449 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
450 }
451
452 // Use a simplistic form of overload resolution to find the candidate.
453 return GetBestOverloadCandidateSimple(Found);
454}
455
Sean Huntffe37fd2011-05-25 20:50:04 +0000456CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
457 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
458 if (I->isMoveAssignmentOperator())
David Blaikie581deb32012-06-06 20:45:41 +0000459 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000460
461 return 0;
462}
463
Douglas Gregor21386642010-09-28 21:55:22 +0000464void CXXRecordDecl::markedVirtualFunctionPure() {
465 // C++ [class.abstract]p2:
466 // A class is abstract if it has at least one pure virtual function.
467 data().Abstract = true;
468}
469
Richard Smith3f5f5582012-06-08 21:09:22 +0000470void CXXRecordDecl::markedConstructorConstexpr(CXXConstructorDecl *CD) {
Richard Smithd3861ce2012-06-10 07:07:24 +0000471 if (!CD->isCopyOrMoveConstructor())
Richard Smith3f5f5582012-06-08 21:09:22 +0000472 data().HasConstexprNonCopyMoveConstructor = true;
473
474 if (CD->isDefaultConstructor())
475 data().HasConstexprDefaultConstructor = true;
476}
477
Douglas Gregor21386642010-09-28 21:55:22 +0000478void CXXRecordDecl::addedMember(Decl *D) {
Joao Matos17d35c32012-08-31 22:18:20 +0000479 if (!D->isImplicit() &&
480 !isa<FieldDecl>(D) &&
481 !isa<IndirectFieldDecl>(D) &&
482 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
483 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
484 data().HasOnlyCMembers = false;
485
486 // Ignore friends and invalid declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000487 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000488 return;
489
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000490 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
491 if (FunTmpl)
492 D = FunTmpl->getTemplatedDecl();
493
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000494 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
495 if (Method->isVirtual()) {
496 // C++ [dcl.init.aggr]p1:
497 // An aggregate is an array or a class with [...] no virtual functions.
498 data().Aggregate = false;
499
500 // C++ [class]p4:
501 // A POD-struct is an aggregate class...
502 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000503
504 // Virtual functions make the class non-empty.
505 // FIXME: Standard ref?
506 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000507
508 // C++ [class.virtual]p1:
509 // A class that declares or inherits a virtual function is called a
510 // polymorphic class.
511 data().Polymorphic = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000512
Richard Smith7d04d3a2012-11-30 05:11:39 +0000513 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
514 // A [default constructor, copy/move constructor, or copy/move
515 // assignment operator for a class X] is trivial [...] if:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000516 // -- class X has no virtual functions [...]
Richard Smith7d04d3a2012-11-30 05:11:39 +0000517 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000518
Chandler Carrutha8225442011-04-30 09:17:45 +0000519 // C++0x [class]p7:
520 // A standard-layout class is a class that: [...]
521 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000522 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000523 }
524 }
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000525
Richard Smithacf796b2012-11-28 06:23:12 +0000526 // Notify the listener if an implicit member was added after the definition
527 // was completed.
528 if (!isBeingDefined() && D->isImplicit())
529 if (ASTMutationListener *L = getASTMutationListener())
530 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000531
Richard Smith7d04d3a2012-11-30 05:11:39 +0000532 // The kind of special member this declaration is, if any.
533 unsigned SMKind = 0;
534
Richard Smithacf796b2012-11-28 06:23:12 +0000535 // Handle constructors.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000536 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smithacf796b2012-11-28 06:23:12 +0000537 if (!Constructor->isImplicit()) {
538 // Note that we have a user-declared constructor.
539 data().UserDeclaredConstructor = true;
540
541 // C++ [class]p4:
542 // A POD-struct is an aggregate class [...]
543 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
544 // type is technically an aggregate in C++0x since it wouldn't be in 03.
545 data().PlainOldData = false;
546 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000547
Richard Smith017ab772011-09-05 02:13:09 +0000548 // Technically, "user-provided" is only defined for special member
549 // functions, but the intent of the standard is clearly that it should apply
550 // to all functions.
551 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000552
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000553 if (Constructor->isDefaultConstructor()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000554 SMKind |= SMF_DefaultConstructor;
555
556 if (UserProvided)
Sean Huntcdee3fe2011-05-11 22:34:38 +0000557 data().UserProvidedDefaultConstructor = true;
Richard Smithacf796b2012-11-28 06:23:12 +0000558 if (Constructor->isConstexpr())
Richard Smith61802452011-12-22 02:22:31 +0000559 data().HasConstexprDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000560 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000561
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000562 if (!FunTmpl) {
Richard Smithacf796b2012-11-28 06:23:12 +0000563 unsigned Quals;
564 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000565 SMKind |= SMF_CopyConstructor;
Richard Smithacf796b2012-11-28 06:23:12 +0000566
567 if (Quals & Qualifiers::Const)
568 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith7d04d3a2012-11-30 05:11:39 +0000569 } else if (Constructor->isMoveConstructor())
570 SMKind |= SMF_MoveConstructor;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000571 }
Richard Smithacf796b2012-11-28 06:23:12 +0000572
573 // Record if we see any constexpr constructors which are neither copy
574 // nor move constructors.
575 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith6b8bc072011-08-10 18:11:37 +0000576 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000577
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000578 // C++ [dcl.init.aggr]p1:
579 // An aggregate is an array or a class with no user-declared
580 // constructors [...].
581 // C++0x [dcl.init.aggr]p1:
582 // An aggregate is an array or a class with no user-provided
583 // constructors [...].
Richard Smithacf796b2012-11-28 06:23:12 +0000584 if (getASTContext().getLangOpts().CPlusPlus0x
585 ? UserProvided : !Constructor->isImplicit())
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000586 data().Aggregate = false;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000587 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000588
Richard Smithacf796b2012-11-28 06:23:12 +0000589 // Handle destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000590 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000591 SMKind |= SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000592
Richard Smith7d04d3a2012-11-30 05:11:39 +0000593 if (!DD->isImplicit())
Richard Smithacf796b2012-11-28 06:23:12 +0000594 data().HasIrrelevantDestructor = false;
595
Richard Smithacf796b2012-11-28 06:23:12 +0000596 // C++11 [class.dtor]p5:
Richard Smith7d04d3a2012-11-30 05:11:39 +0000597 // A destructor is trivial if [...] the destructor is not virtual.
598 if (DD->isVirtual())
599 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000600 }
Richard Smithacf796b2012-11-28 06:23:12 +0000601
602 // Handle member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000603 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000604 if (Method->isCopyAssignmentOperator()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000605 SMKind |= SMF_CopyAssignment;
Richard Smithacf796b2012-11-28 06:23:12 +0000606
607 const ReferenceType *ParamTy =
608 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
609 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
610 data().HasDeclaredCopyAssignmentWithConstParam = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000611 }
Sean Huntffe37fd2011-05-25 20:50:04 +0000612
Richard Smith7d04d3a2012-11-30 05:11:39 +0000613 if (Method->isMoveAssignmentOperator())
614 SMKind |= SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000615
Douglas Gregore80622f2010-09-29 04:25:11 +0000616 // Keep the list of conversion functions up-to-date.
617 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregore80622f2010-09-29 04:25:11 +0000618 // FIXME: We intentionally don't use the decl's access here because it
619 // hasn't been set yet. That's really just a misdesign in Sema.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000620 if (Conversion->getPrimaryTemplate()) {
621 // We don't record specializations.
622 } else if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000623 if (FunTmpl->getPreviousDecl())
624 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000625 FunTmpl);
626 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000627 data().Conversions.addDecl(getASTContext(), FunTmpl);
Douglas Gregore80622f2010-09-29 04:25:11 +0000628 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000629 if (Conversion->getPreviousDecl())
630 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000631 Conversion);
632 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000633 data().Conversions.addDecl(getASTContext(), Conversion);
Douglas Gregore80622f2010-09-29 04:25:11 +0000634 }
635 }
Richard Smithacf796b2012-11-28 06:23:12 +0000636
Richard Smith7d04d3a2012-11-30 05:11:39 +0000637 if (SMKind) {
638 // Note when we have declared a declared special member, and suppress the
639 // implicit declaration of this special member.
640 data().DeclaredSpecialMembers |= SMKind;
641
642 if (!Method->isImplicit()) {
643 data().UserDeclaredSpecialMembers |= SMKind;
644
645 // C++03 [class]p4:
646 // A POD-struct is an aggregate class that has [...] no user-defined
647 // copy assignment operator and no user-defined destructor.
648 //
649 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
650 // aggregates could not have any constructors, clear it even for an
651 // explicitly defaulted or deleted constructor.
652 // type is technically an aggregate in C++0x since it wouldn't be in 03.
653 //
654 // Also, a user-declared move assignment operator makes a class non-POD.
655 // This is an extension in C++03.
656 data().PlainOldData = false;
657 }
658
659 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25,
660 // C++11 [class.dtor]p5:
661 // A [special member] is trivial if it is not user-provided [...]
662 // FIXME: This is bogus. A class can have both (say) a trivial copy
663 // constructor *and* a user-provided copy constructor.
664 if (Method->isUserProvided())
665 data().HasTrivialSpecialMembers &= ~SMKind;
666 }
667
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000668 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000669 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000670
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000671 // Handle non-static data members.
672 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000673 // C++ [class.bit]p2:
674 // A declaration for a bit-field that omits the identifier declares an
675 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
676 // initialized.
677 if (Field->isUnnamedBitfield())
678 return;
679
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000680 // C++ [dcl.init.aggr]p1:
681 // An aggregate is an array or a class (clause 9) with [...] no
682 // private or protected non-static data members (clause 11).
683 //
684 // A POD must be an aggregate.
685 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
686 data().Aggregate = false;
687 data().PlainOldData = false;
688 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000689
690 // C++0x [class]p7:
691 // A standard-layout class is a class that:
692 // [...]
693 // -- has the same access control for all non-static data members,
694 switch (D->getAccess()) {
695 case AS_private: data().HasPrivateFields = true; break;
696 case AS_protected: data().HasProtectedFields = true; break;
697 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000698 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000699 };
700 if ((data().HasPrivateFields + data().HasProtectedFields +
701 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000702 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000703
Douglas Gregor2bb11012011-05-13 01:05:07 +0000704 // Keep track of the presence of mutable fields.
705 if (Field->isMutable())
706 data().HasMutableFields = true;
707
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000708 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000709 // A POD struct is a class that is both a trivial class and a
710 // standard-layout class, and has no non-static data members of type
711 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000712 //
713 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
714 // that does not explicitly have no lifetime makes the class a non-POD.
715 // However, we delay setting PlainOldData to false in this case so that
716 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000717 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000718 // In this case, the class will become a non-POD class when we complete
719 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000720 ASTContext &Context = getASTContext();
721 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000722 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000723 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000724 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
725 setHasObjectMember(true);
726 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000727 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000728
Chandler Carrutha8225442011-04-30 09:17:45 +0000729 if (T->isReferenceType()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000730 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000731
Chandler Carrutha8225442011-04-30 09:17:45 +0000732 // C++0x [class]p7:
733 // A standard-layout class is a class that:
734 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000735 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000736 }
737
Richard Smith86c3ae42012-02-13 03:54:03 +0000738 // Record if this field is the first non-literal or volatile field or base.
739 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000740 data().HasNonLiteralTypeFieldsOrBases = true;
741
Richard Smith7a614d82011-06-11 17:19:42 +0000742 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000743 data().HasInClassInitializer = true;
744
745 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000746 // A default constructor is trivial if [...] no non-static data member
747 // of its class has a brace-or-equal-initializer.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000748 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith7a614d82011-06-11 17:19:42 +0000749
Richard Smithd079abf2012-05-07 01:07:30 +0000750 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000751 // An aggregate is a [...] class with [...] no
752 // brace-or-equal-initializers for non-static data members.
753 data().Aggregate = false;
754
Richard Smithd079abf2012-05-07 01:07:30 +0000755 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000756 // A POD struct is [...] a trivial class.
757 data().PlainOldData = false;
758 }
759
Douglas Gregor85606eb2010-09-28 20:50:54 +0000760 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
761 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
762 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000763 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000764 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000765 // -- for all the non-static data members of its class that are of
766 // class type (or array thereof), each such class has a trivial
767 // default constructor.
768 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000769 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000770
771 // C++0x [class.copy]p13:
772 // A copy/move constructor for class X is trivial if [...]
773 // [...]
774 // -- for each non-static data member of X that is of class type (or
775 // an array thereof), the constructor selected to copy/move that
776 // member is trivial;
777 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000778 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000779 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith426391c2012-11-16 00:53:38 +0000780 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000781 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000782
783 // C++0x [class.copy]p27:
784 // A copy/move assignment operator for class X is trivial if [...]
785 // [...]
786 // -- for each non-static data member of X that is of class type (or
787 // an array thereof), the assignment operator selected to
788 // copy/move that member is trivial;
789 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000790 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000791 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith426391c2012-11-16 00:53:38 +0000792 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000793 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000794
Douglas Gregor85606eb2010-09-28 20:50:54 +0000795 if (!FieldRec->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000796 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000797 if (!FieldRec->hasIrrelevantDestructor())
798 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000799 if (FieldRec->hasObjectMember())
800 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000801
802 // C++0x [class]p7:
803 // A standard-layout class is a class that:
804 // -- has no non-static data members of type non-standard-layout
805 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000806 if (!FieldRec->isStandardLayout())
807 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000808
809 // C++0x [class]p7:
810 // A standard-layout class is a class that:
811 // [...]
812 // -- has no base classes of the same type as the first non-static
813 // data member.
814 // We don't want to expend bits in the state of the record decl
815 // tracking whether this is the first non-static data member so we
816 // cheat a bit and use some of the existing state: the empty bit.
817 // Virtual bases and virtual methods make a class non-empty, but they
818 // also make it non-standard-layout so we needn't check here.
819 // A non-empty base class may leave the class standard-layout, but not
820 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000821 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000822 // data member must come through here with Empty still true, and Empty
823 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000824 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000825 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
826 BE = bases_end();
827 BI != BE; ++BI) {
828 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000829 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000830 break;
831 }
832 }
833 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000834
835 // Keep track of the presence of mutable fields.
836 if (FieldRec->hasMutableFields())
837 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000838
839 // C++11 [class.copy]p13:
840 // If the implicitly-defined constructor would satisfy the
841 // requirements of a constexpr constructor, the implicitly-defined
842 // constructor is constexpr.
843 // C++11 [dcl.constexpr]p4:
844 // -- every constructor involved in initializing non-static data
845 // members [...] shall be a constexpr constructor
846 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000847 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000848 // The standard requires any in-class initializer to be a constant
849 // expression. We consider this to be a defect.
850 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithacf796b2012-11-28 06:23:12 +0000851
852 // C++11 [class.copy]p8:
853 // The implicitly-declared copy constructor for a class X will have
854 // the form 'X::X(const X&)' if [...] for all the non-static data
855 // members of X that are of a class type M (or array thereof), each
856 // such class type has a copy constructor whose first parameter is
857 // of type 'const M&' or 'const volatile M&'.
858 if (!FieldRec->hasCopyConstructorWithConstParam())
859 data().ImplicitCopyConstructorHasConstParam = false;
860
861 // C++11 [class.copy]p18:
862 // The implicitly-declared copy assignment oeprator for a class X will
863 // have the form 'X& X::operator=(const X&)' if [...] for all the
864 // non-static data members of X that are of a class type M (or array
865 // thereof), each such class type has a copy assignment operator whose
866 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
867 if (!FieldRec->hasCopyAssignmentWithConstParam())
868 data().ImplicitCopyAssignmentHasConstParam = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000869 }
Richard Smith61802452011-12-22 02:22:31 +0000870 } else {
871 // Base element type of field is a non-class type.
Richard Smithd3861ce2012-06-10 07:07:24 +0000872 if (!T->isLiteralType() ||
873 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000874 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000875 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000876
877 // C++0x [class]p7:
878 // A standard-layout class is a class that:
879 // [...]
880 // -- either has no non-static data members in the most derived
881 // class and at most one base class with non-static data members,
882 // or has no base classes with non-static data members, and
883 // At this point we know that we have a non-static data member, so the last
884 // clause holds.
885 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000886 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000887
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000888 // If this is not a zero-length bit-field, then the class is not empty.
889 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000890 if (!Field->isBitField() ||
891 (!Field->getBitWidth()->isTypeDependent() &&
892 !Field->getBitWidth()->isValueDependent() &&
893 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000894 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000895 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000896 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000897
898 // Handle using declarations of conversion functions.
899 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
900 if (Shadow->getDeclName().getNameKind()
901 == DeclarationName::CXXConversionFunctionName)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000902 data().Conversions.addDecl(getASTContext(), Shadow, Shadow->getAccess());
Joao Matos17d35c32012-08-31 22:18:20 +0000903}
904
905bool CXXRecordDecl::isCLike() const {
906 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
907 !TemplateOrInstantiation.isNull())
908 return false;
909 if (!hasDefinition())
910 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000911
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000912 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000913}
914
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000915void CXXRecordDecl::getCaptureFields(
916 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000917 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000918 Captures.clear();
919 ThisCapture = 0;
920
Douglas Gregorda8962a2012-02-13 15:44:47 +0000921 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000922 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000923 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000924 C != CEnd; ++C, ++Field) {
925 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +0000926 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000927 continue;
928 }
929
David Blaikie581deb32012-06-06 20:45:41 +0000930 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000931 }
932}
933
934
John McCallb05b5f32010-03-15 09:07:48 +0000935static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
936 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000937 if (isa<UsingShadowDecl>(Conv))
938 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000939 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
940 T = ConvTemp->getTemplatedDecl()->getResultType();
941 else
942 T = cast<CXXConversionDecl>(Conv)->getConversionType();
943 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000944}
945
John McCallb05b5f32010-03-15 09:07:48 +0000946/// Collect the visible conversions of a base class.
947///
James Dennetta1253502012-06-15 22:28:09 +0000948/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +0000949/// \param InVirtual whether this base class is a virtual base (or a base
950/// of a virtual base)
951/// \param Access the access along the inheritance path to this base
952/// \param ParentHiddenTypes the conversions provided by the inheritors
953/// of this base
954/// \param Output the set to which to add conversions from non-virtual bases
955/// \param VOutput the set to which to add conversions from virtual bases
956/// \param HiddenVBaseCs the set of conversions which were hidden in a
957/// virtual base along some inheritance path
958static void CollectVisibleConversions(ASTContext &Context,
959 CXXRecordDecl *Record,
960 bool InVirtual,
961 AccessSpecifier Access,
962 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000963 ASTUnresolvedSet &Output,
John McCallb05b5f32010-03-15 09:07:48 +0000964 UnresolvedSetImpl &VOutput,
965 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
966 // The set of types which have conversions in this class or its
967 // subclasses. As an optimization, we don't copy the derived set
968 // unless it might change.
969 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
970 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
971
972 // Collect the direct conversions and figure out which conversions
973 // will be hidden in the subclasses.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000974 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
975 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
976 if (ConvI != ConvE) {
John McCallb05b5f32010-03-15 09:07:48 +0000977 HiddenTypesBuffer = ParentHiddenTypes;
978 HiddenTypes = &HiddenTypesBuffer;
979
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +0000980 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +0000981 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
982 bool Hidden = ParentHiddenTypes.count(ConvType);
983 if (!Hidden)
984 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +0000985
986 // If this conversion is hidden and we're in a virtual base,
987 // remember that it's hidden along some inheritance path.
988 if (Hidden && InVirtual)
989 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
990
991 // If this conversion isn't hidden, add it to the appropriate output.
992 else if (!Hidden) {
993 AccessSpecifier IAccess
994 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
995
996 if (InVirtual)
997 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000998 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000999 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001000 }
1001 }
1002 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001003
John McCallb05b5f32010-03-15 09:07:48 +00001004 // Collect information recursively from any base classes.
1005 for (CXXRecordDecl::base_class_iterator
1006 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1007 const RecordType *RT = I->getType()->getAs<RecordType>();
1008 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001009
John McCallb05b5f32010-03-15 09:07:48 +00001010 AccessSpecifier BaseAccess
1011 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1012 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001013
John McCallb05b5f32010-03-15 09:07:48 +00001014 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1015 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1016 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001017 }
John McCallb05b5f32010-03-15 09:07:48 +00001018}
Sebastian Redl9994a342009-10-25 17:03:50 +00001019
John McCallb05b5f32010-03-15 09:07:48 +00001020/// Collect the visible conversions of a class.
1021///
1022/// This would be extremely straightforward if it weren't for virtual
1023/// bases. It might be worth special-casing that, really.
1024static void CollectVisibleConversions(ASTContext &Context,
1025 CXXRecordDecl *Record,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001026 ASTUnresolvedSet &Output) {
John McCallb05b5f32010-03-15 09:07:48 +00001027 // The collection of all conversions in virtual bases that we've
1028 // found. These will be added to the output as long as they don't
1029 // appear in the hidden-conversions set.
1030 UnresolvedSet<8> VBaseCs;
1031
1032 // The set of conversions in virtual bases that we've determined to
1033 // be hidden.
1034 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1035
1036 // The set of types hidden by classes derived from this one.
1037 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1038
1039 // Go ahead and collect the direct conversions and add them to the
1040 // hidden-types set.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001041 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1042 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001043 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001044 for (; ConvI != ConvE; ++ConvI)
1045 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCallb05b5f32010-03-15 09:07:48 +00001046
1047 // Recursively collect conversions from base classes.
1048 for (CXXRecordDecl::base_class_iterator
1049 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1050 const RecordType *RT = I->getType()->getAs<RecordType>();
1051 if (!RT) continue;
1052
1053 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1054 I->isVirtual(), I->getAccessSpecifier(),
1055 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1056 }
1057
1058 // Add any unhidden conversions provided by virtual bases.
1059 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1060 I != E; ++I) {
1061 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001062 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001063 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001064}
1065
1066/// getVisibleConversionFunctions - get all conversion functions visible
1067/// in current class; including conversion function templates.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001068std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1069CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001070 // If root class, all conversions are visible.
1071 if (bases_begin() == bases_end())
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001072 return std::make_pair(data().Conversions.begin(), data().Conversions.end());
Fariborz Jahanian62509212009-09-12 18:26:03 +00001073 // If visible conversion list is already evaluated, return it.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001074 if (!data().ComputedVisibleConversions) {
1075 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
1076 data().ComputedVisibleConversions = true;
1077 }
1078 return std::make_pair(data().VisibleConversions.begin(),
1079 data().VisibleConversions.end());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001080}
1081
John McCall32daa422010-03-31 01:36:47 +00001082void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1083 // This operation is O(N) but extremely rare. Sema only uses it to
1084 // remove UsingShadowDecls in a class that were followed by a direct
1085 // declaration, e.g.:
1086 // class A : B {
1087 // using B::operator int;
1088 // operator int();
1089 // };
1090 // This is uncommon by itself and even more uncommon in conjunction
1091 // with sufficiently large numbers of directly-declared conversions
1092 // that asymptotic behavior matters.
1093
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001094 ASTUnresolvedSet &Convs = data().Conversions;
John McCall32daa422010-03-31 01:36:47 +00001095 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1096 if (Convs[I].getDecl() == ConvDecl) {
1097 Convs.erase(I);
1098 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1099 && "conversion was found multiple times in unresolved set");
1100 return;
1101 }
1102 }
1103
1104 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001105}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001106
Douglas Gregorf6b11852009-10-08 15:14:33 +00001107CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001108 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001109 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1110
1111 return 0;
1112}
1113
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001114MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1115 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1116}
1117
Douglas Gregorf6b11852009-10-08 15:14:33 +00001118void
1119CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1120 TemplateSpecializationKind TSK) {
1121 assert(TemplateOrInstantiation.isNull() &&
1122 "Previous template or instantiation?");
1123 assert(!isa<ClassTemplateSpecializationDecl>(this));
1124 TemplateOrInstantiation
1125 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1126}
1127
Anders Carlssonb13e3572009-12-07 06:33:48 +00001128TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1129 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001130 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1131 return Spec->getSpecializationKind();
1132
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001133 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001134 return MSInfo->getTemplateSpecializationKind();
1135
1136 return TSK_Undeclared;
1137}
1138
1139void
1140CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1141 if (ClassTemplateSpecializationDecl *Spec
1142 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1143 Spec->setSpecializationKind(TSK);
1144 return;
1145 }
1146
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001147 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001148 MSInfo->setTemplateSpecializationKind(TSK);
1149 return;
1150 }
1151
David Blaikieb219cfc2011-09-23 05:06:16 +00001152 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001153}
1154
Douglas Gregor1d110e02010-07-01 14:13:13 +00001155CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1156 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001157 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001158
1159 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001160 = Context.DeclarationNames.getCXXDestructorName(
1161 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001162
John McCallc0bf4622010-02-23 00:48:20 +00001163 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001164 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001165 if (I == E)
1166 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001168 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001169 return Dtor;
1170}
1171
Douglas Gregorda2142f2011-02-19 18:51:44 +00001172void CXXRecordDecl::completeDefinition() {
1173 completeDefinition(0);
1174}
1175
1176void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1177 RecordDecl::completeDefinition();
1178
David Blaikie4e4d0842012-03-11 07:00:24 +00001179 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001180 // Objective-C Automatic Reference Counting:
1181 // If a class has a non-static data member of Objective-C pointer
1182 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001183 // default constructor (if any), copy constructor, move constructor,
1184 // copy assignment operator, move assignment operator, and destructor are
1185 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001186 struct DefinitionData &Data = data();
1187 Data.PlainOldData = false;
Richard Smith7d04d3a2012-11-30 05:11:39 +00001188 Data.HasTrivialSpecialMembers = 0;
Richard Smithdfefb842012-02-25 07:33:38 +00001189 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001190 }
1191
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001192 // If the class may be abstract (but hasn't been marked as such), check for
1193 // any pure final overriders.
1194 if (mayBeAbstract()) {
1195 CXXFinalOverriderMap MyFinalOverriders;
1196 if (!FinalOverriders) {
1197 getFinalOverriders(MyFinalOverriders);
1198 FinalOverriders = &MyFinalOverriders;
1199 }
1200
1201 bool Done = false;
1202 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1203 MEnd = FinalOverriders->end();
1204 M != MEnd && !Done; ++M) {
1205 for (OverridingMethods::iterator SO = M->second.begin(),
1206 SOEnd = M->second.end();
1207 SO != SOEnd && !Done; ++SO) {
1208 assert(SO->second.size() > 0 &&
1209 "All virtual functions have overridding virtual functions");
1210
1211 // C++ [class.abstract]p4:
1212 // A class is abstract if it contains or inherits at least one
1213 // pure virtual function for which the final overrider is pure
1214 // virtual.
1215 if (SO->second.front().Method->isPure()) {
1216 data().Abstract = true;
1217 Done = true;
1218 break;
1219 }
1220 }
1221 }
1222 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001223
1224 // Set access bits correctly on the directly-declared conversions.
1225 for (UnresolvedSetIterator I = data().Conversions.begin(),
1226 E = data().Conversions.end();
1227 I != E; ++I)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001228 I.setAccess((*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001229}
1230
1231bool CXXRecordDecl::mayBeAbstract() const {
1232 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1233 isDependentContext())
1234 return false;
1235
1236 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1237 BEnd = bases_end();
1238 B != BEnd; ++B) {
1239 CXXRecordDecl *BaseDecl
1240 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1241 if (BaseDecl->isAbstract())
1242 return true;
1243 }
1244
1245 return false;
1246}
1247
David Blaikie99ba9e32011-12-20 02:48:34 +00001248void CXXMethodDecl::anchor() { }
1249
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001250static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1251 const CXXMethodDecl *BaseMD) {
1252 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1253 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1254 const CXXMethodDecl *MD = *I;
1255 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1256 return true;
1257 if (recursivelyOverrides(MD, BaseMD))
1258 return true;
1259 }
1260 return false;
1261}
1262
1263CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001264CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1265 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001266 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1267 return this;
1268
1269 // Lookup doesn't work for destructors, so handle them separately.
1270 if (isa<CXXDestructorDecl>(this)) {
1271 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001272 if (MD) {
1273 if (recursivelyOverrides(MD, this))
1274 return MD;
1275 if (MayBeBase && recursivelyOverrides(this, MD))
1276 return MD;
1277 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001278 return NULL;
1279 }
1280
1281 lookup_const_result Candidates = RD->lookup(getDeclName());
1282 for (NamedDecl * const * I = Candidates.first; I != Candidates.second; ++I) {
1283 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1284 if (!MD)
1285 continue;
1286 if (recursivelyOverrides(MD, this))
1287 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001288 if (MayBeBase && recursivelyOverrides(this, MD))
1289 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001290 }
1291
1292 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1293 E = RD->bases_end(); I != E; ++I) {
1294 const RecordType *RT = I->getType()->getAs<RecordType>();
1295 if (!RT)
1296 continue;
1297 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1298 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1299 if (T)
1300 return T;
1301 }
1302
1303 return NULL;
1304}
1305
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001306CXXMethodDecl *
1307CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001308 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001309 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001310 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001311 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001312 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001313 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001314 isStatic, SCAsWritten, isInline, isConstexpr,
1315 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001316}
1317
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001318CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1319 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1320 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1321 DeclarationNameInfo(), QualType(),
1322 0, false, SC_None, false, false,
1323 SourceLocation());
1324}
1325
Douglas Gregor90916562009-09-29 18:16:17 +00001326bool CXXMethodDecl::isUsualDeallocationFunction() const {
1327 if (getOverloadedOperator() != OO_Delete &&
1328 getOverloadedOperator() != OO_Array_Delete)
1329 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001330
1331 // C++ [basic.stc.dynamic.deallocation]p2:
1332 // A template instance is never a usual deallocation function,
1333 // regardless of its signature.
1334 if (getPrimaryTemplate())
1335 return false;
1336
Douglas Gregor90916562009-09-29 18:16:17 +00001337 // C++ [basic.stc.dynamic.deallocation]p2:
1338 // If a class T has a member deallocation function named operator delete
1339 // with exactly one parameter, then that function is a usual (non-placement)
1340 // deallocation function. [...]
1341 if (getNumParams() == 1)
1342 return true;
1343
1344 // C++ [basic.stc.dynamic.deallocation]p2:
1345 // [...] If class T does not declare such an operator delete but does
1346 // declare a member deallocation function named operator delete with
1347 // exactly two parameters, the second of which has type std::size_t (18.1),
1348 // then this function is a usual deallocation function.
1349 ASTContext &Context = getASTContext();
1350 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001351 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1352 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001353 return false;
1354
1355 // This function is a usual deallocation function if there are no
1356 // single-parameter deallocation functions of the same kind.
1357 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1358 R.first != R.second; ++R.first) {
1359 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1360 if (FD->getNumParams() == 1)
1361 return false;
1362 }
1363
1364 return true;
1365}
1366
Douglas Gregor06a9f362010-05-01 20:49:11 +00001367bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001368 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001369 // A user-declared copy assignment operator X::operator= is a non-static
1370 // non-template member function of class X with exactly one parameter of
1371 // type X, X&, const X&, volatile X& or const volatile X&.
1372 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1373 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001374 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001375 return false;
1376
1377 QualType ParamType = getParamDecl(0)->getType();
1378 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1379 ParamType = Ref->getPointeeType();
1380
1381 ASTContext &Context = getASTContext();
1382 QualType ClassType
1383 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1384 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1385}
1386
Sean Huntffe37fd2011-05-25 20:50:04 +00001387bool CXXMethodDecl::isMoveAssignmentOperator() const {
1388 // C++0x [class.copy]p19:
1389 // A user-declared move assignment operator X::operator= is a non-static
1390 // non-template member function of class X with exactly one parameter of type
1391 // X&&, const X&&, volatile X&&, or const volatile X&&.
1392 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1393 getPrimaryTemplate() || getDescribedFunctionTemplate())
1394 return false;
1395
1396 QualType ParamType = getParamDecl(0)->getType();
1397 if (!isa<RValueReferenceType>(ParamType))
1398 return false;
1399 ParamType = ParamType->getPointeeType();
1400
1401 ASTContext &Context = getASTContext();
1402 QualType ClassType
1403 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1404 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1405}
1406
Anders Carlsson05eb2442009-05-16 23:58:37 +00001407void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001408 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001409 assert(!MD->getParent()->isDependentContext() &&
1410 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001411 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001412
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001413 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001414}
1415
1416CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001417 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001418 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001419}
1420
1421CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001422 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001423 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001424}
1425
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001426unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001427 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001428 return getASTContext().overridden_methods_size(this);
1429}
1430
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001431QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001432 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1433 // If the member function is declared const, the type of this is const X*,
1434 // if the member function is declared volatile, the type of this is
1435 // volatile X*, and if the member function is declared const volatile,
1436 // the type of this is const volatile X*.
1437
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001438 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001439
John McCall3cb0ebd2010-03-10 03:28:59 +00001440 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001441 ClassTy = C.getQualifiedType(ClassTy,
1442 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001443 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001444}
1445
Eli Friedmand7d7f672009-12-06 20:50:05 +00001446bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001447 // If this function is a template instantiation, look at the template from
1448 // which it was instantiated.
1449 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1450 if (!CheckFn)
1451 CheckFn = this;
1452
Eli Friedmand7d7f672009-12-06 20:50:05 +00001453 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001454 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001455}
1456
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001457bool CXXMethodDecl::isLambdaStaticInvoker() const {
1458 return getParent()->isLambda() &&
1459 getIdentifier() && getIdentifier()->getName() == "__invoke";
1460}
1461
1462
Sean Huntcbb67482011-01-08 20:30:50 +00001463CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1464 TypeSourceInfo *TInfo, bool IsVirtual,
1465 SourceLocation L, Expr *Init,
1466 SourceLocation R,
1467 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001468 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001469 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1470 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001471{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001472}
1473
Sean Huntcbb67482011-01-08 20:30:50 +00001474CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1475 FieldDecl *Member,
1476 SourceLocation MemberLoc,
1477 SourceLocation L, Expr *Init,
1478 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001479 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001480 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001481 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1482{
1483}
1484
Sean Huntcbb67482011-01-08 20:30:50 +00001485CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1486 IndirectFieldDecl *Member,
1487 SourceLocation MemberLoc,
1488 SourceLocation L, Expr *Init,
1489 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001490 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001491 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001492 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001493{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001494}
1495
Sean Huntcbb67482011-01-08 20:30:50 +00001496CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001497 TypeSourceInfo *TInfo,
1498 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001499 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001500 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1501 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001502 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1503{
1504}
1505
1506CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001507 FieldDecl *Member,
1508 SourceLocation MemberLoc,
1509 SourceLocation L, Expr *Init,
1510 SourceLocation R,
1511 VarDecl **Indices,
1512 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001513 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001514 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001515 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001516{
1517 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1518 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1519}
1520
Sean Huntcbb67482011-01-08 20:30:50 +00001521CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1522 FieldDecl *Member,
1523 SourceLocation MemberLoc,
1524 SourceLocation L, Expr *Init,
1525 SourceLocation R,
1526 VarDecl **Indices,
1527 unsigned NumIndices) {
1528 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001529 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001530 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001531 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1532 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001533}
1534
Sean Huntcbb67482011-01-08 20:30:50 +00001535TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001536 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001537 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001538 else
1539 return TypeLoc();
1540}
1541
Sean Huntcbb67482011-01-08 20:30:50 +00001542const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001543 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001544 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001545 else
1546 return 0;
1547}
1548
Sean Huntcbb67482011-01-08 20:30:50 +00001549SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001550 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001551 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001552
1553 if (isInClassMemberInitializer())
1554 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001555
Douglas Gregor76852c22011-11-01 01:16:03 +00001556 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1557 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1558
1559 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001560}
1561
Sean Huntcbb67482011-01-08 20:30:50 +00001562SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001563 if (isInClassMemberInitializer()) {
1564 FieldDecl *D = getAnyMember();
1565 if (Expr *I = D->getInClassInitializer())
1566 return I->getSourceRange();
1567 return SourceRange();
1568 }
1569
Douglas Gregor802ab452009-12-02 22:36:29 +00001570 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001571}
1572
David Blaikie99ba9e32011-12-20 02:48:34 +00001573void CXXConstructorDecl::anchor() { }
1574
Douglas Gregorb48fe382008-10-31 09:07:45 +00001575CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001576CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1577 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1578 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1579 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001580}
1581
1582CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001583CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001584 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001585 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001586 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001587 bool isExplicit, bool isInline,
1588 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001589 assert(NameInfo.getName().getNameKind()
1590 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001591 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001592 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001593 isExplicit, isInline, isImplicitlyDeclared,
1594 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001595}
1596
Douglas Gregor76852c22011-11-01 01:16:03 +00001597CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1598 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1599 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1600 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1601 return Construct->getConstructor();
1602
1603 return 0;
1604}
1605
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001606bool CXXConstructorDecl::isDefaultConstructor() const {
1607 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001608 // A default constructor for a class X is a constructor of class
1609 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001610 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001611 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001612}
1613
Mike Stump1eb44332009-09-09 15:08:12 +00001614bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001615CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001616 return isCopyOrMoveConstructor(TypeQuals) &&
1617 getParamDecl(0)->getType()->isLValueReferenceType();
1618}
1619
1620bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1621 return isCopyOrMoveConstructor(TypeQuals) &&
1622 getParamDecl(0)->getType()->isRValueReferenceType();
1623}
1624
1625/// \brief Determine whether this is a copy or move constructor.
1626bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001627 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001628 // A non-template constructor for class X is a copy constructor
1629 // if its first parameter is of type X&, const X&, volatile X& or
1630 // const volatile X&, and either there are no other parameters
1631 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001632 // C++0x [class.copy]p3:
1633 // A non-template constructor for class X is a move constructor if its
1634 // first parameter is of type X&&, const X&&, volatile X&&, or
1635 // const volatile X&&, and either there are no other parameters or else
1636 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001637 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001638 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001639 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001640 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001641 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001642
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001643 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001644
1645 // Do we have a reference type?
1646 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001647 if (!ParamRefType)
1648 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001649
Douglas Gregorfd476482009-11-13 23:59:09 +00001650 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001651 ASTContext &Context = getASTContext();
1652
Douglas Gregorfd476482009-11-13 23:59:09 +00001653 CanQualType PointeeType
1654 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001655 CanQualType ClassTy
1656 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001657 if (PointeeType.getUnqualifiedType() != ClassTy)
1658 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001659
John McCall0953e762009-09-24 19:53:00 +00001660 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001661
1662 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001663 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001664 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001665}
1666
Anders Carlssonfaccd722009-08-28 16:57:08 +00001667bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001668 // C++ [class.conv.ctor]p1:
1669 // A constructor declared without the function-specifier explicit
1670 // that can be called with a single parameter specifies a
1671 // conversion from the type of its first parameter to the type of
1672 // its class. Such a constructor is called a converting
1673 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001674 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001675 return false;
1676
Mike Stump1eb44332009-09-09 15:08:12 +00001677 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001678 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001679 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001680 (getNumParams() > 1 &&
1681 (getParamDecl(1)->hasDefaultArg() ||
1682 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001683}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001684
Douglas Gregor6493cc52010-11-08 17:16:59 +00001685bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001686 if ((getNumParams() < 1) ||
1687 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1688 (getPrimaryTemplate() == 0) ||
1689 (getDescribedFunctionTemplate() != 0))
1690 return false;
1691
1692 const ParmVarDecl *Param = getParamDecl(0);
1693
1694 ASTContext &Context = getASTContext();
1695 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1696
Douglas Gregor66724ea2009-11-14 01:20:54 +00001697 // Is it the same as our our class type?
1698 CanQualType ClassTy
1699 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1700 if (ParamType.getUnqualifiedType() != ClassTy)
1701 return false;
1702
1703 return true;
1704}
1705
Sebastian Redlf677ea32011-02-05 19:23:19 +00001706const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1707 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001708 method_iterator It = getASTContext().overridden_methods_begin(this);
1709 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001710 return 0;
1711
1712 return cast<CXXConstructorDecl>(*It);
1713}
1714
1715void
1716CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1717 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001718 assert(getASTContext().overridden_methods_size(this) == 0 &&
1719 "Base ctor already set.");
1720 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001721}
1722
David Blaikie99ba9e32011-12-20 02:48:34 +00001723void CXXDestructorDecl::anchor() { }
1724
Douglas Gregor42a552f2008-11-05 20:51:48 +00001725CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001726CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1727 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1728 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001729 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001730}
1731
1732CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001733CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001734 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001735 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001736 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001737 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001738 assert(NameInfo.getName().getNameKind()
1739 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001740 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001741 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001742 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001743}
1744
David Blaikie99ba9e32011-12-20 02:48:34 +00001745void CXXConversionDecl::anchor() { }
1746
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001747CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001748CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1749 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1750 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1751 QualType(), 0, false, false, false,
1752 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001753}
1754
1755CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001756CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001757 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001758 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001759 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001760 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001761 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001762 assert(NameInfo.getName().getNameKind()
1763 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001764 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001765 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001766 isInline, isExplicit, isConstexpr,
1767 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001768}
1769
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001770bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1771 return isImplicit() && getParent()->isLambda() &&
1772 getConversionType()->isBlockPointerType();
1773}
1774
David Blaikie99ba9e32011-12-20 02:48:34 +00001775void LinkageSpecDecl::anchor() { }
1776
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001777LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001778 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001779 SourceLocation ExternLoc,
1780 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001781 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001782 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001783 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001784}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001785
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001786LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1787 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1788 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1789 lang_c, SourceLocation());
1790}
1791
David Blaikie99ba9e32011-12-20 02:48:34 +00001792void UsingDirectiveDecl::anchor() { }
1793
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001794UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1795 SourceLocation L,
1796 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001797 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001798 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001799 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001800 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001801 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1802 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001803 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1804 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001805}
1806
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001807UsingDirectiveDecl *
1808UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1809 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1810 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1811 NestedNameSpecifierLoc(),
1812 SourceLocation(), 0, 0);
1813}
1814
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001815NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1816 if (NamespaceAliasDecl *NA =
1817 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1818 return NA->getNamespace();
1819 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1820}
1821
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001822void NamespaceDecl::anchor() { }
1823
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001824NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1825 SourceLocation StartLoc,
1826 SourceLocation IdLoc, IdentifierInfo *Id,
1827 NamespaceDecl *PrevDecl)
1828 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1829 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1830{
1831 setPreviousDeclaration(PrevDecl);
1832
1833 if (PrevDecl)
1834 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1835}
1836
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001837NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001838 bool Inline, SourceLocation StartLoc,
1839 SourceLocation IdLoc, IdentifierInfo *Id,
1840 NamespaceDecl *PrevDecl) {
1841 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001842}
1843
1844NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1845 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001846 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1847 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001848}
1849
David Blaikie99ba9e32011-12-20 02:48:34 +00001850void NamespaceAliasDecl::anchor() { }
1851
Mike Stump1eb44332009-09-09 15:08:12 +00001852NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001853 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001854 SourceLocation AliasLoc,
1855 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001856 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001857 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001858 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001859 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1860 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001861 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1862 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001863}
1864
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001865NamespaceAliasDecl *
1866NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1867 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1868 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1869 NestedNameSpecifierLoc(),
1870 SourceLocation(), 0);
1871}
1872
David Blaikie99ba9e32011-12-20 02:48:34 +00001873void UsingShadowDecl::anchor() { }
1874
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001875UsingShadowDecl *
1876UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1877 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1878 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1879}
1880
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001881UsingDecl *UsingShadowDecl::getUsingDecl() const {
1882 const UsingShadowDecl *Shadow = this;
1883 while (const UsingShadowDecl *NextShadow =
1884 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1885 Shadow = NextShadow;
1886 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1887}
1888
David Blaikie99ba9e32011-12-20 02:48:34 +00001889void UsingDecl::anchor() { }
1890
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001891void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1892 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1893 "declaration already in set");
1894 assert(S->getUsingDecl() == this);
1895
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001896 if (FirstUsingShadow.getPointer())
1897 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1898 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001899}
1900
1901void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1902 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1903 "declaration not in set");
1904 assert(S->getUsingDecl() == this);
1905
1906 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1907
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001908 if (FirstUsingShadow.getPointer() == S) {
1909 FirstUsingShadow.setPointer(
1910 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001911 S->UsingOrNextShadow = this;
1912 return;
1913 }
1914
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001915 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001916 while (Prev->UsingOrNextShadow != S)
1917 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1918 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1919 S->UsingOrNextShadow = this;
1920}
1921
Douglas Gregordc355712011-02-25 00:36:19 +00001922UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1923 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001924 const DeclarationNameInfo &NameInfo,
1925 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001926 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001927}
1928
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001929UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1930 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1931 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1932 DeclarationNameInfo(), false);
1933}
1934
David Blaikie99ba9e32011-12-20 02:48:34 +00001935void UnresolvedUsingValueDecl::anchor() { }
1936
John McCall7ba107a2009-11-18 02:36:19 +00001937UnresolvedUsingValueDecl *
1938UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1939 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001940 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001941 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001942 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001943 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001944}
1945
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001946UnresolvedUsingValueDecl *
1947UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1948 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1949 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1950 NestedNameSpecifierLoc(),
1951 DeclarationNameInfo());
1952}
1953
David Blaikie99ba9e32011-12-20 02:48:34 +00001954void UnresolvedUsingTypenameDecl::anchor() { }
1955
John McCall7ba107a2009-11-18 02:36:19 +00001956UnresolvedUsingTypenameDecl *
1957UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1958 SourceLocation UsingLoc,
1959 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001960 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001961 SourceLocation TargetNameLoc,
1962 DeclarationName TargetName) {
1963 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001964 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001965 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001966}
1967
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001968UnresolvedUsingTypenameDecl *
1969UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1970 void *Mem = AllocateDeserializedDecl(C, ID,
1971 sizeof(UnresolvedUsingTypenameDecl));
1972 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
1973 SourceLocation(),
1974 NestedNameSpecifierLoc(),
1975 SourceLocation(),
1976 0);
1977}
1978
David Blaikie99ba9e32011-12-20 02:48:34 +00001979void StaticAssertDecl::anchor() { }
1980
Anders Carlssonfb311762009-03-14 00:25:26 +00001981StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001982 SourceLocation StaticAssertLoc,
1983 Expr *AssertExpr,
1984 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00001985 SourceLocation RParenLoc,
1986 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001987 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00001988 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00001989}
1990
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001991StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
1992 unsigned ID) {
1993 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00001994 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
1995 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001996}
1997
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001998static const char *getAccessName(AccessSpecifier AS) {
1999 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002000 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002001 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002002 case AS_public:
2003 return "public";
2004 case AS_private:
2005 return "private";
2006 case AS_protected:
2007 return "protected";
2008 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002009 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002010}
2011
2012const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2013 AccessSpecifier AS) {
2014 return DB << getAccessName(AS);
2015}
Richard Smithf15fda02012-02-02 01:16:57 +00002016
2017const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2018 AccessSpecifier AS) {
2019 return DB << getAccessName(AS);
2020}