blob: a3c0bab8b8af312542ac666668a932c8548719ec [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000017#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000018#include "clang/AST/CXXInheritance.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregor76852c22011-11-01 01:16:03 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000021#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000022#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000023#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Decl Allocation/Deallocation Method Implementations
29//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000030
David Blaikie99ba9e32011-12-20 02:48:34 +000031void AccessSpecDecl::anchor() { }
32
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000033AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
34 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(AccessSpecDecl));
35 return new (Mem) AccessSpecDecl(EmptyShell());
36}
37
John McCall86ff3082010-02-04 22:26:26 +000038CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
39 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000040 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
41 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000042 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000043 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000044 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000045 HasMutableFields(false), HasOnlyCMembers(true),
Richard Smithd079abf2012-05-07 01:07:30 +000046 HasInClassInitializer(false),
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +000047 HasTrivialDefaultConstructor(true),
Richard Smith61802452011-12-22 02:22:31 +000048 HasConstexprNonCopyMoveConstructor(false),
49 DefaultedDefaultConstructorIsConstexpr(true),
Richard Smithd3861ce2012-06-10 07:07:24 +000050 HasConstexprDefaultConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000051 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
52 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
Richard Smithdfefb842012-02-25 07:33:38 +000053 HasIrrelevantDestructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000054 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000055 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000056 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
57 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Richard Smithacf796b2012-11-28 06:23:12 +000058 DeclaredDestructor(false),
59 ImplicitCopyConstructorHasConstParam(true),
60 ImplicitCopyAssignmentHasConstParam(true),
61 HasDeclaredCopyConstructorWithConstParam(false),
62 HasDeclaredCopyAssignmentWithConstParam(false),
63 FailedImplicitMoveConstructor(false), FailedImplicitMoveAssignment(false),
64 IsLambda(false), NumBases(0), NumVBases(0), Bases(), VBases(),
65 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000066}
67
Benjamin Krameree3096a2012-07-04 17:03:33 +000068CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
69 return Bases.get(Definition->getASTContext().getExternalSource());
70}
71
72CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
73 return VBases.get(Definition->getASTContext().getExternalSource());
74}
75
John McCall86ff3082010-02-04 22:26:26 +000076CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000077 SourceLocation StartLoc, SourceLocation IdLoc,
78 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
79 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000080 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000081 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000082
Jay Foad4ba2a172011-01-12 09:06:06 +000083CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000084 DeclContext *DC, SourceLocation StartLoc,
85 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000086 CXXRecordDecl* PrevDecl,
87 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000088 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
89 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000090
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000091 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000092 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000093 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000094 return R;
95}
96
Douglas Gregorda8962a2012-02-13 15:44:47 +000097CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Eli Friedman8da8a662012-09-19 01:18:11 +000098 TypeSourceInfo *Info, SourceLocation Loc,
99 bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +0000100 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
101 0, 0);
102 R->IsBeingDefined = true;
Eli Friedman8da8a662012-09-19 01:18:11 +0000103 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info, Dependent);
Douglas Gregorda8962a2012-02-13 15:44:47 +0000104 C.getTypeDeclType(R, /*PrevDecl=*/0);
105 return R;
106}
107
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000108CXXRecordDecl *
109CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
110 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
111 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
112 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000113}
114
Mike Stump1eb44332009-09-09 15:08:12 +0000115void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000116CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000117 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000118 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000119
Douglas Gregor7c789c12010-10-29 22:39:52 +0000120 if (!data().Bases.isOffset() && data().NumBases > 0)
121 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Richard Smithdd677232011-10-18 20:08:55 +0000123 if (NumBases) {
124 // C++ [dcl.init.aggr]p1:
125 // An aggregate is [...] a class with [...] no base classes [...].
126 data().Aggregate = false;
127
128 // C++ [class]p4:
129 // A POD-struct is an aggregate class...
130 data().PlainOldData = false;
131 }
132
Anders Carlsson6f6de732010-03-29 05:13:12 +0000133 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000134 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000135
136 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000137 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000138
John McCall86ff3082010-02-04 22:26:26 +0000139 data().Bases = new(C) CXXBaseSpecifier [NumBases];
140 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000141 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000142 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000143 // Keep track of inherited vbases for this base class.
144 const CXXBaseSpecifier *Base = Bases[i];
145 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000146 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000147 if (BaseType->isDependentType())
148 continue;
149 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000150 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000151
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000152 // A class with a non-empty base class is not empty.
153 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000154 if (!BaseClassDecl->isEmpty()) {
155 if (!data().Empty) {
156 // C++0x [class]p7:
157 // A standard-layout class is a class that:
158 // [...]
159 // -- either has no non-static data members in the most derived
160 // class and at most one base class with non-static data members,
161 // or has no base classes with non-static data members, and
162 // If this is the second non-empty base, then neither of these two
163 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000164 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000165 }
166
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000167 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000168 data().HasNoNonEmptyBases = false;
169 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000170
Douglas Gregor85606eb2010-09-28 20:50:54 +0000171 // C++ [class.virtual]p1:
172 // A class that declares or inherits a virtual function is called a
173 // polymorphic class.
174 if (BaseClassDecl->isPolymorphic())
175 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000176
Chandler Carrutha8225442011-04-30 09:17:45 +0000177 // C++0x [class]p7:
178 // A standard-layout class is a class that: [...]
179 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000180 if (!BaseClassDecl->isStandardLayout())
181 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000182
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000183 // Record if this base is the first non-literal field or base.
184 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
185 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000186
Anders Carlsson6f6de732010-03-29 05:13:12 +0000187 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000188 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000189 BaseClassDecl->vbases_begin(),
190 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000191 // Add this base if it's not already in the list.
Richard Smithacf796b2012-11-28 06:23:12 +0000192 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000193 VBases.push_back(VBase);
Richard Smithacf796b2012-11-28 06:23:12 +0000194
195 // C++11 [class.copy]p8:
196 // The implicitly-declared copy constructor for a class X will have
197 // the form 'X::X(const X&)' if each [...] virtual base class B of X
198 // has a copy constructor whose first parameter is of type
199 // 'const B&' or 'const volatile B&' [...]
200 if (CXXRecordDecl *VBaseDecl = VBase->getType()->getAsCXXRecordDecl())
201 if (!VBaseDecl->hasCopyConstructorWithConstParam())
202 data().ImplicitCopyConstructorHasConstParam = false;
203 }
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000204 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000205
206 if (Base->isVirtual()) {
207 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000208 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Richard Smithacf796b2012-11-28 06:23:12 +0000209 VBases.push_back(Base);
210
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000211 // C++0x [meta.unary.prop] is_empty:
212 // T is a class type, but not a union type, with ... no virtual base
213 // classes
214 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000215
216 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000217 // A default constructor is trivial [...] if:
218 // -- its class has [...] no virtual bases
219 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000220
221 // C++0x [class.copy]p13:
222 // A copy/move constructor for class X is trivial if it is neither
223 // user-provided nor deleted and if
224 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000225 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000226 data().HasTrivialMoveConstructor = false;
227
228 // C++0x [class.copy]p27:
229 // A copy/move assignment operator for class X is trivial if it is
230 // neither user-provided nor deleted and if
231 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000232 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000233 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000234
235 // C++0x [class]p7:
236 // A standard-layout class is a class that: [...]
237 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000238 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000239
240 // C++11 [dcl.constexpr]p4:
241 // In the definition of a constexpr constructor [...]
242 // -- the class shall not have any virtual base classes
243 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000244 } else {
245 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000246 // A default constructor is trivial [...] if:
247 // -- all the direct base classes of its class have trivial default
248 // constructors.
249 if (!BaseClassDecl->hasTrivialDefaultConstructor())
250 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000251
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000252 // C++0x [class.copy]p13:
253 // A copy/move constructor for class X is trivial if [...]
254 // [...]
255 // -- the constructor selected to copy/move each direct base class
256 // subobject is trivial, and
257 // FIXME: C++0x: We need to only consider the selected constructor
Richard Smith93af2b82012-11-14 07:36:28 +0000258 // instead of all of them. For now, we treat a move constructor as being
259 // non-trivial if it calls anything other than a trivial move constructor.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000260 if (!BaseClassDecl->hasTrivialCopyConstructor())
261 data().HasTrivialCopyConstructor = false;
Richard Smith426391c2012-11-16 00:53:38 +0000262 if (!BaseClassDecl->hasTrivialMoveConstructor())
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000263 data().HasTrivialMoveConstructor = false;
264
265 // C++0x [class.copy]p27:
266 // A copy/move assignment operator for class X is trivial if [...]
267 // [...]
268 // -- the assignment operator selected to copy/move each direct base
269 // class subobject is trivial, and
270 // FIXME: C++0x: We need to only consider the selected operator instead
271 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000272 if (!BaseClassDecl->hasTrivialCopyAssignment())
273 data().HasTrivialCopyAssignment = false;
Richard Smith426391c2012-11-16 00:53:38 +0000274 if (!BaseClassDecl->hasTrivialMoveAssignment())
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000275 data().HasTrivialMoveAssignment = false;
Richard Smith61802452011-12-22 02:22:31 +0000276
277 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000278 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000279 // requirements of a constexpr constructor, the implicitly-defined
280 // default constructor is constexpr.
281 if (!BaseClassDecl->hasConstexprDefaultConstructor())
282 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000283 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000284
285 // C++ [class.ctor]p3:
286 // A destructor is trivial if all the direct base classes of its class
287 // have trivial destructors.
288 if (!BaseClassDecl->hasTrivialDestructor())
289 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000290
291 if (!BaseClassDecl->hasIrrelevantDestructor())
292 data().HasIrrelevantDestructor = false;
293
Richard Smithacf796b2012-11-28 06:23:12 +0000294 // C++11 [class.copy]p18:
295 // The implicitly-declared copy assignment oeprator for a class X will
296 // have the form 'X& X::operator=(const X&)' if each direct base class B
297 // of X has a copy assignment operator whose parameter is of type 'const
298 // B&', 'const volatile B&', or 'B' [...]
299 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
300 data().ImplicitCopyAssignmentHasConstParam = false;
301
302 // C++11 [class.copy]p8:
303 // The implicitly-declared copy constructor for a class X will have
304 // the form 'X::X(const X&)' if each direct [...] base class B of X
305 // has a copy constructor whose first parameter is of type
306 // 'const B&' or 'const volatile B&' [...]
307 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
308 data().ImplicitCopyConstructorHasConstParam = false;
309
John McCallf85e1932011-06-15 23:02:42 +0000310 // A class has an Objective-C object member if... or any of its bases
311 // has an Objective-C object member.
312 if (BaseClassDecl->hasObjectMember())
313 setHasObjectMember(true);
314
Douglas Gregor2bb11012011-05-13 01:05:07 +0000315 // Keep track of the presence of mutable fields.
316 if (BaseClassDecl->hasMutableFields())
317 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000318 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000319
320 if (VBases.empty())
321 return;
322
323 // Create base specifier for any direct or indirect virtual bases.
324 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
325 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000326 for (int I = 0, E = VBases.size(); I != E; ++I)
327 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000328}
329
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000330/// Callback function for CXXRecordDecl::forallBases that acknowledges
331/// that it saw a base class.
332static bool SawBase(const CXXRecordDecl *, void *) {
333 return true;
334}
335
336bool CXXRecordDecl::hasAnyDependentBases() const {
337 if (!isDependentContext())
338 return false;
339
340 return !forallBases(SawBase, 0);
341}
342
Sean Huntffe37fd2011-05-25 20:50:04 +0000343bool CXXRecordDecl::hasConstCopyConstructor() const {
344 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000345}
346
Chandler Carruthb7e95892011-04-23 10:47:28 +0000347bool CXXRecordDecl::isTriviallyCopyable() const {
348 // C++0x [class]p5:
349 // A trivially copyable class is a class that:
350 // -- has no non-trivial copy constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000351 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000352 // -- has no non-trivial move constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000353 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000354 // -- has no non-trivial copy assignment operators,
Richard Smith426391c2012-11-16 00:53:38 +0000355 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000356 // -- has no non-trivial move assignment operators, and
Richard Smith426391c2012-11-16 00:53:38 +0000357 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000358 // -- has a trivial destructor.
359 if (!hasTrivialDestructor()) return false;
360
361 return true;
362}
363
Douglas Gregor0d405db2010-07-01 20:59:04 +0000364/// \brief Perform a simplistic form of overload resolution that only considers
365/// cv-qualifiers on a single parameter, and return the best overload candidate
366/// (if there is one).
367static CXXMethodDecl *
368GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000369 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000370 if (Cands.empty())
371 return 0;
372 if (Cands.size() == 1)
373 return Cands[0].first;
374
375 unsigned Best = 0, N = Cands.size();
376 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000377 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000378 Best = I;
379
Benjamin Kramer2cd7f412012-07-30 15:53:26 +0000380 for (unsigned I = 0; I != N; ++I)
381 if (I != Best && Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000382 return 0;
383
384 return Cands[Best].first;
385}
386
Sean Huntffe37fd2011-05-25 20:50:04 +0000387CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
388 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000389 QualType ClassType
390 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000391 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000392 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000393 Context.getCanonicalType(ClassType));
394 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000395 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000396 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000397 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000398 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000399 // C++ [class.copy]p2:
400 // A non-template constructor for class X is a copy constructor if [...]
401 if (isa<FunctionTemplateDecl>(*Con))
402 continue;
403
Douglas Gregor0d405db2010-07-01 20:59:04 +0000404 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
405 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000406 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
407 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000408 Found.push_back(std::make_pair(
409 const_cast<CXXConstructorDecl *>(Constructor),
410 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000411 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000412 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000413
414 return cast_or_null<CXXConstructorDecl>(
415 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000416}
417
Sean Huntffe37fd2011-05-25 20:50:04 +0000418CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
419 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
420 if (I->isMoveConstructor())
David Blaikie581deb32012-06-06 20:45:41 +0000421 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000422
423 return 0;
424}
425
Douglas Gregorb87786f2010-07-01 17:48:08 +0000426CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
427 ASTContext &Context = getASTContext();
428 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
429 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
430
Chris Lattner5f9e2722011-07-23 10:55:15 +0000431 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000432 DeclContext::lookup_const_iterator Op, OpEnd;
433 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
434 // C++ [class.copy]p9:
435 // A user-declared copy assignment operator is a non-static non-template
436 // member function of class X with exactly one parameter of type X, X&,
437 // const X&, volatile X& or const volatile X&.
438 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
439 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
440 continue;
441
442 const FunctionProtoType *FnType
443 = Method->getType()->getAs<FunctionProtoType>();
444 assert(FnType && "Overloaded operator has no prototype.");
445 // Don't assert on this; an invalid decl might have been left in the AST.
446 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
447 continue;
448
449 QualType ArgType = FnType->getArgType(0);
450 Qualifiers Quals;
451 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
452 ArgType = Ref->getPointeeType();
453 // If we have a const argument and we have a reference to a non-const,
454 // this function does not match.
455 if (ArgIsConst && !ArgType.isConstQualified())
456 continue;
457
458 Quals = ArgType.getQualifiers();
459 } else {
460 // By-value copy-assignment operators are treated like const X&
461 // copy-assignment operators.
462 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
463 }
464
465 if (!Context.hasSameUnqualifiedType(ArgType, Class))
466 continue;
467
468 // Save this copy-assignment operator. It might be "the one".
469 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
470 }
471
472 // Use a simplistic form of overload resolution to find the candidate.
473 return GetBestOverloadCandidateSimple(Found);
474}
475
Sean Huntffe37fd2011-05-25 20:50:04 +0000476CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
477 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
478 if (I->isMoveAssignmentOperator())
David Blaikie581deb32012-06-06 20:45:41 +0000479 return *I;
Sean Huntffe37fd2011-05-25 20:50:04 +0000480
481 return 0;
482}
483
Douglas Gregor21386642010-09-28 21:55:22 +0000484void CXXRecordDecl::markedVirtualFunctionPure() {
485 // C++ [class.abstract]p2:
486 // A class is abstract if it has at least one pure virtual function.
487 data().Abstract = true;
488}
489
Richard Smith3f5f5582012-06-08 21:09:22 +0000490void CXXRecordDecl::markedConstructorConstexpr(CXXConstructorDecl *CD) {
Richard Smithd3861ce2012-06-10 07:07:24 +0000491 if (!CD->isCopyOrMoveConstructor())
Richard Smith3f5f5582012-06-08 21:09:22 +0000492 data().HasConstexprNonCopyMoveConstructor = true;
493
494 if (CD->isDefaultConstructor())
495 data().HasConstexprDefaultConstructor = true;
496}
497
Douglas Gregor21386642010-09-28 21:55:22 +0000498void CXXRecordDecl::addedMember(Decl *D) {
Joao Matos17d35c32012-08-31 22:18:20 +0000499 if (!D->isImplicit() &&
500 !isa<FieldDecl>(D) &&
501 !isa<IndirectFieldDecl>(D) &&
502 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
503 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
504 data().HasOnlyCMembers = false;
505
506 // Ignore friends and invalid declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000507 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000508 return;
509
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000510 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
511 if (FunTmpl)
512 D = FunTmpl->getTemplatedDecl();
513
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000514 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
515 if (Method->isVirtual()) {
516 // C++ [dcl.init.aggr]p1:
517 // An aggregate is an array or a class with [...] no virtual functions.
518 data().Aggregate = false;
519
520 // C++ [class]p4:
521 // A POD-struct is an aggregate class...
522 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000523
524 // Virtual functions make the class non-empty.
525 // FIXME: Standard ref?
526 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000527
528 // C++ [class.virtual]p1:
529 // A class that declares or inherits a virtual function is called a
530 // polymorphic class.
531 data().Polymorphic = true;
532
Sean Hunt023df372011-05-09 18:22:59 +0000533 // C++0x [class.ctor]p5
534 // A default constructor is trivial [...] if:
535 // -- its class has no virtual functions [...]
536 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000537
538 // C++0x [class.copy]p13:
539 // A copy/move constructor for class X is trivial if [...]
540 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000541 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000542 data().HasTrivialMoveConstructor = false;
543
544 // C++0x [class.copy]p27:
545 // A copy/move assignment operator for class X is trivial if [...]
546 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000547 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000548 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000549
Chandler Carrutha8225442011-04-30 09:17:45 +0000550 // C++0x [class]p7:
551 // A standard-layout class is a class that: [...]
552 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000553 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000554 }
555 }
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000556
Richard Smithacf796b2012-11-28 06:23:12 +0000557 // Notify the listener if an implicit member was added after the definition
558 // was completed.
559 if (!isBeingDefined() && D->isImplicit())
560 if (ASTMutationListener *L = getASTMutationListener())
561 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000562
Richard Smithacf796b2012-11-28 06:23:12 +0000563 // Handle constructors.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000564 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smithacf796b2012-11-28 06:23:12 +0000565 if (!Constructor->isImplicit()) {
566 // Note that we have a user-declared constructor.
567 data().UserDeclaredConstructor = true;
568
569 // C++ [class]p4:
570 // A POD-struct is an aggregate class [...]
571 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
572 // type is technically an aggregate in C++0x since it wouldn't be in 03.
573 data().PlainOldData = false;
574 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000575
Richard Smith017ab772011-09-05 02:13:09 +0000576 // Technically, "user-provided" is only defined for special member
577 // functions, but the intent of the standard is clearly that it should apply
578 // to all functions.
579 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000580
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000581 if (Constructor->isDefaultConstructor()) {
582 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000583 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000584 // C++0x [class.ctor]p5:
585 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000586 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000587 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000588 }
Richard Smithacf796b2012-11-28 06:23:12 +0000589 if (Constructor->isConstexpr())
Richard Smith61802452011-12-22 02:22:31 +0000590 data().HasConstexprDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000591 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000592
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000593 // Note when we have a user-declared copy or move constructor, which will
594 // suppress the implicit declaration of those constructors.
595 if (!FunTmpl) {
Richard Smithacf796b2012-11-28 06:23:12 +0000596 unsigned Quals;
597 if (Constructor->isCopyConstructor(Quals)) {
598 if (!Constructor->isImplicit())
599 data().UserDeclaredCopyConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000600 data().DeclaredCopyConstructor = true;
601
602 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000603 // A copy/move constructor for class X is trivial if it is not
604 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000605 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000606 data().HasTrivialCopyConstructor = false;
Richard Smithacf796b2012-11-28 06:23:12 +0000607
608 if (Quals & Qualifiers::Const)
609 data().HasDeclaredCopyConstructorWithConstParam = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000610 } else if (Constructor->isMoveConstructor()) {
Richard Smithacf796b2012-11-28 06:23:12 +0000611 if (!Constructor->isImplicit())
612 data().UserDeclaredMoveConstructor = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000613 data().DeclaredMoveConstructor = true;
614
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000615 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000616 // A copy/move constructor for class X is trivial if it is not
617 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000618 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000619 data().HasTrivialMoveConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000620 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000621 }
Richard Smithacf796b2012-11-28 06:23:12 +0000622
623 // Record if we see any constexpr constructors which are neither copy
624 // nor move constructors.
625 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith6b8bc072011-08-10 18:11:37 +0000626 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000627
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000628 // C++ [dcl.init.aggr]p1:
629 // An aggregate is an array or a class with no user-declared
630 // constructors [...].
631 // C++0x [dcl.init.aggr]p1:
632 // An aggregate is an array or a class with no user-provided
633 // constructors [...].
Richard Smithacf796b2012-11-28 06:23:12 +0000634 if (getASTContext().getLangOpts().CPlusPlus0x
635 ? UserProvided : !Constructor->isImplicit())
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000636 data().Aggregate = false;
637
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000638 return;
639 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000640
Richard Smithacf796b2012-11-28 06:23:12 +0000641 // Handle destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000642 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000643 data().DeclaredDestructor = true;
Richard Smithdfefb842012-02-25 07:33:38 +0000644
Richard Smithacf796b2012-11-28 06:23:12 +0000645 if (!DD->isImplicit()) {
646 data().UserDeclaredDestructor = true;
647 data().HasIrrelevantDestructor = false;
648
649 // C++ [class]p4:
650 // A POD-struct is an aggregate class that has [...] no user-defined
651 // destructor.
652 // This bit is the C++03 POD bit, not the 0x one.
653 data().PlainOldData = false;
654 }
655
656 // C++11 [class.dtor]p5:
Douglas Gregor45fa5602011-11-07 20:56:01 +0000657 // A destructor is trivial if it is not user-provided and if
658 // -- the destructor is not virtual.
Richard Smithd3861ce2012-06-10 07:07:24 +0000659 if (DD->isUserProvided() || DD->isVirtual())
Sean Huntcf34e752011-05-16 22:41:40 +0000660 data().HasTrivialDestructor = false;
Richard Smithd3861ce2012-06-10 07:07:24 +0000661
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000662 return;
663 }
Richard Smithacf796b2012-11-28 06:23:12 +0000664
665 // Handle member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000666 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000667 if (Method->isCopyAssignmentOperator()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000668 // Suppress the implicit declaration of a copy constructor.
Sean Huntffe37fd2011-05-25 20:50:04 +0000669 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000670
Richard Smithacf796b2012-11-28 06:23:12 +0000671 if (!Method->isImplicit()) {
672 data().UserDeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000673
Richard Smithacf796b2012-11-28 06:23:12 +0000674 // C++ [class]p4:
675 // A POD-struct is an aggregate class that [...] has no user-defined
676 // copy assignment operator [...].
677 // This is the C++03 bit only.
678 data().PlainOldData = false;
679
680 // C++11 [class.copy]p25:
681 // A copy/move assignment operator for class X is trivial if it is
682 // not user-provided [...]
683 // FIXME: This is bogus. Having one user-provided copy assignment
684 // doesn't stop another one from being trivial.
685 if (Method->isUserProvided())
686 data().HasTrivialCopyAssignment = false;
687 }
688
689 const ReferenceType *ParamTy =
690 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
691 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
692 data().HasDeclaredCopyAssignmentWithConstParam = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000693 }
Sean Huntffe37fd2011-05-25 20:50:04 +0000694
Richard Smithacf796b2012-11-28 06:23:12 +0000695 if (Method->isMoveAssignmentOperator()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000696 data().DeclaredMoveAssignment = true;
697
Richard Smithacf796b2012-11-28 06:23:12 +0000698 if (!Method->isImplicit()) {
699 data().UserDeclaredMoveAssignment = true;
700
701 // This is an extension in C++03 mode, but we'll keep consistency by
702 // taking a move assignment operator to induce non-POD-ness
703 data().PlainOldData = false;
704
705 // C++0x [class.copy]p27:
706 // A copy/move assignment operator for class X is trivial if it is
707 // neither user-provided nor deleted [...]
708 if (Method->isUserProvided())
709 data().HasTrivialMoveAssignment = false;
710 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000711 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000712
Douglas Gregore80622f2010-09-29 04:25:11 +0000713 // Keep the list of conversion functions up-to-date.
714 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
715 // We don't record specializations.
716 if (Conversion->getPrimaryTemplate())
717 return;
Richard Smithacf796b2012-11-28 06:23:12 +0000718
Douglas Gregore80622f2010-09-29 04:25:11 +0000719 // FIXME: We intentionally don't use the decl's access here because it
720 // hasn't been set yet. That's really just a misdesign in Sema.
721
722 if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000723 if (FunTmpl->getPreviousDecl())
724 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000725 FunTmpl);
726 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000727 data().Conversions.addDecl(getASTContext(), FunTmpl);
Douglas Gregore80622f2010-09-29 04:25:11 +0000728 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000729 if (Conversion->getPreviousDecl())
730 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000731 Conversion);
732 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000733 data().Conversions.addDecl(getASTContext(), Conversion);
Douglas Gregore80622f2010-09-29 04:25:11 +0000734 }
735 }
Richard Smithacf796b2012-11-28 06:23:12 +0000736
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000737 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000738 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000739
740 // Handle non-static data members.
741 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000742 // C++ [class.bit]p2:
743 // A declaration for a bit-field that omits the identifier declares an
744 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
745 // initialized.
746 if (Field->isUnnamedBitfield())
747 return;
748
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000749 // C++ [dcl.init.aggr]p1:
750 // An aggregate is an array or a class (clause 9) with [...] no
751 // private or protected non-static data members (clause 11).
752 //
753 // A POD must be an aggregate.
754 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
755 data().Aggregate = false;
756 data().PlainOldData = false;
757 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000758
759 // C++0x [class]p7:
760 // A standard-layout class is a class that:
761 // [...]
762 // -- has the same access control for all non-static data members,
763 switch (D->getAccess()) {
764 case AS_private: data().HasPrivateFields = true; break;
765 case AS_protected: data().HasProtectedFields = true; break;
766 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000767 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000768 };
769 if ((data().HasPrivateFields + data().HasProtectedFields +
770 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000771 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000772
Douglas Gregor2bb11012011-05-13 01:05:07 +0000773 // Keep track of the presence of mutable fields.
774 if (Field->isMutable())
775 data().HasMutableFields = true;
776
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000777 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000778 // A POD struct is a class that is both a trivial class and a
779 // standard-layout class, and has no non-static data members of type
780 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000781 //
782 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
783 // that does not explicitly have no lifetime makes the class a non-POD.
784 // However, we delay setting PlainOldData to false in this case so that
785 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000786 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000787 // In this case, the class will become a non-POD class when we complete
788 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000789 ASTContext &Context = getASTContext();
790 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000791 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000792 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000793 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
794 setHasObjectMember(true);
795 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000796 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000797
Chandler Carrutha8225442011-04-30 09:17:45 +0000798 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000799 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000800
Chandler Carrutha8225442011-04-30 09:17:45 +0000801 // C++0x [class]p7:
802 // A standard-layout class is a class that:
803 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000804 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000805 }
806
Richard Smith86c3ae42012-02-13 03:54:03 +0000807 // Record if this field is the first non-literal or volatile field or base.
808 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000809 data().HasNonLiteralTypeFieldsOrBases = true;
810
Richard Smith7a614d82011-06-11 17:19:42 +0000811 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000812 data().HasInClassInitializer = true;
813
814 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000815 // A default constructor is trivial if [...] no non-static data member
816 // of its class has a brace-or-equal-initializer.
817 data().HasTrivialDefaultConstructor = false;
818
Richard Smithd079abf2012-05-07 01:07:30 +0000819 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000820 // An aggregate is a [...] class with [...] no
821 // brace-or-equal-initializers for non-static data members.
822 data().Aggregate = false;
823
Richard Smithd079abf2012-05-07 01:07:30 +0000824 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000825 // A POD struct is [...] a trivial class.
826 data().PlainOldData = false;
827 }
828
Douglas Gregor85606eb2010-09-28 20:50:54 +0000829 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
830 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
831 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000832 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000833 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000834 // -- for all the non-static data members of its class that are of
835 // class type (or array thereof), each such class has a trivial
836 // default constructor.
837 if (!FieldRec->hasTrivialDefaultConstructor())
838 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000839
840 // C++0x [class.copy]p13:
841 // A copy/move constructor for class X is trivial if [...]
842 // [...]
843 // -- for each non-static data member of X that is of class type (or
844 // an array thereof), the constructor selected to copy/move that
845 // member is trivial;
846 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000847 if (!FieldRec->hasTrivialCopyConstructor())
848 data().HasTrivialCopyConstructor = false;
Richard Smith426391c2012-11-16 00:53:38 +0000849 if (!FieldRec->hasTrivialMoveConstructor())
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000850 data().HasTrivialMoveConstructor = false;
851
852 // C++0x [class.copy]p27:
853 // A copy/move assignment operator for class X is trivial if [...]
854 // [...]
855 // -- for each non-static data member of X that is of class type (or
856 // an array thereof), the assignment operator selected to
857 // copy/move that member is trivial;
858 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000859 if (!FieldRec->hasTrivialCopyAssignment())
860 data().HasTrivialCopyAssignment = false;
Richard Smith426391c2012-11-16 00:53:38 +0000861 if (!FieldRec->hasTrivialMoveAssignment())
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000862 data().HasTrivialMoveAssignment = false;
863
Douglas Gregor85606eb2010-09-28 20:50:54 +0000864 if (!FieldRec->hasTrivialDestructor())
865 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000866 if (!FieldRec->hasIrrelevantDestructor())
867 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000868 if (FieldRec->hasObjectMember())
869 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000870
871 // C++0x [class]p7:
872 // A standard-layout class is a class that:
873 // -- has no non-static data members of type non-standard-layout
874 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000875 if (!FieldRec->isStandardLayout())
876 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000877
878 // C++0x [class]p7:
879 // A standard-layout class is a class that:
880 // [...]
881 // -- has no base classes of the same type as the first non-static
882 // data member.
883 // We don't want to expend bits in the state of the record decl
884 // tracking whether this is the first non-static data member so we
885 // cheat a bit and use some of the existing state: the empty bit.
886 // Virtual bases and virtual methods make a class non-empty, but they
887 // also make it non-standard-layout so we needn't check here.
888 // A non-empty base class may leave the class standard-layout, but not
889 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000890 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000891 // data member must come through here with Empty still true, and Empty
892 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000893 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000894 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
895 BE = bases_end();
896 BI != BE; ++BI) {
897 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000898 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000899 break;
900 }
901 }
902 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000903
904 // Keep track of the presence of mutable fields.
905 if (FieldRec->hasMutableFields())
906 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000907
908 // C++11 [class.copy]p13:
909 // If the implicitly-defined constructor would satisfy the
910 // requirements of a constexpr constructor, the implicitly-defined
911 // constructor is constexpr.
912 // C++11 [dcl.constexpr]p4:
913 // -- every constructor involved in initializing non-static data
914 // members [...] shall be a constexpr constructor
915 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000916 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000917 // The standard requires any in-class initializer to be a constant
918 // expression. We consider this to be a defect.
919 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithacf796b2012-11-28 06:23:12 +0000920
921 // C++11 [class.copy]p8:
922 // The implicitly-declared copy constructor for a class X will have
923 // the form 'X::X(const X&)' if [...] for all the non-static data
924 // members of X that are of a class type M (or array thereof), each
925 // such class type has a copy constructor whose first parameter is
926 // of type 'const M&' or 'const volatile M&'.
927 if (!FieldRec->hasCopyConstructorWithConstParam())
928 data().ImplicitCopyConstructorHasConstParam = false;
929
930 // C++11 [class.copy]p18:
931 // The implicitly-declared copy assignment oeprator for a class X will
932 // have the form 'X& X::operator=(const X&)' if [...] for all the
933 // non-static data members of X that are of a class type M (or array
934 // thereof), each such class type has a copy assignment operator whose
935 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
936 if (!FieldRec->hasCopyAssignmentWithConstParam())
937 data().ImplicitCopyAssignmentHasConstParam = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000938 }
Richard Smith61802452011-12-22 02:22:31 +0000939 } else {
940 // Base element type of field is a non-class type.
Richard Smithd3861ce2012-06-10 07:07:24 +0000941 if (!T->isLiteralType() ||
942 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000943 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000944 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000945
946 // C++0x [class]p7:
947 // A standard-layout class is a class that:
948 // [...]
949 // -- either has no non-static data members in the most derived
950 // class and at most one base class with non-static data members,
951 // or has no base classes with non-static data members, and
952 // At this point we know that we have a non-static data member, so the last
953 // clause holds.
954 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000955 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000956
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000957 // If this is not a zero-length bit-field, then the class is not empty.
958 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000959 if (!Field->isBitField() ||
960 (!Field->getBitWidth()->isTypeDependent() &&
961 !Field->getBitWidth()->isValueDependent() &&
962 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000963 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000964 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000965 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000966
967 // Handle using declarations of conversion functions.
968 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
969 if (Shadow->getDeclName().getNameKind()
970 == DeclarationName::CXXConversionFunctionName)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +0000971 data().Conversions.addDecl(getASTContext(), Shadow, Shadow->getAccess());
Joao Matos17d35c32012-08-31 22:18:20 +0000972}
973
974bool CXXRecordDecl::isCLike() const {
975 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
976 !TemplateOrInstantiation.isNull())
977 return false;
978 if (!hasDefinition())
979 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000980
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000981 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000982}
983
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000984void CXXRecordDecl::getCaptureFields(
985 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000986 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000987 Captures.clear();
988 ThisCapture = 0;
989
Douglas Gregorda8962a2012-02-13 15:44:47 +0000990 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000991 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000992 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000993 C != CEnd; ++C, ++Field) {
994 if (C->capturesThis()) {
David Blaikie581deb32012-06-06 20:45:41 +0000995 ThisCapture = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000996 continue;
997 }
998
David Blaikie581deb32012-06-06 20:45:41 +0000999 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001000 }
1001}
1002
1003
John McCallb05b5f32010-03-15 09:07:48 +00001004static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1005 QualType T;
John McCall32daa422010-03-31 01:36:47 +00001006 if (isa<UsingShadowDecl>(Conv))
1007 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +00001008 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
1009 T = ConvTemp->getTemplatedDecl()->getResultType();
1010 else
1011 T = cast<CXXConversionDecl>(Conv)->getConversionType();
1012 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +00001013}
1014
John McCallb05b5f32010-03-15 09:07:48 +00001015/// Collect the visible conversions of a base class.
1016///
James Dennetta1253502012-06-15 22:28:09 +00001017/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +00001018/// \param InVirtual whether this base class is a virtual base (or a base
1019/// of a virtual base)
1020/// \param Access the access along the inheritance path to this base
1021/// \param ParentHiddenTypes the conversions provided by the inheritors
1022/// of this base
1023/// \param Output the set to which to add conversions from non-virtual bases
1024/// \param VOutput the set to which to add conversions from virtual bases
1025/// \param HiddenVBaseCs the set of conversions which were hidden in a
1026/// virtual base along some inheritance path
1027static void CollectVisibleConversions(ASTContext &Context,
1028 CXXRecordDecl *Record,
1029 bool InVirtual,
1030 AccessSpecifier Access,
1031 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001032 ASTUnresolvedSet &Output,
John McCallb05b5f32010-03-15 09:07:48 +00001033 UnresolvedSetImpl &VOutput,
1034 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1035 // The set of types which have conversions in this class or its
1036 // subclasses. As an optimization, we don't copy the derived set
1037 // unless it might change.
1038 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1039 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1040
1041 // Collect the direct conversions and figure out which conversions
1042 // will be hidden in the subclasses.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001043 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1044 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1045 if (ConvI != ConvE) {
John McCallb05b5f32010-03-15 09:07:48 +00001046 HiddenTypesBuffer = ParentHiddenTypes;
1047 HiddenTypes = &HiddenTypesBuffer;
1048
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001049 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +00001050 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1051 bool Hidden = ParentHiddenTypes.count(ConvType);
1052 if (!Hidden)
1053 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001054
1055 // If this conversion is hidden and we're in a virtual base,
1056 // remember that it's hidden along some inheritance path.
1057 if (Hidden && InVirtual)
1058 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1059
1060 // If this conversion isn't hidden, add it to the appropriate output.
1061 else if (!Hidden) {
1062 AccessSpecifier IAccess
1063 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1064
1065 if (InVirtual)
1066 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001067 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001068 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001069 }
1070 }
1071 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001072
John McCallb05b5f32010-03-15 09:07:48 +00001073 // Collect information recursively from any base classes.
1074 for (CXXRecordDecl::base_class_iterator
1075 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1076 const RecordType *RT = I->getType()->getAs<RecordType>();
1077 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001078
John McCallb05b5f32010-03-15 09:07:48 +00001079 AccessSpecifier BaseAccess
1080 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1081 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001082
John McCallb05b5f32010-03-15 09:07:48 +00001083 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1084 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1085 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001086 }
John McCallb05b5f32010-03-15 09:07:48 +00001087}
Sebastian Redl9994a342009-10-25 17:03:50 +00001088
John McCallb05b5f32010-03-15 09:07:48 +00001089/// Collect the visible conversions of a class.
1090///
1091/// This would be extremely straightforward if it weren't for virtual
1092/// bases. It might be worth special-casing that, really.
1093static void CollectVisibleConversions(ASTContext &Context,
1094 CXXRecordDecl *Record,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001095 ASTUnresolvedSet &Output) {
John McCallb05b5f32010-03-15 09:07:48 +00001096 // The collection of all conversions in virtual bases that we've
1097 // found. These will be added to the output as long as they don't
1098 // appear in the hidden-conversions set.
1099 UnresolvedSet<8> VBaseCs;
1100
1101 // The set of conversions in virtual bases that we've determined to
1102 // be hidden.
1103 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1104
1105 // The set of types hidden by classes derived from this one.
1106 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1107
1108 // Go ahead and collect the direct conversions and add them to the
1109 // hidden-types set.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001110 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1111 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001112 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001113 for (; ConvI != ConvE; ++ConvI)
1114 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCallb05b5f32010-03-15 09:07:48 +00001115
1116 // Recursively collect conversions from base classes.
1117 for (CXXRecordDecl::base_class_iterator
1118 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1119 const RecordType *RT = I->getType()->getAs<RecordType>();
1120 if (!RT) continue;
1121
1122 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1123 I->isVirtual(), I->getAccessSpecifier(),
1124 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1125 }
1126
1127 // Add any unhidden conversions provided by virtual bases.
1128 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1129 I != E; ++I) {
1130 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001131 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001132 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001133}
1134
1135/// getVisibleConversionFunctions - get all conversion functions visible
1136/// in current class; including conversion function templates.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001137std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1138CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001139 // If root class, all conversions are visible.
1140 if (bases_begin() == bases_end())
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001141 return std::make_pair(data().Conversions.begin(), data().Conversions.end());
Fariborz Jahanian62509212009-09-12 18:26:03 +00001142 // If visible conversion list is already evaluated, return it.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001143 if (!data().ComputedVisibleConversions) {
1144 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
1145 data().ComputedVisibleConversions = true;
1146 }
1147 return std::make_pair(data().VisibleConversions.begin(),
1148 data().VisibleConversions.end());
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
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001163 ASTUnresolvedSet &Convs = data().Conversions;
John McCall32daa422010-03-31 01:36:47 +00001164 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
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001252 // default constructor (if any), copy constructor, move constructor,
1253 // copy assignment operator, move assignment operator, and destructor are
1254 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001255 struct DefinitionData &Data = data();
1256 Data.PlainOldData = false;
1257 Data.HasTrivialDefaultConstructor = false;
1258 Data.HasTrivialCopyConstructor = false;
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001259 Data.HasTrivialMoveConstructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001260 Data.HasTrivialCopyAssignment = false;
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001261 Data.HasTrivialMoveAssignment = false;
John McCallf85e1932011-06-15 23:02:42 +00001262 Data.HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +00001263 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001264 }
1265
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001266 // If the class may be abstract (but hasn't been marked as such), check for
1267 // any pure final overriders.
1268 if (mayBeAbstract()) {
1269 CXXFinalOverriderMap MyFinalOverriders;
1270 if (!FinalOverriders) {
1271 getFinalOverriders(MyFinalOverriders);
1272 FinalOverriders = &MyFinalOverriders;
1273 }
1274
1275 bool Done = false;
1276 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1277 MEnd = FinalOverriders->end();
1278 M != MEnd && !Done; ++M) {
1279 for (OverridingMethods::iterator SO = M->second.begin(),
1280 SOEnd = M->second.end();
1281 SO != SOEnd && !Done; ++SO) {
1282 assert(SO->second.size() > 0 &&
1283 "All virtual functions have overridding virtual functions");
1284
1285 // C++ [class.abstract]p4:
1286 // A class is abstract if it contains or inherits at least one
1287 // pure virtual function for which the final overrider is pure
1288 // virtual.
1289 if (SO->second.front().Method->isPure()) {
1290 data().Abstract = true;
1291 Done = true;
1292 break;
1293 }
1294 }
1295 }
1296 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001297
1298 // Set access bits correctly on the directly-declared conversions.
1299 for (UnresolvedSetIterator I = data().Conversions.begin(),
1300 E = data().Conversions.end();
1301 I != E; ++I)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001302 I.setAccess((*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001303}
1304
1305bool CXXRecordDecl::mayBeAbstract() const {
1306 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1307 isDependentContext())
1308 return false;
1309
1310 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1311 BEnd = bases_end();
1312 B != BEnd; ++B) {
1313 CXXRecordDecl *BaseDecl
1314 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1315 if (BaseDecl->isAbstract())
1316 return true;
1317 }
1318
1319 return false;
1320}
1321
David Blaikie99ba9e32011-12-20 02:48:34 +00001322void CXXMethodDecl::anchor() { }
1323
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001324static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1325 const CXXMethodDecl *BaseMD) {
1326 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1327 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1328 const CXXMethodDecl *MD = *I;
1329 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1330 return true;
1331 if (recursivelyOverrides(MD, BaseMD))
1332 return true;
1333 }
1334 return false;
1335}
1336
1337CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001338CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1339 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001340 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1341 return this;
1342
1343 // Lookup doesn't work for destructors, so handle them separately.
1344 if (isa<CXXDestructorDecl>(this)) {
1345 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001346 if (MD) {
1347 if (recursivelyOverrides(MD, this))
1348 return MD;
1349 if (MayBeBase && recursivelyOverrides(this, MD))
1350 return MD;
1351 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001352 return NULL;
1353 }
1354
1355 lookup_const_result Candidates = RD->lookup(getDeclName());
1356 for (NamedDecl * const * I = Candidates.first; I != Candidates.second; ++I) {
1357 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1358 if (!MD)
1359 continue;
1360 if (recursivelyOverrides(MD, this))
1361 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001362 if (MayBeBase && recursivelyOverrides(this, MD))
1363 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001364 }
1365
1366 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1367 E = RD->bases_end(); I != E; ++I) {
1368 const RecordType *RT = I->getType()->getAs<RecordType>();
1369 if (!RT)
1370 continue;
1371 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1372 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1373 if (T)
1374 return T;
1375 }
1376
1377 return NULL;
1378}
1379
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001380CXXMethodDecl *
1381CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001382 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001383 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001384 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001385 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001386 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001387 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001388 isStatic, SCAsWritten, isInline, isConstexpr,
1389 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001390}
1391
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001392CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1393 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1394 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1395 DeclarationNameInfo(), QualType(),
1396 0, false, SC_None, false, false,
1397 SourceLocation());
1398}
1399
Douglas Gregor90916562009-09-29 18:16:17 +00001400bool CXXMethodDecl::isUsualDeallocationFunction() const {
1401 if (getOverloadedOperator() != OO_Delete &&
1402 getOverloadedOperator() != OO_Array_Delete)
1403 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001404
1405 // C++ [basic.stc.dynamic.deallocation]p2:
1406 // A template instance is never a usual deallocation function,
1407 // regardless of its signature.
1408 if (getPrimaryTemplate())
1409 return false;
1410
Douglas Gregor90916562009-09-29 18:16:17 +00001411 // C++ [basic.stc.dynamic.deallocation]p2:
1412 // If a class T has a member deallocation function named operator delete
1413 // with exactly one parameter, then that function is a usual (non-placement)
1414 // deallocation function. [...]
1415 if (getNumParams() == 1)
1416 return true;
1417
1418 // C++ [basic.stc.dynamic.deallocation]p2:
1419 // [...] If class T does not declare such an operator delete but does
1420 // declare a member deallocation function named operator delete with
1421 // exactly two parameters, the second of which has type std::size_t (18.1),
1422 // then this function is a usual deallocation function.
1423 ASTContext &Context = getASTContext();
1424 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001425 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1426 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001427 return false;
1428
1429 // This function is a usual deallocation function if there are no
1430 // single-parameter deallocation functions of the same kind.
1431 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1432 R.first != R.second; ++R.first) {
1433 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1434 if (FD->getNumParams() == 1)
1435 return false;
1436 }
1437
1438 return true;
1439}
1440
Douglas Gregor06a9f362010-05-01 20:49:11 +00001441bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001442 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001443 // A user-declared copy assignment operator X::operator= is a non-static
1444 // non-template member function of class X with exactly one parameter of
1445 // type X, X&, const X&, volatile X& or const volatile X&.
1446 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1447 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001448 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001449 return false;
1450
1451 QualType ParamType = getParamDecl(0)->getType();
1452 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1453 ParamType = Ref->getPointeeType();
1454
1455 ASTContext &Context = getASTContext();
1456 QualType ClassType
1457 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1458 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1459}
1460
Sean Huntffe37fd2011-05-25 20:50:04 +00001461bool CXXMethodDecl::isMoveAssignmentOperator() const {
1462 // C++0x [class.copy]p19:
1463 // A user-declared move assignment operator X::operator= is a non-static
1464 // non-template member function of class X with exactly one parameter of type
1465 // X&&, const X&&, volatile X&&, or const volatile X&&.
1466 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1467 getPrimaryTemplate() || getDescribedFunctionTemplate())
1468 return false;
1469
1470 QualType ParamType = getParamDecl(0)->getType();
1471 if (!isa<RValueReferenceType>(ParamType))
1472 return false;
1473 ParamType = ParamType->getPointeeType();
1474
1475 ASTContext &Context = getASTContext();
1476 QualType ClassType
1477 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1478 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1479}
1480
Anders Carlsson05eb2442009-05-16 23:58:37 +00001481void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001482 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001483 assert(!MD->getParent()->isDependentContext() &&
1484 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001485 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001486
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001487 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001488}
1489
1490CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001491 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001492 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001493}
1494
1495CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001496 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001497 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001498}
1499
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001500unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001501 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001502 return getASTContext().overridden_methods_size(this);
1503}
1504
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001505QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001506 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1507 // If the member function is declared const, the type of this is const X*,
1508 // if the member function is declared volatile, the type of this is
1509 // volatile X*, and if the member function is declared const volatile,
1510 // the type of this is const volatile X*.
1511
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001512 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001513
John McCall3cb0ebd2010-03-10 03:28:59 +00001514 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001515 ClassTy = C.getQualifiedType(ClassTy,
1516 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001517 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001518}
1519
Eli Friedmand7d7f672009-12-06 20:50:05 +00001520bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001521 // If this function is a template instantiation, look at the template from
1522 // which it was instantiated.
1523 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1524 if (!CheckFn)
1525 CheckFn = this;
1526
Eli Friedmand7d7f672009-12-06 20:50:05 +00001527 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001528 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001529}
1530
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001531bool CXXMethodDecl::isLambdaStaticInvoker() const {
1532 return getParent()->isLambda() &&
1533 getIdentifier() && getIdentifier()->getName() == "__invoke";
1534}
1535
1536
Sean Huntcbb67482011-01-08 20:30:50 +00001537CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1538 TypeSourceInfo *TInfo, bool IsVirtual,
1539 SourceLocation L, Expr *Init,
1540 SourceLocation R,
1541 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001542 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001543 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1544 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001545{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001546}
1547
Sean Huntcbb67482011-01-08 20:30:50 +00001548CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1549 FieldDecl *Member,
1550 SourceLocation MemberLoc,
1551 SourceLocation L, Expr *Init,
1552 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001553 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001554 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001555 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1556{
1557}
1558
Sean Huntcbb67482011-01-08 20:30:50 +00001559CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1560 IndirectFieldDecl *Member,
1561 SourceLocation MemberLoc,
1562 SourceLocation L, Expr *Init,
1563 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001564 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001565 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001566 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001567{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001568}
1569
Sean Huntcbb67482011-01-08 20:30:50 +00001570CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001571 TypeSourceInfo *TInfo,
1572 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001573 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001574 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1575 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001576 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1577{
1578}
1579
1580CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001581 FieldDecl *Member,
1582 SourceLocation MemberLoc,
1583 SourceLocation L, Expr *Init,
1584 SourceLocation R,
1585 VarDecl **Indices,
1586 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001587 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001588 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001589 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001590{
1591 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1592 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1593}
1594
Sean Huntcbb67482011-01-08 20:30:50 +00001595CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1596 FieldDecl *Member,
1597 SourceLocation MemberLoc,
1598 SourceLocation L, Expr *Init,
1599 SourceLocation R,
1600 VarDecl **Indices,
1601 unsigned NumIndices) {
1602 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001603 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001604 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001605 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1606 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001607}
1608
Sean Huntcbb67482011-01-08 20:30:50 +00001609TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001610 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001611 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001612 else
1613 return TypeLoc();
1614}
1615
Sean Huntcbb67482011-01-08 20:30:50 +00001616const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001617 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001618 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001619 else
1620 return 0;
1621}
1622
Sean Huntcbb67482011-01-08 20:30:50 +00001623SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001624 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001625 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001626
1627 if (isInClassMemberInitializer())
1628 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001629
Douglas Gregor76852c22011-11-01 01:16:03 +00001630 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1631 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1632
1633 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001634}
1635
Sean Huntcbb67482011-01-08 20:30:50 +00001636SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001637 if (isInClassMemberInitializer()) {
1638 FieldDecl *D = getAnyMember();
1639 if (Expr *I = D->getInClassInitializer())
1640 return I->getSourceRange();
1641 return SourceRange();
1642 }
1643
Douglas Gregor802ab452009-12-02 22:36:29 +00001644 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001645}
1646
David Blaikie99ba9e32011-12-20 02:48:34 +00001647void CXXConstructorDecl::anchor() { }
1648
Douglas Gregorb48fe382008-10-31 09:07:45 +00001649CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001650CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1651 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1652 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1653 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001654}
1655
1656CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001657CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001658 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001659 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001660 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001661 bool isExplicit, bool isInline,
1662 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001663 assert(NameInfo.getName().getNameKind()
1664 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001665 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001666 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001667 isExplicit, isInline, isImplicitlyDeclared,
1668 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001669}
1670
Douglas Gregor76852c22011-11-01 01:16:03 +00001671CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1672 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1673 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1674 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1675 return Construct->getConstructor();
1676
1677 return 0;
1678}
1679
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001680bool CXXConstructorDecl::isDefaultConstructor() const {
1681 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001682 // A default constructor for a class X is a constructor of class
1683 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001684 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001685 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001686}
1687
Mike Stump1eb44332009-09-09 15:08:12 +00001688bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001689CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001690 return isCopyOrMoveConstructor(TypeQuals) &&
1691 getParamDecl(0)->getType()->isLValueReferenceType();
1692}
1693
1694bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1695 return isCopyOrMoveConstructor(TypeQuals) &&
1696 getParamDecl(0)->getType()->isRValueReferenceType();
1697}
1698
1699/// \brief Determine whether this is a copy or move constructor.
1700bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001701 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001702 // A non-template constructor for class X is a copy constructor
1703 // if its first parameter is of type X&, const X&, volatile X& or
1704 // const volatile X&, and either there are no other parameters
1705 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001706 // C++0x [class.copy]p3:
1707 // A non-template constructor for class X is a move constructor if its
1708 // first parameter is of type X&&, const X&&, volatile X&&, or
1709 // const volatile X&&, and either there are no other parameters or else
1710 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001711 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001712 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001713 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001714 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001715 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001716
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001717 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001718
1719 // Do we have a reference type?
1720 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001721 if (!ParamRefType)
1722 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001723
Douglas Gregorfd476482009-11-13 23:59:09 +00001724 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001725 ASTContext &Context = getASTContext();
1726
Douglas Gregorfd476482009-11-13 23:59:09 +00001727 CanQualType PointeeType
1728 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001729 CanQualType ClassTy
1730 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001731 if (PointeeType.getUnqualifiedType() != ClassTy)
1732 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001733
John McCall0953e762009-09-24 19:53:00 +00001734 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001735
1736 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001737 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001738 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001739}
1740
Anders Carlssonfaccd722009-08-28 16:57:08 +00001741bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001742 // C++ [class.conv.ctor]p1:
1743 // A constructor declared without the function-specifier explicit
1744 // that can be called with a single parameter specifies a
1745 // conversion from the type of its first parameter to the type of
1746 // its class. Such a constructor is called a converting
1747 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001748 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001749 return false;
1750
Mike Stump1eb44332009-09-09 15:08:12 +00001751 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001752 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001753 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001754 (getNumParams() > 1 &&
1755 (getParamDecl(1)->hasDefaultArg() ||
1756 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001757}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001758
Douglas Gregor6493cc52010-11-08 17:16:59 +00001759bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001760 if ((getNumParams() < 1) ||
1761 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1762 (getPrimaryTemplate() == 0) ||
1763 (getDescribedFunctionTemplate() != 0))
1764 return false;
1765
1766 const ParmVarDecl *Param = getParamDecl(0);
1767
1768 ASTContext &Context = getASTContext();
1769 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1770
Douglas Gregor66724ea2009-11-14 01:20:54 +00001771 // Is it the same as our our class type?
1772 CanQualType ClassTy
1773 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1774 if (ParamType.getUnqualifiedType() != ClassTy)
1775 return false;
1776
1777 return true;
1778}
1779
Sebastian Redlf677ea32011-02-05 19:23:19 +00001780const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1781 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001782 method_iterator It = getASTContext().overridden_methods_begin(this);
1783 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001784 return 0;
1785
1786 return cast<CXXConstructorDecl>(*It);
1787}
1788
1789void
1790CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1791 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001792 assert(getASTContext().overridden_methods_size(this) == 0 &&
1793 "Base ctor already set.");
1794 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001795}
1796
David Blaikie99ba9e32011-12-20 02:48:34 +00001797void CXXDestructorDecl::anchor() { }
1798
Douglas Gregor42a552f2008-11-05 20:51:48 +00001799CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001800CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1801 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1802 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001803 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001804}
1805
1806CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001807CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001808 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001809 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001810 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001811 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001812 assert(NameInfo.getName().getNameKind()
1813 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001814 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001815 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001816 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001817}
1818
David Blaikie99ba9e32011-12-20 02:48:34 +00001819void CXXConversionDecl::anchor() { }
1820
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001821CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001822CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1823 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1824 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1825 QualType(), 0, false, false, false,
1826 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001827}
1828
1829CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001830CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001831 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001832 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001833 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001834 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001835 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001836 assert(NameInfo.getName().getNameKind()
1837 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001838 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001839 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001840 isInline, isExplicit, isConstexpr,
1841 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001842}
1843
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001844bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1845 return isImplicit() && getParent()->isLambda() &&
1846 getConversionType()->isBlockPointerType();
1847}
1848
David Blaikie99ba9e32011-12-20 02:48:34 +00001849void LinkageSpecDecl::anchor() { }
1850
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001851LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001852 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001853 SourceLocation ExternLoc,
1854 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001855 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001856 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001857 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001858}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001859
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001860LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1861 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1862 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1863 lang_c, SourceLocation());
1864}
1865
David Blaikie99ba9e32011-12-20 02:48:34 +00001866void UsingDirectiveDecl::anchor() { }
1867
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001868UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1869 SourceLocation L,
1870 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001871 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001872 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001873 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001874 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001875 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1876 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001877 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1878 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001879}
1880
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001881UsingDirectiveDecl *
1882UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1883 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1884 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1885 NestedNameSpecifierLoc(),
1886 SourceLocation(), 0, 0);
1887}
1888
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001889NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1890 if (NamespaceAliasDecl *NA =
1891 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1892 return NA->getNamespace();
1893 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1894}
1895
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001896void NamespaceDecl::anchor() { }
1897
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001898NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1899 SourceLocation StartLoc,
1900 SourceLocation IdLoc, IdentifierInfo *Id,
1901 NamespaceDecl *PrevDecl)
1902 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1903 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1904{
1905 setPreviousDeclaration(PrevDecl);
1906
1907 if (PrevDecl)
1908 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1909}
1910
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001911NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001912 bool Inline, SourceLocation StartLoc,
1913 SourceLocation IdLoc, IdentifierInfo *Id,
1914 NamespaceDecl *PrevDecl) {
1915 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001916}
1917
1918NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1919 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001920 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1921 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001922}
1923
David Blaikie99ba9e32011-12-20 02:48:34 +00001924void NamespaceAliasDecl::anchor() { }
1925
Mike Stump1eb44332009-09-09 15:08:12 +00001926NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001927 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001928 SourceLocation AliasLoc,
1929 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001930 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001931 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001932 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001933 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1934 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001935 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1936 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001937}
1938
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001939NamespaceAliasDecl *
1940NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1941 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1942 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1943 NestedNameSpecifierLoc(),
1944 SourceLocation(), 0);
1945}
1946
David Blaikie99ba9e32011-12-20 02:48:34 +00001947void UsingShadowDecl::anchor() { }
1948
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001949UsingShadowDecl *
1950UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1951 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1952 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1953}
1954
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001955UsingDecl *UsingShadowDecl::getUsingDecl() const {
1956 const UsingShadowDecl *Shadow = this;
1957 while (const UsingShadowDecl *NextShadow =
1958 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1959 Shadow = NextShadow;
1960 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1961}
1962
David Blaikie99ba9e32011-12-20 02:48:34 +00001963void UsingDecl::anchor() { }
1964
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001965void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1966 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1967 "declaration already in set");
1968 assert(S->getUsingDecl() == this);
1969
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001970 if (FirstUsingShadow.getPointer())
1971 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1972 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001973}
1974
1975void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1976 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1977 "declaration not in set");
1978 assert(S->getUsingDecl() == this);
1979
1980 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1981
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001982 if (FirstUsingShadow.getPointer() == S) {
1983 FirstUsingShadow.setPointer(
1984 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001985 S->UsingOrNextShadow = this;
1986 return;
1987 }
1988
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001989 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001990 while (Prev->UsingOrNextShadow != S)
1991 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1992 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1993 S->UsingOrNextShadow = this;
1994}
1995
Douglas Gregordc355712011-02-25 00:36:19 +00001996UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1997 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001998 const DeclarationNameInfo &NameInfo,
1999 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00002000 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002001}
2002
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002003UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2004 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
2005 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
2006 DeclarationNameInfo(), false);
2007}
2008
David Blaikie99ba9e32011-12-20 02:48:34 +00002009void UnresolvedUsingValueDecl::anchor() { }
2010
John McCall7ba107a2009-11-18 02:36:19 +00002011UnresolvedUsingValueDecl *
2012UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2013 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002014 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002015 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00002016 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002017 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00002018}
2019
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002020UnresolvedUsingValueDecl *
2021UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2022 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
2023 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
2024 NestedNameSpecifierLoc(),
2025 DeclarationNameInfo());
2026}
2027
David Blaikie99ba9e32011-12-20 02:48:34 +00002028void UnresolvedUsingTypenameDecl::anchor() { }
2029
John McCall7ba107a2009-11-18 02:36:19 +00002030UnresolvedUsingTypenameDecl *
2031UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2032 SourceLocation UsingLoc,
2033 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002034 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002035 SourceLocation TargetNameLoc,
2036 DeclarationName TargetName) {
2037 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002038 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002039 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00002040}
2041
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002042UnresolvedUsingTypenameDecl *
2043UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2044 void *Mem = AllocateDeserializedDecl(C, ID,
2045 sizeof(UnresolvedUsingTypenameDecl));
2046 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2047 SourceLocation(),
2048 NestedNameSpecifierLoc(),
2049 SourceLocation(),
2050 0);
2051}
2052
David Blaikie99ba9e32011-12-20 02:48:34 +00002053void StaticAssertDecl::anchor() { }
2054
Anders Carlssonfb311762009-03-14 00:25:26 +00002055StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002056 SourceLocation StaticAssertLoc,
2057 Expr *AssertExpr,
2058 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002059 SourceLocation RParenLoc,
2060 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002061 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002062 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00002063}
2064
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002065StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2066 unsigned ID) {
2067 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00002068 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
2069 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002070}
2071
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002072static const char *getAccessName(AccessSpecifier AS) {
2073 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002074 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002075 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002076 case AS_public:
2077 return "public";
2078 case AS_private:
2079 return "private";
2080 case AS_protected:
2081 return "protected";
2082 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002083 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002084}
2085
2086const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2087 AccessSpecifier AS) {
2088 return DB << getAccessName(AS);
2089}
Richard Smithf15fda02012-02-02 01:16:57 +00002090
2091const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2092 AccessSpecifier AS) {
2093 return DB << getAccessName(AS);
2094}