blob: 0c78abb8135a96a8c08cb206a331a73eb796fde2 [file] [log] [blame]
Ted Kremenek21475702008-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 Gregor8ea8fd42009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek21475702008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000017#include "clang/AST/ASTMutationListener.h"
Douglas Gregor8fb95122010-09-29 00:15:42 +000018#include "clang/AST/CXXInheritance.h"
Anders Carlsson5bbe1d72009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregord73f3dd2011-11-01 01:16:03 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +000021#include "clang/AST/TypeLoc.h"
Douglas Gregorb6acda02008-11-12 23:21:09 +000022#include "clang/Basic/IdentifierTable.h"
Douglas Gregor74a34442008-12-23 21:31:30 +000023#include "llvm/ADT/STLExtras.h"
Fariborz Jahaniana9540492009-09-12 19:52:10 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek21475702008-09-05 17:16:31 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Decl Allocation/Deallocation Method Implementations
29//===----------------------------------------------------------------------===//
Douglas Gregor5101c242008-12-05 18:15:24 +000030
David Blaikie68e081d2011-12-20 02:48:34 +000031void AccessSpecDecl::anchor() { }
32
Douglas Gregor72172e92012-01-05 21:55:30 +000033AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
34 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(AccessSpecDecl));
35 return new (Mem) AccessSpecDecl(EmptyShell());
36}
37
John McCall67da35c2010-02-04 22:26:26 +000038CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
39 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Alexis Huntfcaeae42011-05-25 20:50:04 +000040 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
41 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedmanc80ca682009-08-15 22:23:00 +000042 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruth583edf82011-04-30 10:07:30 +000043 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carruthb1963742011-04-30 09:17:45 +000044 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidisb2e31e42012-01-26 18:28:08 +000045 HasMutableFields(false), HasOnlyCMembers(true),
Richard Smithe2648ba2012-05-07 01:07:30 +000046 HasInClassInitializer(false),
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +000047 HasTrivialDefaultConstructor(true),
Richard Smithcc36f692011-12-22 02:22:31 +000048 HasConstexprNonCopyMoveConstructor(false),
49 DefaultedDefaultConstructorIsConstexpr(true),
50 DefaultedCopyConstructorIsConstexpr(true),
51 DefaultedMoveConstructorIsConstexpr(true),
52 HasConstexprDefaultConstructor(false), HasConstexprCopyConstructor(false),
53 HasConstexprMoveConstructor(false), HasTrivialCopyConstructor(true),
Alexis Huntf479f1b2011-05-09 18:22:59 +000054 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
55 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
Richard Smith561fb152012-02-25 07:33:38 +000056 HasIrrelevantDestructor(true),
Alexis Huntf479f1b2011-05-09 18:22:59 +000057 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Alexis Huntea6f0322011-05-11 22:34:38 +000058 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Alexis Huntfcaeae42011-05-25 20:50:04 +000059 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
60 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sebastian Redl22653ba2011-08-30 19:58:05 +000061 DeclaredDestructor(false), FailedImplicitMoveConstructor(false),
Eli Friedman73a04092012-01-07 04:59:52 +000062 FailedImplicitMoveAssignment(false), IsLambda(false), NumBases(0),
63 NumVBases(0), Bases(), VBases(), Definition(D), FirstFriend(0) {
John McCall67da35c2010-02-04 22:26:26 +000064}
65
66CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000067 SourceLocation StartLoc, SourceLocation IdLoc,
68 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
69 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall67da35c2010-02-04 22:26:26 +000070 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +000071 TemplateOrInstantiation() { }
Douglas Gregorb6acda02008-11-12 23:21:09 +000072
Jay Foad39c79802011-01-12 09:06:06 +000073CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000074 DeclContext *DC, SourceLocation StartLoc,
75 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000076 CXXRecordDecl* PrevDecl,
77 bool DelayTypeCreation) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +000078 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
79 Id, PrevDecl);
Mike Stump11289f42009-09-09 15:08:12 +000080
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +000081 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000082 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +000083 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +000084 return R;
85}
86
Douglas Gregorc8a73492012-02-13 15:44:47 +000087CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Douglas Gregor680e9e02012-02-21 19:11:17 +000088 SourceLocation Loc, bool Dependent) {
Douglas Gregorc8a73492012-02-13 15:44:47 +000089 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
90 0, 0);
91 R->IsBeingDefined = true;
Douglas Gregor680e9e02012-02-21 19:11:17 +000092 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Dependent);
Douglas Gregorc8a73492012-02-13 15:44:47 +000093 C.getTypeDeclType(R, /*PrevDecl=*/0);
94 return R;
95}
96
Douglas Gregor72172e92012-01-05 21:55:30 +000097CXXRecordDecl *
98CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
99 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
100 return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
101 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000102}
103
Mike Stump11289f42009-09-09 15:08:12 +0000104void
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000105CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000106 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000107 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000108
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000109 if (!data().Bases.isOffset() && data().NumBases > 0)
110 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000111
Richard Smithaed32b42011-10-18 20:08:55 +0000112 if (NumBases) {
113 // C++ [dcl.init.aggr]p1:
114 // An aggregate is [...] a class with [...] no base classes [...].
115 data().Aggregate = false;
116
117 // C++ [class]p4:
118 // A POD-struct is an aggregate class...
119 data().PlainOldData = false;
120 }
121
Anders Carlssonabb20e62010-03-29 05:13:12 +0000122 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000123 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000124
125 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000126 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000127
John McCall67da35c2010-02-04 22:26:26 +0000128 data().Bases = new(C) CXXBaseSpecifier [NumBases];
129 data().NumBases = NumBases;
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000130 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000131 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000132 // Keep track of inherited vbases for this base class.
133 const CXXBaseSpecifier *Base = Bases[i];
134 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000135 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000136 if (BaseType->isDependentType())
137 continue;
138 CXXRecordDecl *BaseClassDecl
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000139 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000140
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000141 // A class with a non-empty base class is not empty.
142 // FIXME: Standard ref?
Chandler Carruthb1963742011-04-30 09:17:45 +0000143 if (!BaseClassDecl->isEmpty()) {
144 if (!data().Empty) {
145 // C++0x [class]p7:
146 // A standard-layout class is a class that:
147 // [...]
148 // -- either has no non-static data members in the most derived
149 // class and at most one base class with non-static data members,
150 // or has no base classes with non-static data members, and
151 // If this is the second non-empty base, then neither of these two
152 // clauses can be true.
Chandler Carruth583edf82011-04-30 10:07:30 +0000153 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000154 }
155
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000156 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000157 data().HasNoNonEmptyBases = false;
158 }
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000159
Douglas Gregor11c024b2010-09-28 20:50:54 +0000160 // C++ [class.virtual]p1:
161 // A class that declares or inherits a virtual function is called a
162 // polymorphic class.
163 if (BaseClassDecl->isPolymorphic())
164 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000165
Chandler Carruthb1963742011-04-30 09:17:45 +0000166 // C++0x [class]p7:
167 // A standard-layout class is a class that: [...]
168 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000169 if (!BaseClassDecl->isStandardLayout())
170 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000171
Chandler Carruthe71d0622011-04-24 02:49:34 +0000172 // Record if this base is the first non-literal field or base.
173 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
174 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000175
Anders Carlssonabb20e62010-03-29 05:13:12 +0000176 // Now go through all virtual bases of this base and add them.
Mike Stump11289f42009-09-09 15:08:12 +0000177 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000178 BaseClassDecl->vbases_begin(),
179 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000180 // Add this base if it's not already in the list.
Anders Carlssone47380f2010-03-29 19:49:09 +0000181 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlssonabb20e62010-03-29 05:13:12 +0000182 VBases.push_back(VBase);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000183 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000184
185 if (Base->isVirtual()) {
186 // Add this base if it's not already in the list.
Anders Carlssone47380f2010-03-29 19:49:09 +0000187 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlssonabb20e62010-03-29 05:13:12 +0000188 VBases.push_back(Base);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000189
190 // C++0x [meta.unary.prop] is_empty:
191 // T is a class type, but not a union type, with ... no virtual base
192 // classes
193 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000194
195 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000196 // A default constructor is trivial [...] if:
197 // -- its class has [...] no virtual bases
198 data().HasTrivialDefaultConstructor = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000199
200 // C++0x [class.copy]p13:
201 // A copy/move constructor for class X is trivial if it is neither
202 // user-provided nor deleted and if
203 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000204 data().HasTrivialCopyConstructor = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000205 data().HasTrivialMoveConstructor = false;
206
207 // C++0x [class.copy]p27:
208 // A copy/move assignment operator for class X is trivial if it is
209 // neither user-provided nor deleted and if
210 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000211 data().HasTrivialCopyAssignment = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000212 data().HasTrivialMoveAssignment = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000213
214 // C++0x [class]p7:
215 // A standard-layout class is a class that: [...]
216 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000217 data().IsStandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000218
219 // C++11 [dcl.constexpr]p4:
220 // In the definition of a constexpr constructor [...]
221 // -- the class shall not have any virtual base classes
222 data().DefaultedDefaultConstructorIsConstexpr = false;
223 data().DefaultedCopyConstructorIsConstexpr = false;
224 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000225 } else {
226 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000227 // A default constructor is trivial [...] if:
228 // -- all the direct base classes of its class have trivial default
229 // constructors.
230 if (!BaseClassDecl->hasTrivialDefaultConstructor())
231 data().HasTrivialDefaultConstructor = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000232
Chandler Carruthad7d4042011-04-23 23:10:33 +0000233 // C++0x [class.copy]p13:
234 // A copy/move constructor for class X is trivial if [...]
235 // [...]
236 // -- the constructor selected to copy/move each direct base class
237 // subobject is trivial, and
238 // FIXME: C++0x: We need to only consider the selected constructor
239 // instead of all of them.
Douglas Gregor11c024b2010-09-28 20:50:54 +0000240 if (!BaseClassDecl->hasTrivialCopyConstructor())
241 data().HasTrivialCopyConstructor = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000242 if (!BaseClassDecl->hasTrivialMoveConstructor())
243 data().HasTrivialMoveConstructor = false;
244
245 // C++0x [class.copy]p27:
246 // A copy/move assignment operator for class X is trivial if [...]
247 // [...]
248 // -- the assignment operator selected to copy/move each direct base
249 // class subobject is trivial, and
250 // FIXME: C++0x: We need to only consider the selected operator instead
251 // of all of them.
Douglas Gregor11c024b2010-09-28 20:50:54 +0000252 if (!BaseClassDecl->hasTrivialCopyAssignment())
253 data().HasTrivialCopyAssignment = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000254 if (!BaseClassDecl->hasTrivialMoveAssignment())
255 data().HasTrivialMoveAssignment = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000256
257 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000258 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000259 // requirements of a constexpr constructor, the implicitly-defined
260 // default constructor is constexpr.
261 if (!BaseClassDecl->hasConstexprDefaultConstructor())
262 data().DefaultedDefaultConstructorIsConstexpr = false;
263
264 // C++11 [class.copy]p13:
265 // If the implicitly-defined constructor would satisfy the requirements
266 // of a constexpr constructor, the implicitly-defined constructor is
267 // constexpr.
268 // C++11 [dcl.constexpr]p4:
269 // -- every constructor involved in initializing [...] base class
270 // sub-objects shall be a constexpr constructor
271 if (!BaseClassDecl->hasConstexprCopyConstructor())
272 data().DefaultedCopyConstructorIsConstexpr = false;
273 if (BaseClassDecl->hasDeclaredMoveConstructor() ||
274 BaseClassDecl->needsImplicitMoveConstructor())
275 // FIXME: If the implicit move constructor generated for the base class
276 // would be ill-formed, the implicit move constructor generated for the
277 // derived class calls the base class' copy constructor.
278 data().DefaultedMoveConstructorIsConstexpr &=
Richard Smithc101e612012-01-11 18:26:05 +0000279 BaseClassDecl->hasConstexprMoveConstructor();
Richard Smithcc36f692011-12-22 02:22:31 +0000280 else if (!BaseClassDecl->hasConstexprCopyConstructor())
281 data().DefaultedMoveConstructorIsConstexpr = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000282 }
Douglas Gregor11c024b2010-09-28 20:50:54 +0000283
284 // C++ [class.ctor]p3:
285 // A destructor is trivial if all the direct base classes of its class
286 // have trivial destructors.
287 if (!BaseClassDecl->hasTrivialDestructor())
288 data().HasTrivialDestructor = false;
Richard Smith561fb152012-02-25 07:33:38 +0000289
290 if (!BaseClassDecl->hasIrrelevantDestructor())
291 data().HasIrrelevantDestructor = false;
292
John McCall31168b02011-06-15 23:02:42 +0000293 // A class has an Objective-C object member if... or any of its bases
294 // has an Objective-C object member.
295 if (BaseClassDecl->hasObjectMember())
296 setHasObjectMember(true);
297
Douglas Gregor61226d32011-05-13 01:05:07 +0000298 // Keep track of the presence of mutable fields.
299 if (BaseClassDecl->hasMutableFields())
300 data().HasMutableFields = true;
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000301 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000302
303 if (VBases.empty())
304 return;
305
306 // Create base specifier for any direct or indirect virtual bases.
307 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
308 data().NumVBases = VBases.size();
Richard Smith26935e62011-07-12 23:49:11 +0000309 for (int I = 0, E = VBases.size(); I != E; ++I)
310 data().getVBases()[I] = *VBases[I];
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000311}
312
Douglas Gregord2e6a452010-01-14 17:47:39 +0000313/// Callback function for CXXRecordDecl::forallBases that acknowledges
314/// that it saw a base class.
315static bool SawBase(const CXXRecordDecl *, void *) {
316 return true;
317}
318
319bool CXXRecordDecl::hasAnyDependentBases() const {
320 if (!isDependentContext())
321 return false;
322
323 return !forallBases(SawBase, 0);
324}
325
Alexis Huntfcaeae42011-05-25 20:50:04 +0000326bool CXXRecordDecl::hasConstCopyConstructor() const {
327 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000328}
329
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000330bool CXXRecordDecl::isTriviallyCopyable() const {
331 // C++0x [class]p5:
332 // A trivially copyable class is a class that:
333 // -- has no non-trivial copy constructors,
334 if (!hasTrivialCopyConstructor()) return false;
335 // -- has no non-trivial move constructors,
Chandler Carruthad7d4042011-04-23 23:10:33 +0000336 if (!hasTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000337 // -- has no non-trivial copy assignment operators,
338 if (!hasTrivialCopyAssignment()) return false;
339 // -- has no non-trivial move assignment operators, and
Chandler Carruthad7d4042011-04-23 23:10:33 +0000340 if (!hasTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000341 // -- has a trivial destructor.
342 if (!hasTrivialDestructor()) return false;
343
344 return true;
345}
346
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000347/// \brief Perform a simplistic form of overload resolution that only considers
348/// cv-qualifiers on a single parameter, and return the best overload candidate
349/// (if there is one).
350static CXXMethodDecl *
351GetBestOverloadCandidateSimple(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000352 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000353 if (Cands.empty())
354 return 0;
355 if (Cands.size() == 1)
356 return Cands[0].first;
357
358 unsigned Best = 0, N = Cands.size();
359 for (unsigned I = 1; I != N; ++I)
Douglas Gregor1d684c22011-04-28 00:56:09 +0000360 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000361 Best = I;
362
363 for (unsigned I = 1; I != N; ++I)
Douglas Gregor1d684c22011-04-28 00:56:09 +0000364 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000365 return 0;
366
367 return Cands[Best].first;
368}
369
Alexis Huntfcaeae42011-05-25 20:50:04 +0000370CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
371 ASTContext &Context = getASTContext();
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000372 QualType ClassType
373 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump11289f42009-09-09 15:08:12 +0000374 DeclarationName ConstructorName
Douglas Gregor1349b452008-12-15 21:24:18 +0000375 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000376 Context.getCanonicalType(ClassType));
377 unsigned FoundTQs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000378 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregor74a34442008-12-23 21:31:30 +0000379 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000380 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregor74a34442008-12-23 21:31:30 +0000381 Con != ConEnd; ++Con) {
Douglas Gregor2d2282e2009-09-04 14:46:39 +0000382 // C++ [class.copy]p2:
383 // A non-template constructor for class X is a copy constructor if [...]
384 if (isa<FunctionTemplateDecl>(*Con))
385 continue;
386
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000387 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
388 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall8ccfcb52009-09-24 19:53:00 +0000389 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
390 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000391 Found.push_back(std::make_pair(
392 const_cast<CXXConstructorDecl *>(Constructor),
393 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000394 }
Douglas Gregor05379422008-11-03 17:51:48 +0000395 }
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000396
397 return cast_or_null<CXXConstructorDecl>(
398 GetBestOverloadCandidateSimple(Found));
Douglas Gregor05379422008-11-03 17:51:48 +0000399}
400
Alexis Huntfcaeae42011-05-25 20:50:04 +0000401CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
402 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
403 if (I->isMoveConstructor())
David Blaikie2d7c57e2012-04-30 02:36:29 +0000404 return &*I;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000405
406 return 0;
407}
408
Douglas Gregor68e11362010-07-01 17:48:08 +0000409CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
410 ASTContext &Context = getASTContext();
411 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
412 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
413
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000414 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregor68e11362010-07-01 17:48:08 +0000415 DeclContext::lookup_const_iterator Op, OpEnd;
416 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
417 // C++ [class.copy]p9:
418 // A user-declared copy assignment operator is a non-static non-template
419 // member function of class X with exactly one parameter of type X, X&,
420 // const X&, volatile X& or const volatile X&.
421 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
422 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
423 continue;
424
425 const FunctionProtoType *FnType
426 = Method->getType()->getAs<FunctionProtoType>();
427 assert(FnType && "Overloaded operator has no prototype.");
428 // Don't assert on this; an invalid decl might have been left in the AST.
429 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
430 continue;
431
432 QualType ArgType = FnType->getArgType(0);
433 Qualifiers Quals;
434 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
435 ArgType = Ref->getPointeeType();
436 // If we have a const argument and we have a reference to a non-const,
437 // this function does not match.
438 if (ArgIsConst && !ArgType.isConstQualified())
439 continue;
440
441 Quals = ArgType.getQualifiers();
442 } else {
443 // By-value copy-assignment operators are treated like const X&
444 // copy-assignment operators.
445 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
446 }
447
448 if (!Context.hasSameUnqualifiedType(ArgType, Class))
449 continue;
450
451 // Save this copy-assignment operator. It might be "the one".
452 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
453 }
454
455 // Use a simplistic form of overload resolution to find the candidate.
456 return GetBestOverloadCandidateSimple(Found);
457}
458
Alexis Huntfcaeae42011-05-25 20:50:04 +0000459CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
460 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
461 if (I->isMoveAssignmentOperator())
David Blaikie2d7c57e2012-04-30 02:36:29 +0000462 return &*I;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000463
464 return 0;
465}
466
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000467void CXXRecordDecl::markedVirtualFunctionPure() {
468 // C++ [class.abstract]p2:
469 // A class is abstract if it has at least one pure virtual function.
470 data().Abstract = true;
471}
472
473void CXXRecordDecl::addedMember(Decl *D) {
Argyrios Kyrtzidisb2e31e42012-01-26 18:28:08 +0000474 if (!D->isImplicit() &&
475 !isa<FieldDecl>(D) &&
476 !isa<IndirectFieldDecl>(D) &&
477 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class))
478 data().HasOnlyCMembers = false;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000479
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000480 // Ignore friends and invalid declarations.
481 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000482 return;
483
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000484 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
485 if (FunTmpl)
486 D = FunTmpl->getTemplatedDecl();
487
Douglas Gregora832d3e2010-09-28 19:45:33 +0000488 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
489 if (Method->isVirtual()) {
490 // C++ [dcl.init.aggr]p1:
491 // An aggregate is an array or a class with [...] no virtual functions.
492 data().Aggregate = false;
493
494 // C++ [class]p4:
495 // A POD-struct is an aggregate class...
496 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000497
498 // Virtual functions make the class non-empty.
499 // FIXME: Standard ref?
500 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000501
502 // C++ [class.virtual]p1:
503 // A class that declares or inherits a virtual function is called a
504 // polymorphic class.
505 data().Polymorphic = true;
506
Alexis Huntf479f1b2011-05-09 18:22:59 +0000507 // C++0x [class.ctor]p5
508 // A default constructor is trivial [...] if:
509 // -- its class has no virtual functions [...]
510 data().HasTrivialDefaultConstructor = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000511
512 // C++0x [class.copy]p13:
513 // A copy/move constructor for class X is trivial if [...]
514 // -- class X has no virtual functions [...]
Douglas Gregor11c024b2010-09-28 20:50:54 +0000515 data().HasTrivialCopyConstructor = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000516 data().HasTrivialMoveConstructor = false;
517
518 // C++0x [class.copy]p27:
519 // A copy/move assignment operator for class X is trivial if [...]
520 // -- class X has no virtual functions [...]
Douglas Gregor11c024b2010-09-28 20:50:54 +0000521 data().HasTrivialCopyAssignment = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000522 data().HasTrivialMoveAssignment = false;
Douglas Gregor5d1b4e32011-11-07 20:56:01 +0000523
Chandler Carruthb1963742011-04-30 09:17:45 +0000524 // C++0x [class]p7:
525 // A standard-layout class is a class that: [...]
526 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000527 data().IsStandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000528 }
529 }
530
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000531 if (D->isImplicit()) {
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +0000532 // Notify that an implicit member was added after the definition
533 // was completed.
534 if (!isBeingDefined())
535 if (ASTMutationListener *L = getASTMutationListener())
536 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000537
Alexis Huntfcaeae42011-05-25 20:50:04 +0000538 // If this is a special member function, note that it was added and then
539 // return early.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000540 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smithcc36f692011-12-22 02:22:31 +0000541 if (Constructor->isDefaultConstructor()) {
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000542 data().DeclaredDefaultConstructor = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000543 if (Constructor->isConstexpr()) {
544 data().HasConstexprDefaultConstructor = true;
545 data().HasConstexprNonCopyMoveConstructor = true;
546 }
547 } else if (Constructor->isCopyConstructor()) {
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000548 data().DeclaredCopyConstructor = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000549 if (Constructor->isConstexpr())
550 data().HasConstexprCopyConstructor = true;
551 } else if (Constructor->isMoveConstructor()) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000552 data().DeclaredMoveConstructor = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000553 if (Constructor->isConstexpr())
554 data().HasConstexprMoveConstructor = true;
555 } else
Alexis Huntfcaeae42011-05-25 20:50:04 +0000556 goto NotASpecialMember;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000557 return;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000558 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000559 data().DeclaredDestructor = true;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000560 return;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000561 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
562 if (Method->isCopyAssignmentOperator())
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000563 data().DeclaredCopyAssignment = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000564 else if (Method->isMoveAssignmentOperator())
565 data().DeclaredMoveAssignment = true;
566 else
567 goto NotASpecialMember;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000568 return;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000569 }
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000570
Alexis Huntfcaeae42011-05-25 20:50:04 +0000571NotASpecialMember:;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000572 // Any other implicit declarations are handled like normal declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000573 }
574
575 // Handle (user-declared) constructors.
576 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
577 // Note that we have a user-declared constructor.
578 data().UserDeclaredConstructor = true;
579
Richard Smitha5285072011-09-05 02:13:09 +0000580 // Technically, "user-provided" is only defined for special member
581 // functions, but the intent of the standard is clearly that it should apply
582 // to all functions.
583 bool UserProvided = Constructor->isUserProvided();
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000584
Alexis Hunt88c75c32011-05-09 21:45:35 +0000585 if (Constructor->isDefaultConstructor()) {
586 data().DeclaredDefaultConstructor = true;
Richard Smitha5285072011-09-05 02:13:09 +0000587 if (UserProvided) {
Richard Smithcc36f692011-12-22 02:22:31 +0000588 // C++0x [class.ctor]p5:
589 // A default constructor is trivial if it is not user-provided [...]
Alexis Hunt88c75c32011-05-09 21:45:35 +0000590 data().HasTrivialDefaultConstructor = false;
Alexis Huntea6f0322011-05-11 22:34:38 +0000591 data().UserProvidedDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000592 }
Richard Smithcc36f692011-12-22 02:22:31 +0000593 if (Constructor->isConstexpr()) {
594 data().HasConstexprDefaultConstructor = true;
595 data().HasConstexprNonCopyMoveConstructor = true;
596 }
Alexis Hunt88c75c32011-05-09 21:45:35 +0000597 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000598
Chandler Carruthad7d4042011-04-23 23:10:33 +0000599 // Note when we have a user-declared copy or move constructor, which will
600 // suppress the implicit declaration of those constructors.
601 if (!FunTmpl) {
602 if (Constructor->isCopyConstructor()) {
603 data().UserDeclaredCopyConstructor = true;
604 data().DeclaredCopyConstructor = true;
605
606 // C++0x [class.copy]p13:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000607 // A copy/move constructor for class X is trivial if it is not
608 // user-provided [...]
Richard Smitha5285072011-09-05 02:13:09 +0000609 if (UserProvided)
Alexis Huntf479f1b2011-05-09 18:22:59 +0000610 data().HasTrivialCopyConstructor = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000611
612 if (Constructor->isConstexpr())
613 data().HasConstexprCopyConstructor = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000614 } else if (Constructor->isMoveConstructor()) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000615 data().UserDeclaredMoveConstructor = true;
616 data().DeclaredMoveConstructor = true;
617
Chandler Carruthad7d4042011-04-23 23:10:33 +0000618 // C++0x [class.copy]p13:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000619 // A copy/move constructor for class X is trivial if it is not
620 // user-provided [...]
Richard Smitha5285072011-09-05 02:13:09 +0000621 if (UserProvided)
Alexis Huntf479f1b2011-05-09 18:22:59 +0000622 data().HasTrivialMoveConstructor = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000623
624 if (Constructor->isConstexpr())
625 data().HasConstexprMoveConstructor = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000626 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000627 }
Richard Smith111af8d2011-08-10 18:11:37 +0000628 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) {
629 // Record if we see any constexpr constructors which are neither copy
Chandler Carruthe71d0622011-04-24 02:49:34 +0000630 // nor move constructors.
Richard Smith111af8d2011-08-10 18:11:37 +0000631 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000632 }
Chandler Carruthad7d4042011-04-23 23:10:33 +0000633
Alexis Hunt88c75c32011-05-09 21:45:35 +0000634 // C++ [dcl.init.aggr]p1:
635 // An aggregate is an array or a class with no user-declared
636 // constructors [...].
637 // C++0x [dcl.init.aggr]p1:
638 // An aggregate is an array or a class with no user-provided
639 // constructors [...].
David Blaikiebbafb8a2012-03-11 07:00:24 +0000640 if (!getASTContext().getLangOpts().CPlusPlus0x || UserProvided)
Alexis Hunt88c75c32011-05-09 21:45:35 +0000641 data().Aggregate = false;
642
643 // C++ [class]p4:
644 // A POD-struct is an aggregate class [...]
645 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
646 // type is technically an aggregate in C++0x since it wouldn't be in 03.
647 data().PlainOldData = false;
648
Douglas Gregord30e79f2010-09-27 21:17:54 +0000649 return;
650 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000651
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000652 // Handle (user-declared) destructors.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000653 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000654 data().DeclaredDestructor = true;
655 data().UserDeclaredDestructor = true;
Richard Smith561fb152012-02-25 07:33:38 +0000656 data().HasIrrelevantDestructor = false;
657
Douglas Gregora832d3e2010-09-28 19:45:33 +0000658 // C++ [class]p4:
659 // A POD-struct is an aggregate class that has [...] no user-defined
660 // destructor.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000661 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000662 data().PlainOldData = false;
663
Douglas Gregor5d1b4e32011-11-07 20:56:01 +0000664 // C++11 [class.dtor]p5:
665 // A destructor is trivial if it is not user-provided and if
666 // -- the destructor is not virtual.
Richard Smithcc36f692011-12-22 02:22:31 +0000667 if (DD->isUserProvided() || DD->isVirtual()) {
Alexis Hunt97ab5542011-05-16 22:41:40 +0000668 data().HasTrivialDestructor = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000669 // C++11 [dcl.constexpr]p1:
670 // The constexpr specifier shall be applied only to [...] the
671 // declaration of a static data member of a literal type.
672 // C++11 [basic.types]p10:
673 // A type is a literal type if it is [...] a class type that [...] has
674 // a trivial destructor.
675 data().DefaultedDefaultConstructorIsConstexpr = false;
676 data().DefaultedCopyConstructorIsConstexpr = false;
677 data().DefaultedMoveConstructorIsConstexpr = false;
678 }
Douglas Gregor11c024b2010-09-28 20:50:54 +0000679
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000680 return;
681 }
Douglas Gregord30e79f2010-09-27 21:17:54 +0000682
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000683 // Handle (user-declared) member functions.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000684 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000685 if (Method->isCopyAssignmentOperator()) {
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000686 // C++ [class]p4:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000687 // A POD-struct is an aggregate class that [...] has no user-defined
688 // copy assignment operator [...].
Alexis Hunt97ab5542011-05-16 22:41:40 +0000689 // This is the C++03 bit only.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000690 data().PlainOldData = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000691
Alexis Huntfcaeae42011-05-25 20:50:04 +0000692 // This is a copy assignment operator.
Chandler Carruthad7d4042011-04-23 23:10:33 +0000693
Alexis Huntfcaeae42011-05-25 20:50:04 +0000694 // Suppress the implicit declaration of a copy constructor.
695 data().UserDeclaredCopyAssignment = true;
696 data().DeclaredCopyAssignment = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000697
Alexis Huntfcaeae42011-05-25 20:50:04 +0000698 // C++0x [class.copy]p27:
699 // A copy/move assignment operator for class X is trivial if it is
700 // neither user-provided nor deleted [...]
701 if (Method->isUserProvided())
702 data().HasTrivialCopyAssignment = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000703
Alexis Huntfcaeae42011-05-25 20:50:04 +0000704 return;
705 }
706
707 if (Method->isMoveAssignmentOperator()) {
708 // This is an extension in C++03 mode, but we'll keep consistency by
709 // taking a move assignment operator to induce non-POD-ness
710 data().PlainOldData = false;
711
712 // This is a move assignment operator.
713 data().UserDeclaredMoveAssignment = true;
714 data().DeclaredMoveAssignment = true;
715
716 // C++0x [class.copy]p27:
717 // A copy/move assignment operator for class X is trivial if it is
718 // neither user-provided nor deleted [...]
719 if (Method->isUserProvided())
720 data().HasTrivialMoveAssignment = false;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000721 }
Chandler Carruthad7d4042011-04-23 23:10:33 +0000722
Douglas Gregor457104e2010-09-29 04:25:11 +0000723 // Keep the list of conversion functions up-to-date.
724 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
725 // We don't record specializations.
726 if (Conversion->getPrimaryTemplate())
727 return;
728
729 // FIXME: We intentionally don't use the decl's access here because it
730 // hasn't been set yet. That's really just a misdesign in Sema.
731
732 if (FunTmpl) {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000733 if (FunTmpl->getPreviousDecl())
734 data().Conversions.replace(FunTmpl->getPreviousDecl(),
Douglas Gregor457104e2010-09-29 04:25:11 +0000735 FunTmpl);
736 else
737 data().Conversions.addDecl(FunTmpl);
738 } else {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000739 if (Conversion->getPreviousDecl())
740 data().Conversions.replace(Conversion->getPreviousDecl(),
Douglas Gregor457104e2010-09-29 04:25:11 +0000741 Conversion);
742 else
743 data().Conversions.addDecl(Conversion);
744 }
745 }
746
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000747 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000748 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000749
750 // Handle non-static data members.
751 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregor556e5862011-10-10 17:22:13 +0000752 // C++ [class.bit]p2:
753 // A declaration for a bit-field that omits the identifier declares an
754 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
755 // initialized.
756 if (Field->isUnnamedBitfield())
757 return;
758
Douglas Gregora832d3e2010-09-28 19:45:33 +0000759 // C++ [dcl.init.aggr]p1:
760 // An aggregate is an array or a class (clause 9) with [...] no
761 // private or protected non-static data members (clause 11).
762 //
763 // A POD must be an aggregate.
764 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
765 data().Aggregate = false;
766 data().PlainOldData = false;
767 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000768
769 // C++0x [class]p7:
770 // A standard-layout class is a class that:
771 // [...]
772 // -- has the same access control for all non-static data members,
773 switch (D->getAccess()) {
774 case AS_private: data().HasPrivateFields = true; break;
775 case AS_protected: data().HasProtectedFields = true; break;
776 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000777 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000778 };
779 if ((data().HasPrivateFields + data().HasProtectedFields +
780 data().HasPublicFields) > 1)
Chandler Carruth583edf82011-04-30 10:07:30 +0000781 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000782
Douglas Gregor61226d32011-05-13 01:05:07 +0000783 // Keep track of the presence of mutable fields.
784 if (Field->isMutable())
785 data().HasMutableFields = true;
786
Chandler Carruthad7d4042011-04-23 23:10:33 +0000787 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000788 // A POD struct is a class that is both a trivial class and a
789 // standard-layout class, and has no non-static data members of type
790 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000791 //
792 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
793 // that does not explicitly have no lifetime makes the class a non-POD.
794 // However, we delay setting PlainOldData to false in this case so that
795 // Sema has a chance to diagnostic causes where the same class will be
796 // non-POD with Automatic Reference Counting but a POD without Instant Objects.
797 // In this case, the class will become a non-POD class when we complete
798 // the definition.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000799 ASTContext &Context = getASTContext();
800 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000801 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000802 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCall31168b02011-06-15 23:02:42 +0000803 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
804 setHasObjectMember(true);
805 } else if (!T.isPODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000806 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000807
Chandler Carruthb1963742011-04-30 09:17:45 +0000808 if (T->isReferenceType()) {
Alexis Huntf479f1b2011-05-09 18:22:59 +0000809 data().HasTrivialDefaultConstructor = false;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000810
Chandler Carruthb1963742011-04-30 09:17:45 +0000811 // C++0x [class]p7:
812 // A standard-layout class is a class that:
813 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000814 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000815 }
816
Richard Smith3607ffe2012-02-13 03:54:03 +0000817 // Record if this field is the first non-literal or volatile field or base.
818 if (!T->isLiteralType() || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000819 data().HasNonLiteralTypeFieldsOrBases = true;
820
Richard Smith938f40b2011-06-11 17:19:42 +0000821 if (Field->hasInClassInitializer()) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000822 data().HasInClassInitializer = true;
823
824 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000825 // A default constructor is trivial if [...] no non-static data member
826 // of its class has a brace-or-equal-initializer.
827 data().HasTrivialDefaultConstructor = false;
828
Richard Smithe2648ba2012-05-07 01:07:30 +0000829 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +0000830 // An aggregate is a [...] class with [...] no
831 // brace-or-equal-initializers for non-static data members.
832 data().Aggregate = false;
833
Richard Smithe2648ba2012-05-07 01:07:30 +0000834 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +0000835 // A POD struct is [...] a trivial class.
836 data().PlainOldData = false;
837 }
838
Douglas Gregor11c024b2010-09-28 20:50:54 +0000839 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
840 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
841 if (FieldRec->getDefinition()) {
Alexis Huntf479f1b2011-05-09 18:22:59 +0000842 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +0000843 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000844 // -- for all the non-static data members of its class that are of
845 // class type (or array thereof), each such class has a trivial
846 // default constructor.
847 if (!FieldRec->hasTrivialDefaultConstructor())
848 data().HasTrivialDefaultConstructor = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000849
850 // C++0x [class.copy]p13:
851 // A copy/move constructor for class X is trivial if [...]
852 // [...]
853 // -- for each non-static data member of X that is of class type (or
854 // an array thereof), the constructor selected to copy/move that
855 // member is trivial;
856 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor11c024b2010-09-28 20:50:54 +0000857 if (!FieldRec->hasTrivialCopyConstructor())
858 data().HasTrivialCopyConstructor = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000859 if (!FieldRec->hasTrivialMoveConstructor())
860 data().HasTrivialMoveConstructor = false;
861
862 // C++0x [class.copy]p27:
863 // A copy/move assignment operator for class X is trivial if [...]
864 // [...]
865 // -- for each non-static data member of X that is of class type (or
866 // an array thereof), the assignment operator selected to
867 // copy/move that member is trivial;
868 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor11c024b2010-09-28 20:50:54 +0000869 if (!FieldRec->hasTrivialCopyAssignment())
870 data().HasTrivialCopyAssignment = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000871 if (!FieldRec->hasTrivialMoveAssignment())
872 data().HasTrivialMoveAssignment = false;
873
Douglas Gregor11c024b2010-09-28 20:50:54 +0000874 if (!FieldRec->hasTrivialDestructor())
875 data().HasTrivialDestructor = false;
Richard Smith561fb152012-02-25 07:33:38 +0000876 if (!FieldRec->hasIrrelevantDestructor())
877 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +0000878 if (FieldRec->hasObjectMember())
879 setHasObjectMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +0000880
881 // C++0x [class]p7:
882 // A standard-layout class is a class that:
883 // -- has no non-static data members of type non-standard-layout
884 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +0000885 if (!FieldRec->isStandardLayout())
886 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000887
888 // C++0x [class]p7:
889 // A standard-layout class is a class that:
890 // [...]
891 // -- has no base classes of the same type as the first non-static
892 // data member.
893 // We don't want to expend bits in the state of the record decl
894 // tracking whether this is the first non-static data member so we
895 // cheat a bit and use some of the existing state: the empty bit.
896 // Virtual bases and virtual methods make a class non-empty, but they
897 // also make it non-standard-layout so we needn't check here.
898 // A non-empty base class may leave the class standard-layout, but not
899 // if we have arrived here, and have at least on non-static data
Chandler Carruth583edf82011-04-30 10:07:30 +0000900 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carruthb1963742011-04-30 09:17:45 +0000901 // data member must come through here with Empty still true, and Empty
902 // will subsequently be set to false below.
Chandler Carruth583edf82011-04-30 10:07:30 +0000903 if (data().IsStandardLayout && data().Empty) {
Chandler Carruthb1963742011-04-30 09:17:45 +0000904 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
905 BE = bases_end();
906 BI != BE; ++BI) {
907 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000908 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000909 break;
910 }
911 }
912 }
Douglas Gregor61226d32011-05-13 01:05:07 +0000913
914 // Keep track of the presence of mutable fields.
915 if (FieldRec->hasMutableFields())
916 data().HasMutableFields = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000917
918 // C++11 [class.copy]p13:
919 // If the implicitly-defined constructor would satisfy the
920 // requirements of a constexpr constructor, the implicitly-defined
921 // constructor is constexpr.
922 // C++11 [dcl.constexpr]p4:
923 // -- every constructor involved in initializing non-static data
924 // members [...] shall be a constexpr constructor
925 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +0000926 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000927 // The standard requires any in-class initializer to be a constant
928 // expression. We consider this to be a defect.
929 data().DefaultedDefaultConstructorIsConstexpr = false;
930
931 if (!FieldRec->hasConstexprCopyConstructor())
932 data().DefaultedCopyConstructorIsConstexpr = false;
933
934 if (FieldRec->hasDeclaredMoveConstructor() ||
935 FieldRec->needsImplicitMoveConstructor())
936 // FIXME: If the implicit move constructor generated for the member's
937 // class would be ill-formed, the implicit move constructor generated
938 // for this class calls the member's copy constructor.
939 data().DefaultedMoveConstructorIsConstexpr &=
940 FieldRec->hasConstexprMoveConstructor();
941 else if (!FieldRec->hasConstexprCopyConstructor())
942 data().DefaultedMoveConstructorIsConstexpr = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000943 }
Richard Smithcc36f692011-12-22 02:22:31 +0000944 } else {
945 // Base element type of field is a non-class type.
946 if (!T->isLiteralType()) {
947 data().DefaultedDefaultConstructorIsConstexpr = false;
948 data().DefaultedCopyConstructorIsConstexpr = false;
949 data().DefaultedMoveConstructorIsConstexpr = false;
Richard Smithe2648ba2012-05-07 01:07:30 +0000950 } else if (!Field->hasInClassInitializer() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000951 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000952 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000953
954 // C++0x [class]p7:
955 // A standard-layout class is a class that:
956 // [...]
957 // -- either has no non-static data members in the most derived
958 // class and at most one base class with non-static data members,
959 // or has no base classes with non-static data members, and
960 // At this point we know that we have a non-static data member, so the last
961 // clause holds.
962 if (!data().HasNoNonEmptyBases)
Chandler Carruth583edf82011-04-30 10:07:30 +0000963 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000964
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000965 // If this is not a zero-length bit-field, then the class is not empty.
966 if (data().Empty) {
Richard Smithcaf33902011-10-10 18:28:20 +0000967 if (!Field->isBitField() ||
968 (!Field->getBitWidth()->isTypeDependent() &&
969 !Field->getBitWidth()->isValueDependent() &&
970 Field->getBitWidthValue(Context) != 0))
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000971 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000972 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000973 }
Douglas Gregor457104e2010-09-29 04:25:11 +0000974
975 // Handle using declarations of conversion functions.
976 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
977 if (Shadow->getDeclName().getNameKind()
978 == DeclarationName::CXXConversionFunctionName)
979 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000980}
981
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000982bool CXXRecordDecl::isCLike() const {
983 if (getTagKind() == TTK_Class || !TemplateOrInstantiation.isNull())
984 return false;
985 if (!hasDefinition())
986 return true;
987
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +0000988 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000989}
990
Douglas Gregor9c702202012-02-10 07:45:31 +0000991void CXXRecordDecl::getCaptureFields(
992 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +0000993 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +0000994 Captures.clear();
995 ThisCapture = 0;
996
Douglas Gregorc8a73492012-02-13 15:44:47 +0000997 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +0000998 RecordDecl::field_iterator Field = field_begin();
Douglas Gregore5561632012-02-13 17:20:40 +0000999 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001000 C != CEnd; ++C, ++Field) {
1001 if (C->capturesThis()) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00001002 ThisCapture = &*Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001003 continue;
1004 }
1005
David Blaikie2d7c57e2012-04-30 02:36:29 +00001006 Captures[C->getCapturedVar()] = &*Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001007 }
1008}
1009
1010
John McCall1e3a1a72010-03-15 09:07:48 +00001011static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1012 QualType T;
John McCallda4458e2010-03-31 01:36:47 +00001013 if (isa<UsingShadowDecl>(Conv))
1014 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCall1e3a1a72010-03-15 09:07:48 +00001015 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
1016 T = ConvTemp->getTemplatedDecl()->getResultType();
1017 else
1018 T = cast<CXXConversionDecl>(Conv)->getConversionType();
1019 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001020}
1021
John McCall1e3a1a72010-03-15 09:07:48 +00001022/// Collect the visible conversions of a base class.
1023///
1024/// \param Base a base class of the class we're considering
1025/// \param InVirtual whether this base class is a virtual base (or a base
1026/// of a virtual base)
1027/// \param Access the access along the inheritance path to this base
1028/// \param ParentHiddenTypes the conversions provided by the inheritors
1029/// of this base
1030/// \param Output the set to which to add conversions from non-virtual bases
1031/// \param VOutput the set to which to add conversions from virtual bases
1032/// \param HiddenVBaseCs the set of conversions which were hidden in a
1033/// virtual base along some inheritance path
1034static void CollectVisibleConversions(ASTContext &Context,
1035 CXXRecordDecl *Record,
1036 bool InVirtual,
1037 AccessSpecifier Access,
1038 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
1039 UnresolvedSetImpl &Output,
1040 UnresolvedSetImpl &VOutput,
1041 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1042 // The set of types which have conversions in this class or its
1043 // subclasses. As an optimization, we don't copy the derived set
1044 // unless it might change.
1045 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1046 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1047
1048 // Collect the direct conversions and figure out which conversions
1049 // will be hidden in the subclasses.
1050 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1051 if (!Cs.empty()) {
1052 HiddenTypesBuffer = ParentHiddenTypes;
1053 HiddenTypes = &HiddenTypesBuffer;
1054
1055 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001056 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1057 bool Hidden = ParentHiddenTypes.count(ConvType);
1058 if (!Hidden)
1059 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001060
1061 // If this conversion is hidden and we're in a virtual base,
1062 // remember that it's hidden along some inheritance path.
1063 if (Hidden && InVirtual)
1064 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1065
1066 // If this conversion isn't hidden, add it to the appropriate output.
1067 else if (!Hidden) {
1068 AccessSpecifier IAccess
1069 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1070
1071 if (InVirtual)
1072 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001073 else
John McCall1e3a1a72010-03-15 09:07:48 +00001074 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001075 }
1076 }
1077 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001078
John McCall1e3a1a72010-03-15 09:07:48 +00001079 // Collect information recursively from any base classes.
1080 for (CXXRecordDecl::base_class_iterator
1081 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1082 const RecordType *RT = I->getType()->getAs<RecordType>();
1083 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001084
John McCall1e3a1a72010-03-15 09:07:48 +00001085 AccessSpecifier BaseAccess
1086 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1087 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001088
John McCall1e3a1a72010-03-15 09:07:48 +00001089 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1090 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1091 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001092 }
John McCall1e3a1a72010-03-15 09:07:48 +00001093}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001094
John McCall1e3a1a72010-03-15 09:07:48 +00001095/// Collect the visible conversions of a class.
1096///
1097/// This would be extremely straightforward if it weren't for virtual
1098/// bases. It might be worth special-casing that, really.
1099static void CollectVisibleConversions(ASTContext &Context,
1100 CXXRecordDecl *Record,
1101 UnresolvedSetImpl &Output) {
1102 // The collection of all conversions in virtual bases that we've
1103 // found. These will be added to the output as long as they don't
1104 // appear in the hidden-conversions set.
1105 UnresolvedSet<8> VBaseCs;
1106
1107 // The set of conversions in virtual bases that we've determined to
1108 // be hidden.
1109 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1110
1111 // The set of types hidden by classes derived from this one.
1112 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1113
1114 // Go ahead and collect the direct conversions and add them to the
1115 // hidden-types set.
1116 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
1117 Output.append(Cs.begin(), Cs.end());
1118 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
1119 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
1120
1121 // Recursively collect conversions from base classes.
1122 for (CXXRecordDecl::base_class_iterator
1123 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1124 const RecordType *RT = I->getType()->getAs<RecordType>();
1125 if (!RT) continue;
1126
1127 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1128 I->isVirtual(), I->getAccessSpecifier(),
1129 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1130 }
1131
1132 // Add any unhidden conversions provided by virtual bases.
1133 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1134 I != E; ++I) {
1135 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1136 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001137 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001138}
1139
1140/// getVisibleConversionFunctions - get all conversion functions visible
1141/// in current class; including conversion function templates.
John McCallad371252010-01-20 00:46:10 +00001142const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001143 // If root class, all conversions are visible.
1144 if (bases_begin() == bases_end())
John McCall67da35c2010-02-04 22:26:26 +00001145 return &data().Conversions;
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001146 // If visible conversion list is already evaluated, return it.
John McCall67da35c2010-02-04 22:26:26 +00001147 if (data().ComputedVisibleConversions)
1148 return &data().VisibleConversions;
John McCall1e3a1a72010-03-15 09:07:48 +00001149 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall67da35c2010-02-04 22:26:26 +00001150 data().ComputedVisibleConversions = true;
1151 return &data().VisibleConversions;
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001152}
1153
John McCallda4458e2010-03-31 01:36:47 +00001154void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1155 // This operation is O(N) but extremely rare. Sema only uses it to
1156 // remove UsingShadowDecls in a class that were followed by a direct
1157 // declaration, e.g.:
1158 // class A : B {
1159 // using B::operator int;
1160 // operator int();
1161 // };
1162 // This is uncommon by itself and even more uncommon in conjunction
1163 // with sufficiently large numbers of directly-declared conversions
1164 // that asymptotic behavior matters.
1165
1166 UnresolvedSetImpl &Convs = *getConversionFunctions();
1167 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1168 if (Convs[I].getDecl() == ConvDecl) {
1169 Convs.erase(I);
1170 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1171 && "conversion was found multiple times in unresolved set");
1172 return;
1173 }
1174 }
1175
1176 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001177}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001178
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001179CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001180 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001181 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1182
1183 return 0;
1184}
1185
Douglas Gregor06db9f52009-10-12 20:18:28 +00001186MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1187 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1188}
1189
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001190void
1191CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1192 TemplateSpecializationKind TSK) {
1193 assert(TemplateOrInstantiation.isNull() &&
1194 "Previous template or instantiation?");
1195 assert(!isa<ClassTemplateSpecializationDecl>(this));
1196 TemplateOrInstantiation
1197 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1198}
1199
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001200TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1201 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001202 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1203 return Spec->getSpecializationKind();
1204
Douglas Gregor06db9f52009-10-12 20:18:28 +00001205 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001206 return MSInfo->getTemplateSpecializationKind();
1207
1208 return TSK_Undeclared;
1209}
1210
1211void
1212CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1213 if (ClassTemplateSpecializationDecl *Spec
1214 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1215 Spec->setSpecializationKind(TSK);
1216 return;
1217 }
1218
Douglas Gregor06db9f52009-10-12 20:18:28 +00001219 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001220 MSInfo->setTemplateSpecializationKind(TSK);
1221 return;
1222 }
1223
David Blaikie83d382b2011-09-23 05:06:16 +00001224 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001225}
1226
Douglas Gregorbac74902010-07-01 14:13:13 +00001227CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1228 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001229 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001230
1231 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001232 = Context.DeclarationNames.getCXXDestructorName(
1233 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001234
John McCallf8ff7b92010-02-23 00:48:20 +00001235 DeclContext::lookup_const_iterator I, E;
Mike Stump11289f42009-09-09 15:08:12 +00001236 llvm::tie(I, E) = lookup(Name);
Sebastian Redlb469afb2010-09-02 23:19:42 +00001237 if (I == E)
1238 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001239
Anders Carlssonf98849e2009-12-02 17:15:43 +00001240 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson0a637412009-05-29 21:03:38 +00001241 return Dtor;
1242}
1243
Douglas Gregorb11aad82011-02-19 18:51:44 +00001244void CXXRecordDecl::completeDefinition() {
1245 completeDefinition(0);
1246}
1247
1248void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1249 RecordDecl::completeDefinition();
1250
David Blaikiebbafb8a2012-03-11 07:00:24 +00001251 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +00001252 // Objective-C Automatic Reference Counting:
1253 // If a class has a non-static data member of Objective-C pointer
1254 // type (or array thereof), it is a non-POD type and its
1255 // default constructor (if any), copy constructor, copy assignment
1256 // operator, and destructor are non-trivial.
1257 struct DefinitionData &Data = data();
1258 Data.PlainOldData = false;
1259 Data.HasTrivialDefaultConstructor = false;
1260 Data.HasTrivialCopyConstructor = false;
1261 Data.HasTrivialCopyAssignment = false;
1262 Data.HasTrivialDestructor = false;
Richard Smith561fb152012-02-25 07:33:38 +00001263 Data.HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +00001264 }
1265
Douglas Gregor8fb95122010-09-29 00:15:42 +00001266 // If the class may be abstract (but hasn't been marked as such), check for
1267 // any pure final overriders.
1268 if (mayBeAbstract()) {
1269 CXXFinalOverriderMap MyFinalOverriders;
1270 if (!FinalOverriders) {
1271 getFinalOverriders(MyFinalOverriders);
1272 FinalOverriders = &MyFinalOverriders;
1273 }
1274
1275 bool Done = false;
1276 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1277 MEnd = FinalOverriders->end();
1278 M != MEnd && !Done; ++M) {
1279 for (OverridingMethods::iterator SO = M->second.begin(),
1280 SOEnd = M->second.end();
1281 SO != SOEnd && !Done; ++SO) {
1282 assert(SO->second.size() > 0 &&
1283 "All virtual functions have overridding virtual functions");
1284
1285 // C++ [class.abstract]p4:
1286 // A class is abstract if it contains or inherits at least one
1287 // pure virtual function for which the final overrider is pure
1288 // virtual.
1289 if (SO->second.front().Method->isPure()) {
1290 data().Abstract = true;
1291 Done = true;
1292 break;
1293 }
1294 }
1295 }
1296 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001297
1298 // Set access bits correctly on the directly-declared conversions.
1299 for (UnresolvedSetIterator I = data().Conversions.begin(),
1300 E = data().Conversions.end();
1301 I != E; ++I)
1302 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001303}
1304
1305bool CXXRecordDecl::mayBeAbstract() const {
1306 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1307 isDependentContext())
1308 return false;
1309
1310 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1311 BEnd = bases_end();
1312 B != BEnd; ++B) {
1313 CXXRecordDecl *BaseDecl
1314 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1315 if (BaseDecl->isAbstract())
1316 return true;
1317 }
1318
1319 return false;
1320}
1321
David Blaikie68e081d2011-12-20 02:48:34 +00001322void CXXMethodDecl::anchor() { }
1323
Ted Kremenek21475702008-09-05 17:16:31 +00001324CXXMethodDecl *
1325CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001326 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001327 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001328 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00001329 bool isStatic, StorageClass SCAsWritten, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001330 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001331 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001332 isStatic, SCAsWritten, isInline, isConstexpr,
1333 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001334}
1335
Douglas Gregor72172e92012-01-05 21:55:30 +00001336CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1337 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1338 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1339 DeclarationNameInfo(), QualType(),
1340 0, false, SC_None, false, false,
1341 SourceLocation());
1342}
1343
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001344bool CXXMethodDecl::isUsualDeallocationFunction() const {
1345 if (getOverloadedOperator() != OO_Delete &&
1346 getOverloadedOperator() != OO_Array_Delete)
1347 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001348
1349 // C++ [basic.stc.dynamic.deallocation]p2:
1350 // A template instance is never a usual deallocation function,
1351 // regardless of its signature.
1352 if (getPrimaryTemplate())
1353 return false;
1354
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001355 // C++ [basic.stc.dynamic.deallocation]p2:
1356 // If a class T has a member deallocation function named operator delete
1357 // with exactly one parameter, then that function is a usual (non-placement)
1358 // deallocation function. [...]
1359 if (getNumParams() == 1)
1360 return true;
1361
1362 // C++ [basic.stc.dynamic.deallocation]p2:
1363 // [...] If class T does not declare such an operator delete but does
1364 // declare a member deallocation function named operator delete with
1365 // exactly two parameters, the second of which has type std::size_t (18.1),
1366 // then this function is a usual deallocation function.
1367 ASTContext &Context = getASTContext();
1368 if (getNumParams() != 2 ||
Chandler Carruth75cc3592010-02-08 18:54:05 +00001369 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1370 Context.getSizeType()))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001371 return false;
1372
1373 // This function is a usual deallocation function if there are no
1374 // single-parameter deallocation functions of the same kind.
1375 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1376 R.first != R.second; ++R.first) {
1377 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1378 if (FD->getNumParams() == 1)
1379 return false;
1380 }
1381
1382 return true;
1383}
1384
Douglas Gregorb139cd52010-05-01 20:49:11 +00001385bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00001386 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00001387 // A user-declared copy assignment operator X::operator= is a non-static
1388 // non-template member function of class X with exactly one parameter of
1389 // type X, X&, const X&, volatile X& or const volatile X&.
1390 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1391 /*non-static*/ isStatic() ||
Alexis Huntfcaeae42011-05-25 20:50:04 +00001392 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregorb139cd52010-05-01 20:49:11 +00001393 return false;
1394
1395 QualType ParamType = getParamDecl(0)->getType();
1396 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1397 ParamType = Ref->getPointeeType();
1398
1399 ASTContext &Context = getASTContext();
1400 QualType ClassType
1401 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1402 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1403}
1404
Alexis Huntfcaeae42011-05-25 20:50:04 +00001405bool CXXMethodDecl::isMoveAssignmentOperator() const {
1406 // C++0x [class.copy]p19:
1407 // A user-declared move assignment operator X::operator= is a non-static
1408 // non-template member function of class X with exactly one parameter of type
1409 // X&&, const X&&, volatile X&&, or const volatile X&&.
1410 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1411 getPrimaryTemplate() || getDescribedFunctionTemplate())
1412 return false;
1413
1414 QualType ParamType = getParamDecl(0)->getType();
1415 if (!isa<RValueReferenceType>(ParamType))
1416 return false;
1417 ParamType = ParamType->getPointeeType();
1418
1419 ASTContext &Context = getASTContext();
1420 QualType ClassType
1421 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1422 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1423}
1424
Anders Carlsson36d87e12009-05-16 23:58:37 +00001425void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001426 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001427 assert(!MD->getParent()->isDependentContext() &&
1428 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00001429 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001430
Douglas Gregor832940b2010-03-02 23:58:15 +00001431 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001432}
1433
1434CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001435 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor832940b2010-03-02 23:58:15 +00001436 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001437}
1438
1439CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001440 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor832940b2010-03-02 23:58:15 +00001441 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001442}
1443
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001444unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001445 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001446 return getASTContext().overridden_methods_size(this);
1447}
1448
Ted Kremenek21475702008-09-05 17:16:31 +00001449QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00001450 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1451 // If the member function is declared const, the type of this is const X*,
1452 // if the member function is declared volatile, the type of this is
1453 // volatile X*, and if the member function is declared const volatile,
1454 // the type of this is const volatile X*.
1455
Ted Kremenek21475702008-09-05 17:16:31 +00001456 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00001457
John McCalle78aac42010-03-10 03:28:59 +00001458 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00001459 ClassTy = C.getQualifiedType(ClassTy,
1460 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00001461 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00001462}
1463
Eli Friedman71a26d82009-12-06 20:50:05 +00001464bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00001465 // If this function is a template instantiation, look at the template from
1466 // which it was instantiated.
1467 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1468 if (!CheckFn)
1469 CheckFn = this;
1470
Eli Friedman71a26d82009-12-06 20:50:05 +00001471 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001472 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +00001473}
1474
Douglas Gregor355efbb2012-02-17 03:02:34 +00001475bool CXXMethodDecl::isLambdaStaticInvoker() const {
1476 return getParent()->isLambda() &&
1477 getIdentifier() && getIdentifier()->getName() == "__invoke";
1478}
1479
1480
Alexis Hunt1d792652011-01-08 20:30:50 +00001481CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1482 TypeSourceInfo *TInfo, bool IsVirtual,
1483 SourceLocation L, Expr *Init,
1484 SourceLocation R,
1485 SourceLocation EllipsisLoc)
Alexis Hunta50dd462011-01-08 23:01:16 +00001486 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001487 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1488 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001489{
Douglas Gregore8381c02008-11-05 04:29:56 +00001490}
1491
Alexis Hunt1d792652011-01-08 20:30:50 +00001492CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1493 FieldDecl *Member,
1494 SourceLocation MemberLoc,
1495 SourceLocation L, Expr *Init,
1496 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001497 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001498 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichetd583da02010-12-04 09:14:42 +00001499 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1500{
1501}
1502
Alexis Hunt1d792652011-01-08 20:30:50 +00001503CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1504 IndirectFieldDecl *Member,
1505 SourceLocation MemberLoc,
1506 SourceLocation L, Expr *Init,
1507 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001508 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001509 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001510 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001511{
Douglas Gregore8381c02008-11-05 04:29:56 +00001512}
1513
Alexis Hunt1d792652011-01-08 20:30:50 +00001514CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001515 TypeSourceInfo *TInfo,
1516 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00001517 SourceLocation R)
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001518 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1519 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Alexis Huntc5575cc2011-02-26 19:13:13 +00001520 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1521{
1522}
1523
1524CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Alexis Hunt1d792652011-01-08 20:30:50 +00001525 FieldDecl *Member,
1526 SourceLocation MemberLoc,
1527 SourceLocation L, Expr *Init,
1528 SourceLocation R,
1529 VarDecl **Indices,
1530 unsigned NumIndices)
Alexis Hunta50dd462011-01-08 23:01:16 +00001531 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichetd583da02010-12-04 09:14:42 +00001532 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001533 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregor94f9a482010-05-05 05:51:00 +00001534{
1535 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1536 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1537}
1538
Alexis Hunt1d792652011-01-08 20:30:50 +00001539CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1540 FieldDecl *Member,
1541 SourceLocation MemberLoc,
1542 SourceLocation L, Expr *Init,
1543 SourceLocation R,
1544 VarDecl **Indices,
1545 unsigned NumIndices) {
1546 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregor94f9a482010-05-05 05:51:00 +00001547 sizeof(VarDecl *) * NumIndices,
Alexis Hunt1d792652011-01-08 20:30:50 +00001548 llvm::alignOf<CXXCtorInitializer>());
Alexis Hunta50dd462011-01-08 23:01:16 +00001549 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1550 Indices, NumIndices);
Douglas Gregor94f9a482010-05-05 05:51:00 +00001551}
1552
Alexis Hunt1d792652011-01-08 20:30:50 +00001553TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001554 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001555 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001556 else
1557 return TypeLoc();
1558}
1559
Alexis Hunt1d792652011-01-08 20:30:50 +00001560const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001561 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001562 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001563 else
1564 return 0;
1565}
1566
Alexis Hunt1d792652011-01-08 20:30:50 +00001567SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001568 if (isAnyMemberInitializer())
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001569 return getMemberLocation();
Richard Smith938f40b2011-06-11 17:19:42 +00001570
1571 if (isInClassMemberInitializer())
1572 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001573
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001574 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1575 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1576
1577 return SourceLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001578}
1579
Alexis Hunt1d792652011-01-08 20:30:50 +00001580SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001581 if (isInClassMemberInitializer()) {
1582 FieldDecl *D = getAnyMember();
1583 if (Expr *I = D->getInClassInitializer())
1584 return I->getSourceRange();
1585 return SourceRange();
1586 }
1587
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001588 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00001589}
1590
David Blaikie68e081d2011-12-20 02:48:34 +00001591void CXXConstructorDecl::anchor() { }
1592
Douglas Gregor61956c42008-10-31 09:07:45 +00001593CXXConstructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001594CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1595 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1596 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1597 QualType(), 0, false, false, false,false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001598}
1599
1600CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00001601CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001602 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001603 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001604 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001605 bool isExplicit, bool isInline,
1606 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001607 assert(NameInfo.getName().getNameKind()
1608 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001609 "Name must refer to a constructor");
Abramo Bagnaradff19302011-03-08 08:55:46 +00001610 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001611 isExplicit, isInline, isImplicitlyDeclared,
1612 isConstexpr);
Douglas Gregor61956c42008-10-31 09:07:45 +00001613}
1614
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001615CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1616 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1617 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1618 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1619 return Construct->getConstructor();
1620
1621 return 0;
1622}
1623
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001624bool CXXConstructorDecl::isDefaultConstructor() const {
1625 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001626 // A default constructor for a class X is a constructor of class
1627 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001628 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00001629 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001630}
1631
Mike Stump11289f42009-09-09 15:08:12 +00001632bool
Douglas Gregor507eb872009-12-22 00:34:07 +00001633CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00001634 return isCopyOrMoveConstructor(TypeQuals) &&
1635 getParamDecl(0)->getType()->isLValueReferenceType();
1636}
1637
1638bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1639 return isCopyOrMoveConstructor(TypeQuals) &&
1640 getParamDecl(0)->getType()->isRValueReferenceType();
1641}
1642
1643/// \brief Determine whether this is a copy or move constructor.
1644bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001645 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001646 // A non-template constructor for class X is a copy constructor
1647 // if its first parameter is of type X&, const X&, volatile X& or
1648 // const volatile X&, and either there are no other parameters
1649 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00001650 // C++0x [class.copy]p3:
1651 // A non-template constructor for class X is a move constructor if its
1652 // first parameter is of type X&&, const X&&, volatile X&&, or
1653 // const volatile X&&, and either there are no other parameters or else
1654 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001655 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001656 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorff7028a2009-11-13 23:59:09 +00001657 (getPrimaryTemplate() != 0) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001658 (getDescribedFunctionTemplate() != 0))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001659 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001660
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001661 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00001662
1663 // Do we have a reference type?
1664 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00001665 if (!ParamRefType)
1666 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001667
Douglas Gregorff7028a2009-11-13 23:59:09 +00001668 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00001669 ASTContext &Context = getASTContext();
1670
Douglas Gregorff7028a2009-11-13 23:59:09 +00001671 CanQualType PointeeType
1672 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00001673 CanQualType ClassTy
1674 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001675 if (PointeeType.getUnqualifiedType() != ClassTy)
1676 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001677
John McCall8ccfcb52009-09-24 19:53:00 +00001678 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00001679
1680 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001681 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00001682 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001683}
1684
Anders Carlssond20e7952009-08-28 16:57:08 +00001685bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001686 // C++ [class.conv.ctor]p1:
1687 // A constructor declared without the function-specifier explicit
1688 // that can be called with a single parameter specifies a
1689 // conversion from the type of its first parameter to the type of
1690 // its class. Such a constructor is called a converting
1691 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00001692 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001693 return false;
1694
Mike Stump11289f42009-09-09 15:08:12 +00001695 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00001696 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001697 (getNumParams() == 1) ||
Anders Carlsson85446472009-06-06 04:14:07 +00001698 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001699}
Douglas Gregor61956c42008-10-31 09:07:45 +00001700
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00001701bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00001702 if ((getNumParams() < 1) ||
1703 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1704 (getPrimaryTemplate() == 0) ||
1705 (getDescribedFunctionTemplate() != 0))
1706 return false;
1707
1708 const ParmVarDecl *Param = getParamDecl(0);
1709
1710 ASTContext &Context = getASTContext();
1711 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1712
Douglas Gregorffe14e32009-11-14 01:20:54 +00001713 // Is it the same as our our class type?
1714 CanQualType ClassTy
1715 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1716 if (ParamType.getUnqualifiedType() != ClassTy)
1717 return false;
1718
1719 return true;
1720}
1721
Sebastian Redl08905022011-02-05 19:23:19 +00001722const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1723 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001724 method_iterator It = getASTContext().overridden_methods_begin(this);
1725 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redl08905022011-02-05 19:23:19 +00001726 return 0;
1727
1728 return cast<CXXConstructorDecl>(*It);
1729}
1730
1731void
1732CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1733 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001734 assert(getASTContext().overridden_methods_size(this) == 0 &&
1735 "Base ctor already set.");
1736 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redl08905022011-02-05 19:23:19 +00001737}
1738
David Blaikie68e081d2011-12-20 02:48:34 +00001739void CXXDestructorDecl::anchor() { }
1740
Douglas Gregor831c93f2008-11-05 20:51:48 +00001741CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001742CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1743 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1744 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001745 QualType(), 0, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001746}
1747
1748CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00001749CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001750 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001751 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001752 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001753 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001754 assert(NameInfo.getName().getNameKind()
1755 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001756 "Name must refer to a destructor");
Abramo Bagnaradff19302011-03-08 08:55:46 +00001757 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001758 isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001759}
1760
David Blaikie68e081d2011-12-20 02:48:34 +00001761void CXXConversionDecl::anchor() { }
1762
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001763CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001764CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1765 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1766 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1767 QualType(), 0, false, false, false,
1768 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001769}
1770
1771CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001772CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001773 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001774 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001775 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00001776 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00001777 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001778 assert(NameInfo.getName().getNameKind()
1779 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001780 "Name must refer to a conversion function");
Abramo Bagnaradff19302011-03-08 08:55:46 +00001781 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001782 isInline, isExplicit, isConstexpr,
1783 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001784}
1785
Douglas Gregord3b672c2012-02-16 01:06:16 +00001786bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1787 return isImplicit() && getParent()->isLambda() &&
1788 getConversionType()->isBlockPointerType();
1789}
1790
David Blaikie68e081d2011-12-20 02:48:34 +00001791void LinkageSpecDecl::anchor() { }
1792
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00001793LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00001794 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00001795 SourceLocation ExternLoc,
1796 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00001797 LanguageIDs Lang,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00001798 SourceLocation RBraceLoc) {
Abramo Bagnaraea947882011-03-08 16:41:52 +00001799 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00001800}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001801
Douglas Gregor72172e92012-01-05 21:55:30 +00001802LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1803 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1804 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1805 lang_c, SourceLocation());
1806}
1807
David Blaikie68e081d2011-12-20 02:48:34 +00001808void UsingDirectiveDecl::anchor() { }
1809
Douglas Gregor889ceb72009-02-03 19:21:40 +00001810UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1811 SourceLocation L,
1812 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00001813 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001814 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00001815 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001816 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001817 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1818 Used = NS->getOriginalNamespace();
Douglas Gregor12441b32011-02-25 16:33:46 +00001819 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1820 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00001821}
1822
Douglas Gregor72172e92012-01-05 21:55:30 +00001823UsingDirectiveDecl *
1824UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1825 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1826 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1827 NestedNameSpecifierLoc(),
1828 SourceLocation(), 0, 0);
1829}
1830
Sebastian Redla6602e92009-11-23 15:34:23 +00001831NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1832 if (NamespaceAliasDecl *NA =
1833 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1834 return NA->getNamespace();
1835 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1836}
1837
Douglas Gregor72172e92012-01-05 21:55:30 +00001838void NamespaceDecl::anchor() { }
1839
Douglas Gregore57e7522012-01-07 09:11:48 +00001840NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1841 SourceLocation StartLoc,
1842 SourceLocation IdLoc, IdentifierInfo *Id,
1843 NamespaceDecl *PrevDecl)
1844 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1845 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1846{
1847 setPreviousDeclaration(PrevDecl);
1848
1849 if (PrevDecl)
1850 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1851}
1852
Douglas Gregor72172e92012-01-05 21:55:30 +00001853NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001854 bool Inline, SourceLocation StartLoc,
1855 SourceLocation IdLoc, IdentifierInfo *Id,
1856 NamespaceDecl *PrevDecl) {
1857 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00001858}
1859
1860NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1861 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregore57e7522012-01-07 09:11:48 +00001862 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1863 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001864}
1865
David Blaikie68e081d2011-12-20 02:48:34 +00001866void NamespaceAliasDecl::anchor() { }
1867
Mike Stump11289f42009-09-09 15:08:12 +00001868NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00001869 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001870 SourceLocation AliasLoc,
1871 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00001872 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001873 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00001874 NamedDecl *Namespace) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001875 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1876 Namespace = NS->getOriginalNamespace();
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00001877 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1878 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00001879}
1880
Douglas Gregor72172e92012-01-05 21:55:30 +00001881NamespaceAliasDecl *
1882NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1883 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1884 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1885 NestedNameSpecifierLoc(),
1886 SourceLocation(), 0);
1887}
1888
David Blaikie68e081d2011-12-20 02:48:34 +00001889void UsingShadowDecl::anchor() { }
1890
Douglas Gregor72172e92012-01-05 21:55:30 +00001891UsingShadowDecl *
1892UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1893 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1894 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1895}
1896
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00001897UsingDecl *UsingShadowDecl::getUsingDecl() const {
1898 const UsingShadowDecl *Shadow = this;
1899 while (const UsingShadowDecl *NextShadow =
1900 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1901 Shadow = NextShadow;
1902 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1903}
1904
David Blaikie68e081d2011-12-20 02:48:34 +00001905void UsingDecl::anchor() { }
1906
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00001907void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1908 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1909 "declaration already in set");
1910 assert(S->getUsingDecl() == this);
1911
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00001912 if (FirstUsingShadow.getPointer())
1913 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1914 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00001915}
1916
1917void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1918 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1919 "declaration not in set");
1920 assert(S->getUsingDecl() == this);
1921
1922 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1923
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00001924 if (FirstUsingShadow.getPointer() == S) {
1925 FirstUsingShadow.setPointer(
1926 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00001927 S->UsingOrNextShadow = this;
1928 return;
1929 }
1930
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00001931 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00001932 while (Prev->UsingOrNextShadow != S)
1933 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1934 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1935 S->UsingOrNextShadow = this;
1936}
1937
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001938UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1939 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00001940 const DeclarationNameInfo &NameInfo,
1941 bool IsTypeNameArg) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001942 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregorfec52632009-06-20 00:51:54 +00001943}
1944
Douglas Gregor72172e92012-01-05 21:55:30 +00001945UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1946 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
1947 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
1948 DeclarationNameInfo(), false);
1949}
1950
David Blaikie68e081d2011-12-20 02:48:34 +00001951void UnresolvedUsingValueDecl::anchor() { }
1952
John McCalle61f2ba2009-11-18 02:36:19 +00001953UnresolvedUsingValueDecl *
1954UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1955 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001956 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00001957 const DeclarationNameInfo &NameInfo) {
John McCalle61f2ba2009-11-18 02:36:19 +00001958 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001959 QualifierLoc, NameInfo);
John McCalle61f2ba2009-11-18 02:36:19 +00001960}
1961
Douglas Gregor72172e92012-01-05 21:55:30 +00001962UnresolvedUsingValueDecl *
1963UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1964 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
1965 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
1966 NestedNameSpecifierLoc(),
1967 DeclarationNameInfo());
1968}
1969
David Blaikie68e081d2011-12-20 02:48:34 +00001970void UnresolvedUsingTypenameDecl::anchor() { }
1971
John McCalle61f2ba2009-11-18 02:36:19 +00001972UnresolvedUsingTypenameDecl *
1973UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1974 SourceLocation UsingLoc,
1975 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001976 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00001977 SourceLocation TargetNameLoc,
1978 DeclarationName TargetName) {
1979 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00001980 QualifierLoc, TargetNameLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00001981 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +00001982}
1983
Douglas Gregor72172e92012-01-05 21:55:30 +00001984UnresolvedUsingTypenameDecl *
1985UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1986 void *Mem = AllocateDeserializedDecl(C, ID,
1987 sizeof(UnresolvedUsingTypenameDecl));
1988 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
1989 SourceLocation(),
1990 NestedNameSpecifierLoc(),
1991 SourceLocation(),
1992 0);
1993}
1994
David Blaikie68e081d2011-12-20 02:48:34 +00001995void StaticAssertDecl::anchor() { }
1996
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00001997StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00001998 SourceLocation StaticAssertLoc,
1999 Expr *AssertExpr,
2000 StringLiteral *Message,
2001 SourceLocation RParenLoc) {
2002 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2003 RParenLoc);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002004}
2005
Douglas Gregor72172e92012-01-05 21:55:30 +00002006StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2007 unsigned ID) {
2008 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
2009 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,SourceLocation());
2010}
2011
Anders Carlsson6750d162009-03-26 23:46:50 +00002012static const char *getAccessName(AccessSpecifier AS) {
2013 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002014 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002015 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002016 case AS_public:
2017 return "public";
2018 case AS_private:
2019 return "private";
2020 case AS_protected:
2021 return "protected";
2022 }
David Blaikief47fa302012-01-17 02:30:50 +00002023 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002024}
2025
2026const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2027 AccessSpecifier AS) {
2028 return DB << getAccessName(AS);
2029}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002030
2031const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2032 AccessSpecifier AS) {
2033 return DB << getAccessName(AS);
2034}