blob: fcd45088c2e7bea5317549e34508d0c40be1bcb9 [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,
Douglas Gregor9daa7bf2012-02-13 16:35:30 +000041 ArrayRef<VarDecl *> ArrayIndexVars,
42 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregorda8962a2012-02-13 15:44:47 +000043 Stmt *Body) {
44 NumCaptures = Captures.size();
45 NumExplicitCaptures = 0;
46
47 ASTContext &Context = Definition->getASTContext();
Douglas Gregor9daa7bf2012-02-13 16:35:30 +000048 unsigned ArrayIndexSize = 0;
49 if (ArrayIndexVars.size() > 0) {
50 HasArrayIndexVars = true;
51 ArrayIndexSize = sizeof(unsigned) * (Captures.size() + 1)
52 + sizeof(VarDecl *) * ArrayIndexVars.size();
53 }
54
Douglas Gregorda8962a2012-02-13 15:44:47 +000055 this->Extra = Context.Allocate(sizeof(Capture) * Captures.size() +
Douglas Gregor9daa7bf2012-02-13 16:35:30 +000056 sizeof(Stmt*) * (Captures.size() + 1) +
57 ArrayIndexSize);
Douglas Gregorda8962a2012-02-13 15:44:47 +000058
59 // Copy captures.
60 Capture *ToCapture = getCaptures();
61 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
62 if (Captures[I].isExplicit())
63 ++NumExplicitCaptures;
64
65 *ToCapture++ = Captures[I];
66 }
67
68 // Copy initialization expressions for the non-static data members.
69 Stmt **Stored = getStoredStmts();
70 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
71 *Stored++ = CaptureInits[I];
72
73 // Copy the body of the lambda.
74 *Stored++ = Body;
Douglas Gregor9daa7bf2012-02-13 16:35:30 +000075
76 if (ArrayIndexVars.size() > 0) {
77 assert(ArrayIndexStarts.size() == Captures.size());
78 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
79 sizeof(VarDecl *) * ArrayIndexVars.size());
80 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
81 sizeof(unsigned) * Captures.size());
82 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
83 }
Douglas Gregorda8962a2012-02-13 15:44:47 +000084}
85
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000086
John McCall86ff3082010-02-04 22:26:26 +000087CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
88 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000089 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
90 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000091 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000092 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000093 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000094 HasMutableFields(false), HasOnlyCMembers(true),
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +000095 HasTrivialDefaultConstructor(true),
Richard Smith61802452011-12-22 02:22:31 +000096 HasConstexprNonCopyMoveConstructor(false),
97 DefaultedDefaultConstructorIsConstexpr(true),
98 DefaultedCopyConstructorIsConstexpr(true),
99 DefaultedMoveConstructorIsConstexpr(true),
100 HasConstexprDefaultConstructor(false), HasConstexprCopyConstructor(false),
101 HasConstexprMoveConstructor(false), HasTrivialCopyConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +0000102 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
103 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
104 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +0000105 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +0000106 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
107 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sebastian Redl85ea7aa2011-08-30 19:58:05 +0000108 DeclaredDestructor(false), FailedImplicitMoveConstructor(false),
Eli Friedman72899c32012-01-07 04:59:52 +0000109 FailedImplicitMoveAssignment(false), IsLambda(false), NumBases(0),
110 NumVBases(0), Bases(), VBases(), Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +0000111}
112
113CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000114 SourceLocation StartLoc, SourceLocation IdLoc,
115 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
116 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +0000117 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000118 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +0000119
Jay Foad4ba2a172011-01-12 09:06:06 +0000120CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000121 DeclContext *DC, SourceLocation StartLoc,
122 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000123 CXXRecordDecl* PrevDecl,
124 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000125 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
126 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000128 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000129 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +0000130 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000131 return R;
132}
133
Douglas Gregorda8962a2012-02-13 15:44:47 +0000134CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
135 SourceLocation Loc) {
136 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
137 0, 0);
138 R->IsBeingDefined = true;
139 R->DefinitionData = new (C) struct LambdaDefinitionData(R);
140 C.getTypeDeclType(R, /*PrevDecl=*/0);
141 return R;
142}
143
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000144CXXRecordDecl *
145CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
146 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
147 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
148 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000149}
150
Mike Stump1eb44332009-09-09 15:08:12 +0000151void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000152CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000153 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000154 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000155
Douglas Gregor7c789c12010-10-29 22:39:52 +0000156 if (!data().Bases.isOffset() && data().NumBases > 0)
157 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Richard Smithdd677232011-10-18 20:08:55 +0000159 if (NumBases) {
160 // C++ [dcl.init.aggr]p1:
161 // An aggregate is [...] a class with [...] no base classes [...].
162 data().Aggregate = false;
163
164 // C++ [class]p4:
165 // A POD-struct is an aggregate class...
166 data().PlainOldData = false;
167 }
168
Anders Carlsson6f6de732010-03-29 05:13:12 +0000169 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000170 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000171
172 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000173 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
John McCall86ff3082010-02-04 22:26:26 +0000175 data().Bases = new(C) CXXBaseSpecifier [NumBases];
176 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000177 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000178 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000179 // Keep track of inherited vbases for this base class.
180 const CXXBaseSpecifier *Base = Bases[i];
181 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000182 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000183 if (BaseType->isDependentType())
184 continue;
185 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000186 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000187
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000188 // A class with a non-empty base class is not empty.
189 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000190 if (!BaseClassDecl->isEmpty()) {
191 if (!data().Empty) {
192 // C++0x [class]p7:
193 // A standard-layout class is a class that:
194 // [...]
195 // -- either has no non-static data members in the most derived
196 // class and at most one base class with non-static data members,
197 // or has no base classes with non-static data members, and
198 // If this is the second non-empty base, then neither of these two
199 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000200 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000201 }
202
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000203 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000204 data().HasNoNonEmptyBases = false;
205 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000206
Douglas Gregor85606eb2010-09-28 20:50:54 +0000207 // C++ [class.virtual]p1:
208 // A class that declares or inherits a virtual function is called a
209 // polymorphic class.
210 if (BaseClassDecl->isPolymorphic())
211 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000212
Chandler Carrutha8225442011-04-30 09:17:45 +0000213 // C++0x [class]p7:
214 // A standard-layout class is a class that: [...]
215 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000216 if (!BaseClassDecl->isStandardLayout())
217 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000218
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000219 // Record if this base is the first non-literal field or base.
220 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
221 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000222
Anders Carlsson6f6de732010-03-29 05:13:12 +0000223 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000224 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000225 BaseClassDecl->vbases_begin(),
226 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000227 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000228 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000229 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000230 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000231
232 if (Base->isVirtual()) {
233 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000234 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000235 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000236
237 // C++0x [meta.unary.prop] is_empty:
238 // T is a class type, but not a union type, with ... no virtual base
239 // classes
240 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000241
242 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000243 // A default constructor is trivial [...] if:
244 // -- its class has [...] no virtual bases
245 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000246
247 // C++0x [class.copy]p13:
248 // A copy/move constructor for class X is trivial if it is neither
249 // user-provided nor deleted and if
250 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000251 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000252 data().HasTrivialMoveConstructor = false;
253
254 // C++0x [class.copy]p27:
255 // A copy/move assignment operator for class X is trivial if it is
256 // neither user-provided nor deleted and if
257 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000258 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000259 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000260
261 // C++0x [class]p7:
262 // A standard-layout class is a class that: [...]
263 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000264 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000265
266 // C++11 [dcl.constexpr]p4:
267 // In the definition of a constexpr constructor [...]
268 // -- the class shall not have any virtual base classes
269 data().DefaultedDefaultConstructorIsConstexpr = false;
270 data().DefaultedCopyConstructorIsConstexpr = false;
271 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000272 } else {
273 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000274 // A default constructor is trivial [...] if:
275 // -- all the direct base classes of its class have trivial default
276 // constructors.
277 if (!BaseClassDecl->hasTrivialDefaultConstructor())
278 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000279
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000280 // C++0x [class.copy]p13:
281 // A copy/move constructor for class X is trivial if [...]
282 // [...]
283 // -- the constructor selected to copy/move each direct base class
284 // subobject is trivial, and
285 // FIXME: C++0x: We need to only consider the selected constructor
286 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000287 if (!BaseClassDecl->hasTrivialCopyConstructor())
288 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000289 if (!BaseClassDecl->hasTrivialMoveConstructor())
290 data().HasTrivialMoveConstructor = false;
291
292 // C++0x [class.copy]p27:
293 // A copy/move assignment operator for class X is trivial if [...]
294 // [...]
295 // -- the assignment operator selected to copy/move each direct base
296 // class subobject is trivial, and
297 // FIXME: C++0x: We need to only consider the selected operator instead
298 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000299 if (!BaseClassDecl->hasTrivialCopyAssignment())
300 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000301 if (!BaseClassDecl->hasTrivialMoveAssignment())
302 data().HasTrivialMoveAssignment = false;
Richard Smith61802452011-12-22 02:22:31 +0000303
304 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000305 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000306 // requirements of a constexpr constructor, the implicitly-defined
307 // default constructor is constexpr.
308 if (!BaseClassDecl->hasConstexprDefaultConstructor())
309 data().DefaultedDefaultConstructorIsConstexpr = false;
310
311 // C++11 [class.copy]p13:
312 // If the implicitly-defined constructor would satisfy the requirements
313 // of a constexpr constructor, the implicitly-defined constructor is
314 // constexpr.
315 // C++11 [dcl.constexpr]p4:
316 // -- every constructor involved in initializing [...] base class
317 // sub-objects shall be a constexpr constructor
318 if (!BaseClassDecl->hasConstexprCopyConstructor())
319 data().DefaultedCopyConstructorIsConstexpr = false;
320 if (BaseClassDecl->hasDeclaredMoveConstructor() ||
321 BaseClassDecl->needsImplicitMoveConstructor())
322 // FIXME: If the implicit move constructor generated for the base class
323 // would be ill-formed, the implicit move constructor generated for the
324 // derived class calls the base class' copy constructor.
325 data().DefaultedMoveConstructorIsConstexpr &=
Richard Smithde8facc2012-01-11 18:26:05 +0000326 BaseClassDecl->hasConstexprMoveConstructor();
Richard Smith61802452011-12-22 02:22:31 +0000327 else if (!BaseClassDecl->hasConstexprCopyConstructor())
328 data().DefaultedMoveConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000329 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000330
331 // C++ [class.ctor]p3:
332 // A destructor is trivial if all the direct base classes of its class
333 // have trivial destructors.
334 if (!BaseClassDecl->hasTrivialDestructor())
335 data().HasTrivialDestructor = false;
Douglas Gregor2bb11012011-05-13 01:05:07 +0000336
John McCallf85e1932011-06-15 23:02:42 +0000337 // A class has an Objective-C object member if... or any of its bases
338 // has an Objective-C object member.
339 if (BaseClassDecl->hasObjectMember())
340 setHasObjectMember(true);
341
Douglas Gregor2bb11012011-05-13 01:05:07 +0000342 // Keep track of the presence of mutable fields.
343 if (BaseClassDecl->hasMutableFields())
344 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000345 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000346
347 if (VBases.empty())
348 return;
349
350 // Create base specifier for any direct or indirect virtual bases.
351 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
352 data().NumVBases = VBases.size();
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000353 for (int I = 0, E = VBases.size(); I != E; ++I)
354 data().getVBases()[I] = *VBases[I];
Douglas Gregor57c856b2008-10-23 18:13:27 +0000355}
356
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000357/// Callback function for CXXRecordDecl::forallBases that acknowledges
358/// that it saw a base class.
359static bool SawBase(const CXXRecordDecl *, void *) {
360 return true;
361}
362
363bool CXXRecordDecl::hasAnyDependentBases() const {
364 if (!isDependentContext())
365 return false;
366
367 return !forallBases(SawBase, 0);
368}
369
Sean Huntffe37fd2011-05-25 20:50:04 +0000370bool CXXRecordDecl::hasConstCopyConstructor() const {
371 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000372}
373
Chandler Carruthb7e95892011-04-23 10:47:28 +0000374bool CXXRecordDecl::isTriviallyCopyable() const {
375 // C++0x [class]p5:
376 // A trivially copyable class is a class that:
377 // -- has no non-trivial copy constructors,
378 if (!hasTrivialCopyConstructor()) return false;
379 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000380 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000381 // -- has no non-trivial copy assignment operators,
382 if (!hasTrivialCopyAssignment()) return false;
383 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000384 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000385 // -- has a trivial destructor.
386 if (!hasTrivialDestructor()) return false;
387
388 return true;
389}
390
Douglas Gregor0d405db2010-07-01 20:59:04 +0000391/// \brief Perform a simplistic form of overload resolution that only considers
392/// cv-qualifiers on a single parameter, and return the best overload candidate
393/// (if there is one).
394static CXXMethodDecl *
395GetBestOverloadCandidateSimple(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000396 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor0d405db2010-07-01 20:59:04 +0000397 if (Cands.empty())
398 return 0;
399 if (Cands.size() == 1)
400 return Cands[0].first;
401
402 unsigned Best = 0, N = Cands.size();
403 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000404 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000405 Best = I;
406
407 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000408 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000409 return 0;
410
411 return Cands[Best].first;
412}
413
Sean Huntffe37fd2011-05-25 20:50:04 +0000414CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
415 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000416 QualType ClassType
417 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000418 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000419 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000420 Context.getCanonicalType(ClassType));
421 unsigned FoundTQs;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000422 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000423 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000424 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000425 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000426 // C++ [class.copy]p2:
427 // A non-template constructor for class X is a copy constructor if [...]
428 if (isa<FunctionTemplateDecl>(*Con))
429 continue;
430
Douglas Gregor0d405db2010-07-01 20:59:04 +0000431 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
432 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000433 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
434 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000435 Found.push_back(std::make_pair(
436 const_cast<CXXConstructorDecl *>(Constructor),
437 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000438 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000439 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000440
441 return cast_or_null<CXXConstructorDecl>(
442 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000443}
444
Sean Huntffe37fd2011-05-25 20:50:04 +0000445CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
446 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
447 if (I->isMoveConstructor())
448 return *I;
449
450 return 0;
451}
452
Douglas Gregorb87786f2010-07-01 17:48:08 +0000453CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
454 ASTContext &Context = getASTContext();
455 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
456 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
457
Chris Lattner5f9e2722011-07-23 10:55:15 +0000458 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorb87786f2010-07-01 17:48:08 +0000459 DeclContext::lookup_const_iterator Op, OpEnd;
460 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
461 // C++ [class.copy]p9:
462 // A user-declared copy assignment operator is a non-static non-template
463 // member function of class X with exactly one parameter of type X, X&,
464 // const X&, volatile X& or const volatile X&.
465 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
466 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
467 continue;
468
469 const FunctionProtoType *FnType
470 = Method->getType()->getAs<FunctionProtoType>();
471 assert(FnType && "Overloaded operator has no prototype.");
472 // Don't assert on this; an invalid decl might have been left in the AST.
473 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
474 continue;
475
476 QualType ArgType = FnType->getArgType(0);
477 Qualifiers Quals;
478 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
479 ArgType = Ref->getPointeeType();
480 // If we have a const argument and we have a reference to a non-const,
481 // this function does not match.
482 if (ArgIsConst && !ArgType.isConstQualified())
483 continue;
484
485 Quals = ArgType.getQualifiers();
486 } else {
487 // By-value copy-assignment operators are treated like const X&
488 // copy-assignment operators.
489 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
490 }
491
492 if (!Context.hasSameUnqualifiedType(ArgType, Class))
493 continue;
494
495 // Save this copy-assignment operator. It might be "the one".
496 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
497 }
498
499 // Use a simplistic form of overload resolution to find the candidate.
500 return GetBestOverloadCandidateSimple(Found);
501}
502
Sean Huntffe37fd2011-05-25 20:50:04 +0000503CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
504 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
505 if (I->isMoveAssignmentOperator())
506 return *I;
507
508 return 0;
509}
510
Douglas Gregor21386642010-09-28 21:55:22 +0000511void CXXRecordDecl::markedVirtualFunctionPure() {
512 // C++ [class.abstract]p2:
513 // A class is abstract if it has at least one pure virtual function.
514 data().Abstract = true;
515}
516
517void CXXRecordDecl::addedMember(Decl *D) {
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +0000518 if (!D->isImplicit() &&
519 !isa<FieldDecl>(D) &&
520 !isa<IndirectFieldDecl>(D) &&
521 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class))
522 data().HasOnlyCMembers = false;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000523
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000524 // Ignore friends and invalid declarations.
525 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000526 return;
527
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000528 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
529 if (FunTmpl)
530 D = FunTmpl->getTemplatedDecl();
531
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000532 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
533 if (Method->isVirtual()) {
534 // C++ [dcl.init.aggr]p1:
535 // An aggregate is an array or a class with [...] no virtual functions.
536 data().Aggregate = false;
537
538 // C++ [class]p4:
539 // A POD-struct is an aggregate class...
540 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000541
542 // Virtual functions make the class non-empty.
543 // FIXME: Standard ref?
544 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000545
546 // C++ [class.virtual]p1:
547 // A class that declares or inherits a virtual function is called a
548 // polymorphic class.
549 data().Polymorphic = true;
550
Sean Hunt023df372011-05-09 18:22:59 +0000551 // C++0x [class.ctor]p5
552 // A default constructor is trivial [...] if:
553 // -- its class has no virtual functions [...]
554 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000555
556 // C++0x [class.copy]p13:
557 // A copy/move constructor for class X is trivial if [...]
558 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000559 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000560 data().HasTrivialMoveConstructor = false;
561
562 // C++0x [class.copy]p27:
563 // A copy/move assignment operator for class X is trivial if [...]
564 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000565 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000566 data().HasTrivialMoveAssignment = false;
Douglas Gregor45fa5602011-11-07 20:56:01 +0000567
Chandler Carrutha8225442011-04-30 09:17:45 +0000568 // C++0x [class]p7:
569 // A standard-layout class is a class that: [...]
570 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000571 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000572 }
573 }
574
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000575 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000576 // Notify that an implicit member was added after the definition
577 // was completed.
578 if (!isBeingDefined())
579 if (ASTMutationListener *L = getASTMutationListener())
580 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000581
Sean Huntffe37fd2011-05-25 20:50:04 +0000582 // If this is a special member function, note that it was added and then
583 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000584 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith61802452011-12-22 02:22:31 +0000585 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000586 data().DeclaredDefaultConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000587 if (Constructor->isConstexpr()) {
588 data().HasConstexprDefaultConstructor = true;
589 data().HasConstexprNonCopyMoveConstructor = true;
590 }
591 } else if (Constructor->isCopyConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000592 data().DeclaredCopyConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000593 if (Constructor->isConstexpr())
594 data().HasConstexprCopyConstructor = true;
595 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000596 data().DeclaredMoveConstructor = true;
Richard Smith61802452011-12-22 02:22:31 +0000597 if (Constructor->isConstexpr())
598 data().HasConstexprMoveConstructor = true;
599 } else
Sean Huntffe37fd2011-05-25 20:50:04 +0000600 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000601 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000602 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000603 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000604 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000605 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
606 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000607 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000608 else if (Method->isMoveAssignmentOperator())
609 data().DeclaredMoveAssignment = true;
610 else
611 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000612 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000613 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000614
Sean Huntffe37fd2011-05-25 20:50:04 +0000615NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000616 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000617 }
618
619 // Handle (user-declared) constructors.
620 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
621 // Note that we have a user-declared constructor.
622 data().UserDeclaredConstructor = true;
623
Richard Smith017ab772011-09-05 02:13:09 +0000624 // Technically, "user-provided" is only defined for special member
625 // functions, but the intent of the standard is clearly that it should apply
626 // to all functions.
627 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000628
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000629 if (Constructor->isDefaultConstructor()) {
630 data().DeclaredDefaultConstructor = true;
Richard Smith017ab772011-09-05 02:13:09 +0000631 if (UserProvided) {
Richard Smith61802452011-12-22 02:22:31 +0000632 // C++0x [class.ctor]p5:
633 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000634 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000635 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000636 }
Richard Smith61802452011-12-22 02:22:31 +0000637 if (Constructor->isConstexpr()) {
638 data().HasConstexprDefaultConstructor = true;
639 data().HasConstexprNonCopyMoveConstructor = true;
640 }
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000641 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000642
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000643 // Note when we have a user-declared copy or move constructor, which will
644 // suppress the implicit declaration of those constructors.
645 if (!FunTmpl) {
646 if (Constructor->isCopyConstructor()) {
647 data().UserDeclaredCopyConstructor = true;
648 data().DeclaredCopyConstructor = true;
649
650 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000651 // A copy/move constructor for class X is trivial if it is not
652 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000653 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000654 data().HasTrivialCopyConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000655
656 if (Constructor->isConstexpr())
657 data().HasConstexprCopyConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000658 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000659 data().UserDeclaredMoveConstructor = true;
660 data().DeclaredMoveConstructor = true;
661
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000662 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000663 // A copy/move constructor for class X is trivial if it is not
664 // user-provided [...]
Richard Smith017ab772011-09-05 02:13:09 +0000665 if (UserProvided)
Sean Hunt023df372011-05-09 18:22:59 +0000666 data().HasTrivialMoveConstructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000667
668 if (Constructor->isConstexpr())
669 data().HasConstexprMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000670 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000671 }
Richard Smith6b8bc072011-08-10 18:11:37 +0000672 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
673 // Record if we see any constexpr constructors which are neither copy
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000674 // nor move constructors.
Richard Smith6b8bc072011-08-10 18:11:37 +0000675 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000676 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000677
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000678 // C++ [dcl.init.aggr]p1:
679 // An aggregate is an array or a class with no user-declared
680 // constructors [...].
681 // C++0x [dcl.init.aggr]p1:
682 // An aggregate is an array or a class with no user-provided
683 // constructors [...].
684 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
685 data().Aggregate = false;
686
687 // C++ [class]p4:
688 // A POD-struct is an aggregate class [...]
689 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
690 // type is technically an aggregate in C++0x since it wouldn't be in 03.
691 data().PlainOldData = false;
692
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000693 return;
694 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000695
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000696 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000697 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000698 data().DeclaredDestructor = true;
699 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000700
701 // C++ [class]p4:
702 // A POD-struct is an aggregate class that has [...] no user-defined
703 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000704 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000705 data().PlainOldData = false;
706
Douglas Gregor45fa5602011-11-07 20:56:01 +0000707 // C++11 [class.dtor]p5:
708 // A destructor is trivial if it is not user-provided and if
709 // -- the destructor is not virtual.
Richard Smith61802452011-12-22 02:22:31 +0000710 if (DD->isUserProvided() || DD->isVirtual()) {
Sean Huntcf34e752011-05-16 22:41:40 +0000711 data().HasTrivialDestructor = false;
Richard Smith61802452011-12-22 02:22:31 +0000712 // C++11 [dcl.constexpr]p1:
713 // The constexpr specifier shall be applied only to [...] the
714 // declaration of a static data member of a literal type.
715 // C++11 [basic.types]p10:
716 // A type is a literal type if it is [...] a class type that [...] has
717 // a trivial destructor.
718 data().DefaultedDefaultConstructorIsConstexpr = false;
719 data().DefaultedCopyConstructorIsConstexpr = false;
720 data().DefaultedMoveConstructorIsConstexpr = false;
721 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000722
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000723 return;
724 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000725
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000726 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000727 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000728 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000729 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000730 // A POD-struct is an aggregate class that [...] has no user-defined
731 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000732 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000733 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000734
Sean Huntffe37fd2011-05-25 20:50:04 +0000735 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000736
Sean Huntffe37fd2011-05-25 20:50:04 +0000737 // Suppress the implicit declaration of a copy constructor.
738 data().UserDeclaredCopyAssignment = true;
739 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000740
Sean Huntffe37fd2011-05-25 20:50:04 +0000741 // C++0x [class.copy]p27:
742 // A copy/move assignment operator for class X is trivial if it is
743 // neither user-provided nor deleted [...]
744 if (Method->isUserProvided())
745 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000746
Sean Huntffe37fd2011-05-25 20:50:04 +0000747 return;
748 }
749
750 if (Method->isMoveAssignmentOperator()) {
751 // This is an extension in C++03 mode, but we'll keep consistency by
752 // taking a move assignment operator to induce non-POD-ness
753 data().PlainOldData = false;
754
755 // This is a move assignment operator.
756 data().UserDeclaredMoveAssignment = true;
757 data().DeclaredMoveAssignment = true;
758
759 // C++0x [class.copy]p27:
760 // A copy/move assignment operator for class X is trivial if it is
761 // neither user-provided nor deleted [...]
762 if (Method->isUserProvided())
763 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000764 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000765
Douglas Gregore80622f2010-09-29 04:25:11 +0000766 // Keep the list of conversion functions up-to-date.
767 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
768 // We don't record specializations.
769 if (Conversion->getPrimaryTemplate())
770 return;
771
772 // FIXME: We intentionally don't use the decl's access here because it
773 // hasn't been set yet. That's really just a misdesign in Sema.
774
775 if (FunTmpl) {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000776 if (FunTmpl->getPreviousDecl())
777 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000778 FunTmpl);
779 else
780 data().Conversions.addDecl(FunTmpl);
781 } else {
Douglas Gregoref96ee02012-01-14 16:38:05 +0000782 if (Conversion->getPreviousDecl())
783 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregore80622f2010-09-29 04:25:11 +0000784 Conversion);
785 else
786 data().Conversions.addDecl(Conversion);
787 }
788 }
789
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000790 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000791 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000792
793 // Handle non-static data members.
794 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000795 // C++ [class.bit]p2:
796 // A declaration for a bit-field that omits the identifier declares an
797 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
798 // initialized.
799 if (Field->isUnnamedBitfield())
800 return;
801
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000802 // C++ [dcl.init.aggr]p1:
803 // An aggregate is an array or a class (clause 9) with [...] no
804 // private or protected non-static data members (clause 11).
805 //
806 // A POD must be an aggregate.
807 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
808 data().Aggregate = false;
809 data().PlainOldData = false;
810 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000811
812 // C++0x [class]p7:
813 // A standard-layout class is a class that:
814 // [...]
815 // -- has the same access control for all non-static data members,
816 switch (D->getAccess()) {
817 case AS_private: data().HasPrivateFields = true; break;
818 case AS_protected: data().HasProtectedFields = true; break;
819 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000820 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000821 };
822 if ((data().HasPrivateFields + data().HasProtectedFields +
823 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000824 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000825
Douglas Gregor2bb11012011-05-13 01:05:07 +0000826 // Keep track of the presence of mutable fields.
827 if (Field->isMutable())
828 data().HasMutableFields = true;
829
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000830 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000831 // A POD struct is a class that is both a trivial class and a
832 // standard-layout class, and has no non-static data members of type
833 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000834 //
835 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
836 // that does not explicitly have no lifetime makes the class a non-POD.
837 // However, we delay setting PlainOldData to false in this case so that
838 // Sema has a chance to diagnostic causes where the same class will be
839 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
840 // In this case, the class will become a non-POD class when we complete
841 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000842 ASTContext &Context = getASTContext();
843 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000844 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
845 if (!Context.getLangOptions().ObjCAutoRefCount ||
846 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
847 setHasObjectMember(true);
848 } else if (!T.isPODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000849 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000850
Chandler Carrutha8225442011-04-30 09:17:45 +0000851 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000852 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000853
Chandler Carrutha8225442011-04-30 09:17:45 +0000854 // C++0x [class]p7:
855 // A standard-layout class is a class that:
856 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000857 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000858 }
859
Richard Smith86c3ae42012-02-13 03:54:03 +0000860 // Record if this field is the first non-literal or volatile field or base.
861 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000862 data().HasNonLiteralTypeFieldsOrBases = true;
863
Richard Smith7a614d82011-06-11 17:19:42 +0000864 if (Field->hasInClassInitializer()) {
865 // C++0x [class]p5:
866 // A default constructor is trivial if [...] no non-static data member
867 // of its class has a brace-or-equal-initializer.
868 data().HasTrivialDefaultConstructor = false;
869
870 // C++0x [dcl.init.aggr]p1:
871 // An aggregate is a [...] class with [...] no
872 // brace-or-equal-initializers for non-static data members.
873 data().Aggregate = false;
874
875 // C++0x [class]p10:
876 // A POD struct is [...] a trivial class.
877 data().PlainOldData = false;
878 }
879
Douglas Gregor85606eb2010-09-28 20:50:54 +0000880 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
881 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
882 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000883 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000884 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000885 // -- for all the non-static data members of its class that are of
886 // class type (or array thereof), each such class has a trivial
887 // default constructor.
888 if (!FieldRec->hasTrivialDefaultConstructor())
889 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000890
891 // C++0x [class.copy]p13:
892 // A copy/move constructor for class X is trivial if [...]
893 // [...]
894 // -- for each non-static data member of X that is of class type (or
895 // an array thereof), the constructor selected to copy/move that
896 // member is trivial;
897 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000898 if (!FieldRec->hasTrivialCopyConstructor())
899 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000900 if (!FieldRec->hasTrivialMoveConstructor())
901 data().HasTrivialMoveConstructor = false;
902
903 // C++0x [class.copy]p27:
904 // A copy/move assignment operator for class X is trivial if [...]
905 // [...]
906 // -- for each non-static data member of X that is of class type (or
907 // an array thereof), the assignment operator selected to
908 // copy/move that member is trivial;
909 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000910 if (!FieldRec->hasTrivialCopyAssignment())
911 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000912 if (!FieldRec->hasTrivialMoveAssignment())
913 data().HasTrivialMoveAssignment = false;
914
Douglas Gregor85606eb2010-09-28 20:50:54 +0000915 if (!FieldRec->hasTrivialDestructor())
916 data().HasTrivialDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000917 if (FieldRec->hasObjectMember())
918 setHasObjectMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000919
920 // C++0x [class]p7:
921 // A standard-layout class is a class that:
922 // -- has no non-static data members of type non-standard-layout
923 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000924 if (!FieldRec->isStandardLayout())
925 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000926
927 // C++0x [class]p7:
928 // A standard-layout class is a class that:
929 // [...]
930 // -- has no base classes of the same type as the first non-static
931 // data member.
932 // We don't want to expend bits in the state of the record decl
933 // tracking whether this is the first non-static data member so we
934 // cheat a bit and use some of the existing state: the empty bit.
935 // Virtual bases and virtual methods make a class non-empty, but they
936 // also make it non-standard-layout so we needn't check here.
937 // A non-empty base class may leave the class standard-layout, but not
938 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000939 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000940 // data member must come through here with Empty still true, and Empty
941 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000942 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000943 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
944 BE = bases_end();
945 BI != BE; ++BI) {
946 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000947 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000948 break;
949 }
950 }
951 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000952
953 // Keep track of the presence of mutable fields.
954 if (FieldRec->hasMutableFields())
955 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000956
957 // C++11 [class.copy]p13:
958 // If the implicitly-defined constructor would satisfy the
959 // requirements of a constexpr constructor, the implicitly-defined
960 // constructor is constexpr.
961 // C++11 [dcl.constexpr]p4:
962 // -- every constructor involved in initializing non-static data
963 // members [...] shall be a constexpr constructor
964 if (!Field->hasInClassInitializer() &&
965 !FieldRec->hasConstexprDefaultConstructor())
966 // The standard requires any in-class initializer to be a constant
967 // expression. We consider this to be a defect.
968 data().DefaultedDefaultConstructorIsConstexpr = false;
969
970 if (!FieldRec->hasConstexprCopyConstructor())
971 data().DefaultedCopyConstructorIsConstexpr = false;
972
973 if (FieldRec->hasDeclaredMoveConstructor() ||
974 FieldRec->needsImplicitMoveConstructor())
975 // FIXME: If the implicit move constructor generated for the member's
976 // class would be ill-formed, the implicit move constructor generated
977 // for this class calls the member's copy constructor.
978 data().DefaultedMoveConstructorIsConstexpr &=
979 FieldRec->hasConstexprMoveConstructor();
980 else if (!FieldRec->hasConstexprCopyConstructor())
981 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000982 }
Richard Smith61802452011-12-22 02:22:31 +0000983 } else {
984 // Base element type of field is a non-class type.
985 if (!T->isLiteralType()) {
986 data().DefaultedDefaultConstructorIsConstexpr = false;
987 data().DefaultedCopyConstructorIsConstexpr = false;
988 data().DefaultedMoveConstructorIsConstexpr = false;
989 } else if (!Field->hasInClassInitializer())
990 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000991 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000992
993 // C++0x [class]p7:
994 // A standard-layout class is a class that:
995 // [...]
996 // -- either has no non-static data members in the most derived
997 // class and at most one base class with non-static data members,
998 // or has no base classes with non-static data members, and
999 // At this point we know that we have a non-static data member, so the last
1000 // clause holds.
1001 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +00001002 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +00001003
Douglas Gregor2cf9d652010-09-28 20:38:10 +00001004 // If this is not a zero-length bit-field, then the class is not empty.
1005 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001006 if (!Field->isBitField() ||
1007 (!Field->getBitWidth()->isTypeDependent() &&
1008 !Field->getBitWidth()->isValueDependent() &&
1009 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +00001010 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +00001011 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +00001012 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001013
1014 // Handle using declarations of conversion functions.
1015 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
1016 if (Shadow->getDeclName().getNameKind()
1017 == DeclarationName::CXXConversionFunctionName)
1018 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001019}
1020
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +00001021bool CXXRecordDecl::isCLike() const {
1022 if (getTagKind() == TTK_Class || !TemplateOrInstantiation.isNull())
1023 return false;
1024 if (!hasDefinition())
1025 return true;
1026
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +00001027 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +00001028}
1029
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001030void CXXRecordDecl::getCaptureFields(
1031 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +00001032 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001033 Captures.clear();
1034 ThisCapture = 0;
1035
Douglas Gregorda8962a2012-02-13 15:44:47 +00001036 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001037 RecordDecl::field_iterator Field = field_begin();
Douglas Gregorda8962a2012-02-13 15:44:47 +00001038 for (LambdaExpr::Capture *C = Lambda.getCaptures(),
1039 *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001040 C != CEnd; ++C, ++Field) {
1041 if (C->capturesThis()) {
1042 ThisCapture = *Field;
1043 continue;
1044 }
1045
1046 Captures[C->getCapturedVar()] = *Field;
1047 }
1048}
1049
1050
John McCallb05b5f32010-03-15 09:07:48 +00001051static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1052 QualType T;
John McCall32daa422010-03-31 01:36:47 +00001053 if (isa<UsingShadowDecl>(Conv))
1054 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +00001055 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
1056 T = ConvTemp->getTemplatedDecl()->getResultType();
1057 else
1058 T = cast<CXXConversionDecl>(Conv)->getConversionType();
1059 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +00001060}
1061
John McCallb05b5f32010-03-15 09:07:48 +00001062/// Collect the visible conversions of a base class.
1063///
1064/// \param Base a base class of the class we're considering
1065/// \param InVirtual whether this base class is a virtual base (or a base
1066/// of a virtual base)
1067/// \param Access the access along the inheritance path to this base
1068/// \param ParentHiddenTypes the conversions provided by the inheritors
1069/// of this base
1070/// \param Output the set to which to add conversions from non-virtual bases
1071/// \param VOutput the set to which to add conversions from virtual bases
1072/// \param HiddenVBaseCs the set of conversions which were hidden in a
1073/// virtual base along some inheritance path
1074static void CollectVisibleConversions(ASTContext &Context,
1075 CXXRecordDecl *Record,
1076 bool InVirtual,
1077 AccessSpecifier Access,
1078 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
1079 UnresolvedSetImpl &Output,
1080 UnresolvedSetImpl &VOutput,
1081 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1082 // The set of types which have conversions in this class or its
1083 // subclasses. As an optimization, we don't copy the derived set
1084 // unless it might change.
1085 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1086 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1087
1088 // Collect the direct conversions and figure out which conversions
1089 // will be hidden in the subclasses.
1090 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1091 if (!Cs.empty()) {
1092 HiddenTypesBuffer = ParentHiddenTypes;
1093 HiddenTypes = &HiddenTypesBuffer;
1094
1095 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
1096 bool Hidden =
1097 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
1098
1099 // If this conversion is hidden and we're in a virtual base,
1100 // remember that it's hidden along some inheritance path.
1101 if (Hidden && InVirtual)
1102 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1103
1104 // If this conversion isn't hidden, add it to the appropriate output.
1105 else if (!Hidden) {
1106 AccessSpecifier IAccess
1107 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1108
1109 if (InVirtual)
1110 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001111 else
John McCallb05b5f32010-03-15 09:07:48 +00001112 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001113 }
1114 }
1115 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001116
John McCallb05b5f32010-03-15 09:07:48 +00001117 // Collect information recursively from any base classes.
1118 for (CXXRecordDecl::base_class_iterator
1119 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1120 const RecordType *RT = I->getType()->getAs<RecordType>();
1121 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001122
John McCallb05b5f32010-03-15 09:07:48 +00001123 AccessSpecifier BaseAccess
1124 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1125 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001126
John McCallb05b5f32010-03-15 09:07:48 +00001127 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1128 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1129 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001130 }
John McCallb05b5f32010-03-15 09:07:48 +00001131}
Sebastian Redl9994a342009-10-25 17:03:50 +00001132
John McCallb05b5f32010-03-15 09:07:48 +00001133/// Collect the visible conversions of a class.
1134///
1135/// This would be extremely straightforward if it weren't for virtual
1136/// bases. It might be worth special-casing that, really.
1137static void CollectVisibleConversions(ASTContext &Context,
1138 CXXRecordDecl *Record,
1139 UnresolvedSetImpl &Output) {
1140 // The collection of all conversions in virtual bases that we've
1141 // found. These will be added to the output as long as they don't
1142 // appear in the hidden-conversions set.
1143 UnresolvedSet<8> VBaseCs;
1144
1145 // The set of conversions in virtual bases that we've determined to
1146 // be hidden.
1147 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1148
1149 // The set of types hidden by classes derived from this one.
1150 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1151
1152 // Go ahead and collect the direct conversions and add them to the
1153 // hidden-types set.
1154 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1155 Output.append(Cs.begin(), Cs.end());
1156 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1157 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1158
1159 // Recursively collect conversions from base classes.
1160 for (CXXRecordDecl::base_class_iterator
1161 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1162 const RecordType *RT = I->getType()->getAs<RecordType>();
1163 if (!RT) continue;
1164
1165 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1166 I->isVirtual(), I->getAccessSpecifier(),
1167 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1168 }
1169
1170 // Add any unhidden conversions provided by virtual bases.
1171 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1172 I != E; ++I) {
1173 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1174 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001175 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001176}
1177
1178/// getVisibleConversionFunctions - get all conversion functions visible
1179/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +00001180const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +00001181 // If root class, all conversions are visible.
1182 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +00001183 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +00001184 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +00001185 if (data().ComputedVisibleConversions)
1186 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +00001187 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +00001188 data().ComputedVisibleConversions = true;
1189 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +00001190}
1191
John McCall32daa422010-03-31 01:36:47 +00001192void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1193 // This operation is O(N) but extremely rare. Sema only uses it to
1194 // remove UsingShadowDecls in a class that were followed by a direct
1195 // declaration, e.g.:
1196 // class A : B {
1197 // using B::operator int;
1198 // operator int();
1199 // };
1200 // This is uncommon by itself and even more uncommon in conjunction
1201 // with sufficiently large numbers of directly-declared conversions
1202 // that asymptotic behavior matters.
1203
1204 UnresolvedSetImpl &Convs = *getConversionFunctions();
1205 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1206 if (Convs[I].getDecl() == ConvDecl) {
1207 Convs.erase(I);
1208 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1209 && "conversion was found multiple times in unresolved set");
1210 return;
1211 }
1212 }
1213
1214 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001215}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001216
Douglas Gregorf6b11852009-10-08 15:14:33 +00001217CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001218 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001219 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1220
1221 return 0;
1222}
1223
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001224MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1225 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1226}
1227
Douglas Gregorf6b11852009-10-08 15:14:33 +00001228void
1229CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1230 TemplateSpecializationKind TSK) {
1231 assert(TemplateOrInstantiation.isNull() &&
1232 "Previous template or instantiation?");
1233 assert(!isa<ClassTemplateSpecializationDecl>(this));
1234 TemplateOrInstantiation
1235 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1236}
1237
Anders Carlssonb13e3572009-12-07 06:33:48 +00001238TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1239 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001240 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1241 return Spec->getSpecializationKind();
1242
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001243 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001244 return MSInfo->getTemplateSpecializationKind();
1245
1246 return TSK_Undeclared;
1247}
1248
1249void
1250CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1251 if (ClassTemplateSpecializationDecl *Spec
1252 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1253 Spec->setSpecializationKind(TSK);
1254 return;
1255 }
1256
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001257 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001258 MSInfo->setTemplateSpecializationKind(TSK);
1259 return;
1260 }
1261
David Blaikieb219cfc2011-09-23 05:06:16 +00001262 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001263}
1264
Douglas Gregor1d110e02010-07-01 14:13:13 +00001265CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1266 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001267 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001268
1269 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001270 = Context.DeclarationNames.getCXXDestructorName(
1271 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001272
John McCallc0bf4622010-02-23 00:48:20 +00001273 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001274 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001275 if (I == E)
1276 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001278 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001279 return Dtor;
1280}
1281
Douglas Gregorda2142f2011-02-19 18:51:44 +00001282void CXXRecordDecl::completeDefinition() {
1283 completeDefinition(0);
1284}
1285
1286void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1287 RecordDecl::completeDefinition();
1288
John McCallf85e1932011-06-15 23:02:42 +00001289 if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) {
1290 // Objective-C Automatic Reference Counting:
1291 // If a class has a non-static data member of Objective-C pointer
1292 // type (or array thereof), it is a non-POD type and its
1293 // default constructor (if any), copy constructor, copy assignment
1294 // operator, and destructor are non-trivial.
1295 struct DefinitionData &Data = data();
1296 Data.PlainOldData = false;
1297 Data.HasTrivialDefaultConstructor = false;
1298 Data.HasTrivialCopyConstructor = false;
1299 Data.HasTrivialCopyAssignment = false;
1300 Data.HasTrivialDestructor = false;
1301 }
1302
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001303 // If the class may be abstract (but hasn't been marked as such), check for
1304 // any pure final overriders.
1305 if (mayBeAbstract()) {
1306 CXXFinalOverriderMap MyFinalOverriders;
1307 if (!FinalOverriders) {
1308 getFinalOverriders(MyFinalOverriders);
1309 FinalOverriders = &MyFinalOverriders;
1310 }
1311
1312 bool Done = false;
1313 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1314 MEnd = FinalOverriders->end();
1315 M != MEnd && !Done; ++M) {
1316 for (OverridingMethods::iterator SO = M->second.begin(),
1317 SOEnd = M->second.end();
1318 SO != SOEnd && !Done; ++SO) {
1319 assert(SO->second.size() > 0 &&
1320 "All virtual functions have overridding virtual functions");
1321
1322 // C++ [class.abstract]p4:
1323 // A class is abstract if it contains or inherits at least one
1324 // pure virtual function for which the final overrider is pure
1325 // virtual.
1326 if (SO->second.front().Method->isPure()) {
1327 data().Abstract = true;
1328 Done = true;
1329 break;
1330 }
1331 }
1332 }
1333 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001334
1335 // Set access bits correctly on the directly-declared conversions.
1336 for (UnresolvedSetIterator I = data().Conversions.begin(),
1337 E = data().Conversions.end();
1338 I != E; ++I)
1339 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001340}
1341
1342bool CXXRecordDecl::mayBeAbstract() const {
1343 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1344 isDependentContext())
1345 return false;
1346
1347 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1348 BEnd = bases_end();
1349 B != BEnd; ++B) {
1350 CXXRecordDecl *BaseDecl
1351 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1352 if (BaseDecl->isAbstract())
1353 return true;
1354 }
1355
1356 return false;
1357}
1358
David Blaikie99ba9e32011-12-20 02:48:34 +00001359void CXXMethodDecl::anchor() { }
1360
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001361CXXMethodDecl *
1362CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001363 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001364 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001365 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001366 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001367 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001368 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001369 isStatic, SCAsWritten, isInline, isConstexpr,
1370 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001371}
1372
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001373CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1374 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1375 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1376 DeclarationNameInfo(), QualType(),
1377 0, false, SC_None, false, false,
1378 SourceLocation());
1379}
1380
Douglas Gregor90916562009-09-29 18:16:17 +00001381bool CXXMethodDecl::isUsualDeallocationFunction() const {
1382 if (getOverloadedOperator() != OO_Delete &&
1383 getOverloadedOperator() != OO_Array_Delete)
1384 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001385
1386 // C++ [basic.stc.dynamic.deallocation]p2:
1387 // A template instance is never a usual deallocation function,
1388 // regardless of its signature.
1389 if (getPrimaryTemplate())
1390 return false;
1391
Douglas Gregor90916562009-09-29 18:16:17 +00001392 // C++ [basic.stc.dynamic.deallocation]p2:
1393 // If a class T has a member deallocation function named operator delete
1394 // with exactly one parameter, then that function is a usual (non-placement)
1395 // deallocation function. [...]
1396 if (getNumParams() == 1)
1397 return true;
1398
1399 // C++ [basic.stc.dynamic.deallocation]p2:
1400 // [...] If class T does not declare such an operator delete but does
1401 // declare a member deallocation function named operator delete with
1402 // exactly two parameters, the second of which has type std::size_t (18.1),
1403 // then this function is a usual deallocation function.
1404 ASTContext &Context = getASTContext();
1405 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001406 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1407 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001408 return false;
1409
1410 // This function is a usual deallocation function if there are no
1411 // single-parameter deallocation functions of the same kind.
1412 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1413 R.first != R.second; ++R.first) {
1414 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1415 if (FD->getNumParams() == 1)
1416 return false;
1417 }
1418
1419 return true;
1420}
1421
Douglas Gregor06a9f362010-05-01 20:49:11 +00001422bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001423 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001424 // A user-declared copy assignment operator X::operator= is a non-static
1425 // non-template member function of class X with exactly one parameter of
1426 // type X, X&, const X&, volatile X& or const volatile X&.
1427 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1428 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001429 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001430 return false;
1431
1432 QualType ParamType = getParamDecl(0)->getType();
1433 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1434 ParamType = Ref->getPointeeType();
1435
1436 ASTContext &Context = getASTContext();
1437 QualType ClassType
1438 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1439 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1440}
1441
Sean Huntffe37fd2011-05-25 20:50:04 +00001442bool CXXMethodDecl::isMoveAssignmentOperator() const {
1443 // C++0x [class.copy]p19:
1444 // A user-declared move assignment operator X::operator= is a non-static
1445 // non-template member function of class X with exactly one parameter of type
1446 // X&&, const X&&, volatile X&&, or const volatile X&&.
1447 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1448 getPrimaryTemplate() || getDescribedFunctionTemplate())
1449 return false;
1450
1451 QualType ParamType = getParamDecl(0)->getType();
1452 if (!isa<RValueReferenceType>(ParamType))
1453 return false;
1454 ParamType = ParamType->getPointeeType();
1455
1456 ASTContext &Context = getASTContext();
1457 QualType ClassType
1458 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1459 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1460}
1461
Anders Carlsson05eb2442009-05-16 23:58:37 +00001462void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001463 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001464 assert(!MD->getParent()->isDependentContext() &&
1465 "Can't add an overridden method to a class template!");
1466
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001467 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001468}
1469
1470CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001471 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001472}
1473
1474CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001475 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001476}
1477
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001478unsigned CXXMethodDecl::size_overridden_methods() const {
1479 return getASTContext().overridden_methods_size(this);
1480}
1481
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001482QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001483 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1484 // If the member function is declared const, the type of this is const X*,
1485 // if the member function is declared volatile, the type of this is
1486 // volatile X*, and if the member function is declared const volatile,
1487 // the type of this is const volatile X*.
1488
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001489 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001490
John McCall3cb0ebd2010-03-10 03:28:59 +00001491 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001492 ClassTy = C.getQualifiedType(ClassTy,
1493 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001494 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001495}
1496
Eli Friedmand7d7f672009-12-06 20:50:05 +00001497bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001498 // If this function is a template instantiation, look at the template from
1499 // which it was instantiated.
1500 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1501 if (!CheckFn)
1502 CheckFn = this;
1503
Eli Friedmand7d7f672009-12-06 20:50:05 +00001504 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001505 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001506}
1507
Sean Huntcbb67482011-01-08 20:30:50 +00001508CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1509 TypeSourceInfo *TInfo, bool IsVirtual,
1510 SourceLocation L, Expr *Init,
1511 SourceLocation R,
1512 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001513 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001514 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1515 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001516{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001517}
1518
Sean Huntcbb67482011-01-08 20:30:50 +00001519CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1520 FieldDecl *Member,
1521 SourceLocation MemberLoc,
1522 SourceLocation L, Expr *Init,
1523 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001524 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001525 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001526 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1527{
1528}
1529
Sean Huntcbb67482011-01-08 20:30:50 +00001530CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1531 IndirectFieldDecl *Member,
1532 SourceLocation MemberLoc,
1533 SourceLocation L, Expr *Init,
1534 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001535 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001536 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001537 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001538{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001539}
1540
Sean Huntcbb67482011-01-08 20:30:50 +00001541CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001542 TypeSourceInfo *TInfo,
1543 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001544 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001545 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1546 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001547 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1548{
1549}
1550
1551CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001552 FieldDecl *Member,
1553 SourceLocation MemberLoc,
1554 SourceLocation L, Expr *Init,
1555 SourceLocation R,
1556 VarDecl **Indices,
1557 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001558 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001559 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001560 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001561{
1562 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1563 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1564}
1565
Sean Huntcbb67482011-01-08 20:30:50 +00001566CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1567 FieldDecl *Member,
1568 SourceLocation MemberLoc,
1569 SourceLocation L, Expr *Init,
1570 SourceLocation R,
1571 VarDecl **Indices,
1572 unsigned NumIndices) {
1573 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001574 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001575 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001576 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1577 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001578}
1579
Sean Huntcbb67482011-01-08 20:30:50 +00001580TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001581 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001582 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001583 else
1584 return TypeLoc();
1585}
1586
Sean Huntcbb67482011-01-08 20:30:50 +00001587const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001588 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001589 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001590 else
1591 return 0;
1592}
1593
Sean Huntcbb67482011-01-08 20:30:50 +00001594SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001595 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001596 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001597
1598 if (isInClassMemberInitializer())
1599 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001600
Douglas Gregor76852c22011-11-01 01:16:03 +00001601 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1602 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1603
1604 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001605}
1606
Sean Huntcbb67482011-01-08 20:30:50 +00001607SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001608 if (isInClassMemberInitializer()) {
1609 FieldDecl *D = getAnyMember();
1610 if (Expr *I = D->getInClassInitializer())
1611 return I->getSourceRange();
1612 return SourceRange();
1613 }
1614
Douglas Gregor802ab452009-12-02 22:36:29 +00001615 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001616}
1617
David Blaikie99ba9e32011-12-20 02:48:34 +00001618void CXXConstructorDecl::anchor() { }
1619
Douglas Gregorb48fe382008-10-31 09:07:45 +00001620CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001621CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1622 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1623 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1624 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001625}
1626
1627CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001628CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001629 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001630 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001631 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001632 bool isExplicit, bool isInline,
1633 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001634 assert(NameInfo.getName().getNameKind()
1635 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001636 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001637 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001638 isExplicit, isInline, isImplicitlyDeclared,
1639 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001640}
1641
Douglas Gregor76852c22011-11-01 01:16:03 +00001642CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1643 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1644 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1645 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1646 return Construct->getConstructor();
1647
1648 return 0;
1649}
1650
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001651bool CXXConstructorDecl::isDefaultConstructor() const {
1652 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001653 // A default constructor for a class X is a constructor of class
1654 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001655 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001656 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001657}
1658
Mike Stump1eb44332009-09-09 15:08:12 +00001659bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001660CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001661 return isCopyOrMoveConstructor(TypeQuals) &&
1662 getParamDecl(0)->getType()->isLValueReferenceType();
1663}
1664
1665bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1666 return isCopyOrMoveConstructor(TypeQuals) &&
1667 getParamDecl(0)->getType()->isRValueReferenceType();
1668}
1669
1670/// \brief Determine whether this is a copy or move constructor.
1671bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001672 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001673 // A non-template constructor for class X is a copy constructor
1674 // if its first parameter is of type X&, const X&, volatile X& or
1675 // const volatile X&, and either there are no other parameters
1676 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001677 // C++0x [class.copy]p3:
1678 // A non-template constructor for class X is a move constructor if its
1679 // first parameter is of type X&&, const X&&, volatile X&&, or
1680 // const volatile X&&, and either there are no other parameters or else
1681 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001682 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001683 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001684 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001685 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001686 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001687
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001688 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001689
1690 // Do we have a reference type?
1691 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001692 if (!ParamRefType)
1693 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001694
Douglas Gregorfd476482009-11-13 23:59:09 +00001695 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001696 ASTContext &Context = getASTContext();
1697
Douglas Gregorfd476482009-11-13 23:59:09 +00001698 CanQualType PointeeType
1699 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001700 CanQualType ClassTy
1701 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001702 if (PointeeType.getUnqualifiedType() != ClassTy)
1703 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001704
John McCall0953e762009-09-24 19:53:00 +00001705 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001706
1707 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001708 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001709 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001710}
1711
Anders Carlssonfaccd722009-08-28 16:57:08 +00001712bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001713 // C++ [class.conv.ctor]p1:
1714 // A constructor declared without the function-specifier explicit
1715 // that can be called with a single parameter specifies a
1716 // conversion from the type of its first parameter to the type of
1717 // its class. Such a constructor is called a converting
1718 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001719 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001720 return false;
1721
Mike Stump1eb44332009-09-09 15:08:12 +00001722 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001723 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001724 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001725 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001726}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001727
Douglas Gregor6493cc52010-11-08 17:16:59 +00001728bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001729 if ((getNumParams() < 1) ||
1730 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1731 (getPrimaryTemplate() == 0) ||
1732 (getDescribedFunctionTemplate() != 0))
1733 return false;
1734
1735 const ParmVarDecl *Param = getParamDecl(0);
1736
1737 ASTContext &Context = getASTContext();
1738 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1739
Douglas Gregor66724ea2009-11-14 01:20:54 +00001740 // Is it the same as our our class type?
1741 CanQualType ClassTy
1742 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1743 if (ParamType.getUnqualifiedType() != ClassTy)
1744 return false;
1745
1746 return true;
1747}
1748
Sebastian Redlf677ea32011-02-05 19:23:19 +00001749const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1750 // Hack: we store the inherited constructor in the overridden method table
1751 method_iterator It = begin_overridden_methods();
1752 if (It == end_overridden_methods())
1753 return 0;
1754
1755 return cast<CXXConstructorDecl>(*It);
1756}
1757
1758void
1759CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1760 // Hack: we store the inherited constructor in the overridden method table
1761 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1762 addOverriddenMethod(BaseCtor);
1763}
1764
David Blaikie99ba9e32011-12-20 02:48:34 +00001765void CXXDestructorDecl::anchor() { }
1766
Douglas Gregor42a552f2008-11-05 20:51:48 +00001767CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001768CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1769 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1770 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001771 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001772}
1773
1774CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001775CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001776 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001777 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001778 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001779 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001780 assert(NameInfo.getName().getNameKind()
1781 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001782 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001783 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001784 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001785}
1786
David Blaikie99ba9e32011-12-20 02:48:34 +00001787void CXXConversionDecl::anchor() { }
1788
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001789CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001790CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1791 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1792 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1793 QualType(), 0, false, false, false,
1794 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001795}
1796
1797CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001798CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001799 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001800 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001801 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001802 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001803 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001804 assert(NameInfo.getName().getNameKind()
1805 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001806 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001807 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001808 isInline, isExplicit, isConstexpr,
1809 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001810}
1811
David Blaikie99ba9e32011-12-20 02:48:34 +00001812void LinkageSpecDecl::anchor() { }
1813
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001814LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001815 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001816 SourceLocation ExternLoc,
1817 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001818 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001819 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001820 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001821}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001822
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001823LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1824 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1825 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1826 lang_c, SourceLocation());
1827}
1828
David Blaikie99ba9e32011-12-20 02:48:34 +00001829void UsingDirectiveDecl::anchor() { }
1830
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001831UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1832 SourceLocation L,
1833 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001834 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001835 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001836 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001837 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001838 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1839 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001840 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1841 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001842}
1843
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001844UsingDirectiveDecl *
1845UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1846 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1847 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1848 NestedNameSpecifierLoc(),
1849 SourceLocation(), 0, 0);
1850}
1851
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001852NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1853 if (NamespaceAliasDecl *NA =
1854 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1855 return NA->getNamespace();
1856 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1857}
1858
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001859void NamespaceDecl::anchor() { }
1860
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001861NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1862 SourceLocation StartLoc,
1863 SourceLocation IdLoc, IdentifierInfo *Id,
1864 NamespaceDecl *PrevDecl)
1865 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1866 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1867{
1868 setPreviousDeclaration(PrevDecl);
1869
1870 if (PrevDecl)
1871 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1872}
1873
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001874NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001875 bool Inline, SourceLocation StartLoc,
1876 SourceLocation IdLoc, IdentifierInfo *Id,
1877 NamespaceDecl *PrevDecl) {
1878 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001879}
1880
1881NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1882 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001883 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1884 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001885}
1886
David Blaikie99ba9e32011-12-20 02:48:34 +00001887void NamespaceAliasDecl::anchor() { }
1888
Mike Stump1eb44332009-09-09 15:08:12 +00001889NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001890 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001891 SourceLocation AliasLoc,
1892 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001893 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001894 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001895 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001896 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1897 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001898 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1899 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001900}
1901
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001902NamespaceAliasDecl *
1903NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1904 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1905 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1906 NestedNameSpecifierLoc(),
1907 SourceLocation(), 0);
1908}
1909
David Blaikie99ba9e32011-12-20 02:48:34 +00001910void UsingShadowDecl::anchor() { }
1911
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001912UsingShadowDecl *
1913UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1914 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1915 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1916}
1917
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001918UsingDecl *UsingShadowDecl::getUsingDecl() const {
1919 const UsingShadowDecl *Shadow = this;
1920 while (const UsingShadowDecl *NextShadow =
1921 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1922 Shadow = NextShadow;
1923 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1924}
1925
David Blaikie99ba9e32011-12-20 02:48:34 +00001926void UsingDecl::anchor() { }
1927
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001928void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1929 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1930 "declaration already in set");
1931 assert(S->getUsingDecl() == this);
1932
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001933 if (FirstUsingShadow.getPointer())
1934 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1935 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001936}
1937
1938void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1939 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1940 "declaration not in set");
1941 assert(S->getUsingDecl() == this);
1942
1943 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1944
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001945 if (FirstUsingShadow.getPointer() == S) {
1946 FirstUsingShadow.setPointer(
1947 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001948 S->UsingOrNextShadow = this;
1949 return;
1950 }
1951
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001952 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001953 while (Prev->UsingOrNextShadow != S)
1954 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1955 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1956 S->UsingOrNextShadow = this;
1957}
1958
Douglas Gregordc355712011-02-25 00:36:19 +00001959UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1960 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001961 const DeclarationNameInfo &NameInfo,
1962 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001963 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001964}
1965
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001966UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1967 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1968 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1969 DeclarationNameInfo(), false);
1970}
1971
David Blaikie99ba9e32011-12-20 02:48:34 +00001972void UnresolvedUsingValueDecl::anchor() { }
1973
John McCall7ba107a2009-11-18 02:36:19 +00001974UnresolvedUsingValueDecl *
1975UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1976 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001977 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001978 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001979 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001980 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001981}
1982
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001983UnresolvedUsingValueDecl *
1984UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1985 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1986 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1987 NestedNameSpecifierLoc(),
1988 DeclarationNameInfo());
1989}
1990
David Blaikie99ba9e32011-12-20 02:48:34 +00001991void UnresolvedUsingTypenameDecl::anchor() { }
1992
John McCall7ba107a2009-11-18 02:36:19 +00001993UnresolvedUsingTypenameDecl *
1994UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1995 SourceLocation UsingLoc,
1996 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001997 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001998 SourceLocation TargetNameLoc,
1999 DeclarationName TargetName) {
2000 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002001 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002002 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00002003}
2004
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002005UnresolvedUsingTypenameDecl *
2006UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2007 void *Mem = AllocateDeserializedDecl(C, ID,
2008 sizeof(UnresolvedUsingTypenameDecl));
2009 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2010 SourceLocation(),
2011 NestedNameSpecifierLoc(),
2012 SourceLocation(),
2013 0);
2014}
2015
David Blaikie99ba9e32011-12-20 02:48:34 +00002016void StaticAssertDecl::anchor() { }
2017
Anders Carlssonfb311762009-03-14 00:25:26 +00002018StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002019 SourceLocation StaticAssertLoc,
2020 Expr *AssertExpr,
2021 StringLiteral *Message,
2022 SourceLocation RParenLoc) {
2023 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2024 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00002025}
2026
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002027StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2028 unsigned ID) {
2029 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
2030 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,SourceLocation());
2031}
2032
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002033static const char *getAccessName(AccessSpecifier AS) {
2034 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002035 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002036 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002037 case AS_public:
2038 return "public";
2039 case AS_private:
2040 return "private";
2041 case AS_protected:
2042 return "protected";
2043 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002044 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002045}
2046
2047const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2048 AccessSpecifier AS) {
2049 return DB << getAccessName(AS);
2050}
Richard Smithf15fda02012-02-02 01:16:57 +00002051
2052const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2053 AccessSpecifier AS) {
2054 return DB << getAccessName(AS);
2055}