blob: b09b6720955322dcfe4568ac29c7d04ab1b805b8 [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
38
John McCall86ff3082010-02-04 22:26:26 +000039CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
40 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000041 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
42 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000043 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000044 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000045 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Douglas Gregor2bb11012011-05-13 01:05:07 +000046 HasMutableFields(false), HasTrivialDefaultConstructor(true),
Richard Smith61802452011-12-22 02:22:31 +000047 HasConstexprNonCopyMoveConstructor(false),
48 DefaultedDefaultConstructorIsConstexpr(true),
49 DefaultedCopyConstructorIsConstexpr(true),
50 DefaultedMoveConstructorIsConstexpr(true),
51 HasConstexprDefaultConstructor(false), HasConstexprCopyConstructor(false),
52 HasConstexprMoveConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000053 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
54 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
55 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000056 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000057 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
58 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sebastian Redl85ea7aa2011-08-30 19:58:05 +000059 DeclaredDestructor(false), FailedImplicitMoveConstructor(false),
Eli Friedman72899c32012-01-07 04:59:52 +000060 FailedImplicitMoveAssignment(false), IsLambda(false), NumBases(0),
61 NumVBases(0), Bases(), VBases(), Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000062}
63
64CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000065 SourceLocation StartLoc, SourceLocation IdLoc,
66 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
67 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000068 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000069 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000070
Jay Foad4ba2a172011-01-12 09:06:06 +000071CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000072 DeclContext *DC, SourceLocation StartLoc,
73 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000074 CXXRecordDecl* PrevDecl,
75 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000076 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
77 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000078
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000079 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000080 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000081 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000082 return R;
83}
84
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000085CXXRecordDecl *
86CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
87 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
88 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
89 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000090}
91
Mike Stump1eb44332009-09-09 15:08:12 +000092void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000093CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000094 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000095 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +000096
Douglas Gregor7c789c12010-10-29 22:39:52 +000097 if (!data().Bases.isOffset() && data().NumBases > 0)
98 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000099
Richard Smithdd677232011-10-18 20:08:55 +0000100 if (NumBases) {
101 // C++ [dcl.init.aggr]p1:
102 // An aggregate is [...] a class with [...] no base classes [...].
103 data().Aggregate = false;
104
105 // C++ [class]p4:
106 // A POD-struct is an aggregate class...
107 data().PlainOldData = false;
108 }
109
Anders Carlsson6f6de732010-03-29 05:13:12 +0000110 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000111 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000112
113 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000114 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000115
John McCall86ff3082010-02-04 22:26:26 +0000116 data().Bases = new(C) CXXBaseSpecifier [NumBases];
117 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000118 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000119 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000120 // Keep track of inherited vbases for this base class.
121 const CXXBaseSpecifier *Base = Bases[i];
122 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000123 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000124 if (BaseType->isDependentType())
125 continue;
126 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000127 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000128
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000129 // A class with a non-empty base class is not empty.
130 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000131 if (!BaseClassDecl->isEmpty()) {
132 if (!data().Empty) {
133 // C++0x [class]p7:
134 // A standard-layout class is a class that:
135 // [...]
136 // -- either has no non-static data members in the most derived
137 // class and at most one base class with non-static data members,
138 // or has no base classes with non-static data members, and
139 // If this is the second non-empty base, then neither of these two
140 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000141 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000142 }
143
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000144 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000145 data().HasNoNonEmptyBases = false;
146 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000147
Douglas Gregor85606eb2010-09-28 20:50:54 +0000148 // C++ [class.virtual]p1:
149 // A class that declares or inherits a virtual function is called a
150 // polymorphic class.
151 if (BaseClassDecl->isPolymorphic())
152 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000153
Chandler Carrutha8225442011-04-30 09:17:45 +0000154 // C++0x [class]p7:
155 // A standard-layout class is a class that: [...]
156 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000157 if (!BaseClassDecl->isStandardLayout())
158 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000159
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000160 // Record if this base is the first non-literal field or base.
161 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
162 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000163
Anders Carlsson6f6de732010-03-29 05:13:12 +0000164 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000165 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000166 BaseClassDecl->vbases_begin(),
167 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000168 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000169 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000170 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000171 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000172
173 if (Base->isVirtual()) {
174 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000175 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000176 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000177
178 // C++0x [meta.unary.prop] is_empty:
179 // T is a class type, but not a union type, with ... no virtual base
180 // classes
181 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000182
183 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000184 // A default constructor is trivial [...] if:
185 // -- its class has [...] no virtual bases
186 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000187
188 // C++0x [class.copy]p13:
189 // A copy/move constructor for class X is trivial if it is neither
190 // user-provided nor deleted and if
191 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000192 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000193 data().HasTrivialMoveConstructor = false;
194
195 // C++0x [class.copy]p27:
196 // A copy/move assignment operator for class X is trivial if it is
197 // neither user-provided nor deleted and if
198 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000199 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000200 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000201
202 // C++0x [class]p7:
203 // A standard-layout class is a class that: [...]
204 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000205 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000206
207 // C++11 [dcl.constexpr]p4:
208 // In the definition of a constexpr constructor [...]
209 // -- the class shall not have any virtual base classes
210 data().DefaultedDefaultConstructorIsConstexpr = false;
211 data().DefaultedCopyConstructorIsConstexpr = false;
212 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000213 } else {
214 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000215 // A default constructor is trivial [...] if:
216 // -- all the direct base classes of its class have trivial default
217 // constructors.
218 if (!BaseClassDecl->hasTrivialDefaultConstructor())
219 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000220
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000221 // C++0x [class.copy]p13:
222 // A copy/move constructor for class X is trivial if [...]
223 // [...]
224 // -- the constructor selected to copy/move each direct base class
225 // subobject is trivial, and
226 // FIXME: C++0x: We need to only consider the selected constructor
227 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000228 if (!BaseClassDecl->hasTrivialCopyConstructor())
229 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000230 if (!BaseClassDecl->hasTrivialMoveConstructor())
231 data().HasTrivialMoveConstructor = false;
232
233 // C++0x [class.copy]p27:
234 // A copy/move assignment operator for class X is trivial if [...]
235 // [...]
236 // -- the assignment operator selected to copy/move each direct base
237 // class subobject is trivial, and
238 // FIXME: C++0x: We need to only consider the selected operator instead
239 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000240 if (!BaseClassDecl->hasTrivialCopyAssignment())
241 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000242 if (!BaseClassDecl->hasTrivialMoveAssignment())
243 data().HasTrivialMoveAssignment = false;
Richard Smith61802452011-12-22 02:22:31 +0000244
245 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000246 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000247 // requirements of a constexpr constructor, the implicitly-defined
248 // default constructor is constexpr.
249 if (!BaseClassDecl->hasConstexprDefaultConstructor())
250 data().DefaultedDefaultConstructorIsConstexpr = false;
251
252 // C++11 [class.copy]p13:
253 // If the implicitly-defined constructor would satisfy the requirements
254 // of a constexpr constructor, the implicitly-defined constructor is
255 // constexpr.
256 // C++11 [dcl.constexpr]p4:
257 // -- every constructor involved in initializing [...] base class
258 // sub-objects shall be a constexpr constructor
259 if (!BaseClassDecl->hasConstexprCopyConstructor())
260 data().DefaultedCopyConstructorIsConstexpr = false;
261 if (BaseClassDecl->hasDeclaredMoveConstructor() ||
262 BaseClassDecl->needsImplicitMoveConstructor())
263 // FIXME: If the implicit move constructor generated for the base class
264 // would be ill-formed, the implicit move constructor generated for the
265 // derived class calls the base class' copy constructor.
266 data().DefaultedMoveConstructorIsConstexpr &=
Richard Smithde8facc2012-01-11 18:26:05 +0000267 BaseClassDecl->hasConstexprMoveConstructor();
Richard Smith61802452011-12-22 02:22:31 +0000268 else if (!BaseClassDecl->hasConstexprCopyConstructor())
269 data().DefaultedMoveConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000270 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000271
272 // C++ [class.ctor]p3:
273 // A destructor is trivial if all the direct base classes of its class
274 // have trivial destructors.
275 if (!BaseClassDecl->hasTrivialDestructor())
276 data().HasTrivialDestructor = false;
Douglas Gregor2bb11012011-05-13 01:05:07 +0000277
John McCallf85e1932011-06-15 23:02:42 +0000278 // A class has an Objective-C object member if... or any of its bases
279 // has an Objective-C object member.
280 if (BaseClassDecl->hasObjectMember())
281 setHasObjectMember(true);
282
Douglas Gregor2bb11012011-05-13 01:05:07 +0000283 // Keep track of the presence of mutable fields.
284 if (BaseClassDecl->hasMutableFields())
285 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000286 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000287
288 if (VBases.empty())
289 return;
290
291 // Create base specifier for any direct or indirect virtual bases.
292 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
293 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000294 for (int I = 0, E = VBases.size(); I != E; ++I)
295 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000296}
297
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000298/// Callback function for CXXRecordDecl::forallBases that acknowledges
299/// that it saw a base class.
300static bool SawBase(const CXXRecordDecl *, void *) {
301 return true;
302}
303
304bool CXXRecordDecl::hasAnyDependentBases() const {
305 if (!isDependentContext())
306 return false;
307
308 return !forallBases(SawBase, 0);
309}
310
Sean Huntffe37fd2011-05-25 20:50:04 +0000311bool CXXRecordDecl::hasConstCopyConstructor() const {
312 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000313}
314
Chandler Carruthb7e95892011-04-23 10:47:28 +0000315bool CXXRecordDecl::isTriviallyCopyable() const {
316 // C++0x [class]p5:
317 // A trivially copyable class is a class that:
318 // -- has no non-trivial copy constructors,
319 if (!hasTrivialCopyConstructor()) return false;
320 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000321 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000322 // -- has no non-trivial copy assignment operators,
323 if (!hasTrivialCopyAssignment()) return false;
324 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000325 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000326 // -- has a trivial destructor.
327 if (!hasTrivialDestructor()) return false;
328
329 return true;
330}
331
Douglas Gregor0d405db2010-07-01 20:59:04 +0000332/// \brief Perform a simplistic form of overload resolution that only considers
333/// cv-qualifiers on a single parameter, and return the best overload candidate
334/// (if there is one).
335static CXXMethodDecl *
336GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000337 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000338 if (Cands.empty())
339 return 0;
340 if (Cands.size() == 1)
341 return Cands[0].first;
342
343 unsigned Best = 0, N = Cands.size();
344 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000345 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000346 Best = I;
347
348 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000349 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000350 return 0;
351
352 return Cands[Best].first;
353}
354
Sean Huntffe37fd2011-05-25 20:50:04 +0000355CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
356 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000357 QualType ClassType
358 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000359 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000360 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000361 Context.getCanonicalType(ClassType));
362 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000363 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000364 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000365 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000366 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000367 // C++ [class.copy]p2:
368 // A non-template constructor for class X is a copy constructor if [...]
369 if (isa<FunctionTemplateDecl>(*Con))
370 continue;
371
Douglas Gregor0d405db2010-07-01 20:59:04 +0000372 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
373 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000374 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
375 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000376 Found.push_back(std::make_pair(
377 const_cast<CXXConstructorDecl *>(Constructor),
378 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000379 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000380 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000381
382 return cast_or_null<CXXConstructorDecl>(
383 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000384}
385
Sean Huntffe37fd2011-05-25 20:50:04 +0000386CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
387 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
388 if (I->isMoveConstructor())
389 return *I;
390
391 return 0;
392}
393
Douglas Gregorb87786f2010-07-01 17:48:08 +0000394CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
395 ASTContext &Context = getASTContext();
396 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
397 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
398
Chris Lattner5f9e2722011-07-23 10:55:15 +0000399 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000400 DeclContext::lookup_const_iterator Op, OpEnd;
401 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
402 // C++ [class.copy]p9:
403 // A user-declared copy assignment operator is a non-static non-template
404 // member function of class X with exactly one parameter of type X, X&,
405 // const X&, volatile X& or const volatile X&.
406 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
407 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
408 continue;
409
410 const FunctionProtoType *FnType
411 = Method->getType()->getAs<FunctionProtoType>();
412 assert(FnType && "Overloaded operator has no prototype.");
413 // Don't assert on this; an invalid decl might have been left in the AST.
414 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
415 continue;
416
417 QualType ArgType = FnType->getArgType(0);
418 Qualifiers Quals;
419 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
420 ArgType = Ref->getPointeeType();
421 // If we have a const argument and we have a reference to a non-const,
422 // this function does not match.
423 if (ArgIsConst && !ArgType.isConstQualified())
424 continue;
425
426 Quals = ArgType.getQualifiers();
427 } else {
428 // By-value copy-assignment operators are treated like const X&
429 // copy-assignment operators.
430 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
431 }
432
433 if (!Context.hasSameUnqualifiedType(ArgType, Class))
434 continue;
435
436 // Save this copy-assignment operator. It might be "the one".
437 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
438 }
439
440 // Use a simplistic form of overload resolution to find the candidate.
441 return GetBestOverloadCandidateSimple(Found);
442}
443
Sean Huntffe37fd2011-05-25 20:50:04 +0000444CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
445 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
446 if (I->isMoveAssignmentOperator())
447 return *I;
448
449 return 0;
450}
451
Douglas Gregor21386642010-09-28 21:55:22 +0000452void CXXRecordDecl::markedVirtualFunctionPure() {
453 // C++ [class.abstract]p2:
454 // A class is abstract if it has at least one pure virtual function.
455 data().Abstract = true;
456}
457
458void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000459 // Ignore friends and invalid declarations.
460 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000461 return;
462
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000463 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
464 if (FunTmpl)
465 D = FunTmpl->getTemplatedDecl();
466
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000467 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
468 if (Method->isVirtual()) {
469 // C++ [dcl.init.aggr]p1:
470 // An aggregate is an array or a class with [...] no virtual functions.
471 data().Aggregate = false;
472
473 // C++ [class]p4:
474 // A POD-struct is an aggregate class...
475 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000476
477 // Virtual functions make the class non-empty.
478 // FIXME: Standard ref?
479 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000480
481 // C++ [class.virtual]p1:
482 // A class that declares or inherits a virtual function is called a
483 // polymorphic class.
484 data().Polymorphic = true;
485
Sean Hunt023df372011-05-09 18:22:59 +0000486 // C++0x [class.ctor]p5
487 // A default constructor is trivial [...] if:
488 // -- its class has no virtual functions [...]
489 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000490
491 // C++0x [class.copy]p13:
492 // A copy/move constructor for class X is trivial if [...]
493 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000494 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000495 data().HasTrivialMoveConstructor = false;
496
497 // C++0x [class.copy]p27:
498 // A copy/move assignment operator for class X is trivial if [...]
499 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000500 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000501 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000502
Chandler Carrutha8225442011-04-30 09:17:45 +0000503 // C++0x [class]p7:
504 // A standard-layout class is a class that: [...]
505 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000506 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000507 }
508 }
509
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000510 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000511 // Notify that an implicit member was added after the definition
512 // was completed.
513 if (!isBeingDefined())
514 if (ASTMutationListener *L = getASTMutationListener())
515 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000516
Sean Huntffe37fd2011-05-25 20:50:04 +0000517 // If this is a special member function, note that it was added and then
518 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000519 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith61802452011-12-22 02:22:31 +0000520 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000521 data().DeclaredDefaultConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000522 if (Constructor->isConstexpr()) {
523 data().HasConstexprDefaultConstructor = true;
524 data().HasConstexprNonCopyMoveConstructor = true;
525 }
526 } else if (Constructor->isCopyConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000527 data().DeclaredCopyConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000528 if (Constructor->isConstexpr())
529 data().HasConstexprCopyConstructor = true;
530 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000531 data().DeclaredMoveConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000532 if (Constructor->isConstexpr())
533 data().HasConstexprMoveConstructor = true;
534 } else
Sean Huntffe37fd2011-05-25 20:50:04 +0000535 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000536 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000537 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000538 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000539 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000540 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
541 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000542 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000543 else if (Method->isMoveAssignmentOperator())
544 data().DeclaredMoveAssignment = true;
545 else
546 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000547 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000548 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000549
Sean Huntffe37fd2011-05-25 20:50:04 +0000550NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000551 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000552 }
553
554 // Handle (user-declared) constructors.
555 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
556 // Note that we have a user-declared constructor.
557 data().UserDeclaredConstructor = true;
558
Richard Smith017ab772011-09-05 02:13:09 +0000559 // Technically, "user-provided" is only defined for special member
560 // functions, but the intent of the standard is clearly that it should apply
561 // to all functions.
562 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000563
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000564 if (Constructor->isDefaultConstructor()) {
565 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000566 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000567 // C++0x [class.ctor]p5:
568 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000569 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000570 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000571 }
Richard Smith61802452011-12-22 02:22:31 +0000572 if (Constructor->isConstexpr()) {
573 data().HasConstexprDefaultConstructor = true;
574 data().HasConstexprNonCopyMoveConstructor = true;
575 }
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000576 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000577
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000578 // Note when we have a user-declared copy or move constructor, which will
579 // suppress the implicit declaration of those constructors.
580 if (!FunTmpl) {
581 if (Constructor->isCopyConstructor()) {
582 data().UserDeclaredCopyConstructor = true;
583 data().DeclaredCopyConstructor = true;
584
585 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000586 // A copy/move constructor for class X is trivial if it is not
587 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000588 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000589 data().HasTrivialCopyConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000590
591 if (Constructor->isConstexpr())
592 data().HasConstexprCopyConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000593 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000594 data().UserDeclaredMoveConstructor = true;
595 data().DeclaredMoveConstructor = true;
596
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000597 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000598 // A copy/move constructor for class X is trivial if it is not
599 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000600 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000601 data().HasTrivialMoveConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000602
603 if (Constructor->isConstexpr())
604 data().HasConstexprMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000605 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000606 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000607 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
608 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000609 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000610 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000611 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000612
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000613 // C++ [dcl.init.aggr]p1:
614 // An aggregate is an array or a class with no user-declared
615 // constructors [...].
616 // C++0x [dcl.init.aggr]p1:
617 // An aggregate is an array or a class with no user-provided
618 // constructors [...].
619 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
620 data().Aggregate = false;
621
622 // C++ [class]p4:
623 // A POD-struct is an aggregate class [...]
624 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
625 // type is technically an aggregate in C++0x since it wouldn't be in 03.
626 data().PlainOldData = false;
627
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000628 return;
629 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000630
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000631 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000632 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000633 data().DeclaredDestructor = true;
634 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000635
636 // C++ [class]p4:
637 // A POD-struct is an aggregate class that has [...] no user-defined
638 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000639 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000640 data().PlainOldData = false;
641
Douglas Gregor45fa5602011-11-07 20:56:01 +0000642 // C++11 [class.dtor]p5:
643 // A destructor is trivial if it is not user-provided and if
644 // -- the destructor is not virtual.
Richard Smith61802452011-12-22 02:22:31 +0000645 if (DD->isUserProvided() || DD->isVirtual()) {
Sean Huntcf34e752011-05-16 22:41:40 +0000646 data().HasTrivialDestructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000647 // C++11 [dcl.constexpr]p1:
648 // The constexpr specifier shall be applied only to [...] the
649 // declaration of a static data member of a literal type.
650 // C++11 [basic.types]p10:
651 // A type is a literal type if it is [...] a class type that [...] has
652 // a trivial destructor.
653 data().DefaultedDefaultConstructorIsConstexpr = false;
654 data().DefaultedCopyConstructorIsConstexpr = false;
655 data().DefaultedMoveConstructorIsConstexpr = false;
656 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000657
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000658 return;
659 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000660
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000661 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000662 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000663 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000664 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000665 // A POD-struct is an aggregate class that [...] has no user-defined
666 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000667 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000668 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000669
Sean Huntffe37fd2011-05-25 20:50:04 +0000670 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000671
Sean Huntffe37fd2011-05-25 20:50:04 +0000672 // Suppress the implicit declaration of a copy constructor.
673 data().UserDeclaredCopyAssignment = true;
674 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000675
Sean Huntffe37fd2011-05-25 20:50:04 +0000676 // C++0x [class.copy]p27:
677 // A copy/move assignment operator for class X is trivial if it is
678 // neither user-provided nor deleted [...]
679 if (Method->isUserProvided())
680 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000681
Sean Huntffe37fd2011-05-25 20:50:04 +0000682 return;
683 }
684
685 if (Method->isMoveAssignmentOperator()) {
686 // This is an extension in C++03 mode, but we'll keep consistency by
687 // taking a move assignment operator to induce non-POD-ness
688 data().PlainOldData = false;
689
690 // This is a move assignment operator.
691 data().UserDeclaredMoveAssignment = true;
692 data().DeclaredMoveAssignment = true;
693
694 // C++0x [class.copy]p27:
695 // A copy/move assignment operator for class X is trivial if it is
696 // neither user-provided nor deleted [...]
697 if (Method->isUserProvided())
698 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000699 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000700
Douglas Gregore80622f2010-09-29 04:25:11 +0000701 // Keep the list of conversion functions up-to-date.
702 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
703 // We don't record specializations.
704 if (Conversion->getPrimaryTemplate())
705 return;
706
707 // FIXME: We intentionally don't use the decl's access here because it
708 // hasn't been set yet. That's really just a misdesign in Sema.
709
710 if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000711 if (FunTmpl->getPreviousDecl())
712 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000713 FunTmpl);
714 else
715 data().Conversions.addDecl(FunTmpl);
716 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000717 if (Conversion->getPreviousDecl())
718 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000719 Conversion);
720 else
721 data().Conversions.addDecl(Conversion);
722 }
723 }
724
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000725 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000726 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000727
728 // Handle non-static data members.
729 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000730 // C++ [class.bit]p2:
731 // A declaration for a bit-field that omits the identifier declares an
732 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
733 // initialized.
734 if (Field->isUnnamedBitfield())
735 return;
736
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000737 // C++ [dcl.init.aggr]p1:
738 // An aggregate is an array or a class (clause 9) with [...] no
739 // private or protected non-static data members (clause 11).
740 //
741 // A POD must be an aggregate.
742 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
743 data().Aggregate = false;
744 data().PlainOldData = false;
745 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000746
747 // C++0x [class]p7:
748 // A standard-layout class is a class that:
749 // [...]
750 // -- has the same access control for all non-static data members,
751 switch (D->getAccess()) {
752 case AS_private: data().HasPrivateFields = true; break;
753 case AS_protected: data().HasProtectedFields = true; break;
754 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000755 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000756 };
757 if ((data().HasPrivateFields + data().HasProtectedFields +
758 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000759 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000760
Douglas Gregor2bb11012011-05-13 01:05:07 +0000761 // Keep track of the presence of mutable fields.
762 if (Field->isMutable())
763 data().HasMutableFields = true;
764
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000765 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000766 // A POD struct is a class that is both a trivial class and a
767 // standard-layout class, and has no non-static data members of type
768 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000769 //
770 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
771 // that does not explicitly have no lifetime makes the class a non-POD.
772 // However, we delay setting PlainOldData to false in this case so that
773 // Sema has a chance to diagnostic causes where the same class will be
774 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
775 // In this case, the class will become a non-POD class when we complete
776 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000777 ASTContext &Context = getASTContext();
778 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000779 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
780 if (!Context.getLangOptions().ObjCAutoRefCount ||
781 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
782 setHasObjectMember(true);
783 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000784 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000785
Chandler Carrutha8225442011-04-30 09:17:45 +0000786 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000787 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000788
Chandler Carrutha8225442011-04-30 09:17:45 +0000789 // C++0x [class]p7:
790 // A standard-layout class is a class that:
791 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000792 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000793 }
794
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000795 // Record if this field is the first non-literal field or base.
Richard Smith5fa6a042011-10-12 05:08:15 +0000796 // As a slight variation on the standard, we regard mutable members as being
797 // non-literal, since mutating a constexpr variable would break C++11
798 // constant expression semantics.
799 if ((!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType()) ||
800 Field->isMutable())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000801 data().HasNonLiteralTypeFieldsOrBases = true;
802
Richard Smith7a614d82011-06-11 17:19:42 +0000803 if (Field->hasInClassInitializer()) {
804 // C++0x [class]p5:
805 // A default constructor is trivial if [...] no non-static data member
806 // of its class has a brace-or-equal-initializer.
807 data().HasTrivialDefaultConstructor = false;
808
809 // C++0x [dcl.init.aggr]p1:
810 // An aggregate is a [...] class with [...] no
811 // brace-or-equal-initializers for non-static data members.
812 data().Aggregate = false;
813
814 // C++0x [class]p10:
815 // A POD struct is [...] a trivial class.
816 data().PlainOldData = false;
817 }
818
Douglas Gregor85606eb2010-09-28 20:50:54 +0000819 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
820 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
821 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000822 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000823 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000824 // -- for all the non-static data members of its class that are of
825 // class type (or array thereof), each such class has a trivial
826 // default constructor.
827 if (!FieldRec->hasTrivialDefaultConstructor())
828 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000829
830 // C++0x [class.copy]p13:
831 // A copy/move constructor for class X is trivial if [...]
832 // [...]
833 // -- for each non-static data member of X that is of class type (or
834 // an array thereof), the constructor selected to copy/move that
835 // member is trivial;
836 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000837 if (!FieldRec->hasTrivialCopyConstructor())
838 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000839 if (!FieldRec->hasTrivialMoveConstructor())
840 data().HasTrivialMoveConstructor = false;
841
842 // C++0x [class.copy]p27:
843 // A copy/move assignment operator for class X is trivial if [...]
844 // [...]
845 // -- for each non-static data member of X that is of class type (or
846 // an array thereof), the assignment operator selected to
847 // copy/move that member is trivial;
848 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000849 if (!FieldRec->hasTrivialCopyAssignment())
850 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000851 if (!FieldRec->hasTrivialMoveAssignment())
852 data().HasTrivialMoveAssignment = false;
853
Douglas Gregor85606eb2010-09-28 20:50:54 +0000854 if (!FieldRec->hasTrivialDestructor())
855 data().HasTrivialDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000856 if (FieldRec->hasObjectMember())
857 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000858
859 // C++0x [class]p7:
860 // A standard-layout class is a class that:
861 // -- has no non-static data members of type non-standard-layout
862 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000863 if (!FieldRec->isStandardLayout())
864 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000865
866 // C++0x [class]p7:
867 // A standard-layout class is a class that:
868 // [...]
869 // -- has no base classes of the same type as the first non-static
870 // data member.
871 // We don't want to expend bits in the state of the record decl
872 // tracking whether this is the first non-static data member so we
873 // cheat a bit and use some of the existing state: the empty bit.
874 // Virtual bases and virtual methods make a class non-empty, but they
875 // also make it non-standard-layout so we needn't check here.
876 // A non-empty base class may leave the class standard-layout, but not
877 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000878 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000879 // data member must come through here with Empty still true, and Empty
880 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000881 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000882 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
883 BE = bases_end();
884 BI != BE; ++BI) {
885 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000886 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000887 break;
888 }
889 }
890 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000891
892 // Keep track of the presence of mutable fields.
893 if (FieldRec->hasMutableFields())
894 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000895
896 // C++11 [class.copy]p13:
897 // If the implicitly-defined constructor would satisfy the
898 // requirements of a constexpr constructor, the implicitly-defined
899 // constructor is constexpr.
900 // C++11 [dcl.constexpr]p4:
901 // -- every constructor involved in initializing non-static data
902 // members [...] shall be a constexpr constructor
903 if (!Field->hasInClassInitializer() &&
904 !FieldRec->hasConstexprDefaultConstructor())
905 // The standard requires any in-class initializer to be a constant
906 // expression. We consider this to be a defect.
907 data().DefaultedDefaultConstructorIsConstexpr = false;
908
909 if (!FieldRec->hasConstexprCopyConstructor())
910 data().DefaultedCopyConstructorIsConstexpr = false;
911
912 if (FieldRec->hasDeclaredMoveConstructor() ||
913 FieldRec->needsImplicitMoveConstructor())
914 // FIXME: If the implicit move constructor generated for the member's
915 // class would be ill-formed, the implicit move constructor generated
916 // for this class calls the member's copy constructor.
917 data().DefaultedMoveConstructorIsConstexpr &=
918 FieldRec->hasConstexprMoveConstructor();
919 else if (!FieldRec->hasConstexprCopyConstructor())
920 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000921 }
Richard Smith61802452011-12-22 02:22:31 +0000922 } else {
923 // Base element type of field is a non-class type.
924 if (!T->isLiteralType()) {
925 data().DefaultedDefaultConstructorIsConstexpr = false;
926 data().DefaultedCopyConstructorIsConstexpr = false;
927 data().DefaultedMoveConstructorIsConstexpr = false;
928 } else if (!Field->hasInClassInitializer())
929 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000930 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000931
932 // C++0x [class]p7:
933 // A standard-layout class is a class that:
934 // [...]
935 // -- either has no non-static data members in the most derived
936 // class and at most one base class with non-static data members,
937 // or has no base classes with non-static data members, and
938 // At this point we know that we have a non-static data member, so the last
939 // clause holds.
940 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000941 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000942
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000943 // If this is not a zero-length bit-field, then the class is not empty.
944 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000945 if (!Field->isBitField() ||
946 (!Field->getBitWidth()->isTypeDependent() &&
947 !Field->getBitWidth()->isValueDependent() &&
948 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000949 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000950 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000951 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000952
953 // Handle using declarations of conversion functions.
954 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
955 if (Shadow->getDeclName().getNameKind()
956 == DeclarationName::CXXConversionFunctionName)
957 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000958}
959
John McCallb05b5f32010-03-15 09:07:48 +0000960static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
961 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000962 if (isa<UsingShadowDecl>(Conv))
963 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000964 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
965 T = ConvTemp->getTemplatedDecl()->getResultType();
966 else
967 T = cast<CXXConversionDecl>(Conv)->getConversionType();
968 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000969}
970
John McCallb05b5f32010-03-15 09:07:48 +0000971/// Collect the visible conversions of a base class.
972///
973/// \param Base a base class of the class we're considering
974/// \param InVirtual whether this base class is a virtual base (or a base
975/// of a virtual base)
976/// \param Access the access along the inheritance path to this base
977/// \param ParentHiddenTypes the conversions provided by the inheritors
978/// of this base
979/// \param Output the set to which to add conversions from non-virtual bases
980/// \param VOutput the set to which to add conversions from virtual bases
981/// \param HiddenVBaseCs the set of conversions which were hidden in a
982/// virtual base along some inheritance path
983static void CollectVisibleConversions(ASTContext &Context,
984 CXXRecordDecl *Record,
985 bool InVirtual,
986 AccessSpecifier Access,
987 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
988 UnresolvedSetImpl &Output,
989 UnresolvedSetImpl &VOutput,
990 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
991 // The set of types which have conversions in this class or its
992 // subclasses. As an optimization, we don't copy the derived set
993 // unless it might change.
994 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
995 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
996
997 // Collect the direct conversions and figure out which conversions
998 // will be hidden in the subclasses.
999 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1000 if (!Cs.empty()) {
1001 HiddenTypesBuffer = ParentHiddenTypes;
1002 HiddenTypes = &HiddenTypesBuffer;
1003
1004 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
1005 bool Hidden =
1006 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
1007
1008 // If this conversion is hidden and we're in a virtual base,
1009 // remember that it's hidden along some inheritance path.
1010 if (Hidden && InVirtual)
1011 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1012
1013 // If this conversion isn't hidden, add it to the appropriate output.
1014 else if (!Hidden) {
1015 AccessSpecifier IAccess
1016 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1017
1018 if (InVirtual)
1019 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001020 else
John McCallb05b5f32010-03-15 09:07:48 +00001021 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001022 }
1023 }
1024 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001025
John McCallb05b5f32010-03-15 09:07:48 +00001026 // Collect information recursively from any base classes.
1027 for (CXXRecordDecl::base_class_iterator
1028 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1029 const RecordType *RT = I->getType()->getAs<RecordType>();
1030 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001031
John McCallb05b5f32010-03-15 09:07:48 +00001032 AccessSpecifier BaseAccess
1033 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1034 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001035
John McCallb05b5f32010-03-15 09:07:48 +00001036 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1037 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1038 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001039 }
John McCallb05b5f32010-03-15 09:07:48 +00001040}
Sebastian Redl9994a342009-10-25 17:03:50 +00001041
John McCallb05b5f32010-03-15 09:07:48 +00001042/// Collect the visible conversions of a class.
1043///
1044/// This would be extremely straightforward if it weren't for virtual
1045/// bases. It might be worth special-casing that, really.
1046static void CollectVisibleConversions(ASTContext &Context,
1047 CXXRecordDecl *Record,
1048 UnresolvedSetImpl &Output) {
1049 // The collection of all conversions in virtual bases that we've
1050 // found. These will be added to the output as long as they don't
1051 // appear in the hidden-conversions set.
1052 UnresolvedSet<8> VBaseCs;
1053
1054 // The set of conversions in virtual bases that we've determined to
1055 // be hidden.
1056 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1057
1058 // The set of types hidden by classes derived from this one.
1059 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1060
1061 // Go ahead and collect the direct conversions and add them to the
1062 // hidden-types set.
1063 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1064 Output.append(Cs.begin(), Cs.end());
1065 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1066 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1067
1068 // Recursively collect conversions from base classes.
1069 for (CXXRecordDecl::base_class_iterator
1070 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1071 const RecordType *RT = I->getType()->getAs<RecordType>();
1072 if (!RT) continue;
1073
1074 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1075 I->isVirtual(), I->getAccessSpecifier(),
1076 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1077 }
1078
1079 // Add any unhidden conversions provided by virtual bases.
1080 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1081 I != E; ++I) {
1082 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1083 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001084 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001085}
1086
1087/// getVisibleConversionFunctions - get all conversion functions visible
1088/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +00001089const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001090 // If root class, all conversions are visible.
1091 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +00001092 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +00001093 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +00001094 if (data().ComputedVisibleConversions)
1095 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +00001096 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001097 data().ComputedVisibleConversions = true;
1098 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001099}
1100
John McCall32daa422010-03-31 01:36:47 +00001101void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1102 // This operation is O(N) but extremely rare. Sema only uses it to
1103 // remove UsingShadowDecls in a class that were followed by a direct
1104 // declaration, e.g.:
1105 // class A : B {
1106 // using B::operator int;
1107 // operator int();
1108 // };
1109 // This is uncommon by itself and even more uncommon in conjunction
1110 // with sufficiently large numbers of directly-declared conversions
1111 // that asymptotic behavior matters.
1112
1113 UnresolvedSetImpl &Convs = *getConversionFunctions();
1114 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1115 if (Convs[I].getDecl() == ConvDecl) {
1116 Convs.erase(I);
1117 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1118 && "conversion was found multiple times in unresolved set");
1119 return;
1120 }
1121 }
1122
1123 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001124}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001125
Douglas Gregorf6b11852009-10-08 15:14:33 +00001126CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001127 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001128 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1129
1130 return 0;
1131}
1132
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001133MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1134 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1135}
1136
Douglas Gregorf6b11852009-10-08 15:14:33 +00001137void
1138CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1139 TemplateSpecializationKind TSK) {
1140 assert(TemplateOrInstantiation.isNull() &&
1141 "Previous template or instantiation?");
1142 assert(!isa<ClassTemplateSpecializationDecl>(this));
1143 TemplateOrInstantiation
1144 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1145}
1146
Anders Carlssonb13e3572009-12-07 06:33:48 +00001147TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1148 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001149 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1150 return Spec->getSpecializationKind();
1151
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001152 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001153 return MSInfo->getTemplateSpecializationKind();
1154
1155 return TSK_Undeclared;
1156}
1157
1158void
1159CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1160 if (ClassTemplateSpecializationDecl *Spec
1161 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1162 Spec->setSpecializationKind(TSK);
1163 return;
1164 }
1165
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001166 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001167 MSInfo->setTemplateSpecializationKind(TSK);
1168 return;
1169 }
1170
David Blaikieb219cfc2011-09-23 05:06:16 +00001171 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001172}
1173
Douglas Gregor1d110e02010-07-01 14:13:13 +00001174CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1175 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001176 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001177
1178 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001179 = Context.DeclarationNames.getCXXDestructorName(
1180 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001181
John McCallc0bf4622010-02-23 00:48:20 +00001182 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001183 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001184 if (I == E)
1185 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001187 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001188 return Dtor;
1189}
1190
Douglas Gregorda2142f2011-02-19 18:51:44 +00001191void CXXRecordDecl::completeDefinition() {
1192 completeDefinition(0);
1193}
1194
1195void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1196 RecordDecl::completeDefinition();
1197
John McCallf85e1932011-06-15 23:02:42 +00001198 if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) {
1199 // Objective-C Automatic Reference Counting:
1200 // If a class has a non-static data member of Objective-C pointer
1201 // type (or array thereof), it is a non-POD type and its
1202 // default constructor (if any), copy constructor, copy assignment
1203 // operator, and destructor are non-trivial.
1204 struct DefinitionData &Data = data();
1205 Data.PlainOldData = false;
1206 Data.HasTrivialDefaultConstructor = false;
1207 Data.HasTrivialCopyConstructor = false;
1208 Data.HasTrivialCopyAssignment = false;
1209 Data.HasTrivialDestructor = false;
1210 }
1211
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001212 // If the class may be abstract (but hasn't been marked as such), check for
1213 // any pure final overriders.
1214 if (mayBeAbstract()) {
1215 CXXFinalOverriderMap MyFinalOverriders;
1216 if (!FinalOverriders) {
1217 getFinalOverriders(MyFinalOverriders);
1218 FinalOverriders = &MyFinalOverriders;
1219 }
1220
1221 bool Done = false;
1222 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1223 MEnd = FinalOverriders->end();
1224 M != MEnd && !Done; ++M) {
1225 for (OverridingMethods::iterator SO = M->second.begin(),
1226 SOEnd = M->second.end();
1227 SO != SOEnd && !Done; ++SO) {
1228 assert(SO->second.size() > 0 &&
1229 "All virtual functions have overridding virtual functions");
1230
1231 // C++ [class.abstract]p4:
1232 // A class is abstract if it contains or inherits at least one
1233 // pure virtual function for which the final overrider is pure
1234 // virtual.
1235 if (SO->second.front().Method->isPure()) {
1236 data().Abstract = true;
1237 Done = true;
1238 break;
1239 }
1240 }
1241 }
1242 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001243
1244 // Set access bits correctly on the directly-declared conversions.
1245 for (UnresolvedSetIterator I = data().Conversions.begin(),
1246 E = data().Conversions.end();
1247 I != E; ++I)
1248 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001249}
1250
1251bool CXXRecordDecl::mayBeAbstract() const {
1252 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1253 isDependentContext())
1254 return false;
1255
1256 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1257 BEnd = bases_end();
1258 B != BEnd; ++B) {
1259 CXXRecordDecl *BaseDecl
1260 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1261 if (BaseDecl->isAbstract())
1262 return true;
1263 }
1264
1265 return false;
1266}
1267
David Blaikie99ba9e32011-12-20 02:48:34 +00001268void CXXMethodDecl::anchor() { }
1269
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001270CXXMethodDecl *
1271CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001272 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001273 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001274 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001275 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001276 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001277 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001278 isStatic, SCAsWritten, isInline, isConstexpr,
1279 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001280}
1281
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001282CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1283 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1284 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1285 DeclarationNameInfo(), QualType(),
1286 0, false, SC_None, false, false,
1287 SourceLocation());
1288}
1289
Douglas Gregor90916562009-09-29 18:16:17 +00001290bool CXXMethodDecl::isUsualDeallocationFunction() const {
1291 if (getOverloadedOperator() != OO_Delete &&
1292 getOverloadedOperator() != OO_Array_Delete)
1293 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001294
1295 // C++ [basic.stc.dynamic.deallocation]p2:
1296 // A template instance is never a usual deallocation function,
1297 // regardless of its signature.
1298 if (getPrimaryTemplate())
1299 return false;
1300
Douglas Gregor90916562009-09-29 18:16:17 +00001301 // C++ [basic.stc.dynamic.deallocation]p2:
1302 // If a class T has a member deallocation function named operator delete
1303 // with exactly one parameter, then that function is a usual (non-placement)
1304 // deallocation function. [...]
1305 if (getNumParams() == 1)
1306 return true;
1307
1308 // C++ [basic.stc.dynamic.deallocation]p2:
1309 // [...] If class T does not declare such an operator delete but does
1310 // declare a member deallocation function named operator delete with
1311 // exactly two parameters, the second of which has type std::size_t (18.1),
1312 // then this function is a usual deallocation function.
1313 ASTContext &Context = getASTContext();
1314 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001315 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1316 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001317 return false;
1318
1319 // This function is a usual deallocation function if there are no
1320 // single-parameter deallocation functions of the same kind.
1321 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1322 R.first != R.second; ++R.first) {
1323 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1324 if (FD->getNumParams() == 1)
1325 return false;
1326 }
1327
1328 return true;
1329}
1330
Douglas Gregor06a9f362010-05-01 20:49:11 +00001331bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001332 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001333 // A user-declared copy assignment operator X::operator= is a non-static
1334 // non-template member function of class X with exactly one parameter of
1335 // type X, X&, const X&, volatile X& or const volatile X&.
1336 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1337 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001338 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001339 return false;
1340
1341 QualType ParamType = getParamDecl(0)->getType();
1342 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1343 ParamType = Ref->getPointeeType();
1344
1345 ASTContext &Context = getASTContext();
1346 QualType ClassType
1347 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1348 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1349}
1350
Sean Huntffe37fd2011-05-25 20:50:04 +00001351bool CXXMethodDecl::isMoveAssignmentOperator() const {
1352 // C++0x [class.copy]p19:
1353 // A user-declared move assignment operator X::operator= is a non-static
1354 // non-template member function of class X with exactly one parameter of type
1355 // X&&, const X&&, volatile X&&, or const volatile X&&.
1356 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1357 getPrimaryTemplate() || getDescribedFunctionTemplate())
1358 return false;
1359
1360 QualType ParamType = getParamDecl(0)->getType();
1361 if (!isa<RValueReferenceType>(ParamType))
1362 return false;
1363 ParamType = ParamType->getPointeeType();
1364
1365 ASTContext &Context = getASTContext();
1366 QualType ClassType
1367 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1368 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1369}
1370
Anders Carlsson05eb2442009-05-16 23:58:37 +00001371void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001372 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001373 assert(!MD->getParent()->isDependentContext() &&
1374 "Can't add an overridden method to a class template!");
1375
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001376 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001377}
1378
1379CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001380 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001381}
1382
1383CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001384 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001385}
1386
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001387unsigned CXXMethodDecl::size_overridden_methods() const {
1388 return getASTContext().overridden_methods_size(this);
1389}
1390
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001391QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001392 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1393 // If the member function is declared const, the type of this is const X*,
1394 // if the member function is declared volatile, the type of this is
1395 // volatile X*, and if the member function is declared const volatile,
1396 // the type of this is const volatile X*.
1397
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001398 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001399
John McCall3cb0ebd2010-03-10 03:28:59 +00001400 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001401 ClassTy = C.getQualifiedType(ClassTy,
1402 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001403 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001404}
1405
Eli Friedmand7d7f672009-12-06 20:50:05 +00001406bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001407 // If this function is a template instantiation, look at the template from
1408 // which it was instantiated.
1409 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1410 if (!CheckFn)
1411 CheckFn = this;
1412
Eli Friedmand7d7f672009-12-06 20:50:05 +00001413 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001414 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001415}
1416
Sean Huntcbb67482011-01-08 20:30:50 +00001417CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1418 TypeSourceInfo *TInfo, bool IsVirtual,
1419 SourceLocation L, Expr *Init,
1420 SourceLocation R,
1421 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001422 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001423 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1424 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001425{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001426}
1427
Sean Huntcbb67482011-01-08 20:30:50 +00001428CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1429 FieldDecl *Member,
1430 SourceLocation MemberLoc,
1431 SourceLocation L, Expr *Init,
1432 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001433 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001434 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001435 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1436{
1437}
1438
Sean Huntcbb67482011-01-08 20:30:50 +00001439CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1440 IndirectFieldDecl *Member,
1441 SourceLocation MemberLoc,
1442 SourceLocation L, Expr *Init,
1443 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001444 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001445 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001446 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001447{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001448}
1449
Sean Huntcbb67482011-01-08 20:30:50 +00001450CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001451 TypeSourceInfo *TInfo,
1452 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001453 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001454 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1455 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001456 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1457{
1458}
1459
1460CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001461 FieldDecl *Member,
1462 SourceLocation MemberLoc,
1463 SourceLocation L, Expr *Init,
1464 SourceLocation R,
1465 VarDecl **Indices,
1466 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001467 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001468 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001469 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001470{
1471 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1472 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1473}
1474
Sean Huntcbb67482011-01-08 20:30:50 +00001475CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1476 FieldDecl *Member,
1477 SourceLocation MemberLoc,
1478 SourceLocation L, Expr *Init,
1479 SourceLocation R,
1480 VarDecl **Indices,
1481 unsigned NumIndices) {
1482 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001483 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001484 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001485 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1486 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001487}
1488
Sean Huntcbb67482011-01-08 20:30:50 +00001489TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001490 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001491 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001492 else
1493 return TypeLoc();
1494}
1495
Sean Huntcbb67482011-01-08 20:30:50 +00001496const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001497 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001498 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001499 else
1500 return 0;
1501}
1502
Sean Huntcbb67482011-01-08 20:30:50 +00001503SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001504 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001505 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001506
1507 if (isInClassMemberInitializer())
1508 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001509
Douglas Gregor76852c22011-11-01 01:16:03 +00001510 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1511 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1512
1513 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001514}
1515
Sean Huntcbb67482011-01-08 20:30:50 +00001516SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001517 if (isInClassMemberInitializer()) {
1518 FieldDecl *D = getAnyMember();
1519 if (Expr *I = D->getInClassInitializer())
1520 return I->getSourceRange();
1521 return SourceRange();
1522 }
1523
Douglas Gregor802ab452009-12-02 22:36:29 +00001524 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001525}
1526
David Blaikie99ba9e32011-12-20 02:48:34 +00001527void CXXConstructorDecl::anchor() { }
1528
Douglas Gregorb48fe382008-10-31 09:07:45 +00001529CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001530CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1531 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1532 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1533 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001534}
1535
1536CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001537CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001538 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001539 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001540 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001541 bool isExplicit, bool isInline,
1542 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001543 assert(NameInfo.getName().getNameKind()
1544 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001545 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001546 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001547 isExplicit, isInline, isImplicitlyDeclared,
1548 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001549}
1550
Douglas Gregor76852c22011-11-01 01:16:03 +00001551CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1552 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1553 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1554 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1555 return Construct->getConstructor();
1556
1557 return 0;
1558}
1559
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001560bool CXXConstructorDecl::isDefaultConstructor() const {
1561 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001562 // A default constructor for a class X is a constructor of class
1563 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001564 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001565 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001566}
1567
Mike Stump1eb44332009-09-09 15:08:12 +00001568bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001569CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001570 return isCopyOrMoveConstructor(TypeQuals) &&
1571 getParamDecl(0)->getType()->isLValueReferenceType();
1572}
1573
1574bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1575 return isCopyOrMoveConstructor(TypeQuals) &&
1576 getParamDecl(0)->getType()->isRValueReferenceType();
1577}
1578
1579/// \brief Determine whether this is a copy or move constructor.
1580bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001581 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001582 // A non-template constructor for class X is a copy constructor
1583 // if its first parameter is of type X&, const X&, volatile X& or
1584 // const volatile X&, and either there are no other parameters
1585 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001586 // C++0x [class.copy]p3:
1587 // A non-template constructor for class X is a move constructor if its
1588 // first parameter is of type X&&, const X&&, volatile X&&, or
1589 // const volatile X&&, and either there are no other parameters or else
1590 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001591 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001592 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001593 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001594 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001595 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001596
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001597 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001598
1599 // Do we have a reference type?
1600 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001601 if (!ParamRefType)
1602 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001603
Douglas Gregorfd476482009-11-13 23:59:09 +00001604 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001605 ASTContext &Context = getASTContext();
1606
Douglas Gregorfd476482009-11-13 23:59:09 +00001607 CanQualType PointeeType
1608 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001609 CanQualType ClassTy
1610 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001611 if (PointeeType.getUnqualifiedType() != ClassTy)
1612 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001613
John McCall0953e762009-09-24 19:53:00 +00001614 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001615
1616 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001617 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001618 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001619}
1620
Anders Carlssonfaccd722009-08-28 16:57:08 +00001621bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001622 // C++ [class.conv.ctor]p1:
1623 // A constructor declared without the function-specifier explicit
1624 // that can be called with a single parameter specifies a
1625 // conversion from the type of its first parameter to the type of
1626 // its class. Such a constructor is called a converting
1627 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001628 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001629 return false;
1630
Mike Stump1eb44332009-09-09 15:08:12 +00001631 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001632 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001633 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001634 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001635}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001636
Douglas Gregor6493cc52010-11-08 17:16:59 +00001637bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001638 if ((getNumParams() < 1) ||
1639 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1640 (getPrimaryTemplate() == 0) ||
1641 (getDescribedFunctionTemplate() != 0))
1642 return false;
1643
1644 const ParmVarDecl *Param = getParamDecl(0);
1645
1646 ASTContext &Context = getASTContext();
1647 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1648
Douglas Gregor66724ea2009-11-14 01:20:54 +00001649 // Is it the same as our our class type?
1650 CanQualType ClassTy
1651 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1652 if (ParamType.getUnqualifiedType() != ClassTy)
1653 return false;
1654
1655 return true;
1656}
1657
Sebastian Redlf677ea32011-02-05 19:23:19 +00001658const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1659 // Hack: we store the inherited constructor in the overridden method table
1660 method_iterator It = begin_overridden_methods();
1661 if (It == end_overridden_methods())
1662 return 0;
1663
1664 return cast<CXXConstructorDecl>(*It);
1665}
1666
1667void
1668CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1669 // Hack: we store the inherited constructor in the overridden method table
1670 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1671 addOverriddenMethod(BaseCtor);
1672}
1673
David Blaikie99ba9e32011-12-20 02:48:34 +00001674void CXXDestructorDecl::anchor() { }
1675
Douglas Gregor42a552f2008-11-05 20:51:48 +00001676CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001677CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1678 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1679 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001680 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001681}
1682
1683CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001684CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001685 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001686 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001687 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001688 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001689 assert(NameInfo.getName().getNameKind()
1690 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001691 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001692 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001693 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001694}
1695
David Blaikie99ba9e32011-12-20 02:48:34 +00001696void CXXConversionDecl::anchor() { }
1697
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001698CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001699CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1700 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1701 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1702 QualType(), 0, false, false, false,
1703 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001704}
1705
1706CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001707CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001708 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001709 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001710 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001711 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001712 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001713 assert(NameInfo.getName().getNameKind()
1714 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001715 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001716 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001717 isInline, isExplicit, isConstexpr,
1718 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001719}
1720
David Blaikie99ba9e32011-12-20 02:48:34 +00001721void LinkageSpecDecl::anchor() { }
1722
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001723LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001724 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001725 SourceLocation ExternLoc,
1726 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001727 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001728 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001729 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001730}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001731
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001732LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1733 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1734 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1735 lang_c, SourceLocation());
1736}
1737
David Blaikie99ba9e32011-12-20 02:48:34 +00001738void UsingDirectiveDecl::anchor() { }
1739
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001740UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1741 SourceLocation L,
1742 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001743 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001744 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001745 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001746 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001747 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1748 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001749 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1750 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001751}
1752
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001753UsingDirectiveDecl *
1754UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1755 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1756 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1757 NestedNameSpecifierLoc(),
1758 SourceLocation(), 0, 0);
1759}
1760
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001761NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1762 if (NamespaceAliasDecl *NA =
1763 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1764 return NA->getNamespace();
1765 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1766}
1767
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001768void NamespaceDecl::anchor() { }
1769
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001770NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1771 SourceLocation StartLoc,
1772 SourceLocation IdLoc, IdentifierInfo *Id,
1773 NamespaceDecl *PrevDecl)
1774 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1775 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1776{
1777 setPreviousDeclaration(PrevDecl);
1778
1779 if (PrevDecl)
1780 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1781}
1782
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001783NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001784 bool Inline, SourceLocation StartLoc,
1785 SourceLocation IdLoc, IdentifierInfo *Id,
1786 NamespaceDecl *PrevDecl) {
1787 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001788}
1789
1790NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1791 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001792 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1793 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001794}
1795
David Blaikie99ba9e32011-12-20 02:48:34 +00001796void NamespaceAliasDecl::anchor() { }
1797
Mike Stump1eb44332009-09-09 15:08:12 +00001798NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001799 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001800 SourceLocation AliasLoc,
1801 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001802 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001803 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001804 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001805 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1806 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001807 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1808 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001809}
1810
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001811NamespaceAliasDecl *
1812NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1813 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1814 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1815 NestedNameSpecifierLoc(),
1816 SourceLocation(), 0);
1817}
1818
David Blaikie99ba9e32011-12-20 02:48:34 +00001819void UsingShadowDecl::anchor() { }
1820
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001821UsingShadowDecl *
1822UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1823 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1824 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1825}
1826
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001827UsingDecl *UsingShadowDecl::getUsingDecl() const {
1828 const UsingShadowDecl *Shadow = this;
1829 while (const UsingShadowDecl *NextShadow =
1830 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1831 Shadow = NextShadow;
1832 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1833}
1834
David Blaikie99ba9e32011-12-20 02:48:34 +00001835void UsingDecl::anchor() { }
1836
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001837void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1838 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1839 "declaration already in set");
1840 assert(S->getUsingDecl() == this);
1841
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001842 if (FirstUsingShadow.getPointer())
1843 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1844 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001845}
1846
1847void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1848 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1849 "declaration not in set");
1850 assert(S->getUsingDecl() == this);
1851
1852 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1853
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001854 if (FirstUsingShadow.getPointer() == S) {
1855 FirstUsingShadow.setPointer(
1856 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001857 S->UsingOrNextShadow = this;
1858 return;
1859 }
1860
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001861 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001862 while (Prev->UsingOrNextShadow != S)
1863 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1864 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1865 S->UsingOrNextShadow = this;
1866}
1867
Douglas Gregordc355712011-02-25 00:36:19 +00001868UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1869 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001870 const DeclarationNameInfo &NameInfo,
1871 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001872 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001873}
1874
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001875UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1876 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1877 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1878 DeclarationNameInfo(), false);
1879}
1880
David Blaikie99ba9e32011-12-20 02:48:34 +00001881void UnresolvedUsingValueDecl::anchor() { }
1882
John McCall7ba107a2009-11-18 02:36:19 +00001883UnresolvedUsingValueDecl *
1884UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1885 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001886 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001887 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001888 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001889 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001890}
1891
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001892UnresolvedUsingValueDecl *
1893UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1894 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1895 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1896 NestedNameSpecifierLoc(),
1897 DeclarationNameInfo());
1898}
1899
David Blaikie99ba9e32011-12-20 02:48:34 +00001900void UnresolvedUsingTypenameDecl::anchor() { }
1901
John McCall7ba107a2009-11-18 02:36:19 +00001902UnresolvedUsingTypenameDecl *
1903UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1904 SourceLocation UsingLoc,
1905 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001906 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001907 SourceLocation TargetNameLoc,
1908 DeclarationName TargetName) {
1909 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001910 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001911 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001912}
1913
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001914UnresolvedUsingTypenameDecl *
1915UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1916 void *Mem = AllocateDeserializedDecl(C, ID,
1917 sizeof(UnresolvedUsingTypenameDecl));
1918 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
1919 SourceLocation(),
1920 NestedNameSpecifierLoc(),
1921 SourceLocation(),
1922 0);
1923}
1924
David Blaikie99ba9e32011-12-20 02:48:34 +00001925void StaticAssertDecl::anchor() { }
1926
Anders Carlssonfb311762009-03-14 00:25:26 +00001927StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001928 SourceLocation StaticAssertLoc,
1929 Expr *AssertExpr,
1930 StringLiteral *Message,
1931 SourceLocation RParenLoc) {
1932 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1933 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001934}
1935
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001936StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
1937 unsigned ID) {
1938 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
1939 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,SourceLocation());
1940}
1941
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001942static const char *getAccessName(AccessSpecifier AS) {
1943 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001944 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00001945 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001946 case AS_public:
1947 return "public";
1948 case AS_private:
1949 return "private";
1950 case AS_protected:
1951 return "protected";
1952 }
David Blaikie561d3ab2012-01-17 02:30:50 +00001953 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001954}
1955
1956const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1957 AccessSpecifier AS) {
1958 return DB << getAccessName(AS);
1959}