blob: 1c0316db71cc0b3494b8f36c3315676042dfd288 [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)
39 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000040 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
41 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000042 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000043 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000044 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000045 HasMutableFields(false), HasOnlyCMembers(true),
Richard Smithd079abf2012-05-07 01:07:30 +000046 HasInClassInitializer(false),
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +000047 HasTrivialDefaultConstructor(true),
Richard Smith61802452011-12-22 02:22:31 +000048 HasConstexprNonCopyMoveConstructor(false),
49 DefaultedDefaultConstructorIsConstexpr(true),
50 DefaultedCopyConstructorIsConstexpr(true),
51 DefaultedMoveConstructorIsConstexpr(true),
52 HasConstexprDefaultConstructor(false), HasConstexprCopyConstructor(false),
53 HasConstexprMoveConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000054 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
55 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
Richard Smithdfefb842012-02-25 07:33:38 +000056 HasIrrelevantDestructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000057 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000058 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000059 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
60 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sebastian Redl85ea7aa2011-08-30 19:58:05 +000061 DeclaredDestructor(false), FailedImplicitMoveConstructor(false),
Eli Friedman72899c32012-01-07 04:59:52 +000062 FailedImplicitMoveAssignment(false), IsLambda(false), NumBases(0),
63 NumVBases(0), Bases(), VBases(), Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000064}
65
66CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000067 SourceLocation StartLoc, SourceLocation IdLoc,
68 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
69 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000070 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000071 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000072
Jay Foad4ba2a172011-01-12 09:06:06 +000073CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000074 DeclContext *DC, SourceLocation StartLoc,
75 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000076 CXXRecordDecl* PrevDecl,
77 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000078 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
79 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000080
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000081 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000082 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000083 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000084 return R;
85}
86
Douglas Gregorda8962a2012-02-13 15:44:47 +000087CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Douglas Gregorf4b7de12012-02-21 19:11:17 +000088 SourceLocation Loc, bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +000089 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
90 0, 0);
91 R->IsBeingDefined = true;
Douglas Gregorf4b7de12012-02-21 19:11:17 +000092 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Dependent);
Douglas Gregorda8962a2012-02-13 15:44:47 +000093 C.getTypeDeclType(R, /*PrevDecl=*/0);
94 return R;
95}
96
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000097CXXRecordDecl *
98CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
99 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
100 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
101 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000102}
103
Mike Stump1eb44332009-09-09 15:08:12 +0000104void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000105CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000106 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000107 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000108
Douglas Gregor7c789c12010-10-29 22:39:52 +0000109 if (!data().Bases.isOffset() && data().NumBases > 0)
110 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Richard Smithdd677232011-10-18 20:08:55 +0000112 if (NumBases) {
113 // C++ [dcl.init.aggr]p1:
114 // An aggregate is [...] a class with [...] no base classes [...].
115 data().Aggregate = false;
116
117 // C++ [class]p4:
118 // A POD-struct is an aggregate class...
119 data().PlainOldData = false;
120 }
121
Anders Carlsson6f6de732010-03-29 05:13:12 +0000122 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000123 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000124
125 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000126 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000127
John McCall86ff3082010-02-04 22:26:26 +0000128 data().Bases = new(C) CXXBaseSpecifier [NumBases];
129 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000130 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000131 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000132 // Keep track of inherited vbases for this base class.
133 const CXXBaseSpecifier *Base = Bases[i];
134 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000135 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000136 if (BaseType->isDependentType())
137 continue;
138 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000139 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000140
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000141 // A class with a non-empty base class is not empty.
142 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000143 if (!BaseClassDecl->isEmpty()) {
144 if (!data().Empty) {
145 // C++0x [class]p7:
146 // A standard-layout class is a class that:
147 // [...]
148 // -- either has no non-static data members in the most derived
149 // class and at most one base class with non-static data members,
150 // or has no base classes with non-static data members, and
151 // If this is the second non-empty base, then neither of these two
152 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000153 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000154 }
155
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000156 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000157 data().HasNoNonEmptyBases = false;
158 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000159
Douglas Gregor85606eb2010-09-28 20:50:54 +0000160 // C++ [class.virtual]p1:
161 // A class that declares or inherits a virtual function is called a
162 // polymorphic class.
163 if (BaseClassDecl->isPolymorphic())
164 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000165
Chandler Carrutha8225442011-04-30 09:17:45 +0000166 // C++0x [class]p7:
167 // A standard-layout class is a class that: [...]
168 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000169 if (!BaseClassDecl->isStandardLayout())
170 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000171
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000172 // Record if this base is the first non-literal field or base.
173 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
174 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000175
Anders Carlsson6f6de732010-03-29 05:13:12 +0000176 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000177 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000178 BaseClassDecl->vbases_begin(),
179 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000180 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000181 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000182 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000183 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000184
185 if (Base->isVirtual()) {
186 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000187 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000188 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000189
190 // C++0x [meta.unary.prop] is_empty:
191 // T is a class type, but not a union type, with ... no virtual base
192 // classes
193 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000194
195 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000196 // A default constructor is trivial [...] if:
197 // -- its class has [...] no virtual bases
198 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000199
200 // C++0x [class.copy]p13:
201 // A copy/move constructor for class X is trivial if it is neither
202 // user-provided nor deleted and if
203 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000204 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000205 data().HasTrivialMoveConstructor = false;
206
207 // C++0x [class.copy]p27:
208 // A copy/move assignment operator for class X is trivial if it is
209 // neither user-provided nor deleted and if
210 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000211 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000212 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000213
214 // C++0x [class]p7:
215 // A standard-layout class is a class that: [...]
216 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000217 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000218
219 // C++11 [dcl.constexpr]p4:
220 // In the definition of a constexpr constructor [...]
221 // -- the class shall not have any virtual base classes
222 data().DefaultedDefaultConstructorIsConstexpr = false;
223 data().DefaultedCopyConstructorIsConstexpr = false;
224 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000225 } else {
226 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000227 // A default constructor is trivial [...] if:
228 // -- all the direct base classes of its class have trivial default
229 // constructors.
230 if (!BaseClassDecl->hasTrivialDefaultConstructor())
231 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000232
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000233 // C++0x [class.copy]p13:
234 // A copy/move constructor for class X is trivial if [...]
235 // [...]
236 // -- the constructor selected to copy/move each direct base class
237 // subobject is trivial, and
238 // FIXME: C++0x: We need to only consider the selected constructor
239 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000240 if (!BaseClassDecl->hasTrivialCopyConstructor())
241 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000242 if (!BaseClassDecl->hasTrivialMoveConstructor())
243 data().HasTrivialMoveConstructor = false;
244
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())
253 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000254 if (!BaseClassDecl->hasTrivialMoveAssignment())
255 data().HasTrivialMoveAssignment = false;
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;
263
264 // C++11 [class.copy]p13:
265 // If the implicitly-defined constructor would satisfy the requirements
266 // of a constexpr constructor, the implicitly-defined constructor is
267 // constexpr.
268 // C++11 [dcl.constexpr]p4:
269 // -- every constructor involved in initializing [...] base class
270 // sub-objects shall be a constexpr constructor
271 if (!BaseClassDecl->hasConstexprCopyConstructor())
272 data().DefaultedCopyConstructorIsConstexpr = false;
273 if (BaseClassDecl->hasDeclaredMoveConstructor() ||
274 BaseClassDecl->needsImplicitMoveConstructor())
275 // FIXME: If the implicit move constructor generated for the base class
276 // would be ill-formed, the implicit move constructor generated for the
277 // derived class calls the base class' copy constructor.
278 data().DefaultedMoveConstructorIsConstexpr &=
Richard Smithde8facc2012-01-11 18:26:05 +0000279 BaseClassDecl->hasConstexprMoveConstructor();
Richard Smith61802452011-12-22 02:22:31 +0000280 else if (!BaseClassDecl->hasConstexprCopyConstructor())
281 data().DefaultedMoveConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000282 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000283
284 // C++ [class.ctor]p3:
285 // A destructor is trivial if all the direct base classes of its class
286 // have trivial destructors.
287 if (!BaseClassDecl->hasTrivialDestructor())
288 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000289
290 if (!BaseClassDecl->hasIrrelevantDestructor())
291 data().HasIrrelevantDestructor = false;
292
John McCallf85e1932011-06-15 23:02:42 +0000293 // A class has an Objective-C object member if... or any of its bases
294 // has an Objective-C object member.
295 if (BaseClassDecl->hasObjectMember())
296 setHasObjectMember(true);
297
Douglas Gregor2bb11012011-05-13 01:05:07 +0000298 // Keep track of the presence of mutable fields.
299 if (BaseClassDecl->hasMutableFields())
300 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000301 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000302
303 if (VBases.empty())
304 return;
305
306 // Create base specifier for any direct or indirect virtual bases.
307 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
308 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000309 for (int I = 0, E = VBases.size(); I != E; ++I)
310 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000311}
312
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000313/// Callback function for CXXRecordDecl::forallBases that acknowledges
314/// that it saw a base class.
315static bool SawBase(const CXXRecordDecl *, void *) {
316 return true;
317}
318
319bool CXXRecordDecl::hasAnyDependentBases() const {
320 if (!isDependentContext())
321 return false;
322
323 return !forallBases(SawBase, 0);
324}
325
Sean Huntffe37fd2011-05-25 20:50:04 +0000326bool CXXRecordDecl::hasConstCopyConstructor() const {
327 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000328}
329
Chandler Carruthb7e95892011-04-23 10:47:28 +0000330bool CXXRecordDecl::isTriviallyCopyable() const {
331 // C++0x [class]p5:
332 // A trivially copyable class is a class that:
333 // -- has no non-trivial copy constructors,
334 if (!hasTrivialCopyConstructor()) return false;
335 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000336 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000337 // -- has no non-trivial copy assignment operators,
338 if (!hasTrivialCopyAssignment()) return false;
339 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000340 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000341 // -- has a trivial destructor.
342 if (!hasTrivialDestructor()) return false;
343
344 return true;
345}
346
Douglas Gregor0d405db2010-07-01 20:59:04 +0000347/// \brief Perform a simplistic form of overload resolution that only considers
348/// cv-qualifiers on a single parameter, and return the best overload candidate
349/// (if there is one).
350static CXXMethodDecl *
351GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000352 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000353 if (Cands.empty())
354 return 0;
355 if (Cands.size() == 1)
356 return Cands[0].first;
357
358 unsigned Best = 0, N = Cands.size();
359 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000360 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000361 Best = I;
362
363 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000364 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000365 return 0;
366
367 return Cands[Best].first;
368}
369
Sean Huntffe37fd2011-05-25 20:50:04 +0000370CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
371 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000372 QualType ClassType
373 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000374 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000375 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000376 Context.getCanonicalType(ClassType));
377 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000378 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000379 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000380 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000381 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000382 // C++ [class.copy]p2:
383 // A non-template constructor for class X is a copy constructor if [...]
384 if (isa<FunctionTemplateDecl>(*Con))
385 continue;
386
Douglas Gregor0d405db2010-07-01 20:59:04 +0000387 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
388 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000389 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
390 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000391 Found.push_back(std::make_pair(
392 const_cast<CXXConstructorDecl *>(Constructor),
393 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000394 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000395 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000396
397 return cast_or_null<CXXConstructorDecl>(
398 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000399}
400
Sean Huntffe37fd2011-05-25 20:50:04 +0000401CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
402 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
403 if (I->isMoveConstructor())
David Blaikie581deb32012-06-06 20:45:41 +0000404 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000405
406 return 0;
407}
408
Douglas Gregorb87786f2010-07-01 17:48:08 +0000409CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
410 ASTContext &Context = getASTContext();
411 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
412 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
413
Chris Lattner5f9e2722011-07-23 10:55:15 +0000414 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000415 DeclContext::lookup_const_iterator Op, OpEnd;
416 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
417 // C++ [class.copy]p9:
418 // A user-declared copy assignment operator is a non-static non-template
419 // member function of class X with exactly one parameter of type X, X&,
420 // const X&, volatile X& or const volatile X&.
421 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
422 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
423 continue;
424
425 const FunctionProtoType *FnType
426 = Method->getType()->getAs<FunctionProtoType>();
427 assert(FnType && "Overloaded operator has no prototype.");
428 // Don't assert on this; an invalid decl might have been left in the AST.
429 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
430 continue;
431
432 QualType ArgType = FnType->getArgType(0);
433 Qualifiers Quals;
434 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
435 ArgType = Ref->getPointeeType();
436 // If we have a const argument and we have a reference to a non-const,
437 // this function does not match.
438 if (ArgIsConst && !ArgType.isConstQualified())
439 continue;
440
441 Quals = ArgType.getQualifiers();
442 } else {
443 // By-value copy-assignment operators are treated like const X&
444 // copy-assignment operators.
445 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
446 }
447
448 if (!Context.hasSameUnqualifiedType(ArgType, Class))
449 continue;
450
451 // Save this copy-assignment operator. It might be "the one".
452 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
453 }
454
455 // Use a simplistic form of overload resolution to find the candidate.
456 return GetBestOverloadCandidateSimple(Found);
457}
458
Sean Huntffe37fd2011-05-25 20:50:04 +0000459CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
460 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
461 if (I->isMoveAssignmentOperator())
David Blaikie581deb32012-06-06 20:45:41 +0000462 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000463
464 return 0;
465}
466
Douglas Gregor21386642010-09-28 21:55:22 +0000467void CXXRecordDecl::markedVirtualFunctionPure() {
468 // C++ [class.abstract]p2:
469 // A class is abstract if it has at least one pure virtual function.
470 data().Abstract = true;
471}
472
473void CXXRecordDecl::addedMember(Decl *D) {
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +0000474 if (!D->isImplicit() &&
475 !isa<FieldDecl>(D) &&
476 !isa<IndirectFieldDecl>(D) &&
477 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class))
478 data().HasOnlyCMembers = false;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000479
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000480 // Ignore friends and invalid declarations.
481 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000482 return;
483
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000484 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
485 if (FunTmpl)
486 D = FunTmpl->getTemplatedDecl();
487
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000488 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
489 if (Method->isVirtual()) {
490 // C++ [dcl.init.aggr]p1:
491 // An aggregate is an array or a class with [...] no virtual functions.
492 data().Aggregate = false;
493
494 // C++ [class]p4:
495 // A POD-struct is an aggregate class...
496 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000497
498 // Virtual functions make the class non-empty.
499 // FIXME: Standard ref?
500 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000501
502 // C++ [class.virtual]p1:
503 // A class that declares or inherits a virtual function is called a
504 // polymorphic class.
505 data().Polymorphic = true;
506
Sean Hunt023df372011-05-09 18:22:59 +0000507 // C++0x [class.ctor]p5
508 // A default constructor is trivial [...] if:
509 // -- its class has no virtual functions [...]
510 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000511
512 // C++0x [class.copy]p13:
513 // A copy/move constructor for class X is trivial if [...]
514 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000515 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000516 data().HasTrivialMoveConstructor = false;
517
518 // C++0x [class.copy]p27:
519 // A copy/move assignment operator for class X is trivial if [...]
520 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000521 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000522 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000523
Chandler Carrutha8225442011-04-30 09:17:45 +0000524 // C++0x [class]p7:
525 // A standard-layout class is a class that: [...]
526 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000527 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000528 }
529 }
530
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000531 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000532 // Notify that an implicit member was added after the definition
533 // was completed.
534 if (!isBeingDefined())
535 if (ASTMutationListener *L = getASTMutationListener())
536 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000537
Sean Huntffe37fd2011-05-25 20:50:04 +0000538 // If this is a special member function, note that it was added and then
539 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000540 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith61802452011-12-22 02:22:31 +0000541 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000542 data().DeclaredDefaultConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000543 if (Constructor->isConstexpr()) {
544 data().HasConstexprDefaultConstructor = true;
545 data().HasConstexprNonCopyMoveConstructor = true;
546 }
547 } else if (Constructor->isCopyConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000548 data().DeclaredCopyConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000549 if (Constructor->isConstexpr())
550 data().HasConstexprCopyConstructor = true;
551 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000552 data().DeclaredMoveConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000553 if (Constructor->isConstexpr())
554 data().HasConstexprMoveConstructor = true;
555 } else
Sean Huntffe37fd2011-05-25 20:50:04 +0000556 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000557 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000558 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000559 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000560 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000561 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
562 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000563 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000564 else if (Method->isMoveAssignmentOperator())
565 data().DeclaredMoveAssignment = true;
566 else
567 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000568 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000569 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000570
Sean Huntffe37fd2011-05-25 20:50:04 +0000571NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000572 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000573 }
574
575 // Handle (user-declared) constructors.
576 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
577 // Note that we have a user-declared constructor.
578 data().UserDeclaredConstructor = true;
579
Richard Smith017ab772011-09-05 02:13:09 +0000580 // Technically, "user-provided" is only defined for special member
581 // functions, but the intent of the standard is clearly that it should apply
582 // to all functions.
583 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000584
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000585 if (Constructor->isDefaultConstructor()) {
586 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000587 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000588 // C++0x [class.ctor]p5:
589 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000590 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000591 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000592 }
Richard Smith61802452011-12-22 02:22:31 +0000593 if (Constructor->isConstexpr()) {
594 data().HasConstexprDefaultConstructor = true;
595 data().HasConstexprNonCopyMoveConstructor = true;
596 }
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000597 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000598
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000599 // Note when we have a user-declared copy or move constructor, which will
600 // suppress the implicit declaration of those constructors.
601 if (!FunTmpl) {
602 if (Constructor->isCopyConstructor()) {
603 data().UserDeclaredCopyConstructor = true;
604 data().DeclaredCopyConstructor = true;
605
606 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000607 // A copy/move constructor for class X is trivial if it is not
608 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000609 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000610 data().HasTrivialCopyConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000611
612 if (Constructor->isConstexpr())
613 data().HasConstexprCopyConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000614 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000615 data().UserDeclaredMoveConstructor = true;
616 data().DeclaredMoveConstructor = true;
617
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000618 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000619 // A copy/move constructor for class X is trivial if it is not
620 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000621 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000622 data().HasTrivialMoveConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000623
624 if (Constructor->isConstexpr())
625 data().HasConstexprMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000626 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000627 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000628 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
629 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000630 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000631 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000632 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000633
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000634 // C++ [dcl.init.aggr]p1:
635 // An aggregate is an array or a class with no user-declared
636 // constructors [...].
637 // C++0x [dcl.init.aggr]p1:
638 // An aggregate is an array or a class with no user-provided
639 // constructors [...].
David Blaikie4e4d0842012-03-11 07:00:24 +0000640 if (!getASTContext().getLangOpts().CPlusPlus0x || UserProvided)
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000641 data().Aggregate = false;
642
643 // C++ [class]p4:
644 // A POD-struct is an aggregate class [...]
645 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
646 // type is technically an aggregate in C++0x since it wouldn't be in 03.
647 data().PlainOldData = false;
648
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000649 return;
650 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000651
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000652 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000653 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000654 data().DeclaredDestructor = true;
655 data().UserDeclaredDestructor = true;
Richard Smithdfefb842012-02-25 07:33:38 +0000656 data().HasIrrelevantDestructor = false;
657
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000658 // C++ [class]p4:
659 // A POD-struct is an aggregate class that has [...] no user-defined
660 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000661 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000662 data().PlainOldData = false;
663
Douglas Gregor45fa5602011-11-07 20:56:01 +0000664 // C++11 [class.dtor]p5:
665 // A destructor is trivial if it is not user-provided and if
666 // -- the destructor is not virtual.
Richard Smith61802452011-12-22 02:22:31 +0000667 if (DD->isUserProvided() || DD->isVirtual()) {
Sean Huntcf34e752011-05-16 22:41:40 +0000668 data().HasTrivialDestructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000669 // C++11 [dcl.constexpr]p1:
670 // The constexpr specifier shall be applied only to [...] the
671 // declaration of a static data member of a literal type.
672 // C++11 [basic.types]p10:
673 // A type is a literal type if it is [...] a class type that [...] has
674 // a trivial destructor.
675 data().DefaultedDefaultConstructorIsConstexpr = false;
676 data().DefaultedCopyConstructorIsConstexpr = false;
677 data().DefaultedMoveConstructorIsConstexpr = false;
678 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000679
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000680 return;
681 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000682
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000683 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000684 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000685 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000686 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000687 // A POD-struct is an aggregate class that [...] has no user-defined
688 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000689 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000690 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000691
Sean Huntffe37fd2011-05-25 20:50:04 +0000692 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000693
Sean Huntffe37fd2011-05-25 20:50:04 +0000694 // Suppress the implicit declaration of a copy constructor.
695 data().UserDeclaredCopyAssignment = true;
696 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000697
Sean Huntffe37fd2011-05-25 20:50:04 +0000698 // C++0x [class.copy]p27:
699 // A copy/move assignment operator for class X is trivial if it is
700 // neither user-provided nor deleted [...]
701 if (Method->isUserProvided())
702 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000703
Sean Huntffe37fd2011-05-25 20:50:04 +0000704 return;
705 }
706
707 if (Method->isMoveAssignmentOperator()) {
708 // This is an extension in C++03 mode, but we'll keep consistency by
709 // taking a move assignment operator to induce non-POD-ness
710 data().PlainOldData = false;
711
712 // This is a move assignment operator.
713 data().UserDeclaredMoveAssignment = true;
714 data().DeclaredMoveAssignment = true;
715
716 // C++0x [class.copy]p27:
717 // A copy/move assignment operator for class X is trivial if it is
718 // neither user-provided nor deleted [...]
719 if (Method->isUserProvided())
720 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000721 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000722
Douglas Gregore80622f2010-09-29 04:25:11 +0000723 // Keep the list of conversion functions up-to-date.
724 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
725 // We don't record specializations.
726 if (Conversion->getPrimaryTemplate())
727 return;
728
729 // FIXME: We intentionally don't use the decl's access here because it
730 // hasn't been set yet. That's really just a misdesign in Sema.
731
732 if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000733 if (FunTmpl->getPreviousDecl())
734 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000735 FunTmpl);
736 else
737 data().Conversions.addDecl(FunTmpl);
738 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000739 if (Conversion->getPreviousDecl())
740 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000741 Conversion);
742 else
743 data().Conversions.addDecl(Conversion);
744 }
745 }
746
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000747 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000748 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000749
750 // Handle non-static data members.
751 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000752 // C++ [class.bit]p2:
753 // A declaration for a bit-field that omits the identifier declares an
754 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
755 // initialized.
756 if (Field->isUnnamedBitfield())
757 return;
758
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000759 // C++ [dcl.init.aggr]p1:
760 // An aggregate is an array or a class (clause 9) with [...] no
761 // private or protected non-static data members (clause 11).
762 //
763 // A POD must be an aggregate.
764 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
765 data().Aggregate = false;
766 data().PlainOldData = false;
767 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000768
769 // C++0x [class]p7:
770 // A standard-layout class is a class that:
771 // [...]
772 // -- has the same access control for all non-static data members,
773 switch (D->getAccess()) {
774 case AS_private: data().HasPrivateFields = true; break;
775 case AS_protected: data().HasProtectedFields = true; break;
776 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000777 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000778 };
779 if ((data().HasPrivateFields + data().HasProtectedFields +
780 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000781 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000782
Douglas Gregor2bb11012011-05-13 01:05:07 +0000783 // Keep track of the presence of mutable fields.
784 if (Field->isMutable())
785 data().HasMutableFields = true;
786
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000787 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000788 // A POD struct is a class that is both a trivial class and a
789 // standard-layout class, and has no non-static data members of type
790 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000791 //
792 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
793 // that does not explicitly have no lifetime makes the class a non-POD.
794 // However, we delay setting PlainOldData to false in this case so that
795 // Sema has a chance to diagnostic causes where the same class will be
796 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
797 // In this case, the class will become a non-POD class when we complete
798 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000799 ASTContext &Context = getASTContext();
800 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000801 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000802 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000803 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
804 setHasObjectMember(true);
805 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000806 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000807
Chandler Carrutha8225442011-04-30 09:17:45 +0000808 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000809 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000810
Chandler Carrutha8225442011-04-30 09:17:45 +0000811 // C++0x [class]p7:
812 // A standard-layout class is a class that:
813 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000814 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000815 }
816
Richard Smith86c3ae42012-02-13 03:54:03 +0000817 // Record if this field is the first non-literal or volatile field or base.
818 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000819 data().HasNonLiteralTypeFieldsOrBases = true;
820
Richard Smith7a614d82011-06-11 17:19:42 +0000821 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000822 data().HasInClassInitializer = true;
823
824 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000825 // A default constructor is trivial if [...] no non-static data member
826 // of its class has a brace-or-equal-initializer.
827 data().HasTrivialDefaultConstructor = false;
828
Richard Smithd079abf2012-05-07 01:07:30 +0000829 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000830 // An aggregate is a [...] class with [...] no
831 // brace-or-equal-initializers for non-static data members.
832 data().Aggregate = false;
833
Richard Smithd079abf2012-05-07 01:07:30 +0000834 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000835 // A POD struct is [...] a trivial class.
836 data().PlainOldData = false;
837 }
838
Douglas Gregor85606eb2010-09-28 20:50:54 +0000839 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
840 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
841 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000842 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000843 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000844 // -- for all the non-static data members of its class that are of
845 // class type (or array thereof), each such class has a trivial
846 // default constructor.
847 if (!FieldRec->hasTrivialDefaultConstructor())
848 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000849
850 // C++0x [class.copy]p13:
851 // A copy/move constructor for class X is trivial if [...]
852 // [...]
853 // -- for each non-static data member of X that is of class type (or
854 // an array thereof), the constructor selected to copy/move that
855 // member is trivial;
856 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000857 if (!FieldRec->hasTrivialCopyConstructor())
858 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000859 if (!FieldRec->hasTrivialMoveConstructor())
860 data().HasTrivialMoveConstructor = false;
861
862 // C++0x [class.copy]p27:
863 // A copy/move assignment operator for class X is trivial if [...]
864 // [...]
865 // -- for each non-static data member of X that is of class type (or
866 // an array thereof), the assignment operator selected to
867 // copy/move that member is trivial;
868 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000869 if (!FieldRec->hasTrivialCopyAssignment())
870 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000871 if (!FieldRec->hasTrivialMoveAssignment())
872 data().HasTrivialMoveAssignment = false;
873
Douglas Gregor85606eb2010-09-28 20:50:54 +0000874 if (!FieldRec->hasTrivialDestructor())
875 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000876 if (!FieldRec->hasIrrelevantDestructor())
877 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000878 if (FieldRec->hasObjectMember())
879 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000880
881 // C++0x [class]p7:
882 // A standard-layout class is a class that:
883 // -- has no non-static data members of type non-standard-layout
884 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000885 if (!FieldRec->isStandardLayout())
886 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000887
888 // C++0x [class]p7:
889 // A standard-layout class is a class that:
890 // [...]
891 // -- has no base classes of the same type as the first non-static
892 // data member.
893 // We don't want to expend bits in the state of the record decl
894 // tracking whether this is the first non-static data member so we
895 // cheat a bit and use some of the existing state: the empty bit.
896 // Virtual bases and virtual methods make a class non-empty, but they
897 // also make it non-standard-layout so we needn't check here.
898 // A non-empty base class may leave the class standard-layout, but not
899 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000900 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000901 // data member must come through here with Empty still true, and Empty
902 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000903 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000904 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
905 BE = bases_end();
906 BI != BE; ++BI) {
907 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000908 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000909 break;
910 }
911 }
912 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000913
914 // Keep track of the presence of mutable fields.
915 if (FieldRec->hasMutableFields())
916 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000917
918 // C++11 [class.copy]p13:
919 // If the implicitly-defined constructor would satisfy the
920 // requirements of a constexpr constructor, the implicitly-defined
921 // constructor is constexpr.
922 // C++11 [dcl.constexpr]p4:
923 // -- every constructor involved in initializing non-static data
924 // members [...] shall be a constexpr constructor
925 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000926 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000927 // The standard requires any in-class initializer to be a constant
928 // expression. We consider this to be a defect.
929 data().DefaultedDefaultConstructorIsConstexpr = false;
930
931 if (!FieldRec->hasConstexprCopyConstructor())
932 data().DefaultedCopyConstructorIsConstexpr = false;
933
934 if (FieldRec->hasDeclaredMoveConstructor() ||
935 FieldRec->needsImplicitMoveConstructor())
936 // FIXME: If the implicit move constructor generated for the member's
937 // class would be ill-formed, the implicit move constructor generated
938 // for this class calls the member's copy constructor.
939 data().DefaultedMoveConstructorIsConstexpr &=
940 FieldRec->hasConstexprMoveConstructor();
941 else if (!FieldRec->hasConstexprCopyConstructor())
942 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000943 }
Richard Smith61802452011-12-22 02:22:31 +0000944 } else {
945 // Base element type of field is a non-class type.
946 if (!T->isLiteralType()) {
947 data().DefaultedDefaultConstructorIsConstexpr = false;
948 data().DefaultedCopyConstructorIsConstexpr = false;
949 data().DefaultedMoveConstructorIsConstexpr = false;
Richard Smithd079abf2012-05-07 01:07:30 +0000950 } else if (!Field->hasInClassInitializer() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000951 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000952 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000953
954 // C++0x [class]p7:
955 // A standard-layout class is a class that:
956 // [...]
957 // -- either has no non-static data members in the most derived
958 // class and at most one base class with non-static data members,
959 // or has no base classes with non-static data members, and
960 // At this point we know that we have a non-static data member, so the last
961 // clause holds.
962 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000963 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000964
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000965 // If this is not a zero-length bit-field, then the class is not empty.
966 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000967 if (!Field->isBitField() ||
968 (!Field->getBitWidth()->isTypeDependent() &&
969 !Field->getBitWidth()->isValueDependent() &&
970 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000971 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000972 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000973 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000974
975 // Handle using declarations of conversion functions.
976 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
977 if (Shadow->getDeclName().getNameKind()
978 == DeclarationName::CXXConversionFunctionName)
979 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000980}
981
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000982bool CXXRecordDecl::isCLike() const {
983 if (getTagKind() == TTK_Class || !TemplateOrInstantiation.isNull())
984 return false;
985 if (!hasDefinition())
986 return true;
987
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000988 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000989}
990
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000991void CXXRecordDecl::getCaptureFields(
992 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000993 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000994 Captures.clear();
995 ThisCapture = 0;
996
Douglas Gregorda8962a2012-02-13 15:44:47 +0000997 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000998 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000999 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001000 C != CEnd; ++C, ++Field) {
1001 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +00001002 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001003 continue;
1004 }
1005
David Blaikie581deb32012-06-06 20:45:41 +00001006 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001007 }
1008}
1009
1010
John McCallb05b5f32010-03-15 09:07:48 +00001011static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1012 QualType T;
John McCall32daa422010-03-31 01:36:47 +00001013 if (isa<UsingShadowDecl>(Conv))
1014 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +00001015 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
1016 T = ConvTemp->getTemplatedDecl()->getResultType();
1017 else
1018 T = cast<CXXConversionDecl>(Conv)->getConversionType();
1019 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +00001020}
1021
John McCallb05b5f32010-03-15 09:07:48 +00001022/// Collect the visible conversions of a base class.
1023///
1024/// \param Base a base class of the class we're considering
1025/// \param InVirtual whether this base class is a virtual base (or a base
1026/// of a virtual base)
1027/// \param Access the access along the inheritance path to this base
1028/// \param ParentHiddenTypes the conversions provided by the inheritors
1029/// of this base
1030/// \param Output the set to which to add conversions from non-virtual bases
1031/// \param VOutput the set to which to add conversions from virtual bases
1032/// \param HiddenVBaseCs the set of conversions which were hidden in a
1033/// virtual base along some inheritance path
1034static void CollectVisibleConversions(ASTContext &Context,
1035 CXXRecordDecl *Record,
1036 bool InVirtual,
1037 AccessSpecifier Access,
1038 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
1039 UnresolvedSetImpl &Output,
1040 UnresolvedSetImpl &VOutput,
1041 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1042 // The set of types which have conversions in this class or its
1043 // subclasses. As an optimization, we don't copy the derived set
1044 // unless it might change.
1045 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1046 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1047
1048 // Collect the direct conversions and figure out which conversions
1049 // will be hidden in the subclasses.
1050 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1051 if (!Cs.empty()) {
1052 HiddenTypesBuffer = ParentHiddenTypes;
1053 HiddenTypes = &HiddenTypesBuffer;
1054
1055 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +00001056 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1057 bool Hidden = ParentHiddenTypes.count(ConvType);
1058 if (!Hidden)
1059 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001060
1061 // If this conversion is hidden and we're in a virtual base,
1062 // remember that it's hidden along some inheritance path.
1063 if (Hidden && InVirtual)
1064 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1065
1066 // If this conversion isn't hidden, add it to the appropriate output.
1067 else if (!Hidden) {
1068 AccessSpecifier IAccess
1069 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1070
1071 if (InVirtual)
1072 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001073 else
John McCallb05b5f32010-03-15 09:07:48 +00001074 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001075 }
1076 }
1077 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001078
John McCallb05b5f32010-03-15 09:07:48 +00001079 // Collect information recursively from any base classes.
1080 for (CXXRecordDecl::base_class_iterator
1081 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1082 const RecordType *RT = I->getType()->getAs<RecordType>();
1083 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001084
John McCallb05b5f32010-03-15 09:07:48 +00001085 AccessSpecifier BaseAccess
1086 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1087 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001088
John McCallb05b5f32010-03-15 09:07:48 +00001089 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1090 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1091 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001092 }
John McCallb05b5f32010-03-15 09:07:48 +00001093}
Sebastian Redl9994a342009-10-25 17:03:50 +00001094
John McCallb05b5f32010-03-15 09:07:48 +00001095/// Collect the visible conversions of a class.
1096///
1097/// This would be extremely straightforward if it weren't for virtual
1098/// bases. It might be worth special-casing that, really.
1099static void CollectVisibleConversions(ASTContext &Context,
1100 CXXRecordDecl *Record,
1101 UnresolvedSetImpl &Output) {
1102 // The collection of all conversions in virtual bases that we've
1103 // found. These will be added to the output as long as they don't
1104 // appear in the hidden-conversions set.
1105 UnresolvedSet<8> VBaseCs;
1106
1107 // The set of conversions in virtual bases that we've determined to
1108 // be hidden.
1109 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1110
1111 // The set of types hidden by classes derived from this one.
1112 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1113
1114 // Go ahead and collect the direct conversions and add them to the
1115 // hidden-types set.
1116 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1117 Output.append(Cs.begin(), Cs.end());
1118 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1119 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1120
1121 // Recursively collect conversions from base classes.
1122 for (CXXRecordDecl::base_class_iterator
1123 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1124 const RecordType *RT = I->getType()->getAs<RecordType>();
1125 if (!RT) continue;
1126
1127 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1128 I->isVirtual(), I->getAccessSpecifier(),
1129 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1130 }
1131
1132 // Add any unhidden conversions provided by virtual bases.
1133 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1134 I != E; ++I) {
1135 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1136 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001137 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001138}
1139
1140/// getVisibleConversionFunctions - get all conversion functions visible
1141/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +00001142const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001143 // If root class, all conversions are visible.
1144 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +00001145 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +00001146 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +00001147 if (data().ComputedVisibleConversions)
1148 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +00001149 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001150 data().ComputedVisibleConversions = true;
1151 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001152}
1153
John McCall32daa422010-03-31 01:36:47 +00001154void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1155 // This operation is O(N) but extremely rare. Sema only uses it to
1156 // remove UsingShadowDecls in a class that were followed by a direct
1157 // declaration, e.g.:
1158 // class A : B {
1159 // using B::operator int;
1160 // operator int();
1161 // };
1162 // This is uncommon by itself and even more uncommon in conjunction
1163 // with sufficiently large numbers of directly-declared conversions
1164 // that asymptotic behavior matters.
1165
1166 UnresolvedSetImpl &Convs = *getConversionFunctions();
1167 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1168 if (Convs[I].getDecl() == ConvDecl) {
1169 Convs.erase(I);
1170 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1171 && "conversion was found multiple times in unresolved set");
1172 return;
1173 }
1174 }
1175
1176 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001177}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001178
Douglas Gregorf6b11852009-10-08 15:14:33 +00001179CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001180 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001181 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1182
1183 return 0;
1184}
1185
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001186MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1187 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1188}
1189
Douglas Gregorf6b11852009-10-08 15:14:33 +00001190void
1191CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1192 TemplateSpecializationKind TSK) {
1193 assert(TemplateOrInstantiation.isNull() &&
1194 "Previous template or instantiation?");
1195 assert(!isa<ClassTemplateSpecializationDecl>(this));
1196 TemplateOrInstantiation
1197 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1198}
1199
Anders Carlssonb13e3572009-12-07 06:33:48 +00001200TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1201 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001202 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1203 return Spec->getSpecializationKind();
1204
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001205 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001206 return MSInfo->getTemplateSpecializationKind();
1207
1208 return TSK_Undeclared;
1209}
1210
1211void
1212CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1213 if (ClassTemplateSpecializationDecl *Spec
1214 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1215 Spec->setSpecializationKind(TSK);
1216 return;
1217 }
1218
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001219 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001220 MSInfo->setTemplateSpecializationKind(TSK);
1221 return;
1222 }
1223
David Blaikieb219cfc2011-09-23 05:06:16 +00001224 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001225}
1226
Douglas Gregor1d110e02010-07-01 14:13:13 +00001227CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1228 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001229 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001230
1231 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001232 = Context.DeclarationNames.getCXXDestructorName(
1233 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001234
John McCallc0bf4622010-02-23 00:48:20 +00001235 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001236 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001237 if (I == E)
1238 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001240 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001241 return Dtor;
1242}
1243
Douglas Gregorda2142f2011-02-19 18:51:44 +00001244void CXXRecordDecl::completeDefinition() {
1245 completeDefinition(0);
1246}
1247
1248void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1249 RecordDecl::completeDefinition();
1250
David Blaikie4e4d0842012-03-11 07:00:24 +00001251 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001252 // Objective-C Automatic Reference Counting:
1253 // If a class has a non-static data member of Objective-C pointer
1254 // type (or array thereof), it is a non-POD type and its
1255 // default constructor (if any), copy constructor, copy assignment
1256 // operator, and destructor are non-trivial.
1257 struct DefinitionData &Data = data();
1258 Data.PlainOldData = false;
1259 Data.HasTrivialDefaultConstructor = false;
1260 Data.HasTrivialCopyConstructor = false;
1261 Data.HasTrivialCopyAssignment = false;
1262 Data.HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +00001263 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001264 }
1265
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001266 // If the class may be abstract (but hasn't been marked as such), check for
1267 // any pure final overriders.
1268 if (mayBeAbstract()) {
1269 CXXFinalOverriderMap MyFinalOverriders;
1270 if (!FinalOverriders) {
1271 getFinalOverriders(MyFinalOverriders);
1272 FinalOverriders = &MyFinalOverriders;
1273 }
1274
1275 bool Done = false;
1276 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1277 MEnd = FinalOverriders->end();
1278 M != MEnd && !Done; ++M) {
1279 for (OverridingMethods::iterator SO = M->second.begin(),
1280 SOEnd = M->second.end();
1281 SO != SOEnd && !Done; ++SO) {
1282 assert(SO->second.size() > 0 &&
1283 "All virtual functions have overridding virtual functions");
1284
1285 // C++ [class.abstract]p4:
1286 // A class is abstract if it contains or inherits at least one
1287 // pure virtual function for which the final overrider is pure
1288 // virtual.
1289 if (SO->second.front().Method->isPure()) {
1290 data().Abstract = true;
1291 Done = true;
1292 break;
1293 }
1294 }
1295 }
1296 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001297
1298 // Set access bits correctly on the directly-declared conversions.
1299 for (UnresolvedSetIterator I = data().Conversions.begin(),
1300 E = data().Conversions.end();
1301 I != E; ++I)
1302 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001303}
1304
1305bool CXXRecordDecl::mayBeAbstract() const {
1306 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1307 isDependentContext())
1308 return false;
1309
1310 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1311 BEnd = bases_end();
1312 B != BEnd; ++B) {
1313 CXXRecordDecl *BaseDecl
1314 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1315 if (BaseDecl->isAbstract())
1316 return true;
1317 }
1318
1319 return false;
1320}
1321
David Blaikie99ba9e32011-12-20 02:48:34 +00001322void CXXMethodDecl::anchor() { }
1323
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001324CXXMethodDecl *
1325CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001326 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001327 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001328 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001329 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001330 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001331 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001332 isStatic, SCAsWritten, isInline, isConstexpr,
1333 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001334}
1335
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001336CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1337 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1338 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1339 DeclarationNameInfo(), QualType(),
1340 0, false, SC_None, false, false,
1341 SourceLocation());
1342}
1343
Douglas Gregor90916562009-09-29 18:16:17 +00001344bool CXXMethodDecl::isUsualDeallocationFunction() const {
1345 if (getOverloadedOperator() != OO_Delete &&
1346 getOverloadedOperator() != OO_Array_Delete)
1347 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001348
1349 // C++ [basic.stc.dynamic.deallocation]p2:
1350 // A template instance is never a usual deallocation function,
1351 // regardless of its signature.
1352 if (getPrimaryTemplate())
1353 return false;
1354
Douglas Gregor90916562009-09-29 18:16:17 +00001355 // C++ [basic.stc.dynamic.deallocation]p2:
1356 // If a class T has a member deallocation function named operator delete
1357 // with exactly one parameter, then that function is a usual (non-placement)
1358 // deallocation function. [...]
1359 if (getNumParams() == 1)
1360 return true;
1361
1362 // C++ [basic.stc.dynamic.deallocation]p2:
1363 // [...] If class T does not declare such an operator delete but does
1364 // declare a member deallocation function named operator delete with
1365 // exactly two parameters, the second of which has type std::size_t (18.1),
1366 // then this function is a usual deallocation function.
1367 ASTContext &Context = getASTContext();
1368 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001369 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1370 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001371 return false;
1372
1373 // This function is a usual deallocation function if there are no
1374 // single-parameter deallocation functions of the same kind.
1375 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1376 R.first != R.second; ++R.first) {
1377 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1378 if (FD->getNumParams() == 1)
1379 return false;
1380 }
1381
1382 return true;
1383}
1384
Douglas Gregor06a9f362010-05-01 20:49:11 +00001385bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001386 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001387 // A user-declared copy assignment operator X::operator= is a non-static
1388 // non-template member function of class X with exactly one parameter of
1389 // type X, X&, const X&, volatile X& or const volatile X&.
1390 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1391 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001392 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001393 return false;
1394
1395 QualType ParamType = getParamDecl(0)->getType();
1396 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1397 ParamType = Ref->getPointeeType();
1398
1399 ASTContext &Context = getASTContext();
1400 QualType ClassType
1401 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1402 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1403}
1404
Sean Huntffe37fd2011-05-25 20:50:04 +00001405bool CXXMethodDecl::isMoveAssignmentOperator() const {
1406 // C++0x [class.copy]p19:
1407 // A user-declared move assignment operator X::operator= is a non-static
1408 // non-template member function of class X with exactly one parameter of type
1409 // X&&, const X&&, volatile X&&, or const volatile X&&.
1410 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1411 getPrimaryTemplate() || getDescribedFunctionTemplate())
1412 return false;
1413
1414 QualType ParamType = getParamDecl(0)->getType();
1415 if (!isa<RValueReferenceType>(ParamType))
1416 return false;
1417 ParamType = ParamType->getPointeeType();
1418
1419 ASTContext &Context = getASTContext();
1420 QualType ClassType
1421 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1422 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1423}
1424
Anders Carlsson05eb2442009-05-16 23:58:37 +00001425void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001426 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001427 assert(!MD->getParent()->isDependentContext() &&
1428 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001429 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001430
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001431 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001432}
1433
1434CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001435 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001436 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001437}
1438
1439CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001440 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001441 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001442}
1443
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001444unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001445 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001446 return getASTContext().overridden_methods_size(this);
1447}
1448
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001449QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001450 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1451 // If the member function is declared const, the type of this is const X*,
1452 // if the member function is declared volatile, the type of this is
1453 // volatile X*, and if the member function is declared const volatile,
1454 // the type of this is const volatile X*.
1455
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001456 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001457
John McCall3cb0ebd2010-03-10 03:28:59 +00001458 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001459 ClassTy = C.getQualifiedType(ClassTy,
1460 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001461 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001462}
1463
Eli Friedmand7d7f672009-12-06 20:50:05 +00001464bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001465 // If this function is a template instantiation, look at the template from
1466 // which it was instantiated.
1467 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1468 if (!CheckFn)
1469 CheckFn = this;
1470
Eli Friedmand7d7f672009-12-06 20:50:05 +00001471 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001472 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001473}
1474
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001475bool CXXMethodDecl::isLambdaStaticInvoker() const {
1476 return getParent()->isLambda() &&
1477 getIdentifier() && getIdentifier()->getName() == "__invoke";
1478}
1479
1480
Sean Huntcbb67482011-01-08 20:30:50 +00001481CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1482 TypeSourceInfo *TInfo, bool IsVirtual,
1483 SourceLocation L, Expr *Init,
1484 SourceLocation R,
1485 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001486 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001487 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1488 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001489{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001490}
1491
Sean Huntcbb67482011-01-08 20:30:50 +00001492CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1493 FieldDecl *Member,
1494 SourceLocation MemberLoc,
1495 SourceLocation L, Expr *Init,
1496 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001497 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001498 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001499 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1500{
1501}
1502
Sean Huntcbb67482011-01-08 20:30:50 +00001503CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1504 IndirectFieldDecl *Member,
1505 SourceLocation MemberLoc,
1506 SourceLocation L, Expr *Init,
1507 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001508 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001509 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001510 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001511{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001512}
1513
Sean Huntcbb67482011-01-08 20:30:50 +00001514CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001515 TypeSourceInfo *TInfo,
1516 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001517 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001518 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1519 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001520 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1521{
1522}
1523
1524CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001525 FieldDecl *Member,
1526 SourceLocation MemberLoc,
1527 SourceLocation L, Expr *Init,
1528 SourceLocation R,
1529 VarDecl **Indices,
1530 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001531 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001532 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001533 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001534{
1535 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1536 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1537}
1538
Sean Huntcbb67482011-01-08 20:30:50 +00001539CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1540 FieldDecl *Member,
1541 SourceLocation MemberLoc,
1542 SourceLocation L, Expr *Init,
1543 SourceLocation R,
1544 VarDecl **Indices,
1545 unsigned NumIndices) {
1546 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001547 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001548 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001549 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1550 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001551}
1552
Sean Huntcbb67482011-01-08 20:30:50 +00001553TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001554 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001555 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001556 else
1557 return TypeLoc();
1558}
1559
Sean Huntcbb67482011-01-08 20:30:50 +00001560const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001561 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001562 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001563 else
1564 return 0;
1565}
1566
Sean Huntcbb67482011-01-08 20:30:50 +00001567SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001568 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001569 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001570
1571 if (isInClassMemberInitializer())
1572 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001573
Douglas Gregor76852c22011-11-01 01:16:03 +00001574 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1575 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1576
1577 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001578}
1579
Sean Huntcbb67482011-01-08 20:30:50 +00001580SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001581 if (isInClassMemberInitializer()) {
1582 FieldDecl *D = getAnyMember();
1583 if (Expr *I = D->getInClassInitializer())
1584 return I->getSourceRange();
1585 return SourceRange();
1586 }
1587
Douglas Gregor802ab452009-12-02 22:36:29 +00001588 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001589}
1590
David Blaikie99ba9e32011-12-20 02:48:34 +00001591void CXXConstructorDecl::anchor() { }
1592
Douglas Gregorb48fe382008-10-31 09:07:45 +00001593CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001594CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1595 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1596 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1597 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001598}
1599
1600CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001601CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001602 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001603 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001604 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001605 bool isExplicit, bool isInline,
1606 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001607 assert(NameInfo.getName().getNameKind()
1608 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001609 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001610 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001611 isExplicit, isInline, isImplicitlyDeclared,
1612 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001613}
1614
Douglas Gregor76852c22011-11-01 01:16:03 +00001615CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1616 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1617 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1618 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1619 return Construct->getConstructor();
1620
1621 return 0;
1622}
1623
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001624bool CXXConstructorDecl::isDefaultConstructor() const {
1625 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001626 // A default constructor for a class X is a constructor of class
1627 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001628 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001629 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001630}
1631
Mike Stump1eb44332009-09-09 15:08:12 +00001632bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001633CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001634 return isCopyOrMoveConstructor(TypeQuals) &&
1635 getParamDecl(0)->getType()->isLValueReferenceType();
1636}
1637
1638bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1639 return isCopyOrMoveConstructor(TypeQuals) &&
1640 getParamDecl(0)->getType()->isRValueReferenceType();
1641}
1642
1643/// \brief Determine whether this is a copy or move constructor.
1644bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001645 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001646 // A non-template constructor for class X is a copy constructor
1647 // if its first parameter is of type X&, const X&, volatile X& or
1648 // const volatile X&, and either there are no other parameters
1649 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001650 // C++0x [class.copy]p3:
1651 // A non-template constructor for class X is a move constructor if its
1652 // first parameter is of type X&&, const X&&, volatile X&&, or
1653 // const volatile X&&, and either there are no other parameters or else
1654 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001655 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001656 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001657 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001658 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001659 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001660
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001661 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001662
1663 // Do we have a reference type?
1664 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001665 if (!ParamRefType)
1666 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001667
Douglas Gregorfd476482009-11-13 23:59:09 +00001668 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001669 ASTContext &Context = getASTContext();
1670
Douglas Gregorfd476482009-11-13 23:59:09 +00001671 CanQualType PointeeType
1672 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001673 CanQualType ClassTy
1674 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001675 if (PointeeType.getUnqualifiedType() != ClassTy)
1676 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001677
John McCall0953e762009-09-24 19:53:00 +00001678 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001679
1680 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001681 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001682 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001683}
1684
Anders Carlssonfaccd722009-08-28 16:57:08 +00001685bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001686 // C++ [class.conv.ctor]p1:
1687 // A constructor declared without the function-specifier explicit
1688 // that can be called with a single parameter specifies a
1689 // conversion from the type of its first parameter to the type of
1690 // its class. Such a constructor is called a converting
1691 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001692 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001693 return false;
1694
Mike Stump1eb44332009-09-09 15:08:12 +00001695 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001696 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001697 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001698 (getNumParams() > 1 &&
1699 (getParamDecl(1)->hasDefaultArg() ||
1700 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001701}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001702
Douglas Gregor6493cc52010-11-08 17:16:59 +00001703bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001704 if ((getNumParams() < 1) ||
1705 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1706 (getPrimaryTemplate() == 0) ||
1707 (getDescribedFunctionTemplate() != 0))
1708 return false;
1709
1710 const ParmVarDecl *Param = getParamDecl(0);
1711
1712 ASTContext &Context = getASTContext();
1713 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1714
Douglas Gregor66724ea2009-11-14 01:20:54 +00001715 // Is it the same as our our class type?
1716 CanQualType ClassTy
1717 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1718 if (ParamType.getUnqualifiedType() != ClassTy)
1719 return false;
1720
1721 return true;
1722}
1723
Sebastian Redlf677ea32011-02-05 19:23:19 +00001724const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1725 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001726 method_iterator It = getASTContext().overridden_methods_begin(this);
1727 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001728 return 0;
1729
1730 return cast<CXXConstructorDecl>(*It);
1731}
1732
1733void
1734CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1735 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001736 assert(getASTContext().overridden_methods_size(this) == 0 &&
1737 "Base ctor already set.");
1738 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001739}
1740
David Blaikie99ba9e32011-12-20 02:48:34 +00001741void CXXDestructorDecl::anchor() { }
1742
Douglas Gregor42a552f2008-11-05 20:51:48 +00001743CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001744CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1745 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1746 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001747 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001748}
1749
1750CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001751CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001752 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001753 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001754 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001755 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001756 assert(NameInfo.getName().getNameKind()
1757 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001758 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001759 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001760 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001761}
1762
David Blaikie99ba9e32011-12-20 02:48:34 +00001763void CXXConversionDecl::anchor() { }
1764
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001765CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001766CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1767 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1768 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1769 QualType(), 0, false, false, false,
1770 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001771}
1772
1773CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001774CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001775 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001776 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001777 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001778 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001779 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001780 assert(NameInfo.getName().getNameKind()
1781 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001782 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001783 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001784 isInline, isExplicit, isConstexpr,
1785 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001786}
1787
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001788bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1789 return isImplicit() && getParent()->isLambda() &&
1790 getConversionType()->isBlockPointerType();
1791}
1792
David Blaikie99ba9e32011-12-20 02:48:34 +00001793void LinkageSpecDecl::anchor() { }
1794
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001795LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001796 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001797 SourceLocation ExternLoc,
1798 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001799 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001800 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001801 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001802}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001803
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001804LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1805 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1806 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1807 lang_c, SourceLocation());
1808}
1809
David Blaikie99ba9e32011-12-20 02:48:34 +00001810void UsingDirectiveDecl::anchor() { }
1811
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001812UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1813 SourceLocation L,
1814 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001815 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001816 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001817 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001818 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001819 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1820 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001821 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1822 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001823}
1824
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001825UsingDirectiveDecl *
1826UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1827 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1828 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1829 NestedNameSpecifierLoc(),
1830 SourceLocation(), 0, 0);
1831}
1832
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001833NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1834 if (NamespaceAliasDecl *NA =
1835 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1836 return NA->getNamespace();
1837 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1838}
1839
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001840void NamespaceDecl::anchor() { }
1841
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001842NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1843 SourceLocation StartLoc,
1844 SourceLocation IdLoc, IdentifierInfo *Id,
1845 NamespaceDecl *PrevDecl)
1846 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1847 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1848{
1849 setPreviousDeclaration(PrevDecl);
1850
1851 if (PrevDecl)
1852 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1853}
1854
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001855NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001856 bool Inline, SourceLocation StartLoc,
1857 SourceLocation IdLoc, IdentifierInfo *Id,
1858 NamespaceDecl *PrevDecl) {
1859 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001860}
1861
1862NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1863 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001864 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1865 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001866}
1867
David Blaikie99ba9e32011-12-20 02:48:34 +00001868void NamespaceAliasDecl::anchor() { }
1869
Mike Stump1eb44332009-09-09 15:08:12 +00001870NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001871 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001872 SourceLocation AliasLoc,
1873 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001874 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001875 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001876 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001877 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1878 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001879 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1880 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001881}
1882
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001883NamespaceAliasDecl *
1884NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1885 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1886 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1887 NestedNameSpecifierLoc(),
1888 SourceLocation(), 0);
1889}
1890
David Blaikie99ba9e32011-12-20 02:48:34 +00001891void UsingShadowDecl::anchor() { }
1892
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001893UsingShadowDecl *
1894UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1895 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1896 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1897}
1898
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001899UsingDecl *UsingShadowDecl::getUsingDecl() const {
1900 const UsingShadowDecl *Shadow = this;
1901 while (const UsingShadowDecl *NextShadow =
1902 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1903 Shadow = NextShadow;
1904 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1905}
1906
David Blaikie99ba9e32011-12-20 02:48:34 +00001907void UsingDecl::anchor() { }
1908
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001909void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1910 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1911 "declaration already in set");
1912 assert(S->getUsingDecl() == this);
1913
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001914 if (FirstUsingShadow.getPointer())
1915 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1916 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001917}
1918
1919void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1920 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1921 "declaration not in set");
1922 assert(S->getUsingDecl() == this);
1923
1924 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1925
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001926 if (FirstUsingShadow.getPointer() == S) {
1927 FirstUsingShadow.setPointer(
1928 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001929 S->UsingOrNextShadow = this;
1930 return;
1931 }
1932
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001933 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001934 while (Prev->UsingOrNextShadow != S)
1935 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1936 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1937 S->UsingOrNextShadow = this;
1938}
1939
Douglas Gregordc355712011-02-25 00:36:19 +00001940UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1941 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001942 const DeclarationNameInfo &NameInfo,
1943 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001944 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001945}
1946
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001947UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1948 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1949 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1950 DeclarationNameInfo(), false);
1951}
1952
David Blaikie99ba9e32011-12-20 02:48:34 +00001953void UnresolvedUsingValueDecl::anchor() { }
1954
John McCall7ba107a2009-11-18 02:36:19 +00001955UnresolvedUsingValueDecl *
1956UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1957 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001958 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001959 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001960 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001961 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001962}
1963
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001964UnresolvedUsingValueDecl *
1965UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1966 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1967 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1968 NestedNameSpecifierLoc(),
1969 DeclarationNameInfo());
1970}
1971
David Blaikie99ba9e32011-12-20 02:48:34 +00001972void UnresolvedUsingTypenameDecl::anchor() { }
1973
John McCall7ba107a2009-11-18 02:36:19 +00001974UnresolvedUsingTypenameDecl *
1975UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1976 SourceLocation UsingLoc,
1977 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001978 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001979 SourceLocation TargetNameLoc,
1980 DeclarationName TargetName) {
1981 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001982 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001983 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001984}
1985
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001986UnresolvedUsingTypenameDecl *
1987UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1988 void *Mem = AllocateDeserializedDecl(C, ID,
1989 sizeof(UnresolvedUsingTypenameDecl));
1990 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
1991 SourceLocation(),
1992 NestedNameSpecifierLoc(),
1993 SourceLocation(),
1994 0);
1995}
1996
David Blaikie99ba9e32011-12-20 02:48:34 +00001997void StaticAssertDecl::anchor() { }
1998
Anders Carlssonfb311762009-03-14 00:25:26 +00001999StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002000 SourceLocation StaticAssertLoc,
2001 Expr *AssertExpr,
2002 StringLiteral *Message,
2003 SourceLocation RParenLoc) {
2004 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2005 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00002006}
2007
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002008StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2009 unsigned ID) {
2010 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
2011 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,SourceLocation());
2012}
2013
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002014static const char *getAccessName(AccessSpecifier AS) {
2015 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002016 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002017 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002018 case AS_public:
2019 return "public";
2020 case AS_private:
2021 return "private";
2022 case AS_protected:
2023 return "protected";
2024 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002025 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002026}
2027
2028const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2029 AccessSpecifier AS) {
2030 return DB << getAccessName(AS);
2031}
Richard Smithf15fda02012-02-02 01:16:57 +00002032
2033const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2034 AccessSpecifier AS) {
2035 return DB << getAccessName(AS);
2036}