blob: 47c898921410e69760a779b329c17a2a1b3bcc80 [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),
Benjamin Kramer6e35b652011-04-30 08:55:35 +000034 Abstract(false), HasStandardLayout(true), 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)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000273 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000274 Best = I;
275
276 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000277 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000278 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);
John Wiegley20c0da72011-04-27 23:09:49 +0000929
930 ASTContext &Context = getASTContext();
931 if (const RecordType *RT = getTypeForDecl()->getAs<RecordType>())
932 data().HasStandardLayout = RT->hasStandardLayout(Context);
Douglas Gregorda2142f2011-02-19 18:51:44 +0000933}
934
935void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
936 RecordDecl::completeDefinition();
937
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000938 // If the class may be abstract (but hasn't been marked as such), check for
939 // any pure final overriders.
940 if (mayBeAbstract()) {
941 CXXFinalOverriderMap MyFinalOverriders;
942 if (!FinalOverriders) {
943 getFinalOverriders(MyFinalOverriders);
944 FinalOverriders = &MyFinalOverriders;
945 }
946
947 bool Done = false;
948 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
949 MEnd = FinalOverriders->end();
950 M != MEnd && !Done; ++M) {
951 for (OverridingMethods::iterator SO = M->second.begin(),
952 SOEnd = M->second.end();
953 SO != SOEnd && !Done; ++SO) {
954 assert(SO->second.size() > 0 &&
955 "All virtual functions have overridding virtual functions");
956
957 // C++ [class.abstract]p4:
958 // A class is abstract if it contains or inherits at least one
959 // pure virtual function for which the final overrider is pure
960 // virtual.
961 if (SO->second.front().Method->isPure()) {
962 data().Abstract = true;
963 Done = true;
964 break;
965 }
966 }
967 }
968 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000969
970 // Set access bits correctly on the directly-declared conversions.
971 for (UnresolvedSetIterator I = data().Conversions.begin(),
972 E = data().Conversions.end();
973 I != E; ++I)
974 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000975}
976
977bool CXXRecordDecl::mayBeAbstract() const {
978 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
979 isDependentContext())
980 return false;
981
982 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
983 BEnd = bases_end();
984 B != BEnd; ++B) {
985 CXXRecordDecl *BaseDecl
986 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
987 if (BaseDecl->isAbstract())
988 return true;
989 }
990
991 return false;
992}
993
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000994CXXMethodDecl *
995CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000996 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000997 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000998 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +0000999 bool isStatic, StorageClass SCAsWritten, bool isInline,
1000 SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001001 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001002 isStatic, SCAsWritten, isInline, EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001003}
1004
Douglas Gregor90916562009-09-29 18:16:17 +00001005bool CXXMethodDecl::isUsualDeallocationFunction() const {
1006 if (getOverloadedOperator() != OO_Delete &&
1007 getOverloadedOperator() != OO_Array_Delete)
1008 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001009
1010 // C++ [basic.stc.dynamic.deallocation]p2:
1011 // A template instance is never a usual deallocation function,
1012 // regardless of its signature.
1013 if (getPrimaryTemplate())
1014 return false;
1015
Douglas Gregor90916562009-09-29 18:16:17 +00001016 // C++ [basic.stc.dynamic.deallocation]p2:
1017 // If a class T has a member deallocation function named operator delete
1018 // with exactly one parameter, then that function is a usual (non-placement)
1019 // deallocation function. [...]
1020 if (getNumParams() == 1)
1021 return true;
1022
1023 // C++ [basic.stc.dynamic.deallocation]p2:
1024 // [...] If class T does not declare such an operator delete but does
1025 // declare a member deallocation function named operator delete with
1026 // exactly two parameters, the second of which has type std::size_t (18.1),
1027 // then this function is a usual deallocation function.
1028 ASTContext &Context = getASTContext();
1029 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001030 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1031 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001032 return false;
1033
1034 // This function is a usual deallocation function if there are no
1035 // single-parameter deallocation functions of the same kind.
1036 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1037 R.first != R.second; ++R.first) {
1038 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1039 if (FD->getNumParams() == 1)
1040 return false;
1041 }
1042
1043 return true;
1044}
1045
Douglas Gregor06a9f362010-05-01 20:49:11 +00001046bool CXXMethodDecl::isCopyAssignmentOperator() const {
1047 // C++0x [class.copy]p19:
1048 // A user-declared copy assignment operator X::operator= is a non-static
1049 // non-template member function of class X with exactly one parameter of
1050 // type X, X&, const X&, volatile X& or const volatile X&.
1051 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1052 /*non-static*/ isStatic() ||
1053 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1054 /*exactly one parameter*/getNumParams() != 1)
1055 return false;
1056
1057 QualType ParamType = getParamDecl(0)->getType();
1058 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1059 ParamType = Ref->getPointeeType();
1060
1061 ASTContext &Context = getASTContext();
1062 QualType ClassType
1063 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1064 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1065}
1066
Anders Carlsson05eb2442009-05-16 23:58:37 +00001067void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001068 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001069 assert(!MD->getParent()->isDependentContext() &&
1070 "Can't add an overridden method to a class template!");
1071
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001072 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001073}
1074
1075CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001076 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001077}
1078
1079CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001080 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001081}
1082
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001083unsigned CXXMethodDecl::size_overridden_methods() const {
1084 return getASTContext().overridden_methods_size(this);
1085}
1086
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001087QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001088 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1089 // If the member function is declared const, the type of this is const X*,
1090 // if the member function is declared volatile, the type of this is
1091 // volatile X*, and if the member function is declared const volatile,
1092 // the type of this is const volatile X*.
1093
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001094 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001095
John McCall3cb0ebd2010-03-10 03:28:59 +00001096 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001097 ClassTy = C.getQualifiedType(ClassTy,
1098 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001099 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001100}
1101
Eli Friedmand7d7f672009-12-06 20:50:05 +00001102bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001103 // If this function is a template instantiation, look at the template from
1104 // which it was instantiated.
1105 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1106 if (!CheckFn)
1107 CheckFn = this;
1108
Eli Friedmand7d7f672009-12-06 20:50:05 +00001109 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001110 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001111}
1112
Sean Huntcbb67482011-01-08 20:30:50 +00001113CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1114 TypeSourceInfo *TInfo, bool IsVirtual,
1115 SourceLocation L, Expr *Init,
1116 SourceLocation R,
1117 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001118 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001119 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1120 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001121{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001122}
1123
Sean Huntcbb67482011-01-08 20:30:50 +00001124CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1125 FieldDecl *Member,
1126 SourceLocation MemberLoc,
1127 SourceLocation L, Expr *Init,
1128 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001129 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001130 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1131 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1132{
1133}
1134
Sean Huntcbb67482011-01-08 20:30:50 +00001135CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1136 IndirectFieldDecl *Member,
1137 SourceLocation MemberLoc,
1138 SourceLocation L, Expr *Init,
1139 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001140 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001141 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001142 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001143{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001144}
1145
Sean Huntcbb67482011-01-08 20:30:50 +00001146CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Hunt41717662011-02-26 19:13:13 +00001147 SourceLocation D, SourceLocation L,
1148 CXXConstructorDecl *Target, Expr *Init,
1149 SourceLocation R)
1150 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1151 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1152 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1153{
1154}
1155
1156CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001157 FieldDecl *Member,
1158 SourceLocation MemberLoc,
1159 SourceLocation L, Expr *Init,
1160 SourceLocation R,
1161 VarDecl **Indices,
1162 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001163 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001164 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001165 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001166{
1167 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1168 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1169}
1170
Sean Huntcbb67482011-01-08 20:30:50 +00001171CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1172 FieldDecl *Member,
1173 SourceLocation MemberLoc,
1174 SourceLocation L, Expr *Init,
1175 SourceLocation R,
1176 VarDecl **Indices,
1177 unsigned NumIndices) {
1178 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001179 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001180 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001181 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1182 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001183}
1184
Sean Huntcbb67482011-01-08 20:30:50 +00001185TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001186 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001187 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001188 else
1189 return TypeLoc();
1190}
1191
Sean Huntcbb67482011-01-08 20:30:50 +00001192const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001193 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001194 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001195 else
1196 return 0;
1197}
1198
Sean Huntcbb67482011-01-08 20:30:50 +00001199SourceLocation CXXCtorInitializer::getSourceLocation() const {
Sean Hunt41717662011-02-26 19:13:13 +00001200 if (isAnyMemberInitializer() || isDelegatingInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001201 return getMemberLocation();
1202
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001203 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001204}
1205
Sean Huntcbb67482011-01-08 20:30:50 +00001206SourceRange CXXCtorInitializer::getSourceRange() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001207 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001208}
1209
Douglas Gregorb48fe382008-10-31 09:07:45 +00001210CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001211CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001212 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001213 QualType(), 0, false, false, false);
1214}
1215
1216CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001217CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001218 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001219 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001220 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001221 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001222 bool isInline,
1223 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001224 assert(NameInfo.getName().getNameKind()
1225 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001226 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001227 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
1228 isExplicit, isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001229}
1230
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001231bool CXXConstructorDecl::isDefaultConstructor() const {
1232 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001233 // A default constructor for a class X is a constructor of class
1234 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001235 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001236 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001237}
1238
Mike Stump1eb44332009-09-09 15:08:12 +00001239bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001240CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001241 return isCopyOrMoveConstructor(TypeQuals) &&
1242 getParamDecl(0)->getType()->isLValueReferenceType();
1243}
1244
1245bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1246 return isCopyOrMoveConstructor(TypeQuals) &&
1247 getParamDecl(0)->getType()->isRValueReferenceType();
1248}
1249
1250/// \brief Determine whether this is a copy or move constructor.
1251bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001252 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001253 // A non-template constructor for class X is a copy constructor
1254 // if its first parameter is of type X&, const X&, volatile X& or
1255 // const volatile X&, and either there are no other parameters
1256 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001257 // C++0x [class.copy]p3:
1258 // A non-template constructor for class X is a move constructor if its
1259 // first parameter is of type X&&, const X&&, volatile X&&, or
1260 // const volatile X&&, and either there are no other parameters or else
1261 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001262 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001263 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001264 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001265 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001266 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001267
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001268 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001269
1270 // Do we have a reference type?
1271 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001272 if (!ParamRefType)
1273 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001274
Douglas Gregorfd476482009-11-13 23:59:09 +00001275 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001276 ASTContext &Context = getASTContext();
1277
Douglas Gregorfd476482009-11-13 23:59:09 +00001278 CanQualType PointeeType
1279 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001280 CanQualType ClassTy
1281 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001282 if (PointeeType.getUnqualifiedType() != ClassTy)
1283 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001284
John McCall0953e762009-09-24 19:53:00 +00001285 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001286
1287 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001288 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001289 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001290}
1291
Anders Carlssonfaccd722009-08-28 16:57:08 +00001292bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001293 // C++ [class.conv.ctor]p1:
1294 // A constructor declared without the function-specifier explicit
1295 // that can be called with a single parameter specifies a
1296 // conversion from the type of its first parameter to the type of
1297 // its class. Such a constructor is called a converting
1298 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001299 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001300 return false;
1301
Mike Stump1eb44332009-09-09 15:08:12 +00001302 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001303 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001304 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001305 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001306}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001307
Douglas Gregor6493cc52010-11-08 17:16:59 +00001308bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001309 if ((getNumParams() < 1) ||
1310 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1311 (getPrimaryTemplate() == 0) ||
1312 (getDescribedFunctionTemplate() != 0))
1313 return false;
1314
1315 const ParmVarDecl *Param = getParamDecl(0);
1316
1317 ASTContext &Context = getASTContext();
1318 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1319
Douglas Gregor66724ea2009-11-14 01:20:54 +00001320 // Is it the same as our our class type?
1321 CanQualType ClassTy
1322 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1323 if (ParamType.getUnqualifiedType() != ClassTy)
1324 return false;
1325
1326 return true;
1327}
1328
Sebastian Redlf677ea32011-02-05 19:23:19 +00001329const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1330 // Hack: we store the inherited constructor in the overridden method table
1331 method_iterator It = begin_overridden_methods();
1332 if (It == end_overridden_methods())
1333 return 0;
1334
1335 return cast<CXXConstructorDecl>(*It);
1336}
1337
1338void
1339CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1340 // Hack: we store the inherited constructor in the overridden method table
1341 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1342 addOverriddenMethod(BaseCtor);
1343}
1344
Douglas Gregor42a552f2008-11-05 20:51:48 +00001345CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001346CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001347 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001348 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001349}
1350
1351CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001352CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001353 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001354 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001355 QualType T, TypeSourceInfo *TInfo,
1356 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001357 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001358 assert(NameInfo.getName().getNameKind()
1359 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001360 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001361 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001362 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001363}
1364
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001365CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001366CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001367 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001368 QualType(), 0, false, false,
1369 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001370}
1371
1372CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001373CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001374 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001375 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001376 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001377 bool isInline, bool isExplicit,
1378 SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001379 assert(NameInfo.getName().getNameKind()
1380 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001381 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001382 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001383 isInline, isExplicit, EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001384}
1385
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001386LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001387 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001388 SourceLocation ExternLoc,
1389 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001390 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001391 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001392 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001393}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001394
1395UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1396 SourceLocation L,
1397 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001398 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001399 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001400 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001401 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001402 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1403 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001404 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1405 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001406}
1407
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001408NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1409 if (NamespaceAliasDecl *NA =
1410 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1411 return NA->getNamespace();
1412 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1413}
1414
Mike Stump1eb44332009-09-09 15:08:12 +00001415NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001416 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001417 SourceLocation AliasLoc,
1418 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001419 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001420 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001421 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001422 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1423 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001424 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1425 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001426}
1427
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001428UsingDecl *UsingShadowDecl::getUsingDecl() const {
1429 const UsingShadowDecl *Shadow = this;
1430 while (const UsingShadowDecl *NextShadow =
1431 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1432 Shadow = NextShadow;
1433 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1434}
1435
1436void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1437 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1438 "declaration already in set");
1439 assert(S->getUsingDecl() == this);
1440
1441 if (FirstUsingShadow)
1442 S->UsingOrNextShadow = FirstUsingShadow;
1443 FirstUsingShadow = S;
1444}
1445
1446void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1447 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1448 "declaration not in set");
1449 assert(S->getUsingDecl() == this);
1450
1451 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1452
1453 if (FirstUsingShadow == S) {
1454 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1455 S->UsingOrNextShadow = this;
1456 return;
1457 }
1458
1459 UsingShadowDecl *Prev = FirstUsingShadow;
1460 while (Prev->UsingOrNextShadow != S)
1461 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1462 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1463 S->UsingOrNextShadow = this;
1464}
1465
Douglas Gregordc355712011-02-25 00:36:19 +00001466UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1467 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001468 const DeclarationNameInfo &NameInfo,
1469 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001470 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001471}
1472
John McCall7ba107a2009-11-18 02:36:19 +00001473UnresolvedUsingValueDecl *
1474UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1475 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001476 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001477 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001478 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001479 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001480}
1481
1482UnresolvedUsingTypenameDecl *
1483UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1484 SourceLocation UsingLoc,
1485 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001486 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001487 SourceLocation TargetNameLoc,
1488 DeclarationName TargetName) {
1489 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001490 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001491 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001492}
1493
Anders Carlssonfb311762009-03-14 00:25:26 +00001494StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001495 SourceLocation StaticAssertLoc,
1496 Expr *AssertExpr,
1497 StringLiteral *Message,
1498 SourceLocation RParenLoc) {
1499 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1500 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001501}
1502
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001503static const char *getAccessName(AccessSpecifier AS) {
1504 switch (AS) {
1505 default:
1506 case AS_none:
1507 assert("Invalid access specifier!");
1508 return 0;
1509 case AS_public:
1510 return "public";
1511 case AS_private:
1512 return "private";
1513 case AS_protected:
1514 return "protected";
1515 }
1516}
1517
1518const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1519 AccessSpecifier AS) {
1520 return DB << getAccessName(AS);
1521}