blob: 43cea51e93c34ad4b957ee7b134618aa0f6359ec [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000017#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000018#include "clang/AST/CXXInheritance.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000020#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000021#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000022#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000023#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// Decl Allocation/Deallocation Method Implementations
28//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000029
John McCall86ff3082010-02-04 22:26:26 +000030CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
31 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000032 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000033 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000034 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000035 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Douglas Gregor2bb11012011-05-13 01:05:07 +000036 HasMutableFields(false), HasTrivialDefaultConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000037 HasConstExprNonCopyMoveConstructor(false), HasTrivialCopyConstructor(true),
38 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
39 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
40 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000041 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Hunt37b8c9e2011-05-09 21:45:35 +000042 DeclaredCopyConstructor(false), DeclaredCopyAssignment(false),
43 DeclaredDestructor(false), NumBases(0), NumVBases(0), Bases(), VBases(),
44 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000045}
46
47CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000048 SourceLocation StartLoc, SourceLocation IdLoc,
49 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
50 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000051 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000052 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000053
Jay Foad4ba2a172011-01-12 09:06:06 +000054CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000055 DeclContext *DC, SourceLocation StartLoc,
56 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000057 CXXRecordDecl* PrevDecl,
58 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000059 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
60 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000061
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000062 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000063 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000064 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000065 return R;
66}
67
Jay Foad4ba2a172011-01-12 09:06:06 +000068CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000069 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
70 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000071}
72
Mike Stump1eb44332009-09-09 15:08:12 +000073void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000074CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000075 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000076 ASTContext &C = getASTContext();
77
Mike Stump1eb44332009-09-09 15:08:12 +000078 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000079 // An aggregate is an array or a class (clause 9) with [...]
80 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000081 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000082
Douglas Gregor7c789c12010-10-29 22:39:52 +000083 if (!data().Bases.isOffset() && data().NumBases > 0)
84 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000085
Anders Carlsson6f6de732010-03-29 05:13:12 +000086 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000087 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000088
89 // The virtual bases of this class.
90 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000091
John McCall86ff3082010-02-04 22:26:26 +000092 data().Bases = new(C) CXXBaseSpecifier [NumBases];
93 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000094 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +000095 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000096 // Keep track of inherited vbases for this base class.
97 const CXXBaseSpecifier *Base = Bases[i];
98 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +000099 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000100 if (BaseType->isDependentType())
101 continue;
102 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000103 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000104
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000105 // C++ [dcl.init.aggr]p1:
106 // An aggregate is [...] a class with [...] no base classes [...].
107 data().Aggregate = false;
108
109 // C++ [class]p4:
110 // A POD-struct is an aggregate class...
111 data().PlainOldData = false;
112
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000113 // A class with a non-empty base class is not empty.
114 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000115 if (!BaseClassDecl->isEmpty()) {
116 if (!data().Empty) {
117 // C++0x [class]p7:
118 // A standard-layout class is a class that:
119 // [...]
120 // -- either has no non-static data members in the most derived
121 // class and at most one base class with non-static data members,
122 // or has no base classes with non-static data members, and
123 // If this is the second non-empty base, then neither of these two
124 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000125 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000126 }
127
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000128 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000129 data().HasNoNonEmptyBases = false;
130 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000131
Douglas Gregor85606eb2010-09-28 20:50:54 +0000132 // C++ [class.virtual]p1:
133 // A class that declares or inherits a virtual function is called a
134 // polymorphic class.
135 if (BaseClassDecl->isPolymorphic())
136 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000137
Chandler Carrutha8225442011-04-30 09:17:45 +0000138 // C++0x [class]p7:
139 // A standard-layout class is a class that: [...]
140 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000141 if (!BaseClassDecl->isStandardLayout())
142 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000143
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000144 // Record if this base is the first non-literal field or base.
145 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
146 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000147
Anders Carlsson6f6de732010-03-29 05:13:12 +0000148 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000149 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000150 BaseClassDecl->vbases_begin(),
151 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000152 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000153 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000154 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000155 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000156
157 if (Base->isVirtual()) {
158 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000159 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000160 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000161
162 // C++0x [meta.unary.prop] is_empty:
163 // T is a class type, but not a union type, with ... no virtual base
164 // classes
165 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000166
167 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000168 // A default constructor is trivial [...] if:
169 // -- its class has [...] no virtual bases
170 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000171
172 // C++0x [class.copy]p13:
173 // A copy/move constructor for class X is trivial if it is neither
174 // user-provided nor deleted and if
175 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000176 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000177 data().HasTrivialMoveConstructor = false;
178
179 // C++0x [class.copy]p27:
180 // A copy/move assignment operator for class X is trivial if it is
181 // neither user-provided nor deleted and if
182 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000183 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000184 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000185
186 // C++0x [class]p7:
187 // A standard-layout class is a class that: [...]
188 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000189 data().IsStandardLayout = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000190 } else {
191 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000192 // A default constructor is trivial [...] if:
193 // -- all the direct base classes of its class have trivial default
194 // constructors.
195 if (!BaseClassDecl->hasTrivialDefaultConstructor())
196 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000197
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000198 // C++0x [class.copy]p13:
199 // A copy/move constructor for class X is trivial if [...]
200 // [...]
201 // -- the constructor selected to copy/move each direct base class
202 // subobject is trivial, and
203 // FIXME: C++0x: We need to only consider the selected constructor
204 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000205 if (!BaseClassDecl->hasTrivialCopyConstructor())
206 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000207 if (!BaseClassDecl->hasTrivialMoveConstructor())
208 data().HasTrivialMoveConstructor = false;
209
210 // C++0x [class.copy]p27:
211 // A copy/move assignment operator for class X is trivial if [...]
212 // [...]
213 // -- the assignment operator selected to copy/move each direct base
214 // class subobject is trivial, and
215 // FIXME: C++0x: We need to only consider the selected operator instead
216 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000217 if (!BaseClassDecl->hasTrivialCopyAssignment())
218 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000219 if (!BaseClassDecl->hasTrivialMoveAssignment())
220 data().HasTrivialMoveAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000221 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000222
223 // C++ [class.ctor]p3:
224 // A destructor is trivial if all the direct base classes of its class
225 // have trivial destructors.
226 if (!BaseClassDecl->hasTrivialDestructor())
227 data().HasTrivialDestructor = false;
Douglas Gregor2bb11012011-05-13 01:05:07 +0000228
229 // Keep track of the presence of mutable fields.
230 if (BaseClassDecl->hasMutableFields())
231 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000232 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000233
234 if (VBases.empty())
235 return;
236
237 // Create base specifier for any direct or indirect virtual bases.
238 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
239 data().NumVBases = VBases.size();
240 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000241 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
242
Anders Carlsson6f6de732010-03-29 05:13:12 +0000243 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000244 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000245 continue;
246
Nick Lewycky56062202010-07-26 16:56:01 +0000247 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
248 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000249
Douglas Gregor7c789c12010-10-29 22:39:52 +0000250 data().getVBases()[I] =
Anders Carlsson6f6de732010-03-29 05:13:12 +0000251 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000252 VBaseClassDecl->getTagKind() == TTK_Class,
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000253 VBases[I]->getAccessSpecifier(), VBaseTypeInfo,
254 SourceLocation());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000255 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000256}
257
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000258/// Callback function for CXXRecordDecl::forallBases that acknowledges
259/// that it saw a base class.
260static bool SawBase(const CXXRecordDecl *, void *) {
261 return true;
262}
263
264bool CXXRecordDecl::hasAnyDependentBases() const {
265 if (!isDependentContext())
266 return false;
267
268 return !forallBases(SawBase, 0);
269}
270
Jay Foad4ba2a172011-01-12 09:06:06 +0000271bool CXXRecordDecl::hasConstCopyConstructor(const ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000272 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000273}
274
Chandler Carruthb7e95892011-04-23 10:47:28 +0000275bool CXXRecordDecl::isTriviallyCopyable() const {
276 // C++0x [class]p5:
277 // A trivially copyable class is a class that:
278 // -- has no non-trivial copy constructors,
279 if (!hasTrivialCopyConstructor()) return false;
280 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000281 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000282 // -- has no non-trivial copy assignment operators,
283 if (!hasTrivialCopyAssignment()) return false;
284 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000285 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000286 // -- has a trivial destructor.
287 if (!hasTrivialDestructor()) return false;
288
289 return true;
290}
291
Douglas Gregor0d405db2010-07-01 20:59:04 +0000292/// \brief Perform a simplistic form of overload resolution that only considers
293/// cv-qualifiers on a single parameter, and return the best overload candidate
294/// (if there is one).
295static CXXMethodDecl *
296GetBestOverloadCandidateSimple(
297 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
298 if (Cands.empty())
299 return 0;
300 if (Cands.size() == 1)
301 return Cands[0].first;
302
303 unsigned Best = 0, N = Cands.size();
304 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000305 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000306 Best = I;
307
308 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000309 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000310 return 0;
311
312 return Cands[Best].first;
313}
314
Jay Foad4ba2a172011-01-12 09:06:06 +0000315CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(const ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000316 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000317 QualType ClassType
318 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000319 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000320 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000321 Context.getCanonicalType(ClassType));
322 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000323 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000324 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000325 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000326 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000327 // C++ [class.copy]p2:
328 // A non-template constructor for class X is a copy constructor if [...]
329 if (isa<FunctionTemplateDecl>(*Con))
330 continue;
331
Douglas Gregor0d405db2010-07-01 20:59:04 +0000332 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
333 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000334 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
335 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000336 Found.push_back(std::make_pair(
337 const_cast<CXXConstructorDecl *>(Constructor),
338 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000339 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000340 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000341
342 return cast_or_null<CXXConstructorDecl>(
343 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000344}
345
Douglas Gregorb87786f2010-07-01 17:48:08 +0000346CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
347 ASTContext &Context = getASTContext();
348 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
349 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
350
351 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
352 DeclContext::lookup_const_iterator Op, OpEnd;
353 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
354 // C++ [class.copy]p9:
355 // A user-declared copy assignment operator is a non-static non-template
356 // member function of class X with exactly one parameter of type X, X&,
357 // const X&, volatile X& or const volatile X&.
358 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
359 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
360 continue;
361
362 const FunctionProtoType *FnType
363 = Method->getType()->getAs<FunctionProtoType>();
364 assert(FnType && "Overloaded operator has no prototype.");
365 // Don't assert on this; an invalid decl might have been left in the AST.
366 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
367 continue;
368
369 QualType ArgType = FnType->getArgType(0);
370 Qualifiers Quals;
371 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
372 ArgType = Ref->getPointeeType();
373 // If we have a const argument and we have a reference to a non-const,
374 // this function does not match.
375 if (ArgIsConst && !ArgType.isConstQualified())
376 continue;
377
378 Quals = ArgType.getQualifiers();
379 } else {
380 // By-value copy-assignment operators are treated like const X&
381 // copy-assignment operators.
382 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
383 }
384
385 if (!Context.hasSameUnqualifiedType(ArgType, Class))
386 continue;
387
388 // Save this copy-assignment operator. It might be "the one".
389 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
390 }
391
392 // Use a simplistic form of overload resolution to find the candidate.
393 return GetBestOverloadCandidateSimple(Found);
394}
395
Douglas Gregor21386642010-09-28 21:55:22 +0000396void CXXRecordDecl::markedVirtualFunctionPure() {
397 // C++ [class.abstract]p2:
398 // A class is abstract if it has at least one pure virtual function.
399 data().Abstract = true;
400}
401
402void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000403 // Ignore friends and invalid declarations.
404 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000405 return;
406
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000407 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
408 if (FunTmpl)
409 D = FunTmpl->getTemplatedDecl();
410
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000411 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
412 if (Method->isVirtual()) {
413 // C++ [dcl.init.aggr]p1:
414 // An aggregate is an array or a class with [...] no virtual functions.
415 data().Aggregate = false;
416
417 // C++ [class]p4:
418 // A POD-struct is an aggregate class...
419 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000420
421 // Virtual functions make the class non-empty.
422 // FIXME: Standard ref?
423 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000424
425 // C++ [class.virtual]p1:
426 // A class that declares or inherits a virtual function is called a
427 // polymorphic class.
428 data().Polymorphic = true;
429
Sean Hunt023df372011-05-09 18:22:59 +0000430 // C++0x [class.ctor]p5
431 // A default constructor is trivial [...] if:
432 // -- its class has no virtual functions [...]
433 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000434
435 // C++0x [class.copy]p13:
436 // A copy/move constructor for class X is trivial if [...]
437 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000438 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000439 data().HasTrivialMoveConstructor = false;
440
441 // C++0x [class.copy]p27:
442 // A copy/move assignment operator for class X is trivial if [...]
443 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000444 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000445 data().HasTrivialMoveAssignment = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000446 // FIXME: Destructor?
Chandler Carrutha8225442011-04-30 09:17:45 +0000447
448 // C++0x [class]p7:
449 // A standard-layout class is a class that: [...]
450 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000451 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000452 }
453 }
454
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000455 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000456 // Notify that an implicit member was added after the definition
457 // was completed.
458 if (!isBeingDefined())
459 if (ASTMutationListener *L = getASTMutationListener())
460 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000461
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000462 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
463 // If this is the implicit default constructor, note that we have now
464 // declared it.
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000465 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000466 data().DeclaredDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000467 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000468 // If this is the implicit copy constructor, note that we have now
469 // declared it.
470 else if (Constructor->isCopyConstructor())
471 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000472 return;
473 }
474
475 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000476 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000477 return;
478 }
479
480 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000481 // If this is the implicit copy constructor, note that we have now
482 // declared it.
483 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000484 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000485 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000486 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000487 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000488
489 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000490 }
491
492 // Handle (user-declared) constructors.
493 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
494 // Note that we have a user-declared constructor.
495 data().UserDeclaredConstructor = true;
496
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000497 // FIXME: Under C++0x, /only/ special member functions may be user-provided.
498 // This is probably a defect.
499 bool UserProvided = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000500
Sean Hunt023df372011-05-09 18:22:59 +0000501 // C++0x [class.ctor]p5:
502 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000503 if (Constructor->isDefaultConstructor()) {
504 data().DeclaredDefaultConstructor = true;
505 if (Constructor->isUserProvided()) {
506 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000507 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000508 UserProvided = true;
509 }
510 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000511
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000512 // Note when we have a user-declared copy or move constructor, which will
513 // suppress the implicit declaration of those constructors.
514 if (!FunTmpl) {
515 if (Constructor->isCopyConstructor()) {
516 data().UserDeclaredCopyConstructor = true;
517 data().DeclaredCopyConstructor = true;
518
519 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000520 // A copy/move constructor for class X is trivial if it is not
521 // user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000522 if (Constructor->isUserProvided()) {
Sean Hunt023df372011-05-09 18:22:59 +0000523 data().HasTrivialCopyConstructor = false;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000524 UserProvided = true;
525 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000526 } else if (Constructor->isMoveConstructor()) {
527 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000528 // A copy/move constructor for class X is trivial if it is not
529 // user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000530 if (Constructor->isUserProvided()) {
Sean Hunt023df372011-05-09 18:22:59 +0000531 data().HasTrivialMoveConstructor = false;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000532 UserProvided = true;
533 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000534 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000535 }
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000536 if (Constructor->isConstExpr() &&
537 !Constructor->isCopyOrMoveConstructor()) {
538 // Record if we see any constexpr constructors which are niether copy
539 // nor move constructors.
540 data().HasConstExprNonCopyMoveConstructor = true;
541 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000542
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000543 // C++ [dcl.init.aggr]p1:
544 // An aggregate is an array or a class with no user-declared
545 // constructors [...].
546 // C++0x [dcl.init.aggr]p1:
547 // An aggregate is an array or a class with no user-provided
548 // constructors [...].
549 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
550 data().Aggregate = false;
551
552 // C++ [class]p4:
553 // A POD-struct is an aggregate class [...]
554 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
555 // type is technically an aggregate in C++0x since it wouldn't be in 03.
556 data().PlainOldData = false;
557
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000558 return;
559 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000560
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000561 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000562 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000563 data().DeclaredDestructor = true;
564 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000565
566 // C++ [class]p4:
567 // A POD-struct is an aggregate class that has [...] no user-defined
568 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000569 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000570 data().PlainOldData = false;
571
Sean Huntcf34e752011-05-16 22:41:40 +0000572 // C++0x [class.dtor]p5:
573 // A destructor is trivial if it is not user-provided and [...]
574 if (DD->isUserProvided())
575 data().HasTrivialDestructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000576
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000577 return;
578 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000579
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000580 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000581 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
582 if (Method->getOverloadedOperator() == OO_Equal) {
583 // We're interested specifically in copy assignment operators.
584 const FunctionProtoType *FnType
585 = Method->getType()->getAs<FunctionProtoType>();
586 assert(FnType && "Overloaded operator has no proto function type.");
587 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
588
589 // Copy assignment operators must be non-templates.
590 if (Method->getPrimaryTemplate() || FunTmpl)
591 return;
592
593 ASTContext &Context = getASTContext();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000594 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
595 const_cast<CXXRecordDecl*>(this)));
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000596
597 bool isRValueRefArg = false;
598 QualType ArgType = FnType->getArgType(0);
599 if (const LValueReferenceType *Ref =
600 ArgType->getAs<LValueReferenceType>()) {
601 ArgType = Ref->getPointeeType();
602 } else if (const RValueReferenceType *Ref =
603 ArgType->getAs<RValueReferenceType>()) {
604 ArgType = Ref->getPointeeType();
605 isRValueRefArg = true;
606 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000607 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
608 return;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000609
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000610 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000611 // A POD-struct is an aggregate class that [...] has no user-defined
612 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000613 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000614 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000615
616 if (!isRValueRefArg) {
617 // This is a copy assignment operator.
618
619 // Suppress the implicit declaration of a copy constructor.
620 data().UserDeclaredCopyAssignment = true;
621 data().DeclaredCopyAssignment = true;
622
623 // C++0x [class.copy]p27:
624 // A copy/move assignment operator for class X is trivial if it is
625 // neither user-provided nor deleted [...]
Sean Huntcf34e752011-05-16 22:41:40 +0000626 if (Method->isUserProvided())
627 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000628 } else {
629 // This is a move assignment operator.
630
631 // C++0x [class.copy]p27:
632 // A copy/move assignment operator for class X is trivial if it is
633 // neither user-provided nor deleted [...]
Sean Huntcf34e752011-05-16 22:41:40 +0000634 if (Method->isUserProvided())
635 data().HasTrivialMoveAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000636 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000637 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000638
Douglas Gregore80622f2010-09-29 04:25:11 +0000639 // Keep the list of conversion functions up-to-date.
640 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
641 // We don't record specializations.
642 if (Conversion->getPrimaryTemplate())
643 return;
644
645 // FIXME: We intentionally don't use the decl's access here because it
646 // hasn't been set yet. That's really just a misdesign in Sema.
647
648 if (FunTmpl) {
649 if (FunTmpl->getPreviousDeclaration())
650 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
651 FunTmpl);
652 else
653 data().Conversions.addDecl(FunTmpl);
654 } else {
655 if (Conversion->getPreviousDeclaration())
656 data().Conversions.replace(Conversion->getPreviousDeclaration(),
657 Conversion);
658 else
659 data().Conversions.addDecl(Conversion);
660 }
661 }
662
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000663 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000664 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000665
666 // Handle non-static data members.
667 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
668 // C++ [dcl.init.aggr]p1:
669 // An aggregate is an array or a class (clause 9) with [...] no
670 // private or protected non-static data members (clause 11).
671 //
672 // A POD must be an aggregate.
673 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
674 data().Aggregate = false;
675 data().PlainOldData = false;
676 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000677
678 // C++0x [class]p7:
679 // A standard-layout class is a class that:
680 // [...]
681 // -- has the same access control for all non-static data members,
682 switch (D->getAccess()) {
683 case AS_private: data().HasPrivateFields = true; break;
684 case AS_protected: data().HasProtectedFields = true; break;
685 case AS_public: data().HasPublicFields = true; break;
686 case AS_none: assert(0 && "Invalid access specifier");
687 };
688 if ((data().HasPrivateFields + data().HasProtectedFields +
689 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000690 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000691
Douglas Gregor2bb11012011-05-13 01:05:07 +0000692 // Keep track of the presence of mutable fields.
693 if (Field->isMutable())
694 data().HasMutableFields = true;
695
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000696 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000697 // A POD struct is a class that is both a trivial class and a
698 // standard-layout class, and has no non-static data members of type
699 // non-POD struct, non-POD union (or array of such types).
700 ASTContext &Context = getASTContext();
701 QualType T = Context.getBaseElementType(Field->getType());
702 if (!T->isPODType())
703 data().PlainOldData = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000704 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000705 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000706
Chandler Carrutha8225442011-04-30 09:17:45 +0000707 // C++0x [class]p7:
708 // A standard-layout class is a class that:
709 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000710 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000711 }
712
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000713 // Record if this field is the first non-literal field or base.
714 if (!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType())
715 data().HasNonLiteralTypeFieldsOrBases = true;
716
Douglas Gregor85606eb2010-09-28 20:50:54 +0000717 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
718 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
719 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000720 // C++0x [class.ctor]p5:
721 // A defulat constructor is trivial [...] if:
722 // -- for all the non-static data members of its class that are of
723 // class type (or array thereof), each such class has a trivial
724 // default constructor.
725 if (!FieldRec->hasTrivialDefaultConstructor())
726 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000727
728 // C++0x [class.copy]p13:
729 // A copy/move constructor for class X is trivial if [...]
730 // [...]
731 // -- for each non-static data member of X that is of class type (or
732 // an array thereof), the constructor selected to copy/move that
733 // member is trivial;
734 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000735 if (!FieldRec->hasTrivialCopyConstructor())
736 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000737 if (!FieldRec->hasTrivialMoveConstructor())
738 data().HasTrivialMoveConstructor = false;
739
740 // C++0x [class.copy]p27:
741 // A copy/move assignment operator for class X is trivial if [...]
742 // [...]
743 // -- for each non-static data member of X that is of class type (or
744 // an array thereof), the assignment operator selected to
745 // copy/move that member is trivial;
746 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000747 if (!FieldRec->hasTrivialCopyAssignment())
748 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000749 if (!FieldRec->hasTrivialMoveAssignment())
750 data().HasTrivialMoveAssignment = false;
751
Douglas Gregor85606eb2010-09-28 20:50:54 +0000752 if (!FieldRec->hasTrivialDestructor())
753 data().HasTrivialDestructor = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000754
755 // C++0x [class]p7:
756 // A standard-layout class is a class that:
757 // -- has no non-static data members of type non-standard-layout
758 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000759 if (!FieldRec->isStandardLayout())
760 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000761
762 // C++0x [class]p7:
763 // A standard-layout class is a class that:
764 // [...]
765 // -- has no base classes of the same type as the first non-static
766 // data member.
767 // We don't want to expend bits in the state of the record decl
768 // tracking whether this is the first non-static data member so we
769 // cheat a bit and use some of the existing state: the empty bit.
770 // Virtual bases and virtual methods make a class non-empty, but they
771 // also make it non-standard-layout so we needn't check here.
772 // A non-empty base class may leave the class standard-layout, but not
773 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000774 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000775 // data member must come through here with Empty still true, and Empty
776 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000777 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000778 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
779 BE = bases_end();
780 BI != BE; ++BI) {
781 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000782 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000783 break;
784 }
785 }
786 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000787
788 // Keep track of the presence of mutable fields.
789 if (FieldRec->hasMutableFields())
790 data().HasMutableFields = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000791 }
792 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000793
794 // C++0x [class]p7:
795 // A standard-layout class is a class that:
796 // [...]
797 // -- either has no non-static data members in the most derived
798 // class and at most one base class with non-static data members,
799 // or has no base classes with non-static data members, and
800 // At this point we know that we have a non-static data member, so the last
801 // clause holds.
802 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000803 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000804
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000805 // If this is not a zero-length bit-field, then the class is not empty.
806 if (data().Empty) {
807 if (!Field->getBitWidth())
808 data().Empty = false;
809 else if (!Field->getBitWidth()->isTypeDependent() &&
810 !Field->getBitWidth()->isValueDependent()) {
811 llvm::APSInt Bits;
812 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
813 if (!!Bits)
814 data().Empty = false;
815 }
816 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000817 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000818
819 // Handle using declarations of conversion functions.
820 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
821 if (Shadow->getDeclName().getNameKind()
822 == DeclarationName::CXXConversionFunctionName)
823 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000824}
825
John McCallb05b5f32010-03-15 09:07:48 +0000826static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
827 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000828 if (isa<UsingShadowDecl>(Conv))
829 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000830 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
831 T = ConvTemp->getTemplatedDecl()->getResultType();
832 else
833 T = cast<CXXConversionDecl>(Conv)->getConversionType();
834 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000835}
836
John McCallb05b5f32010-03-15 09:07:48 +0000837/// Collect the visible conversions of a base class.
838///
839/// \param Base a base class of the class we're considering
840/// \param InVirtual whether this base class is a virtual base (or a base
841/// of a virtual base)
842/// \param Access the access along the inheritance path to this base
843/// \param ParentHiddenTypes the conversions provided by the inheritors
844/// of this base
845/// \param Output the set to which to add conversions from non-virtual bases
846/// \param VOutput the set to which to add conversions from virtual bases
847/// \param HiddenVBaseCs the set of conversions which were hidden in a
848/// virtual base along some inheritance path
849static void CollectVisibleConversions(ASTContext &Context,
850 CXXRecordDecl *Record,
851 bool InVirtual,
852 AccessSpecifier Access,
853 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
854 UnresolvedSetImpl &Output,
855 UnresolvedSetImpl &VOutput,
856 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
857 // The set of types which have conversions in this class or its
858 // subclasses. As an optimization, we don't copy the derived set
859 // unless it might change.
860 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
861 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
862
863 // Collect the direct conversions and figure out which conversions
864 // will be hidden in the subclasses.
865 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
866 if (!Cs.empty()) {
867 HiddenTypesBuffer = ParentHiddenTypes;
868 HiddenTypes = &HiddenTypesBuffer;
869
870 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
871 bool Hidden =
872 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
873
874 // If this conversion is hidden and we're in a virtual base,
875 // remember that it's hidden along some inheritance path.
876 if (Hidden && InVirtual)
877 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
878
879 // If this conversion isn't hidden, add it to the appropriate output.
880 else if (!Hidden) {
881 AccessSpecifier IAccess
882 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
883
884 if (InVirtual)
885 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000886 else
John McCallb05b5f32010-03-15 09:07:48 +0000887 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000888 }
889 }
890 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000891
John McCallb05b5f32010-03-15 09:07:48 +0000892 // Collect information recursively from any base classes.
893 for (CXXRecordDecl::base_class_iterator
894 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
895 const RecordType *RT = I->getType()->getAs<RecordType>();
896 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000897
John McCallb05b5f32010-03-15 09:07:48 +0000898 AccessSpecifier BaseAccess
899 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
900 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000901
John McCallb05b5f32010-03-15 09:07:48 +0000902 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
903 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
904 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000905 }
John McCallb05b5f32010-03-15 09:07:48 +0000906}
Sebastian Redl9994a342009-10-25 17:03:50 +0000907
John McCallb05b5f32010-03-15 09:07:48 +0000908/// Collect the visible conversions of a class.
909///
910/// This would be extremely straightforward if it weren't for virtual
911/// bases. It might be worth special-casing that, really.
912static void CollectVisibleConversions(ASTContext &Context,
913 CXXRecordDecl *Record,
914 UnresolvedSetImpl &Output) {
915 // The collection of all conversions in virtual bases that we've
916 // found. These will be added to the output as long as they don't
917 // appear in the hidden-conversions set.
918 UnresolvedSet<8> VBaseCs;
919
920 // The set of conversions in virtual bases that we've determined to
921 // be hidden.
922 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
923
924 // The set of types hidden by classes derived from this one.
925 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
926
927 // Go ahead and collect the direct conversions and add them to the
928 // hidden-types set.
929 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
930 Output.append(Cs.begin(), Cs.end());
931 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
932 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
933
934 // Recursively collect conversions from base classes.
935 for (CXXRecordDecl::base_class_iterator
936 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
937 const RecordType *RT = I->getType()->getAs<RecordType>();
938 if (!RT) continue;
939
940 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
941 I->isVirtual(), I->getAccessSpecifier(),
942 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
943 }
944
945 // Add any unhidden conversions provided by virtual bases.
946 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
947 I != E; ++I) {
948 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
949 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000950 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000951}
952
953/// getVisibleConversionFunctions - get all conversion functions visible
954/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000955const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000956 // If root class, all conversions are visible.
957 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000958 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000959 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000960 if (data().ComputedVisibleConversions)
961 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000962 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000963 data().ComputedVisibleConversions = true;
964 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000965}
966
John McCall32daa422010-03-31 01:36:47 +0000967void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
968 // This operation is O(N) but extremely rare. Sema only uses it to
969 // remove UsingShadowDecls in a class that were followed by a direct
970 // declaration, e.g.:
971 // class A : B {
972 // using B::operator int;
973 // operator int();
974 // };
975 // This is uncommon by itself and even more uncommon in conjunction
976 // with sufficiently large numbers of directly-declared conversions
977 // that asymptotic behavior matters.
978
979 UnresolvedSetImpl &Convs = *getConversionFunctions();
980 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
981 if (Convs[I].getDecl() == ConvDecl) {
982 Convs.erase(I);
983 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
984 && "conversion was found multiple times in unresolved set");
985 return;
986 }
987 }
988
989 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000990}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000991
Douglas Gregorf6b11852009-10-08 15:14:33 +0000992CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000993 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000994 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
995
996 return 0;
997}
998
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000999MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1000 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1001}
1002
Douglas Gregorf6b11852009-10-08 15:14:33 +00001003void
1004CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1005 TemplateSpecializationKind TSK) {
1006 assert(TemplateOrInstantiation.isNull() &&
1007 "Previous template or instantiation?");
1008 assert(!isa<ClassTemplateSpecializationDecl>(this));
1009 TemplateOrInstantiation
1010 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1011}
1012
Anders Carlssonb13e3572009-12-07 06:33:48 +00001013TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1014 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001015 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1016 return Spec->getSpecializationKind();
1017
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001018 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001019 return MSInfo->getTemplateSpecializationKind();
1020
1021 return TSK_Undeclared;
1022}
1023
1024void
1025CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1026 if (ClassTemplateSpecializationDecl *Spec
1027 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1028 Spec->setSpecializationKind(TSK);
1029 return;
1030 }
1031
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001032 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001033 MSInfo->setTemplateSpecializationKind(TSK);
1034 return;
1035 }
1036
1037 assert(false && "Not a class template or member class specialization");
1038}
1039
Douglas Gregor1d110e02010-07-01 14:13:13 +00001040CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1041 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001042 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001043
1044 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001045 = Context.DeclarationNames.getCXXDestructorName(
1046 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001047
John McCallc0bf4622010-02-23 00:48:20 +00001048 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001049 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001050 if (I == E)
1051 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001053 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001054 return Dtor;
1055}
1056
Douglas Gregorda2142f2011-02-19 18:51:44 +00001057void CXXRecordDecl::completeDefinition() {
1058 completeDefinition(0);
1059}
1060
1061void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1062 RecordDecl::completeDefinition();
1063
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001064 // If the class may be abstract (but hasn't been marked as such), check for
1065 // any pure final overriders.
1066 if (mayBeAbstract()) {
1067 CXXFinalOverriderMap MyFinalOverriders;
1068 if (!FinalOverriders) {
1069 getFinalOverriders(MyFinalOverriders);
1070 FinalOverriders = &MyFinalOverriders;
1071 }
1072
1073 bool Done = false;
1074 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1075 MEnd = FinalOverriders->end();
1076 M != MEnd && !Done; ++M) {
1077 for (OverridingMethods::iterator SO = M->second.begin(),
1078 SOEnd = M->second.end();
1079 SO != SOEnd && !Done; ++SO) {
1080 assert(SO->second.size() > 0 &&
1081 "All virtual functions have overridding virtual functions");
1082
1083 // C++ [class.abstract]p4:
1084 // A class is abstract if it contains or inherits at least one
1085 // pure virtual function for which the final overrider is pure
1086 // virtual.
1087 if (SO->second.front().Method->isPure()) {
1088 data().Abstract = true;
1089 Done = true;
1090 break;
1091 }
1092 }
1093 }
1094 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001095
1096 // Set access bits correctly on the directly-declared conversions.
1097 for (UnresolvedSetIterator I = data().Conversions.begin(),
1098 E = data().Conversions.end();
1099 I != E; ++I)
1100 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001101}
1102
1103bool CXXRecordDecl::mayBeAbstract() const {
1104 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1105 isDependentContext())
1106 return false;
1107
1108 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1109 BEnd = bases_end();
1110 B != BEnd; ++B) {
1111 CXXRecordDecl *BaseDecl
1112 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1113 if (BaseDecl->isAbstract())
1114 return true;
1115 }
1116
1117 return false;
1118}
1119
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001120CXXMethodDecl *
1121CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001122 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001123 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001124 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001125 bool isStatic, StorageClass SCAsWritten, bool isInline,
1126 SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001127 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001128 isStatic, SCAsWritten, isInline, EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001129}
1130
Douglas Gregor90916562009-09-29 18:16:17 +00001131bool CXXMethodDecl::isUsualDeallocationFunction() const {
1132 if (getOverloadedOperator() != OO_Delete &&
1133 getOverloadedOperator() != OO_Array_Delete)
1134 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001135
1136 // C++ [basic.stc.dynamic.deallocation]p2:
1137 // A template instance is never a usual deallocation function,
1138 // regardless of its signature.
1139 if (getPrimaryTemplate())
1140 return false;
1141
Douglas Gregor90916562009-09-29 18:16:17 +00001142 // C++ [basic.stc.dynamic.deallocation]p2:
1143 // If a class T has a member deallocation function named operator delete
1144 // with exactly one parameter, then that function is a usual (non-placement)
1145 // deallocation function. [...]
1146 if (getNumParams() == 1)
1147 return true;
1148
1149 // C++ [basic.stc.dynamic.deallocation]p2:
1150 // [...] If class T does not declare such an operator delete but does
1151 // declare a member deallocation function named operator delete with
1152 // exactly two parameters, the second of which has type std::size_t (18.1),
1153 // then this function is a usual deallocation function.
1154 ASTContext &Context = getASTContext();
1155 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001156 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1157 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001158 return false;
1159
1160 // This function is a usual deallocation function if there are no
1161 // single-parameter deallocation functions of the same kind.
1162 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1163 R.first != R.second; ++R.first) {
1164 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1165 if (FD->getNumParams() == 1)
1166 return false;
1167 }
1168
1169 return true;
1170}
1171
Douglas Gregor06a9f362010-05-01 20:49:11 +00001172bool CXXMethodDecl::isCopyAssignmentOperator() const {
1173 // C++0x [class.copy]p19:
1174 // A user-declared copy assignment operator X::operator= is a non-static
1175 // non-template member function of class X with exactly one parameter of
1176 // type X, X&, const X&, volatile X& or const volatile X&.
1177 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1178 /*non-static*/ isStatic() ||
1179 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1180 /*exactly one parameter*/getNumParams() != 1)
1181 return false;
1182
1183 QualType ParamType = getParamDecl(0)->getType();
1184 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1185 ParamType = Ref->getPointeeType();
1186
1187 ASTContext &Context = getASTContext();
1188 QualType ClassType
1189 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1190 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1191}
1192
Anders Carlsson05eb2442009-05-16 23:58:37 +00001193void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001194 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001195 assert(!MD->getParent()->isDependentContext() &&
1196 "Can't add an overridden method to a class template!");
1197
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001198 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001199}
1200
1201CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001202 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001203}
1204
1205CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001206 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001207}
1208
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001209unsigned CXXMethodDecl::size_overridden_methods() const {
1210 return getASTContext().overridden_methods_size(this);
1211}
1212
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001213QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001214 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1215 // If the member function is declared const, the type of this is const X*,
1216 // if the member function is declared volatile, the type of this is
1217 // volatile X*, and if the member function is declared const volatile,
1218 // the type of this is const volatile X*.
1219
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001220 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001221
John McCall3cb0ebd2010-03-10 03:28:59 +00001222 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001223 ClassTy = C.getQualifiedType(ClassTy,
1224 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001225 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001226}
1227
Eli Friedmand7d7f672009-12-06 20:50:05 +00001228bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001229 // If this function is a template instantiation, look at the template from
1230 // which it was instantiated.
1231 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1232 if (!CheckFn)
1233 CheckFn = this;
1234
Eli Friedmand7d7f672009-12-06 20:50:05 +00001235 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001236 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001237}
1238
Sean Huntcbb67482011-01-08 20:30:50 +00001239CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1240 TypeSourceInfo *TInfo, bool IsVirtual,
1241 SourceLocation L, Expr *Init,
1242 SourceLocation R,
1243 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001244 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001245 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1246 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001247{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001248}
1249
Sean Huntcbb67482011-01-08 20:30:50 +00001250CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1251 FieldDecl *Member,
1252 SourceLocation MemberLoc,
1253 SourceLocation L, Expr *Init,
1254 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001255 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001256 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1257 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1258{
1259}
1260
Sean Huntcbb67482011-01-08 20:30:50 +00001261CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1262 IndirectFieldDecl *Member,
1263 SourceLocation MemberLoc,
1264 SourceLocation L, Expr *Init,
1265 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001266 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001267 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001268 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001269{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001270}
1271
Sean Huntcbb67482011-01-08 20:30:50 +00001272CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Hunt41717662011-02-26 19:13:13 +00001273 SourceLocation D, SourceLocation L,
1274 CXXConstructorDecl *Target, Expr *Init,
1275 SourceLocation R)
1276 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1277 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1278 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1279{
1280}
1281
1282CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001283 FieldDecl *Member,
1284 SourceLocation MemberLoc,
1285 SourceLocation L, Expr *Init,
1286 SourceLocation R,
1287 VarDecl **Indices,
1288 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001289 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001290 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001291 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001292{
1293 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1294 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1295}
1296
Sean Huntcbb67482011-01-08 20:30:50 +00001297CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1298 FieldDecl *Member,
1299 SourceLocation MemberLoc,
1300 SourceLocation L, Expr *Init,
1301 SourceLocation R,
1302 VarDecl **Indices,
1303 unsigned NumIndices) {
1304 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001305 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001306 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001307 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1308 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001309}
1310
Sean Huntcbb67482011-01-08 20:30:50 +00001311TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001312 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001313 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001314 else
1315 return TypeLoc();
1316}
1317
Sean Huntcbb67482011-01-08 20:30:50 +00001318const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001319 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001320 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001321 else
1322 return 0;
1323}
1324
Sean Huntcbb67482011-01-08 20:30:50 +00001325SourceLocation CXXCtorInitializer::getSourceLocation() const {
Sean Hunt41717662011-02-26 19:13:13 +00001326 if (isAnyMemberInitializer() || isDelegatingInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001327 return getMemberLocation();
1328
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001329 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001330}
1331
Sean Huntcbb67482011-01-08 20:30:50 +00001332SourceRange CXXCtorInitializer::getSourceRange() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001333 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001334}
1335
Douglas Gregorb48fe382008-10-31 09:07:45 +00001336CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001337CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001338 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Sean Hunt5f802e52011-05-06 00:11:07 +00001339 QualType(), 0, false, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001340}
1341
1342CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001343CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001344 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001345 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001346 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001347 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001348 bool isInline,
Sean Hunt5f802e52011-05-06 00:11:07 +00001349 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001350 assert(NameInfo.getName().getNameKind()
1351 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001352 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001353 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Sean Hunt5f802e52011-05-06 00:11:07 +00001354 isExplicit, isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001355}
1356
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001357bool CXXConstructorDecl::isDefaultConstructor() const {
1358 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001359 // A default constructor for a class X is a constructor of class
1360 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001361 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001362 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001363}
1364
Mike Stump1eb44332009-09-09 15:08:12 +00001365bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001366CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001367 return isCopyOrMoveConstructor(TypeQuals) &&
1368 getParamDecl(0)->getType()->isLValueReferenceType();
1369}
1370
1371bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1372 return isCopyOrMoveConstructor(TypeQuals) &&
1373 getParamDecl(0)->getType()->isRValueReferenceType();
1374}
1375
1376/// \brief Determine whether this is a copy or move constructor.
1377bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001378 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001379 // A non-template constructor for class X is a copy constructor
1380 // if its first parameter is of type X&, const X&, volatile X& or
1381 // const volatile X&, and either there are no other parameters
1382 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001383 // C++0x [class.copy]p3:
1384 // A non-template constructor for class X is a move constructor if its
1385 // first parameter is of type X&&, const X&&, volatile X&&, or
1386 // const volatile X&&, and either there are no other parameters or else
1387 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001388 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001389 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001390 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001391 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001392 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001393
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001394 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001395
1396 // Do we have a reference type?
1397 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001398 if (!ParamRefType)
1399 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001400
Douglas Gregorfd476482009-11-13 23:59:09 +00001401 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001402 ASTContext &Context = getASTContext();
1403
Douglas Gregorfd476482009-11-13 23:59:09 +00001404 CanQualType PointeeType
1405 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001406 CanQualType ClassTy
1407 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001408 if (PointeeType.getUnqualifiedType() != ClassTy)
1409 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001410
John McCall0953e762009-09-24 19:53:00 +00001411 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001412
1413 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001414 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001415 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001416}
1417
Anders Carlssonfaccd722009-08-28 16:57:08 +00001418bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001419 // C++ [class.conv.ctor]p1:
1420 // A constructor declared without the function-specifier explicit
1421 // that can be called with a single parameter specifies a
1422 // conversion from the type of its first parameter to the type of
1423 // its class. Such a constructor is called a converting
1424 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001425 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001426 return false;
1427
Mike Stump1eb44332009-09-09 15:08:12 +00001428 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001429 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001430 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001431 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001432}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001433
Douglas Gregor6493cc52010-11-08 17:16:59 +00001434bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001435 if ((getNumParams() < 1) ||
1436 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1437 (getPrimaryTemplate() == 0) ||
1438 (getDescribedFunctionTemplate() != 0))
1439 return false;
1440
1441 const ParmVarDecl *Param = getParamDecl(0);
1442
1443 ASTContext &Context = getASTContext();
1444 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1445
Douglas Gregor66724ea2009-11-14 01:20:54 +00001446 // Is it the same as our our class type?
1447 CanQualType ClassTy
1448 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1449 if (ParamType.getUnqualifiedType() != ClassTy)
1450 return false;
1451
1452 return true;
1453}
1454
Sebastian Redlf677ea32011-02-05 19:23:19 +00001455const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1456 // Hack: we store the inherited constructor in the overridden method table
1457 method_iterator It = begin_overridden_methods();
1458 if (It == end_overridden_methods())
1459 return 0;
1460
1461 return cast<CXXConstructorDecl>(*It);
1462}
1463
1464void
1465CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1466 // Hack: we store the inherited constructor in the overridden method table
1467 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1468 addOverriddenMethod(BaseCtor);
1469}
1470
Douglas Gregor42a552f2008-11-05 20:51:48 +00001471CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001472CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001473 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001474 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001475}
1476
1477CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001478CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001479 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001480 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001481 QualType T, TypeSourceInfo *TInfo,
1482 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001483 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001484 assert(NameInfo.getName().getNameKind()
1485 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001486 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001487 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001488 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001489}
1490
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001491CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001492CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001493 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001494 QualType(), 0, false, false,
1495 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001496}
1497
1498CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001499CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001500 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001501 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001502 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001503 bool isInline, bool isExplicit,
1504 SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001505 assert(NameInfo.getName().getNameKind()
1506 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001507 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001508 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001509 isInline, isExplicit, EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001510}
1511
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001512LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001513 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001514 SourceLocation ExternLoc,
1515 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001516 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001517 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001518 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001519}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001520
1521UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1522 SourceLocation L,
1523 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001524 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001525 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001526 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001527 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001528 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1529 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001530 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1531 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001532}
1533
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001534NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1535 if (NamespaceAliasDecl *NA =
1536 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1537 return NA->getNamespace();
1538 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1539}
1540
Mike Stump1eb44332009-09-09 15:08:12 +00001541NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001542 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001543 SourceLocation AliasLoc,
1544 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001545 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001546 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001547 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001548 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1549 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001550 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1551 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001552}
1553
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001554UsingDecl *UsingShadowDecl::getUsingDecl() const {
1555 const UsingShadowDecl *Shadow = this;
1556 while (const UsingShadowDecl *NextShadow =
1557 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1558 Shadow = NextShadow;
1559 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1560}
1561
1562void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1563 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1564 "declaration already in set");
1565 assert(S->getUsingDecl() == this);
1566
1567 if (FirstUsingShadow)
1568 S->UsingOrNextShadow = FirstUsingShadow;
1569 FirstUsingShadow = S;
1570}
1571
1572void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1573 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1574 "declaration not in set");
1575 assert(S->getUsingDecl() == this);
1576
1577 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1578
1579 if (FirstUsingShadow == S) {
1580 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1581 S->UsingOrNextShadow = this;
1582 return;
1583 }
1584
1585 UsingShadowDecl *Prev = FirstUsingShadow;
1586 while (Prev->UsingOrNextShadow != S)
1587 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1588 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1589 S->UsingOrNextShadow = this;
1590}
1591
Douglas Gregordc355712011-02-25 00:36:19 +00001592UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1593 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001594 const DeclarationNameInfo &NameInfo,
1595 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001596 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001597}
1598
John McCall7ba107a2009-11-18 02:36:19 +00001599UnresolvedUsingValueDecl *
1600UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1601 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001602 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001603 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001604 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001605 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001606}
1607
1608UnresolvedUsingTypenameDecl *
1609UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1610 SourceLocation UsingLoc,
1611 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001612 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001613 SourceLocation TargetNameLoc,
1614 DeclarationName TargetName) {
1615 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001616 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001617 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001618}
1619
Anders Carlssonfb311762009-03-14 00:25:26 +00001620StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001621 SourceLocation StaticAssertLoc,
1622 Expr *AssertExpr,
1623 StringLiteral *Message,
1624 SourceLocation RParenLoc) {
1625 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1626 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001627}
1628
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001629static const char *getAccessName(AccessSpecifier AS) {
1630 switch (AS) {
1631 default:
1632 case AS_none:
1633 assert("Invalid access specifier!");
1634 return 0;
1635 case AS_public:
1636 return "public";
1637 case AS_private:
1638 return "private";
1639 case AS_protected:
1640 return "protected";
1641 }
1642}
1643
1644const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1645 AccessSpecifier AS) {
1646 return DB << getAccessName(AS);
1647}