blob: 0e28a5e7dd76bf3318a3cb6c1588f385cbd53c95 [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///
972/// \param Base a base class of the class we're considering
973/// \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
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001272CXXMethodDecl *
1273CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001274 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001275 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001276 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001277 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001278 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001279 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001280 isStatic, SCAsWritten, isInline, isConstexpr,
1281 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001282}
1283
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001284CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1285 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1286 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1287 DeclarationNameInfo(), QualType(),
1288 0, false, SC_None, false, false,
1289 SourceLocation());
1290}
1291
Douglas Gregor90916562009-09-29 18:16:17 +00001292bool CXXMethodDecl::isUsualDeallocationFunction() const {
1293 if (getOverloadedOperator() != OO_Delete &&
1294 getOverloadedOperator() != OO_Array_Delete)
1295 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001296
1297 // C++ [basic.stc.dynamic.deallocation]p2:
1298 // A template instance is never a usual deallocation function,
1299 // regardless of its signature.
1300 if (getPrimaryTemplate())
1301 return false;
1302
Douglas Gregor90916562009-09-29 18:16:17 +00001303 // C++ [basic.stc.dynamic.deallocation]p2:
1304 // If a class T has a member deallocation function named operator delete
1305 // with exactly one parameter, then that function is a usual (non-placement)
1306 // deallocation function. [...]
1307 if (getNumParams() == 1)
1308 return true;
1309
1310 // C++ [basic.stc.dynamic.deallocation]p2:
1311 // [...] If class T does not declare such an operator delete but does
1312 // declare a member deallocation function named operator delete with
1313 // exactly two parameters, the second of which has type std::size_t (18.1),
1314 // then this function is a usual deallocation function.
1315 ASTContext &Context = getASTContext();
1316 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001317 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1318 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001319 return false;
1320
1321 // This function is a usual deallocation function if there are no
1322 // single-parameter deallocation functions of the same kind.
1323 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1324 R.first != R.second; ++R.first) {
1325 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1326 if (FD->getNumParams() == 1)
1327 return false;
1328 }
1329
1330 return true;
1331}
1332
Douglas Gregor06a9f362010-05-01 20:49:11 +00001333bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001334 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001335 // A user-declared copy assignment operator X::operator= is a non-static
1336 // non-template member function of class X with exactly one parameter of
1337 // type X, X&, const X&, volatile X& or const volatile X&.
1338 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1339 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001340 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001341 return false;
1342
1343 QualType ParamType = getParamDecl(0)->getType();
1344 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1345 ParamType = Ref->getPointeeType();
1346
1347 ASTContext &Context = getASTContext();
1348 QualType ClassType
1349 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1350 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1351}
1352
Sean Huntffe37fd2011-05-25 20:50:04 +00001353bool CXXMethodDecl::isMoveAssignmentOperator() const {
1354 // C++0x [class.copy]p19:
1355 // A user-declared move assignment operator X::operator= is a non-static
1356 // non-template member function of class X with exactly one parameter of type
1357 // X&&, const X&&, volatile X&&, or const volatile X&&.
1358 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1359 getPrimaryTemplate() || getDescribedFunctionTemplate())
1360 return false;
1361
1362 QualType ParamType = getParamDecl(0)->getType();
1363 if (!isa<RValueReferenceType>(ParamType))
1364 return false;
1365 ParamType = ParamType->getPointeeType();
1366
1367 ASTContext &Context = getASTContext();
1368 QualType ClassType
1369 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1370 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1371}
1372
Anders Carlsson05eb2442009-05-16 23:58:37 +00001373void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001374 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001375 assert(!MD->getParent()->isDependentContext() &&
1376 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001377 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001378
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001379 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001380}
1381
1382CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001383 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001384 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001385}
1386
1387CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001388 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001389 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001390}
1391
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001392unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001393 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001394 return getASTContext().overridden_methods_size(this);
1395}
1396
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001397QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001398 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1399 // If the member function is declared const, the type of this is const X*,
1400 // if the member function is declared volatile, the type of this is
1401 // volatile X*, and if the member function is declared const volatile,
1402 // the type of this is const volatile X*.
1403
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001404 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001405
John McCall3cb0ebd2010-03-10 03:28:59 +00001406 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001407 ClassTy = C.getQualifiedType(ClassTy,
1408 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001409 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001410}
1411
Eli Friedmand7d7f672009-12-06 20:50:05 +00001412bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001413 // If this function is a template instantiation, look at the template from
1414 // which it was instantiated.
1415 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1416 if (!CheckFn)
1417 CheckFn = this;
1418
Eli Friedmand7d7f672009-12-06 20:50:05 +00001419 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001420 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001421}
1422
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001423bool CXXMethodDecl::isLambdaStaticInvoker() const {
1424 return getParent()->isLambda() &&
1425 getIdentifier() && getIdentifier()->getName() == "__invoke";
1426}
1427
1428
Sean Huntcbb67482011-01-08 20:30:50 +00001429CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1430 TypeSourceInfo *TInfo, bool IsVirtual,
1431 SourceLocation L, Expr *Init,
1432 SourceLocation R,
1433 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001434 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001435 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1436 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001437{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001438}
1439
Sean Huntcbb67482011-01-08 20:30:50 +00001440CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1441 FieldDecl *Member,
1442 SourceLocation MemberLoc,
1443 SourceLocation L, Expr *Init,
1444 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001445 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001446 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001447 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1448{
1449}
1450
Sean Huntcbb67482011-01-08 20:30:50 +00001451CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1452 IndirectFieldDecl *Member,
1453 SourceLocation MemberLoc,
1454 SourceLocation L, Expr *Init,
1455 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001456 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001457 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001458 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001459{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001460}
1461
Sean Huntcbb67482011-01-08 20:30:50 +00001462CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001463 TypeSourceInfo *TInfo,
1464 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001465 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001466 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1467 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001468 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1469{
1470}
1471
1472CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001473 FieldDecl *Member,
1474 SourceLocation MemberLoc,
1475 SourceLocation L, Expr *Init,
1476 SourceLocation R,
1477 VarDecl **Indices,
1478 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001479 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001480 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001481 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001482{
1483 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1484 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1485}
1486
Sean Huntcbb67482011-01-08 20:30:50 +00001487CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1488 FieldDecl *Member,
1489 SourceLocation MemberLoc,
1490 SourceLocation L, Expr *Init,
1491 SourceLocation R,
1492 VarDecl **Indices,
1493 unsigned NumIndices) {
1494 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001495 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001496 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001497 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1498 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001499}
1500
Sean Huntcbb67482011-01-08 20:30:50 +00001501TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001502 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001503 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001504 else
1505 return TypeLoc();
1506}
1507
Sean Huntcbb67482011-01-08 20:30:50 +00001508const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001509 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001510 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001511 else
1512 return 0;
1513}
1514
Sean Huntcbb67482011-01-08 20:30:50 +00001515SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001516 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001517 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001518
1519 if (isInClassMemberInitializer())
1520 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001521
Douglas Gregor76852c22011-11-01 01:16:03 +00001522 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1523 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1524
1525 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001526}
1527
Sean Huntcbb67482011-01-08 20:30:50 +00001528SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001529 if (isInClassMemberInitializer()) {
1530 FieldDecl *D = getAnyMember();
1531 if (Expr *I = D->getInClassInitializer())
1532 return I->getSourceRange();
1533 return SourceRange();
1534 }
1535
Douglas Gregor802ab452009-12-02 22:36:29 +00001536 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001537}
1538
David Blaikie99ba9e32011-12-20 02:48:34 +00001539void CXXConstructorDecl::anchor() { }
1540
Douglas Gregorb48fe382008-10-31 09:07:45 +00001541CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001542CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1543 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1544 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1545 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001546}
1547
1548CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001549CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001550 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001551 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001552 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001553 bool isExplicit, bool isInline,
1554 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001555 assert(NameInfo.getName().getNameKind()
1556 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001557 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001558 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001559 isExplicit, isInline, isImplicitlyDeclared,
1560 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001561}
1562
Douglas Gregor76852c22011-11-01 01:16:03 +00001563CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1564 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1565 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1566 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1567 return Construct->getConstructor();
1568
1569 return 0;
1570}
1571
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001572bool CXXConstructorDecl::isDefaultConstructor() const {
1573 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001574 // A default constructor for a class X is a constructor of class
1575 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001576 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001577 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001578}
1579
Mike Stump1eb44332009-09-09 15:08:12 +00001580bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001581CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001582 return isCopyOrMoveConstructor(TypeQuals) &&
1583 getParamDecl(0)->getType()->isLValueReferenceType();
1584}
1585
1586bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1587 return isCopyOrMoveConstructor(TypeQuals) &&
1588 getParamDecl(0)->getType()->isRValueReferenceType();
1589}
1590
1591/// \brief Determine whether this is a copy or move constructor.
1592bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001593 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001594 // A non-template constructor for class X is a copy constructor
1595 // if its first parameter is of type X&, const X&, volatile X& or
1596 // const volatile X&, and either there are no other parameters
1597 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001598 // C++0x [class.copy]p3:
1599 // A non-template constructor for class X is a move constructor if its
1600 // first parameter is of type X&&, const X&&, volatile X&&, or
1601 // const volatile X&&, and either there are no other parameters or else
1602 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001603 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001604 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001605 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001606 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001607 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001608
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001609 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001610
1611 // Do we have a reference type?
1612 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001613 if (!ParamRefType)
1614 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001615
Douglas Gregorfd476482009-11-13 23:59:09 +00001616 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001617 ASTContext &Context = getASTContext();
1618
Douglas Gregorfd476482009-11-13 23:59:09 +00001619 CanQualType PointeeType
1620 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001621 CanQualType ClassTy
1622 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001623 if (PointeeType.getUnqualifiedType() != ClassTy)
1624 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001625
John McCall0953e762009-09-24 19:53:00 +00001626 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001627
1628 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001629 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001630 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001631}
1632
Anders Carlssonfaccd722009-08-28 16:57:08 +00001633bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001634 // C++ [class.conv.ctor]p1:
1635 // A constructor declared without the function-specifier explicit
1636 // that can be called with a single parameter specifies a
1637 // conversion from the type of its first parameter to the type of
1638 // its class. Such a constructor is called a converting
1639 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001640 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001641 return false;
1642
Mike Stump1eb44332009-09-09 15:08:12 +00001643 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001644 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001645 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001646 (getNumParams() > 1 &&
1647 (getParamDecl(1)->hasDefaultArg() ||
1648 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001649}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001650
Douglas Gregor6493cc52010-11-08 17:16:59 +00001651bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001652 if ((getNumParams() < 1) ||
1653 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1654 (getPrimaryTemplate() == 0) ||
1655 (getDescribedFunctionTemplate() != 0))
1656 return false;
1657
1658 const ParmVarDecl *Param = getParamDecl(0);
1659
1660 ASTContext &Context = getASTContext();
1661 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1662
Douglas Gregor66724ea2009-11-14 01:20:54 +00001663 // Is it the same as our our class type?
1664 CanQualType ClassTy
1665 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1666 if (ParamType.getUnqualifiedType() != ClassTy)
1667 return false;
1668
1669 return true;
1670}
1671
Sebastian Redlf677ea32011-02-05 19:23:19 +00001672const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1673 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001674 method_iterator It = getASTContext().overridden_methods_begin(this);
1675 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001676 return 0;
1677
1678 return cast<CXXConstructorDecl>(*It);
1679}
1680
1681void
1682CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1683 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001684 assert(getASTContext().overridden_methods_size(this) == 0 &&
1685 "Base ctor already set.");
1686 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001687}
1688
David Blaikie99ba9e32011-12-20 02:48:34 +00001689void CXXDestructorDecl::anchor() { }
1690
Douglas Gregor42a552f2008-11-05 20:51:48 +00001691CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001692CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1693 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1694 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001695 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001696}
1697
1698CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001699CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001700 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001701 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001702 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001703 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001704 assert(NameInfo.getName().getNameKind()
1705 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001706 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001707 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001708 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001709}
1710
David Blaikie99ba9e32011-12-20 02:48:34 +00001711void CXXConversionDecl::anchor() { }
1712
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001713CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001714CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1715 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1716 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1717 QualType(), 0, false, false, false,
1718 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001719}
1720
1721CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001722CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001723 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001724 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001725 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001726 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001727 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001728 assert(NameInfo.getName().getNameKind()
1729 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001730 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001731 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001732 isInline, isExplicit, isConstexpr,
1733 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001734}
1735
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001736bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1737 return isImplicit() && getParent()->isLambda() &&
1738 getConversionType()->isBlockPointerType();
1739}
1740
David Blaikie99ba9e32011-12-20 02:48:34 +00001741void LinkageSpecDecl::anchor() { }
1742
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001743LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001744 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001745 SourceLocation ExternLoc,
1746 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001747 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001748 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001749 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001750}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001751
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001752LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1753 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1754 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1755 lang_c, SourceLocation());
1756}
1757
David Blaikie99ba9e32011-12-20 02:48:34 +00001758void UsingDirectiveDecl::anchor() { }
1759
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001760UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1761 SourceLocation L,
1762 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001763 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001764 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001765 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001766 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001767 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1768 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001769 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1770 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001771}
1772
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001773UsingDirectiveDecl *
1774UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1775 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1776 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1777 NestedNameSpecifierLoc(),
1778 SourceLocation(), 0, 0);
1779}
1780
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001781NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1782 if (NamespaceAliasDecl *NA =
1783 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1784 return NA->getNamespace();
1785 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1786}
1787
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001788void NamespaceDecl::anchor() { }
1789
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001790NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1791 SourceLocation StartLoc,
1792 SourceLocation IdLoc, IdentifierInfo *Id,
1793 NamespaceDecl *PrevDecl)
1794 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1795 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1796{
1797 setPreviousDeclaration(PrevDecl);
1798
1799 if (PrevDecl)
1800 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1801}
1802
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001803NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001804 bool Inline, SourceLocation StartLoc,
1805 SourceLocation IdLoc, IdentifierInfo *Id,
1806 NamespaceDecl *PrevDecl) {
1807 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001808}
1809
1810NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1811 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001812 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1813 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001814}
1815
David Blaikie99ba9e32011-12-20 02:48:34 +00001816void NamespaceAliasDecl::anchor() { }
1817
Mike Stump1eb44332009-09-09 15:08:12 +00001818NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001819 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001820 SourceLocation AliasLoc,
1821 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001822 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001823 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001824 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001825 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1826 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001827 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1828 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001829}
1830
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001831NamespaceAliasDecl *
1832NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1833 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1834 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1835 NestedNameSpecifierLoc(),
1836 SourceLocation(), 0);
1837}
1838
David Blaikie99ba9e32011-12-20 02:48:34 +00001839void UsingShadowDecl::anchor() { }
1840
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001841UsingShadowDecl *
1842UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1843 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1844 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1845}
1846
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001847UsingDecl *UsingShadowDecl::getUsingDecl() const {
1848 const UsingShadowDecl *Shadow = this;
1849 while (const UsingShadowDecl *NextShadow =
1850 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1851 Shadow = NextShadow;
1852 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1853}
1854
David Blaikie99ba9e32011-12-20 02:48:34 +00001855void UsingDecl::anchor() { }
1856
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001857void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1858 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1859 "declaration already in set");
1860 assert(S->getUsingDecl() == this);
1861
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001862 if (FirstUsingShadow.getPointer())
1863 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1864 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001865}
1866
1867void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1868 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1869 "declaration not in set");
1870 assert(S->getUsingDecl() == this);
1871
1872 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1873
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001874 if (FirstUsingShadow.getPointer() == S) {
1875 FirstUsingShadow.setPointer(
1876 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001877 S->UsingOrNextShadow = this;
1878 return;
1879 }
1880
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001881 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001882 while (Prev->UsingOrNextShadow != S)
1883 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1884 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1885 S->UsingOrNextShadow = this;
1886}
1887
Douglas Gregordc355712011-02-25 00:36:19 +00001888UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1889 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001890 const DeclarationNameInfo &NameInfo,
1891 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001892 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001893}
1894
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001895UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1896 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1897 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1898 DeclarationNameInfo(), false);
1899}
1900
David Blaikie99ba9e32011-12-20 02:48:34 +00001901void UnresolvedUsingValueDecl::anchor() { }
1902
John McCall7ba107a2009-11-18 02:36:19 +00001903UnresolvedUsingValueDecl *
1904UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1905 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001906 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001907 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001908 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001909 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001910}
1911
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001912UnresolvedUsingValueDecl *
1913UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1914 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1915 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1916 NestedNameSpecifierLoc(),
1917 DeclarationNameInfo());
1918}
1919
David Blaikie99ba9e32011-12-20 02:48:34 +00001920void UnresolvedUsingTypenameDecl::anchor() { }
1921
John McCall7ba107a2009-11-18 02:36:19 +00001922UnresolvedUsingTypenameDecl *
1923UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1924 SourceLocation UsingLoc,
1925 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001926 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001927 SourceLocation TargetNameLoc,
1928 DeclarationName TargetName) {
1929 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001930 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001931 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001932}
1933
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001934UnresolvedUsingTypenameDecl *
1935UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1936 void *Mem = AllocateDeserializedDecl(C, ID,
1937 sizeof(UnresolvedUsingTypenameDecl));
1938 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
1939 SourceLocation(),
1940 NestedNameSpecifierLoc(),
1941 SourceLocation(),
1942 0);
1943}
1944
David Blaikie99ba9e32011-12-20 02:48:34 +00001945void StaticAssertDecl::anchor() { }
1946
Anders Carlssonfb311762009-03-14 00:25:26 +00001947StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001948 SourceLocation StaticAssertLoc,
1949 Expr *AssertExpr,
1950 StringLiteral *Message,
1951 SourceLocation RParenLoc) {
1952 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1953 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001954}
1955
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001956StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
1957 unsigned ID) {
1958 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
1959 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,SourceLocation());
1960}
1961
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001962static const char *getAccessName(AccessSpecifier AS) {
1963 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001964 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00001965 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001966 case AS_public:
1967 return "public";
1968 case AS_private:
1969 return "private";
1970 case AS_protected:
1971 return "protected";
1972 }
David Blaikie561d3ab2012-01-17 02:30:50 +00001973 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001974}
1975
1976const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1977 AccessSpecifier AS) {
1978 return DB << getAccessName(AS);
1979}
Richard Smithf15fda02012-02-02 01:16:57 +00001980
1981const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
1982 AccessSpecifier AS) {
1983 return DB << getAccessName(AS);
1984}