blob: 84aa7c5c881321809852501da77f7b313bb6224d [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
Richard Smith3f5f5582012-06-08 21:09:22 +0000473void CXXRecordDecl::markedConstructorConstexpr(CXXConstructorDecl *CD) {
474 if (CD->isCopyConstructor())
475 data().HasConstexprCopyConstructor = true;
476 else if (CD->isMoveConstructor())
477 data().HasConstexprMoveConstructor = true;
478 else
479 data().HasConstexprNonCopyMoveConstructor = true;
480
481 if (CD->isDefaultConstructor())
482 data().HasConstexprDefaultConstructor = true;
483}
484
Douglas Gregor21386642010-09-28 21:55:22 +0000485void CXXRecordDecl::addedMember(Decl *D) {
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +0000486 if (!D->isImplicit() &&
487 !isa<FieldDecl>(D) &&
488 !isa<IndirectFieldDecl>(D) &&
489 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class))
490 data().HasOnlyCMembers = false;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000491
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000492 // Ignore friends and invalid declarations.
493 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000494 return;
495
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000496 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
497 if (FunTmpl)
498 D = FunTmpl->getTemplatedDecl();
499
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000500 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
501 if (Method->isVirtual()) {
502 // C++ [dcl.init.aggr]p1:
503 // An aggregate is an array or a class with [...] no virtual functions.
504 data().Aggregate = false;
505
506 // C++ [class]p4:
507 // A POD-struct is an aggregate class...
508 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000509
510 // Virtual functions make the class non-empty.
511 // FIXME: Standard ref?
512 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000513
514 // C++ [class.virtual]p1:
515 // A class that declares or inherits a virtual function is called a
516 // polymorphic class.
517 data().Polymorphic = true;
518
Sean Hunt023df372011-05-09 18:22:59 +0000519 // C++0x [class.ctor]p5
520 // A default constructor is trivial [...] if:
521 // -- its class has no virtual functions [...]
522 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000523
524 // C++0x [class.copy]p13:
525 // A copy/move constructor for class X is trivial if [...]
526 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000527 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000528 data().HasTrivialMoveConstructor = false;
529
530 // C++0x [class.copy]p27:
531 // A copy/move assignment operator for class X is trivial if [...]
532 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000533 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000534 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000535
Chandler Carrutha8225442011-04-30 09:17:45 +0000536 // C++0x [class]p7:
537 // A standard-layout class is a class that: [...]
538 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000539 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000540 }
541 }
542
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000543 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000544 // Notify that an implicit member was added after the definition
545 // was completed.
546 if (!isBeingDefined())
547 if (ASTMutationListener *L = getASTMutationListener())
548 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000549
Sean Huntffe37fd2011-05-25 20:50:04 +0000550 // If this is a special member function, note that it was added and then
551 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000552 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith61802452011-12-22 02:22:31 +0000553 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000554 data().DeclaredDefaultConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000555 if (Constructor->isConstexpr()) {
556 data().HasConstexprDefaultConstructor = true;
557 data().HasConstexprNonCopyMoveConstructor = true;
558 }
559 } else if (Constructor->isCopyConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000560 data().DeclaredCopyConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000561 if (Constructor->isConstexpr())
562 data().HasConstexprCopyConstructor = true;
563 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000564 data().DeclaredMoveConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000565 if (Constructor->isConstexpr())
566 data().HasConstexprMoveConstructor = true;
567 } else
Sean Huntffe37fd2011-05-25 20:50:04 +0000568 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000569 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000570 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000571 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000572 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000573 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
574 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000575 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000576 else if (Method->isMoveAssignmentOperator())
577 data().DeclaredMoveAssignment = true;
578 else
579 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000580 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000581 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000582
Sean Huntffe37fd2011-05-25 20:50:04 +0000583NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000584 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000585 }
586
587 // Handle (user-declared) constructors.
588 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
589 // Note that we have a user-declared constructor.
590 data().UserDeclaredConstructor = true;
591
Richard Smith017ab772011-09-05 02:13:09 +0000592 // Technically, "user-provided" is only defined for special member
593 // functions, but the intent of the standard is clearly that it should apply
594 // to all functions.
595 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000596
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000597 if (Constructor->isDefaultConstructor()) {
598 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000599 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000600 // C++0x [class.ctor]p5:
601 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000602 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000603 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000604 }
Richard Smith61802452011-12-22 02:22:31 +0000605 if (Constructor->isConstexpr()) {
606 data().HasConstexprDefaultConstructor = true;
607 data().HasConstexprNonCopyMoveConstructor = true;
608 }
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000609 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000610
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000611 // Note when we have a user-declared copy or move constructor, which will
612 // suppress the implicit declaration of those constructors.
613 if (!FunTmpl) {
614 if (Constructor->isCopyConstructor()) {
615 data().UserDeclaredCopyConstructor = true;
616 data().DeclaredCopyConstructor = true;
617
618 // 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().HasTrivialCopyConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000623
624 if (Constructor->isConstexpr())
625 data().HasConstexprCopyConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000626 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000627 data().UserDeclaredMoveConstructor = true;
628 data().DeclaredMoveConstructor = true;
629
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000630 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000631 // A copy/move constructor for class X is trivial if it is not
632 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000633 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000634 data().HasTrivialMoveConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000635
636 if (Constructor->isConstexpr())
637 data().HasConstexprMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000638 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000639 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000640 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
641 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000642 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000643 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000644 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000645
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000646 // C++ [dcl.init.aggr]p1:
647 // An aggregate is an array or a class with no user-declared
648 // constructors [...].
649 // C++0x [dcl.init.aggr]p1:
650 // An aggregate is an array or a class with no user-provided
651 // constructors [...].
David Blaikie4e4d0842012-03-11 07:00:24 +0000652 if (!getASTContext().getLangOpts().CPlusPlus0x || UserProvided)
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000653 data().Aggregate = false;
654
655 // C++ [class]p4:
656 // A POD-struct is an aggregate class [...]
657 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
658 // type is technically an aggregate in C++0x since it wouldn't be in 03.
659 data().PlainOldData = false;
660
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000661 return;
662 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000663
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000664 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000665 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000666 data().DeclaredDestructor = true;
667 data().UserDeclaredDestructor = true;
Richard Smithdfefb842012-02-25 07:33:38 +0000668 data().HasIrrelevantDestructor = false;
669
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000670 // C++ [class]p4:
671 // A POD-struct is an aggregate class that has [...] no user-defined
672 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000673 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000674 data().PlainOldData = false;
675
Douglas Gregor45fa5602011-11-07 20:56:01 +0000676 // C++11 [class.dtor]p5:
677 // A destructor is trivial if it is not user-provided and if
678 // -- the destructor is not virtual.
Richard Smith61802452011-12-22 02:22:31 +0000679 if (DD->isUserProvided() || DD->isVirtual()) {
Sean Huntcf34e752011-05-16 22:41:40 +0000680 data().HasTrivialDestructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000681 // C++11 [dcl.constexpr]p1:
682 // The constexpr specifier shall be applied only to [...] the
683 // declaration of a static data member of a literal type.
684 // C++11 [basic.types]p10:
685 // A type is a literal type if it is [...] a class type that [...] has
686 // a trivial destructor.
687 data().DefaultedDefaultConstructorIsConstexpr = false;
688 data().DefaultedCopyConstructorIsConstexpr = false;
689 data().DefaultedMoveConstructorIsConstexpr = false;
690 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000691
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000692 return;
693 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000694
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000695 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000696 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000697 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000698 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000699 // A POD-struct is an aggregate class that [...] has no user-defined
700 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000701 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000702 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000703
Sean Huntffe37fd2011-05-25 20:50:04 +0000704 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000705
Sean Huntffe37fd2011-05-25 20:50:04 +0000706 // Suppress the implicit declaration of a copy constructor.
707 data().UserDeclaredCopyAssignment = true;
708 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000709
Sean Huntffe37fd2011-05-25 20:50:04 +0000710 // C++0x [class.copy]p27:
711 // A copy/move assignment operator for class X is trivial if it is
712 // neither user-provided nor deleted [...]
713 if (Method->isUserProvided())
714 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000715
Sean Huntffe37fd2011-05-25 20:50:04 +0000716 return;
717 }
718
719 if (Method->isMoveAssignmentOperator()) {
720 // This is an extension in C++03 mode, but we'll keep consistency by
721 // taking a move assignment operator to induce non-POD-ness
722 data().PlainOldData = false;
723
724 // This is a move assignment operator.
725 data().UserDeclaredMoveAssignment = true;
726 data().DeclaredMoveAssignment = true;
727
728 // C++0x [class.copy]p27:
729 // A copy/move assignment operator for class X is trivial if it is
730 // neither user-provided nor deleted [...]
731 if (Method->isUserProvided())
732 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000733 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000734
Douglas Gregore80622f2010-09-29 04:25:11 +0000735 // Keep the list of conversion functions up-to-date.
736 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
737 // We don't record specializations.
738 if (Conversion->getPrimaryTemplate())
739 return;
740
741 // FIXME: We intentionally don't use the decl's access here because it
742 // hasn't been set yet. That's really just a misdesign in Sema.
743
744 if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000745 if (FunTmpl->getPreviousDecl())
746 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000747 FunTmpl);
748 else
749 data().Conversions.addDecl(FunTmpl);
750 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000751 if (Conversion->getPreviousDecl())
752 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000753 Conversion);
754 else
755 data().Conversions.addDecl(Conversion);
756 }
757 }
758
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000759 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000760 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000761
762 // Handle non-static data members.
763 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000764 // C++ [class.bit]p2:
765 // A declaration for a bit-field that omits the identifier declares an
766 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
767 // initialized.
768 if (Field->isUnnamedBitfield())
769 return;
770
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000771 // C++ [dcl.init.aggr]p1:
772 // An aggregate is an array or a class (clause 9) with [...] no
773 // private or protected non-static data members (clause 11).
774 //
775 // A POD must be an aggregate.
776 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
777 data().Aggregate = false;
778 data().PlainOldData = false;
779 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000780
781 // C++0x [class]p7:
782 // A standard-layout class is a class that:
783 // [...]
784 // -- has the same access control for all non-static data members,
785 switch (D->getAccess()) {
786 case AS_private: data().HasPrivateFields = true; break;
787 case AS_protected: data().HasProtectedFields = true; break;
788 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000789 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000790 };
791 if ((data().HasPrivateFields + data().HasProtectedFields +
792 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000793 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000794
Douglas Gregor2bb11012011-05-13 01:05:07 +0000795 // Keep track of the presence of mutable fields.
796 if (Field->isMutable())
797 data().HasMutableFields = true;
798
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000799 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000800 // A POD struct is a class that is both a trivial class and a
801 // standard-layout class, and has no non-static data members of type
802 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000803 //
804 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
805 // that does not explicitly have no lifetime makes the class a non-POD.
806 // However, we delay setting PlainOldData to false in this case so that
807 // Sema has a chance to diagnostic causes where the same class will be
808 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
809 // In this case, the class will become a non-POD class when we complete
810 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000811 ASTContext &Context = getASTContext();
812 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000813 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000814 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000815 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
816 setHasObjectMember(true);
817 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000818 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000819
Chandler Carrutha8225442011-04-30 09:17:45 +0000820 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000821 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000822
Chandler Carrutha8225442011-04-30 09:17:45 +0000823 // C++0x [class]p7:
824 // A standard-layout class is a class that:
825 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000826 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000827 }
828
Richard Smith86c3ae42012-02-13 03:54:03 +0000829 // Record if this field is the first non-literal or volatile field or base.
830 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000831 data().HasNonLiteralTypeFieldsOrBases = true;
832
Richard Smith7a614d82011-06-11 17:19:42 +0000833 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000834 data().HasInClassInitializer = true;
835
836 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000837 // A default constructor is trivial if [...] no non-static data member
838 // of its class has a brace-or-equal-initializer.
839 data().HasTrivialDefaultConstructor = false;
840
Richard Smithd079abf2012-05-07 01:07:30 +0000841 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000842 // An aggregate is a [...] class with [...] no
843 // brace-or-equal-initializers for non-static data members.
844 data().Aggregate = false;
845
Richard Smithd079abf2012-05-07 01:07:30 +0000846 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000847 // A POD struct is [...] a trivial class.
848 data().PlainOldData = false;
849 }
850
Douglas Gregor85606eb2010-09-28 20:50:54 +0000851 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
852 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
853 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000854 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000855 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000856 // -- for all the non-static data members of its class that are of
857 // class type (or array thereof), each such class has a trivial
858 // default constructor.
859 if (!FieldRec->hasTrivialDefaultConstructor())
860 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000861
862 // C++0x [class.copy]p13:
863 // A copy/move constructor 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 constructor selected to copy/move that
867 // member is trivial;
868 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000869 if (!FieldRec->hasTrivialCopyConstructor())
870 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000871 if (!FieldRec->hasTrivialMoveConstructor())
872 data().HasTrivialMoveConstructor = false;
873
874 // C++0x [class.copy]p27:
875 // A copy/move assignment operator for class X is trivial if [...]
876 // [...]
877 // -- for each non-static data member of X that is of class type (or
878 // an array thereof), the assignment operator selected to
879 // copy/move that member is trivial;
880 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000881 if (!FieldRec->hasTrivialCopyAssignment())
882 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000883 if (!FieldRec->hasTrivialMoveAssignment())
884 data().HasTrivialMoveAssignment = false;
885
Douglas Gregor85606eb2010-09-28 20:50:54 +0000886 if (!FieldRec->hasTrivialDestructor())
887 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000888 if (!FieldRec->hasIrrelevantDestructor())
889 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000890 if (FieldRec->hasObjectMember())
891 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000892
893 // C++0x [class]p7:
894 // A standard-layout class is a class that:
895 // -- has no non-static data members of type non-standard-layout
896 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000897 if (!FieldRec->isStandardLayout())
898 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000899
900 // C++0x [class]p7:
901 // A standard-layout class is a class that:
902 // [...]
903 // -- has no base classes of the same type as the first non-static
904 // data member.
905 // We don't want to expend bits in the state of the record decl
906 // tracking whether this is the first non-static data member so we
907 // cheat a bit and use some of the existing state: the empty bit.
908 // Virtual bases and virtual methods make a class non-empty, but they
909 // also make it non-standard-layout so we needn't check here.
910 // A non-empty base class may leave the class standard-layout, but not
911 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000912 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000913 // data member must come through here with Empty still true, and Empty
914 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000915 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000916 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
917 BE = bases_end();
918 BI != BE; ++BI) {
919 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000920 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000921 break;
922 }
923 }
924 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000925
926 // Keep track of the presence of mutable fields.
927 if (FieldRec->hasMutableFields())
928 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000929
930 // C++11 [class.copy]p13:
931 // If the implicitly-defined constructor would satisfy the
932 // requirements of a constexpr constructor, the implicitly-defined
933 // constructor is constexpr.
934 // C++11 [dcl.constexpr]p4:
935 // -- every constructor involved in initializing non-static data
936 // members [...] shall be a constexpr constructor
937 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000938 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000939 // The standard requires any in-class initializer to be a constant
940 // expression. We consider this to be a defect.
941 data().DefaultedDefaultConstructorIsConstexpr = false;
942
943 if (!FieldRec->hasConstexprCopyConstructor())
944 data().DefaultedCopyConstructorIsConstexpr = false;
945
946 if (FieldRec->hasDeclaredMoveConstructor() ||
947 FieldRec->needsImplicitMoveConstructor())
948 // FIXME: If the implicit move constructor generated for the member's
949 // class would be ill-formed, the implicit move constructor generated
950 // for this class calls the member's copy constructor.
951 data().DefaultedMoveConstructorIsConstexpr &=
952 FieldRec->hasConstexprMoveConstructor();
953 else if (!FieldRec->hasConstexprCopyConstructor())
954 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000955 }
Richard Smith61802452011-12-22 02:22:31 +0000956 } else {
957 // Base element type of field is a non-class type.
958 if (!T->isLiteralType()) {
959 data().DefaultedDefaultConstructorIsConstexpr = false;
960 data().DefaultedCopyConstructorIsConstexpr = false;
961 data().DefaultedMoveConstructorIsConstexpr = false;
Richard Smithd079abf2012-05-07 01:07:30 +0000962 } else if (!Field->hasInClassInitializer() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000963 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000964 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000965
966 // C++0x [class]p7:
967 // A standard-layout class is a class that:
968 // [...]
969 // -- either has no non-static data members in the most derived
970 // class and at most one base class with non-static data members,
971 // or has no base classes with non-static data members, and
972 // At this point we know that we have a non-static data member, so the last
973 // clause holds.
974 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000975 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000976
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000977 // If this is not a zero-length bit-field, then the class is not empty.
978 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000979 if (!Field->isBitField() ||
980 (!Field->getBitWidth()->isTypeDependent() &&
981 !Field->getBitWidth()->isValueDependent() &&
982 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000983 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000984 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000985 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000986
987 // Handle using declarations of conversion functions.
988 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
989 if (Shadow->getDeclName().getNameKind()
990 == DeclarationName::CXXConversionFunctionName)
991 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000992}
993
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000994bool CXXRecordDecl::isCLike() const {
995 if (getTagKind() == TTK_Class || !TemplateOrInstantiation.isNull())
996 return false;
997 if (!hasDefinition())
998 return true;
999
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +00001000 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +00001001}
1002
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001003void CXXRecordDecl::getCaptureFields(
1004 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +00001005 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001006 Captures.clear();
1007 ThisCapture = 0;
1008
Douglas Gregorda8962a2012-02-13 15:44:47 +00001009 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001010 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +00001011 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001012 C != CEnd; ++C, ++Field) {
1013 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +00001014 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001015 continue;
1016 }
1017
David Blaikie581deb32012-06-06 20:45:41 +00001018 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001019 }
1020}
1021
1022
John McCallb05b5f32010-03-15 09:07:48 +00001023static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1024 QualType T;
John McCall32daa422010-03-31 01:36:47 +00001025 if (isa<UsingShadowDecl>(Conv))
1026 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +00001027 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
1028 T = ConvTemp->getTemplatedDecl()->getResultType();
1029 else
1030 T = cast<CXXConversionDecl>(Conv)->getConversionType();
1031 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +00001032}
1033
John McCallb05b5f32010-03-15 09:07:48 +00001034/// Collect the visible conversions of a base class.
1035///
1036/// \param Base a base class of the class we're considering
1037/// \param InVirtual whether this base class is a virtual base (or a base
1038/// of a virtual base)
1039/// \param Access the access along the inheritance path to this base
1040/// \param ParentHiddenTypes the conversions provided by the inheritors
1041/// of this base
1042/// \param Output the set to which to add conversions from non-virtual bases
1043/// \param VOutput the set to which to add conversions from virtual bases
1044/// \param HiddenVBaseCs the set of conversions which were hidden in a
1045/// virtual base along some inheritance path
1046static void CollectVisibleConversions(ASTContext &Context,
1047 CXXRecordDecl *Record,
1048 bool InVirtual,
1049 AccessSpecifier Access,
1050 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
1051 UnresolvedSetImpl &Output,
1052 UnresolvedSetImpl &VOutput,
1053 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1054 // The set of types which have conversions in this class or its
1055 // subclasses. As an optimization, we don't copy the derived set
1056 // unless it might change.
1057 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1058 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1059
1060 // Collect the direct conversions and figure out which conversions
1061 // will be hidden in the subclasses.
1062 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1063 if (!Cs.empty()) {
1064 HiddenTypesBuffer = ParentHiddenTypes;
1065 HiddenTypes = &HiddenTypesBuffer;
1066
1067 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +00001068 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1069 bool Hidden = ParentHiddenTypes.count(ConvType);
1070 if (!Hidden)
1071 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001072
1073 // If this conversion is hidden and we're in a virtual base,
1074 // remember that it's hidden along some inheritance path.
1075 if (Hidden && InVirtual)
1076 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1077
1078 // If this conversion isn't hidden, add it to the appropriate output.
1079 else if (!Hidden) {
1080 AccessSpecifier IAccess
1081 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1082
1083 if (InVirtual)
1084 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001085 else
John McCallb05b5f32010-03-15 09:07:48 +00001086 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001087 }
1088 }
1089 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001090
John McCallb05b5f32010-03-15 09:07:48 +00001091 // Collect information recursively from any base classes.
1092 for (CXXRecordDecl::base_class_iterator
1093 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1094 const RecordType *RT = I->getType()->getAs<RecordType>();
1095 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001096
John McCallb05b5f32010-03-15 09:07:48 +00001097 AccessSpecifier BaseAccess
1098 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1099 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001100
John McCallb05b5f32010-03-15 09:07:48 +00001101 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1102 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1103 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001104 }
John McCallb05b5f32010-03-15 09:07:48 +00001105}
Sebastian Redl9994a342009-10-25 17:03:50 +00001106
John McCallb05b5f32010-03-15 09:07:48 +00001107/// Collect the visible conversions of a class.
1108///
1109/// This would be extremely straightforward if it weren't for virtual
1110/// bases. It might be worth special-casing that, really.
1111static void CollectVisibleConversions(ASTContext &Context,
1112 CXXRecordDecl *Record,
1113 UnresolvedSetImpl &Output) {
1114 // The collection of all conversions in virtual bases that we've
1115 // found. These will be added to the output as long as they don't
1116 // appear in the hidden-conversions set.
1117 UnresolvedSet<8> VBaseCs;
1118
1119 // The set of conversions in virtual bases that we've determined to
1120 // be hidden.
1121 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1122
1123 // The set of types hidden by classes derived from this one.
1124 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1125
1126 // Go ahead and collect the direct conversions and add them to the
1127 // hidden-types set.
1128 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1129 Output.append(Cs.begin(), Cs.end());
1130 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1131 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1132
1133 // Recursively collect conversions from base classes.
1134 for (CXXRecordDecl::base_class_iterator
1135 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1136 const RecordType *RT = I->getType()->getAs<RecordType>();
1137 if (!RT) continue;
1138
1139 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1140 I->isVirtual(), I->getAccessSpecifier(),
1141 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1142 }
1143
1144 // Add any unhidden conversions provided by virtual bases.
1145 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1146 I != E; ++I) {
1147 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1148 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001149 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001150}
1151
1152/// getVisibleConversionFunctions - get all conversion functions visible
1153/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +00001154const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001155 // If root class, all conversions are visible.
1156 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +00001157 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +00001158 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +00001159 if (data().ComputedVisibleConversions)
1160 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +00001161 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001162 data().ComputedVisibleConversions = true;
1163 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001164}
1165
John McCall32daa422010-03-31 01:36:47 +00001166void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1167 // This operation is O(N) but extremely rare. Sema only uses it to
1168 // remove UsingShadowDecls in a class that were followed by a direct
1169 // declaration, e.g.:
1170 // class A : B {
1171 // using B::operator int;
1172 // operator int();
1173 // };
1174 // This is uncommon by itself and even more uncommon in conjunction
1175 // with sufficiently large numbers of directly-declared conversions
1176 // that asymptotic behavior matters.
1177
1178 UnresolvedSetImpl &Convs = *getConversionFunctions();
1179 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1180 if (Convs[I].getDecl() == ConvDecl) {
1181 Convs.erase(I);
1182 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1183 && "conversion was found multiple times in unresolved set");
1184 return;
1185 }
1186 }
1187
1188 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001189}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001190
Douglas Gregorf6b11852009-10-08 15:14:33 +00001191CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001192 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001193 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1194
1195 return 0;
1196}
1197
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001198MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1199 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1200}
1201
Douglas Gregorf6b11852009-10-08 15:14:33 +00001202void
1203CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1204 TemplateSpecializationKind TSK) {
1205 assert(TemplateOrInstantiation.isNull() &&
1206 "Previous template or instantiation?");
1207 assert(!isa<ClassTemplateSpecializationDecl>(this));
1208 TemplateOrInstantiation
1209 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1210}
1211
Anders Carlssonb13e3572009-12-07 06:33:48 +00001212TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1213 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001214 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1215 return Spec->getSpecializationKind();
1216
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001217 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001218 return MSInfo->getTemplateSpecializationKind();
1219
1220 return TSK_Undeclared;
1221}
1222
1223void
1224CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1225 if (ClassTemplateSpecializationDecl *Spec
1226 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1227 Spec->setSpecializationKind(TSK);
1228 return;
1229 }
1230
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001231 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001232 MSInfo->setTemplateSpecializationKind(TSK);
1233 return;
1234 }
1235
David Blaikieb219cfc2011-09-23 05:06:16 +00001236 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001237}
1238
Douglas Gregor1d110e02010-07-01 14:13:13 +00001239CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1240 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001241 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001242
1243 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001244 = Context.DeclarationNames.getCXXDestructorName(
1245 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001246
John McCallc0bf4622010-02-23 00:48:20 +00001247 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001248 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001249 if (I == E)
1250 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001252 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001253 return Dtor;
1254}
1255
Douglas Gregorda2142f2011-02-19 18:51:44 +00001256void CXXRecordDecl::completeDefinition() {
1257 completeDefinition(0);
1258}
1259
1260void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1261 RecordDecl::completeDefinition();
1262
David Blaikie4e4d0842012-03-11 07:00:24 +00001263 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001264 // Objective-C Automatic Reference Counting:
1265 // If a class has a non-static data member of Objective-C pointer
1266 // type (or array thereof), it is a non-POD type and its
1267 // default constructor (if any), copy constructor, copy assignment
1268 // operator, and destructor are non-trivial.
1269 struct DefinitionData &Data = data();
1270 Data.PlainOldData = false;
1271 Data.HasTrivialDefaultConstructor = false;
1272 Data.HasTrivialCopyConstructor = false;
1273 Data.HasTrivialCopyAssignment = false;
1274 Data.HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +00001275 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001276 }
1277
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001278 // If the class may be abstract (but hasn't been marked as such), check for
1279 // any pure final overriders.
1280 if (mayBeAbstract()) {
1281 CXXFinalOverriderMap MyFinalOverriders;
1282 if (!FinalOverriders) {
1283 getFinalOverriders(MyFinalOverriders);
1284 FinalOverriders = &MyFinalOverriders;
1285 }
1286
1287 bool Done = false;
1288 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1289 MEnd = FinalOverriders->end();
1290 M != MEnd && !Done; ++M) {
1291 for (OverridingMethods::iterator SO = M->second.begin(),
1292 SOEnd = M->second.end();
1293 SO != SOEnd && !Done; ++SO) {
1294 assert(SO->second.size() > 0 &&
1295 "All virtual functions have overridding virtual functions");
1296
1297 // C++ [class.abstract]p4:
1298 // A class is abstract if it contains or inherits at least one
1299 // pure virtual function for which the final overrider is pure
1300 // virtual.
1301 if (SO->second.front().Method->isPure()) {
1302 data().Abstract = true;
1303 Done = true;
1304 break;
1305 }
1306 }
1307 }
1308 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001309
1310 // Set access bits correctly on the directly-declared conversions.
1311 for (UnresolvedSetIterator I = data().Conversions.begin(),
1312 E = data().Conversions.end();
1313 I != E; ++I)
1314 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001315}
1316
1317bool CXXRecordDecl::mayBeAbstract() const {
1318 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1319 isDependentContext())
1320 return false;
1321
1322 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1323 BEnd = bases_end();
1324 B != BEnd; ++B) {
1325 CXXRecordDecl *BaseDecl
1326 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1327 if (BaseDecl->isAbstract())
1328 return true;
1329 }
1330
1331 return false;
1332}
1333
David Blaikie99ba9e32011-12-20 02:48:34 +00001334void CXXMethodDecl::anchor() { }
1335
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001336CXXMethodDecl *
1337CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001338 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001339 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001340 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001341 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001342 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001343 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001344 isStatic, SCAsWritten, isInline, isConstexpr,
1345 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001346}
1347
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001348CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1349 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1350 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1351 DeclarationNameInfo(), QualType(),
1352 0, false, SC_None, false, false,
1353 SourceLocation());
1354}
1355
Douglas Gregor90916562009-09-29 18:16:17 +00001356bool CXXMethodDecl::isUsualDeallocationFunction() const {
1357 if (getOverloadedOperator() != OO_Delete &&
1358 getOverloadedOperator() != OO_Array_Delete)
1359 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001360
1361 // C++ [basic.stc.dynamic.deallocation]p2:
1362 // A template instance is never a usual deallocation function,
1363 // regardless of its signature.
1364 if (getPrimaryTemplate())
1365 return false;
1366
Douglas Gregor90916562009-09-29 18:16:17 +00001367 // C++ [basic.stc.dynamic.deallocation]p2:
1368 // If a class T has a member deallocation function named operator delete
1369 // with exactly one parameter, then that function is a usual (non-placement)
1370 // deallocation function. [...]
1371 if (getNumParams() == 1)
1372 return true;
1373
1374 // C++ [basic.stc.dynamic.deallocation]p2:
1375 // [...] If class T does not declare such an operator delete but does
1376 // declare a member deallocation function named operator delete with
1377 // exactly two parameters, the second of which has type std::size_t (18.1),
1378 // then this function is a usual deallocation function.
1379 ASTContext &Context = getASTContext();
1380 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001381 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1382 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001383 return false;
1384
1385 // This function is a usual deallocation function if there are no
1386 // single-parameter deallocation functions of the same kind.
1387 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1388 R.first != R.second; ++R.first) {
1389 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1390 if (FD->getNumParams() == 1)
1391 return false;
1392 }
1393
1394 return true;
1395}
1396
Douglas Gregor06a9f362010-05-01 20:49:11 +00001397bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001398 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001399 // A user-declared copy assignment operator X::operator= is a non-static
1400 // non-template member function of class X with exactly one parameter of
1401 // type X, X&, const X&, volatile X& or const volatile X&.
1402 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1403 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001404 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001405 return false;
1406
1407 QualType ParamType = getParamDecl(0)->getType();
1408 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1409 ParamType = Ref->getPointeeType();
1410
1411 ASTContext &Context = getASTContext();
1412 QualType ClassType
1413 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1414 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1415}
1416
Sean Huntffe37fd2011-05-25 20:50:04 +00001417bool CXXMethodDecl::isMoveAssignmentOperator() const {
1418 // C++0x [class.copy]p19:
1419 // A user-declared move assignment operator X::operator= is a non-static
1420 // non-template member function of class X with exactly one parameter of type
1421 // X&&, const X&&, volatile X&&, or const volatile X&&.
1422 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1423 getPrimaryTemplate() || getDescribedFunctionTemplate())
1424 return false;
1425
1426 QualType ParamType = getParamDecl(0)->getType();
1427 if (!isa<RValueReferenceType>(ParamType))
1428 return false;
1429 ParamType = ParamType->getPointeeType();
1430
1431 ASTContext &Context = getASTContext();
1432 QualType ClassType
1433 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1434 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1435}
1436
Anders Carlsson05eb2442009-05-16 23:58:37 +00001437void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001438 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001439 assert(!MD->getParent()->isDependentContext() &&
1440 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001441 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001442
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001443 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001444}
1445
1446CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001447 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001448 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001449}
1450
1451CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001452 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001453 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001454}
1455
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001456unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001457 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001458 return getASTContext().overridden_methods_size(this);
1459}
1460
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001461QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001462 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1463 // If the member function is declared const, the type of this is const X*,
1464 // if the member function is declared volatile, the type of this is
1465 // volatile X*, and if the member function is declared const volatile,
1466 // the type of this is const volatile X*.
1467
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001468 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001469
John McCall3cb0ebd2010-03-10 03:28:59 +00001470 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001471 ClassTy = C.getQualifiedType(ClassTy,
1472 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001473 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001474}
1475
Eli Friedmand7d7f672009-12-06 20:50:05 +00001476bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001477 // If this function is a template instantiation, look at the template from
1478 // which it was instantiated.
1479 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1480 if (!CheckFn)
1481 CheckFn = this;
1482
Eli Friedmand7d7f672009-12-06 20:50:05 +00001483 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001484 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001485}
1486
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001487bool CXXMethodDecl::isLambdaStaticInvoker() const {
1488 return getParent()->isLambda() &&
1489 getIdentifier() && getIdentifier()->getName() == "__invoke";
1490}
1491
1492
Sean Huntcbb67482011-01-08 20:30:50 +00001493CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1494 TypeSourceInfo *TInfo, bool IsVirtual,
1495 SourceLocation L, Expr *Init,
1496 SourceLocation R,
1497 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001498 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001499 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1500 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001501{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001502}
1503
Sean Huntcbb67482011-01-08 20:30:50 +00001504CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1505 FieldDecl *Member,
1506 SourceLocation MemberLoc,
1507 SourceLocation L, Expr *Init,
1508 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001509 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001510 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001511 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1512{
1513}
1514
Sean Huntcbb67482011-01-08 20:30:50 +00001515CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1516 IndirectFieldDecl *Member,
1517 SourceLocation MemberLoc,
1518 SourceLocation L, Expr *Init,
1519 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001520 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001521 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001522 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001523{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001524}
1525
Sean Huntcbb67482011-01-08 20:30:50 +00001526CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001527 TypeSourceInfo *TInfo,
1528 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001529 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001530 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1531 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001532 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1533{
1534}
1535
1536CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001537 FieldDecl *Member,
1538 SourceLocation MemberLoc,
1539 SourceLocation L, Expr *Init,
1540 SourceLocation R,
1541 VarDecl **Indices,
1542 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001543 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001544 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001545 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001546{
1547 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1548 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1549}
1550
Sean Huntcbb67482011-01-08 20:30:50 +00001551CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1552 FieldDecl *Member,
1553 SourceLocation MemberLoc,
1554 SourceLocation L, Expr *Init,
1555 SourceLocation R,
1556 VarDecl **Indices,
1557 unsigned NumIndices) {
1558 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001559 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001560 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001561 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1562 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001563}
1564
Sean Huntcbb67482011-01-08 20:30:50 +00001565TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001566 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001567 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001568 else
1569 return TypeLoc();
1570}
1571
Sean Huntcbb67482011-01-08 20:30:50 +00001572const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001573 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001574 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001575 else
1576 return 0;
1577}
1578
Sean Huntcbb67482011-01-08 20:30:50 +00001579SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001580 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001581 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001582
1583 if (isInClassMemberInitializer())
1584 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001585
Douglas Gregor76852c22011-11-01 01:16:03 +00001586 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1587 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1588
1589 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001590}
1591
Sean Huntcbb67482011-01-08 20:30:50 +00001592SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001593 if (isInClassMemberInitializer()) {
1594 FieldDecl *D = getAnyMember();
1595 if (Expr *I = D->getInClassInitializer())
1596 return I->getSourceRange();
1597 return SourceRange();
1598 }
1599
Douglas Gregor802ab452009-12-02 22:36:29 +00001600 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001601}
1602
David Blaikie99ba9e32011-12-20 02:48:34 +00001603void CXXConstructorDecl::anchor() { }
1604
Douglas Gregorb48fe382008-10-31 09:07:45 +00001605CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001606CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1607 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1608 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1609 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001610}
1611
1612CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001613CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001614 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001615 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001616 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001617 bool isExplicit, bool isInline,
1618 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001619 assert(NameInfo.getName().getNameKind()
1620 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001621 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001622 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001623 isExplicit, isInline, isImplicitlyDeclared,
1624 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001625}
1626
Douglas Gregor76852c22011-11-01 01:16:03 +00001627CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1628 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1629 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1630 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1631 return Construct->getConstructor();
1632
1633 return 0;
1634}
1635
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001636bool CXXConstructorDecl::isDefaultConstructor() const {
1637 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001638 // A default constructor for a class X is a constructor of class
1639 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001640 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001641 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001642}
1643
Mike Stump1eb44332009-09-09 15:08:12 +00001644bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001645CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001646 return isCopyOrMoveConstructor(TypeQuals) &&
1647 getParamDecl(0)->getType()->isLValueReferenceType();
1648}
1649
1650bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1651 return isCopyOrMoveConstructor(TypeQuals) &&
1652 getParamDecl(0)->getType()->isRValueReferenceType();
1653}
1654
1655/// \brief Determine whether this is a copy or move constructor.
1656bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001657 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001658 // A non-template constructor for class X is a copy constructor
1659 // if its first parameter is of type X&, const X&, volatile X& or
1660 // const volatile X&, and either there are no other parameters
1661 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001662 // C++0x [class.copy]p3:
1663 // A non-template constructor for class X is a move constructor if its
1664 // first parameter is of type X&&, const X&&, volatile X&&, or
1665 // const volatile X&&, and either there are no other parameters or else
1666 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001667 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001668 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001669 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001670 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001671 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001672
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001673 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001674
1675 // Do we have a reference type?
1676 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001677 if (!ParamRefType)
1678 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001679
Douglas Gregorfd476482009-11-13 23:59:09 +00001680 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001681 ASTContext &Context = getASTContext();
1682
Douglas Gregorfd476482009-11-13 23:59:09 +00001683 CanQualType PointeeType
1684 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001685 CanQualType ClassTy
1686 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001687 if (PointeeType.getUnqualifiedType() != ClassTy)
1688 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001689
John McCall0953e762009-09-24 19:53:00 +00001690 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001691
1692 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001693 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001694 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001695}
1696
Anders Carlssonfaccd722009-08-28 16:57:08 +00001697bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001698 // C++ [class.conv.ctor]p1:
1699 // A constructor declared without the function-specifier explicit
1700 // that can be called with a single parameter specifies a
1701 // conversion from the type of its first parameter to the type of
1702 // its class. Such a constructor is called a converting
1703 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001704 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001705 return false;
1706
Mike Stump1eb44332009-09-09 15:08:12 +00001707 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001708 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001709 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001710 (getNumParams() > 1 &&
1711 (getParamDecl(1)->hasDefaultArg() ||
1712 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001713}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001714
Douglas Gregor6493cc52010-11-08 17:16:59 +00001715bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001716 if ((getNumParams() < 1) ||
1717 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1718 (getPrimaryTemplate() == 0) ||
1719 (getDescribedFunctionTemplate() != 0))
1720 return false;
1721
1722 const ParmVarDecl *Param = getParamDecl(0);
1723
1724 ASTContext &Context = getASTContext();
1725 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1726
Douglas Gregor66724ea2009-11-14 01:20:54 +00001727 // Is it the same as our our class type?
1728 CanQualType ClassTy
1729 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1730 if (ParamType.getUnqualifiedType() != ClassTy)
1731 return false;
1732
1733 return true;
1734}
1735
Sebastian Redlf677ea32011-02-05 19:23:19 +00001736const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1737 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001738 method_iterator It = getASTContext().overridden_methods_begin(this);
1739 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001740 return 0;
1741
1742 return cast<CXXConstructorDecl>(*It);
1743}
1744
1745void
1746CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1747 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001748 assert(getASTContext().overridden_methods_size(this) == 0 &&
1749 "Base ctor already set.");
1750 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001751}
1752
David Blaikie99ba9e32011-12-20 02:48:34 +00001753void CXXDestructorDecl::anchor() { }
1754
Douglas Gregor42a552f2008-11-05 20:51:48 +00001755CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001756CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1757 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1758 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001759 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001760}
1761
1762CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001763CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001764 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001765 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001766 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001767 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001768 assert(NameInfo.getName().getNameKind()
1769 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001770 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001771 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001772 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001773}
1774
David Blaikie99ba9e32011-12-20 02:48:34 +00001775void CXXConversionDecl::anchor() { }
1776
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001777CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001778CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1779 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1780 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1781 QualType(), 0, false, false, false,
1782 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001783}
1784
1785CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001786CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001787 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001788 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001789 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001790 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001791 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001792 assert(NameInfo.getName().getNameKind()
1793 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001794 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001795 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001796 isInline, isExplicit, isConstexpr,
1797 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001798}
1799
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001800bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1801 return isImplicit() && getParent()->isLambda() &&
1802 getConversionType()->isBlockPointerType();
1803}
1804
David Blaikie99ba9e32011-12-20 02:48:34 +00001805void LinkageSpecDecl::anchor() { }
1806
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001807LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001808 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001809 SourceLocation ExternLoc,
1810 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001811 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001812 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001813 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001814}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001815
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001816LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1817 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1818 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1819 lang_c, SourceLocation());
1820}
1821
David Blaikie99ba9e32011-12-20 02:48:34 +00001822void UsingDirectiveDecl::anchor() { }
1823
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001824UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1825 SourceLocation L,
1826 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001827 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001828 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001829 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001830 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001831 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1832 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001833 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1834 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001835}
1836
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001837UsingDirectiveDecl *
1838UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1839 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1840 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1841 NestedNameSpecifierLoc(),
1842 SourceLocation(), 0, 0);
1843}
1844
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001845NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1846 if (NamespaceAliasDecl *NA =
1847 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1848 return NA->getNamespace();
1849 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1850}
1851
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001852void NamespaceDecl::anchor() { }
1853
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001854NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1855 SourceLocation StartLoc,
1856 SourceLocation IdLoc, IdentifierInfo *Id,
1857 NamespaceDecl *PrevDecl)
1858 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1859 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1860{
1861 setPreviousDeclaration(PrevDecl);
1862
1863 if (PrevDecl)
1864 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1865}
1866
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001867NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001868 bool Inline, SourceLocation StartLoc,
1869 SourceLocation IdLoc, IdentifierInfo *Id,
1870 NamespaceDecl *PrevDecl) {
1871 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001872}
1873
1874NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1875 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001876 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1877 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001878}
1879
David Blaikie99ba9e32011-12-20 02:48:34 +00001880void NamespaceAliasDecl::anchor() { }
1881
Mike Stump1eb44332009-09-09 15:08:12 +00001882NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001883 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001884 SourceLocation AliasLoc,
1885 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001886 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001887 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001888 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001889 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1890 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001891 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1892 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001893}
1894
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001895NamespaceAliasDecl *
1896NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1897 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1898 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1899 NestedNameSpecifierLoc(),
1900 SourceLocation(), 0);
1901}
1902
David Blaikie99ba9e32011-12-20 02:48:34 +00001903void UsingShadowDecl::anchor() { }
1904
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001905UsingShadowDecl *
1906UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1907 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1908 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1909}
1910
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001911UsingDecl *UsingShadowDecl::getUsingDecl() const {
1912 const UsingShadowDecl *Shadow = this;
1913 while (const UsingShadowDecl *NextShadow =
1914 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1915 Shadow = NextShadow;
1916 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1917}
1918
David Blaikie99ba9e32011-12-20 02:48:34 +00001919void UsingDecl::anchor() { }
1920
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001921void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1922 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1923 "declaration already in set");
1924 assert(S->getUsingDecl() == this);
1925
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001926 if (FirstUsingShadow.getPointer())
1927 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1928 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001929}
1930
1931void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1932 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1933 "declaration not in set");
1934 assert(S->getUsingDecl() == this);
1935
1936 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1937
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001938 if (FirstUsingShadow.getPointer() == S) {
1939 FirstUsingShadow.setPointer(
1940 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001941 S->UsingOrNextShadow = this;
1942 return;
1943 }
1944
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001945 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001946 while (Prev->UsingOrNextShadow != S)
1947 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1948 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1949 S->UsingOrNextShadow = this;
1950}
1951
Douglas Gregordc355712011-02-25 00:36:19 +00001952UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1953 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001954 const DeclarationNameInfo &NameInfo,
1955 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001956 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001957}
1958
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001959UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1960 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1961 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1962 DeclarationNameInfo(), false);
1963}
1964
David Blaikie99ba9e32011-12-20 02:48:34 +00001965void UnresolvedUsingValueDecl::anchor() { }
1966
John McCall7ba107a2009-11-18 02:36:19 +00001967UnresolvedUsingValueDecl *
1968UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1969 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001970 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001971 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001972 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001973 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001974}
1975
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001976UnresolvedUsingValueDecl *
1977UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1978 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1979 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1980 NestedNameSpecifierLoc(),
1981 DeclarationNameInfo());
1982}
1983
David Blaikie99ba9e32011-12-20 02:48:34 +00001984void UnresolvedUsingTypenameDecl::anchor() { }
1985
John McCall7ba107a2009-11-18 02:36:19 +00001986UnresolvedUsingTypenameDecl *
1987UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1988 SourceLocation UsingLoc,
1989 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001990 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001991 SourceLocation TargetNameLoc,
1992 DeclarationName TargetName) {
1993 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001994 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001995 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001996}
1997
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001998UnresolvedUsingTypenameDecl *
1999UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2000 void *Mem = AllocateDeserializedDecl(C, ID,
2001 sizeof(UnresolvedUsingTypenameDecl));
2002 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2003 SourceLocation(),
2004 NestedNameSpecifierLoc(),
2005 SourceLocation(),
2006 0);
2007}
2008
David Blaikie99ba9e32011-12-20 02:48:34 +00002009void StaticAssertDecl::anchor() { }
2010
Anders Carlssonfb311762009-03-14 00:25:26 +00002011StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002012 SourceLocation StaticAssertLoc,
2013 Expr *AssertExpr,
2014 StringLiteral *Message,
2015 SourceLocation RParenLoc) {
2016 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2017 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00002018}
2019
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002020StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2021 unsigned ID) {
2022 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
2023 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,SourceLocation());
2024}
2025
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002026static const char *getAccessName(AccessSpecifier AS) {
2027 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002028 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002029 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002030 case AS_public:
2031 return "public";
2032 case AS_private:
2033 return "private";
2034 case AS_protected:
2035 return "protected";
2036 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002037 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002038}
2039
2040const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2041 AccessSpecifier AS) {
2042 return DB << getAccessName(AS);
2043}
Richard Smithf15fda02012-02-02 01:16:57 +00002044
2045const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2046 AccessSpecifier AS) {
2047 return DB << getAccessName(AS);
2048}