blob: a71750bb4c8cc0e3b2f8af98ed4cab78286fb6f0 [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),
34 Abstract(false), HasTrivialConstructor(true),
Chandler Carruth9b6347c2011-04-24 02:49:34 +000035 HasConstExprNonCopyMoveConstructor(false),
Chandler Carruth4d6e5a22011-04-23 23:10:33 +000036 HasTrivialCopyConstructor(true), HasTrivialMoveConstructor(true),
37 HasTrivialCopyAssignment(true), HasTrivialMoveAssignment(true),
Chandler Carruth9b6347c2011-04-24 02:49:34 +000038 HasTrivialDestructor(true), HasNonLiteralTypeFieldsOrBases(false),
39 ComputedVisibleConversions(false),
Douglas Gregor18274032010-07-03 00:47:00 +000040 DeclaredDefaultConstructor(false), DeclaredCopyConstructor(false),
Douglas Gregora376d102010-07-02 21:50:04 +000041 DeclaredCopyAssignment(false), DeclaredDestructor(false),
Anders Carlssoncb88a1f2011-01-24 16:26:15 +000042 NumBases(0), NumVBases(0), Bases(), VBases(),
43 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000044}
45
46CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000047 SourceLocation StartLoc, SourceLocation IdLoc,
48 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
49 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000050 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000051 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000052
Jay Foad4ba2a172011-01-12 09:06:06 +000053CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000054 DeclContext *DC, SourceLocation StartLoc,
55 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000056 CXXRecordDecl* PrevDecl,
57 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000058 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
59 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000060
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000061 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000062 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000063 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000064 return R;
65}
66
Jay Foad4ba2a172011-01-12 09:06:06 +000067CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000068 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
69 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000070}
71
Mike Stump1eb44332009-09-09 15:08:12 +000072void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000073CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000074 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000075 ASTContext &C = getASTContext();
76
Mike Stump1eb44332009-09-09 15:08:12 +000077 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000078 // An aggregate is an array or a class (clause 9) with [...]
79 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000080 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000081
Douglas Gregor7c789c12010-10-29 22:39:52 +000082 if (!data().Bases.isOffset() && data().NumBases > 0)
83 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000084
Anders Carlsson6f6de732010-03-29 05:13:12 +000085 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000086 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000087
88 // The virtual bases of this class.
89 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000090
John McCall86ff3082010-02-04 22:26:26 +000091 data().Bases = new(C) CXXBaseSpecifier [NumBases];
92 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000093 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +000094 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000095 // Keep track of inherited vbases for this base class.
96 const CXXBaseSpecifier *Base = Bases[i];
97 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +000098 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000099 if (BaseType->isDependentType())
100 continue;
101 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000102 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000103
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000104 // C++ [dcl.init.aggr]p1:
105 // An aggregate is [...] a class with [...] no base classes [...].
106 data().Aggregate = false;
107
108 // C++ [class]p4:
109 // A POD-struct is an aggregate class...
110 data().PlainOldData = false;
111
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000112 // A class with a non-empty base class is not empty.
113 // FIXME: Standard ref?
114 if (!BaseClassDecl->isEmpty())
115 data().Empty = false;
116
Douglas Gregor85606eb2010-09-28 20:50:54 +0000117 // C++ [class.virtual]p1:
118 // A class that declares or inherits a virtual function is called a
119 // polymorphic class.
120 if (BaseClassDecl->isPolymorphic())
121 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000122
123 // Record if this base is the first non-literal field or base.
124 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
125 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000126
Anders Carlsson6f6de732010-03-29 05:13:12 +0000127 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000128 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000129 BaseClassDecl->vbases_begin(),
130 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000131 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000132 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000133 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000134 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000135
136 if (Base->isVirtual()) {
137 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000138 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000139 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000140
141 // C++0x [meta.unary.prop] is_empty:
142 // T is a class type, but not a union type, with ... no virtual base
143 // classes
144 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000145
146 // C++ [class.ctor]p5:
147 // A constructor is trivial if its class has no virtual base classes.
148 data().HasTrivialConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000149
150 // C++0x [class.copy]p13:
151 // A copy/move constructor for class X is trivial if it is neither
152 // user-provided nor deleted and if
153 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000154 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000155 data().HasTrivialMoveConstructor = false;
156
157 // C++0x [class.copy]p27:
158 // A copy/move assignment operator for class X is trivial if it is
159 // neither user-provided nor deleted and if
160 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000161 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000162 data().HasTrivialMoveAssignment = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000163 } else {
164 // C++ [class.ctor]p5:
165 // A constructor is trivial if all the direct base classes of its
166 // class have trivial constructors.
167 if (!BaseClassDecl->hasTrivialConstructor())
168 data().HasTrivialConstructor = false;
169
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000170 // C++0x [class.copy]p13:
171 // A copy/move constructor for class X is trivial if [...]
172 // [...]
173 // -- the constructor selected to copy/move each direct base class
174 // subobject is trivial, and
175 // FIXME: C++0x: We need to only consider the selected constructor
176 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000177 if (!BaseClassDecl->hasTrivialCopyConstructor())
178 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000179 if (!BaseClassDecl->hasTrivialMoveConstructor())
180 data().HasTrivialMoveConstructor = false;
181
182 // C++0x [class.copy]p27:
183 // A copy/move assignment operator for class X is trivial if [...]
184 // [...]
185 // -- the assignment operator selected to copy/move each direct base
186 // class subobject is trivial, and
187 // FIXME: C++0x: We need to only consider the selected operator instead
188 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000189 if (!BaseClassDecl->hasTrivialCopyAssignment())
190 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000191 if (!BaseClassDecl->hasTrivialMoveAssignment())
192 data().HasTrivialMoveAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000193 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000194
195 // C++ [class.ctor]p3:
196 // A destructor is trivial if all the direct base classes of its class
197 // have trivial destructors.
198 if (!BaseClassDecl->hasTrivialDestructor())
199 data().HasTrivialDestructor = false;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000200 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000201
202 if (VBases.empty())
203 return;
204
205 // Create base specifier for any direct or indirect virtual bases.
206 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
207 data().NumVBases = VBases.size();
208 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000209 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
210
Anders Carlsson6f6de732010-03-29 05:13:12 +0000211 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000212 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000213 continue;
214
Nick Lewycky56062202010-07-26 16:56:01 +0000215 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
216 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000217
Douglas Gregor7c789c12010-10-29 22:39:52 +0000218 data().getVBases()[I] =
Anders Carlsson6f6de732010-03-29 05:13:12 +0000219 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000220 VBaseClassDecl->getTagKind() == TTK_Class,
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000221 VBases[I]->getAccessSpecifier(), VBaseTypeInfo,
222 SourceLocation());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000223 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000224}
225
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000226/// Callback function for CXXRecordDecl::forallBases that acknowledges
227/// that it saw a base class.
228static bool SawBase(const CXXRecordDecl *, void *) {
229 return true;
230}
231
232bool CXXRecordDecl::hasAnyDependentBases() const {
233 if (!isDependentContext())
234 return false;
235
236 return !forallBases(SawBase, 0);
237}
238
Jay Foad4ba2a172011-01-12 09:06:06 +0000239bool CXXRecordDecl::hasConstCopyConstructor(const ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000240 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000241}
242
Chandler Carruthb7e95892011-04-23 10:47:28 +0000243bool CXXRecordDecl::isTriviallyCopyable() const {
244 // C++0x [class]p5:
245 // A trivially copyable class is a class that:
246 // -- has no non-trivial copy constructors,
247 if (!hasTrivialCopyConstructor()) return false;
248 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000249 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000250 // -- has no non-trivial copy assignment operators,
251 if (!hasTrivialCopyAssignment()) return false;
252 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000253 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000254 // -- has a trivial destructor.
255 if (!hasTrivialDestructor()) return false;
256
257 return true;
258}
259
Douglas Gregor0d405db2010-07-01 20:59:04 +0000260/// \brief Perform a simplistic form of overload resolution that only considers
261/// cv-qualifiers on a single parameter, and return the best overload candidate
262/// (if there is one).
263static CXXMethodDecl *
264GetBestOverloadCandidateSimple(
265 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
266 if (Cands.empty())
267 return 0;
268 if (Cands.size() == 1)
269 return Cands[0].first;
270
271 unsigned Best = 0, N = Cands.size();
272 for (unsigned I = 1; I != N; ++I)
273 if (Cands[Best].second.isSupersetOf(Cands[I].second))
274 Best = I;
275
276 for (unsigned I = 1; I != N; ++I)
277 if (Cands[Best].second.isSupersetOf(Cands[I].second))
278 return 0;
279
280 return Cands[Best].first;
281}
282
Jay Foad4ba2a172011-01-12 09:06:06 +0000283CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(const ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000284 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000285 QualType ClassType
286 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000287 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000288 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000289 Context.getCanonicalType(ClassType));
290 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000291 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000292 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000293 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000294 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000295 // C++ [class.copy]p2:
296 // A non-template constructor for class X is a copy constructor if [...]
297 if (isa<FunctionTemplateDecl>(*Con))
298 continue;
299
Douglas Gregor0d405db2010-07-01 20:59:04 +0000300 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
301 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000302 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
303 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000304 Found.push_back(std::make_pair(
305 const_cast<CXXConstructorDecl *>(Constructor),
306 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000307 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000308 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000309
310 return cast_or_null<CXXConstructorDecl>(
311 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000312}
313
Douglas Gregorb87786f2010-07-01 17:48:08 +0000314CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
315 ASTContext &Context = getASTContext();
316 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
317 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
318
319 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
320 DeclContext::lookup_const_iterator Op, OpEnd;
321 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
322 // C++ [class.copy]p9:
323 // A user-declared copy assignment operator is a non-static non-template
324 // member function of class X with exactly one parameter of type X, X&,
325 // const X&, volatile X& or const volatile X&.
326 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
327 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
328 continue;
329
330 const FunctionProtoType *FnType
331 = Method->getType()->getAs<FunctionProtoType>();
332 assert(FnType && "Overloaded operator has no prototype.");
333 // Don't assert on this; an invalid decl might have been left in the AST.
334 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
335 continue;
336
337 QualType ArgType = FnType->getArgType(0);
338 Qualifiers Quals;
339 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
340 ArgType = Ref->getPointeeType();
341 // If we have a const argument and we have a reference to a non-const,
342 // this function does not match.
343 if (ArgIsConst && !ArgType.isConstQualified())
344 continue;
345
346 Quals = ArgType.getQualifiers();
347 } else {
348 // By-value copy-assignment operators are treated like const X&
349 // copy-assignment operators.
350 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
351 }
352
353 if (!Context.hasSameUnqualifiedType(ArgType, Class))
354 continue;
355
356 // Save this copy-assignment operator. It might be "the one".
357 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
358 }
359
360 // Use a simplistic form of overload resolution to find the candidate.
361 return GetBestOverloadCandidateSimple(Found);
362}
363
Douglas Gregor21386642010-09-28 21:55:22 +0000364void CXXRecordDecl::markedVirtualFunctionPure() {
365 // C++ [class.abstract]p2:
366 // A class is abstract if it has at least one pure virtual function.
367 data().Abstract = true;
368}
369
370void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000371 // Ignore friends and invalid declarations.
372 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000373 return;
374
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000375 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
376 if (FunTmpl)
377 D = FunTmpl->getTemplatedDecl();
378
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000379 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
380 if (Method->isVirtual()) {
381 // C++ [dcl.init.aggr]p1:
382 // An aggregate is an array or a class with [...] no virtual functions.
383 data().Aggregate = false;
384
385 // C++ [class]p4:
386 // A POD-struct is an aggregate class...
387 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000388
389 // Virtual functions make the class non-empty.
390 // FIXME: Standard ref?
391 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000392
393 // C++ [class.virtual]p1:
394 // A class that declares or inherits a virtual function is called a
395 // polymorphic class.
396 data().Polymorphic = true;
397
398 // None of the special member functions are trivial.
399 data().HasTrivialConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000400
401 // C++0x [class.copy]p13:
402 // A copy/move constructor for class X is trivial if [...]
403 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000404 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000405 data().HasTrivialMoveConstructor = false;
406
407 // C++0x [class.copy]p27:
408 // A copy/move assignment operator for class X is trivial if [...]
409 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000410 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000411 data().HasTrivialMoveAssignment = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000412 // FIXME: Destructor?
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000413 }
414 }
415
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000416 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000417 // Notify that an implicit member was added after the definition
418 // was completed.
419 if (!isBeingDefined())
420 if (ASTMutationListener *L = getASTMutationListener())
421 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000422
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000423 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
424 // If this is the implicit default constructor, note that we have now
425 // declared it.
426 if (Constructor->isDefaultConstructor())
427 data().DeclaredDefaultConstructor = true;
428 // If this is the implicit copy constructor, note that we have now
429 // declared it.
430 else if (Constructor->isCopyConstructor())
431 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000432 return;
433 }
434
435 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000436 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000437 return;
438 }
439
440 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000441 // If this is the implicit copy constructor, note that we have now
442 // declared it.
443 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000444 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000445 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000446 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000447 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000448
449 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000450 }
451
452 // Handle (user-declared) constructors.
453 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
454 // Note that we have a user-declared constructor.
455 data().UserDeclaredConstructor = true;
456
457 // Note that we have no need of an implicitly-declared default constructor.
458 data().DeclaredDefaultConstructor = true;
459
460 // C++ [dcl.init.aggr]p1:
461 // An aggregate is an array or a class (clause 9) with no
462 // user-declared constructors (12.1) [...].
463 data().Aggregate = false;
464
465 // C++ [class]p4:
466 // A POD-struct is an aggregate class [...]
467 data().PlainOldData = false;
468
469 // C++ [class.ctor]p5:
470 // A constructor is trivial if it is an implicitly-declared default
471 // constructor.
472 // FIXME: C++0x: don't do this for "= default" default constructors.
473 data().HasTrivialConstructor = false;
474
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000475 // Note when we have a user-declared copy or move constructor, which will
476 // suppress the implicit declaration of those constructors.
477 if (!FunTmpl) {
478 if (Constructor->isCopyConstructor()) {
479 data().UserDeclaredCopyConstructor = true;
480 data().DeclaredCopyConstructor = true;
481
482 // C++0x [class.copy]p13:
483 // A copy/move constructor for class X is trivial if it is neither
484 // user-provided nor deleted
485 // FIXME: C++0x: don't do this for "= default" copy constructors.
486 data().HasTrivialCopyConstructor = false;
487 } else if (Constructor->isMoveConstructor()) {
488 // C++0x [class.copy]p13:
489 // A copy/move constructor for class X is trivial if it is neither
490 // user-provided nor deleted
491 // FIXME: C++0x: don't do this for "= default" move constructors.
492 data().HasTrivialMoveConstructor = false;
493 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000494 }
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000495 if (Constructor->isConstExpr() &&
496 !Constructor->isCopyOrMoveConstructor()) {
497 // Record if we see any constexpr constructors which are niether copy
498 // nor move constructors.
499 data().HasConstExprNonCopyMoveConstructor = true;
500 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000501
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000502 return;
503 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000504
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000505 // Handle (user-declared) destructors.
506 if (isa<CXXDestructorDecl>(D)) {
507 data().DeclaredDestructor = true;
508 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000509
510 // C++ [class]p4:
511 // A POD-struct is an aggregate class that has [...] no user-defined
512 // destructor.
513 data().PlainOldData = false;
514
Douglas Gregor85606eb2010-09-28 20:50:54 +0000515 // C++ [class.dtor]p3:
516 // A destructor is trivial if it is an implicitly-declared destructor and
517 // [...].
518 //
519 // FIXME: C++0x: don't do this for "= default" destructors
520 data().HasTrivialDestructor = false;
521
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000522 return;
523 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000524
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000525 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000526 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
527 if (Method->getOverloadedOperator() == OO_Equal) {
528 // We're interested specifically in copy assignment operators.
529 const FunctionProtoType *FnType
530 = Method->getType()->getAs<FunctionProtoType>();
531 assert(FnType && "Overloaded operator has no proto function type.");
532 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
533
534 // Copy assignment operators must be non-templates.
535 if (Method->getPrimaryTemplate() || FunTmpl)
536 return;
537
538 ASTContext &Context = getASTContext();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000539 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
540 const_cast<CXXRecordDecl*>(this)));
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000541
542 bool isRValueRefArg = false;
543 QualType ArgType = FnType->getArgType(0);
544 if (const LValueReferenceType *Ref =
545 ArgType->getAs<LValueReferenceType>()) {
546 ArgType = Ref->getPointeeType();
547 } else if (const RValueReferenceType *Ref =
548 ArgType->getAs<RValueReferenceType>()) {
549 ArgType = Ref->getPointeeType();
550 isRValueRefArg = true;
551 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000552 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
553 return;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000554
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000555 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000556 // A POD-struct is an aggregate class that [...] has no user-defined
557 // copy assignment operator [...].
558 // FIXME: This should be probably determined dynamically in terms of
559 // other more precise attributes to correctly model how it is specified
560 // in C++0x. Setting it here happens to do the right thing.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000561 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000562
563 if (!isRValueRefArg) {
564 // This is a copy assignment operator.
565
566 // Suppress the implicit declaration of a copy constructor.
567 data().UserDeclaredCopyAssignment = true;
568 data().DeclaredCopyAssignment = true;
569
570 // C++0x [class.copy]p27:
571 // A copy/move assignment operator for class X is trivial if it is
572 // neither user-provided nor deleted [...]
573 // FIXME: C++0x: don't do this for "= default" copy operators.
574 data().HasTrivialCopyAssignment = false;
575 } else {
576 // This is a move assignment operator.
577
578 // C++0x [class.copy]p27:
579 // A copy/move assignment operator for class X is trivial if it is
580 // neither user-provided nor deleted [...]
581 // FIXME: C++0x: don't do this for "= default" copy operators.
582 data().HasTrivialMoveAssignment = false;
583 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000584 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000585
Douglas Gregore80622f2010-09-29 04:25:11 +0000586 // Keep the list of conversion functions up-to-date.
587 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
588 // We don't record specializations.
589 if (Conversion->getPrimaryTemplate())
590 return;
591
592 // FIXME: We intentionally don't use the decl's access here because it
593 // hasn't been set yet. That's really just a misdesign in Sema.
594
595 if (FunTmpl) {
596 if (FunTmpl->getPreviousDeclaration())
597 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
598 FunTmpl);
599 else
600 data().Conversions.addDecl(FunTmpl);
601 } else {
602 if (Conversion->getPreviousDeclaration())
603 data().Conversions.replace(Conversion->getPreviousDeclaration(),
604 Conversion);
605 else
606 data().Conversions.addDecl(Conversion);
607 }
608 }
609
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000610 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000611 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000612
613 // Handle non-static data members.
614 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
615 // C++ [dcl.init.aggr]p1:
616 // An aggregate is an array or a class (clause 9) with [...] no
617 // private or protected non-static data members (clause 11).
618 //
619 // A POD must be an aggregate.
620 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
621 data().Aggregate = false;
622 data().PlainOldData = false;
623 }
624
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000625 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000626 // A POD struct is a class that is both a trivial class and a
627 // standard-layout class, and has no non-static data members of type
628 // non-POD struct, non-POD union (or array of such types).
629 ASTContext &Context = getASTContext();
630 QualType T = Context.getBaseElementType(Field->getType());
631 if (!T->isPODType())
632 data().PlainOldData = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000633 if (T->isReferenceType())
634 data().HasTrivialConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000635
636 // Record if this field is the first non-literal field or base.
637 if (!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType())
638 data().HasNonLiteralTypeFieldsOrBases = true;
639
Douglas Gregor85606eb2010-09-28 20:50:54 +0000640 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
641 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
642 if (FieldRec->getDefinition()) {
643 if (!FieldRec->hasTrivialConstructor())
644 data().HasTrivialConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000645
646 // C++0x [class.copy]p13:
647 // A copy/move constructor for class X is trivial if [...]
648 // [...]
649 // -- for each non-static data member of X that is of class type (or
650 // an array thereof), the constructor selected to copy/move that
651 // member is trivial;
652 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000653 if (!FieldRec->hasTrivialCopyConstructor())
654 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000655 if (!FieldRec->hasTrivialMoveConstructor())
656 data().HasTrivialMoveConstructor = false;
657
658 // C++0x [class.copy]p27:
659 // A copy/move assignment operator for class X is trivial if [...]
660 // [...]
661 // -- for each non-static data member of X that is of class type (or
662 // an array thereof), the assignment operator selected to
663 // copy/move that member is trivial;
664 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000665 if (!FieldRec->hasTrivialCopyAssignment())
666 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000667 if (!FieldRec->hasTrivialMoveAssignment())
668 data().HasTrivialMoveAssignment = false;
669
Douglas Gregor85606eb2010-09-28 20:50:54 +0000670 if (!FieldRec->hasTrivialDestructor())
671 data().HasTrivialDestructor = false;
672 }
673 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000674
675 // If this is not a zero-length bit-field, then the class is not empty.
676 if (data().Empty) {
677 if (!Field->getBitWidth())
678 data().Empty = false;
679 else if (!Field->getBitWidth()->isTypeDependent() &&
680 !Field->getBitWidth()->isValueDependent()) {
681 llvm::APSInt Bits;
682 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
683 if (!!Bits)
684 data().Empty = false;
685 }
686 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000687 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000688
689 // Handle using declarations of conversion functions.
690 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
691 if (Shadow->getDeclName().getNameKind()
692 == DeclarationName::CXXConversionFunctionName)
693 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000694}
695
John McCallb05b5f32010-03-15 09:07:48 +0000696static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
697 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000698 if (isa<UsingShadowDecl>(Conv))
699 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000700 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
701 T = ConvTemp->getTemplatedDecl()->getResultType();
702 else
703 T = cast<CXXConversionDecl>(Conv)->getConversionType();
704 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000705}
706
John McCallb05b5f32010-03-15 09:07:48 +0000707/// Collect the visible conversions of a base class.
708///
709/// \param Base a base class of the class we're considering
710/// \param InVirtual whether this base class is a virtual base (or a base
711/// of a virtual base)
712/// \param Access the access along the inheritance path to this base
713/// \param ParentHiddenTypes the conversions provided by the inheritors
714/// of this base
715/// \param Output the set to which to add conversions from non-virtual bases
716/// \param VOutput the set to which to add conversions from virtual bases
717/// \param HiddenVBaseCs the set of conversions which were hidden in a
718/// virtual base along some inheritance path
719static void CollectVisibleConversions(ASTContext &Context,
720 CXXRecordDecl *Record,
721 bool InVirtual,
722 AccessSpecifier Access,
723 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
724 UnresolvedSetImpl &Output,
725 UnresolvedSetImpl &VOutput,
726 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
727 // The set of types which have conversions in this class or its
728 // subclasses. As an optimization, we don't copy the derived set
729 // unless it might change.
730 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
731 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
732
733 // Collect the direct conversions and figure out which conversions
734 // will be hidden in the subclasses.
735 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
736 if (!Cs.empty()) {
737 HiddenTypesBuffer = ParentHiddenTypes;
738 HiddenTypes = &HiddenTypesBuffer;
739
740 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
741 bool Hidden =
742 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
743
744 // If this conversion is hidden and we're in a virtual base,
745 // remember that it's hidden along some inheritance path.
746 if (Hidden && InVirtual)
747 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
748
749 // If this conversion isn't hidden, add it to the appropriate output.
750 else if (!Hidden) {
751 AccessSpecifier IAccess
752 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
753
754 if (InVirtual)
755 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000756 else
John McCallb05b5f32010-03-15 09:07:48 +0000757 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000758 }
759 }
760 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000761
John McCallb05b5f32010-03-15 09:07:48 +0000762 // Collect information recursively from any base classes.
763 for (CXXRecordDecl::base_class_iterator
764 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
765 const RecordType *RT = I->getType()->getAs<RecordType>();
766 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000767
John McCallb05b5f32010-03-15 09:07:48 +0000768 AccessSpecifier BaseAccess
769 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
770 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000771
John McCallb05b5f32010-03-15 09:07:48 +0000772 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
773 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
774 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000775 }
John McCallb05b5f32010-03-15 09:07:48 +0000776}
Sebastian Redl9994a342009-10-25 17:03:50 +0000777
John McCallb05b5f32010-03-15 09:07:48 +0000778/// Collect the visible conversions of a class.
779///
780/// This would be extremely straightforward if it weren't for virtual
781/// bases. It might be worth special-casing that, really.
782static void CollectVisibleConversions(ASTContext &Context,
783 CXXRecordDecl *Record,
784 UnresolvedSetImpl &Output) {
785 // The collection of all conversions in virtual bases that we've
786 // found. These will be added to the output as long as they don't
787 // appear in the hidden-conversions set.
788 UnresolvedSet<8> VBaseCs;
789
790 // The set of conversions in virtual bases that we've determined to
791 // be hidden.
792 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
793
794 // The set of types hidden by classes derived from this one.
795 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
796
797 // Go ahead and collect the direct conversions and add them to the
798 // hidden-types set.
799 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
800 Output.append(Cs.begin(), Cs.end());
801 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
802 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
803
804 // Recursively collect conversions from base classes.
805 for (CXXRecordDecl::base_class_iterator
806 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
807 const RecordType *RT = I->getType()->getAs<RecordType>();
808 if (!RT) continue;
809
810 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
811 I->isVirtual(), I->getAccessSpecifier(),
812 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
813 }
814
815 // Add any unhidden conversions provided by virtual bases.
816 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
817 I != E; ++I) {
818 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
819 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000820 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000821}
822
823/// getVisibleConversionFunctions - get all conversion functions visible
824/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000825const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000826 // If root class, all conversions are visible.
827 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000828 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000829 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000830 if (data().ComputedVisibleConversions)
831 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000832 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000833 data().ComputedVisibleConversions = true;
834 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000835}
836
John McCall32daa422010-03-31 01:36:47 +0000837void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
838 // This operation is O(N) but extremely rare. Sema only uses it to
839 // remove UsingShadowDecls in a class that were followed by a direct
840 // declaration, e.g.:
841 // class A : B {
842 // using B::operator int;
843 // operator int();
844 // };
845 // This is uncommon by itself and even more uncommon in conjunction
846 // with sufficiently large numbers of directly-declared conversions
847 // that asymptotic behavior matters.
848
849 UnresolvedSetImpl &Convs = *getConversionFunctions();
850 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
851 if (Convs[I].getDecl() == ConvDecl) {
852 Convs.erase(I);
853 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
854 && "conversion was found multiple times in unresolved set");
855 return;
856 }
857 }
858
859 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000860}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000861
Douglas Gregorf6b11852009-10-08 15:14:33 +0000862CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000863 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000864 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
865
866 return 0;
867}
868
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000869MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
870 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
871}
872
Douglas Gregorf6b11852009-10-08 15:14:33 +0000873void
874CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
875 TemplateSpecializationKind TSK) {
876 assert(TemplateOrInstantiation.isNull() &&
877 "Previous template or instantiation?");
878 assert(!isa<ClassTemplateSpecializationDecl>(this));
879 TemplateOrInstantiation
880 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
881}
882
Anders Carlssonb13e3572009-12-07 06:33:48 +0000883TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
884 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000885 = dyn_cast<ClassTemplateSpecializationDecl>(this))
886 return Spec->getSpecializationKind();
887
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000888 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000889 return MSInfo->getTemplateSpecializationKind();
890
891 return TSK_Undeclared;
892}
893
894void
895CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
896 if (ClassTemplateSpecializationDecl *Spec
897 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
898 Spec->setSpecializationKind(TSK);
899 return;
900 }
901
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000902 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000903 MSInfo->setTemplateSpecializationKind(TSK);
904 return;
905 }
906
907 assert(false && "Not a class template or member class specialization");
908}
909
Douglas Gregor1d110e02010-07-01 14:13:13 +0000910CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
911 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000912 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000913
914 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000915 = Context.DeclarationNames.getCXXDestructorName(
916 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000917
John McCallc0bf4622010-02-23 00:48:20 +0000918 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000919 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000920 if (I == E)
921 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000923 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000924 return Dtor;
925}
926
Douglas Gregorda2142f2011-02-19 18:51:44 +0000927void CXXRecordDecl::completeDefinition() {
928 completeDefinition(0);
929}
930
931void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
932 RecordDecl::completeDefinition();
933
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000934 // If the class may be abstract (but hasn't been marked as such), check for
935 // any pure final overriders.
936 if (mayBeAbstract()) {
937 CXXFinalOverriderMap MyFinalOverriders;
938 if (!FinalOverriders) {
939 getFinalOverriders(MyFinalOverriders);
940 FinalOverriders = &MyFinalOverriders;
941 }
942
943 bool Done = false;
944 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
945 MEnd = FinalOverriders->end();
946 M != MEnd && !Done; ++M) {
947 for (OverridingMethods::iterator SO = M->second.begin(),
948 SOEnd = M->second.end();
949 SO != SOEnd && !Done; ++SO) {
950 assert(SO->second.size() > 0 &&
951 "All virtual functions have overridding virtual functions");
952
953 // C++ [class.abstract]p4:
954 // A class is abstract if it contains or inherits at least one
955 // pure virtual function for which the final overrider is pure
956 // virtual.
957 if (SO->second.front().Method->isPure()) {
958 data().Abstract = true;
959 Done = true;
960 break;
961 }
962 }
963 }
964 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000965
966 // Set access bits correctly on the directly-declared conversions.
967 for (UnresolvedSetIterator I = data().Conversions.begin(),
968 E = data().Conversions.end();
969 I != E; ++I)
970 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000971}
972
973bool CXXRecordDecl::mayBeAbstract() const {
974 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
975 isDependentContext())
976 return false;
977
978 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
979 BEnd = bases_end();
980 B != BEnd; ++B) {
981 CXXRecordDecl *BaseDecl
982 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
983 if (BaseDecl->isAbstract())
984 return true;
985 }
986
987 return false;
988}
989
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000990CXXMethodDecl *
991CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000992 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000993 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000994 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +0000995 bool isStatic, StorageClass SCAsWritten, bool isInline,
996 SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000997 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +0000998 isStatic, SCAsWritten, isInline, EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000999}
1000
Douglas Gregor90916562009-09-29 18:16:17 +00001001bool CXXMethodDecl::isUsualDeallocationFunction() const {
1002 if (getOverloadedOperator() != OO_Delete &&
1003 getOverloadedOperator() != OO_Array_Delete)
1004 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001005
1006 // C++ [basic.stc.dynamic.deallocation]p2:
1007 // A template instance is never a usual deallocation function,
1008 // regardless of its signature.
1009 if (getPrimaryTemplate())
1010 return false;
1011
Douglas Gregor90916562009-09-29 18:16:17 +00001012 // C++ [basic.stc.dynamic.deallocation]p2:
1013 // If a class T has a member deallocation function named operator delete
1014 // with exactly one parameter, then that function is a usual (non-placement)
1015 // deallocation function. [...]
1016 if (getNumParams() == 1)
1017 return true;
1018
1019 // C++ [basic.stc.dynamic.deallocation]p2:
1020 // [...] If class T does not declare such an operator delete but does
1021 // declare a member deallocation function named operator delete with
1022 // exactly two parameters, the second of which has type std::size_t (18.1),
1023 // then this function is a usual deallocation function.
1024 ASTContext &Context = getASTContext();
1025 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001026 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1027 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001028 return false;
1029
1030 // This function is a usual deallocation function if there are no
1031 // single-parameter deallocation functions of the same kind.
1032 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1033 R.first != R.second; ++R.first) {
1034 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1035 if (FD->getNumParams() == 1)
1036 return false;
1037 }
1038
1039 return true;
1040}
1041
Douglas Gregor06a9f362010-05-01 20:49:11 +00001042bool CXXMethodDecl::isCopyAssignmentOperator() const {
1043 // C++0x [class.copy]p19:
1044 // A user-declared copy assignment operator X::operator= is a non-static
1045 // non-template member function of class X with exactly one parameter of
1046 // type X, X&, const X&, volatile X& or const volatile X&.
1047 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1048 /*non-static*/ isStatic() ||
1049 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1050 /*exactly one parameter*/getNumParams() != 1)
1051 return false;
1052
1053 QualType ParamType = getParamDecl(0)->getType();
1054 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1055 ParamType = Ref->getPointeeType();
1056
1057 ASTContext &Context = getASTContext();
1058 QualType ClassType
1059 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1060 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1061}
1062
Anders Carlsson05eb2442009-05-16 23:58:37 +00001063void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001064 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001065 assert(!MD->getParent()->isDependentContext() &&
1066 "Can't add an overridden method to a class template!");
1067
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001068 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001069}
1070
1071CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001072 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001073}
1074
1075CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001076 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001077}
1078
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001079unsigned CXXMethodDecl::size_overridden_methods() const {
1080 return getASTContext().overridden_methods_size(this);
1081}
1082
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001083QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001084 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1085 // If the member function is declared const, the type of this is const X*,
1086 // if the member function is declared volatile, the type of this is
1087 // volatile X*, and if the member function is declared const volatile,
1088 // the type of this is const volatile X*.
1089
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001090 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001091
John McCall3cb0ebd2010-03-10 03:28:59 +00001092 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001093 ClassTy = C.getQualifiedType(ClassTy,
1094 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001095 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001096}
1097
Eli Friedmand7d7f672009-12-06 20:50:05 +00001098bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001099 // If this function is a template instantiation, look at the template from
1100 // which it was instantiated.
1101 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1102 if (!CheckFn)
1103 CheckFn = this;
1104
Eli Friedmand7d7f672009-12-06 20:50:05 +00001105 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001106 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001107}
1108
Sean Huntcbb67482011-01-08 20:30:50 +00001109CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1110 TypeSourceInfo *TInfo, bool IsVirtual,
1111 SourceLocation L, Expr *Init,
1112 SourceLocation R,
1113 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001114 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001115 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1116 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001117{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001118}
1119
Sean Huntcbb67482011-01-08 20:30:50 +00001120CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1121 FieldDecl *Member,
1122 SourceLocation MemberLoc,
1123 SourceLocation L, Expr *Init,
1124 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001125 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001126 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1127 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1128{
1129}
1130
Sean Huntcbb67482011-01-08 20:30:50 +00001131CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1132 IndirectFieldDecl *Member,
1133 SourceLocation MemberLoc,
1134 SourceLocation L, Expr *Init,
1135 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001136 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001137 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001138 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001139{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001140}
1141
Sean Huntcbb67482011-01-08 20:30:50 +00001142CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Hunt41717662011-02-26 19:13:13 +00001143 SourceLocation D, SourceLocation L,
1144 CXXConstructorDecl *Target, Expr *Init,
1145 SourceLocation R)
1146 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1147 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1148 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1149{
1150}
1151
1152CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001153 FieldDecl *Member,
1154 SourceLocation MemberLoc,
1155 SourceLocation L, Expr *Init,
1156 SourceLocation R,
1157 VarDecl **Indices,
1158 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001159 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001160 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001161 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001162{
1163 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1164 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1165}
1166
Sean Huntcbb67482011-01-08 20:30:50 +00001167CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1168 FieldDecl *Member,
1169 SourceLocation MemberLoc,
1170 SourceLocation L, Expr *Init,
1171 SourceLocation R,
1172 VarDecl **Indices,
1173 unsigned NumIndices) {
1174 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001175 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001176 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001177 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1178 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001179}
1180
Sean Huntcbb67482011-01-08 20:30:50 +00001181TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001182 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001183 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001184 else
1185 return TypeLoc();
1186}
1187
Sean Huntcbb67482011-01-08 20:30:50 +00001188const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001189 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001190 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001191 else
1192 return 0;
1193}
1194
Sean Huntcbb67482011-01-08 20:30:50 +00001195SourceLocation CXXCtorInitializer::getSourceLocation() const {
Sean Hunt41717662011-02-26 19:13:13 +00001196 if (isAnyMemberInitializer() || isDelegatingInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001197 return getMemberLocation();
1198
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001199 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001200}
1201
Sean Huntcbb67482011-01-08 20:30:50 +00001202SourceRange CXXCtorInitializer::getSourceRange() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001203 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001204}
1205
Douglas Gregorb48fe382008-10-31 09:07:45 +00001206CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001207CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001208 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001209 QualType(), 0, false, false, false);
1210}
1211
1212CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001213CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001214 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001215 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001216 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001217 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001218 bool isInline,
1219 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001220 assert(NameInfo.getName().getNameKind()
1221 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001222 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001223 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
1224 isExplicit, isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001225}
1226
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001227bool CXXConstructorDecl::isDefaultConstructor() const {
1228 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001229 // A default constructor for a class X is a constructor of class
1230 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001231 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001232 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001233}
1234
Mike Stump1eb44332009-09-09 15:08:12 +00001235bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001236CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001237 return isCopyOrMoveConstructor(TypeQuals) &&
1238 getParamDecl(0)->getType()->isLValueReferenceType();
1239}
1240
1241bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1242 return isCopyOrMoveConstructor(TypeQuals) &&
1243 getParamDecl(0)->getType()->isRValueReferenceType();
1244}
1245
1246/// \brief Determine whether this is a copy or move constructor.
1247bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001248 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001249 // A non-template constructor for class X is a copy constructor
1250 // if its first parameter is of type X&, const X&, volatile X& or
1251 // const volatile X&, and either there are no other parameters
1252 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001253 // C++0x [class.copy]p3:
1254 // A non-template constructor for class X is a move constructor if its
1255 // first parameter is of type X&&, const X&&, volatile X&&, or
1256 // const volatile X&&, and either there are no other parameters or else
1257 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001258 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001259 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001260 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001261 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001262 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001263
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001264 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001265
1266 // Do we have a reference type?
1267 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001268 if (!ParamRefType)
1269 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001270
Douglas Gregorfd476482009-11-13 23:59:09 +00001271 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001272 ASTContext &Context = getASTContext();
1273
Douglas Gregorfd476482009-11-13 23:59:09 +00001274 CanQualType PointeeType
1275 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001276 CanQualType ClassTy
1277 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001278 if (PointeeType.getUnqualifiedType() != ClassTy)
1279 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001280
John McCall0953e762009-09-24 19:53:00 +00001281 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001282
1283 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001284 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001285 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001286}
1287
Anders Carlssonfaccd722009-08-28 16:57:08 +00001288bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001289 // C++ [class.conv.ctor]p1:
1290 // A constructor declared without the function-specifier explicit
1291 // that can be called with a single parameter specifies a
1292 // conversion from the type of its first parameter to the type of
1293 // its class. Such a constructor is called a converting
1294 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001295 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001296 return false;
1297
Mike Stump1eb44332009-09-09 15:08:12 +00001298 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001299 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001300 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001301 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001302}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001303
Douglas Gregor6493cc52010-11-08 17:16:59 +00001304bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001305 if ((getNumParams() < 1) ||
1306 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1307 (getPrimaryTemplate() == 0) ||
1308 (getDescribedFunctionTemplate() != 0))
1309 return false;
1310
1311 const ParmVarDecl *Param = getParamDecl(0);
1312
1313 ASTContext &Context = getASTContext();
1314 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1315
Douglas Gregor66724ea2009-11-14 01:20:54 +00001316 // Is it the same as our our class type?
1317 CanQualType ClassTy
1318 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1319 if (ParamType.getUnqualifiedType() != ClassTy)
1320 return false;
1321
1322 return true;
1323}
1324
Sebastian Redlf677ea32011-02-05 19:23:19 +00001325const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1326 // Hack: we store the inherited constructor in the overridden method table
1327 method_iterator It = begin_overridden_methods();
1328 if (It == end_overridden_methods())
1329 return 0;
1330
1331 return cast<CXXConstructorDecl>(*It);
1332}
1333
1334void
1335CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1336 // Hack: we store the inherited constructor in the overridden method table
1337 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1338 addOverriddenMethod(BaseCtor);
1339}
1340
Douglas Gregor42a552f2008-11-05 20:51:48 +00001341CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001342CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001343 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001344 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001345}
1346
1347CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001348CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001349 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001350 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001351 QualType T, TypeSourceInfo *TInfo,
1352 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001353 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001354 assert(NameInfo.getName().getNameKind()
1355 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001356 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001357 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001358 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001359}
1360
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001361CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001362CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001363 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001364 QualType(), 0, false, false,
1365 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001366}
1367
1368CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001369CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001370 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001371 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001372 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001373 bool isInline, bool isExplicit,
1374 SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001375 assert(NameInfo.getName().getNameKind()
1376 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001377 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001378 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001379 isInline, isExplicit, EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001380}
1381
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001382LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001383 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001384 SourceLocation ExternLoc,
1385 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001386 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001387 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001388 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001389}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001390
1391UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1392 SourceLocation L,
1393 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001394 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001395 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001396 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001397 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001398 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1399 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001400 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1401 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001402}
1403
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001404NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1405 if (NamespaceAliasDecl *NA =
1406 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1407 return NA->getNamespace();
1408 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1409}
1410
Mike Stump1eb44332009-09-09 15:08:12 +00001411NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001412 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001413 SourceLocation AliasLoc,
1414 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001415 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001416 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001417 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001418 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1419 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001420 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1421 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001422}
1423
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001424UsingDecl *UsingShadowDecl::getUsingDecl() const {
1425 const UsingShadowDecl *Shadow = this;
1426 while (const UsingShadowDecl *NextShadow =
1427 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1428 Shadow = NextShadow;
1429 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1430}
1431
1432void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1433 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1434 "declaration already in set");
1435 assert(S->getUsingDecl() == this);
1436
1437 if (FirstUsingShadow)
1438 S->UsingOrNextShadow = FirstUsingShadow;
1439 FirstUsingShadow = S;
1440}
1441
1442void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1443 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1444 "declaration not in set");
1445 assert(S->getUsingDecl() == this);
1446
1447 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1448
1449 if (FirstUsingShadow == S) {
1450 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1451 S->UsingOrNextShadow = this;
1452 return;
1453 }
1454
1455 UsingShadowDecl *Prev = FirstUsingShadow;
1456 while (Prev->UsingOrNextShadow != S)
1457 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1458 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1459 S->UsingOrNextShadow = this;
1460}
1461
Douglas Gregordc355712011-02-25 00:36:19 +00001462UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1463 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001464 const DeclarationNameInfo &NameInfo,
1465 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001466 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001467}
1468
John McCall7ba107a2009-11-18 02:36:19 +00001469UnresolvedUsingValueDecl *
1470UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1471 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001472 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001473 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001474 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001475 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001476}
1477
1478UnresolvedUsingTypenameDecl *
1479UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1480 SourceLocation UsingLoc,
1481 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001482 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001483 SourceLocation TargetNameLoc,
1484 DeclarationName TargetName) {
1485 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001486 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001487 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001488}
1489
Anders Carlssonfb311762009-03-14 00:25:26 +00001490StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001491 SourceLocation StaticAssertLoc,
1492 Expr *AssertExpr,
1493 StringLiteral *Message,
1494 SourceLocation RParenLoc) {
1495 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1496 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001497}
1498
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001499static const char *getAccessName(AccessSpecifier AS) {
1500 switch (AS) {
1501 default:
1502 case AS_none:
1503 assert("Invalid access specifier!");
1504 return 0;
1505 case AS_public:
1506 return "public";
1507 case AS_private:
1508 return "private";
1509 case AS_protected:
1510 return "protected";
1511 }
1512}
1513
1514const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1515 AccessSpecifier AS) {
1516 return DB << getAccessName(AS);
1517}