blob: afd23f66a35decd5003665db8457cd2ab499b59e [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),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000046 HasMutableFields(false), HasOnlyCMembers(true),
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +000047 HasTrivialDefaultConstructor(true),
Richard Smith61802452011-12-22 02:22:31 +000048 HasConstexprNonCopyMoveConstructor(false),
49 DefaultedDefaultConstructorIsConstexpr(true),
50 DefaultedCopyConstructorIsConstexpr(true),
51 DefaultedMoveConstructorIsConstexpr(true),
52 HasConstexprDefaultConstructor(false), HasConstexprCopyConstructor(false),
53 HasConstexprMoveConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000054 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
55 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
56 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000057 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000058 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
59 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sebastian Redl85ea7aa2011-08-30 19:58:05 +000060 DeclaredDestructor(false), FailedImplicitMoveConstructor(false),
Eli Friedman72899c32012-01-07 04:59:52 +000061 FailedImplicitMoveAssignment(false), IsLambda(false), NumBases(0),
62 NumVBases(0), Bases(), VBases(), Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000063}
64
65CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000066 SourceLocation StartLoc, SourceLocation IdLoc,
67 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
68 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000069 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000070 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000071
Jay Foad4ba2a172011-01-12 09:06:06 +000072CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000073 DeclContext *DC, SourceLocation StartLoc,
74 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000075 CXXRecordDecl* PrevDecl,
76 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000077 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
78 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000080 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000081 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000082 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000083 return R;
84}
85
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000086CXXRecordDecl *
87CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
88 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
89 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
90 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000091}
92
Mike Stump1eb44332009-09-09 15:08:12 +000093void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000094CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000095 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000096 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +000097
Douglas Gregor7c789c12010-10-29 22:39:52 +000098 if (!data().Bases.isOffset() && data().NumBases > 0)
99 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Richard Smithdd677232011-10-18 20:08:55 +0000101 if (NumBases) {
102 // C++ [dcl.init.aggr]p1:
103 // An aggregate is [...] a class with [...] no base classes [...].
104 data().Aggregate = false;
105
106 // C++ [class]p4:
107 // A POD-struct is an aggregate class...
108 data().PlainOldData = false;
109 }
110
Anders Carlsson6f6de732010-03-29 05:13:12 +0000111 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000112 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000113
114 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000115 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000116
John McCall86ff3082010-02-04 22:26:26 +0000117 data().Bases = new(C) CXXBaseSpecifier [NumBases];
118 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000119 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000120 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000121 // Keep track of inherited vbases for this base class.
122 const CXXBaseSpecifier *Base = Bases[i];
123 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000124 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000125 if (BaseType->isDependentType())
126 continue;
127 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000128 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000129
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000130 // A class with a non-empty base class is not empty.
131 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000132 if (!BaseClassDecl->isEmpty()) {
133 if (!data().Empty) {
134 // C++0x [class]p7:
135 // A standard-layout class is a class that:
136 // [...]
137 // -- either has no non-static data members in the most derived
138 // class and at most one base class with non-static data members,
139 // or has no base classes with non-static data members, and
140 // If this is the second non-empty base, then neither of these two
141 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000142 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000143 }
144
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000145 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000146 data().HasNoNonEmptyBases = false;
147 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000148
Douglas Gregor85606eb2010-09-28 20:50:54 +0000149 // C++ [class.virtual]p1:
150 // A class that declares or inherits a virtual function is called a
151 // polymorphic class.
152 if (BaseClassDecl->isPolymorphic())
153 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000154
Chandler Carrutha8225442011-04-30 09:17:45 +0000155 // C++0x [class]p7:
156 // A standard-layout class is a class that: [...]
157 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000158 if (!BaseClassDecl->isStandardLayout())
159 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000160
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000161 // Record if this base is the first non-literal field or base.
162 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
163 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000164
Anders Carlsson6f6de732010-03-29 05:13:12 +0000165 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000166 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000167 BaseClassDecl->vbases_begin(),
168 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000169 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000170 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000171 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000172 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000173
174 if (Base->isVirtual()) {
175 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000176 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000177 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000178
179 // C++0x [meta.unary.prop] is_empty:
180 // T is a class type, but not a union type, with ... no virtual base
181 // classes
182 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000183
184 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000185 // A default constructor is trivial [...] if:
186 // -- its class has [...] no virtual bases
187 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000188
189 // C++0x [class.copy]p13:
190 // A copy/move constructor for class X is trivial if it is neither
191 // user-provided nor deleted and if
192 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000193 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000194 data().HasTrivialMoveConstructor = false;
195
196 // C++0x [class.copy]p27:
197 // A copy/move assignment operator for class X is trivial if it is
198 // neither user-provided nor deleted and if
199 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000200 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000201 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000202
203 // C++0x [class]p7:
204 // A standard-layout class is a class that: [...]
205 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000206 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000207
208 // C++11 [dcl.constexpr]p4:
209 // In the definition of a constexpr constructor [...]
210 // -- the class shall not have any virtual base classes
211 data().DefaultedDefaultConstructorIsConstexpr = false;
212 data().DefaultedCopyConstructorIsConstexpr = false;
213 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000214 } else {
215 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000216 // A default constructor is trivial [...] if:
217 // -- all the direct base classes of its class have trivial default
218 // constructors.
219 if (!BaseClassDecl->hasTrivialDefaultConstructor())
220 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000221
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000222 // C++0x [class.copy]p13:
223 // A copy/move constructor for class X is trivial if [...]
224 // [...]
225 // -- the constructor selected to copy/move each direct base class
226 // subobject is trivial, and
227 // FIXME: C++0x: We need to only consider the selected constructor
228 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000229 if (!BaseClassDecl->hasTrivialCopyConstructor())
230 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000231 if (!BaseClassDecl->hasTrivialMoveConstructor())
232 data().HasTrivialMoveConstructor = false;
233
234 // C++0x [class.copy]p27:
235 // A copy/move assignment operator for class X is trivial if [...]
236 // [...]
237 // -- the assignment operator selected to copy/move each direct base
238 // class subobject is trivial, and
239 // FIXME: C++0x: We need to only consider the selected operator instead
240 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000241 if (!BaseClassDecl->hasTrivialCopyAssignment())
242 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000243 if (!BaseClassDecl->hasTrivialMoveAssignment())
244 data().HasTrivialMoveAssignment = false;
Richard Smith61802452011-12-22 02:22:31 +0000245
246 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000247 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000248 // requirements of a constexpr constructor, the implicitly-defined
249 // default constructor is constexpr.
250 if (!BaseClassDecl->hasConstexprDefaultConstructor())
251 data().DefaultedDefaultConstructorIsConstexpr = false;
252
253 // C++11 [class.copy]p13:
254 // If the implicitly-defined constructor would satisfy the requirements
255 // of a constexpr constructor, the implicitly-defined constructor is
256 // constexpr.
257 // C++11 [dcl.constexpr]p4:
258 // -- every constructor involved in initializing [...] base class
259 // sub-objects shall be a constexpr constructor
260 if (!BaseClassDecl->hasConstexprCopyConstructor())
261 data().DefaultedCopyConstructorIsConstexpr = false;
262 if (BaseClassDecl->hasDeclaredMoveConstructor() ||
263 BaseClassDecl->needsImplicitMoveConstructor())
264 // FIXME: If the implicit move constructor generated for the base class
265 // would be ill-formed, the implicit move constructor generated for the
266 // derived class calls the base class' copy constructor.
267 data().DefaultedMoveConstructorIsConstexpr &=
Richard Smithde8facc2012-01-11 18:26:05 +0000268 BaseClassDecl->hasConstexprMoveConstructor();
Richard Smith61802452011-12-22 02:22:31 +0000269 else if (!BaseClassDecl->hasConstexprCopyConstructor())
270 data().DefaultedMoveConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000271 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000272
273 // C++ [class.ctor]p3:
274 // A destructor is trivial if all the direct base classes of its class
275 // have trivial destructors.
276 if (!BaseClassDecl->hasTrivialDestructor())
277 data().HasTrivialDestructor = false;
Douglas Gregor2bb11012011-05-13 01:05:07 +0000278
John McCallf85e1932011-06-15 23:02:42 +0000279 // A class has an Objective-C object member if... or any of its bases
280 // has an Objective-C object member.
281 if (BaseClassDecl->hasObjectMember())
282 setHasObjectMember(true);
283
Douglas Gregor2bb11012011-05-13 01:05:07 +0000284 // Keep track of the presence of mutable fields.
285 if (BaseClassDecl->hasMutableFields())
286 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000287 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000288
289 if (VBases.empty())
290 return;
291
292 // Create base specifier for any direct or indirect virtual bases.
293 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
294 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000295 for (int I = 0, E = VBases.size(); I != E; ++I)
296 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000297}
298
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000299/// Callback function for CXXRecordDecl::forallBases that acknowledges
300/// that it saw a base class.
301static bool SawBase(const CXXRecordDecl *, void *) {
302 return true;
303}
304
305bool CXXRecordDecl::hasAnyDependentBases() const {
306 if (!isDependentContext())
307 return false;
308
309 return !forallBases(SawBase, 0);
310}
311
Sean Huntffe37fd2011-05-25 20:50:04 +0000312bool CXXRecordDecl::hasConstCopyConstructor() const {
313 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000314}
315
Chandler Carruthb7e95892011-04-23 10:47:28 +0000316bool CXXRecordDecl::isTriviallyCopyable() const {
317 // C++0x [class]p5:
318 // A trivially copyable class is a class that:
319 // -- has no non-trivial copy constructors,
320 if (!hasTrivialCopyConstructor()) return false;
321 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000322 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000323 // -- has no non-trivial copy assignment operators,
324 if (!hasTrivialCopyAssignment()) return false;
325 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000326 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000327 // -- has a trivial destructor.
328 if (!hasTrivialDestructor()) return false;
329
330 return true;
331}
332
Douglas Gregor0d405db2010-07-01 20:59:04 +0000333/// \brief Perform a simplistic form of overload resolution that only considers
334/// cv-qualifiers on a single parameter, and return the best overload candidate
335/// (if there is one).
336static CXXMethodDecl *
337GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000338 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000339 if (Cands.empty())
340 return 0;
341 if (Cands.size() == 1)
342 return Cands[0].first;
343
344 unsigned Best = 0, N = Cands.size();
345 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000346 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000347 Best = I;
348
349 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000350 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000351 return 0;
352
353 return Cands[Best].first;
354}
355
Sean Huntffe37fd2011-05-25 20:50:04 +0000356CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
357 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000358 QualType ClassType
359 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000360 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000361 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000362 Context.getCanonicalType(ClassType));
363 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000364 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000365 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000366 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000367 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000368 // C++ [class.copy]p2:
369 // A non-template constructor for class X is a copy constructor if [...]
370 if (isa<FunctionTemplateDecl>(*Con))
371 continue;
372
Douglas Gregor0d405db2010-07-01 20:59:04 +0000373 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
374 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000375 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
376 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000377 Found.push_back(std::make_pair(
378 const_cast<CXXConstructorDecl *>(Constructor),
379 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000380 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000381 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000382
383 return cast_or_null<CXXConstructorDecl>(
384 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000385}
386
Sean Huntffe37fd2011-05-25 20:50:04 +0000387CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
388 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
389 if (I->isMoveConstructor())
390 return *I;
391
392 return 0;
393}
394
Douglas Gregorb87786f2010-07-01 17:48:08 +0000395CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
396 ASTContext &Context = getASTContext();
397 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
398 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
399
Chris Lattner5f9e2722011-07-23 10:55:15 +0000400 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000401 DeclContext::lookup_const_iterator Op, OpEnd;
402 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
403 // C++ [class.copy]p9:
404 // A user-declared copy assignment operator is a non-static non-template
405 // member function of class X with exactly one parameter of type X, X&,
406 // const X&, volatile X& or const volatile X&.
407 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
408 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
409 continue;
410
411 const FunctionProtoType *FnType
412 = Method->getType()->getAs<FunctionProtoType>();
413 assert(FnType && "Overloaded operator has no prototype.");
414 // Don't assert on this; an invalid decl might have been left in the AST.
415 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
416 continue;
417
418 QualType ArgType = FnType->getArgType(0);
419 Qualifiers Quals;
420 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
421 ArgType = Ref->getPointeeType();
422 // If we have a const argument and we have a reference to a non-const,
423 // this function does not match.
424 if (ArgIsConst && !ArgType.isConstQualified())
425 continue;
426
427 Quals = ArgType.getQualifiers();
428 } else {
429 // By-value copy-assignment operators are treated like const X&
430 // copy-assignment operators.
431 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
432 }
433
434 if (!Context.hasSameUnqualifiedType(ArgType, Class))
435 continue;
436
437 // Save this copy-assignment operator. It might be "the one".
438 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
439 }
440
441 // Use a simplistic form of overload resolution to find the candidate.
442 return GetBestOverloadCandidateSimple(Found);
443}
444
Sean Huntffe37fd2011-05-25 20:50:04 +0000445CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
446 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
447 if (I->isMoveAssignmentOperator())
448 return *I;
449
450 return 0;
451}
452
Douglas Gregor21386642010-09-28 21:55:22 +0000453void CXXRecordDecl::markedVirtualFunctionPure() {
454 // C++ [class.abstract]p2:
455 // A class is abstract if it has at least one pure virtual function.
456 data().Abstract = true;
457}
458
459void CXXRecordDecl::addedMember(Decl *D) {
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +0000460 if (!D->isImplicit() &&
461 !isa<FieldDecl>(D) &&
462 !isa<IndirectFieldDecl>(D) &&
463 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class))
464 data().HasOnlyCMembers = false;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000465
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000466 // Ignore friends and invalid declarations.
467 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000468 return;
469
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000470 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
471 if (FunTmpl)
472 D = FunTmpl->getTemplatedDecl();
473
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000474 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
475 if (Method->isVirtual()) {
476 // C++ [dcl.init.aggr]p1:
477 // An aggregate is an array or a class with [...] no virtual functions.
478 data().Aggregate = false;
479
480 // C++ [class]p4:
481 // A POD-struct is an aggregate class...
482 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000483
484 // Virtual functions make the class non-empty.
485 // FIXME: Standard ref?
486 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000487
488 // C++ [class.virtual]p1:
489 // A class that declares or inherits a virtual function is called a
490 // polymorphic class.
491 data().Polymorphic = true;
492
Sean Hunt023df372011-05-09 18:22:59 +0000493 // C++0x [class.ctor]p5
494 // A default constructor is trivial [...] if:
495 // -- its class has no virtual functions [...]
496 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000497
498 // C++0x [class.copy]p13:
499 // A copy/move constructor for class X is trivial if [...]
500 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000501 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000502 data().HasTrivialMoveConstructor = false;
503
504 // C++0x [class.copy]p27:
505 // A copy/move assignment operator for class X is trivial if [...]
506 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000507 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000508 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000509
Chandler Carrutha8225442011-04-30 09:17:45 +0000510 // C++0x [class]p7:
511 // A standard-layout class is a class that: [...]
512 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000513 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000514 }
515 }
516
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000517 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000518 // Notify that an implicit member was added after the definition
519 // was completed.
520 if (!isBeingDefined())
521 if (ASTMutationListener *L = getASTMutationListener())
522 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000523
Sean Huntffe37fd2011-05-25 20:50:04 +0000524 // If this is a special member function, note that it was added and then
525 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000526 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith61802452011-12-22 02:22:31 +0000527 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000528 data().DeclaredDefaultConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000529 if (Constructor->isConstexpr()) {
530 data().HasConstexprDefaultConstructor = true;
531 data().HasConstexprNonCopyMoveConstructor = true;
532 }
533 } else if (Constructor->isCopyConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000534 data().DeclaredCopyConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000535 if (Constructor->isConstexpr())
536 data().HasConstexprCopyConstructor = true;
537 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000538 data().DeclaredMoveConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000539 if (Constructor->isConstexpr())
540 data().HasConstexprMoveConstructor = true;
541 } else
Sean Huntffe37fd2011-05-25 20:50:04 +0000542 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000543 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000544 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000545 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000546 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000547 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
548 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000549 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000550 else if (Method->isMoveAssignmentOperator())
551 data().DeclaredMoveAssignment = true;
552 else
553 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000554 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000555 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000556
Sean Huntffe37fd2011-05-25 20:50:04 +0000557NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000558 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000559 }
560
561 // Handle (user-declared) constructors.
562 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
563 // Note that we have a user-declared constructor.
564 data().UserDeclaredConstructor = true;
565
Richard Smith017ab772011-09-05 02:13:09 +0000566 // Technically, "user-provided" is only defined for special member
567 // functions, but the intent of the standard is clearly that it should apply
568 // to all functions.
569 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000570
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000571 if (Constructor->isDefaultConstructor()) {
572 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000573 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000574 // C++0x [class.ctor]p5:
575 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000576 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000577 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000578 }
Richard Smith61802452011-12-22 02:22:31 +0000579 if (Constructor->isConstexpr()) {
580 data().HasConstexprDefaultConstructor = true;
581 data().HasConstexprNonCopyMoveConstructor = true;
582 }
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000583 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000584
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000585 // Note when we have a user-declared copy or move constructor, which will
586 // suppress the implicit declaration of those constructors.
587 if (!FunTmpl) {
588 if (Constructor->isCopyConstructor()) {
589 data().UserDeclaredCopyConstructor = true;
590 data().DeclaredCopyConstructor = true;
591
592 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000593 // A copy/move constructor for class X is trivial if it is not
594 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000595 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000596 data().HasTrivialCopyConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000597
598 if (Constructor->isConstexpr())
599 data().HasConstexprCopyConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000600 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000601 data().UserDeclaredMoveConstructor = true;
602 data().DeclaredMoveConstructor = true;
603
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000604 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000605 // A copy/move constructor for class X is trivial if it is not
606 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000607 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000608 data().HasTrivialMoveConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000609
610 if (Constructor->isConstexpr())
611 data().HasConstexprMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000612 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000613 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000614 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
615 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000616 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000617 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000618 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000619
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000620 // C++ [dcl.init.aggr]p1:
621 // An aggregate is an array or a class with no user-declared
622 // constructors [...].
623 // C++0x [dcl.init.aggr]p1:
624 // An aggregate is an array or a class with no user-provided
625 // constructors [...].
626 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
627 data().Aggregate = false;
628
629 // C++ [class]p4:
630 // A POD-struct is an aggregate class [...]
631 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
632 // type is technically an aggregate in C++0x since it wouldn't be in 03.
633 data().PlainOldData = false;
634
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000635 return;
636 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000637
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000638 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000639 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000640 data().DeclaredDestructor = true;
641 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000642
643 // C++ [class]p4:
644 // A POD-struct is an aggregate class that has [...] no user-defined
645 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000646 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000647 data().PlainOldData = false;
648
Douglas Gregor45fa5602011-11-07 20:56:01 +0000649 // C++11 [class.dtor]p5:
650 // A destructor is trivial if it is not user-provided and if
651 // -- the destructor is not virtual.
Richard Smith61802452011-12-22 02:22:31 +0000652 if (DD->isUserProvided() || DD->isVirtual()) {
Sean Huntcf34e752011-05-16 22:41:40 +0000653 data().HasTrivialDestructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000654 // C++11 [dcl.constexpr]p1:
655 // The constexpr specifier shall be applied only to [...] the
656 // declaration of a static data member of a literal type.
657 // C++11 [basic.types]p10:
658 // A type is a literal type if it is [...] a class type that [...] has
659 // a trivial destructor.
660 data().DefaultedDefaultConstructorIsConstexpr = false;
661 data().DefaultedCopyConstructorIsConstexpr = false;
662 data().DefaultedMoveConstructorIsConstexpr = false;
663 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000664
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000665 return;
666 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000667
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000668 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000669 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000670 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000671 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000672 // A POD-struct is an aggregate class that [...] has no user-defined
673 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000674 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000675 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000676
Sean Huntffe37fd2011-05-25 20:50:04 +0000677 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000678
Sean Huntffe37fd2011-05-25 20:50:04 +0000679 // Suppress the implicit declaration of a copy constructor.
680 data().UserDeclaredCopyAssignment = true;
681 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000682
Sean Huntffe37fd2011-05-25 20:50:04 +0000683 // C++0x [class.copy]p27:
684 // A copy/move assignment operator for class X is trivial if it is
685 // neither user-provided nor deleted [...]
686 if (Method->isUserProvided())
687 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000688
Sean Huntffe37fd2011-05-25 20:50:04 +0000689 return;
690 }
691
692 if (Method->isMoveAssignmentOperator()) {
693 // This is an extension in C++03 mode, but we'll keep consistency by
694 // taking a move assignment operator to induce non-POD-ness
695 data().PlainOldData = false;
696
697 // This is a move assignment operator.
698 data().UserDeclaredMoveAssignment = true;
699 data().DeclaredMoveAssignment = true;
700
701 // C++0x [class.copy]p27:
702 // A copy/move assignment operator for class X is trivial if it is
703 // neither user-provided nor deleted [...]
704 if (Method->isUserProvided())
705 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000706 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000707
Douglas Gregore80622f2010-09-29 04:25:11 +0000708 // Keep the list of conversion functions up-to-date.
709 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
710 // We don't record specializations.
711 if (Conversion->getPrimaryTemplate())
712 return;
713
714 // FIXME: We intentionally don't use the decl's access here because it
715 // hasn't been set yet. That's really just a misdesign in Sema.
716
717 if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000718 if (FunTmpl->getPreviousDecl())
719 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000720 FunTmpl);
721 else
722 data().Conversions.addDecl(FunTmpl);
723 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000724 if (Conversion->getPreviousDecl())
725 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000726 Conversion);
727 else
728 data().Conversions.addDecl(Conversion);
729 }
730 }
731
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000732 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000733 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000734
735 // Handle non-static data members.
736 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000737 // C++ [class.bit]p2:
738 // A declaration for a bit-field that omits the identifier declares an
739 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
740 // initialized.
741 if (Field->isUnnamedBitfield())
742 return;
743
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000744 // C++ [dcl.init.aggr]p1:
745 // An aggregate is an array or a class (clause 9) with [...] no
746 // private or protected non-static data members (clause 11).
747 //
748 // A POD must be an aggregate.
749 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
750 data().Aggregate = false;
751 data().PlainOldData = false;
752 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000753
754 // C++0x [class]p7:
755 // A standard-layout class is a class that:
756 // [...]
757 // -- has the same access control for all non-static data members,
758 switch (D->getAccess()) {
759 case AS_private: data().HasPrivateFields = true; break;
760 case AS_protected: data().HasProtectedFields = true; break;
761 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000762 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000763 };
764 if ((data().HasPrivateFields + data().HasProtectedFields +
765 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000766 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000767
Douglas Gregor2bb11012011-05-13 01:05:07 +0000768 // Keep track of the presence of mutable fields.
769 if (Field->isMutable())
770 data().HasMutableFields = true;
771
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000772 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000773 // A POD struct is a class that is both a trivial class and a
774 // standard-layout class, and has no non-static data members of type
775 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000776 //
777 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
778 // that does not explicitly have no lifetime makes the class a non-POD.
779 // However, we delay setting PlainOldData to false in this case so that
780 // Sema has a chance to diagnostic causes where the same class will be
781 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
782 // In this case, the class will become a non-POD class when we complete
783 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000784 ASTContext &Context = getASTContext();
785 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000786 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
787 if (!Context.getLangOptions().ObjCAutoRefCount ||
788 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
789 setHasObjectMember(true);
790 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000791 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000792
Chandler Carrutha8225442011-04-30 09:17:45 +0000793 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000794 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000795
Chandler Carrutha8225442011-04-30 09:17:45 +0000796 // C++0x [class]p7:
797 // A standard-layout class is a class that:
798 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000799 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000800 }
801
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000802 // Record if this field is the first non-literal field or base.
Richard Smith5fa6a042011-10-12 05:08:15 +0000803 // As a slight variation on the standard, we regard mutable members as being
804 // non-literal, since mutating a constexpr variable would break C++11
805 // constant expression semantics.
806 if ((!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType()) ||
807 Field->isMutable())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000808 data().HasNonLiteralTypeFieldsOrBases = true;
809
Richard Smith7a614d82011-06-11 17:19:42 +0000810 if (Field->hasInClassInitializer()) {
811 // C++0x [class]p5:
812 // A default constructor is trivial if [...] no non-static data member
813 // of its class has a brace-or-equal-initializer.
814 data().HasTrivialDefaultConstructor = false;
815
816 // C++0x [dcl.init.aggr]p1:
817 // An aggregate is a [...] class with [...] no
818 // brace-or-equal-initializers for non-static data members.
819 data().Aggregate = false;
820
821 // C++0x [class]p10:
822 // A POD struct is [...] a trivial class.
823 data().PlainOldData = false;
824 }
825
Douglas Gregor85606eb2010-09-28 20:50:54 +0000826 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
827 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
828 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000829 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000830 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000831 // -- for all the non-static data members of its class that are of
832 // class type (or array thereof), each such class has a trivial
833 // default constructor.
834 if (!FieldRec->hasTrivialDefaultConstructor())
835 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000836
837 // C++0x [class.copy]p13:
838 // A copy/move constructor for class X is trivial if [...]
839 // [...]
840 // -- for each non-static data member of X that is of class type (or
841 // an array thereof), the constructor selected to copy/move that
842 // member is trivial;
843 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000844 if (!FieldRec->hasTrivialCopyConstructor())
845 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000846 if (!FieldRec->hasTrivialMoveConstructor())
847 data().HasTrivialMoveConstructor = false;
848
849 // C++0x [class.copy]p27:
850 // A copy/move assignment operator for class X is trivial if [...]
851 // [...]
852 // -- for each non-static data member of X that is of class type (or
853 // an array thereof), the assignment operator selected to
854 // copy/move that member is trivial;
855 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000856 if (!FieldRec->hasTrivialCopyAssignment())
857 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000858 if (!FieldRec->hasTrivialMoveAssignment())
859 data().HasTrivialMoveAssignment = false;
860
Douglas Gregor85606eb2010-09-28 20:50:54 +0000861 if (!FieldRec->hasTrivialDestructor())
862 data().HasTrivialDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000863 if (FieldRec->hasObjectMember())
864 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000865
866 // C++0x [class]p7:
867 // A standard-layout class is a class that:
868 // -- has no non-static data members of type non-standard-layout
869 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000870 if (!FieldRec->isStandardLayout())
871 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000872
873 // C++0x [class]p7:
874 // A standard-layout class is a class that:
875 // [...]
876 // -- has no base classes of the same type as the first non-static
877 // data member.
878 // We don't want to expend bits in the state of the record decl
879 // tracking whether this is the first non-static data member so we
880 // cheat a bit and use some of the existing state: the empty bit.
881 // Virtual bases and virtual methods make a class non-empty, but they
882 // also make it non-standard-layout so we needn't check here.
883 // A non-empty base class may leave the class standard-layout, but not
884 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000885 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000886 // data member must come through here with Empty still true, and Empty
887 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000888 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000889 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
890 BE = bases_end();
891 BI != BE; ++BI) {
892 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000893 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000894 break;
895 }
896 }
897 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000898
899 // Keep track of the presence of mutable fields.
900 if (FieldRec->hasMutableFields())
901 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000902
903 // C++11 [class.copy]p13:
904 // If the implicitly-defined constructor would satisfy the
905 // requirements of a constexpr constructor, the implicitly-defined
906 // constructor is constexpr.
907 // C++11 [dcl.constexpr]p4:
908 // -- every constructor involved in initializing non-static data
909 // members [...] shall be a constexpr constructor
910 if (!Field->hasInClassInitializer() &&
911 !FieldRec->hasConstexprDefaultConstructor())
912 // The standard requires any in-class initializer to be a constant
913 // expression. We consider this to be a defect.
914 data().DefaultedDefaultConstructorIsConstexpr = false;
915
916 if (!FieldRec->hasConstexprCopyConstructor())
917 data().DefaultedCopyConstructorIsConstexpr = false;
918
919 if (FieldRec->hasDeclaredMoveConstructor() ||
920 FieldRec->needsImplicitMoveConstructor())
921 // FIXME: If the implicit move constructor generated for the member's
922 // class would be ill-formed, the implicit move constructor generated
923 // for this class calls the member's copy constructor.
924 data().DefaultedMoveConstructorIsConstexpr &=
925 FieldRec->hasConstexprMoveConstructor();
926 else if (!FieldRec->hasConstexprCopyConstructor())
927 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000928 }
Richard Smith61802452011-12-22 02:22:31 +0000929 } else {
930 // Base element type of field is a non-class type.
931 if (!T->isLiteralType()) {
932 data().DefaultedDefaultConstructorIsConstexpr = false;
933 data().DefaultedCopyConstructorIsConstexpr = false;
934 data().DefaultedMoveConstructorIsConstexpr = false;
935 } else if (!Field->hasInClassInitializer())
936 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000937 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000938
939 // C++0x [class]p7:
940 // A standard-layout class is a class that:
941 // [...]
942 // -- either has no non-static data members in the most derived
943 // class and at most one base class with non-static data members,
944 // or has no base classes with non-static data members, and
945 // At this point we know that we have a non-static data member, so the last
946 // clause holds.
947 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000948 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000949
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000950 // If this is not a zero-length bit-field, then the class is not empty.
951 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000952 if (!Field->isBitField() ||
953 (!Field->getBitWidth()->isTypeDependent() &&
954 !Field->getBitWidth()->isValueDependent() &&
955 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000956 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000957 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000958 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000959
960 // Handle using declarations of conversion functions.
961 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
962 if (Shadow->getDeclName().getNameKind()
963 == DeclarationName::CXXConversionFunctionName)
964 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000965}
966
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000967bool CXXRecordDecl::isCLike() const {
968 if (getTagKind() == TTK_Class || !TemplateOrInstantiation.isNull())
969 return false;
970 if (!hasDefinition())
971 return true;
972
Argyrios Kyrtzidis69b26d62012-01-24 01:37:11 +0000973 return isPOD() &&
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +0000974 data().HasOnlyCMembers &&
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000975 !data().HasPrivateFields &&
976 !data().HasProtectedFields &&
977 !data().NumBases;
978}
979
John McCallb05b5f32010-03-15 09:07:48 +0000980static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
981 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000982 if (isa<UsingShadowDecl>(Conv))
983 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000984 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
985 T = ConvTemp->getTemplatedDecl()->getResultType();
986 else
987 T = cast<CXXConversionDecl>(Conv)->getConversionType();
988 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000989}
990
John McCallb05b5f32010-03-15 09:07:48 +0000991/// Collect the visible conversions of a base class.
992///
993/// \param Base a base class of the class we're considering
994/// \param InVirtual whether this base class is a virtual base (or a base
995/// of a virtual base)
996/// \param Access the access along the inheritance path to this base
997/// \param ParentHiddenTypes the conversions provided by the inheritors
998/// of this base
999/// \param Output the set to which to add conversions from non-virtual bases
1000/// \param VOutput the set to which to add conversions from virtual bases
1001/// \param HiddenVBaseCs the set of conversions which were hidden in a
1002/// virtual base along some inheritance path
1003static void CollectVisibleConversions(ASTContext &Context,
1004 CXXRecordDecl *Record,
1005 bool InVirtual,
1006 AccessSpecifier Access,
1007 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
1008 UnresolvedSetImpl &Output,
1009 UnresolvedSetImpl &VOutput,
1010 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1011 // The set of types which have conversions in this class or its
1012 // subclasses. As an optimization, we don't copy the derived set
1013 // unless it might change.
1014 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1015 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1016
1017 // Collect the direct conversions and figure out which conversions
1018 // will be hidden in the subclasses.
1019 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1020 if (!Cs.empty()) {
1021 HiddenTypesBuffer = ParentHiddenTypes;
1022 HiddenTypes = &HiddenTypesBuffer;
1023
1024 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
1025 bool Hidden =
1026 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
1027
1028 // If this conversion is hidden and we're in a virtual base,
1029 // remember that it's hidden along some inheritance path.
1030 if (Hidden && InVirtual)
1031 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1032
1033 // If this conversion isn't hidden, add it to the appropriate output.
1034 else if (!Hidden) {
1035 AccessSpecifier IAccess
1036 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1037
1038 if (InVirtual)
1039 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001040 else
John McCallb05b5f32010-03-15 09:07:48 +00001041 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001042 }
1043 }
1044 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001045
John McCallb05b5f32010-03-15 09:07:48 +00001046 // Collect information recursively from any base classes.
1047 for (CXXRecordDecl::base_class_iterator
1048 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1049 const RecordType *RT = I->getType()->getAs<RecordType>();
1050 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001051
John McCallb05b5f32010-03-15 09:07:48 +00001052 AccessSpecifier BaseAccess
1053 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1054 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001055
John McCallb05b5f32010-03-15 09:07:48 +00001056 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1057 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1058 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001059 }
John McCallb05b5f32010-03-15 09:07:48 +00001060}
Sebastian Redl9994a342009-10-25 17:03:50 +00001061
John McCallb05b5f32010-03-15 09:07:48 +00001062/// Collect the visible conversions of a class.
1063///
1064/// This would be extremely straightforward if it weren't for virtual
1065/// bases. It might be worth special-casing that, really.
1066static void CollectVisibleConversions(ASTContext &Context,
1067 CXXRecordDecl *Record,
1068 UnresolvedSetImpl &Output) {
1069 // The collection of all conversions in virtual bases that we've
1070 // found. These will be added to the output as long as they don't
1071 // appear in the hidden-conversions set.
1072 UnresolvedSet<8> VBaseCs;
1073
1074 // The set of conversions in virtual bases that we've determined to
1075 // be hidden.
1076 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1077
1078 // The set of types hidden by classes derived from this one.
1079 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1080
1081 // Go ahead and collect the direct conversions and add them to the
1082 // hidden-types set.
1083 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1084 Output.append(Cs.begin(), Cs.end());
1085 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1086 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1087
1088 // Recursively collect conversions from base classes.
1089 for (CXXRecordDecl::base_class_iterator
1090 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1091 const RecordType *RT = I->getType()->getAs<RecordType>();
1092 if (!RT) continue;
1093
1094 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1095 I->isVirtual(), I->getAccessSpecifier(),
1096 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1097 }
1098
1099 // Add any unhidden conversions provided by virtual bases.
1100 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1101 I != E; ++I) {
1102 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1103 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001104 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001105}
1106
1107/// getVisibleConversionFunctions - get all conversion functions visible
1108/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +00001109const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001110 // If root class, all conversions are visible.
1111 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +00001112 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +00001113 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +00001114 if (data().ComputedVisibleConversions)
1115 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +00001116 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001117 data().ComputedVisibleConversions = true;
1118 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001119}
1120
John McCall32daa422010-03-31 01:36:47 +00001121void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1122 // This operation is O(N) but extremely rare. Sema only uses it to
1123 // remove UsingShadowDecls in a class that were followed by a direct
1124 // declaration, e.g.:
1125 // class A : B {
1126 // using B::operator int;
1127 // operator int();
1128 // };
1129 // This is uncommon by itself and even more uncommon in conjunction
1130 // with sufficiently large numbers of directly-declared conversions
1131 // that asymptotic behavior matters.
1132
1133 UnresolvedSetImpl &Convs = *getConversionFunctions();
1134 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1135 if (Convs[I].getDecl() == ConvDecl) {
1136 Convs.erase(I);
1137 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1138 && "conversion was found multiple times in unresolved set");
1139 return;
1140 }
1141 }
1142
1143 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001144}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001145
Douglas Gregorf6b11852009-10-08 15:14:33 +00001146CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001147 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001148 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1149
1150 return 0;
1151}
1152
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001153MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1154 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1155}
1156
Douglas Gregorf6b11852009-10-08 15:14:33 +00001157void
1158CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1159 TemplateSpecializationKind TSK) {
1160 assert(TemplateOrInstantiation.isNull() &&
1161 "Previous template or instantiation?");
1162 assert(!isa<ClassTemplateSpecializationDecl>(this));
1163 TemplateOrInstantiation
1164 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1165}
1166
Anders Carlssonb13e3572009-12-07 06:33:48 +00001167TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1168 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001169 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1170 return Spec->getSpecializationKind();
1171
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001172 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001173 return MSInfo->getTemplateSpecializationKind();
1174
1175 return TSK_Undeclared;
1176}
1177
1178void
1179CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1180 if (ClassTemplateSpecializationDecl *Spec
1181 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1182 Spec->setSpecializationKind(TSK);
1183 return;
1184 }
1185
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001186 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001187 MSInfo->setTemplateSpecializationKind(TSK);
1188 return;
1189 }
1190
David Blaikieb219cfc2011-09-23 05:06:16 +00001191 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001192}
1193
Douglas Gregor1d110e02010-07-01 14:13:13 +00001194CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1195 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001196 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001197
1198 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001199 = Context.DeclarationNames.getCXXDestructorName(
1200 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001201
John McCallc0bf4622010-02-23 00:48:20 +00001202 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001203 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001204 if (I == E)
1205 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001207 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001208 return Dtor;
1209}
1210
Douglas Gregorda2142f2011-02-19 18:51:44 +00001211void CXXRecordDecl::completeDefinition() {
1212 completeDefinition(0);
1213}
1214
1215void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1216 RecordDecl::completeDefinition();
1217
John McCallf85e1932011-06-15 23:02:42 +00001218 if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) {
1219 // Objective-C Automatic Reference Counting:
1220 // If a class has a non-static data member of Objective-C pointer
1221 // type (or array thereof), it is a non-POD type and its
1222 // default constructor (if any), copy constructor, copy assignment
1223 // operator, and destructor are non-trivial.
1224 struct DefinitionData &Data = data();
1225 Data.PlainOldData = false;
1226 Data.HasTrivialDefaultConstructor = false;
1227 Data.HasTrivialCopyConstructor = false;
1228 Data.HasTrivialCopyAssignment = false;
1229 Data.HasTrivialDestructor = false;
1230 }
1231
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001232 // If the class may be abstract (but hasn't been marked as such), check for
1233 // any pure final overriders.
1234 if (mayBeAbstract()) {
1235 CXXFinalOverriderMap MyFinalOverriders;
1236 if (!FinalOverriders) {
1237 getFinalOverriders(MyFinalOverriders);
1238 FinalOverriders = &MyFinalOverriders;
1239 }
1240
1241 bool Done = false;
1242 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1243 MEnd = FinalOverriders->end();
1244 M != MEnd && !Done; ++M) {
1245 for (OverridingMethods::iterator SO = M->second.begin(),
1246 SOEnd = M->second.end();
1247 SO != SOEnd && !Done; ++SO) {
1248 assert(SO->second.size() > 0 &&
1249 "All virtual functions have overridding virtual functions");
1250
1251 // C++ [class.abstract]p4:
1252 // A class is abstract if it contains or inherits at least one
1253 // pure virtual function for which the final overrider is pure
1254 // virtual.
1255 if (SO->second.front().Method->isPure()) {
1256 data().Abstract = true;
1257 Done = true;
1258 break;
1259 }
1260 }
1261 }
1262 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001263
1264 // Set access bits correctly on the directly-declared conversions.
1265 for (UnresolvedSetIterator I = data().Conversions.begin(),
1266 E = data().Conversions.end();
1267 I != E; ++I)
1268 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001269}
1270
1271bool CXXRecordDecl::mayBeAbstract() const {
1272 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1273 isDependentContext())
1274 return false;
1275
1276 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1277 BEnd = bases_end();
1278 B != BEnd; ++B) {
1279 CXXRecordDecl *BaseDecl
1280 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1281 if (BaseDecl->isAbstract())
1282 return true;
1283 }
1284
1285 return false;
1286}
1287
David Blaikie99ba9e32011-12-20 02:48:34 +00001288void CXXMethodDecl::anchor() { }
1289
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001290CXXMethodDecl *
1291CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001292 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001293 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001294 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001295 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001296 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001297 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001298 isStatic, SCAsWritten, isInline, isConstexpr,
1299 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001300}
1301
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001302CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1303 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1304 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1305 DeclarationNameInfo(), QualType(),
1306 0, false, SC_None, false, false,
1307 SourceLocation());
1308}
1309
Douglas Gregor90916562009-09-29 18:16:17 +00001310bool CXXMethodDecl::isUsualDeallocationFunction() const {
1311 if (getOverloadedOperator() != OO_Delete &&
1312 getOverloadedOperator() != OO_Array_Delete)
1313 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001314
1315 // C++ [basic.stc.dynamic.deallocation]p2:
1316 // A template instance is never a usual deallocation function,
1317 // regardless of its signature.
1318 if (getPrimaryTemplate())
1319 return false;
1320
Douglas Gregor90916562009-09-29 18:16:17 +00001321 // C++ [basic.stc.dynamic.deallocation]p2:
1322 // If a class T has a member deallocation function named operator delete
1323 // with exactly one parameter, then that function is a usual (non-placement)
1324 // deallocation function. [...]
1325 if (getNumParams() == 1)
1326 return true;
1327
1328 // C++ [basic.stc.dynamic.deallocation]p2:
1329 // [...] If class T does not declare such an operator delete but does
1330 // declare a member deallocation function named operator delete with
1331 // exactly two parameters, the second of which has type std::size_t (18.1),
1332 // then this function is a usual deallocation function.
1333 ASTContext &Context = getASTContext();
1334 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001335 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1336 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001337 return false;
1338
1339 // This function is a usual deallocation function if there are no
1340 // single-parameter deallocation functions of the same kind.
1341 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1342 R.first != R.second; ++R.first) {
1343 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1344 if (FD->getNumParams() == 1)
1345 return false;
1346 }
1347
1348 return true;
1349}
1350
Douglas Gregor06a9f362010-05-01 20:49:11 +00001351bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001352 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001353 // A user-declared copy assignment operator X::operator= is a non-static
1354 // non-template member function of class X with exactly one parameter of
1355 // type X, X&, const X&, volatile X& or const volatile X&.
1356 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1357 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001358 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001359 return false;
1360
1361 QualType ParamType = getParamDecl(0)->getType();
1362 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1363 ParamType = Ref->getPointeeType();
1364
1365 ASTContext &Context = getASTContext();
1366 QualType ClassType
1367 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1368 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1369}
1370
Sean Huntffe37fd2011-05-25 20:50:04 +00001371bool CXXMethodDecl::isMoveAssignmentOperator() const {
1372 // C++0x [class.copy]p19:
1373 // A user-declared move assignment operator X::operator= is a non-static
1374 // non-template member function of class X with exactly one parameter of type
1375 // X&&, const X&&, volatile X&&, or const volatile X&&.
1376 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1377 getPrimaryTemplate() || getDescribedFunctionTemplate())
1378 return false;
1379
1380 QualType ParamType = getParamDecl(0)->getType();
1381 if (!isa<RValueReferenceType>(ParamType))
1382 return false;
1383 ParamType = ParamType->getPointeeType();
1384
1385 ASTContext &Context = getASTContext();
1386 QualType ClassType
1387 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1388 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1389}
1390
Anders Carlsson05eb2442009-05-16 23:58:37 +00001391void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001392 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001393 assert(!MD->getParent()->isDependentContext() &&
1394 "Can't add an overridden method to a class template!");
1395
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001396 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001397}
1398
1399CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001400 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001401}
1402
1403CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001404 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001405}
1406
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001407unsigned CXXMethodDecl::size_overridden_methods() const {
1408 return getASTContext().overridden_methods_size(this);
1409}
1410
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001411QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001412 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1413 // If the member function is declared const, the type of this is const X*,
1414 // if the member function is declared volatile, the type of this is
1415 // volatile X*, and if the member function is declared const volatile,
1416 // the type of this is const volatile X*.
1417
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001418 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001419
John McCall3cb0ebd2010-03-10 03:28:59 +00001420 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001421 ClassTy = C.getQualifiedType(ClassTy,
1422 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001423 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001424}
1425
Eli Friedmand7d7f672009-12-06 20:50:05 +00001426bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001427 // If this function is a template instantiation, look at the template from
1428 // which it was instantiated.
1429 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1430 if (!CheckFn)
1431 CheckFn = this;
1432
Eli Friedmand7d7f672009-12-06 20:50:05 +00001433 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001434 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001435}
1436
Sean Huntcbb67482011-01-08 20:30:50 +00001437CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1438 TypeSourceInfo *TInfo, bool IsVirtual,
1439 SourceLocation L, Expr *Init,
1440 SourceLocation R,
1441 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001442 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001443 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1444 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001445{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001446}
1447
Sean Huntcbb67482011-01-08 20:30:50 +00001448CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1449 FieldDecl *Member,
1450 SourceLocation MemberLoc,
1451 SourceLocation L, Expr *Init,
1452 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001453 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001454 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001455 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1456{
1457}
1458
Sean Huntcbb67482011-01-08 20:30:50 +00001459CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1460 IndirectFieldDecl *Member,
1461 SourceLocation MemberLoc,
1462 SourceLocation L, Expr *Init,
1463 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001464 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001465 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001466 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001467{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001468}
1469
Sean Huntcbb67482011-01-08 20:30:50 +00001470CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001471 TypeSourceInfo *TInfo,
1472 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001473 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001474 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1475 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001476 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1477{
1478}
1479
1480CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001481 FieldDecl *Member,
1482 SourceLocation MemberLoc,
1483 SourceLocation L, Expr *Init,
1484 SourceLocation R,
1485 VarDecl **Indices,
1486 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001487 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001488 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001489 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001490{
1491 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1492 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1493}
1494
Sean Huntcbb67482011-01-08 20:30:50 +00001495CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1496 FieldDecl *Member,
1497 SourceLocation MemberLoc,
1498 SourceLocation L, Expr *Init,
1499 SourceLocation R,
1500 VarDecl **Indices,
1501 unsigned NumIndices) {
1502 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001503 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001504 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001505 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1506 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001507}
1508
Sean Huntcbb67482011-01-08 20:30:50 +00001509TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001510 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001511 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001512 else
1513 return TypeLoc();
1514}
1515
Sean Huntcbb67482011-01-08 20:30:50 +00001516const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001517 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001518 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001519 else
1520 return 0;
1521}
1522
Sean Huntcbb67482011-01-08 20:30:50 +00001523SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001524 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001525 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001526
1527 if (isInClassMemberInitializer())
1528 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001529
Douglas Gregor76852c22011-11-01 01:16:03 +00001530 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1531 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1532
1533 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001534}
1535
Sean Huntcbb67482011-01-08 20:30:50 +00001536SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001537 if (isInClassMemberInitializer()) {
1538 FieldDecl *D = getAnyMember();
1539 if (Expr *I = D->getInClassInitializer())
1540 return I->getSourceRange();
1541 return SourceRange();
1542 }
1543
Douglas Gregor802ab452009-12-02 22:36:29 +00001544 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001545}
1546
David Blaikie99ba9e32011-12-20 02:48:34 +00001547void CXXConstructorDecl::anchor() { }
1548
Douglas Gregorb48fe382008-10-31 09:07:45 +00001549CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001550CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1551 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1552 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1553 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001554}
1555
1556CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001557CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001558 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001559 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001560 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001561 bool isExplicit, bool isInline,
1562 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001563 assert(NameInfo.getName().getNameKind()
1564 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001565 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001566 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001567 isExplicit, isInline, isImplicitlyDeclared,
1568 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001569}
1570
Douglas Gregor76852c22011-11-01 01:16:03 +00001571CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1572 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1573 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1574 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1575 return Construct->getConstructor();
1576
1577 return 0;
1578}
1579
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001580bool CXXConstructorDecl::isDefaultConstructor() const {
1581 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001582 // A default constructor for a class X is a constructor of class
1583 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001584 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001585 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001586}
1587
Mike Stump1eb44332009-09-09 15:08:12 +00001588bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001589CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001590 return isCopyOrMoveConstructor(TypeQuals) &&
1591 getParamDecl(0)->getType()->isLValueReferenceType();
1592}
1593
1594bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1595 return isCopyOrMoveConstructor(TypeQuals) &&
1596 getParamDecl(0)->getType()->isRValueReferenceType();
1597}
1598
1599/// \brief Determine whether this is a copy or move constructor.
1600bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001601 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001602 // A non-template constructor for class X is a copy constructor
1603 // if its first parameter is of type X&, const X&, volatile X& or
1604 // const volatile X&, and either there are no other parameters
1605 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001606 // C++0x [class.copy]p3:
1607 // A non-template constructor for class X is a move constructor if its
1608 // first parameter is of type X&&, const X&&, volatile X&&, or
1609 // const volatile X&&, and either there are no other parameters or else
1610 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001611 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001612 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001613 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001614 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001615 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001616
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001617 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001618
1619 // Do we have a reference type?
1620 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001621 if (!ParamRefType)
1622 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001623
Douglas Gregorfd476482009-11-13 23:59:09 +00001624 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001625 ASTContext &Context = getASTContext();
1626
Douglas Gregorfd476482009-11-13 23:59:09 +00001627 CanQualType PointeeType
1628 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001629 CanQualType ClassTy
1630 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001631 if (PointeeType.getUnqualifiedType() != ClassTy)
1632 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001633
John McCall0953e762009-09-24 19:53:00 +00001634 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001635
1636 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001637 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001638 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001639}
1640
Anders Carlssonfaccd722009-08-28 16:57:08 +00001641bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001642 // C++ [class.conv.ctor]p1:
1643 // A constructor declared without the function-specifier explicit
1644 // that can be called with a single parameter specifies a
1645 // conversion from the type of its first parameter to the type of
1646 // its class. Such a constructor is called a converting
1647 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001648 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001649 return false;
1650
Mike Stump1eb44332009-09-09 15:08:12 +00001651 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001652 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001653 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001654 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001655}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001656
Douglas Gregor6493cc52010-11-08 17:16:59 +00001657bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001658 if ((getNumParams() < 1) ||
1659 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1660 (getPrimaryTemplate() == 0) ||
1661 (getDescribedFunctionTemplate() != 0))
1662 return false;
1663
1664 const ParmVarDecl *Param = getParamDecl(0);
1665
1666 ASTContext &Context = getASTContext();
1667 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1668
Douglas Gregor66724ea2009-11-14 01:20:54 +00001669 // Is it the same as our our class type?
1670 CanQualType ClassTy
1671 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1672 if (ParamType.getUnqualifiedType() != ClassTy)
1673 return false;
1674
1675 return true;
1676}
1677
Sebastian Redlf677ea32011-02-05 19:23:19 +00001678const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1679 // Hack: we store the inherited constructor in the overridden method table
1680 method_iterator It = begin_overridden_methods();
1681 if (It == end_overridden_methods())
1682 return 0;
1683
1684 return cast<CXXConstructorDecl>(*It);
1685}
1686
1687void
1688CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1689 // Hack: we store the inherited constructor in the overridden method table
1690 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1691 addOverriddenMethod(BaseCtor);
1692}
1693
David Blaikie99ba9e32011-12-20 02:48:34 +00001694void CXXDestructorDecl::anchor() { }
1695
Douglas Gregor42a552f2008-11-05 20:51:48 +00001696CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001697CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1698 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1699 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001700 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001701}
1702
1703CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001704CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001705 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001706 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001707 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001708 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001709 assert(NameInfo.getName().getNameKind()
1710 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001711 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001712 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001713 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001714}
1715
David Blaikie99ba9e32011-12-20 02:48:34 +00001716void CXXConversionDecl::anchor() { }
1717
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001718CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001719CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1720 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1721 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1722 QualType(), 0, false, false, false,
1723 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001724}
1725
1726CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001727CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001728 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001729 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001730 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001731 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001732 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001733 assert(NameInfo.getName().getNameKind()
1734 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001735 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001736 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001737 isInline, isExplicit, isConstexpr,
1738 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001739}
1740
David Blaikie99ba9e32011-12-20 02:48:34 +00001741void LinkageSpecDecl::anchor() { }
1742
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001743LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001744 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001745 SourceLocation ExternLoc,
1746 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001747 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001748 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001749 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001750}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001751
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001752LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1753 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1754 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1755 lang_c, SourceLocation());
1756}
1757
David Blaikie99ba9e32011-12-20 02:48:34 +00001758void UsingDirectiveDecl::anchor() { }
1759
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001760UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1761 SourceLocation L,
1762 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001763 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001764 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001765 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001766 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001767 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1768 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001769 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1770 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001771}
1772
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001773UsingDirectiveDecl *
1774UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1775 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1776 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1777 NestedNameSpecifierLoc(),
1778 SourceLocation(), 0, 0);
1779}
1780
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001781NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1782 if (NamespaceAliasDecl *NA =
1783 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1784 return NA->getNamespace();
1785 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1786}
1787
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001788void NamespaceDecl::anchor() { }
1789
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001790NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1791 SourceLocation StartLoc,
1792 SourceLocation IdLoc, IdentifierInfo *Id,
1793 NamespaceDecl *PrevDecl)
1794 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1795 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1796{
1797 setPreviousDeclaration(PrevDecl);
1798
1799 if (PrevDecl)
1800 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1801}
1802
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001803NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001804 bool Inline, SourceLocation StartLoc,
1805 SourceLocation IdLoc, IdentifierInfo *Id,
1806 NamespaceDecl *PrevDecl) {
1807 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001808}
1809
1810NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1811 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001812 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1813 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001814}
1815
David Blaikie99ba9e32011-12-20 02:48:34 +00001816void NamespaceAliasDecl::anchor() { }
1817
Mike Stump1eb44332009-09-09 15:08:12 +00001818NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001819 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001820 SourceLocation AliasLoc,
1821 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001822 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001823 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001824 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001825 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1826 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001827 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1828 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001829}
1830
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001831NamespaceAliasDecl *
1832NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1833 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1834 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1835 NestedNameSpecifierLoc(),
1836 SourceLocation(), 0);
1837}
1838
David Blaikie99ba9e32011-12-20 02:48:34 +00001839void UsingShadowDecl::anchor() { }
1840
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001841UsingShadowDecl *
1842UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1843 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1844 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1845}
1846
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001847UsingDecl *UsingShadowDecl::getUsingDecl() const {
1848 const UsingShadowDecl *Shadow = this;
1849 while (const UsingShadowDecl *NextShadow =
1850 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1851 Shadow = NextShadow;
1852 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1853}
1854
David Blaikie99ba9e32011-12-20 02:48:34 +00001855void UsingDecl::anchor() { }
1856
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001857void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1858 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1859 "declaration already in set");
1860 assert(S->getUsingDecl() == this);
1861
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001862 if (FirstUsingShadow.getPointer())
1863 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1864 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001865}
1866
1867void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1868 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1869 "declaration not in set");
1870 assert(S->getUsingDecl() == this);
1871
1872 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1873
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001874 if (FirstUsingShadow.getPointer() == S) {
1875 FirstUsingShadow.setPointer(
1876 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001877 S->UsingOrNextShadow = this;
1878 return;
1879 }
1880
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001881 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001882 while (Prev->UsingOrNextShadow != S)
1883 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1884 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1885 S->UsingOrNextShadow = this;
1886}
1887
Douglas Gregordc355712011-02-25 00:36:19 +00001888UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1889 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001890 const DeclarationNameInfo &NameInfo,
1891 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001892 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001893}
1894
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001895UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1896 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1897 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1898 DeclarationNameInfo(), false);
1899}
1900
David Blaikie99ba9e32011-12-20 02:48:34 +00001901void UnresolvedUsingValueDecl::anchor() { }
1902
John McCall7ba107a2009-11-18 02:36:19 +00001903UnresolvedUsingValueDecl *
1904UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1905 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001906 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001907 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001908 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001909 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001910}
1911
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001912UnresolvedUsingValueDecl *
1913UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1914 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1915 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1916 NestedNameSpecifierLoc(),
1917 DeclarationNameInfo());
1918}
1919
David Blaikie99ba9e32011-12-20 02:48:34 +00001920void UnresolvedUsingTypenameDecl::anchor() { }
1921
John McCall7ba107a2009-11-18 02:36:19 +00001922UnresolvedUsingTypenameDecl *
1923UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1924 SourceLocation UsingLoc,
1925 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001926 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001927 SourceLocation TargetNameLoc,
1928 DeclarationName TargetName) {
1929 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001930 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001931 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001932}
1933
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001934UnresolvedUsingTypenameDecl *
1935UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1936 void *Mem = AllocateDeserializedDecl(C, ID,
1937 sizeof(UnresolvedUsingTypenameDecl));
1938 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
1939 SourceLocation(),
1940 NestedNameSpecifierLoc(),
1941 SourceLocation(),
1942 0);
1943}
1944
David Blaikie99ba9e32011-12-20 02:48:34 +00001945void StaticAssertDecl::anchor() { }
1946
Anders Carlssonfb311762009-03-14 00:25:26 +00001947StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001948 SourceLocation StaticAssertLoc,
1949 Expr *AssertExpr,
1950 StringLiteral *Message,
1951 SourceLocation RParenLoc) {
1952 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1953 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001954}
1955
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001956StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
1957 unsigned ID) {
1958 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
1959 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,SourceLocation());
1960}
1961
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001962static const char *getAccessName(AccessSpecifier AS) {
1963 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001964 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00001965 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001966 case AS_public:
1967 return "public";
1968 case AS_private:
1969 return "private";
1970 case AS_protected:
1971 return "protected";
1972 }
David Blaikie561d3ab2012-01-17 02:30:50 +00001973 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001974}
1975
1976const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1977 AccessSpecifier AS) {
1978 return DB << getAccessName(AS);
1979}