blob: 257a507b236debe5c7a6d09f7d80b878007dc135 [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
Douglas Gregorda8962a2012-02-13 15:44:47 +000038void CXXRecordDecl::LambdaDefinitionData::allocateExtra(
39 ArrayRef<LambdaExpr::Capture> Captures,
40 ArrayRef<Expr *> CaptureInits,
41 Stmt *Body) {
42 NumCaptures = Captures.size();
43 NumExplicitCaptures = 0;
44
45 ASTContext &Context = Definition->getASTContext();
46 this->Extra = Context.Allocate(sizeof(Capture) * Captures.size() +
47 sizeof(Stmt*) * (Captures.size() + 1));
48
49 // Copy captures.
50 Capture *ToCapture = getCaptures();
51 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
52 if (Captures[I].isExplicit())
53 ++NumExplicitCaptures;
54
55 *ToCapture++ = Captures[I];
56 }
57
58 // Copy initialization expressions for the non-static data members.
59 Stmt **Stored = getStoredStmts();
60 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
61 *Stored++ = CaptureInits[I];
62
63 // Copy the body of the lambda.
64 *Stored++ = Body;
65}
66
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000067
John McCall86ff3082010-02-04 22:26:26 +000068CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
69 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000070 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
71 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000072 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000073 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000074 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000075 HasMutableFields(false), HasOnlyCMembers(true),
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +000076 HasTrivialDefaultConstructor(true),
Richard Smith61802452011-12-22 02:22:31 +000077 HasConstexprNonCopyMoveConstructor(false),
78 DefaultedDefaultConstructorIsConstexpr(true),
79 DefaultedCopyConstructorIsConstexpr(true),
80 DefaultedMoveConstructorIsConstexpr(true),
81 HasConstexprDefaultConstructor(false), HasConstexprCopyConstructor(false),
82 HasConstexprMoveConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000083 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
84 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
85 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000086 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000087 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
88 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sebastian Redl85ea7aa2011-08-30 19:58:05 +000089 DeclaredDestructor(false), FailedImplicitMoveConstructor(false),
Eli Friedman72899c32012-01-07 04:59:52 +000090 FailedImplicitMoveAssignment(false), IsLambda(false), NumBases(0),
91 NumVBases(0), Bases(), VBases(), Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000092}
93
94CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000095 SourceLocation StartLoc, SourceLocation IdLoc,
96 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
97 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000098 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000099 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +0000100
Jay Foad4ba2a172011-01-12 09:06:06 +0000101CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000102 DeclContext *DC, SourceLocation StartLoc,
103 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000104 CXXRecordDecl* PrevDecl,
105 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000106 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
107 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000109 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000110 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +0000111 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000112 return R;
113}
114
Douglas Gregorda8962a2012-02-13 15:44:47 +0000115CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
116 SourceLocation Loc) {
117 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
118 0, 0);
119 R->IsBeingDefined = true;
120 R->DefinitionData = new (C) struct LambdaDefinitionData(R);
121 C.getTypeDeclType(R, /*PrevDecl=*/0);
122 return R;
123}
124
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000125CXXRecordDecl *
126CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
127 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
128 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
129 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000130}
131
Mike Stump1eb44332009-09-09 15:08:12 +0000132void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000133CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000134 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000135 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000136
Douglas Gregor7c789c12010-10-29 22:39:52 +0000137 if (!data().Bases.isOffset() && data().NumBases > 0)
138 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Richard Smithdd677232011-10-18 20:08:55 +0000140 if (NumBases) {
141 // C++ [dcl.init.aggr]p1:
142 // An aggregate is [...] a class with [...] no base classes [...].
143 data().Aggregate = false;
144
145 // C++ [class]p4:
146 // A POD-struct is an aggregate class...
147 data().PlainOldData = false;
148 }
149
Anders Carlsson6f6de732010-03-29 05:13:12 +0000150 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000151 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000152
153 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000154 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000155
John McCall86ff3082010-02-04 22:26:26 +0000156 data().Bases = new(C) CXXBaseSpecifier [NumBases];
157 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000158 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000159 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000160 // Keep track of inherited vbases for this base class.
161 const CXXBaseSpecifier *Base = Bases[i];
162 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000163 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000164 if (BaseType->isDependentType())
165 continue;
166 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000167 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000168
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000169 // A class with a non-empty base class is not empty.
170 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000171 if (!BaseClassDecl->isEmpty()) {
172 if (!data().Empty) {
173 // C++0x [class]p7:
174 // A standard-layout class is a class that:
175 // [...]
176 // -- either has no non-static data members in the most derived
177 // class and at most one base class with non-static data members,
178 // or has no base classes with non-static data members, and
179 // If this is the second non-empty base, then neither of these two
180 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000181 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000182 }
183
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000184 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000185 data().HasNoNonEmptyBases = false;
186 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000187
Douglas Gregor85606eb2010-09-28 20:50:54 +0000188 // C++ [class.virtual]p1:
189 // A class that declares or inherits a virtual function is called a
190 // polymorphic class.
191 if (BaseClassDecl->isPolymorphic())
192 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000193
Chandler Carrutha8225442011-04-30 09:17:45 +0000194 // C++0x [class]p7:
195 // A standard-layout class is a class that: [...]
196 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000197 if (!BaseClassDecl->isStandardLayout())
198 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000199
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000200 // Record if this base is the first non-literal field or base.
201 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
202 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000203
Anders Carlsson6f6de732010-03-29 05:13:12 +0000204 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000205 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000206 BaseClassDecl->vbases_begin(),
207 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000208 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000209 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000210 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000211 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000212
213 if (Base->isVirtual()) {
214 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000215 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000216 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000217
218 // C++0x [meta.unary.prop] is_empty:
219 // T is a class type, but not a union type, with ... no virtual base
220 // classes
221 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000222
223 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000224 // A default constructor is trivial [...] if:
225 // -- its class has [...] no virtual bases
226 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000227
228 // C++0x [class.copy]p13:
229 // A copy/move constructor for class X is trivial if it is neither
230 // user-provided nor deleted and if
231 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000232 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000233 data().HasTrivialMoveConstructor = false;
234
235 // C++0x [class.copy]p27:
236 // A copy/move assignment operator for class X is trivial if it is
237 // neither user-provided nor deleted and if
238 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000239 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000240 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000241
242 // C++0x [class]p7:
243 // A standard-layout class is a class that: [...]
244 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000245 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000246
247 // C++11 [dcl.constexpr]p4:
248 // In the definition of a constexpr constructor [...]
249 // -- the class shall not have any virtual base classes
250 data().DefaultedDefaultConstructorIsConstexpr = false;
251 data().DefaultedCopyConstructorIsConstexpr = false;
252 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000253 } else {
254 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000255 // A default constructor is trivial [...] if:
256 // -- all the direct base classes of its class have trivial default
257 // constructors.
258 if (!BaseClassDecl->hasTrivialDefaultConstructor())
259 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000260
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000261 // C++0x [class.copy]p13:
262 // A copy/move constructor for class X is trivial if [...]
263 // [...]
264 // -- the constructor selected to copy/move each direct base class
265 // subobject is trivial, and
266 // FIXME: C++0x: We need to only consider the selected constructor
267 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000268 if (!BaseClassDecl->hasTrivialCopyConstructor())
269 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000270 if (!BaseClassDecl->hasTrivialMoveConstructor())
271 data().HasTrivialMoveConstructor = false;
272
273 // C++0x [class.copy]p27:
274 // A copy/move assignment operator for class X is trivial if [...]
275 // [...]
276 // -- the assignment operator selected to copy/move each direct base
277 // class subobject is trivial, and
278 // FIXME: C++0x: We need to only consider the selected operator instead
279 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000280 if (!BaseClassDecl->hasTrivialCopyAssignment())
281 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000282 if (!BaseClassDecl->hasTrivialMoveAssignment())
283 data().HasTrivialMoveAssignment = false;
Richard Smith61802452011-12-22 02:22:31 +0000284
285 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000286 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000287 // requirements of a constexpr constructor, the implicitly-defined
288 // default constructor is constexpr.
289 if (!BaseClassDecl->hasConstexprDefaultConstructor())
290 data().DefaultedDefaultConstructorIsConstexpr = false;
291
292 // C++11 [class.copy]p13:
293 // If the implicitly-defined constructor would satisfy the requirements
294 // of a constexpr constructor, the implicitly-defined constructor is
295 // constexpr.
296 // C++11 [dcl.constexpr]p4:
297 // -- every constructor involved in initializing [...] base class
298 // sub-objects shall be a constexpr constructor
299 if (!BaseClassDecl->hasConstexprCopyConstructor())
300 data().DefaultedCopyConstructorIsConstexpr = false;
301 if (BaseClassDecl->hasDeclaredMoveConstructor() ||
302 BaseClassDecl->needsImplicitMoveConstructor())
303 // FIXME: If the implicit move constructor generated for the base class
304 // would be ill-formed, the implicit move constructor generated for the
305 // derived class calls the base class' copy constructor.
306 data().DefaultedMoveConstructorIsConstexpr &=
Richard Smithde8facc2012-01-11 18:26:05 +0000307 BaseClassDecl->hasConstexprMoveConstructor();
Richard Smith61802452011-12-22 02:22:31 +0000308 else if (!BaseClassDecl->hasConstexprCopyConstructor())
309 data().DefaultedMoveConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000310 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000311
312 // C++ [class.ctor]p3:
313 // A destructor is trivial if all the direct base classes of its class
314 // have trivial destructors.
315 if (!BaseClassDecl->hasTrivialDestructor())
316 data().HasTrivialDestructor = false;
Douglas Gregor2bb11012011-05-13 01:05:07 +0000317
John McCallf85e1932011-06-15 23:02:42 +0000318 // A class has an Objective-C object member if... or any of its bases
319 // has an Objective-C object member.
320 if (BaseClassDecl->hasObjectMember())
321 setHasObjectMember(true);
322
Douglas Gregor2bb11012011-05-13 01:05:07 +0000323 // Keep track of the presence of mutable fields.
324 if (BaseClassDecl->hasMutableFields())
325 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000326 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000327
328 if (VBases.empty())
329 return;
330
331 // Create base specifier for any direct or indirect virtual bases.
332 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
333 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000334 for (int I = 0, E = VBases.size(); I != E; ++I)
335 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000336}
337
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000338/// Callback function for CXXRecordDecl::forallBases that acknowledges
339/// that it saw a base class.
340static bool SawBase(const CXXRecordDecl *, void *) {
341 return true;
342}
343
344bool CXXRecordDecl::hasAnyDependentBases() const {
345 if (!isDependentContext())
346 return false;
347
348 return !forallBases(SawBase, 0);
349}
350
Sean Huntffe37fd2011-05-25 20:50:04 +0000351bool CXXRecordDecl::hasConstCopyConstructor() const {
352 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000353}
354
Chandler Carruthb7e95892011-04-23 10:47:28 +0000355bool CXXRecordDecl::isTriviallyCopyable() const {
356 // C++0x [class]p5:
357 // A trivially copyable class is a class that:
358 // -- has no non-trivial copy constructors,
359 if (!hasTrivialCopyConstructor()) return false;
360 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000361 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000362 // -- has no non-trivial copy assignment operators,
363 if (!hasTrivialCopyAssignment()) return false;
364 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000365 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000366 // -- has a trivial destructor.
367 if (!hasTrivialDestructor()) return false;
368
369 return true;
370}
371
Douglas Gregor0d405db2010-07-01 20:59:04 +0000372/// \brief Perform a simplistic form of overload resolution that only considers
373/// cv-qualifiers on a single parameter, and return the best overload candidate
374/// (if there is one).
375static CXXMethodDecl *
376GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000377 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000378 if (Cands.empty())
379 return 0;
380 if (Cands.size() == 1)
381 return Cands[0].first;
382
383 unsigned Best = 0, N = Cands.size();
384 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000385 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000386 Best = I;
387
388 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000389 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000390 return 0;
391
392 return Cands[Best].first;
393}
394
Sean Huntffe37fd2011-05-25 20:50:04 +0000395CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
396 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000397 QualType ClassType
398 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000399 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000400 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000401 Context.getCanonicalType(ClassType));
402 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000403 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000404 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000405 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000406 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000407 // C++ [class.copy]p2:
408 // A non-template constructor for class X is a copy constructor if [...]
409 if (isa<FunctionTemplateDecl>(*Con))
410 continue;
411
Douglas Gregor0d405db2010-07-01 20:59:04 +0000412 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
413 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000414 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
415 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000416 Found.push_back(std::make_pair(
417 const_cast<CXXConstructorDecl *>(Constructor),
418 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000419 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000420 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000421
422 return cast_or_null<CXXConstructorDecl>(
423 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000424}
425
Sean Huntffe37fd2011-05-25 20:50:04 +0000426CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
427 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
428 if (I->isMoveConstructor())
429 return *I;
430
431 return 0;
432}
433
Douglas Gregorb87786f2010-07-01 17:48:08 +0000434CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
435 ASTContext &Context = getASTContext();
436 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
437 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
438
Chris Lattner5f9e2722011-07-23 10:55:15 +0000439 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000440 DeclContext::lookup_const_iterator Op, OpEnd;
441 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
442 // C++ [class.copy]p9:
443 // A user-declared copy assignment operator is a non-static non-template
444 // member function of class X with exactly one parameter of type X, X&,
445 // const X&, volatile X& or const volatile X&.
446 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
447 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
448 continue;
449
450 const FunctionProtoType *FnType
451 = Method->getType()->getAs<FunctionProtoType>();
452 assert(FnType && "Overloaded operator has no prototype.");
453 // Don't assert on this; an invalid decl might have been left in the AST.
454 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
455 continue;
456
457 QualType ArgType = FnType->getArgType(0);
458 Qualifiers Quals;
459 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
460 ArgType = Ref->getPointeeType();
461 // If we have a const argument and we have a reference to a non-const,
462 // this function does not match.
463 if (ArgIsConst && !ArgType.isConstQualified())
464 continue;
465
466 Quals = ArgType.getQualifiers();
467 } else {
468 // By-value copy-assignment operators are treated like const X&
469 // copy-assignment operators.
470 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
471 }
472
473 if (!Context.hasSameUnqualifiedType(ArgType, Class))
474 continue;
475
476 // Save this copy-assignment operator. It might be "the one".
477 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
478 }
479
480 // Use a simplistic form of overload resolution to find the candidate.
481 return GetBestOverloadCandidateSimple(Found);
482}
483
Sean Huntffe37fd2011-05-25 20:50:04 +0000484CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
485 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
486 if (I->isMoveAssignmentOperator())
487 return *I;
488
489 return 0;
490}
491
Douglas Gregor21386642010-09-28 21:55:22 +0000492void CXXRecordDecl::markedVirtualFunctionPure() {
493 // C++ [class.abstract]p2:
494 // A class is abstract if it has at least one pure virtual function.
495 data().Abstract = true;
496}
497
498void CXXRecordDecl::addedMember(Decl *D) {
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +0000499 if (!D->isImplicit() &&
500 !isa<FieldDecl>(D) &&
501 !isa<IndirectFieldDecl>(D) &&
502 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class))
503 data().HasOnlyCMembers = false;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000504
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000505 // Ignore friends and invalid declarations.
506 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000507 return;
508
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000509 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
510 if (FunTmpl)
511 D = FunTmpl->getTemplatedDecl();
512
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000513 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
514 if (Method->isVirtual()) {
515 // C++ [dcl.init.aggr]p1:
516 // An aggregate is an array or a class with [...] no virtual functions.
517 data().Aggregate = false;
518
519 // C++ [class]p4:
520 // A POD-struct is an aggregate class...
521 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000522
523 // Virtual functions make the class non-empty.
524 // FIXME: Standard ref?
525 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000526
527 // C++ [class.virtual]p1:
528 // A class that declares or inherits a virtual function is called a
529 // polymorphic class.
530 data().Polymorphic = true;
531
Sean Hunt023df372011-05-09 18:22:59 +0000532 // C++0x [class.ctor]p5
533 // A default constructor is trivial [...] if:
534 // -- its class has no virtual functions [...]
535 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000536
537 // C++0x [class.copy]p13:
538 // A copy/move constructor for class X is trivial if [...]
539 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000540 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000541 data().HasTrivialMoveConstructor = false;
542
543 // C++0x [class.copy]p27:
544 // A copy/move assignment operator for class X is trivial if [...]
545 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000546 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000547 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000548
Chandler Carrutha8225442011-04-30 09:17:45 +0000549 // C++0x [class]p7:
550 // A standard-layout class is a class that: [...]
551 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000552 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000553 }
554 }
555
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000556 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000557 // Notify that an implicit member was added after the definition
558 // was completed.
559 if (!isBeingDefined())
560 if (ASTMutationListener *L = getASTMutationListener())
561 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000562
Sean Huntffe37fd2011-05-25 20:50:04 +0000563 // If this is a special member function, note that it was added and then
564 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000565 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith61802452011-12-22 02:22:31 +0000566 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000567 data().DeclaredDefaultConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000568 if (Constructor->isConstexpr()) {
569 data().HasConstexprDefaultConstructor = true;
570 data().HasConstexprNonCopyMoveConstructor = true;
571 }
572 } else if (Constructor->isCopyConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000573 data().DeclaredCopyConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000574 if (Constructor->isConstexpr())
575 data().HasConstexprCopyConstructor = true;
576 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000577 data().DeclaredMoveConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000578 if (Constructor->isConstexpr())
579 data().HasConstexprMoveConstructor = true;
580 } else
Sean Huntffe37fd2011-05-25 20:50:04 +0000581 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000582 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000583 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000584 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000585 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000586 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
587 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000588 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000589 else if (Method->isMoveAssignmentOperator())
590 data().DeclaredMoveAssignment = true;
591 else
592 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000593 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000594 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000595
Sean Huntffe37fd2011-05-25 20:50:04 +0000596NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000597 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000598 }
599
600 // Handle (user-declared) constructors.
601 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
602 // Note that we have a user-declared constructor.
603 data().UserDeclaredConstructor = true;
604
Richard Smith017ab772011-09-05 02:13:09 +0000605 // Technically, "user-provided" is only defined for special member
606 // functions, but the intent of the standard is clearly that it should apply
607 // to all functions.
608 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000609
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000610 if (Constructor->isDefaultConstructor()) {
611 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000612 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000613 // C++0x [class.ctor]p5:
614 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000615 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000616 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000617 }
Richard Smith61802452011-12-22 02:22:31 +0000618 if (Constructor->isConstexpr()) {
619 data().HasConstexprDefaultConstructor = true;
620 data().HasConstexprNonCopyMoveConstructor = true;
621 }
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000622 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000623
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000624 // Note when we have a user-declared copy or move constructor, which will
625 // suppress the implicit declaration of those constructors.
626 if (!FunTmpl) {
627 if (Constructor->isCopyConstructor()) {
628 data().UserDeclaredCopyConstructor = true;
629 data().DeclaredCopyConstructor = true;
630
631 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000632 // A copy/move constructor for class X is trivial if it is not
633 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000634 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000635 data().HasTrivialCopyConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000636
637 if (Constructor->isConstexpr())
638 data().HasConstexprCopyConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000639 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000640 data().UserDeclaredMoveConstructor = true;
641 data().DeclaredMoveConstructor = true;
642
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000643 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000644 // A copy/move constructor for class X is trivial if it is not
645 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000646 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000647 data().HasTrivialMoveConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000648
649 if (Constructor->isConstexpr())
650 data().HasConstexprMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000651 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000652 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000653 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
654 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000655 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000656 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000657 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000658
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000659 // C++ [dcl.init.aggr]p1:
660 // An aggregate is an array or a class with no user-declared
661 // constructors [...].
662 // C++0x [dcl.init.aggr]p1:
663 // An aggregate is an array or a class with no user-provided
664 // constructors [...].
665 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
666 data().Aggregate = false;
667
668 // C++ [class]p4:
669 // A POD-struct is an aggregate class [...]
670 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
671 // type is technically an aggregate in C++0x since it wouldn't be in 03.
672 data().PlainOldData = false;
673
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000674 return;
675 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000676
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000677 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000678 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000679 data().DeclaredDestructor = true;
680 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000681
682 // C++ [class]p4:
683 // A POD-struct is an aggregate class that has [...] no user-defined
684 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000685 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000686 data().PlainOldData = false;
687
Douglas Gregor45fa5602011-11-07 20:56:01 +0000688 // C++11 [class.dtor]p5:
689 // A destructor is trivial if it is not user-provided and if
690 // -- the destructor is not virtual.
Richard Smith61802452011-12-22 02:22:31 +0000691 if (DD->isUserProvided() || DD->isVirtual()) {
Sean Huntcf34e752011-05-16 22:41:40 +0000692 data().HasTrivialDestructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000693 // C++11 [dcl.constexpr]p1:
694 // The constexpr specifier shall be applied only to [...] the
695 // declaration of a static data member of a literal type.
696 // C++11 [basic.types]p10:
697 // A type is a literal type if it is [...] a class type that [...] has
698 // a trivial destructor.
699 data().DefaultedDefaultConstructorIsConstexpr = false;
700 data().DefaultedCopyConstructorIsConstexpr = false;
701 data().DefaultedMoveConstructorIsConstexpr = false;
702 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000703
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000704 return;
705 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000706
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000707 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000708 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000709 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000710 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000711 // A POD-struct is an aggregate class that [...] has no user-defined
712 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000713 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000714 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000715
Sean Huntffe37fd2011-05-25 20:50:04 +0000716 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000717
Sean Huntffe37fd2011-05-25 20:50:04 +0000718 // Suppress the implicit declaration of a copy constructor.
719 data().UserDeclaredCopyAssignment = true;
720 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000721
Sean Huntffe37fd2011-05-25 20:50:04 +0000722 // C++0x [class.copy]p27:
723 // A copy/move assignment operator for class X is trivial if it is
724 // neither user-provided nor deleted [...]
725 if (Method->isUserProvided())
726 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000727
Sean Huntffe37fd2011-05-25 20:50:04 +0000728 return;
729 }
730
731 if (Method->isMoveAssignmentOperator()) {
732 // This is an extension in C++03 mode, but we'll keep consistency by
733 // taking a move assignment operator to induce non-POD-ness
734 data().PlainOldData = false;
735
736 // This is a move assignment operator.
737 data().UserDeclaredMoveAssignment = true;
738 data().DeclaredMoveAssignment = true;
739
740 // C++0x [class.copy]p27:
741 // A copy/move assignment operator for class X is trivial if it is
742 // neither user-provided nor deleted [...]
743 if (Method->isUserProvided())
744 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000745 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000746
Douglas Gregore80622f2010-09-29 04:25:11 +0000747 // Keep the list of conversion functions up-to-date.
748 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
749 // We don't record specializations.
750 if (Conversion->getPrimaryTemplate())
751 return;
752
753 // FIXME: We intentionally don't use the decl's access here because it
754 // hasn't been set yet. That's really just a misdesign in Sema.
755
756 if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000757 if (FunTmpl->getPreviousDecl())
758 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000759 FunTmpl);
760 else
761 data().Conversions.addDecl(FunTmpl);
762 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000763 if (Conversion->getPreviousDecl())
764 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000765 Conversion);
766 else
767 data().Conversions.addDecl(Conversion);
768 }
769 }
770
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000771 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000772 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000773
774 // Handle non-static data members.
775 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000776 // C++ [class.bit]p2:
777 // A declaration for a bit-field that omits the identifier declares an
778 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
779 // initialized.
780 if (Field->isUnnamedBitfield())
781 return;
782
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000783 // C++ [dcl.init.aggr]p1:
784 // An aggregate is an array or a class (clause 9) with [...] no
785 // private or protected non-static data members (clause 11).
786 //
787 // A POD must be an aggregate.
788 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
789 data().Aggregate = false;
790 data().PlainOldData = false;
791 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000792
793 // C++0x [class]p7:
794 // A standard-layout class is a class that:
795 // [...]
796 // -- has the same access control for all non-static data members,
797 switch (D->getAccess()) {
798 case AS_private: data().HasPrivateFields = true; break;
799 case AS_protected: data().HasProtectedFields = true; break;
800 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000801 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000802 };
803 if ((data().HasPrivateFields + data().HasProtectedFields +
804 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000805 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000806
Douglas Gregor2bb11012011-05-13 01:05:07 +0000807 // Keep track of the presence of mutable fields.
808 if (Field->isMutable())
809 data().HasMutableFields = true;
810
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000811 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000812 // A POD struct is a class that is both a trivial class and a
813 // standard-layout class, and has no non-static data members of type
814 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000815 //
816 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
817 // that does not explicitly have no lifetime makes the class a non-POD.
818 // However, we delay setting PlainOldData to false in this case so that
819 // Sema has a chance to diagnostic causes where the same class will be
820 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
821 // In this case, the class will become a non-POD class when we complete
822 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000823 ASTContext &Context = getASTContext();
824 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000825 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
826 if (!Context.getLangOptions().ObjCAutoRefCount ||
827 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
828 setHasObjectMember(true);
829 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000830 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000831
Chandler Carrutha8225442011-04-30 09:17:45 +0000832 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000833 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000834
Chandler Carrutha8225442011-04-30 09:17:45 +0000835 // C++0x [class]p7:
836 // A standard-layout class is a class that:
837 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000838 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000839 }
840
Richard Smith86c3ae42012-02-13 03:54:03 +0000841 // Record if this field is the first non-literal or volatile field or base.
842 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000843 data().HasNonLiteralTypeFieldsOrBases = true;
844
Richard Smith7a614d82011-06-11 17:19:42 +0000845 if (Field->hasInClassInitializer()) {
846 // C++0x [class]p5:
847 // A default constructor is trivial if [...] no non-static data member
848 // of its class has a brace-or-equal-initializer.
849 data().HasTrivialDefaultConstructor = false;
850
851 // C++0x [dcl.init.aggr]p1:
852 // An aggregate is a [...] class with [...] no
853 // brace-or-equal-initializers for non-static data members.
854 data().Aggregate = false;
855
856 // C++0x [class]p10:
857 // A POD struct is [...] a trivial class.
858 data().PlainOldData = false;
859 }
860
Douglas Gregor85606eb2010-09-28 20:50:54 +0000861 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
862 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
863 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000864 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000865 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000866 // -- for all the non-static data members of its class that are of
867 // class type (or array thereof), each such class has a trivial
868 // default constructor.
869 if (!FieldRec->hasTrivialDefaultConstructor())
870 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000871
872 // C++0x [class.copy]p13:
873 // A copy/move constructor for class X is trivial if [...]
874 // [...]
875 // -- for each non-static data member of X that is of class type (or
876 // an array thereof), the constructor selected to copy/move that
877 // member is trivial;
878 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000879 if (!FieldRec->hasTrivialCopyConstructor())
880 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000881 if (!FieldRec->hasTrivialMoveConstructor())
882 data().HasTrivialMoveConstructor = false;
883
884 // C++0x [class.copy]p27:
885 // A copy/move assignment operator for class X is trivial if [...]
886 // [...]
887 // -- for each non-static data member of X that is of class type (or
888 // an array thereof), the assignment operator selected to
889 // copy/move that member is trivial;
890 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000891 if (!FieldRec->hasTrivialCopyAssignment())
892 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000893 if (!FieldRec->hasTrivialMoveAssignment())
894 data().HasTrivialMoveAssignment = false;
895
Douglas Gregor85606eb2010-09-28 20:50:54 +0000896 if (!FieldRec->hasTrivialDestructor())
897 data().HasTrivialDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000898 if (FieldRec->hasObjectMember())
899 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000900
901 // C++0x [class]p7:
902 // A standard-layout class is a class that:
903 // -- has no non-static data members of type non-standard-layout
904 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000905 if (!FieldRec->isStandardLayout())
906 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000907
908 // C++0x [class]p7:
909 // A standard-layout class is a class that:
910 // [...]
911 // -- has no base classes of the same type as the first non-static
912 // data member.
913 // We don't want to expend bits in the state of the record decl
914 // tracking whether this is the first non-static data member so we
915 // cheat a bit and use some of the existing state: the empty bit.
916 // Virtual bases and virtual methods make a class non-empty, but they
917 // also make it non-standard-layout so we needn't check here.
918 // A non-empty base class may leave the class standard-layout, but not
919 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000920 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000921 // data member must come through here with Empty still true, and Empty
922 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000923 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000924 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
925 BE = bases_end();
926 BI != BE; ++BI) {
927 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000928 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000929 break;
930 }
931 }
932 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000933
934 // Keep track of the presence of mutable fields.
935 if (FieldRec->hasMutableFields())
936 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000937
938 // C++11 [class.copy]p13:
939 // If the implicitly-defined constructor would satisfy the
940 // requirements of a constexpr constructor, the implicitly-defined
941 // constructor is constexpr.
942 // C++11 [dcl.constexpr]p4:
943 // -- every constructor involved in initializing non-static data
944 // members [...] shall be a constexpr constructor
945 if (!Field->hasInClassInitializer() &&
946 !FieldRec->hasConstexprDefaultConstructor())
947 // The standard requires any in-class initializer to be a constant
948 // expression. We consider this to be a defect.
949 data().DefaultedDefaultConstructorIsConstexpr = false;
950
951 if (!FieldRec->hasConstexprCopyConstructor())
952 data().DefaultedCopyConstructorIsConstexpr = false;
953
954 if (FieldRec->hasDeclaredMoveConstructor() ||
955 FieldRec->needsImplicitMoveConstructor())
956 // FIXME: If the implicit move constructor generated for the member's
957 // class would be ill-formed, the implicit move constructor generated
958 // for this class calls the member's copy constructor.
959 data().DefaultedMoveConstructorIsConstexpr &=
960 FieldRec->hasConstexprMoveConstructor();
961 else if (!FieldRec->hasConstexprCopyConstructor())
962 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000963 }
Richard Smith61802452011-12-22 02:22:31 +0000964 } else {
965 // Base element type of field is a non-class type.
966 if (!T->isLiteralType()) {
967 data().DefaultedDefaultConstructorIsConstexpr = false;
968 data().DefaultedCopyConstructorIsConstexpr = false;
969 data().DefaultedMoveConstructorIsConstexpr = false;
970 } else if (!Field->hasInClassInitializer())
971 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000972 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000973
974 // C++0x [class]p7:
975 // A standard-layout class is a class that:
976 // [...]
977 // -- either has no non-static data members in the most derived
978 // class and at most one base class with non-static data members,
979 // or has no base classes with non-static data members, and
980 // At this point we know that we have a non-static data member, so the last
981 // clause holds.
982 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000983 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000984
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000985 // If this is not a zero-length bit-field, then the class is not empty.
986 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000987 if (!Field->isBitField() ||
988 (!Field->getBitWidth()->isTypeDependent() &&
989 !Field->getBitWidth()->isValueDependent() &&
990 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000991 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000992 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000993 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000994
995 // Handle using declarations of conversion functions.
996 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
997 if (Shadow->getDeclName().getNameKind()
998 == DeclarationName::CXXConversionFunctionName)
999 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001000}
1001
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +00001002bool CXXRecordDecl::isCLike() const {
1003 if (getTagKind() == TTK_Class || !TemplateOrInstantiation.isNull())
1004 return false;
1005 if (!hasDefinition())
1006 return true;
1007
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +00001008 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +00001009}
1010
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001011void CXXRecordDecl::getCaptureFields(
1012 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +00001013 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001014 Captures.clear();
1015 ThisCapture = 0;
1016
Douglas Gregorda8962a2012-02-13 15:44:47 +00001017 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001018 RecordDecl::field_iterator Field = field_begin();
Douglas Gregorda8962a2012-02-13 15:44:47 +00001019 for (LambdaExpr::Capture *C = Lambda.getCaptures(),
1020 *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001021 C != CEnd; ++C, ++Field) {
1022 if (C->capturesThis()) {
1023 ThisCapture = *Field;
1024 continue;
1025 }
1026
1027 Captures[C->getCapturedVar()] = *Field;
1028 }
1029}
1030
1031
John McCallb05b5f32010-03-15 09:07:48 +00001032static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1033 QualType T;
John McCall32daa422010-03-31 01:36:47 +00001034 if (isa<UsingShadowDecl>(Conv))
1035 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +00001036 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
1037 T = ConvTemp->getTemplatedDecl()->getResultType();
1038 else
1039 T = cast<CXXConversionDecl>(Conv)->getConversionType();
1040 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +00001041}
1042
John McCallb05b5f32010-03-15 09:07:48 +00001043/// Collect the visible conversions of a base class.
1044///
1045/// \param Base a base class of the class we're considering
1046/// \param InVirtual whether this base class is a virtual base (or a base
1047/// of a virtual base)
1048/// \param Access the access along the inheritance path to this base
1049/// \param ParentHiddenTypes the conversions provided by the inheritors
1050/// of this base
1051/// \param Output the set to which to add conversions from non-virtual bases
1052/// \param VOutput the set to which to add conversions from virtual bases
1053/// \param HiddenVBaseCs the set of conversions which were hidden in a
1054/// virtual base along some inheritance path
1055static void CollectVisibleConversions(ASTContext &Context,
1056 CXXRecordDecl *Record,
1057 bool InVirtual,
1058 AccessSpecifier Access,
1059 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
1060 UnresolvedSetImpl &Output,
1061 UnresolvedSetImpl &VOutput,
1062 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1063 // The set of types which have conversions in this class or its
1064 // subclasses. As an optimization, we don't copy the derived set
1065 // unless it might change.
1066 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1067 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1068
1069 // Collect the direct conversions and figure out which conversions
1070 // will be hidden in the subclasses.
1071 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1072 if (!Cs.empty()) {
1073 HiddenTypesBuffer = ParentHiddenTypes;
1074 HiddenTypes = &HiddenTypesBuffer;
1075
1076 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
1077 bool Hidden =
1078 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
1079
1080 // If this conversion is hidden and we're in a virtual base,
1081 // remember that it's hidden along some inheritance path.
1082 if (Hidden && InVirtual)
1083 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1084
1085 // If this conversion isn't hidden, add it to the appropriate output.
1086 else if (!Hidden) {
1087 AccessSpecifier IAccess
1088 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1089
1090 if (InVirtual)
1091 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001092 else
John McCallb05b5f32010-03-15 09:07:48 +00001093 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001094 }
1095 }
1096 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001097
John McCallb05b5f32010-03-15 09:07:48 +00001098 // Collect information recursively from any base classes.
1099 for (CXXRecordDecl::base_class_iterator
1100 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1101 const RecordType *RT = I->getType()->getAs<RecordType>();
1102 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001103
John McCallb05b5f32010-03-15 09:07:48 +00001104 AccessSpecifier BaseAccess
1105 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1106 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001107
John McCallb05b5f32010-03-15 09:07:48 +00001108 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1109 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1110 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001111 }
John McCallb05b5f32010-03-15 09:07:48 +00001112}
Sebastian Redl9994a342009-10-25 17:03:50 +00001113
John McCallb05b5f32010-03-15 09:07:48 +00001114/// Collect the visible conversions of a class.
1115///
1116/// This would be extremely straightforward if it weren't for virtual
1117/// bases. It might be worth special-casing that, really.
1118static void CollectVisibleConversions(ASTContext &Context,
1119 CXXRecordDecl *Record,
1120 UnresolvedSetImpl &Output) {
1121 // The collection of all conversions in virtual bases that we've
1122 // found. These will be added to the output as long as they don't
1123 // appear in the hidden-conversions set.
1124 UnresolvedSet<8> VBaseCs;
1125
1126 // The set of conversions in virtual bases that we've determined to
1127 // be hidden.
1128 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1129
1130 // The set of types hidden by classes derived from this one.
1131 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1132
1133 // Go ahead and collect the direct conversions and add them to the
1134 // hidden-types set.
1135 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1136 Output.append(Cs.begin(), Cs.end());
1137 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1138 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1139
1140 // Recursively collect conversions from base classes.
1141 for (CXXRecordDecl::base_class_iterator
1142 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1143 const RecordType *RT = I->getType()->getAs<RecordType>();
1144 if (!RT) continue;
1145
1146 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1147 I->isVirtual(), I->getAccessSpecifier(),
1148 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1149 }
1150
1151 // Add any unhidden conversions provided by virtual bases.
1152 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1153 I != E; ++I) {
1154 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1155 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001156 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001157}
1158
1159/// getVisibleConversionFunctions - get all conversion functions visible
1160/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +00001161const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001162 // If root class, all conversions are visible.
1163 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +00001164 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +00001165 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +00001166 if (data().ComputedVisibleConversions)
1167 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +00001168 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001169 data().ComputedVisibleConversions = true;
1170 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001171}
1172
John McCall32daa422010-03-31 01:36:47 +00001173void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1174 // This operation is O(N) but extremely rare. Sema only uses it to
1175 // remove UsingShadowDecls in a class that were followed by a direct
1176 // declaration, e.g.:
1177 // class A : B {
1178 // using B::operator int;
1179 // operator int();
1180 // };
1181 // This is uncommon by itself and even more uncommon in conjunction
1182 // with sufficiently large numbers of directly-declared conversions
1183 // that asymptotic behavior matters.
1184
1185 UnresolvedSetImpl &Convs = *getConversionFunctions();
1186 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1187 if (Convs[I].getDecl() == ConvDecl) {
1188 Convs.erase(I);
1189 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1190 && "conversion was found multiple times in unresolved set");
1191 return;
1192 }
1193 }
1194
1195 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001196}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001197
Douglas Gregorf6b11852009-10-08 15:14:33 +00001198CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001199 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001200 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1201
1202 return 0;
1203}
1204
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001205MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1206 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1207}
1208
Douglas Gregorf6b11852009-10-08 15:14:33 +00001209void
1210CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1211 TemplateSpecializationKind TSK) {
1212 assert(TemplateOrInstantiation.isNull() &&
1213 "Previous template or instantiation?");
1214 assert(!isa<ClassTemplateSpecializationDecl>(this));
1215 TemplateOrInstantiation
1216 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1217}
1218
Anders Carlssonb13e3572009-12-07 06:33:48 +00001219TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1220 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001221 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1222 return Spec->getSpecializationKind();
1223
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001224 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001225 return MSInfo->getTemplateSpecializationKind();
1226
1227 return TSK_Undeclared;
1228}
1229
1230void
1231CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1232 if (ClassTemplateSpecializationDecl *Spec
1233 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1234 Spec->setSpecializationKind(TSK);
1235 return;
1236 }
1237
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001238 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001239 MSInfo->setTemplateSpecializationKind(TSK);
1240 return;
1241 }
1242
David Blaikieb219cfc2011-09-23 05:06:16 +00001243 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001244}
1245
Douglas Gregor1d110e02010-07-01 14:13:13 +00001246CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1247 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001248 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001249
1250 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001251 = Context.DeclarationNames.getCXXDestructorName(
1252 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001253
John McCallc0bf4622010-02-23 00:48:20 +00001254 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001255 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001256 if (I == E)
1257 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001259 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001260 return Dtor;
1261}
1262
Douglas Gregorda2142f2011-02-19 18:51:44 +00001263void CXXRecordDecl::completeDefinition() {
1264 completeDefinition(0);
1265}
1266
1267void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1268 RecordDecl::completeDefinition();
1269
John McCallf85e1932011-06-15 23:02:42 +00001270 if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) {
1271 // Objective-C Automatic Reference Counting:
1272 // If a class has a non-static data member of Objective-C pointer
1273 // type (or array thereof), it is a non-POD type and its
1274 // default constructor (if any), copy constructor, copy assignment
1275 // operator, and destructor are non-trivial.
1276 struct DefinitionData &Data = data();
1277 Data.PlainOldData = false;
1278 Data.HasTrivialDefaultConstructor = false;
1279 Data.HasTrivialCopyConstructor = false;
1280 Data.HasTrivialCopyAssignment = false;
1281 Data.HasTrivialDestructor = false;
1282 }
1283
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001284 // If the class may be abstract (but hasn't been marked as such), check for
1285 // any pure final overriders.
1286 if (mayBeAbstract()) {
1287 CXXFinalOverriderMap MyFinalOverriders;
1288 if (!FinalOverriders) {
1289 getFinalOverriders(MyFinalOverriders);
1290 FinalOverriders = &MyFinalOverriders;
1291 }
1292
1293 bool Done = false;
1294 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1295 MEnd = FinalOverriders->end();
1296 M != MEnd && !Done; ++M) {
1297 for (OverridingMethods::iterator SO = M->second.begin(),
1298 SOEnd = M->second.end();
1299 SO != SOEnd && !Done; ++SO) {
1300 assert(SO->second.size() > 0 &&
1301 "All virtual functions have overridding virtual functions");
1302
1303 // C++ [class.abstract]p4:
1304 // A class is abstract if it contains or inherits at least one
1305 // pure virtual function for which the final overrider is pure
1306 // virtual.
1307 if (SO->second.front().Method->isPure()) {
1308 data().Abstract = true;
1309 Done = true;
1310 break;
1311 }
1312 }
1313 }
1314 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001315
1316 // Set access bits correctly on the directly-declared conversions.
1317 for (UnresolvedSetIterator I = data().Conversions.begin(),
1318 E = data().Conversions.end();
1319 I != E; ++I)
1320 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001321}
1322
1323bool CXXRecordDecl::mayBeAbstract() const {
1324 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1325 isDependentContext())
1326 return false;
1327
1328 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1329 BEnd = bases_end();
1330 B != BEnd; ++B) {
1331 CXXRecordDecl *BaseDecl
1332 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1333 if (BaseDecl->isAbstract())
1334 return true;
1335 }
1336
1337 return false;
1338}
1339
David Blaikie99ba9e32011-12-20 02:48:34 +00001340void CXXMethodDecl::anchor() { }
1341
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001342CXXMethodDecl *
1343CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001344 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001345 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001346 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001347 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001348 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001349 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001350 isStatic, SCAsWritten, isInline, isConstexpr,
1351 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001352}
1353
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001354CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1355 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1356 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1357 DeclarationNameInfo(), QualType(),
1358 0, false, SC_None, false, false,
1359 SourceLocation());
1360}
1361
Douglas Gregor90916562009-09-29 18:16:17 +00001362bool CXXMethodDecl::isUsualDeallocationFunction() const {
1363 if (getOverloadedOperator() != OO_Delete &&
1364 getOverloadedOperator() != OO_Array_Delete)
1365 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001366
1367 // C++ [basic.stc.dynamic.deallocation]p2:
1368 // A template instance is never a usual deallocation function,
1369 // regardless of its signature.
1370 if (getPrimaryTemplate())
1371 return false;
1372
Douglas Gregor90916562009-09-29 18:16:17 +00001373 // C++ [basic.stc.dynamic.deallocation]p2:
1374 // If a class T has a member deallocation function named operator delete
1375 // with exactly one parameter, then that function is a usual (non-placement)
1376 // deallocation function. [...]
1377 if (getNumParams() == 1)
1378 return true;
1379
1380 // C++ [basic.stc.dynamic.deallocation]p2:
1381 // [...] If class T does not declare such an operator delete but does
1382 // declare a member deallocation function named operator delete with
1383 // exactly two parameters, the second of which has type std::size_t (18.1),
1384 // then this function is a usual deallocation function.
1385 ASTContext &Context = getASTContext();
1386 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001387 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1388 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001389 return false;
1390
1391 // This function is a usual deallocation function if there are no
1392 // single-parameter deallocation functions of the same kind.
1393 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1394 R.first != R.second; ++R.first) {
1395 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1396 if (FD->getNumParams() == 1)
1397 return false;
1398 }
1399
1400 return true;
1401}
1402
Douglas Gregor06a9f362010-05-01 20:49:11 +00001403bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001404 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001405 // A user-declared copy assignment operator X::operator= is a non-static
1406 // non-template member function of class X with exactly one parameter of
1407 // type X, X&, const X&, volatile X& or const volatile X&.
1408 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1409 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001410 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001411 return false;
1412
1413 QualType ParamType = getParamDecl(0)->getType();
1414 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1415 ParamType = Ref->getPointeeType();
1416
1417 ASTContext &Context = getASTContext();
1418 QualType ClassType
1419 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1420 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1421}
1422
Sean Huntffe37fd2011-05-25 20:50:04 +00001423bool CXXMethodDecl::isMoveAssignmentOperator() const {
1424 // C++0x [class.copy]p19:
1425 // A user-declared move assignment operator X::operator= is a non-static
1426 // non-template member function of class X with exactly one parameter of type
1427 // X&&, const X&&, volatile X&&, or const volatile X&&.
1428 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1429 getPrimaryTemplate() || getDescribedFunctionTemplate())
1430 return false;
1431
1432 QualType ParamType = getParamDecl(0)->getType();
1433 if (!isa<RValueReferenceType>(ParamType))
1434 return false;
1435 ParamType = ParamType->getPointeeType();
1436
1437 ASTContext &Context = getASTContext();
1438 QualType ClassType
1439 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1440 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1441}
1442
Anders Carlsson05eb2442009-05-16 23:58:37 +00001443void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001444 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001445 assert(!MD->getParent()->isDependentContext() &&
1446 "Can't add an overridden method to a class template!");
1447
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001448 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001449}
1450
1451CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001452 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001453}
1454
1455CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001456 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001457}
1458
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001459unsigned CXXMethodDecl::size_overridden_methods() const {
1460 return getASTContext().overridden_methods_size(this);
1461}
1462
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001463QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001464 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1465 // If the member function is declared const, the type of this is const X*,
1466 // if the member function is declared volatile, the type of this is
1467 // volatile X*, and if the member function is declared const volatile,
1468 // the type of this is const volatile X*.
1469
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001470 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001471
John McCall3cb0ebd2010-03-10 03:28:59 +00001472 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001473 ClassTy = C.getQualifiedType(ClassTy,
1474 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001475 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001476}
1477
Eli Friedmand7d7f672009-12-06 20:50:05 +00001478bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001479 // If this function is a template instantiation, look at the template from
1480 // which it was instantiated.
1481 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1482 if (!CheckFn)
1483 CheckFn = this;
1484
Eli Friedmand7d7f672009-12-06 20:50:05 +00001485 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001486 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001487}
1488
Sean Huntcbb67482011-01-08 20:30:50 +00001489CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1490 TypeSourceInfo *TInfo, bool IsVirtual,
1491 SourceLocation L, Expr *Init,
1492 SourceLocation R,
1493 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001494 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001495 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1496 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001497{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001498}
1499
Sean Huntcbb67482011-01-08 20:30:50 +00001500CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1501 FieldDecl *Member,
1502 SourceLocation MemberLoc,
1503 SourceLocation L, Expr *Init,
1504 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001505 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001506 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001507 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1508{
1509}
1510
Sean Huntcbb67482011-01-08 20:30:50 +00001511CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1512 IndirectFieldDecl *Member,
1513 SourceLocation MemberLoc,
1514 SourceLocation L, Expr *Init,
1515 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001516 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001517 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001518 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001519{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001520}
1521
Sean Huntcbb67482011-01-08 20:30:50 +00001522CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001523 TypeSourceInfo *TInfo,
1524 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001525 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001526 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1527 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001528 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1529{
1530}
1531
1532CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001533 FieldDecl *Member,
1534 SourceLocation MemberLoc,
1535 SourceLocation L, Expr *Init,
1536 SourceLocation R,
1537 VarDecl **Indices,
1538 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001539 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001540 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001541 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001542{
1543 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1544 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1545}
1546
Sean Huntcbb67482011-01-08 20:30:50 +00001547CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1548 FieldDecl *Member,
1549 SourceLocation MemberLoc,
1550 SourceLocation L, Expr *Init,
1551 SourceLocation R,
1552 VarDecl **Indices,
1553 unsigned NumIndices) {
1554 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001555 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001556 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001557 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1558 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001559}
1560
Sean Huntcbb67482011-01-08 20:30:50 +00001561TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001562 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001563 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001564 else
1565 return TypeLoc();
1566}
1567
Sean Huntcbb67482011-01-08 20:30:50 +00001568const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001569 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001570 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001571 else
1572 return 0;
1573}
1574
Sean Huntcbb67482011-01-08 20:30:50 +00001575SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001576 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001577 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001578
1579 if (isInClassMemberInitializer())
1580 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001581
Douglas Gregor76852c22011-11-01 01:16:03 +00001582 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1583 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1584
1585 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001586}
1587
Sean Huntcbb67482011-01-08 20:30:50 +00001588SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001589 if (isInClassMemberInitializer()) {
1590 FieldDecl *D = getAnyMember();
1591 if (Expr *I = D->getInClassInitializer())
1592 return I->getSourceRange();
1593 return SourceRange();
1594 }
1595
Douglas Gregor802ab452009-12-02 22:36:29 +00001596 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001597}
1598
David Blaikie99ba9e32011-12-20 02:48:34 +00001599void CXXConstructorDecl::anchor() { }
1600
Douglas Gregorb48fe382008-10-31 09:07:45 +00001601CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001602CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1603 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1604 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1605 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001606}
1607
1608CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001609CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001610 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001611 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001612 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001613 bool isExplicit, bool isInline,
1614 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001615 assert(NameInfo.getName().getNameKind()
1616 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001617 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001618 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001619 isExplicit, isInline, isImplicitlyDeclared,
1620 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001621}
1622
Douglas Gregor76852c22011-11-01 01:16:03 +00001623CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1624 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1625 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1626 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1627 return Construct->getConstructor();
1628
1629 return 0;
1630}
1631
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001632bool CXXConstructorDecl::isDefaultConstructor() const {
1633 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001634 // A default constructor for a class X is a constructor of class
1635 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001636 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001637 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001638}
1639
Mike Stump1eb44332009-09-09 15:08:12 +00001640bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001641CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001642 return isCopyOrMoveConstructor(TypeQuals) &&
1643 getParamDecl(0)->getType()->isLValueReferenceType();
1644}
1645
1646bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1647 return isCopyOrMoveConstructor(TypeQuals) &&
1648 getParamDecl(0)->getType()->isRValueReferenceType();
1649}
1650
1651/// \brief Determine whether this is a copy or move constructor.
1652bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001653 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001654 // A non-template constructor for class X is a copy constructor
1655 // if its first parameter is of type X&, const X&, volatile X& or
1656 // const volatile X&, and either there are no other parameters
1657 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001658 // C++0x [class.copy]p3:
1659 // A non-template constructor for class X is a move constructor if its
1660 // first parameter is of type X&&, const X&&, volatile X&&, or
1661 // const volatile X&&, and either there are no other parameters or else
1662 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001663 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001664 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001665 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001666 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001667 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001668
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001669 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001670
1671 // Do we have a reference type?
1672 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001673 if (!ParamRefType)
1674 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001675
Douglas Gregorfd476482009-11-13 23:59:09 +00001676 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001677 ASTContext &Context = getASTContext();
1678
Douglas Gregorfd476482009-11-13 23:59:09 +00001679 CanQualType PointeeType
1680 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001681 CanQualType ClassTy
1682 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001683 if (PointeeType.getUnqualifiedType() != ClassTy)
1684 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001685
John McCall0953e762009-09-24 19:53:00 +00001686 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001687
1688 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001689 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001690 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001691}
1692
Anders Carlssonfaccd722009-08-28 16:57:08 +00001693bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001694 // C++ [class.conv.ctor]p1:
1695 // A constructor declared without the function-specifier explicit
1696 // that can be called with a single parameter specifies a
1697 // conversion from the type of its first parameter to the type of
1698 // its class. Such a constructor is called a converting
1699 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001700 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001701 return false;
1702
Mike Stump1eb44332009-09-09 15:08:12 +00001703 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001704 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001705 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001706 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001707}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001708
Douglas Gregor6493cc52010-11-08 17:16:59 +00001709bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001710 if ((getNumParams() < 1) ||
1711 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1712 (getPrimaryTemplate() == 0) ||
1713 (getDescribedFunctionTemplate() != 0))
1714 return false;
1715
1716 const ParmVarDecl *Param = getParamDecl(0);
1717
1718 ASTContext &Context = getASTContext();
1719 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1720
Douglas Gregor66724ea2009-11-14 01:20:54 +00001721 // Is it the same as our our class type?
1722 CanQualType ClassTy
1723 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1724 if (ParamType.getUnqualifiedType() != ClassTy)
1725 return false;
1726
1727 return true;
1728}
1729
Sebastian Redlf677ea32011-02-05 19:23:19 +00001730const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1731 // Hack: we store the inherited constructor in the overridden method table
1732 method_iterator It = begin_overridden_methods();
1733 if (It == end_overridden_methods())
1734 return 0;
1735
1736 return cast<CXXConstructorDecl>(*It);
1737}
1738
1739void
1740CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1741 // Hack: we store the inherited constructor in the overridden method table
1742 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1743 addOverriddenMethod(BaseCtor);
1744}
1745
David Blaikie99ba9e32011-12-20 02:48:34 +00001746void CXXDestructorDecl::anchor() { }
1747
Douglas Gregor42a552f2008-11-05 20:51:48 +00001748CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001749CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1750 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1751 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001752 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001753}
1754
1755CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001756CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001757 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001758 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001759 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001760 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001761 assert(NameInfo.getName().getNameKind()
1762 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001763 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001764 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001765 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001766}
1767
David Blaikie99ba9e32011-12-20 02:48:34 +00001768void CXXConversionDecl::anchor() { }
1769
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001770CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001771CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1772 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1773 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1774 QualType(), 0, false, false, false,
1775 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001776}
1777
1778CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001779CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001780 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001781 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001782 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001783 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001784 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001785 assert(NameInfo.getName().getNameKind()
1786 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001787 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001788 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001789 isInline, isExplicit, isConstexpr,
1790 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001791}
1792
David Blaikie99ba9e32011-12-20 02:48:34 +00001793void LinkageSpecDecl::anchor() { }
1794
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001795LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001796 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001797 SourceLocation ExternLoc,
1798 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001799 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001800 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001801 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001802}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001803
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001804LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1805 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1806 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1807 lang_c, SourceLocation());
1808}
1809
David Blaikie99ba9e32011-12-20 02:48:34 +00001810void UsingDirectiveDecl::anchor() { }
1811
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001812UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1813 SourceLocation L,
1814 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001815 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001816 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001817 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001818 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001819 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1820 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001821 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1822 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001823}
1824
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001825UsingDirectiveDecl *
1826UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1827 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1828 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1829 NestedNameSpecifierLoc(),
1830 SourceLocation(), 0, 0);
1831}
1832
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001833NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1834 if (NamespaceAliasDecl *NA =
1835 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1836 return NA->getNamespace();
1837 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1838}
1839
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001840void NamespaceDecl::anchor() { }
1841
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001842NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1843 SourceLocation StartLoc,
1844 SourceLocation IdLoc, IdentifierInfo *Id,
1845 NamespaceDecl *PrevDecl)
1846 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1847 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1848{
1849 setPreviousDeclaration(PrevDecl);
1850
1851 if (PrevDecl)
1852 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1853}
1854
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001855NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001856 bool Inline, SourceLocation StartLoc,
1857 SourceLocation IdLoc, IdentifierInfo *Id,
1858 NamespaceDecl *PrevDecl) {
1859 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001860}
1861
1862NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1863 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001864 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1865 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001866}
1867
David Blaikie99ba9e32011-12-20 02:48:34 +00001868void NamespaceAliasDecl::anchor() { }
1869
Mike Stump1eb44332009-09-09 15:08:12 +00001870NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001871 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001872 SourceLocation AliasLoc,
1873 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001874 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001875 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001876 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001877 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1878 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001879 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1880 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001881}
1882
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001883NamespaceAliasDecl *
1884NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1885 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1886 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1887 NestedNameSpecifierLoc(),
1888 SourceLocation(), 0);
1889}
1890
David Blaikie99ba9e32011-12-20 02:48:34 +00001891void UsingShadowDecl::anchor() { }
1892
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001893UsingShadowDecl *
1894UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1895 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1896 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1897}
1898
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001899UsingDecl *UsingShadowDecl::getUsingDecl() const {
1900 const UsingShadowDecl *Shadow = this;
1901 while (const UsingShadowDecl *NextShadow =
1902 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1903 Shadow = NextShadow;
1904 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1905}
1906
David Blaikie99ba9e32011-12-20 02:48:34 +00001907void UsingDecl::anchor() { }
1908
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001909void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1910 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1911 "declaration already in set");
1912 assert(S->getUsingDecl() == this);
1913
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001914 if (FirstUsingShadow.getPointer())
1915 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1916 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001917}
1918
1919void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1920 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1921 "declaration not in set");
1922 assert(S->getUsingDecl() == this);
1923
1924 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1925
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001926 if (FirstUsingShadow.getPointer() == S) {
1927 FirstUsingShadow.setPointer(
1928 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001929 S->UsingOrNextShadow = this;
1930 return;
1931 }
1932
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001933 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001934 while (Prev->UsingOrNextShadow != S)
1935 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1936 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1937 S->UsingOrNextShadow = this;
1938}
1939
Douglas Gregordc355712011-02-25 00:36:19 +00001940UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1941 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001942 const DeclarationNameInfo &NameInfo,
1943 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001944 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001945}
1946
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001947UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1948 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1949 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1950 DeclarationNameInfo(), false);
1951}
1952
David Blaikie99ba9e32011-12-20 02:48:34 +00001953void UnresolvedUsingValueDecl::anchor() { }
1954
John McCall7ba107a2009-11-18 02:36:19 +00001955UnresolvedUsingValueDecl *
1956UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1957 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001958 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001959 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001960 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001961 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001962}
1963
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001964UnresolvedUsingValueDecl *
1965UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1966 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1967 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1968 NestedNameSpecifierLoc(),
1969 DeclarationNameInfo());
1970}
1971
David Blaikie99ba9e32011-12-20 02:48:34 +00001972void UnresolvedUsingTypenameDecl::anchor() { }
1973
John McCall7ba107a2009-11-18 02:36:19 +00001974UnresolvedUsingTypenameDecl *
1975UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1976 SourceLocation UsingLoc,
1977 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001978 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001979 SourceLocation TargetNameLoc,
1980 DeclarationName TargetName) {
1981 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001982 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001983 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001984}
1985
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001986UnresolvedUsingTypenameDecl *
1987UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1988 void *Mem = AllocateDeserializedDecl(C, ID,
1989 sizeof(UnresolvedUsingTypenameDecl));
1990 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
1991 SourceLocation(),
1992 NestedNameSpecifierLoc(),
1993 SourceLocation(),
1994 0);
1995}
1996
David Blaikie99ba9e32011-12-20 02:48:34 +00001997void StaticAssertDecl::anchor() { }
1998
Anders Carlssonfb311762009-03-14 00:25:26 +00001999StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002000 SourceLocation StaticAssertLoc,
2001 Expr *AssertExpr,
2002 StringLiteral *Message,
2003 SourceLocation RParenLoc) {
2004 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2005 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00002006}
2007
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002008StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2009 unsigned ID) {
2010 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
2011 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,SourceLocation());
2012}
2013
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002014static const char *getAccessName(AccessSpecifier AS) {
2015 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002016 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002017 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002018 case AS_public:
2019 return "public";
2020 case AS_private:
2021 return "private";
2022 case AS_protected:
2023 return "protected";
2024 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002025 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002026}
2027
2028const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2029 AccessSpecifier AS) {
2030 return DB << getAccessName(AS);
2031}
Richard Smithf15fda02012-02-02 01:16:57 +00002032
2033const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2034 AccessSpecifier AS) {
2035 return DB << getAccessName(AS);
2036}