blob: 89a6661cbf6816349fbe7078d99634aba301c040 [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000017#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000018#include "clang/AST/CXXInheritance.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregor76852c22011-11-01 01:16:03 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000021#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000022#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000023#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Decl Allocation/Deallocation Method Implementations
29//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000030
David Blaikie99ba9e32011-12-20 02:48:34 +000031void AccessSpecDecl::anchor() { }
32
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000033AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
34 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(AccessSpecDecl));
35 return new (Mem) AccessSpecDecl(EmptyShell());
36}
37
John McCall86ff3082010-02-04 22:26:26 +000038CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
39 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000040 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
41 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000042 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000043 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000044 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000045 HasMutableFields(false), HasOnlyCMembers(true),
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +000046 HasTrivialDefaultConstructor(true),
Richard Smith61802452011-12-22 02:22:31 +000047 HasConstexprNonCopyMoveConstructor(false),
48 DefaultedDefaultConstructorIsConstexpr(true),
49 DefaultedCopyConstructorIsConstexpr(true),
50 DefaultedMoveConstructorIsConstexpr(true),
51 HasConstexprDefaultConstructor(false), HasConstexprCopyConstructor(false),
52 HasConstexprMoveConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000053 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
54 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
Richard Smithdfefb842012-02-25 07:33:38 +000055 HasIrrelevantDestructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000056 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000057 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000058 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
59 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sebastian Redl85ea7aa2011-08-30 19:58:05 +000060 DeclaredDestructor(false), FailedImplicitMoveConstructor(false),
Eli Friedman72899c32012-01-07 04:59:52 +000061 FailedImplicitMoveAssignment(false), IsLambda(false), NumBases(0),
62 NumVBases(0), Bases(), VBases(), Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000063}
64
65CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000066 SourceLocation StartLoc, SourceLocation IdLoc,
67 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
68 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000069 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000070 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000071
Jay Foad4ba2a172011-01-12 09:06:06 +000072CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000073 DeclContext *DC, SourceLocation StartLoc,
74 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000075 CXXRecordDecl* PrevDecl,
76 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000077 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
78 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000080 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000081 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000082 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000083 return R;
84}
85
Douglas Gregorda8962a2012-02-13 15:44:47 +000086CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Douglas Gregorf4b7de12012-02-21 19:11:17 +000087 SourceLocation Loc, bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +000088 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
89 0, 0);
90 R->IsBeingDefined = true;
Douglas Gregorf4b7de12012-02-21 19:11:17 +000091 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Dependent);
Douglas Gregorda8962a2012-02-13 15:44:47 +000092 C.getTypeDeclType(R, /*PrevDecl=*/0);
93 return R;
94}
95
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000096CXXRecordDecl *
97CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
98 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
99 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
100 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000101}
102
Mike Stump1eb44332009-09-09 15:08:12 +0000103void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000104CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000105 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000106 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000107
Douglas Gregor7c789c12010-10-29 22:39:52 +0000108 if (!data().Bases.isOffset() && data().NumBases > 0)
109 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Richard Smithdd677232011-10-18 20:08:55 +0000111 if (NumBases) {
112 // C++ [dcl.init.aggr]p1:
113 // An aggregate is [...] a class with [...] no base classes [...].
114 data().Aggregate = false;
115
116 // C++ [class]p4:
117 // A POD-struct is an aggregate class...
118 data().PlainOldData = false;
119 }
120
Anders Carlsson6f6de732010-03-29 05:13:12 +0000121 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000122 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000123
124 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000125 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000126
John McCall86ff3082010-02-04 22:26:26 +0000127 data().Bases = new(C) CXXBaseSpecifier [NumBases];
128 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000129 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000130 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000131 // Keep track of inherited vbases for this base class.
132 const CXXBaseSpecifier *Base = Bases[i];
133 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000134 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000135 if (BaseType->isDependentType())
136 continue;
137 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000138 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000139
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000140 // A class with a non-empty base class is not empty.
141 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000142 if (!BaseClassDecl->isEmpty()) {
143 if (!data().Empty) {
144 // C++0x [class]p7:
145 // A standard-layout class is a class that:
146 // [...]
147 // -- either has no non-static data members in the most derived
148 // class and at most one base class with non-static data members,
149 // or has no base classes with non-static data members, and
150 // If this is the second non-empty base, then neither of these two
151 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000152 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000153 }
154
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000155 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000156 data().HasNoNonEmptyBases = false;
157 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000158
Douglas Gregor85606eb2010-09-28 20:50:54 +0000159 // C++ [class.virtual]p1:
160 // A class that declares or inherits a virtual function is called a
161 // polymorphic class.
162 if (BaseClassDecl->isPolymorphic())
163 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000164
Chandler Carrutha8225442011-04-30 09:17:45 +0000165 // C++0x [class]p7:
166 // A standard-layout class is a class that: [...]
167 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000168 if (!BaseClassDecl->isStandardLayout())
169 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000170
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000171 // Record if this base is the first non-literal field or base.
172 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
173 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000174
Anders Carlsson6f6de732010-03-29 05:13:12 +0000175 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000176 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000177 BaseClassDecl->vbases_begin(),
178 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000179 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000180 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000181 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000182 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000183
184 if (Base->isVirtual()) {
185 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000186 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000187 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000188
189 // C++0x [meta.unary.prop] is_empty:
190 // T is a class type, but not a union type, with ... no virtual base
191 // classes
192 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000193
194 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000195 // A default constructor is trivial [...] if:
196 // -- its class has [...] no virtual bases
197 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000198
199 // C++0x [class.copy]p13:
200 // A copy/move constructor for class X is trivial if it is neither
201 // user-provided nor deleted and if
202 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000203 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000204 data().HasTrivialMoveConstructor = false;
205
206 // C++0x [class.copy]p27:
207 // A copy/move assignment operator for class X is trivial if it is
208 // neither user-provided nor deleted and if
209 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000210 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000211 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000212
213 // C++0x [class]p7:
214 // A standard-layout class is a class that: [...]
215 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000216 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000217
218 // C++11 [dcl.constexpr]p4:
219 // In the definition of a constexpr constructor [...]
220 // -- the class shall not have any virtual base classes
221 data().DefaultedDefaultConstructorIsConstexpr = false;
222 data().DefaultedCopyConstructorIsConstexpr = false;
223 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000224 } else {
225 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000226 // A default constructor is trivial [...] if:
227 // -- all the direct base classes of its class have trivial default
228 // constructors.
229 if (!BaseClassDecl->hasTrivialDefaultConstructor())
230 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000231
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000232 // C++0x [class.copy]p13:
233 // A copy/move constructor for class X is trivial if [...]
234 // [...]
235 // -- the constructor selected to copy/move each direct base class
236 // subobject is trivial, and
237 // FIXME: C++0x: We need to only consider the selected constructor
238 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000239 if (!BaseClassDecl->hasTrivialCopyConstructor())
240 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000241 if (!BaseClassDecl->hasTrivialMoveConstructor())
242 data().HasTrivialMoveConstructor = false;
243
244 // C++0x [class.copy]p27:
245 // A copy/move assignment operator for class X is trivial if [...]
246 // [...]
247 // -- the assignment operator selected to copy/move each direct base
248 // class subobject is trivial, and
249 // FIXME: C++0x: We need to only consider the selected operator instead
250 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000251 if (!BaseClassDecl->hasTrivialCopyAssignment())
252 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000253 if (!BaseClassDecl->hasTrivialMoveAssignment())
254 data().HasTrivialMoveAssignment = false;
Richard Smith61802452011-12-22 02:22:31 +0000255
256 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000257 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000258 // requirements of a constexpr constructor, the implicitly-defined
259 // default constructor is constexpr.
260 if (!BaseClassDecl->hasConstexprDefaultConstructor())
261 data().DefaultedDefaultConstructorIsConstexpr = false;
262
263 // C++11 [class.copy]p13:
264 // If the implicitly-defined constructor would satisfy the requirements
265 // of a constexpr constructor, the implicitly-defined constructor is
266 // constexpr.
267 // C++11 [dcl.constexpr]p4:
268 // -- every constructor involved in initializing [...] base class
269 // sub-objects shall be a constexpr constructor
270 if (!BaseClassDecl->hasConstexprCopyConstructor())
271 data().DefaultedCopyConstructorIsConstexpr = false;
272 if (BaseClassDecl->hasDeclaredMoveConstructor() ||
273 BaseClassDecl->needsImplicitMoveConstructor())
274 // FIXME: If the implicit move constructor generated for the base class
275 // would be ill-formed, the implicit move constructor generated for the
276 // derived class calls the base class' copy constructor.
277 data().DefaultedMoveConstructorIsConstexpr &=
Richard Smithde8facc2012-01-11 18:26:05 +0000278 BaseClassDecl->hasConstexprMoveConstructor();
Richard Smith61802452011-12-22 02:22:31 +0000279 else if (!BaseClassDecl->hasConstexprCopyConstructor())
280 data().DefaultedMoveConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000281 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000282
283 // C++ [class.ctor]p3:
284 // A destructor is trivial if all the direct base classes of its class
285 // have trivial destructors.
286 if (!BaseClassDecl->hasTrivialDestructor())
287 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000288
289 if (!BaseClassDecl->hasIrrelevantDestructor())
290 data().HasIrrelevantDestructor = false;
291
John McCallf85e1932011-06-15 23:02:42 +0000292 // A class has an Objective-C object member if... or any of its bases
293 // has an Objective-C object member.
294 if (BaseClassDecl->hasObjectMember())
295 setHasObjectMember(true);
296
Douglas Gregor2bb11012011-05-13 01:05:07 +0000297 // Keep track of the presence of mutable fields.
298 if (BaseClassDecl->hasMutableFields())
299 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000300 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000301
302 if (VBases.empty())
303 return;
304
305 // Create base specifier for any direct or indirect virtual bases.
306 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
307 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000308 for (int I = 0, E = VBases.size(); I != E; ++I)
309 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000310}
311
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000312/// Callback function for CXXRecordDecl::forallBases that acknowledges
313/// that it saw a base class.
314static bool SawBase(const CXXRecordDecl *, void *) {
315 return true;
316}
317
318bool CXXRecordDecl::hasAnyDependentBases() const {
319 if (!isDependentContext())
320 return false;
321
322 return !forallBases(SawBase, 0);
323}
324
Sean Huntffe37fd2011-05-25 20:50:04 +0000325bool CXXRecordDecl::hasConstCopyConstructor() const {
326 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000327}
328
Chandler Carruthb7e95892011-04-23 10:47:28 +0000329bool CXXRecordDecl::isTriviallyCopyable() const {
330 // C++0x [class]p5:
331 // A trivially copyable class is a class that:
332 // -- has no non-trivial copy constructors,
333 if (!hasTrivialCopyConstructor()) return false;
334 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000335 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000336 // -- has no non-trivial copy assignment operators,
337 if (!hasTrivialCopyAssignment()) return false;
338 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000339 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000340 // -- has a trivial destructor.
341 if (!hasTrivialDestructor()) return false;
342
343 return true;
344}
345
Douglas Gregor0d405db2010-07-01 20:59:04 +0000346/// \brief Perform a simplistic form of overload resolution that only considers
347/// cv-qualifiers on a single parameter, and return the best overload candidate
348/// (if there is one).
349static CXXMethodDecl *
350GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000351 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000352 if (Cands.empty())
353 return 0;
354 if (Cands.size() == 1)
355 return Cands[0].first;
356
357 unsigned Best = 0, N = Cands.size();
358 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000359 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000360 Best = I;
361
362 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000363 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000364 return 0;
365
366 return Cands[Best].first;
367}
368
Sean Huntffe37fd2011-05-25 20:50:04 +0000369CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
370 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000371 QualType ClassType
372 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000373 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000374 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000375 Context.getCanonicalType(ClassType));
376 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000377 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000378 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000379 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000380 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000381 // C++ [class.copy]p2:
382 // A non-template constructor for class X is a copy constructor if [...]
383 if (isa<FunctionTemplateDecl>(*Con))
384 continue;
385
Douglas Gregor0d405db2010-07-01 20:59:04 +0000386 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
387 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000388 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
389 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000390 Found.push_back(std::make_pair(
391 const_cast<CXXConstructorDecl *>(Constructor),
392 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000393 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000394 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000395
396 return cast_or_null<CXXConstructorDecl>(
397 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000398}
399
Sean Huntffe37fd2011-05-25 20:50:04 +0000400CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
401 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
402 if (I->isMoveConstructor())
403 return *I;
404
405 return 0;
406}
407
Douglas Gregorb87786f2010-07-01 17:48:08 +0000408CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
409 ASTContext &Context = getASTContext();
410 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
411 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
412
Chris Lattner5f9e2722011-07-23 10:55:15 +0000413 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000414 DeclContext::lookup_const_iterator Op, OpEnd;
415 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
416 // C++ [class.copy]p9:
417 // A user-declared copy assignment operator is a non-static non-template
418 // member function of class X with exactly one parameter of type X, X&,
419 // const X&, volatile X& or const volatile X&.
420 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
421 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
422 continue;
423
424 const FunctionProtoType *FnType
425 = Method->getType()->getAs<FunctionProtoType>();
426 assert(FnType && "Overloaded operator has no prototype.");
427 // Don't assert on this; an invalid decl might have been left in the AST.
428 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
429 continue;
430
431 QualType ArgType = FnType->getArgType(0);
432 Qualifiers Quals;
433 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
434 ArgType = Ref->getPointeeType();
435 // If we have a const argument and we have a reference to a non-const,
436 // this function does not match.
437 if (ArgIsConst && !ArgType.isConstQualified())
438 continue;
439
440 Quals = ArgType.getQualifiers();
441 } else {
442 // By-value copy-assignment operators are treated like const X&
443 // copy-assignment operators.
444 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
445 }
446
447 if (!Context.hasSameUnqualifiedType(ArgType, Class))
448 continue;
449
450 // Save this copy-assignment operator. It might be "the one".
451 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
452 }
453
454 // Use a simplistic form of overload resolution to find the candidate.
455 return GetBestOverloadCandidateSimple(Found);
456}
457
Sean Huntffe37fd2011-05-25 20:50:04 +0000458CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
459 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
460 if (I->isMoveAssignmentOperator())
461 return *I;
462
463 return 0;
464}
465
Douglas Gregor21386642010-09-28 21:55:22 +0000466void CXXRecordDecl::markedVirtualFunctionPure() {
467 // C++ [class.abstract]p2:
468 // A class is abstract if it has at least one pure virtual function.
469 data().Abstract = true;
470}
471
472void CXXRecordDecl::addedMember(Decl *D) {
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +0000473 if (!D->isImplicit() &&
474 !isa<FieldDecl>(D) &&
475 !isa<IndirectFieldDecl>(D) &&
476 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class))
477 data().HasOnlyCMembers = false;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000478
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000479 // Ignore friends and invalid declarations.
480 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000481 return;
482
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000483 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
484 if (FunTmpl)
485 D = FunTmpl->getTemplatedDecl();
486
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000487 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
488 if (Method->isVirtual()) {
489 // C++ [dcl.init.aggr]p1:
490 // An aggregate is an array or a class with [...] no virtual functions.
491 data().Aggregate = false;
492
493 // C++ [class]p4:
494 // A POD-struct is an aggregate class...
495 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000496
497 // Virtual functions make the class non-empty.
498 // FIXME: Standard ref?
499 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000500
501 // C++ [class.virtual]p1:
502 // A class that declares or inherits a virtual function is called a
503 // polymorphic class.
504 data().Polymorphic = true;
505
Sean Hunt023df372011-05-09 18:22:59 +0000506 // C++0x [class.ctor]p5
507 // A default constructor is trivial [...] if:
508 // -- its class has no virtual functions [...]
509 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000510
511 // C++0x [class.copy]p13:
512 // A copy/move constructor for class X is trivial if [...]
513 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000514 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000515 data().HasTrivialMoveConstructor = false;
516
517 // C++0x [class.copy]p27:
518 // A copy/move assignment operator for class X is trivial if [...]
519 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000520 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000521 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000522
Chandler Carrutha8225442011-04-30 09:17:45 +0000523 // C++0x [class]p7:
524 // A standard-layout class is a class that: [...]
525 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000526 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000527 }
528 }
529
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000530 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000531 // Notify that an implicit member was added after the definition
532 // was completed.
533 if (!isBeingDefined())
534 if (ASTMutationListener *L = getASTMutationListener())
535 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000536
Sean Huntffe37fd2011-05-25 20:50:04 +0000537 // If this is a special member function, note that it was added and then
538 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000539 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith61802452011-12-22 02:22:31 +0000540 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000541 data().DeclaredDefaultConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000542 if (Constructor->isConstexpr()) {
543 data().HasConstexprDefaultConstructor = true;
544 data().HasConstexprNonCopyMoveConstructor = true;
545 }
546 } else if (Constructor->isCopyConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000547 data().DeclaredCopyConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000548 if (Constructor->isConstexpr())
549 data().HasConstexprCopyConstructor = true;
550 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000551 data().DeclaredMoveConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000552 if (Constructor->isConstexpr())
553 data().HasConstexprMoveConstructor = true;
554 } else
Sean Huntffe37fd2011-05-25 20:50:04 +0000555 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000556 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000557 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000558 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000559 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000560 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
561 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000562 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000563 else if (Method->isMoveAssignmentOperator())
564 data().DeclaredMoveAssignment = true;
565 else
566 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000567 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000568 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000569
Sean Huntffe37fd2011-05-25 20:50:04 +0000570NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000571 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000572 }
573
574 // Handle (user-declared) constructors.
575 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
576 // Note that we have a user-declared constructor.
577 data().UserDeclaredConstructor = true;
578
Richard Smith017ab772011-09-05 02:13:09 +0000579 // Technically, "user-provided" is only defined for special member
580 // functions, but the intent of the standard is clearly that it should apply
581 // to all functions.
582 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000583
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000584 if (Constructor->isDefaultConstructor()) {
585 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000586 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000587 // C++0x [class.ctor]p5:
588 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000589 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000590 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000591 }
Richard Smith61802452011-12-22 02:22:31 +0000592 if (Constructor->isConstexpr()) {
593 data().HasConstexprDefaultConstructor = true;
594 data().HasConstexprNonCopyMoveConstructor = true;
595 }
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000596 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000597
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000598 // Note when we have a user-declared copy or move constructor, which will
599 // suppress the implicit declaration of those constructors.
600 if (!FunTmpl) {
601 if (Constructor->isCopyConstructor()) {
602 data().UserDeclaredCopyConstructor = true;
603 data().DeclaredCopyConstructor = true;
604
605 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000606 // A copy/move constructor for class X is trivial if it is not
607 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000608 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000609 data().HasTrivialCopyConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000610
611 if (Constructor->isConstexpr())
612 data().HasConstexprCopyConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000613 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000614 data().UserDeclaredMoveConstructor = true;
615 data().DeclaredMoveConstructor = true;
616
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000617 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000618 // A copy/move constructor for class X is trivial if it is not
619 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000620 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000621 data().HasTrivialMoveConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000622
623 if (Constructor->isConstexpr())
624 data().HasConstexprMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000625 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000626 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000627 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
628 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000629 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000630 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000631 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000632
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000633 // C++ [dcl.init.aggr]p1:
634 // An aggregate is an array or a class with no user-declared
635 // constructors [...].
636 // C++0x [dcl.init.aggr]p1:
637 // An aggregate is an array or a class with no user-provided
638 // constructors [...].
639 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
640 data().Aggregate = false;
641
642 // C++ [class]p4:
643 // A POD-struct is an aggregate class [...]
644 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
645 // type is technically an aggregate in C++0x since it wouldn't be in 03.
646 data().PlainOldData = false;
647
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000648 return;
649 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000650
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000651 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000652 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000653 data().DeclaredDestructor = true;
654 data().UserDeclaredDestructor = true;
Richard Smithdfefb842012-02-25 07:33:38 +0000655 data().HasIrrelevantDestructor = false;
656
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000657 // C++ [class]p4:
658 // A POD-struct is an aggregate class that has [...] no user-defined
659 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000660 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000661 data().PlainOldData = false;
662
Douglas Gregor45fa5602011-11-07 20:56:01 +0000663 // C++11 [class.dtor]p5:
664 // A destructor is trivial if it is not user-provided and if
665 // -- the destructor is not virtual.
Richard Smith61802452011-12-22 02:22:31 +0000666 if (DD->isUserProvided() || DD->isVirtual()) {
Sean Huntcf34e752011-05-16 22:41:40 +0000667 data().HasTrivialDestructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000668 // C++11 [dcl.constexpr]p1:
669 // The constexpr specifier shall be applied only to [...] the
670 // declaration of a static data member of a literal type.
671 // C++11 [basic.types]p10:
672 // A type is a literal type if it is [...] a class type that [...] has
673 // a trivial destructor.
674 data().DefaultedDefaultConstructorIsConstexpr = false;
675 data().DefaultedCopyConstructorIsConstexpr = false;
676 data().DefaultedMoveConstructorIsConstexpr = false;
677 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000678
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000679 return;
680 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000681
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000682 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000683 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000684 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000685 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000686 // A POD-struct is an aggregate class that [...] has no user-defined
687 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000688 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000689 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000690
Sean Huntffe37fd2011-05-25 20:50:04 +0000691 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000692
Sean Huntffe37fd2011-05-25 20:50:04 +0000693 // Suppress the implicit declaration of a copy constructor.
694 data().UserDeclaredCopyAssignment = true;
695 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000696
Sean Huntffe37fd2011-05-25 20:50:04 +0000697 // C++0x [class.copy]p27:
698 // A copy/move assignment operator for class X is trivial if it is
699 // neither user-provided nor deleted [...]
700 if (Method->isUserProvided())
701 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000702
Sean Huntffe37fd2011-05-25 20:50:04 +0000703 return;
704 }
705
706 if (Method->isMoveAssignmentOperator()) {
707 // This is an extension in C++03 mode, but we'll keep consistency by
708 // taking a move assignment operator to induce non-POD-ness
709 data().PlainOldData = false;
710
711 // This is a move assignment operator.
712 data().UserDeclaredMoveAssignment = true;
713 data().DeclaredMoveAssignment = true;
714
715 // C++0x [class.copy]p27:
716 // A copy/move assignment operator for class X is trivial if it is
717 // neither user-provided nor deleted [...]
718 if (Method->isUserProvided())
719 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000720 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000721
Douglas Gregore80622f2010-09-29 04:25:11 +0000722 // Keep the list of conversion functions up-to-date.
723 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
724 // We don't record specializations.
725 if (Conversion->getPrimaryTemplate())
726 return;
727
728 // FIXME: We intentionally don't use the decl's access here because it
729 // hasn't been set yet. That's really just a misdesign in Sema.
730
731 if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000732 if (FunTmpl->getPreviousDecl())
733 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000734 FunTmpl);
735 else
736 data().Conversions.addDecl(FunTmpl);
737 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000738 if (Conversion->getPreviousDecl())
739 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000740 Conversion);
741 else
742 data().Conversions.addDecl(Conversion);
743 }
744 }
745
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000746 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000747 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000748
749 // Handle non-static data members.
750 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000751 // C++ [class.bit]p2:
752 // A declaration for a bit-field that omits the identifier declares an
753 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
754 // initialized.
755 if (Field->isUnnamedBitfield())
756 return;
757
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000758 // C++ [dcl.init.aggr]p1:
759 // An aggregate is an array or a class (clause 9) with [...] no
760 // private or protected non-static data members (clause 11).
761 //
762 // A POD must be an aggregate.
763 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
764 data().Aggregate = false;
765 data().PlainOldData = false;
766 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000767
768 // C++0x [class]p7:
769 // A standard-layout class is a class that:
770 // [...]
771 // -- has the same access control for all non-static data members,
772 switch (D->getAccess()) {
773 case AS_private: data().HasPrivateFields = true; break;
774 case AS_protected: data().HasProtectedFields = true; break;
775 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000776 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000777 };
778 if ((data().HasPrivateFields + data().HasProtectedFields +
779 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000780 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000781
Douglas Gregor2bb11012011-05-13 01:05:07 +0000782 // Keep track of the presence of mutable fields.
783 if (Field->isMutable())
784 data().HasMutableFields = true;
785
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000786 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000787 // A POD struct is a class that is both a trivial class and a
788 // standard-layout class, and has no non-static data members of type
789 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000790 //
791 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
792 // that does not explicitly have no lifetime makes the class a non-POD.
793 // However, we delay setting PlainOldData to false in this case so that
794 // Sema has a chance to diagnostic causes where the same class will be
795 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
796 // In this case, the class will become a non-POD class when we complete
797 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000798 ASTContext &Context = getASTContext();
799 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000800 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
801 if (!Context.getLangOptions().ObjCAutoRefCount ||
802 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
803 setHasObjectMember(true);
804 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000805 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000806
Chandler Carrutha8225442011-04-30 09:17:45 +0000807 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000808 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000809
Chandler Carrutha8225442011-04-30 09:17:45 +0000810 // C++0x [class]p7:
811 // A standard-layout class is a class that:
812 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000813 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000814 }
815
Richard Smith86c3ae42012-02-13 03:54:03 +0000816 // Record if this field is the first non-literal or volatile field or base.
817 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000818 data().HasNonLiteralTypeFieldsOrBases = true;
819
Richard Smith7a614d82011-06-11 17:19:42 +0000820 if (Field->hasInClassInitializer()) {
821 // C++0x [class]p5:
822 // A default constructor is trivial if [...] no non-static data member
823 // of its class has a brace-or-equal-initializer.
824 data().HasTrivialDefaultConstructor = false;
825
826 // C++0x [dcl.init.aggr]p1:
827 // An aggregate is a [...] class with [...] no
828 // brace-or-equal-initializers for non-static data members.
829 data().Aggregate = false;
830
831 // C++0x [class]p10:
832 // A POD struct is [...] a trivial class.
833 data().PlainOldData = false;
834 }
835
Douglas Gregor85606eb2010-09-28 20:50:54 +0000836 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
837 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
838 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000839 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000840 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000841 // -- for all the non-static data members of its class that are of
842 // class type (or array thereof), each such class has a trivial
843 // default constructor.
844 if (!FieldRec->hasTrivialDefaultConstructor())
845 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000846
847 // C++0x [class.copy]p13:
848 // A copy/move constructor for class X is trivial if [...]
849 // [...]
850 // -- for each non-static data member of X that is of class type (or
851 // an array thereof), the constructor selected to copy/move that
852 // member is trivial;
853 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000854 if (!FieldRec->hasTrivialCopyConstructor())
855 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000856 if (!FieldRec->hasTrivialMoveConstructor())
857 data().HasTrivialMoveConstructor = false;
858
859 // C++0x [class.copy]p27:
860 // A copy/move assignment operator for class X is trivial if [...]
861 // [...]
862 // -- for each non-static data member of X that is of class type (or
863 // an array thereof), the assignment operator selected to
864 // copy/move that member is trivial;
865 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000866 if (!FieldRec->hasTrivialCopyAssignment())
867 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000868 if (!FieldRec->hasTrivialMoveAssignment())
869 data().HasTrivialMoveAssignment = false;
870
Douglas Gregor85606eb2010-09-28 20:50:54 +0000871 if (!FieldRec->hasTrivialDestructor())
872 data().HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +0000873 if (!FieldRec->hasIrrelevantDestructor())
874 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000875 if (FieldRec->hasObjectMember())
876 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000877
878 // C++0x [class]p7:
879 // A standard-layout class is a class that:
880 // -- has no non-static data members of type non-standard-layout
881 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000882 if (!FieldRec->isStandardLayout())
883 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000884
885 // C++0x [class]p7:
886 // A standard-layout class is a class that:
887 // [...]
888 // -- has no base classes of the same type as the first non-static
889 // data member.
890 // We don't want to expend bits in the state of the record decl
891 // tracking whether this is the first non-static data member so we
892 // cheat a bit and use some of the existing state: the empty bit.
893 // Virtual bases and virtual methods make a class non-empty, but they
894 // also make it non-standard-layout so we needn't check here.
895 // A non-empty base class may leave the class standard-layout, but not
896 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000897 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000898 // data member must come through here with Empty still true, and Empty
899 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000900 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000901 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
902 BE = bases_end();
903 BI != BE; ++BI) {
904 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000905 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000906 break;
907 }
908 }
909 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000910
911 // Keep track of the presence of mutable fields.
912 if (FieldRec->hasMutableFields())
913 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000914
915 // C++11 [class.copy]p13:
916 // If the implicitly-defined constructor would satisfy the
917 // requirements of a constexpr constructor, the implicitly-defined
918 // constructor is constexpr.
919 // C++11 [dcl.constexpr]p4:
920 // -- every constructor involved in initializing non-static data
921 // members [...] shall be a constexpr constructor
922 if (!Field->hasInClassInitializer() &&
923 !FieldRec->hasConstexprDefaultConstructor())
924 // The standard requires any in-class initializer to be a constant
925 // expression. We consider this to be a defect.
926 data().DefaultedDefaultConstructorIsConstexpr = false;
927
928 if (!FieldRec->hasConstexprCopyConstructor())
929 data().DefaultedCopyConstructorIsConstexpr = false;
930
931 if (FieldRec->hasDeclaredMoveConstructor() ||
932 FieldRec->needsImplicitMoveConstructor())
933 // FIXME: If the implicit move constructor generated for the member's
934 // class would be ill-formed, the implicit move constructor generated
935 // for this class calls the member's copy constructor.
936 data().DefaultedMoveConstructorIsConstexpr &=
937 FieldRec->hasConstexprMoveConstructor();
938 else if (!FieldRec->hasConstexprCopyConstructor())
939 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000940 }
Richard Smith61802452011-12-22 02:22:31 +0000941 } else {
942 // Base element type of field is a non-class type.
943 if (!T->isLiteralType()) {
944 data().DefaultedDefaultConstructorIsConstexpr = false;
945 data().DefaultedCopyConstructorIsConstexpr = false;
946 data().DefaultedMoveConstructorIsConstexpr = false;
947 } else if (!Field->hasInClassInitializer())
948 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000949 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000950
951 // C++0x [class]p7:
952 // A standard-layout class is a class that:
953 // [...]
954 // -- either has no non-static data members in the most derived
955 // class and at most one base class with non-static data members,
956 // or has no base classes with non-static data members, and
957 // At this point we know that we have a non-static data member, so the last
958 // clause holds.
959 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000960 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000961
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000962 // If this is not a zero-length bit-field, then the class is not empty.
963 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000964 if (!Field->isBitField() ||
965 (!Field->getBitWidth()->isTypeDependent() &&
966 !Field->getBitWidth()->isValueDependent() &&
967 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000968 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000969 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000970 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000971
972 // Handle using declarations of conversion functions.
973 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
974 if (Shadow->getDeclName().getNameKind()
975 == DeclarationName::CXXConversionFunctionName)
976 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000977}
978
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000979bool CXXRecordDecl::isCLike() const {
980 if (getTagKind() == TTK_Class || !TemplateOrInstantiation.isNull())
981 return false;
982 if (!hasDefinition())
983 return true;
984
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000985 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000986}
987
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000988void CXXRecordDecl::getCaptureFields(
989 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000990 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000991 Captures.clear();
992 ThisCapture = 0;
993
Douglas Gregorda8962a2012-02-13 15:44:47 +0000994 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000995 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000996 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000997 C != CEnd; ++C, ++Field) {
998 if (C->capturesThis()) {
999 ThisCapture = *Field;
1000 continue;
1001 }
1002
1003 Captures[C->getCapturedVar()] = *Field;
1004 }
1005}
1006
1007
John McCallb05b5f32010-03-15 09:07:48 +00001008static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1009 QualType T;
John McCall32daa422010-03-31 01:36:47 +00001010 if (isa<UsingShadowDecl>(Conv))
1011 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +00001012 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
1013 T = ConvTemp->getTemplatedDecl()->getResultType();
1014 else
1015 T = cast<CXXConversionDecl>(Conv)->getConversionType();
1016 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +00001017}
1018
John McCallb05b5f32010-03-15 09:07:48 +00001019/// Collect the visible conversions of a base class.
1020///
1021/// \param Base a base class of the class we're considering
1022/// \param InVirtual whether this base class is a virtual base (or a base
1023/// of a virtual base)
1024/// \param Access the access along the inheritance path to this base
1025/// \param ParentHiddenTypes the conversions provided by the inheritors
1026/// of this base
1027/// \param Output the set to which to add conversions from non-virtual bases
1028/// \param VOutput the set to which to add conversions from virtual bases
1029/// \param HiddenVBaseCs the set of conversions which were hidden in a
1030/// virtual base along some inheritance path
1031static void CollectVisibleConversions(ASTContext &Context,
1032 CXXRecordDecl *Record,
1033 bool InVirtual,
1034 AccessSpecifier Access,
1035 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
1036 UnresolvedSetImpl &Output,
1037 UnresolvedSetImpl &VOutput,
1038 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1039 // The set of types which have conversions in this class or its
1040 // subclasses. As an optimization, we don't copy the derived set
1041 // unless it might change.
1042 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1043 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1044
1045 // Collect the direct conversions and figure out which conversions
1046 // will be hidden in the subclasses.
1047 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1048 if (!Cs.empty()) {
1049 HiddenTypesBuffer = ParentHiddenTypes;
1050 HiddenTypes = &HiddenTypesBuffer;
1051
1052 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
1053 bool Hidden =
1054 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
1055
1056 // If this conversion is hidden and we're in a virtual base,
1057 // remember that it's hidden along some inheritance path.
1058 if (Hidden && InVirtual)
1059 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1060
1061 // If this conversion isn't hidden, add it to the appropriate output.
1062 else if (!Hidden) {
1063 AccessSpecifier IAccess
1064 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1065
1066 if (InVirtual)
1067 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001068 else
John McCallb05b5f32010-03-15 09:07:48 +00001069 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001070 }
1071 }
1072 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001073
John McCallb05b5f32010-03-15 09:07:48 +00001074 // Collect information recursively from any base classes.
1075 for (CXXRecordDecl::base_class_iterator
1076 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1077 const RecordType *RT = I->getType()->getAs<RecordType>();
1078 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001079
John McCallb05b5f32010-03-15 09:07:48 +00001080 AccessSpecifier BaseAccess
1081 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1082 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001083
John McCallb05b5f32010-03-15 09:07:48 +00001084 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1085 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1086 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001087 }
John McCallb05b5f32010-03-15 09:07:48 +00001088}
Sebastian Redl9994a342009-10-25 17:03:50 +00001089
John McCallb05b5f32010-03-15 09:07:48 +00001090/// Collect the visible conversions of a class.
1091///
1092/// This would be extremely straightforward if it weren't for virtual
1093/// bases. It might be worth special-casing that, really.
1094static void CollectVisibleConversions(ASTContext &Context,
1095 CXXRecordDecl *Record,
1096 UnresolvedSetImpl &Output) {
1097 // The collection of all conversions in virtual bases that we've
1098 // found. These will be added to the output as long as they don't
1099 // appear in the hidden-conversions set.
1100 UnresolvedSet<8> VBaseCs;
1101
1102 // The set of conversions in virtual bases that we've determined to
1103 // be hidden.
1104 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1105
1106 // The set of types hidden by classes derived from this one.
1107 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1108
1109 // Go ahead and collect the direct conversions and add them to the
1110 // hidden-types set.
1111 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1112 Output.append(Cs.begin(), Cs.end());
1113 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1114 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1115
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())))
1131 Output.addDecl(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.
John McCalleec51cf2010-01-20 00:46:10 +00001137const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001138 // If root class, all conversions are visible.
1139 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +00001140 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +00001141 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +00001142 if (data().ComputedVisibleConversions)
1143 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +00001144 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001145 data().ComputedVisibleConversions = true;
1146 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001147}
1148
John McCall32daa422010-03-31 01:36:47 +00001149void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1150 // This operation is O(N) but extremely rare. Sema only uses it to
1151 // remove UsingShadowDecls in a class that were followed by a direct
1152 // declaration, e.g.:
1153 // class A : B {
1154 // using B::operator int;
1155 // operator int();
1156 // };
1157 // This is uncommon by itself and even more uncommon in conjunction
1158 // with sufficiently large numbers of directly-declared conversions
1159 // that asymptotic behavior matters.
1160
1161 UnresolvedSetImpl &Convs = *getConversionFunctions();
1162 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1163 if (Convs[I].getDecl() == ConvDecl) {
1164 Convs.erase(I);
1165 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1166 && "conversion was found multiple times in unresolved set");
1167 return;
1168 }
1169 }
1170
1171 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001172}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001173
Douglas Gregorf6b11852009-10-08 15:14:33 +00001174CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001175 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001176 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1177
1178 return 0;
1179}
1180
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001181MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1182 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1183}
1184
Douglas Gregorf6b11852009-10-08 15:14:33 +00001185void
1186CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1187 TemplateSpecializationKind TSK) {
1188 assert(TemplateOrInstantiation.isNull() &&
1189 "Previous template or instantiation?");
1190 assert(!isa<ClassTemplateSpecializationDecl>(this));
1191 TemplateOrInstantiation
1192 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1193}
1194
Anders Carlssonb13e3572009-12-07 06:33:48 +00001195TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1196 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001197 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1198 return Spec->getSpecializationKind();
1199
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001200 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001201 return MSInfo->getTemplateSpecializationKind();
1202
1203 return TSK_Undeclared;
1204}
1205
1206void
1207CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1208 if (ClassTemplateSpecializationDecl *Spec
1209 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1210 Spec->setSpecializationKind(TSK);
1211 return;
1212 }
1213
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001214 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001215 MSInfo->setTemplateSpecializationKind(TSK);
1216 return;
1217 }
1218
David Blaikieb219cfc2011-09-23 05:06:16 +00001219 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001220}
1221
Douglas Gregor1d110e02010-07-01 14:13:13 +00001222CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1223 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001224 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001225
1226 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001227 = Context.DeclarationNames.getCXXDestructorName(
1228 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001229
John McCallc0bf4622010-02-23 00:48:20 +00001230 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001231 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001232 if (I == E)
1233 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001235 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001236 return Dtor;
1237}
1238
Douglas Gregorda2142f2011-02-19 18:51:44 +00001239void CXXRecordDecl::completeDefinition() {
1240 completeDefinition(0);
1241}
1242
1243void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1244 RecordDecl::completeDefinition();
1245
John McCallf85e1932011-06-15 23:02:42 +00001246 if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) {
1247 // Objective-C Automatic Reference Counting:
1248 // If a class has a non-static data member of Objective-C pointer
1249 // type (or array thereof), it is a non-POD type and its
1250 // default constructor (if any), copy constructor, copy assignment
1251 // operator, and destructor are non-trivial.
1252 struct DefinitionData &Data = data();
1253 Data.PlainOldData = false;
1254 Data.HasTrivialDefaultConstructor = false;
1255 Data.HasTrivialCopyConstructor = false;
1256 Data.HasTrivialCopyAssignment = false;
1257 Data.HasTrivialDestructor = false;
Richard Smithdfefb842012-02-25 07:33:38 +00001258 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001259 }
1260
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001261 // If the class may be abstract (but hasn't been marked as such), check for
1262 // any pure final overriders.
1263 if (mayBeAbstract()) {
1264 CXXFinalOverriderMap MyFinalOverriders;
1265 if (!FinalOverriders) {
1266 getFinalOverriders(MyFinalOverriders);
1267 FinalOverriders = &MyFinalOverriders;
1268 }
1269
1270 bool Done = false;
1271 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1272 MEnd = FinalOverriders->end();
1273 M != MEnd && !Done; ++M) {
1274 for (OverridingMethods::iterator SO = M->second.begin(),
1275 SOEnd = M->second.end();
1276 SO != SOEnd && !Done; ++SO) {
1277 assert(SO->second.size() > 0 &&
1278 "All virtual functions have overridding virtual functions");
1279
1280 // C++ [class.abstract]p4:
1281 // A class is abstract if it contains or inherits at least one
1282 // pure virtual function for which the final overrider is pure
1283 // virtual.
1284 if (SO->second.front().Method->isPure()) {
1285 data().Abstract = true;
1286 Done = true;
1287 break;
1288 }
1289 }
1290 }
1291 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001292
1293 // Set access bits correctly on the directly-declared conversions.
1294 for (UnresolvedSetIterator I = data().Conversions.begin(),
1295 E = data().Conversions.end();
1296 I != E; ++I)
1297 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001298}
1299
1300bool CXXRecordDecl::mayBeAbstract() const {
1301 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1302 isDependentContext())
1303 return false;
1304
1305 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1306 BEnd = bases_end();
1307 B != BEnd; ++B) {
1308 CXXRecordDecl *BaseDecl
1309 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1310 if (BaseDecl->isAbstract())
1311 return true;
1312 }
1313
1314 return false;
1315}
1316
David Blaikie99ba9e32011-12-20 02:48:34 +00001317void CXXMethodDecl::anchor() { }
1318
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001319CXXMethodDecl *
1320CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001321 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001322 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001323 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001324 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001325 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001326 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001327 isStatic, SCAsWritten, isInline, isConstexpr,
1328 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001329}
1330
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001331CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1332 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1333 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1334 DeclarationNameInfo(), QualType(),
1335 0, false, SC_None, false, false,
1336 SourceLocation());
1337}
1338
Douglas Gregor90916562009-09-29 18:16:17 +00001339bool CXXMethodDecl::isUsualDeallocationFunction() const {
1340 if (getOverloadedOperator() != OO_Delete &&
1341 getOverloadedOperator() != OO_Array_Delete)
1342 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001343
1344 // C++ [basic.stc.dynamic.deallocation]p2:
1345 // A template instance is never a usual deallocation function,
1346 // regardless of its signature.
1347 if (getPrimaryTemplate())
1348 return false;
1349
Douglas Gregor90916562009-09-29 18:16:17 +00001350 // C++ [basic.stc.dynamic.deallocation]p2:
1351 // If a class T has a member deallocation function named operator delete
1352 // with exactly one parameter, then that function is a usual (non-placement)
1353 // deallocation function. [...]
1354 if (getNumParams() == 1)
1355 return true;
1356
1357 // C++ [basic.stc.dynamic.deallocation]p2:
1358 // [...] If class T does not declare such an operator delete but does
1359 // declare a member deallocation function named operator delete with
1360 // exactly two parameters, the second of which has type std::size_t (18.1),
1361 // then this function is a usual deallocation function.
1362 ASTContext &Context = getASTContext();
1363 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001364 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1365 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001366 return false;
1367
1368 // This function is a usual deallocation function if there are no
1369 // single-parameter deallocation functions of the same kind.
1370 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1371 R.first != R.second; ++R.first) {
1372 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1373 if (FD->getNumParams() == 1)
1374 return false;
1375 }
1376
1377 return true;
1378}
1379
Douglas Gregor06a9f362010-05-01 20:49:11 +00001380bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001381 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001382 // A user-declared copy assignment operator X::operator= is a non-static
1383 // non-template member function of class X with exactly one parameter of
1384 // type X, X&, const X&, volatile X& or const volatile X&.
1385 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1386 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001387 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001388 return false;
1389
1390 QualType ParamType = getParamDecl(0)->getType();
1391 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1392 ParamType = Ref->getPointeeType();
1393
1394 ASTContext &Context = getASTContext();
1395 QualType ClassType
1396 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1397 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1398}
1399
Sean Huntffe37fd2011-05-25 20:50:04 +00001400bool CXXMethodDecl::isMoveAssignmentOperator() const {
1401 // C++0x [class.copy]p19:
1402 // A user-declared move assignment operator X::operator= is a non-static
1403 // non-template member function of class X with exactly one parameter of type
1404 // X&&, const X&&, volatile X&&, or const volatile X&&.
1405 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1406 getPrimaryTemplate() || getDescribedFunctionTemplate())
1407 return false;
1408
1409 QualType ParamType = getParamDecl(0)->getType();
1410 if (!isa<RValueReferenceType>(ParamType))
1411 return false;
1412 ParamType = ParamType->getPointeeType();
1413
1414 ASTContext &Context = getASTContext();
1415 QualType ClassType
1416 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1417 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1418}
1419
Anders Carlsson05eb2442009-05-16 23:58:37 +00001420void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001421 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001422 assert(!MD->getParent()->isDependentContext() &&
1423 "Can't add an overridden method to a class template!");
1424
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001425 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001426}
1427
1428CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001429 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001430}
1431
1432CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001433 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001434}
1435
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001436unsigned CXXMethodDecl::size_overridden_methods() const {
1437 return getASTContext().overridden_methods_size(this);
1438}
1439
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001440QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001441 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1442 // If the member function is declared const, the type of this is const X*,
1443 // if the member function is declared volatile, the type of this is
1444 // volatile X*, and if the member function is declared const volatile,
1445 // the type of this is const volatile X*.
1446
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001447 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001448
John McCall3cb0ebd2010-03-10 03:28:59 +00001449 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001450 ClassTy = C.getQualifiedType(ClassTy,
1451 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001452 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001453}
1454
Eli Friedmand7d7f672009-12-06 20:50:05 +00001455bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001456 // If this function is a template instantiation, look at the template from
1457 // which it was instantiated.
1458 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1459 if (!CheckFn)
1460 CheckFn = this;
1461
Eli Friedmand7d7f672009-12-06 20:50:05 +00001462 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001463 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001464}
1465
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001466bool CXXMethodDecl::isLambdaStaticInvoker() const {
1467 return getParent()->isLambda() &&
1468 getIdentifier() && getIdentifier()->getName() == "__invoke";
1469}
1470
1471
Sean Huntcbb67482011-01-08 20:30:50 +00001472CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1473 TypeSourceInfo *TInfo, bool IsVirtual,
1474 SourceLocation L, Expr *Init,
1475 SourceLocation R,
1476 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001477 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001478 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1479 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001480{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001481}
1482
Sean Huntcbb67482011-01-08 20:30:50 +00001483CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1484 FieldDecl *Member,
1485 SourceLocation MemberLoc,
1486 SourceLocation L, Expr *Init,
1487 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001488 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001489 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001490 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1491{
1492}
1493
Sean Huntcbb67482011-01-08 20:30:50 +00001494CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1495 IndirectFieldDecl *Member,
1496 SourceLocation MemberLoc,
1497 SourceLocation L, Expr *Init,
1498 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001499 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001500 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001501 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001502{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001503}
1504
Sean Huntcbb67482011-01-08 20:30:50 +00001505CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001506 TypeSourceInfo *TInfo,
1507 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001508 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001509 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1510 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001511 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1512{
1513}
1514
1515CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001516 FieldDecl *Member,
1517 SourceLocation MemberLoc,
1518 SourceLocation L, Expr *Init,
1519 SourceLocation R,
1520 VarDecl **Indices,
1521 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001522 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001523 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001524 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001525{
1526 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1527 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1528}
1529
Sean Huntcbb67482011-01-08 20:30:50 +00001530CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1531 FieldDecl *Member,
1532 SourceLocation MemberLoc,
1533 SourceLocation L, Expr *Init,
1534 SourceLocation R,
1535 VarDecl **Indices,
1536 unsigned NumIndices) {
1537 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001538 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001539 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001540 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1541 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001542}
1543
Sean Huntcbb67482011-01-08 20:30:50 +00001544TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001545 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001546 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001547 else
1548 return TypeLoc();
1549}
1550
Sean Huntcbb67482011-01-08 20:30:50 +00001551const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001552 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001553 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001554 else
1555 return 0;
1556}
1557
Sean Huntcbb67482011-01-08 20:30:50 +00001558SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001559 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001560 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001561
1562 if (isInClassMemberInitializer())
1563 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001564
Douglas Gregor76852c22011-11-01 01:16:03 +00001565 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1566 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1567
1568 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001569}
1570
Sean Huntcbb67482011-01-08 20:30:50 +00001571SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001572 if (isInClassMemberInitializer()) {
1573 FieldDecl *D = getAnyMember();
1574 if (Expr *I = D->getInClassInitializer())
1575 return I->getSourceRange();
1576 return SourceRange();
1577 }
1578
Douglas Gregor802ab452009-12-02 22:36:29 +00001579 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001580}
1581
David Blaikie99ba9e32011-12-20 02:48:34 +00001582void CXXConstructorDecl::anchor() { }
1583
Douglas Gregorb48fe382008-10-31 09:07:45 +00001584CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001585CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1586 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1587 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1588 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001589}
1590
1591CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001592CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001593 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001594 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001595 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001596 bool isExplicit, bool isInline,
1597 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001598 assert(NameInfo.getName().getNameKind()
1599 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001600 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001601 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001602 isExplicit, isInline, isImplicitlyDeclared,
1603 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001604}
1605
Douglas Gregor76852c22011-11-01 01:16:03 +00001606CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1607 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1608 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1609 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1610 return Construct->getConstructor();
1611
1612 return 0;
1613}
1614
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001615bool CXXConstructorDecl::isDefaultConstructor() const {
1616 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001617 // A default constructor for a class X is a constructor of class
1618 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001619 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001620 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001621}
1622
Mike Stump1eb44332009-09-09 15:08:12 +00001623bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001624CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001625 return isCopyOrMoveConstructor(TypeQuals) &&
1626 getParamDecl(0)->getType()->isLValueReferenceType();
1627}
1628
1629bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1630 return isCopyOrMoveConstructor(TypeQuals) &&
1631 getParamDecl(0)->getType()->isRValueReferenceType();
1632}
1633
1634/// \brief Determine whether this is a copy or move constructor.
1635bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001636 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001637 // A non-template constructor for class X is a copy constructor
1638 // if its first parameter is of type X&, const X&, volatile X& or
1639 // const volatile X&, and either there are no other parameters
1640 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001641 // C++0x [class.copy]p3:
1642 // A non-template constructor for class X is a move constructor if its
1643 // first parameter is of type X&&, const X&&, volatile X&&, or
1644 // const volatile X&&, and either there are no other parameters or else
1645 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001646 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001647 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001648 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001649 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001650 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001651
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001652 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001653
1654 // Do we have a reference type?
1655 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001656 if (!ParamRefType)
1657 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001658
Douglas Gregorfd476482009-11-13 23:59:09 +00001659 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001660 ASTContext &Context = getASTContext();
1661
Douglas Gregorfd476482009-11-13 23:59:09 +00001662 CanQualType PointeeType
1663 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001664 CanQualType ClassTy
1665 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001666 if (PointeeType.getUnqualifiedType() != ClassTy)
1667 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001668
John McCall0953e762009-09-24 19:53:00 +00001669 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001670
1671 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001672 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001673 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001674}
1675
Anders Carlssonfaccd722009-08-28 16:57:08 +00001676bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001677 // C++ [class.conv.ctor]p1:
1678 // A constructor declared without the function-specifier explicit
1679 // that can be called with a single parameter specifies a
1680 // conversion from the type of its first parameter to the type of
1681 // its class. Such a constructor is called a converting
1682 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001683 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001684 return false;
1685
Mike Stump1eb44332009-09-09 15:08:12 +00001686 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001687 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001688 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001689 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001690}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001691
Douglas Gregor6493cc52010-11-08 17:16:59 +00001692bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001693 if ((getNumParams() < 1) ||
1694 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1695 (getPrimaryTemplate() == 0) ||
1696 (getDescribedFunctionTemplate() != 0))
1697 return false;
1698
1699 const ParmVarDecl *Param = getParamDecl(0);
1700
1701 ASTContext &Context = getASTContext();
1702 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1703
Douglas Gregor66724ea2009-11-14 01:20:54 +00001704 // Is it the same as our our class type?
1705 CanQualType ClassTy
1706 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1707 if (ParamType.getUnqualifiedType() != ClassTy)
1708 return false;
1709
1710 return true;
1711}
1712
Sebastian Redlf677ea32011-02-05 19:23:19 +00001713const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1714 // Hack: we store the inherited constructor in the overridden method table
1715 method_iterator It = begin_overridden_methods();
1716 if (It == end_overridden_methods())
1717 return 0;
1718
1719 return cast<CXXConstructorDecl>(*It);
1720}
1721
1722void
1723CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1724 // Hack: we store the inherited constructor in the overridden method table
1725 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1726 addOverriddenMethod(BaseCtor);
1727}
1728
David Blaikie99ba9e32011-12-20 02:48:34 +00001729void CXXDestructorDecl::anchor() { }
1730
Douglas Gregor42a552f2008-11-05 20:51:48 +00001731CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001732CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1733 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1734 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001735 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001736}
1737
1738CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001739CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001740 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001741 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001742 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001743 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001744 assert(NameInfo.getName().getNameKind()
1745 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001746 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001747 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001748 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001749}
1750
David Blaikie99ba9e32011-12-20 02:48:34 +00001751void CXXConversionDecl::anchor() { }
1752
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001753CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001754CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1755 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1756 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1757 QualType(), 0, false, false, false,
1758 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001759}
1760
1761CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001762CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001763 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001764 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001765 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001766 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001767 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001768 assert(NameInfo.getName().getNameKind()
1769 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001770 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001771 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001772 isInline, isExplicit, isConstexpr,
1773 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001774}
1775
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001776bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1777 return isImplicit() && getParent()->isLambda() &&
1778 getConversionType()->isBlockPointerType();
1779}
1780
David Blaikie99ba9e32011-12-20 02:48:34 +00001781void LinkageSpecDecl::anchor() { }
1782
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001783LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001784 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001785 SourceLocation ExternLoc,
1786 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001787 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001788 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001789 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001790}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001791
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001792LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1793 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1794 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1795 lang_c, SourceLocation());
1796}
1797
David Blaikie99ba9e32011-12-20 02:48:34 +00001798void UsingDirectiveDecl::anchor() { }
1799
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001800UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1801 SourceLocation L,
1802 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001803 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001804 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001805 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001806 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001807 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1808 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001809 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1810 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001811}
1812
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001813UsingDirectiveDecl *
1814UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1815 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1816 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1817 NestedNameSpecifierLoc(),
1818 SourceLocation(), 0, 0);
1819}
1820
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001821NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1822 if (NamespaceAliasDecl *NA =
1823 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1824 return NA->getNamespace();
1825 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1826}
1827
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001828void NamespaceDecl::anchor() { }
1829
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001830NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1831 SourceLocation StartLoc,
1832 SourceLocation IdLoc, IdentifierInfo *Id,
1833 NamespaceDecl *PrevDecl)
1834 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1835 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1836{
1837 setPreviousDeclaration(PrevDecl);
1838
1839 if (PrevDecl)
1840 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1841}
1842
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001843NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001844 bool Inline, SourceLocation StartLoc,
1845 SourceLocation IdLoc, IdentifierInfo *Id,
1846 NamespaceDecl *PrevDecl) {
1847 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001848}
1849
1850NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1851 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001852 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1853 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001854}
1855
David Blaikie99ba9e32011-12-20 02:48:34 +00001856void NamespaceAliasDecl::anchor() { }
1857
Mike Stump1eb44332009-09-09 15:08:12 +00001858NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001859 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001860 SourceLocation AliasLoc,
1861 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001862 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001863 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001864 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001865 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1866 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001867 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1868 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001869}
1870
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001871NamespaceAliasDecl *
1872NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1873 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1874 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1875 NestedNameSpecifierLoc(),
1876 SourceLocation(), 0);
1877}
1878
David Blaikie99ba9e32011-12-20 02:48:34 +00001879void UsingShadowDecl::anchor() { }
1880
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001881UsingShadowDecl *
1882UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1883 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1884 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1885}
1886
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001887UsingDecl *UsingShadowDecl::getUsingDecl() const {
1888 const UsingShadowDecl *Shadow = this;
1889 while (const UsingShadowDecl *NextShadow =
1890 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1891 Shadow = NextShadow;
1892 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1893}
1894
David Blaikie99ba9e32011-12-20 02:48:34 +00001895void UsingDecl::anchor() { }
1896
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001897void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1898 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1899 "declaration already in set");
1900 assert(S->getUsingDecl() == this);
1901
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001902 if (FirstUsingShadow.getPointer())
1903 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1904 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001905}
1906
1907void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1908 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1909 "declaration not in set");
1910 assert(S->getUsingDecl() == this);
1911
1912 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1913
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001914 if (FirstUsingShadow.getPointer() == S) {
1915 FirstUsingShadow.setPointer(
1916 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001917 S->UsingOrNextShadow = this;
1918 return;
1919 }
1920
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001921 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001922 while (Prev->UsingOrNextShadow != S)
1923 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1924 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1925 S->UsingOrNextShadow = this;
1926}
1927
Douglas Gregordc355712011-02-25 00:36:19 +00001928UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1929 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001930 const DeclarationNameInfo &NameInfo,
1931 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001932 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001933}
1934
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001935UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1936 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1937 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1938 DeclarationNameInfo(), false);
1939}
1940
David Blaikie99ba9e32011-12-20 02:48:34 +00001941void UnresolvedUsingValueDecl::anchor() { }
1942
John McCall7ba107a2009-11-18 02:36:19 +00001943UnresolvedUsingValueDecl *
1944UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1945 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001946 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001947 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001948 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001949 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001950}
1951
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001952UnresolvedUsingValueDecl *
1953UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1954 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1955 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1956 NestedNameSpecifierLoc(),
1957 DeclarationNameInfo());
1958}
1959
David Blaikie99ba9e32011-12-20 02:48:34 +00001960void UnresolvedUsingTypenameDecl::anchor() { }
1961
John McCall7ba107a2009-11-18 02:36:19 +00001962UnresolvedUsingTypenameDecl *
1963UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1964 SourceLocation UsingLoc,
1965 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001966 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001967 SourceLocation TargetNameLoc,
1968 DeclarationName TargetName) {
1969 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001970 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001971 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001972}
1973
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001974UnresolvedUsingTypenameDecl *
1975UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1976 void *Mem = AllocateDeserializedDecl(C, ID,
1977 sizeof(UnresolvedUsingTypenameDecl));
1978 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
1979 SourceLocation(),
1980 NestedNameSpecifierLoc(),
1981 SourceLocation(),
1982 0);
1983}
1984
David Blaikie99ba9e32011-12-20 02:48:34 +00001985void StaticAssertDecl::anchor() { }
1986
Anders Carlssonfb311762009-03-14 00:25:26 +00001987StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001988 SourceLocation StaticAssertLoc,
1989 Expr *AssertExpr,
1990 StringLiteral *Message,
1991 SourceLocation RParenLoc) {
1992 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1993 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001994}
1995
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001996StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
1997 unsigned ID) {
1998 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
1999 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,SourceLocation());
2000}
2001
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002002static const char *getAccessName(AccessSpecifier AS) {
2003 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002004 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002005 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002006 case AS_public:
2007 return "public";
2008 case AS_private:
2009 return "private";
2010 case AS_protected:
2011 return "protected";
2012 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002013 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002014}
2015
2016const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2017 AccessSpecifier AS) {
2018 return DB << getAccessName(AS);
2019}
Richard Smithf15fda02012-02-02 01:16:57 +00002020
2021const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2022 AccessSpecifier AS) {
2023 return DB << getAccessName(AS);
2024}