blob: e27d880ecf9b87111880cd77321db88e5c095f17 [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),
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +000046 HasTrivialDefaultConstructor(true),
Richard Smith61802452011-12-22 02:22:31 +000047 HasConstexprNonCopyMoveConstructor(false),
48 DefaultedDefaultConstructorIsConstexpr(true),
49 DefaultedCopyConstructorIsConstexpr(true),
50 DefaultedMoveConstructorIsConstexpr(true),
51 HasConstexprDefaultConstructor(false), HasConstexprCopyConstructor(false),
52 HasConstexprMoveConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000053 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
54 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
Richard Smithdfefb842012-02-25 07:33:38 +000055 HasIrrelevantDestructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000056 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000057 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000058 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
59 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sebastian Redl85ea7aa2011-08-30 19:58:05 +000060 DeclaredDestructor(false), FailedImplicitMoveConstructor(false),
Eli Friedman72899c32012-01-07 04:59:52 +000061 FailedImplicitMoveAssignment(false), IsLambda(false), NumBases(0),
62 NumVBases(0), Bases(), VBases(), Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000063}
64
65CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000066 SourceLocation StartLoc, SourceLocation IdLoc,
67 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
68 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000069 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000070 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000071
Jay Foad4ba2a172011-01-12 09:06:06 +000072CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000073 DeclContext *DC, SourceLocation StartLoc,
74 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000075 CXXRecordDecl* PrevDecl,
76 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000077 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
78 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000080 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000081 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000082 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000083 return R;
84}
85
Douglas Gregorda8962a2012-02-13 15:44:47 +000086CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Douglas Gregorf4b7de12012-02-21 19:11:17 +000087 SourceLocation Loc, bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +000088 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
89 0, 0);
90 R->IsBeingDefined = true;
Douglas Gregorf4b7de12012-02-21 19:11:17 +000091 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Dependent);
Douglas Gregorda8962a2012-02-13 15:44:47 +000092 C.getTypeDeclType(R, /*PrevDecl=*/0);
93 return R;
94}
95
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000096CXXRecordDecl *
97CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
98 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
99 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
100 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000101}
102
Mike Stump1eb44332009-09-09 15:08:12 +0000103void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000104CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000105 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000106 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000107
Douglas Gregor7c789c12010-10-29 22:39:52 +0000108 if (!data().Bases.isOffset() && data().NumBases > 0)
109 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Richard Smithdd677232011-10-18 20:08:55 +0000111 if (NumBases) {
112 // C++ [dcl.init.aggr]p1:
113 // An aggregate is [...] a class with [...] no base classes [...].
114 data().Aggregate = false;
115
116 // C++ [class]p4:
117 // A POD-struct is an aggregate class...
118 data().PlainOldData = false;
119 }
120
Anders Carlsson6f6de732010-03-29 05:13:12 +0000121 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000122 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000123
124 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000125 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000126
John McCall86ff3082010-02-04 22:26:26 +0000127 data().Bases = new(C) CXXBaseSpecifier [NumBases];
128 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000129 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000130 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000131 // Keep track of inherited vbases for this base class.
132 const CXXBaseSpecifier *Base = Bases[i];
133 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000134 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000135 if (BaseType->isDependentType())
136 continue;
137 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000138 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000139
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000140 // A class with a non-empty base class is not empty.
141 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000142 if (!BaseClassDecl->isEmpty()) {
143 if (!data().Empty) {
144 // C++0x [class]p7:
145 // A standard-layout class is a class that:
146 // [...]
147 // -- either has no non-static data members in the most derived
148 // class and at most one base class with non-static data members,
149 // or has no base classes with non-static data members, and
150 // If this is the second non-empty base, then neither of these two
151 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000152 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000153 }
154
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000155 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000156 data().HasNoNonEmptyBases = false;
157 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000158
Douglas Gregor85606eb2010-09-28 20:50:54 +0000159 // C++ [class.virtual]p1:
160 // A class that declares or inherits a virtual function is called a
161 // polymorphic class.
162 if (BaseClassDecl->isPolymorphic())
163 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000164
Chandler Carrutha8225442011-04-30 09:17:45 +0000165 // C++0x [class]p7:
166 // A standard-layout class is a class that: [...]
167 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000168 if (!BaseClassDecl->isStandardLayout())
169 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000170
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000171 // Record if this base is the first non-literal field or base.
172 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
173 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000174
Anders Carlsson6f6de732010-03-29 05:13:12 +0000175 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000176 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000177 BaseClassDecl->vbases_begin(),
178 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000179 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000180 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000181 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000182 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000183
184 if (Base->isVirtual()) {
185 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000186 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000187 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000188
189 // C++0x [meta.unary.prop] is_empty:
190 // T is a class type, but not a union type, with ... no virtual base
191 // classes
192 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000193
194 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000195 // A default constructor is trivial [...] if:
196 // -- its class has [...] no virtual bases
197 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000198
199 // C++0x [class.copy]p13:
200 // A copy/move constructor for class X is trivial if it is neither
201 // user-provided nor deleted and if
202 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000203 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000204 data().HasTrivialMoveConstructor = false;
205
206 // C++0x [class.copy]p27:
207 // A copy/move assignment operator for class X is trivial if it is
208 // neither user-provided nor deleted and if
209 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000210 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000211 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000212
213 // C++0x [class]p7:
214 // A standard-layout class is a class that: [...]
215 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000216 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000217
218 // C++11 [dcl.constexpr]p4:
219 // In the definition of a constexpr constructor [...]
220 // -- the class shall not have any virtual base classes
221 data().DefaultedDefaultConstructorIsConstexpr = false;
222 data().DefaultedCopyConstructorIsConstexpr = false;
223 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000224 } else {
225 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000226 // A default constructor is trivial [...] if:
227 // -- all the direct base classes of its class have trivial default
228 // constructors.
229 if (!BaseClassDecl->hasTrivialDefaultConstructor())
230 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000231
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000232 // C++0x [class.copy]p13:
233 // A copy/move constructor for class X is trivial if [...]
234 // [...]
235 // -- the constructor selected to copy/move each direct base class
236 // subobject is trivial, and
237 // FIXME: C++0x: We need to only consider the selected constructor
238 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000239 if (!BaseClassDecl->hasTrivialCopyConstructor())
240 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000241 if (!BaseClassDecl->hasTrivialMoveConstructor())
242 data().HasTrivialMoveConstructor = false;
243
244 // C++0x [class.copy]p27:
245 // A copy/move assignment operator for class X is trivial if [...]
246 // [...]
247 // -- the assignment operator selected to copy/move each direct base
248 // class subobject is trivial, and
249 // FIXME: C++0x: We need to only consider the selected operator instead
250 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000251 if (!BaseClassDecl->hasTrivialCopyAssignment())
252 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000253 if (!BaseClassDecl->hasTrivialMoveAssignment())
254 data().HasTrivialMoveAssignment = false;
Richard Smith61802452011-12-22 02:22:31 +0000255
256 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000257 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000258 // requirements of a constexpr constructor, the implicitly-defined
259 // default constructor is constexpr.
260 if (!BaseClassDecl->hasConstexprDefaultConstructor())
261 data().DefaultedDefaultConstructorIsConstexpr = false;
262
263 // C++11 [class.copy]p13:
264 // If the implicitly-defined constructor would satisfy the requirements
265 // of a constexpr constructor, the implicitly-defined constructor is
266 // constexpr.
267 // C++11 [dcl.constexpr]p4:
268 // -- every constructor involved in initializing [...] base class
269 // sub-objects shall be a constexpr constructor
270 if (!BaseClassDecl->hasConstexprCopyConstructor())
271 data().DefaultedCopyConstructorIsConstexpr = false;
272 if (BaseClassDecl->hasDeclaredMoveConstructor() ||
273 BaseClassDecl->needsImplicitMoveConstructor())
274 // FIXME: If the implicit move constructor generated for the base class
275 // would be ill-formed, the implicit move constructor generated for the
276 // derived class calls the base class' copy constructor.
277 data().DefaultedMoveConstructorIsConstexpr &=
Richard Smithde8facc2012-01-11 18:26:05 +0000278 BaseClassDecl->hasConstexprMoveConstructor();
Richard Smith61802452011-12-22 02:22:31 +0000279 else if (!BaseClassDecl->hasConstexprCopyConstructor())
280 data().DefaultedMoveConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000281 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000282
283 // C++ [class.ctor]p3:
284 // A destructor is trivial if all the direct base classes of its class
285 // have trivial destructors.
286 if (!BaseClassDecl->hasTrivialDestructor())
287 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000288
289 if (!BaseClassDecl->hasIrrelevantDestructor())
290 data().HasIrrelevantDestructor = false;
291
John McCallf85e1932011-06-15 23:02:42 +0000292 // A class has an Objective-C object member if... or any of its bases
293 // has an Objective-C object member.
294 if (BaseClassDecl->hasObjectMember())
295 setHasObjectMember(true);
296
Douglas Gregor2bb11012011-05-13 01:05:07 +0000297 // Keep track of the presence of mutable fields.
298 if (BaseClassDecl->hasMutableFields())
299 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000300 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000301
302 if (VBases.empty())
303 return;
304
305 // Create base specifier for any direct or indirect virtual bases.
306 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
307 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000308 for (int I = 0, E = VBases.size(); I != E; ++I)
309 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000310}
311
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000312/// Callback function for CXXRecordDecl::forallBases that acknowledges
313/// that it saw a base class.
314static bool SawBase(const CXXRecordDecl *, void *) {
315 return true;
316}
317
318bool CXXRecordDecl::hasAnyDependentBases() const {
319 if (!isDependentContext())
320 return false;
321
322 return !forallBases(SawBase, 0);
323}
324
Sean Huntffe37fd2011-05-25 20:50:04 +0000325bool CXXRecordDecl::hasConstCopyConstructor() const {
326 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000327}
328
Chandler Carruthb7e95892011-04-23 10:47:28 +0000329bool CXXRecordDecl::isTriviallyCopyable() const {
330 // C++0x [class]p5:
331 // A trivially copyable class is a class that:
332 // -- has no non-trivial copy constructors,
333 if (!hasTrivialCopyConstructor()) return false;
334 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000335 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000336 // -- has no non-trivial copy assignment operators,
337 if (!hasTrivialCopyAssignment()) return false;
338 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000339 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000340 // -- has a trivial destructor.
341 if (!hasTrivialDestructor()) return false;
342
343 return true;
344}
345
Douglas Gregor0d405db2010-07-01 20:59:04 +0000346/// \brief Perform a simplistic form of overload resolution that only considers
347/// cv-qualifiers on a single parameter, and return the best overload candidate
348/// (if there is one).
349static CXXMethodDecl *
350GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000351 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000352 if (Cands.empty())
353 return 0;
354 if (Cands.size() == 1)
355 return Cands[0].first;
356
357 unsigned Best = 0, N = Cands.size();
358 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000359 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000360 Best = I;
361
362 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000363 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000364 return 0;
365
366 return Cands[Best].first;
367}
368
Sean Huntffe37fd2011-05-25 20:50:04 +0000369CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
370 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000371 QualType ClassType
372 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000373 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000374 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000375 Context.getCanonicalType(ClassType));
376 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000377 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000378 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000379 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000380 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000381 // C++ [class.copy]p2:
382 // A non-template constructor for class X is a copy constructor if [...]
383 if (isa<FunctionTemplateDecl>(*Con))
384 continue;
385
Douglas Gregor0d405db2010-07-01 20:59:04 +0000386 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
387 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000388 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
389 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000390 Found.push_back(std::make_pair(
391 const_cast<CXXConstructorDecl *>(Constructor),
392 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000393 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000394 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000395
396 return cast_or_null<CXXConstructorDecl>(
397 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000398}
399
Sean Huntffe37fd2011-05-25 20:50:04 +0000400CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
401 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
402 if (I->isMoveConstructor())
David Blaikie262bc182012-04-30 02:36:29 +0000403 return &*I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000404
405 return 0;
406}
407
Douglas Gregorb87786f2010-07-01 17:48:08 +0000408CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
409 ASTContext &Context = getASTContext();
410 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
411 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
412
Chris Lattner5f9e2722011-07-23 10:55:15 +0000413 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000414 DeclContext::lookup_const_iterator Op, OpEnd;
415 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
416 // C++ [class.copy]p9:
417 // A user-declared copy assignment operator is a non-static non-template
418 // member function of class X with exactly one parameter of type X, X&,
419 // const X&, volatile X& or const volatile X&.
420 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
421 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
422 continue;
423
424 const FunctionProtoType *FnType
425 = Method->getType()->getAs<FunctionProtoType>();
426 assert(FnType && "Overloaded operator has no prototype.");
427 // Don't assert on this; an invalid decl might have been left in the AST.
428 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
429 continue;
430
431 QualType ArgType = FnType->getArgType(0);
432 Qualifiers Quals;
433 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
434 ArgType = Ref->getPointeeType();
435 // If we have a const argument and we have a reference to a non-const,
436 // this function does not match.
437 if (ArgIsConst && !ArgType.isConstQualified())
438 continue;
439
440 Quals = ArgType.getQualifiers();
441 } else {
442 // By-value copy-assignment operators are treated like const X&
443 // copy-assignment operators.
444 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
445 }
446
447 if (!Context.hasSameUnqualifiedType(ArgType, Class))
448 continue;
449
450 // Save this copy-assignment operator. It might be "the one".
451 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
452 }
453
454 // Use a simplistic form of overload resolution to find the candidate.
455 return GetBestOverloadCandidateSimple(Found);
456}
457
Sean Huntffe37fd2011-05-25 20:50:04 +0000458CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
459 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
460 if (I->isMoveAssignmentOperator())
David Blaikie262bc182012-04-30 02:36:29 +0000461 return &*I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000462
463 return 0;
464}
465
Douglas Gregor21386642010-09-28 21:55:22 +0000466void CXXRecordDecl::markedVirtualFunctionPure() {
467 // C++ [class.abstract]p2:
468 // A class is abstract if it has at least one pure virtual function.
469 data().Abstract = true;
470}
471
472void CXXRecordDecl::addedMember(Decl *D) {
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +0000473 if (!D->isImplicit() &&
474 !isa<FieldDecl>(D) &&
475 !isa<IndirectFieldDecl>(D) &&
476 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class))
477 data().HasOnlyCMembers = false;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000478
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000479 // Ignore friends and invalid declarations.
480 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000481 return;
482
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000483 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
484 if (FunTmpl)
485 D = FunTmpl->getTemplatedDecl();
486
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000487 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
488 if (Method->isVirtual()) {
489 // C++ [dcl.init.aggr]p1:
490 // An aggregate is an array or a class with [...] no virtual functions.
491 data().Aggregate = false;
492
493 // C++ [class]p4:
494 // A POD-struct is an aggregate class...
495 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000496
497 // Virtual functions make the class non-empty.
498 // FIXME: Standard ref?
499 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000500
501 // C++ [class.virtual]p1:
502 // A class that declares or inherits a virtual function is called a
503 // polymorphic class.
504 data().Polymorphic = true;
505
Sean Hunt023df372011-05-09 18:22:59 +0000506 // C++0x [class.ctor]p5
507 // A default constructor is trivial [...] if:
508 // -- its class has no virtual functions [...]
509 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000510
511 // C++0x [class.copy]p13:
512 // A copy/move constructor for class X is trivial if [...]
513 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000514 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000515 data().HasTrivialMoveConstructor = false;
516
517 // C++0x [class.copy]p27:
518 // A copy/move assignment operator for class X is trivial if [...]
519 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000520 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000521 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000522
Chandler Carrutha8225442011-04-30 09:17:45 +0000523 // C++0x [class]p7:
524 // A standard-layout class is a class that: [...]
525 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000526 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000527 }
528 }
529
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000530 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000531 // Notify that an implicit member was added after the definition
532 // was completed.
533 if (!isBeingDefined())
534 if (ASTMutationListener *L = getASTMutationListener())
535 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000536
Sean Huntffe37fd2011-05-25 20:50:04 +0000537 // If this is a special member function, note that it was added and then
538 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000539 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith61802452011-12-22 02:22:31 +0000540 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000541 data().DeclaredDefaultConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000542 if (Constructor->isConstexpr()) {
543 data().HasConstexprDefaultConstructor = true;
544 data().HasConstexprNonCopyMoveConstructor = true;
545 }
546 } else if (Constructor->isCopyConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000547 data().DeclaredCopyConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000548 if (Constructor->isConstexpr())
549 data().HasConstexprCopyConstructor = true;
550 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000551 data().DeclaredMoveConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000552 if (Constructor->isConstexpr())
553 data().HasConstexprMoveConstructor = true;
554 } else
Sean Huntffe37fd2011-05-25 20:50:04 +0000555 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000556 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000557 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000558 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000559 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000560 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
561 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000562 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000563 else if (Method->isMoveAssignmentOperator())
564 data().DeclaredMoveAssignment = true;
565 else
566 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000567 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000568 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000569
Sean Huntffe37fd2011-05-25 20:50:04 +0000570NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000571 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000572 }
573
574 // Handle (user-declared) constructors.
575 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
576 // Note that we have a user-declared constructor.
577 data().UserDeclaredConstructor = true;
578
Richard Smith017ab772011-09-05 02:13:09 +0000579 // Technically, "user-provided" is only defined for special member
580 // functions, but the intent of the standard is clearly that it should apply
581 // to all functions.
582 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000583
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000584 if (Constructor->isDefaultConstructor()) {
585 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000586 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000587 // C++0x [class.ctor]p5:
588 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000589 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000590 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000591 }
Richard Smith61802452011-12-22 02:22:31 +0000592 if (Constructor->isConstexpr()) {
593 data().HasConstexprDefaultConstructor = true;
594 data().HasConstexprNonCopyMoveConstructor = true;
595 }
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000596 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000597
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000598 // Note when we have a user-declared copy or move constructor, which will
599 // suppress the implicit declaration of those constructors.
600 if (!FunTmpl) {
601 if (Constructor->isCopyConstructor()) {
602 data().UserDeclaredCopyConstructor = true;
603 data().DeclaredCopyConstructor = true;
604
605 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000606 // A copy/move constructor for class X is trivial if it is not
607 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000608 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000609 data().HasTrivialCopyConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000610
611 if (Constructor->isConstexpr())
612 data().HasConstexprCopyConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000613 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000614 data().UserDeclaredMoveConstructor = true;
615 data().DeclaredMoveConstructor = true;
616
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000617 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000618 // A copy/move constructor for class X is trivial if it is not
619 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000620 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000621 data().HasTrivialMoveConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000622
623 if (Constructor->isConstexpr())
624 data().HasConstexprMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000625 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000626 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000627 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
628 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000629 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000630 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000631 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000632
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000633 // C++ [dcl.init.aggr]p1:
634 // An aggregate is an array or a class with no user-declared
635 // constructors [...].
636 // C++0x [dcl.init.aggr]p1:
637 // An aggregate is an array or a class with no user-provided
638 // constructors [...].
David Blaikie4e4d0842012-03-11 07:00:24 +0000639 if (!getASTContext().getLangOpts().CPlusPlus0x || UserProvided)
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000640 data().Aggregate = false;
641
642 // C++ [class]p4:
643 // A POD-struct is an aggregate class [...]
644 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
645 // type is technically an aggregate in C++0x since it wouldn't be in 03.
646 data().PlainOldData = false;
647
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000648 return;
649 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000650
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000651 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000652 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000653 data().DeclaredDestructor = true;
654 data().UserDeclaredDestructor = true;
Richard Smithdfefb842012-02-25 07:33:38 +0000655 data().HasIrrelevantDestructor = false;
656
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000657 // C++ [class]p4:
658 // A POD-struct is an aggregate class that has [...] no user-defined
659 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000660 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000661 data().PlainOldData = false;
662
Douglas Gregor45fa5602011-11-07 20:56:01 +0000663 // C++11 [class.dtor]p5:
664 // A destructor is trivial if it is not user-provided and if
665 // -- the destructor is not virtual.
Richard Smith61802452011-12-22 02:22:31 +0000666 if (DD->isUserProvided() || DD->isVirtual()) {
Sean Huntcf34e752011-05-16 22:41:40 +0000667 data().HasTrivialDestructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000668 // C++11 [dcl.constexpr]p1:
669 // The constexpr specifier shall be applied only to [...] the
670 // declaration of a static data member of a literal type.
671 // C++11 [basic.types]p10:
672 // A type is a literal type if it is [...] a class type that [...] has
673 // a trivial destructor.
674 data().DefaultedDefaultConstructorIsConstexpr = false;
675 data().DefaultedCopyConstructorIsConstexpr = false;
676 data().DefaultedMoveConstructorIsConstexpr = false;
677 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000678
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000679 return;
680 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000681
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000682 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000683 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000684 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000685 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000686 // A POD-struct is an aggregate class that [...] has no user-defined
687 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000688 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000689 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000690
Sean Huntffe37fd2011-05-25 20:50:04 +0000691 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000692
Sean Huntffe37fd2011-05-25 20:50:04 +0000693 // Suppress the implicit declaration of a copy constructor.
694 data().UserDeclaredCopyAssignment = true;
695 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000696
Sean Huntffe37fd2011-05-25 20:50:04 +0000697 // C++0x [class.copy]p27:
698 // A copy/move assignment operator for class X is trivial if it is
699 // neither user-provided nor deleted [...]
700 if (Method->isUserProvided())
701 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000702
Sean Huntffe37fd2011-05-25 20:50:04 +0000703 return;
704 }
705
706 if (Method->isMoveAssignmentOperator()) {
707 // This is an extension in C++03 mode, but we'll keep consistency by
708 // taking a move assignment operator to induce non-POD-ness
709 data().PlainOldData = false;
710
711 // This is a move assignment operator.
712 data().UserDeclaredMoveAssignment = true;
713 data().DeclaredMoveAssignment = true;
714
715 // C++0x [class.copy]p27:
716 // A copy/move assignment operator for class X is trivial if it is
717 // neither user-provided nor deleted [...]
718 if (Method->isUserProvided())
719 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000720 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000721
Douglas Gregore80622f2010-09-29 04:25:11 +0000722 // Keep the list of conversion functions up-to-date.
723 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
724 // We don't record specializations.
725 if (Conversion->getPrimaryTemplate())
726 return;
727
728 // FIXME: We intentionally don't use the decl's access here because it
729 // hasn't been set yet. That's really just a misdesign in Sema.
730
731 if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000732 if (FunTmpl->getPreviousDecl())
733 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000734 FunTmpl);
735 else
736 data().Conversions.addDecl(FunTmpl);
737 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000738 if (Conversion->getPreviousDecl())
739 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000740 Conversion);
741 else
742 data().Conversions.addDecl(Conversion);
743 }
744 }
745
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000746 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000747 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000748
749 // Handle non-static data members.
750 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000751 // C++ [class.bit]p2:
752 // A declaration for a bit-field that omits the identifier declares an
753 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
754 // initialized.
755 if (Field->isUnnamedBitfield())
756 return;
757
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000758 // C++ [dcl.init.aggr]p1:
759 // An aggregate is an array or a class (clause 9) with [...] no
760 // private or protected non-static data members (clause 11).
761 //
762 // A POD must be an aggregate.
763 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
764 data().Aggregate = false;
765 data().PlainOldData = false;
766 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000767
768 // C++0x [class]p7:
769 // A standard-layout class is a class that:
770 // [...]
771 // -- has the same access control for all non-static data members,
772 switch (D->getAccess()) {
773 case AS_private: data().HasPrivateFields = true; break;
774 case AS_protected: data().HasProtectedFields = true; break;
775 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000776 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000777 };
778 if ((data().HasPrivateFields + data().HasProtectedFields +
779 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000780 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000781
Douglas Gregor2bb11012011-05-13 01:05:07 +0000782 // Keep track of the presence of mutable fields.
783 if (Field->isMutable())
784 data().HasMutableFields = true;
785
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000786 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000787 // A POD struct is a class that is both a trivial class and a
788 // standard-layout class, and has no non-static data members of type
789 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000790 //
791 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
792 // that does not explicitly have no lifetime makes the class a non-POD.
793 // However, we delay setting PlainOldData to false in this case so that
794 // Sema has a chance to diagnostic causes where the same class will be
795 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
796 // In this case, the class will become a non-POD class when we complete
797 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000798 ASTContext &Context = getASTContext();
799 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000800 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000801 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000802 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
803 setHasObjectMember(true);
804 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000805 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000806
Chandler Carrutha8225442011-04-30 09:17:45 +0000807 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000808 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000809
Chandler Carrutha8225442011-04-30 09:17:45 +0000810 // C++0x [class]p7:
811 // A standard-layout class is a class that:
812 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000813 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000814 }
815
Richard Smith86c3ae42012-02-13 03:54:03 +0000816 // Record if this field is the first non-literal or volatile field or base.
817 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000818 data().HasNonLiteralTypeFieldsOrBases = true;
819
Richard Smith7a614d82011-06-11 17:19:42 +0000820 if (Field->hasInClassInitializer()) {
821 // C++0x [class]p5:
822 // A default constructor is trivial if [...] no non-static data member
823 // of its class has a brace-or-equal-initializer.
824 data().HasTrivialDefaultConstructor = false;
825
826 // C++0x [dcl.init.aggr]p1:
827 // An aggregate is a [...] class with [...] no
828 // brace-or-equal-initializers for non-static data members.
829 data().Aggregate = false;
830
831 // C++0x [class]p10:
832 // A POD struct is [...] a trivial class.
833 data().PlainOldData = false;
834 }
835
Douglas Gregor85606eb2010-09-28 20:50:54 +0000836 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
837 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
838 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000839 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000840 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000841 // -- for all the non-static data members of its class that are of
842 // class type (or array thereof), each such class has a trivial
843 // default constructor.
844 if (!FieldRec->hasTrivialDefaultConstructor())
845 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000846
847 // C++0x [class.copy]p13:
848 // A copy/move constructor for class X is trivial if [...]
849 // [...]
850 // -- for each non-static data member of X that is of class type (or
851 // an array thereof), the constructor selected to copy/move that
852 // member is trivial;
853 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000854 if (!FieldRec->hasTrivialCopyConstructor())
855 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000856 if (!FieldRec->hasTrivialMoveConstructor())
857 data().HasTrivialMoveConstructor = false;
858
859 // C++0x [class.copy]p27:
860 // A copy/move assignment operator for class X is trivial if [...]
861 // [...]
862 // -- for each non-static data member of X that is of class type (or
863 // an array thereof), the assignment operator selected to
864 // copy/move that member is trivial;
865 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000866 if (!FieldRec->hasTrivialCopyAssignment())
867 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000868 if (!FieldRec->hasTrivialMoveAssignment())
869 data().HasTrivialMoveAssignment = false;
870
Douglas Gregor85606eb2010-09-28 20:50:54 +0000871 if (!FieldRec->hasTrivialDestructor())
872 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000873 if (!FieldRec->hasIrrelevantDestructor())
874 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000875 if (FieldRec->hasObjectMember())
876 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000877
878 // C++0x [class]p7:
879 // A standard-layout class is a class that:
880 // -- has no non-static data members of type non-standard-layout
881 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000882 if (!FieldRec->isStandardLayout())
883 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000884
885 // C++0x [class]p7:
886 // A standard-layout class is a class that:
887 // [...]
888 // -- has no base classes of the same type as the first non-static
889 // data member.
890 // We don't want to expend bits in the state of the record decl
891 // tracking whether this is the first non-static data member so we
892 // cheat a bit and use some of the existing state: the empty bit.
893 // Virtual bases and virtual methods make a class non-empty, but they
894 // also make it non-standard-layout so we needn't check here.
895 // A non-empty base class may leave the class standard-layout, but not
896 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000897 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000898 // data member must come through here with Empty still true, and Empty
899 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000900 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000901 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
902 BE = bases_end();
903 BI != BE; ++BI) {
904 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000905 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000906 break;
907 }
908 }
909 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000910
911 // Keep track of the presence of mutable fields.
912 if (FieldRec->hasMutableFields())
913 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000914
915 // C++11 [class.copy]p13:
916 // If the implicitly-defined constructor would satisfy the
917 // requirements of a constexpr constructor, the implicitly-defined
918 // constructor is constexpr.
919 // C++11 [dcl.constexpr]p4:
920 // -- every constructor involved in initializing non-static data
921 // members [...] shall be a constexpr constructor
922 if (!Field->hasInClassInitializer() &&
923 !FieldRec->hasConstexprDefaultConstructor())
924 // The standard requires any in-class initializer to be a constant
925 // expression. We consider this to be a defect.
926 data().DefaultedDefaultConstructorIsConstexpr = false;
927
928 if (!FieldRec->hasConstexprCopyConstructor())
929 data().DefaultedCopyConstructorIsConstexpr = false;
930
931 if (FieldRec->hasDeclaredMoveConstructor() ||
932 FieldRec->needsImplicitMoveConstructor())
933 // FIXME: If the implicit move constructor generated for the member's
934 // class would be ill-formed, the implicit move constructor generated
935 // for this class calls the member's copy constructor.
936 data().DefaultedMoveConstructorIsConstexpr &=
937 FieldRec->hasConstexprMoveConstructor();
938 else if (!FieldRec->hasConstexprCopyConstructor())
939 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000940 }
Richard Smith61802452011-12-22 02:22:31 +0000941 } else {
942 // Base element type of field is a non-class type.
943 if (!T->isLiteralType()) {
944 data().DefaultedDefaultConstructorIsConstexpr = false;
945 data().DefaultedCopyConstructorIsConstexpr = false;
946 data().DefaultedMoveConstructorIsConstexpr = false;
947 } else if (!Field->hasInClassInitializer())
948 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000949 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000950
951 // C++0x [class]p7:
952 // A standard-layout class is a class that:
953 // [...]
954 // -- either has no non-static data members in the most derived
955 // class and at most one base class with non-static data members,
956 // or has no base classes with non-static data members, and
957 // At this point we know that we have a non-static data member, so the last
958 // clause holds.
959 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000960 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000961
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000962 // If this is not a zero-length bit-field, then the class is not empty.
963 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000964 if (!Field->isBitField() ||
965 (!Field->getBitWidth()->isTypeDependent() &&
966 !Field->getBitWidth()->isValueDependent() &&
967 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000968 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000969 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000970 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000971
972 // Handle using declarations of conversion functions.
973 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
974 if (Shadow->getDeclName().getNameKind()
975 == DeclarationName::CXXConversionFunctionName)
976 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000977}
978
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000979bool CXXRecordDecl::isCLike() const {
980 if (getTagKind() == TTK_Class || !TemplateOrInstantiation.isNull())
981 return false;
982 if (!hasDefinition())
983 return true;
984
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000985 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000986}
987
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000988void CXXRecordDecl::getCaptureFields(
989 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000990 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000991 Captures.clear();
992 ThisCapture = 0;
993
Douglas Gregorda8962a2012-02-13 15:44:47 +0000994 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000995 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000996 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000997 C != CEnd; ++C, ++Field) {
998 if (C->capturesThis()) {
David Blaikie262bc182012-04-30 02:36:29 +0000999 ThisCapture = &*Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001000 continue;
1001 }
1002
David Blaikie262bc182012-04-30 02:36:29 +00001003 Captures[C->getCapturedVar()] = &*Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001004 }
1005}
1006
1007
John McCallb05b5f32010-03-15 09:07:48 +00001008static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1009 QualType T;
John McCall32daa422010-03-31 01:36:47 +00001010 if (isa<UsingShadowDecl>(Conv))
1011 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +00001012 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
1013 T = ConvTemp->getTemplatedDecl()->getResultType();
1014 else
1015 T = cast<CXXConversionDecl>(Conv)->getConversionType();
1016 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +00001017}
1018
John McCallb05b5f32010-03-15 09:07:48 +00001019/// Collect the visible conversions of a base class.
1020///
1021/// \param Base a base class of the class we're considering
1022/// \param InVirtual whether this base class is a virtual base (or a base
1023/// of a virtual base)
1024/// \param Access the access along the inheritance path to this base
1025/// \param ParentHiddenTypes the conversions provided by the inheritors
1026/// of this base
1027/// \param Output the set to which to add conversions from non-virtual bases
1028/// \param VOutput the set to which to add conversions from virtual bases
1029/// \param HiddenVBaseCs the set of conversions which were hidden in a
1030/// virtual base along some inheritance path
1031static void CollectVisibleConversions(ASTContext &Context,
1032 CXXRecordDecl *Record,
1033 bool InVirtual,
1034 AccessSpecifier Access,
1035 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
1036 UnresolvedSetImpl &Output,
1037 UnresolvedSetImpl &VOutput,
1038 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1039 // The set of types which have conversions in this class or its
1040 // subclasses. As an optimization, we don't copy the derived set
1041 // unless it might change.
1042 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1043 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1044
1045 // Collect the direct conversions and figure out which conversions
1046 // will be hidden in the subclasses.
1047 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1048 if (!Cs.empty()) {
1049 HiddenTypesBuffer = ParentHiddenTypes;
1050 HiddenTypes = &HiddenTypesBuffer;
1051
1052 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +00001053 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1054 bool Hidden = ParentHiddenTypes.count(ConvType);
1055 if (!Hidden)
1056 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001057
1058 // If this conversion is hidden and we're in a virtual base,
1059 // remember that it's hidden along some inheritance path.
1060 if (Hidden && InVirtual)
1061 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1062
1063 // If this conversion isn't hidden, add it to the appropriate output.
1064 else if (!Hidden) {
1065 AccessSpecifier IAccess
1066 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1067
1068 if (InVirtual)
1069 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001070 else
John McCallb05b5f32010-03-15 09:07:48 +00001071 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001072 }
1073 }
1074 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001075
John McCallb05b5f32010-03-15 09:07:48 +00001076 // Collect information recursively from any base classes.
1077 for (CXXRecordDecl::base_class_iterator
1078 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1079 const RecordType *RT = I->getType()->getAs<RecordType>();
1080 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001081
John McCallb05b5f32010-03-15 09:07:48 +00001082 AccessSpecifier BaseAccess
1083 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1084 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001085
John McCallb05b5f32010-03-15 09:07:48 +00001086 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1087 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1088 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001089 }
John McCallb05b5f32010-03-15 09:07:48 +00001090}
Sebastian Redl9994a342009-10-25 17:03:50 +00001091
John McCallb05b5f32010-03-15 09:07:48 +00001092/// Collect the visible conversions of a class.
1093///
1094/// This would be extremely straightforward if it weren't for virtual
1095/// bases. It might be worth special-casing that, really.
1096static void CollectVisibleConversions(ASTContext &Context,
1097 CXXRecordDecl *Record,
1098 UnresolvedSetImpl &Output) {
1099 // The collection of all conversions in virtual bases that we've
1100 // found. These will be added to the output as long as they don't
1101 // appear in the hidden-conversions set.
1102 UnresolvedSet<8> VBaseCs;
1103
1104 // The set of conversions in virtual bases that we've determined to
1105 // be hidden.
1106 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1107
1108 // The set of types hidden by classes derived from this one.
1109 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1110
1111 // Go ahead and collect the direct conversions and add them to the
1112 // hidden-types set.
1113 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1114 Output.append(Cs.begin(), Cs.end());
1115 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1116 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1117
1118 // Recursively collect conversions from base classes.
1119 for (CXXRecordDecl::base_class_iterator
1120 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1121 const RecordType *RT = I->getType()->getAs<RecordType>();
1122 if (!RT) continue;
1123
1124 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1125 I->isVirtual(), I->getAccessSpecifier(),
1126 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1127 }
1128
1129 // Add any unhidden conversions provided by virtual bases.
1130 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1131 I != E; ++I) {
1132 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1133 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001134 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001135}
1136
1137/// getVisibleConversionFunctions - get all conversion functions visible
1138/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +00001139const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001140 // If root class, all conversions are visible.
1141 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +00001142 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +00001143 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +00001144 if (data().ComputedVisibleConversions)
1145 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +00001146 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001147 data().ComputedVisibleConversions = true;
1148 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001149}
1150
John McCall32daa422010-03-31 01:36:47 +00001151void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1152 // This operation is O(N) but extremely rare. Sema only uses it to
1153 // remove UsingShadowDecls in a class that were followed by a direct
1154 // declaration, e.g.:
1155 // class A : B {
1156 // using B::operator int;
1157 // operator int();
1158 // };
1159 // This is uncommon by itself and even more uncommon in conjunction
1160 // with sufficiently large numbers of directly-declared conversions
1161 // that asymptotic behavior matters.
1162
1163 UnresolvedSetImpl &Convs = *getConversionFunctions();
1164 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1165 if (Convs[I].getDecl() == ConvDecl) {
1166 Convs.erase(I);
1167 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1168 && "conversion was found multiple times in unresolved set");
1169 return;
1170 }
1171 }
1172
1173 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001174}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001175
Douglas Gregorf6b11852009-10-08 15:14:33 +00001176CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001177 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001178 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1179
1180 return 0;
1181}
1182
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001183MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1184 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1185}
1186
Douglas Gregorf6b11852009-10-08 15:14:33 +00001187void
1188CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1189 TemplateSpecializationKind TSK) {
1190 assert(TemplateOrInstantiation.isNull() &&
1191 "Previous template or instantiation?");
1192 assert(!isa<ClassTemplateSpecializationDecl>(this));
1193 TemplateOrInstantiation
1194 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1195}
1196
Anders Carlssonb13e3572009-12-07 06:33:48 +00001197TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1198 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001199 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1200 return Spec->getSpecializationKind();
1201
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001202 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001203 return MSInfo->getTemplateSpecializationKind();
1204
1205 return TSK_Undeclared;
1206}
1207
1208void
1209CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1210 if (ClassTemplateSpecializationDecl *Spec
1211 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1212 Spec->setSpecializationKind(TSK);
1213 return;
1214 }
1215
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001216 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001217 MSInfo->setTemplateSpecializationKind(TSK);
1218 return;
1219 }
1220
David Blaikieb219cfc2011-09-23 05:06:16 +00001221 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001222}
1223
Douglas Gregor1d110e02010-07-01 14:13:13 +00001224CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1225 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001226 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001227
1228 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001229 = Context.DeclarationNames.getCXXDestructorName(
1230 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001231
John McCallc0bf4622010-02-23 00:48:20 +00001232 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001233 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001234 if (I == E)
1235 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001237 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001238 return Dtor;
1239}
1240
Douglas Gregorda2142f2011-02-19 18:51:44 +00001241void CXXRecordDecl::completeDefinition() {
1242 completeDefinition(0);
1243}
1244
1245void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1246 RecordDecl::completeDefinition();
1247
David Blaikie4e4d0842012-03-11 07:00:24 +00001248 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001249 // Objective-C Automatic Reference Counting:
1250 // If a class has a non-static data member of Objective-C pointer
1251 // type (or array thereof), it is a non-POD type and its
1252 // default constructor (if any), copy constructor, copy assignment
1253 // operator, and destructor are non-trivial.
1254 struct DefinitionData &Data = data();
1255 Data.PlainOldData = false;
1256 Data.HasTrivialDefaultConstructor = false;
1257 Data.HasTrivialCopyConstructor = false;
1258 Data.HasTrivialCopyAssignment = false;
1259 Data.HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +00001260 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001261 }
1262
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001263 // If the class may be abstract (but hasn't been marked as such), check for
1264 // any pure final overriders.
1265 if (mayBeAbstract()) {
1266 CXXFinalOverriderMap MyFinalOverriders;
1267 if (!FinalOverriders) {
1268 getFinalOverriders(MyFinalOverriders);
1269 FinalOverriders = &MyFinalOverriders;
1270 }
1271
1272 bool Done = false;
1273 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1274 MEnd = FinalOverriders->end();
1275 M != MEnd && !Done; ++M) {
1276 for (OverridingMethods::iterator SO = M->second.begin(),
1277 SOEnd = M->second.end();
1278 SO != SOEnd && !Done; ++SO) {
1279 assert(SO->second.size() > 0 &&
1280 "All virtual functions have overridding virtual functions");
1281
1282 // C++ [class.abstract]p4:
1283 // A class is abstract if it contains or inherits at least one
1284 // pure virtual function for which the final overrider is pure
1285 // virtual.
1286 if (SO->second.front().Method->isPure()) {
1287 data().Abstract = true;
1288 Done = true;
1289 break;
1290 }
1291 }
1292 }
1293 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001294
1295 // Set access bits correctly on the directly-declared conversions.
1296 for (UnresolvedSetIterator I = data().Conversions.begin(),
1297 E = data().Conversions.end();
1298 I != E; ++I)
1299 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001300}
1301
1302bool CXXRecordDecl::mayBeAbstract() const {
1303 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1304 isDependentContext())
1305 return false;
1306
1307 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1308 BEnd = bases_end();
1309 B != BEnd; ++B) {
1310 CXXRecordDecl *BaseDecl
1311 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1312 if (BaseDecl->isAbstract())
1313 return true;
1314 }
1315
1316 return false;
1317}
1318
David Blaikie99ba9e32011-12-20 02:48:34 +00001319void CXXMethodDecl::anchor() { }
1320
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001321CXXMethodDecl *
1322CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001323 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001324 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001325 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001326 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001327 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001328 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001329 isStatic, SCAsWritten, isInline, isConstexpr,
1330 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001331}
1332
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001333CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1334 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1335 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1336 DeclarationNameInfo(), QualType(),
1337 0, false, SC_None, false, false,
1338 SourceLocation());
1339}
1340
Douglas Gregor90916562009-09-29 18:16:17 +00001341bool CXXMethodDecl::isUsualDeallocationFunction() const {
1342 if (getOverloadedOperator() != OO_Delete &&
1343 getOverloadedOperator() != OO_Array_Delete)
1344 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001345
1346 // C++ [basic.stc.dynamic.deallocation]p2:
1347 // A template instance is never a usual deallocation function,
1348 // regardless of its signature.
1349 if (getPrimaryTemplate())
1350 return false;
1351
Douglas Gregor90916562009-09-29 18:16:17 +00001352 // C++ [basic.stc.dynamic.deallocation]p2:
1353 // If a class T has a member deallocation function named operator delete
1354 // with exactly one parameter, then that function is a usual (non-placement)
1355 // deallocation function. [...]
1356 if (getNumParams() == 1)
1357 return true;
1358
1359 // C++ [basic.stc.dynamic.deallocation]p2:
1360 // [...] If class T does not declare such an operator delete but does
1361 // declare a member deallocation function named operator delete with
1362 // exactly two parameters, the second of which has type std::size_t (18.1),
1363 // then this function is a usual deallocation function.
1364 ASTContext &Context = getASTContext();
1365 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001366 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1367 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001368 return false;
1369
1370 // This function is a usual deallocation function if there are no
1371 // single-parameter deallocation functions of the same kind.
1372 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1373 R.first != R.second; ++R.first) {
1374 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1375 if (FD->getNumParams() == 1)
1376 return false;
1377 }
1378
1379 return true;
1380}
1381
Douglas Gregor06a9f362010-05-01 20:49:11 +00001382bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001383 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001384 // A user-declared copy assignment operator X::operator= is a non-static
1385 // non-template member function of class X with exactly one parameter of
1386 // type X, X&, const X&, volatile X& or const volatile X&.
1387 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1388 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001389 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001390 return false;
1391
1392 QualType ParamType = getParamDecl(0)->getType();
1393 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1394 ParamType = Ref->getPointeeType();
1395
1396 ASTContext &Context = getASTContext();
1397 QualType ClassType
1398 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1399 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1400}
1401
Sean Huntffe37fd2011-05-25 20:50:04 +00001402bool CXXMethodDecl::isMoveAssignmentOperator() const {
1403 // C++0x [class.copy]p19:
1404 // A user-declared move assignment operator X::operator= is a non-static
1405 // non-template member function of class X with exactly one parameter of type
1406 // X&&, const X&&, volatile X&&, or const volatile X&&.
1407 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1408 getPrimaryTemplate() || getDescribedFunctionTemplate())
1409 return false;
1410
1411 QualType ParamType = getParamDecl(0)->getType();
1412 if (!isa<RValueReferenceType>(ParamType))
1413 return false;
1414 ParamType = ParamType->getPointeeType();
1415
1416 ASTContext &Context = getASTContext();
1417 QualType ClassType
1418 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1419 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1420}
1421
Anders Carlsson05eb2442009-05-16 23:58:37 +00001422void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001423 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001424 assert(!MD->getParent()->isDependentContext() &&
1425 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001426 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001427
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001428 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001429}
1430
1431CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001432 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001433 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001434}
1435
1436CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001437 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001438 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001439}
1440
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001441unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001442 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001443 return getASTContext().overridden_methods_size(this);
1444}
1445
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001446QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001447 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1448 // If the member function is declared const, the type of this is const X*,
1449 // if the member function is declared volatile, the type of this is
1450 // volatile X*, and if the member function is declared const volatile,
1451 // the type of this is const volatile X*.
1452
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001453 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001454
John McCall3cb0ebd2010-03-10 03:28:59 +00001455 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001456 ClassTy = C.getQualifiedType(ClassTy,
1457 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001458 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001459}
1460
Eli Friedmand7d7f672009-12-06 20:50:05 +00001461bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001462 // If this function is a template instantiation, look at the template from
1463 // which it was instantiated.
1464 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1465 if (!CheckFn)
1466 CheckFn = this;
1467
Eli Friedmand7d7f672009-12-06 20:50:05 +00001468 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001469 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001470}
1471
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001472bool CXXMethodDecl::isLambdaStaticInvoker() const {
1473 return getParent()->isLambda() &&
1474 getIdentifier() && getIdentifier()->getName() == "__invoke";
1475}
1476
1477
Sean Huntcbb67482011-01-08 20:30:50 +00001478CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1479 TypeSourceInfo *TInfo, bool IsVirtual,
1480 SourceLocation L, Expr *Init,
1481 SourceLocation R,
1482 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001483 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001484 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1485 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001486{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001487}
1488
Sean Huntcbb67482011-01-08 20:30:50 +00001489CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1490 FieldDecl *Member,
1491 SourceLocation MemberLoc,
1492 SourceLocation L, Expr *Init,
1493 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001494 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001495 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001496 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1497{
1498}
1499
Sean Huntcbb67482011-01-08 20:30:50 +00001500CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1501 IndirectFieldDecl *Member,
1502 SourceLocation MemberLoc,
1503 SourceLocation L, Expr *Init,
1504 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001505 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001506 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001507 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001508{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001509}
1510
Sean Huntcbb67482011-01-08 20:30:50 +00001511CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001512 TypeSourceInfo *TInfo,
1513 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001514 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001515 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1516 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001517 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1518{
1519}
1520
1521CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001522 FieldDecl *Member,
1523 SourceLocation MemberLoc,
1524 SourceLocation L, Expr *Init,
1525 SourceLocation R,
1526 VarDecl **Indices,
1527 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001528 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001529 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001530 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001531{
1532 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1533 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1534}
1535
Sean Huntcbb67482011-01-08 20:30:50 +00001536CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1537 FieldDecl *Member,
1538 SourceLocation MemberLoc,
1539 SourceLocation L, Expr *Init,
1540 SourceLocation R,
1541 VarDecl **Indices,
1542 unsigned NumIndices) {
1543 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001544 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001545 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001546 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1547 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001548}
1549
Sean Huntcbb67482011-01-08 20:30:50 +00001550TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001551 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001552 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001553 else
1554 return TypeLoc();
1555}
1556
Sean Huntcbb67482011-01-08 20:30:50 +00001557const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001558 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001559 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001560 else
1561 return 0;
1562}
1563
Sean Huntcbb67482011-01-08 20:30:50 +00001564SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001565 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001566 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001567
1568 if (isInClassMemberInitializer())
1569 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001570
Douglas Gregor76852c22011-11-01 01:16:03 +00001571 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1572 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1573
1574 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001575}
1576
Sean Huntcbb67482011-01-08 20:30:50 +00001577SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001578 if (isInClassMemberInitializer()) {
1579 FieldDecl *D = getAnyMember();
1580 if (Expr *I = D->getInClassInitializer())
1581 return I->getSourceRange();
1582 return SourceRange();
1583 }
1584
Douglas Gregor802ab452009-12-02 22:36:29 +00001585 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001586}
1587
David Blaikie99ba9e32011-12-20 02:48:34 +00001588void CXXConstructorDecl::anchor() { }
1589
Douglas Gregorb48fe382008-10-31 09:07:45 +00001590CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001591CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1592 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1593 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1594 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001595}
1596
1597CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001598CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001599 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001600 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001601 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001602 bool isExplicit, bool isInline,
1603 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001604 assert(NameInfo.getName().getNameKind()
1605 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001606 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001607 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001608 isExplicit, isInline, isImplicitlyDeclared,
1609 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001610}
1611
Douglas Gregor76852c22011-11-01 01:16:03 +00001612CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1613 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1614 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1615 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1616 return Construct->getConstructor();
1617
1618 return 0;
1619}
1620
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001621bool CXXConstructorDecl::isDefaultConstructor() const {
1622 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001623 // A default constructor for a class X is a constructor of class
1624 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001625 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001626 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001627}
1628
Mike Stump1eb44332009-09-09 15:08:12 +00001629bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001630CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001631 return isCopyOrMoveConstructor(TypeQuals) &&
1632 getParamDecl(0)->getType()->isLValueReferenceType();
1633}
1634
1635bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1636 return isCopyOrMoveConstructor(TypeQuals) &&
1637 getParamDecl(0)->getType()->isRValueReferenceType();
1638}
1639
1640/// \brief Determine whether this is a copy or move constructor.
1641bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001642 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001643 // A non-template constructor for class X is a copy constructor
1644 // if its first parameter is of type X&, const X&, volatile X& or
1645 // const volatile X&, and either there are no other parameters
1646 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001647 // C++0x [class.copy]p3:
1648 // A non-template constructor for class X is a move constructor if its
1649 // first parameter is of type X&&, const X&&, volatile X&&, or
1650 // const volatile X&&, and either there are no other parameters or else
1651 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001652 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001653 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001654 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001655 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001656 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001657
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001658 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001659
1660 // Do we have a reference type?
1661 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001662 if (!ParamRefType)
1663 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001664
Douglas Gregorfd476482009-11-13 23:59:09 +00001665 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001666 ASTContext &Context = getASTContext();
1667
Douglas Gregorfd476482009-11-13 23:59:09 +00001668 CanQualType PointeeType
1669 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001670 CanQualType ClassTy
1671 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001672 if (PointeeType.getUnqualifiedType() != ClassTy)
1673 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001674
John McCall0953e762009-09-24 19:53:00 +00001675 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001676
1677 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001678 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001679 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001680}
1681
Anders Carlssonfaccd722009-08-28 16:57:08 +00001682bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001683 // C++ [class.conv.ctor]p1:
1684 // A constructor declared without the function-specifier explicit
1685 // that can be called with a single parameter specifies a
1686 // conversion from the type of its first parameter to the type of
1687 // its class. Such a constructor is called a converting
1688 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001689 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001690 return false;
1691
Mike Stump1eb44332009-09-09 15:08:12 +00001692 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001693 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001694 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001695 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001696}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001697
Douglas Gregor6493cc52010-11-08 17:16:59 +00001698bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001699 if ((getNumParams() < 1) ||
1700 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1701 (getPrimaryTemplate() == 0) ||
1702 (getDescribedFunctionTemplate() != 0))
1703 return false;
1704
1705 const ParmVarDecl *Param = getParamDecl(0);
1706
1707 ASTContext &Context = getASTContext();
1708 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1709
Douglas Gregor66724ea2009-11-14 01:20:54 +00001710 // Is it the same as our our class type?
1711 CanQualType ClassTy
1712 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1713 if (ParamType.getUnqualifiedType() != ClassTy)
1714 return false;
1715
1716 return true;
1717}
1718
Sebastian Redlf677ea32011-02-05 19:23:19 +00001719const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1720 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001721 method_iterator It = getASTContext().overridden_methods_begin(this);
1722 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001723 return 0;
1724
1725 return cast<CXXConstructorDecl>(*It);
1726}
1727
1728void
1729CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1730 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001731 assert(getASTContext().overridden_methods_size(this) == 0 &&
1732 "Base ctor already set.");
1733 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001734}
1735
David Blaikie99ba9e32011-12-20 02:48:34 +00001736void CXXDestructorDecl::anchor() { }
1737
Douglas Gregor42a552f2008-11-05 20:51:48 +00001738CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001739CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1740 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1741 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001742 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001743}
1744
1745CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001746CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001747 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001748 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001749 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001750 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001751 assert(NameInfo.getName().getNameKind()
1752 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001753 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001754 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001755 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001756}
1757
David Blaikie99ba9e32011-12-20 02:48:34 +00001758void CXXConversionDecl::anchor() { }
1759
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001760CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001761CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1762 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1763 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1764 QualType(), 0, false, false, false,
1765 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001766}
1767
1768CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001769CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001770 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001771 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001772 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001773 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001774 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001775 assert(NameInfo.getName().getNameKind()
1776 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001777 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001778 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001779 isInline, isExplicit, isConstexpr,
1780 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001781}
1782
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001783bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1784 return isImplicit() && getParent()->isLambda() &&
1785 getConversionType()->isBlockPointerType();
1786}
1787
David Blaikie99ba9e32011-12-20 02:48:34 +00001788void LinkageSpecDecl::anchor() { }
1789
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001790LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001791 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001792 SourceLocation ExternLoc,
1793 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001794 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001795 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001796 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001797}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001798
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001799LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1800 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1801 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1802 lang_c, SourceLocation());
1803}
1804
David Blaikie99ba9e32011-12-20 02:48:34 +00001805void UsingDirectiveDecl::anchor() { }
1806
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001807UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1808 SourceLocation L,
1809 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001810 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001811 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001812 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001813 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001814 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1815 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001816 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1817 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001818}
1819
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001820UsingDirectiveDecl *
1821UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1822 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1823 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1824 NestedNameSpecifierLoc(),
1825 SourceLocation(), 0, 0);
1826}
1827
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001828NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1829 if (NamespaceAliasDecl *NA =
1830 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1831 return NA->getNamespace();
1832 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1833}
1834
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001835void NamespaceDecl::anchor() { }
1836
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001837NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1838 SourceLocation StartLoc,
1839 SourceLocation IdLoc, IdentifierInfo *Id,
1840 NamespaceDecl *PrevDecl)
1841 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1842 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1843{
1844 setPreviousDeclaration(PrevDecl);
1845
1846 if (PrevDecl)
1847 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1848}
1849
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001850NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001851 bool Inline, SourceLocation StartLoc,
1852 SourceLocation IdLoc, IdentifierInfo *Id,
1853 NamespaceDecl *PrevDecl) {
1854 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001855}
1856
1857NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1858 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001859 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1860 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001861}
1862
David Blaikie99ba9e32011-12-20 02:48:34 +00001863void NamespaceAliasDecl::anchor() { }
1864
Mike Stump1eb44332009-09-09 15:08:12 +00001865NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001866 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001867 SourceLocation AliasLoc,
1868 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001869 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001870 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001871 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001872 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1873 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001874 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1875 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001876}
1877
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001878NamespaceAliasDecl *
1879NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1880 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1881 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1882 NestedNameSpecifierLoc(),
1883 SourceLocation(), 0);
1884}
1885
David Blaikie99ba9e32011-12-20 02:48:34 +00001886void UsingShadowDecl::anchor() { }
1887
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001888UsingShadowDecl *
1889UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1890 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1891 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1892}
1893
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001894UsingDecl *UsingShadowDecl::getUsingDecl() const {
1895 const UsingShadowDecl *Shadow = this;
1896 while (const UsingShadowDecl *NextShadow =
1897 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1898 Shadow = NextShadow;
1899 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1900}
1901
David Blaikie99ba9e32011-12-20 02:48:34 +00001902void UsingDecl::anchor() { }
1903
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001904void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1905 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1906 "declaration already in set");
1907 assert(S->getUsingDecl() == this);
1908
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001909 if (FirstUsingShadow.getPointer())
1910 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1911 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001912}
1913
1914void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1915 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1916 "declaration not in set");
1917 assert(S->getUsingDecl() == this);
1918
1919 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1920
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001921 if (FirstUsingShadow.getPointer() == S) {
1922 FirstUsingShadow.setPointer(
1923 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001924 S->UsingOrNextShadow = this;
1925 return;
1926 }
1927
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001928 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001929 while (Prev->UsingOrNextShadow != S)
1930 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1931 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1932 S->UsingOrNextShadow = this;
1933}
1934
Douglas Gregordc355712011-02-25 00:36:19 +00001935UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1936 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001937 const DeclarationNameInfo &NameInfo,
1938 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001939 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001940}
1941
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001942UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1943 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1944 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1945 DeclarationNameInfo(), false);
1946}
1947
David Blaikie99ba9e32011-12-20 02:48:34 +00001948void UnresolvedUsingValueDecl::anchor() { }
1949
John McCall7ba107a2009-11-18 02:36:19 +00001950UnresolvedUsingValueDecl *
1951UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1952 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001953 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001954 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001955 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001956 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001957}
1958
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001959UnresolvedUsingValueDecl *
1960UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1961 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1962 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1963 NestedNameSpecifierLoc(),
1964 DeclarationNameInfo());
1965}
1966
David Blaikie99ba9e32011-12-20 02:48:34 +00001967void UnresolvedUsingTypenameDecl::anchor() { }
1968
John McCall7ba107a2009-11-18 02:36:19 +00001969UnresolvedUsingTypenameDecl *
1970UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1971 SourceLocation UsingLoc,
1972 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001973 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001974 SourceLocation TargetNameLoc,
1975 DeclarationName TargetName) {
1976 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001977 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001978 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001979}
1980
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001981UnresolvedUsingTypenameDecl *
1982UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1983 void *Mem = AllocateDeserializedDecl(C, ID,
1984 sizeof(UnresolvedUsingTypenameDecl));
1985 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
1986 SourceLocation(),
1987 NestedNameSpecifierLoc(),
1988 SourceLocation(),
1989 0);
1990}
1991
David Blaikie99ba9e32011-12-20 02:48:34 +00001992void StaticAssertDecl::anchor() { }
1993
Anders Carlssonfb311762009-03-14 00:25:26 +00001994StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001995 SourceLocation StaticAssertLoc,
1996 Expr *AssertExpr,
1997 StringLiteral *Message,
1998 SourceLocation RParenLoc) {
1999 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2000 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00002001}
2002
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002003StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2004 unsigned ID) {
2005 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
2006 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,SourceLocation());
2007}
2008
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002009static const char *getAccessName(AccessSpecifier AS) {
2010 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002011 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002012 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002013 case AS_public:
2014 return "public";
2015 case AS_private:
2016 return "private";
2017 case AS_protected:
2018 return "protected";
2019 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002020 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002021}
2022
2023const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2024 AccessSpecifier AS) {
2025 return DB << getAccessName(AS);
2026}
Richard Smithf15fda02012-02-02 01:16:57 +00002027
2028const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2029 AccessSpecifier AS) {
2030 return DB << getAccessName(AS);
2031}