blob: 4de157e9f5a4eba05d8759cedcd697b994001669 [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000017#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000018#include "clang/AST/CXXInheritance.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregor76852c22011-11-01 01:16:03 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000021#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000022#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000023#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Decl Allocation/Deallocation Method Implementations
29//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000030
David Blaikie99ba9e32011-12-20 02:48:34 +000031void AccessSpecDecl::anchor() { }
32
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000033AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
34 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(AccessSpecDecl));
35 return new (Mem) AccessSpecDecl(EmptyShell());
36}
37
John McCall86ff3082010-02-04 22:26:26 +000038CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
39 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000040 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
41 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000042 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000043 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000044 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000045 HasMutableFields(false), HasOnlyCMembers(true),
Richard Smithd079abf2012-05-07 01:07:30 +000046 HasInClassInitializer(false),
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +000047 HasTrivialDefaultConstructor(true),
Richard Smith61802452011-12-22 02:22:31 +000048 HasConstexprNonCopyMoveConstructor(false),
49 DefaultedDefaultConstructorIsConstexpr(true),
Richard Smithd3861ce2012-06-10 07:07:24 +000050 HasConstexprDefaultConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000051 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
52 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
Richard Smithdfefb842012-02-25 07:33:38 +000053 HasIrrelevantDestructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000054 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000055 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000056 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
57 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sebastian Redl85ea7aa2011-08-30 19:58:05 +000058 DeclaredDestructor(false), FailedImplicitMoveConstructor(false),
Eli Friedman72899c32012-01-07 04:59:52 +000059 FailedImplicitMoveAssignment(false), IsLambda(false), NumBases(0),
60 NumVBases(0), Bases(), VBases(), Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000061}
62
63CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000064 SourceLocation StartLoc, SourceLocation IdLoc,
65 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
66 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000067 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000068 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000069
Jay Foad4ba2a172011-01-12 09:06:06 +000070CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000071 DeclContext *DC, SourceLocation StartLoc,
72 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000073 CXXRecordDecl* PrevDecl,
74 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000075 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
76 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000077
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000078 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000079 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000080 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000081 return R;
82}
83
Douglas Gregorda8962a2012-02-13 15:44:47 +000084CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Douglas Gregorf4b7de12012-02-21 19:11:17 +000085 SourceLocation Loc, bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +000086 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
87 0, 0);
88 R->IsBeingDefined = true;
Douglas Gregorf4b7de12012-02-21 19:11:17 +000089 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Dependent);
Douglas Gregorda8962a2012-02-13 15:44:47 +000090 C.getTypeDeclType(R, /*PrevDecl=*/0);
91 return R;
92}
93
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000094CXXRecordDecl *
95CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
96 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
97 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
98 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000099}
100
Mike Stump1eb44332009-09-09 15:08:12 +0000101void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000102CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000103 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000104 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000105
Douglas Gregor7c789c12010-10-29 22:39:52 +0000106 if (!data().Bases.isOffset() && data().NumBases > 0)
107 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Richard Smithdd677232011-10-18 20:08:55 +0000109 if (NumBases) {
110 // C++ [dcl.init.aggr]p1:
111 // An aggregate is [...] a class with [...] no base classes [...].
112 data().Aggregate = false;
113
114 // C++ [class]p4:
115 // A POD-struct is an aggregate class...
116 data().PlainOldData = false;
117 }
118
Anders Carlsson6f6de732010-03-29 05:13:12 +0000119 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000120 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000121
122 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000123 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000124
John McCall86ff3082010-02-04 22:26:26 +0000125 data().Bases = new(C) CXXBaseSpecifier [NumBases];
126 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000127 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000128 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000129 // Keep track of inherited vbases for this base class.
130 const CXXBaseSpecifier *Base = Bases[i];
131 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000132 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000133 if (BaseType->isDependentType())
134 continue;
135 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000136 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000137
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000138 // A class with a non-empty base class is not empty.
139 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000140 if (!BaseClassDecl->isEmpty()) {
141 if (!data().Empty) {
142 // C++0x [class]p7:
143 // A standard-layout class is a class that:
144 // [...]
145 // -- either has no non-static data members in the most derived
146 // class and at most one base class with non-static data members,
147 // or has no base classes with non-static data members, and
148 // If this is the second non-empty base, then neither of these two
149 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000150 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000151 }
152
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000153 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000154 data().HasNoNonEmptyBases = false;
155 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000156
Douglas Gregor85606eb2010-09-28 20:50:54 +0000157 // C++ [class.virtual]p1:
158 // A class that declares or inherits a virtual function is called a
159 // polymorphic class.
160 if (BaseClassDecl->isPolymorphic())
161 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000162
Chandler Carrutha8225442011-04-30 09:17:45 +0000163 // C++0x [class]p7:
164 // A standard-layout class is a class that: [...]
165 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000166 if (!BaseClassDecl->isStandardLayout())
167 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000168
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000169 // Record if this base is the first non-literal field or base.
170 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
171 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000172
Anders Carlsson6f6de732010-03-29 05:13:12 +0000173 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000174 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000175 BaseClassDecl->vbases_begin(),
176 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000177 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000178 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000179 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000180 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000181
182 if (Base->isVirtual()) {
183 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000184 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000185 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000186
187 // C++0x [meta.unary.prop] is_empty:
188 // T is a class type, but not a union type, with ... no virtual base
189 // classes
190 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000191
192 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000193 // A default constructor is trivial [...] if:
194 // -- its class has [...] no virtual bases
195 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000196
197 // C++0x [class.copy]p13:
198 // A copy/move constructor for class X is trivial if it is neither
199 // user-provided nor deleted and if
200 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000201 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000202 data().HasTrivialMoveConstructor = false;
203
204 // C++0x [class.copy]p27:
205 // A copy/move assignment operator for class X is trivial if it is
206 // neither user-provided nor deleted and if
207 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000208 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000209 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000210
211 // C++0x [class]p7:
212 // A standard-layout class is a class that: [...]
213 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000214 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000215
216 // C++11 [dcl.constexpr]p4:
217 // In the definition of a constexpr constructor [...]
218 // -- the class shall not have any virtual base classes
219 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000220 } else {
221 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000222 // A default constructor is trivial [...] if:
223 // -- all the direct base classes of its class have trivial default
224 // constructors.
225 if (!BaseClassDecl->hasTrivialDefaultConstructor())
226 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000227
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000228 // C++0x [class.copy]p13:
229 // A copy/move constructor for class X is trivial if [...]
230 // [...]
231 // -- the constructor selected to copy/move each direct base class
232 // subobject is trivial, and
233 // FIXME: C++0x: We need to only consider the selected constructor
234 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000235 if (!BaseClassDecl->hasTrivialCopyConstructor())
236 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000237 if (!BaseClassDecl->hasTrivialMoveConstructor())
238 data().HasTrivialMoveConstructor = false;
239
240 // C++0x [class.copy]p27:
241 // A copy/move assignment operator for class X is trivial if [...]
242 // [...]
243 // -- the assignment operator selected to copy/move each direct base
244 // class subobject is trivial, and
245 // FIXME: C++0x: We need to only consider the selected operator instead
246 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000247 if (!BaseClassDecl->hasTrivialCopyAssignment())
248 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000249 if (!BaseClassDecl->hasTrivialMoveAssignment())
250 data().HasTrivialMoveAssignment = false;
Richard Smith61802452011-12-22 02:22:31 +0000251
252 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000253 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000254 // requirements of a constexpr constructor, the implicitly-defined
255 // default constructor is constexpr.
256 if (!BaseClassDecl->hasConstexprDefaultConstructor())
257 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000258 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000259
260 // C++ [class.ctor]p3:
261 // A destructor is trivial if all the direct base classes of its class
262 // have trivial destructors.
263 if (!BaseClassDecl->hasTrivialDestructor())
264 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000265
266 if (!BaseClassDecl->hasIrrelevantDestructor())
267 data().HasIrrelevantDestructor = false;
268
John McCallf85e1932011-06-15 23:02:42 +0000269 // A class has an Objective-C object member if... or any of its bases
270 // has an Objective-C object member.
271 if (BaseClassDecl->hasObjectMember())
272 setHasObjectMember(true);
273
Douglas Gregor2bb11012011-05-13 01:05:07 +0000274 // Keep track of the presence of mutable fields.
275 if (BaseClassDecl->hasMutableFields())
276 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000277 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000278
279 if (VBases.empty())
280 return;
281
282 // Create base specifier for any direct or indirect virtual bases.
283 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
284 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000285 for (int I = 0, E = VBases.size(); I != E; ++I)
286 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000287}
288
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000289/// Callback function for CXXRecordDecl::forallBases that acknowledges
290/// that it saw a base class.
291static bool SawBase(const CXXRecordDecl *, void *) {
292 return true;
293}
294
295bool CXXRecordDecl::hasAnyDependentBases() const {
296 if (!isDependentContext())
297 return false;
298
299 return !forallBases(SawBase, 0);
300}
301
Sean Huntffe37fd2011-05-25 20:50:04 +0000302bool CXXRecordDecl::hasConstCopyConstructor() const {
303 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000304}
305
Chandler Carruthb7e95892011-04-23 10:47:28 +0000306bool CXXRecordDecl::isTriviallyCopyable() const {
307 // C++0x [class]p5:
308 // A trivially copyable class is a class that:
309 // -- has no non-trivial copy constructors,
310 if (!hasTrivialCopyConstructor()) return false;
311 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000312 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000313 // -- has no non-trivial copy assignment operators,
314 if (!hasTrivialCopyAssignment()) return false;
315 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000316 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000317 // -- has a trivial destructor.
318 if (!hasTrivialDestructor()) return false;
319
320 return true;
321}
322
Douglas Gregor0d405db2010-07-01 20:59:04 +0000323/// \brief Perform a simplistic form of overload resolution that only considers
324/// cv-qualifiers on a single parameter, and return the best overload candidate
325/// (if there is one).
326static CXXMethodDecl *
327GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000328 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000329 if (Cands.empty())
330 return 0;
331 if (Cands.size() == 1)
332 return Cands[0].first;
333
334 unsigned Best = 0, N = Cands.size();
335 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000336 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000337 Best = I;
338
339 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000340 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000341 return 0;
342
343 return Cands[Best].first;
344}
345
Sean Huntffe37fd2011-05-25 20:50:04 +0000346CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
347 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000348 QualType ClassType
349 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000350 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000351 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000352 Context.getCanonicalType(ClassType));
353 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000354 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000355 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000356 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000357 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000358 // C++ [class.copy]p2:
359 // A non-template constructor for class X is a copy constructor if [...]
360 if (isa<FunctionTemplateDecl>(*Con))
361 continue;
362
Douglas Gregor0d405db2010-07-01 20:59:04 +0000363 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
364 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000365 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
366 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000367 Found.push_back(std::make_pair(
368 const_cast<CXXConstructorDecl *>(Constructor),
369 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000370 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000371 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000372
373 return cast_or_null<CXXConstructorDecl>(
374 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000375}
376
Sean Huntffe37fd2011-05-25 20:50:04 +0000377CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
378 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
379 if (I->isMoveConstructor())
David Blaikie581deb32012-06-06 20:45:41 +0000380 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000381
382 return 0;
383}
384
Douglas Gregorb87786f2010-07-01 17:48:08 +0000385CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
386 ASTContext &Context = getASTContext();
387 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
388 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
389
Chris Lattner5f9e2722011-07-23 10:55:15 +0000390 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000391 DeclContext::lookup_const_iterator Op, OpEnd;
392 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
393 // C++ [class.copy]p9:
394 // A user-declared copy assignment operator is a non-static non-template
395 // member function of class X with exactly one parameter of type X, X&,
396 // const X&, volatile X& or const volatile X&.
397 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
398 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
399 continue;
400
401 const FunctionProtoType *FnType
402 = Method->getType()->getAs<FunctionProtoType>();
403 assert(FnType && "Overloaded operator has no prototype.");
404 // Don't assert on this; an invalid decl might have been left in the AST.
405 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
406 continue;
407
408 QualType ArgType = FnType->getArgType(0);
409 Qualifiers Quals;
410 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
411 ArgType = Ref->getPointeeType();
412 // If we have a const argument and we have a reference to a non-const,
413 // this function does not match.
414 if (ArgIsConst && !ArgType.isConstQualified())
415 continue;
416
417 Quals = ArgType.getQualifiers();
418 } else {
419 // By-value copy-assignment operators are treated like const X&
420 // copy-assignment operators.
421 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
422 }
423
424 if (!Context.hasSameUnqualifiedType(ArgType, Class))
425 continue;
426
427 // Save this copy-assignment operator. It might be "the one".
428 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
429 }
430
431 // Use a simplistic form of overload resolution to find the candidate.
432 return GetBestOverloadCandidateSimple(Found);
433}
434
Sean Huntffe37fd2011-05-25 20:50:04 +0000435CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
436 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
437 if (I->isMoveAssignmentOperator())
David Blaikie581deb32012-06-06 20:45:41 +0000438 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000439
440 return 0;
441}
442
Douglas Gregor21386642010-09-28 21:55:22 +0000443void CXXRecordDecl::markedVirtualFunctionPure() {
444 // C++ [class.abstract]p2:
445 // A class is abstract if it has at least one pure virtual function.
446 data().Abstract = true;
447}
448
Richard Smith3f5f5582012-06-08 21:09:22 +0000449void CXXRecordDecl::markedConstructorConstexpr(CXXConstructorDecl *CD) {
Richard Smithd3861ce2012-06-10 07:07:24 +0000450 if (!CD->isCopyOrMoveConstructor())
Richard Smith3f5f5582012-06-08 21:09:22 +0000451 data().HasConstexprNonCopyMoveConstructor = true;
452
453 if (CD->isDefaultConstructor())
454 data().HasConstexprDefaultConstructor = true;
455}
456
Douglas Gregor21386642010-09-28 21:55:22 +0000457void CXXRecordDecl::addedMember(Decl *D) {
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +0000458 if (!D->isImplicit() &&
459 !isa<FieldDecl>(D) &&
460 !isa<IndirectFieldDecl>(D) &&
461 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class))
462 data().HasOnlyCMembers = false;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000463
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000464 // Ignore friends and invalid declarations.
465 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000466 return;
467
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000468 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
469 if (FunTmpl)
470 D = FunTmpl->getTemplatedDecl();
471
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000472 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
473 if (Method->isVirtual()) {
474 // C++ [dcl.init.aggr]p1:
475 // An aggregate is an array or a class with [...] no virtual functions.
476 data().Aggregate = false;
477
478 // C++ [class]p4:
479 // A POD-struct is an aggregate class...
480 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000481
482 // Virtual functions make the class non-empty.
483 // FIXME: Standard ref?
484 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000485
486 // C++ [class.virtual]p1:
487 // A class that declares or inherits a virtual function is called a
488 // polymorphic class.
489 data().Polymorphic = true;
490
Sean Hunt023df372011-05-09 18:22:59 +0000491 // C++0x [class.ctor]p5
492 // A default constructor is trivial [...] if:
493 // -- its class has no virtual functions [...]
494 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000495
496 // C++0x [class.copy]p13:
497 // A copy/move constructor for class X is trivial if [...]
498 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000499 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000500 data().HasTrivialMoveConstructor = false;
501
502 // C++0x [class.copy]p27:
503 // A copy/move assignment operator for class X is trivial if [...]
504 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000505 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000506 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000507
Chandler Carrutha8225442011-04-30 09:17:45 +0000508 // C++0x [class]p7:
509 // A standard-layout class is a class that: [...]
510 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000511 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000512 }
513 }
514
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000515 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000516 // Notify that an implicit member was added after the definition
517 // was completed.
518 if (!isBeingDefined())
519 if (ASTMutationListener *L = getASTMutationListener())
520 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000521
Sean Huntffe37fd2011-05-25 20:50:04 +0000522 // If this is a special member function, note that it was added and then
523 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000524 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith61802452011-12-22 02:22:31 +0000525 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000526 data().DeclaredDefaultConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000527 if (Constructor->isConstexpr()) {
528 data().HasConstexprDefaultConstructor = true;
529 data().HasConstexprNonCopyMoveConstructor = true;
530 }
531 } else if (Constructor->isCopyConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000532 data().DeclaredCopyConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000533 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000534 data().DeclaredMoveConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000535 } else
Sean Huntffe37fd2011-05-25 20:50:04 +0000536 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000537 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000538 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000539 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000540 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000541 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
542 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000543 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000544 else if (Method->isMoveAssignmentOperator())
545 data().DeclaredMoveAssignment = true;
546 else
547 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000548 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000549 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000550
Sean Huntffe37fd2011-05-25 20:50:04 +0000551NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000552 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000553 }
554
555 // Handle (user-declared) constructors.
556 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
557 // Note that we have a user-declared constructor.
558 data().UserDeclaredConstructor = true;
559
Richard Smith017ab772011-09-05 02:13:09 +0000560 // Technically, "user-provided" is only defined for special member
561 // functions, but the intent of the standard is clearly that it should apply
562 // to all functions.
563 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000564
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000565 if (Constructor->isDefaultConstructor()) {
566 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000567 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000568 // C++0x [class.ctor]p5:
569 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000570 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000571 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000572 }
Richard Smith61802452011-12-22 02:22:31 +0000573 if (Constructor->isConstexpr()) {
574 data().HasConstexprDefaultConstructor = true;
575 data().HasConstexprNonCopyMoveConstructor = true;
576 }
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000577 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000578
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000579 // Note when we have a user-declared copy or move constructor, which will
580 // suppress the implicit declaration of those constructors.
581 if (!FunTmpl) {
582 if (Constructor->isCopyConstructor()) {
583 data().UserDeclaredCopyConstructor = true;
584 data().DeclaredCopyConstructor = true;
585
586 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000587 // A copy/move constructor for class X is trivial if it is not
588 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000589 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000590 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000591 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000592 data().UserDeclaredMoveConstructor = true;
593 data().DeclaredMoveConstructor = true;
594
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000595 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000596 // A copy/move constructor for class X is trivial if it is not
597 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000598 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000599 data().HasTrivialMoveConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000600 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000601 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000602 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
603 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000604 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000605 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000606 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000607
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000608 // C++ [dcl.init.aggr]p1:
609 // An aggregate is an array or a class with no user-declared
610 // constructors [...].
611 // C++0x [dcl.init.aggr]p1:
612 // An aggregate is an array or a class with no user-provided
613 // constructors [...].
David Blaikie4e4d0842012-03-11 07:00:24 +0000614 if (!getASTContext().getLangOpts().CPlusPlus0x || UserProvided)
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000615 data().Aggregate = false;
616
617 // C++ [class]p4:
618 // A POD-struct is an aggregate class [...]
619 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
620 // type is technically an aggregate in C++0x since it wouldn't be in 03.
621 data().PlainOldData = false;
622
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000623 return;
624 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000625
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000626 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000627 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000628 data().DeclaredDestructor = true;
629 data().UserDeclaredDestructor = true;
Richard Smithdfefb842012-02-25 07:33:38 +0000630 data().HasIrrelevantDestructor = false;
631
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000632 // C++ [class]p4:
633 // A POD-struct is an aggregate class that has [...] no user-defined
634 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000635 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000636 data().PlainOldData = false;
637
Douglas Gregor45fa5602011-11-07 20:56:01 +0000638 // C++11 [class.dtor]p5:
639 // A destructor is trivial if it is not user-provided and if
640 // -- the destructor is not virtual.
Richard Smithd3861ce2012-06-10 07:07:24 +0000641 if (DD->isUserProvided() || DD->isVirtual())
Sean Huntcf34e752011-05-16 22:41:40 +0000642 data().HasTrivialDestructor = false;
Richard Smithd3861ce2012-06-10 07:07:24 +0000643
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000644 return;
645 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000646
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000647 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000648 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000649 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000650 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000651 // A POD-struct is an aggregate class that [...] has no user-defined
652 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000653 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000654 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000655
Sean Huntffe37fd2011-05-25 20:50:04 +0000656 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000657
Sean Huntffe37fd2011-05-25 20:50:04 +0000658 // Suppress the implicit declaration of a copy constructor.
659 data().UserDeclaredCopyAssignment = true;
660 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000661
Sean Huntffe37fd2011-05-25 20:50:04 +0000662 // C++0x [class.copy]p27:
663 // A copy/move assignment operator for class X is trivial if it is
664 // neither user-provided nor deleted [...]
665 if (Method->isUserProvided())
666 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000667
Sean Huntffe37fd2011-05-25 20:50:04 +0000668 return;
669 }
670
671 if (Method->isMoveAssignmentOperator()) {
672 // This is an extension in C++03 mode, but we'll keep consistency by
673 // taking a move assignment operator to induce non-POD-ness
674 data().PlainOldData = false;
675
676 // This is a move assignment operator.
677 data().UserDeclaredMoveAssignment = true;
678 data().DeclaredMoveAssignment = true;
679
680 // C++0x [class.copy]p27:
681 // A copy/move assignment operator for class X is trivial if it is
682 // neither user-provided nor deleted [...]
683 if (Method->isUserProvided())
684 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000685 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000686
Douglas Gregore80622f2010-09-29 04:25:11 +0000687 // Keep the list of conversion functions up-to-date.
688 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
689 // We don't record specializations.
690 if (Conversion->getPrimaryTemplate())
691 return;
692
693 // FIXME: We intentionally don't use the decl's access here because it
694 // hasn't been set yet. That's really just a misdesign in Sema.
695
696 if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000697 if (FunTmpl->getPreviousDecl())
698 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000699 FunTmpl);
700 else
701 data().Conversions.addDecl(FunTmpl);
702 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000703 if (Conversion->getPreviousDecl())
704 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000705 Conversion);
706 else
707 data().Conversions.addDecl(Conversion);
708 }
709 }
710
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000711 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000712 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000713
714 // Handle non-static data members.
715 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000716 // C++ [class.bit]p2:
717 // A declaration for a bit-field that omits the identifier declares an
718 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
719 // initialized.
720 if (Field->isUnnamedBitfield())
721 return;
722
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000723 // C++ [dcl.init.aggr]p1:
724 // An aggregate is an array or a class (clause 9) with [...] no
725 // private or protected non-static data members (clause 11).
726 //
727 // A POD must be an aggregate.
728 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
729 data().Aggregate = false;
730 data().PlainOldData = false;
731 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000732
733 // C++0x [class]p7:
734 // A standard-layout class is a class that:
735 // [...]
736 // -- has the same access control for all non-static data members,
737 switch (D->getAccess()) {
738 case AS_private: data().HasPrivateFields = true; break;
739 case AS_protected: data().HasProtectedFields = true; break;
740 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000741 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000742 };
743 if ((data().HasPrivateFields + data().HasProtectedFields +
744 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000745 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000746
Douglas Gregor2bb11012011-05-13 01:05:07 +0000747 // Keep track of the presence of mutable fields.
748 if (Field->isMutable())
749 data().HasMutableFields = true;
750
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000751 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000752 // A POD struct is a class that is both a trivial class and a
753 // standard-layout class, and has no non-static data members of type
754 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000755 //
756 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
757 // that does not explicitly have no lifetime makes the class a non-POD.
758 // However, we delay setting PlainOldData to false in this case so that
759 // Sema has a chance to diagnostic causes where the same class will be
760 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
761 // In this case, the class will become a non-POD class when we complete
762 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000763 ASTContext &Context = getASTContext();
764 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000765 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000766 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000767 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
768 setHasObjectMember(true);
769 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000770 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000771
Chandler Carrutha8225442011-04-30 09:17:45 +0000772 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000773 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000774
Chandler Carrutha8225442011-04-30 09:17:45 +0000775 // C++0x [class]p7:
776 // A standard-layout class is a class that:
777 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000778 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000779 }
780
Richard Smith86c3ae42012-02-13 03:54:03 +0000781 // Record if this field is the first non-literal or volatile field or base.
782 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000783 data().HasNonLiteralTypeFieldsOrBases = true;
784
Richard Smith7a614d82011-06-11 17:19:42 +0000785 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000786 data().HasInClassInitializer = true;
787
788 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000789 // A default constructor is trivial if [...] no non-static data member
790 // of its class has a brace-or-equal-initializer.
791 data().HasTrivialDefaultConstructor = false;
792
Richard Smithd079abf2012-05-07 01:07:30 +0000793 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000794 // An aggregate is a [...] class with [...] no
795 // brace-or-equal-initializers for non-static data members.
796 data().Aggregate = false;
797
Richard Smithd079abf2012-05-07 01:07:30 +0000798 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000799 // A POD struct is [...] a trivial class.
800 data().PlainOldData = false;
801 }
802
Douglas Gregor85606eb2010-09-28 20:50:54 +0000803 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
804 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
805 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000806 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000807 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000808 // -- for all the non-static data members of its class that are of
809 // class type (or array thereof), each such class has a trivial
810 // default constructor.
811 if (!FieldRec->hasTrivialDefaultConstructor())
812 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000813
814 // C++0x [class.copy]p13:
815 // A copy/move constructor for class X is trivial if [...]
816 // [...]
817 // -- for each non-static data member of X that is of class type (or
818 // an array thereof), the constructor selected to copy/move that
819 // member is trivial;
820 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000821 if (!FieldRec->hasTrivialCopyConstructor())
822 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000823 if (!FieldRec->hasTrivialMoveConstructor())
824 data().HasTrivialMoveConstructor = false;
825
826 // C++0x [class.copy]p27:
827 // A copy/move assignment operator for class X is trivial if [...]
828 // [...]
829 // -- for each non-static data member of X that is of class type (or
830 // an array thereof), the assignment operator selected to
831 // copy/move that member is trivial;
832 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000833 if (!FieldRec->hasTrivialCopyAssignment())
834 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000835 if (!FieldRec->hasTrivialMoveAssignment())
836 data().HasTrivialMoveAssignment = false;
837
Douglas Gregor85606eb2010-09-28 20:50:54 +0000838 if (!FieldRec->hasTrivialDestructor())
839 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000840 if (!FieldRec->hasIrrelevantDestructor())
841 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000842 if (FieldRec->hasObjectMember())
843 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000844
845 // C++0x [class]p7:
846 // A standard-layout class is a class that:
847 // -- has no non-static data members of type non-standard-layout
848 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000849 if (!FieldRec->isStandardLayout())
850 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000851
852 // C++0x [class]p7:
853 // A standard-layout class is a class that:
854 // [...]
855 // -- has no base classes of the same type as the first non-static
856 // data member.
857 // We don't want to expend bits in the state of the record decl
858 // tracking whether this is the first non-static data member so we
859 // cheat a bit and use some of the existing state: the empty bit.
860 // Virtual bases and virtual methods make a class non-empty, but they
861 // also make it non-standard-layout so we needn't check here.
862 // A non-empty base class may leave the class standard-layout, but not
863 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000864 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000865 // data member must come through here with Empty still true, and Empty
866 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000867 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000868 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
869 BE = bases_end();
870 BI != BE; ++BI) {
871 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000872 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000873 break;
874 }
875 }
876 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000877
878 // Keep track of the presence of mutable fields.
879 if (FieldRec->hasMutableFields())
880 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000881
882 // C++11 [class.copy]p13:
883 // If the implicitly-defined constructor would satisfy the
884 // requirements of a constexpr constructor, the implicitly-defined
885 // constructor is constexpr.
886 // C++11 [dcl.constexpr]p4:
887 // -- every constructor involved in initializing non-static data
888 // members [...] shall be a constexpr constructor
889 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000890 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000891 // The standard requires any in-class initializer to be a constant
892 // expression. We consider this to be a defect.
893 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000894 }
Richard Smith61802452011-12-22 02:22:31 +0000895 } else {
896 // Base element type of field is a non-class type.
Richard Smithd3861ce2012-06-10 07:07:24 +0000897 if (!T->isLiteralType() ||
898 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000899 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000900 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000901
902 // C++0x [class]p7:
903 // A standard-layout class is a class that:
904 // [...]
905 // -- either has no non-static data members in the most derived
906 // class and at most one base class with non-static data members,
907 // or has no base classes with non-static data members, and
908 // At this point we know that we have a non-static data member, so the last
909 // clause holds.
910 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000911 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000912
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000913 // If this is not a zero-length bit-field, then the class is not empty.
914 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000915 if (!Field->isBitField() ||
916 (!Field->getBitWidth()->isTypeDependent() &&
917 !Field->getBitWidth()->isValueDependent() &&
918 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000919 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000920 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000921 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000922
923 // Handle using declarations of conversion functions.
924 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
925 if (Shadow->getDeclName().getNameKind()
926 == DeclarationName::CXXConversionFunctionName)
927 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000928}
929
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000930bool CXXRecordDecl::isCLike() const {
931 if (getTagKind() == TTK_Class || !TemplateOrInstantiation.isNull())
932 return false;
933 if (!hasDefinition())
934 return true;
935
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000936 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000937}
938
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000939void CXXRecordDecl::getCaptureFields(
940 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000941 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000942 Captures.clear();
943 ThisCapture = 0;
944
Douglas Gregorda8962a2012-02-13 15:44:47 +0000945 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000946 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000947 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000948 C != CEnd; ++C, ++Field) {
949 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +0000950 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000951 continue;
952 }
953
David Blaikie581deb32012-06-06 20:45:41 +0000954 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000955 }
956}
957
958
John McCallb05b5f32010-03-15 09:07:48 +0000959static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
960 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000961 if (isa<UsingShadowDecl>(Conv))
962 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000963 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
964 T = ConvTemp->getTemplatedDecl()->getResultType();
965 else
966 T = cast<CXXConversionDecl>(Conv)->getConversionType();
967 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000968}
969
John McCallb05b5f32010-03-15 09:07:48 +0000970/// Collect the visible conversions of a base class.
971///
James Dennetta1253502012-06-15 22:28:09 +0000972/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +0000973/// \param InVirtual whether this base class is a virtual base (or a base
974/// of a virtual base)
975/// \param Access the access along the inheritance path to this base
976/// \param ParentHiddenTypes the conversions provided by the inheritors
977/// of this base
978/// \param Output the set to which to add conversions from non-virtual bases
979/// \param VOutput the set to which to add conversions from virtual bases
980/// \param HiddenVBaseCs the set of conversions which were hidden in a
981/// virtual base along some inheritance path
982static void CollectVisibleConversions(ASTContext &Context,
983 CXXRecordDecl *Record,
984 bool InVirtual,
985 AccessSpecifier Access,
986 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
987 UnresolvedSetImpl &Output,
988 UnresolvedSetImpl &VOutput,
989 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
990 // The set of types which have conversions in this class or its
991 // subclasses. As an optimization, we don't copy the derived set
992 // unless it might change.
993 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
994 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
995
996 // Collect the direct conversions and figure out which conversions
997 // will be hidden in the subclasses.
998 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
999 if (!Cs.empty()) {
1000 HiddenTypesBuffer = ParentHiddenTypes;
1001 HiddenTypes = &HiddenTypesBuffer;
1002
1003 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +00001004 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1005 bool Hidden = ParentHiddenTypes.count(ConvType);
1006 if (!Hidden)
1007 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001008
1009 // If this conversion is hidden and we're in a virtual base,
1010 // remember that it's hidden along some inheritance path.
1011 if (Hidden && InVirtual)
1012 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1013
1014 // If this conversion isn't hidden, add it to the appropriate output.
1015 else if (!Hidden) {
1016 AccessSpecifier IAccess
1017 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1018
1019 if (InVirtual)
1020 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001021 else
John McCallb05b5f32010-03-15 09:07:48 +00001022 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001023 }
1024 }
1025 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001026
John McCallb05b5f32010-03-15 09:07:48 +00001027 // Collect information recursively from any base classes.
1028 for (CXXRecordDecl::base_class_iterator
1029 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1030 const RecordType *RT = I->getType()->getAs<RecordType>();
1031 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001032
John McCallb05b5f32010-03-15 09:07:48 +00001033 AccessSpecifier BaseAccess
1034 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1035 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001036
John McCallb05b5f32010-03-15 09:07:48 +00001037 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1038 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1039 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001040 }
John McCallb05b5f32010-03-15 09:07:48 +00001041}
Sebastian Redl9994a342009-10-25 17:03:50 +00001042
John McCallb05b5f32010-03-15 09:07:48 +00001043/// Collect the visible conversions of a class.
1044///
1045/// This would be extremely straightforward if it weren't for virtual
1046/// bases. It might be worth special-casing that, really.
1047static void CollectVisibleConversions(ASTContext &Context,
1048 CXXRecordDecl *Record,
1049 UnresolvedSetImpl &Output) {
1050 // The collection of all conversions in virtual bases that we've
1051 // found. These will be added to the output as long as they don't
1052 // appear in the hidden-conversions set.
1053 UnresolvedSet<8> VBaseCs;
1054
1055 // The set of conversions in virtual bases that we've determined to
1056 // be hidden.
1057 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1058
1059 // The set of types hidden by classes derived from this one.
1060 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1061
1062 // Go ahead and collect the direct conversions and add them to the
1063 // hidden-types set.
1064 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1065 Output.append(Cs.begin(), Cs.end());
1066 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1067 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1068
1069 // Recursively collect conversions from base classes.
1070 for (CXXRecordDecl::base_class_iterator
1071 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1072 const RecordType *RT = I->getType()->getAs<RecordType>();
1073 if (!RT) continue;
1074
1075 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1076 I->isVirtual(), I->getAccessSpecifier(),
1077 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1078 }
1079
1080 // Add any unhidden conversions provided by virtual bases.
1081 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1082 I != E; ++I) {
1083 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1084 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001085 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001086}
1087
1088/// getVisibleConversionFunctions - get all conversion functions visible
1089/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +00001090const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001091 // If root class, all conversions are visible.
1092 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +00001093 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +00001094 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +00001095 if (data().ComputedVisibleConversions)
1096 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +00001097 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001098 data().ComputedVisibleConversions = true;
1099 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001100}
1101
John McCall32daa422010-03-31 01:36:47 +00001102void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1103 // This operation is O(N) but extremely rare. Sema only uses it to
1104 // remove UsingShadowDecls in a class that were followed by a direct
1105 // declaration, e.g.:
1106 // class A : B {
1107 // using B::operator int;
1108 // operator int();
1109 // };
1110 // This is uncommon by itself and even more uncommon in conjunction
1111 // with sufficiently large numbers of directly-declared conversions
1112 // that asymptotic behavior matters.
1113
1114 UnresolvedSetImpl &Convs = *getConversionFunctions();
1115 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1116 if (Convs[I].getDecl() == ConvDecl) {
1117 Convs.erase(I);
1118 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1119 && "conversion was found multiple times in unresolved set");
1120 return;
1121 }
1122 }
1123
1124 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001125}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001126
Douglas Gregorf6b11852009-10-08 15:14:33 +00001127CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001128 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001129 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1130
1131 return 0;
1132}
1133
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001134MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1135 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1136}
1137
Douglas Gregorf6b11852009-10-08 15:14:33 +00001138void
1139CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1140 TemplateSpecializationKind TSK) {
1141 assert(TemplateOrInstantiation.isNull() &&
1142 "Previous template or instantiation?");
1143 assert(!isa<ClassTemplateSpecializationDecl>(this));
1144 TemplateOrInstantiation
1145 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1146}
1147
Anders Carlssonb13e3572009-12-07 06:33:48 +00001148TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1149 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001150 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1151 return Spec->getSpecializationKind();
1152
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001153 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001154 return MSInfo->getTemplateSpecializationKind();
1155
1156 return TSK_Undeclared;
1157}
1158
1159void
1160CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1161 if (ClassTemplateSpecializationDecl *Spec
1162 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1163 Spec->setSpecializationKind(TSK);
1164 return;
1165 }
1166
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001167 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001168 MSInfo->setTemplateSpecializationKind(TSK);
1169 return;
1170 }
1171
David Blaikieb219cfc2011-09-23 05:06:16 +00001172 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001173}
1174
Douglas Gregor1d110e02010-07-01 14:13:13 +00001175CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1176 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001177 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001178
1179 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001180 = Context.DeclarationNames.getCXXDestructorName(
1181 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001182
John McCallc0bf4622010-02-23 00:48:20 +00001183 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001184 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001185 if (I == E)
1186 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001188 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001189 return Dtor;
1190}
1191
Douglas Gregorda2142f2011-02-19 18:51:44 +00001192void CXXRecordDecl::completeDefinition() {
1193 completeDefinition(0);
1194}
1195
1196void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1197 RecordDecl::completeDefinition();
1198
David Blaikie4e4d0842012-03-11 07:00:24 +00001199 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001200 // Objective-C Automatic Reference Counting:
1201 // If a class has a non-static data member of Objective-C pointer
1202 // type (or array thereof), it is a non-POD type and its
1203 // default constructor (if any), copy constructor, copy assignment
1204 // operator, and destructor are non-trivial.
1205 struct DefinitionData &Data = data();
1206 Data.PlainOldData = false;
1207 Data.HasTrivialDefaultConstructor = false;
1208 Data.HasTrivialCopyConstructor = false;
1209 Data.HasTrivialCopyAssignment = false;
1210 Data.HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +00001211 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001212 }
1213
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001214 // If the class may be abstract (but hasn't been marked as such), check for
1215 // any pure final overriders.
1216 if (mayBeAbstract()) {
1217 CXXFinalOverriderMap MyFinalOverriders;
1218 if (!FinalOverriders) {
1219 getFinalOverriders(MyFinalOverriders);
1220 FinalOverriders = &MyFinalOverriders;
1221 }
1222
1223 bool Done = false;
1224 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1225 MEnd = FinalOverriders->end();
1226 M != MEnd && !Done; ++M) {
1227 for (OverridingMethods::iterator SO = M->second.begin(),
1228 SOEnd = M->second.end();
1229 SO != SOEnd && !Done; ++SO) {
1230 assert(SO->second.size() > 0 &&
1231 "All virtual functions have overridding virtual functions");
1232
1233 // C++ [class.abstract]p4:
1234 // A class is abstract if it contains or inherits at least one
1235 // pure virtual function for which the final overrider is pure
1236 // virtual.
1237 if (SO->second.front().Method->isPure()) {
1238 data().Abstract = true;
1239 Done = true;
1240 break;
1241 }
1242 }
1243 }
1244 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001245
1246 // Set access bits correctly on the directly-declared conversions.
1247 for (UnresolvedSetIterator I = data().Conversions.begin(),
1248 E = data().Conversions.end();
1249 I != E; ++I)
1250 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001251}
1252
1253bool CXXRecordDecl::mayBeAbstract() const {
1254 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1255 isDependentContext())
1256 return false;
1257
1258 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1259 BEnd = bases_end();
1260 B != BEnd; ++B) {
1261 CXXRecordDecl *BaseDecl
1262 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1263 if (BaseDecl->isAbstract())
1264 return true;
1265 }
1266
1267 return false;
1268}
1269
David Blaikie99ba9e32011-12-20 02:48:34 +00001270void CXXMethodDecl::anchor() { }
1271
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001272static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1273 const CXXMethodDecl *BaseMD) {
1274 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1275 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1276 const CXXMethodDecl *MD = *I;
1277 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1278 return true;
1279 if (recursivelyOverrides(MD, BaseMD))
1280 return true;
1281 }
1282 return false;
1283}
1284
1285CXXMethodDecl *
1286CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD) {
1287 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1288 return this;
1289
1290 // Lookup doesn't work for destructors, so handle them separately.
1291 if (isa<CXXDestructorDecl>(this)) {
1292 CXXMethodDecl *MD = RD->getDestructor();
Rafael Espindola0713d992012-06-27 17:44:39 +00001293 if (MD && recursivelyOverrides(MD, this))
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001294 return MD;
1295 return NULL;
1296 }
1297
1298 lookup_const_result Candidates = RD->lookup(getDeclName());
1299 for (NamedDecl * const * I = Candidates.first; I != Candidates.second; ++I) {
1300 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1301 if (!MD)
1302 continue;
1303 if (recursivelyOverrides(MD, this))
1304 return MD;
1305 }
1306
1307 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1308 E = RD->bases_end(); I != E; ++I) {
1309 const RecordType *RT = I->getType()->getAs<RecordType>();
1310 if (!RT)
1311 continue;
1312 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1313 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1314 if (T)
1315 return T;
1316 }
1317
1318 return NULL;
1319}
1320
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001321CXXMethodDecl *
1322CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001323 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001324 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001325 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001326 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001327 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001328 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001329 isStatic, SCAsWritten, isInline, isConstexpr,
1330 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001331}
1332
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001333CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1334 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1335 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1336 DeclarationNameInfo(), QualType(),
1337 0, false, SC_None, false, false,
1338 SourceLocation());
1339}
1340
Douglas Gregor90916562009-09-29 18:16:17 +00001341bool CXXMethodDecl::isUsualDeallocationFunction() const {
1342 if (getOverloadedOperator() != OO_Delete &&
1343 getOverloadedOperator() != OO_Array_Delete)
1344 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001345
1346 // C++ [basic.stc.dynamic.deallocation]p2:
1347 // A template instance is never a usual deallocation function,
1348 // regardless of its signature.
1349 if (getPrimaryTemplate())
1350 return false;
1351
Douglas Gregor90916562009-09-29 18:16:17 +00001352 // C++ [basic.stc.dynamic.deallocation]p2:
1353 // If a class T has a member deallocation function named operator delete
1354 // with exactly one parameter, then that function is a usual (non-placement)
1355 // deallocation function. [...]
1356 if (getNumParams() == 1)
1357 return true;
1358
1359 // C++ [basic.stc.dynamic.deallocation]p2:
1360 // [...] If class T does not declare such an operator delete but does
1361 // declare a member deallocation function named operator delete with
1362 // exactly two parameters, the second of which has type std::size_t (18.1),
1363 // then this function is a usual deallocation function.
1364 ASTContext &Context = getASTContext();
1365 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001366 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1367 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001368 return false;
1369
1370 // This function is a usual deallocation function if there are no
1371 // single-parameter deallocation functions of the same kind.
1372 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1373 R.first != R.second; ++R.first) {
1374 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1375 if (FD->getNumParams() == 1)
1376 return false;
1377 }
1378
1379 return true;
1380}
1381
Douglas Gregor06a9f362010-05-01 20:49:11 +00001382bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001383 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001384 // A user-declared copy assignment operator X::operator= is a non-static
1385 // non-template member function of class X with exactly one parameter of
1386 // type X, X&, const X&, volatile X& or const volatile X&.
1387 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1388 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001389 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001390 return false;
1391
1392 QualType ParamType = getParamDecl(0)->getType();
1393 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1394 ParamType = Ref->getPointeeType();
1395
1396 ASTContext &Context = getASTContext();
1397 QualType ClassType
1398 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1399 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1400}
1401
Sean Huntffe37fd2011-05-25 20:50:04 +00001402bool CXXMethodDecl::isMoveAssignmentOperator() const {
1403 // C++0x [class.copy]p19:
1404 // A user-declared move assignment operator X::operator= is a non-static
1405 // non-template member function of class X with exactly one parameter of type
1406 // X&&, const X&&, volatile X&&, or const volatile X&&.
1407 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1408 getPrimaryTemplate() || getDescribedFunctionTemplate())
1409 return false;
1410
1411 QualType ParamType = getParamDecl(0)->getType();
1412 if (!isa<RValueReferenceType>(ParamType))
1413 return false;
1414 ParamType = ParamType->getPointeeType();
1415
1416 ASTContext &Context = getASTContext();
1417 QualType ClassType
1418 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1419 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1420}
1421
Anders Carlsson05eb2442009-05-16 23:58:37 +00001422void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001423 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001424 assert(!MD->getParent()->isDependentContext() &&
1425 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001426 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001427
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001428 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001429}
1430
1431CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001432 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001433 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001434}
1435
1436CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001437 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001438 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001439}
1440
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001441unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001442 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001443 return getASTContext().overridden_methods_size(this);
1444}
1445
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001446QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001447 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1448 // If the member function is declared const, the type of this is const X*,
1449 // if the member function is declared volatile, the type of this is
1450 // volatile X*, and if the member function is declared const volatile,
1451 // the type of this is const volatile X*.
1452
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001453 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001454
John McCall3cb0ebd2010-03-10 03:28:59 +00001455 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001456 ClassTy = C.getQualifiedType(ClassTy,
1457 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001458 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001459}
1460
Eli Friedmand7d7f672009-12-06 20:50:05 +00001461bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001462 // If this function is a template instantiation, look at the template from
1463 // which it was instantiated.
1464 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1465 if (!CheckFn)
1466 CheckFn = this;
1467
Eli Friedmand7d7f672009-12-06 20:50:05 +00001468 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001469 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001470}
1471
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001472bool CXXMethodDecl::isLambdaStaticInvoker() const {
1473 return getParent()->isLambda() &&
1474 getIdentifier() && getIdentifier()->getName() == "__invoke";
1475}
1476
1477
Sean Huntcbb67482011-01-08 20:30:50 +00001478CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1479 TypeSourceInfo *TInfo, bool IsVirtual,
1480 SourceLocation L, Expr *Init,
1481 SourceLocation R,
1482 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001483 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001484 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1485 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001486{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001487}
1488
Sean Huntcbb67482011-01-08 20:30:50 +00001489CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1490 FieldDecl *Member,
1491 SourceLocation MemberLoc,
1492 SourceLocation L, Expr *Init,
1493 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001494 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001495 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001496 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1497{
1498}
1499
Sean Huntcbb67482011-01-08 20:30:50 +00001500CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1501 IndirectFieldDecl *Member,
1502 SourceLocation MemberLoc,
1503 SourceLocation L, Expr *Init,
1504 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001505 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001506 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001507 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001508{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001509}
1510
Sean Huntcbb67482011-01-08 20:30:50 +00001511CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001512 TypeSourceInfo *TInfo,
1513 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001514 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001515 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1516 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001517 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1518{
1519}
1520
1521CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001522 FieldDecl *Member,
1523 SourceLocation MemberLoc,
1524 SourceLocation L, Expr *Init,
1525 SourceLocation R,
1526 VarDecl **Indices,
1527 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001528 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001529 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001530 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001531{
1532 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1533 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1534}
1535
Sean Huntcbb67482011-01-08 20:30:50 +00001536CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1537 FieldDecl *Member,
1538 SourceLocation MemberLoc,
1539 SourceLocation L, Expr *Init,
1540 SourceLocation R,
1541 VarDecl **Indices,
1542 unsigned NumIndices) {
1543 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001544 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001545 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001546 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1547 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001548}
1549
Sean Huntcbb67482011-01-08 20:30:50 +00001550TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001551 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001552 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001553 else
1554 return TypeLoc();
1555}
1556
Sean Huntcbb67482011-01-08 20:30:50 +00001557const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001558 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001559 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001560 else
1561 return 0;
1562}
1563
Sean Huntcbb67482011-01-08 20:30:50 +00001564SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001565 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001566 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001567
1568 if (isInClassMemberInitializer())
1569 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001570
Douglas Gregor76852c22011-11-01 01:16:03 +00001571 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1572 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1573
1574 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001575}
1576
Sean Huntcbb67482011-01-08 20:30:50 +00001577SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001578 if (isInClassMemberInitializer()) {
1579 FieldDecl *D = getAnyMember();
1580 if (Expr *I = D->getInClassInitializer())
1581 return I->getSourceRange();
1582 return SourceRange();
1583 }
1584
Douglas Gregor802ab452009-12-02 22:36:29 +00001585 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001586}
1587
David Blaikie99ba9e32011-12-20 02:48:34 +00001588void CXXConstructorDecl::anchor() { }
1589
Douglas Gregorb48fe382008-10-31 09:07:45 +00001590CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001591CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1592 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1593 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1594 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001595}
1596
1597CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001598CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001599 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001600 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001601 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001602 bool isExplicit, bool isInline,
1603 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001604 assert(NameInfo.getName().getNameKind()
1605 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001606 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001607 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001608 isExplicit, isInline, isImplicitlyDeclared,
1609 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001610}
1611
Douglas Gregor76852c22011-11-01 01:16:03 +00001612CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1613 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1614 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1615 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1616 return Construct->getConstructor();
1617
1618 return 0;
1619}
1620
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001621bool CXXConstructorDecl::isDefaultConstructor() const {
1622 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001623 // A default constructor for a class X is a constructor of class
1624 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001625 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001626 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001627}
1628
Mike Stump1eb44332009-09-09 15:08:12 +00001629bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001630CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001631 return isCopyOrMoveConstructor(TypeQuals) &&
1632 getParamDecl(0)->getType()->isLValueReferenceType();
1633}
1634
1635bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1636 return isCopyOrMoveConstructor(TypeQuals) &&
1637 getParamDecl(0)->getType()->isRValueReferenceType();
1638}
1639
1640/// \brief Determine whether this is a copy or move constructor.
1641bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001642 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001643 // A non-template constructor for class X is a copy constructor
1644 // if its first parameter is of type X&, const X&, volatile X& or
1645 // const volatile X&, and either there are no other parameters
1646 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001647 // C++0x [class.copy]p3:
1648 // A non-template constructor for class X is a move constructor if its
1649 // first parameter is of type X&&, const X&&, volatile X&&, or
1650 // const volatile X&&, and either there are no other parameters or else
1651 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001652 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001653 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001654 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001655 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001656 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001657
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001658 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001659
1660 // Do we have a reference type?
1661 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001662 if (!ParamRefType)
1663 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001664
Douglas Gregorfd476482009-11-13 23:59:09 +00001665 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001666 ASTContext &Context = getASTContext();
1667
Douglas Gregorfd476482009-11-13 23:59:09 +00001668 CanQualType PointeeType
1669 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001670 CanQualType ClassTy
1671 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001672 if (PointeeType.getUnqualifiedType() != ClassTy)
1673 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001674
John McCall0953e762009-09-24 19:53:00 +00001675 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001676
1677 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001678 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001679 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001680}
1681
Anders Carlssonfaccd722009-08-28 16:57:08 +00001682bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001683 // C++ [class.conv.ctor]p1:
1684 // A constructor declared without the function-specifier explicit
1685 // that can be called with a single parameter specifies a
1686 // conversion from the type of its first parameter to the type of
1687 // its class. Such a constructor is called a converting
1688 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001689 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001690 return false;
1691
Mike Stump1eb44332009-09-09 15:08:12 +00001692 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001693 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001694 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001695 (getNumParams() > 1 &&
1696 (getParamDecl(1)->hasDefaultArg() ||
1697 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001698}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001699
Douglas Gregor6493cc52010-11-08 17:16:59 +00001700bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001701 if ((getNumParams() < 1) ||
1702 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1703 (getPrimaryTemplate() == 0) ||
1704 (getDescribedFunctionTemplate() != 0))
1705 return false;
1706
1707 const ParmVarDecl *Param = getParamDecl(0);
1708
1709 ASTContext &Context = getASTContext();
1710 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1711
Douglas Gregor66724ea2009-11-14 01:20:54 +00001712 // Is it the same as our our class type?
1713 CanQualType ClassTy
1714 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1715 if (ParamType.getUnqualifiedType() != ClassTy)
1716 return false;
1717
1718 return true;
1719}
1720
Sebastian Redlf677ea32011-02-05 19:23:19 +00001721const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1722 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001723 method_iterator It = getASTContext().overridden_methods_begin(this);
1724 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001725 return 0;
1726
1727 return cast<CXXConstructorDecl>(*It);
1728}
1729
1730void
1731CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1732 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001733 assert(getASTContext().overridden_methods_size(this) == 0 &&
1734 "Base ctor already set.");
1735 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001736}
1737
David Blaikie99ba9e32011-12-20 02:48:34 +00001738void CXXDestructorDecl::anchor() { }
1739
Douglas Gregor42a552f2008-11-05 20:51:48 +00001740CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001741CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1742 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1743 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001744 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001745}
1746
1747CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001748CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001749 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001750 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001751 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001752 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001753 assert(NameInfo.getName().getNameKind()
1754 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001755 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001756 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001757 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001758}
1759
David Blaikie99ba9e32011-12-20 02:48:34 +00001760void CXXConversionDecl::anchor() { }
1761
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001762CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001763CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1764 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1765 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1766 QualType(), 0, false, false, false,
1767 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001768}
1769
1770CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001771CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001772 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001773 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001774 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001775 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001776 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001777 assert(NameInfo.getName().getNameKind()
1778 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001779 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001780 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001781 isInline, isExplicit, isConstexpr,
1782 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001783}
1784
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001785bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1786 return isImplicit() && getParent()->isLambda() &&
1787 getConversionType()->isBlockPointerType();
1788}
1789
David Blaikie99ba9e32011-12-20 02:48:34 +00001790void LinkageSpecDecl::anchor() { }
1791
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001792LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001793 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001794 SourceLocation ExternLoc,
1795 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001796 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001797 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001798 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001799}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001800
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001801LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1802 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1803 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1804 lang_c, SourceLocation());
1805}
1806
David Blaikie99ba9e32011-12-20 02:48:34 +00001807void UsingDirectiveDecl::anchor() { }
1808
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001809UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1810 SourceLocation L,
1811 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001812 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001813 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001814 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001815 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001816 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1817 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001818 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1819 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001820}
1821
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001822UsingDirectiveDecl *
1823UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1824 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1825 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1826 NestedNameSpecifierLoc(),
1827 SourceLocation(), 0, 0);
1828}
1829
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001830NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1831 if (NamespaceAliasDecl *NA =
1832 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1833 return NA->getNamespace();
1834 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1835}
1836
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001837void NamespaceDecl::anchor() { }
1838
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001839NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1840 SourceLocation StartLoc,
1841 SourceLocation IdLoc, IdentifierInfo *Id,
1842 NamespaceDecl *PrevDecl)
1843 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1844 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1845{
1846 setPreviousDeclaration(PrevDecl);
1847
1848 if (PrevDecl)
1849 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1850}
1851
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001852NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001853 bool Inline, SourceLocation StartLoc,
1854 SourceLocation IdLoc, IdentifierInfo *Id,
1855 NamespaceDecl *PrevDecl) {
1856 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001857}
1858
1859NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1860 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001861 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1862 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001863}
1864
David Blaikie99ba9e32011-12-20 02:48:34 +00001865void NamespaceAliasDecl::anchor() { }
1866
Mike Stump1eb44332009-09-09 15:08:12 +00001867NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001868 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001869 SourceLocation AliasLoc,
1870 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001871 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001872 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001873 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001874 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1875 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001876 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1877 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001878}
1879
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001880NamespaceAliasDecl *
1881NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1882 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1883 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1884 NestedNameSpecifierLoc(),
1885 SourceLocation(), 0);
1886}
1887
David Blaikie99ba9e32011-12-20 02:48:34 +00001888void UsingShadowDecl::anchor() { }
1889
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001890UsingShadowDecl *
1891UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1892 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1893 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1894}
1895
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001896UsingDecl *UsingShadowDecl::getUsingDecl() const {
1897 const UsingShadowDecl *Shadow = this;
1898 while (const UsingShadowDecl *NextShadow =
1899 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1900 Shadow = NextShadow;
1901 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1902}
1903
David Blaikie99ba9e32011-12-20 02:48:34 +00001904void UsingDecl::anchor() { }
1905
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001906void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1907 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1908 "declaration already in set");
1909 assert(S->getUsingDecl() == this);
1910
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001911 if (FirstUsingShadow.getPointer())
1912 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1913 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001914}
1915
1916void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1917 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1918 "declaration not in set");
1919 assert(S->getUsingDecl() == this);
1920
1921 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1922
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001923 if (FirstUsingShadow.getPointer() == S) {
1924 FirstUsingShadow.setPointer(
1925 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001926 S->UsingOrNextShadow = this;
1927 return;
1928 }
1929
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001930 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001931 while (Prev->UsingOrNextShadow != S)
1932 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1933 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1934 S->UsingOrNextShadow = this;
1935}
1936
Douglas Gregordc355712011-02-25 00:36:19 +00001937UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1938 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001939 const DeclarationNameInfo &NameInfo,
1940 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001941 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001942}
1943
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001944UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1945 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1946 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1947 DeclarationNameInfo(), false);
1948}
1949
David Blaikie99ba9e32011-12-20 02:48:34 +00001950void UnresolvedUsingValueDecl::anchor() { }
1951
John McCall7ba107a2009-11-18 02:36:19 +00001952UnresolvedUsingValueDecl *
1953UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1954 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001955 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001956 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001957 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001958 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001959}
1960
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001961UnresolvedUsingValueDecl *
1962UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1963 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1964 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1965 NestedNameSpecifierLoc(),
1966 DeclarationNameInfo());
1967}
1968
David Blaikie99ba9e32011-12-20 02:48:34 +00001969void UnresolvedUsingTypenameDecl::anchor() { }
1970
John McCall7ba107a2009-11-18 02:36:19 +00001971UnresolvedUsingTypenameDecl *
1972UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1973 SourceLocation UsingLoc,
1974 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001975 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001976 SourceLocation TargetNameLoc,
1977 DeclarationName TargetName) {
1978 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001979 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001980 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001981}
1982
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001983UnresolvedUsingTypenameDecl *
1984UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1985 void *Mem = AllocateDeserializedDecl(C, ID,
1986 sizeof(UnresolvedUsingTypenameDecl));
1987 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
1988 SourceLocation(),
1989 NestedNameSpecifierLoc(),
1990 SourceLocation(),
1991 0);
1992}
1993
David Blaikie99ba9e32011-12-20 02:48:34 +00001994void StaticAssertDecl::anchor() { }
1995
Anders Carlssonfb311762009-03-14 00:25:26 +00001996StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001997 SourceLocation StaticAssertLoc,
1998 Expr *AssertExpr,
1999 StringLiteral *Message,
2000 SourceLocation RParenLoc) {
2001 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2002 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00002003}
2004
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002005StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2006 unsigned ID) {
2007 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
2008 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,SourceLocation());
2009}
2010
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002011static const char *getAccessName(AccessSpecifier AS) {
2012 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002013 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002014 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002015 case AS_public:
2016 return "public";
2017 case AS_private:
2018 return "private";
2019 case AS_protected:
2020 return "protected";
2021 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002022 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002023}
2024
2025const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2026 AccessSpecifier AS) {
2027 return DB << getAccessName(AS);
2028}
Richard Smithf15fda02012-02-02 01:16:57 +00002029
2030const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2031 AccessSpecifier AS) {
2032 return DB << getAccessName(AS);
2033}