blob: af796d54c4d2644416cc1cfeaf4388426bff8bd8 [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 Carruth4d6e5a22011-04-23 23:10:33 +000035 HasTrivialCopyConstructor(true), HasTrivialMoveConstructor(true),
36 HasTrivialCopyAssignment(true), HasTrivialMoveAssignment(true),
Fariborz Jahanian62509212009-09-12 18:26:03 +000037 HasTrivialDestructor(true), ComputedVisibleConversions(false),
Douglas Gregor18274032010-07-03 00:47:00 +000038 DeclaredDefaultConstructor(false), DeclaredCopyConstructor(false),
Douglas Gregora376d102010-07-02 21:50:04 +000039 DeclaredCopyAssignment(false), DeclaredDestructor(false),
Anders Carlssoncb88a1f2011-01-24 16:26:15 +000040 NumBases(0), NumVBases(0), Bases(), VBases(),
41 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000042}
43
44CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000045 SourceLocation StartLoc, SourceLocation IdLoc,
46 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
47 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000048 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000049 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000050
Jay Foad4ba2a172011-01-12 09:06:06 +000051CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000052 DeclContext *DC, SourceLocation StartLoc,
53 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000054 CXXRecordDecl* PrevDecl,
55 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000056 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
57 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000058
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000059 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000060 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000061 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000062 return R;
63}
64
Jay Foad4ba2a172011-01-12 09:06:06 +000065CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000066 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
67 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000068}
69
Mike Stump1eb44332009-09-09 15:08:12 +000070void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000071CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000072 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000073 ASTContext &C = getASTContext();
74
Mike Stump1eb44332009-09-09 15:08:12 +000075 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000076 // An aggregate is an array or a class (clause 9) with [...]
77 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000078 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000079
Douglas Gregor7c789c12010-10-29 22:39:52 +000080 if (!data().Bases.isOffset() && data().NumBases > 0)
81 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000082
Anders Carlsson6f6de732010-03-29 05:13:12 +000083 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000084 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000085
86 // The virtual bases of this class.
87 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000088
John McCall86ff3082010-02-04 22:26:26 +000089 data().Bases = new(C) CXXBaseSpecifier [NumBases];
90 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000091 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +000092 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000093 // Keep track of inherited vbases for this base class.
94 const CXXBaseSpecifier *Base = Bases[i];
95 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +000096 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000097 if (BaseType->isDependentType())
98 continue;
99 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000100 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000101
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000102 // C++ [dcl.init.aggr]p1:
103 // An aggregate is [...] a class with [...] no base classes [...].
104 data().Aggregate = false;
105
106 // C++ [class]p4:
107 // A POD-struct is an aggregate class...
108 data().PlainOldData = false;
109
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000110 // A class with a non-empty base class is not empty.
111 // FIXME: Standard ref?
112 if (!BaseClassDecl->isEmpty())
113 data().Empty = false;
114
Douglas Gregor85606eb2010-09-28 20:50:54 +0000115 // C++ [class.virtual]p1:
116 // A class that declares or inherits a virtual function is called a
117 // polymorphic class.
118 if (BaseClassDecl->isPolymorphic())
119 data().Polymorphic = true;
120
Anders Carlsson6f6de732010-03-29 05:13:12 +0000121 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000122 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000123 BaseClassDecl->vbases_begin(),
124 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000125 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000126 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000127 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000128 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000129
130 if (Base->isVirtual()) {
131 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000132 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000133 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000134
135 // C++0x [meta.unary.prop] is_empty:
136 // T is a class type, but not a union type, with ... no virtual base
137 // classes
138 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000139
140 // C++ [class.ctor]p5:
141 // A constructor is trivial if its class has no virtual base classes.
142 data().HasTrivialConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000143
144 // C++0x [class.copy]p13:
145 // A copy/move constructor for class X is trivial if it is neither
146 // user-provided nor deleted and if
147 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000148 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000149 data().HasTrivialMoveConstructor = false;
150
151 // C++0x [class.copy]p27:
152 // A copy/move assignment operator for class X is trivial if it is
153 // neither user-provided nor deleted and if
154 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000155 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000156 data().HasTrivialMoveAssignment = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000157 } else {
158 // C++ [class.ctor]p5:
159 // A constructor is trivial if all the direct base classes of its
160 // class have trivial constructors.
161 if (!BaseClassDecl->hasTrivialConstructor())
162 data().HasTrivialConstructor = false;
163
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000164 // C++0x [class.copy]p13:
165 // A copy/move constructor for class X is trivial if [...]
166 // [...]
167 // -- the constructor selected to copy/move each direct base class
168 // subobject is trivial, and
169 // FIXME: C++0x: We need to only consider the selected constructor
170 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000171 if (!BaseClassDecl->hasTrivialCopyConstructor())
172 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000173 if (!BaseClassDecl->hasTrivialMoveConstructor())
174 data().HasTrivialMoveConstructor = false;
175
176 // C++0x [class.copy]p27:
177 // A copy/move assignment operator for class X is trivial if [...]
178 // [...]
179 // -- the assignment operator selected to copy/move each direct base
180 // class subobject is trivial, and
181 // FIXME: C++0x: We need to only consider the selected operator instead
182 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000183 if (!BaseClassDecl->hasTrivialCopyAssignment())
184 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000185 if (!BaseClassDecl->hasTrivialMoveAssignment())
186 data().HasTrivialMoveAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000187 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000188
189 // C++ [class.ctor]p3:
190 // A destructor is trivial if all the direct base classes of its class
191 // have trivial destructors.
192 if (!BaseClassDecl->hasTrivialDestructor())
193 data().HasTrivialDestructor = false;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000194 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000195
196 if (VBases.empty())
197 return;
198
199 // Create base specifier for any direct or indirect virtual bases.
200 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
201 data().NumVBases = VBases.size();
202 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000203 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
204
Anders Carlsson6f6de732010-03-29 05:13:12 +0000205 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000206 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000207 continue;
208
Nick Lewycky56062202010-07-26 16:56:01 +0000209 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
210 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000211
Douglas Gregor7c789c12010-10-29 22:39:52 +0000212 data().getVBases()[I] =
Anders Carlsson6f6de732010-03-29 05:13:12 +0000213 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000214 VBaseClassDecl->getTagKind() == TTK_Class,
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000215 VBases[I]->getAccessSpecifier(), VBaseTypeInfo,
216 SourceLocation());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000217 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000218}
219
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000220/// Callback function for CXXRecordDecl::forallBases that acknowledges
221/// that it saw a base class.
222static bool SawBase(const CXXRecordDecl *, void *) {
223 return true;
224}
225
226bool CXXRecordDecl::hasAnyDependentBases() const {
227 if (!isDependentContext())
228 return false;
229
230 return !forallBases(SawBase, 0);
231}
232
Jay Foad4ba2a172011-01-12 09:06:06 +0000233bool CXXRecordDecl::hasConstCopyConstructor(const ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000234 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000235}
236
Chandler Carruthb7e95892011-04-23 10:47:28 +0000237bool CXXRecordDecl::isTriviallyCopyable() const {
238 // C++0x [class]p5:
239 // A trivially copyable class is a class that:
240 // -- has no non-trivial copy constructors,
241 if (!hasTrivialCopyConstructor()) return false;
242 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000243 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000244 // -- has no non-trivial copy assignment operators,
245 if (!hasTrivialCopyAssignment()) return false;
246 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000247 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000248 // -- has a trivial destructor.
249 if (!hasTrivialDestructor()) return false;
250
251 return true;
252}
253
Douglas Gregor0d405db2010-07-01 20:59:04 +0000254/// \brief Perform a simplistic form of overload resolution that only considers
255/// cv-qualifiers on a single parameter, and return the best overload candidate
256/// (if there is one).
257static CXXMethodDecl *
258GetBestOverloadCandidateSimple(
259 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
260 if (Cands.empty())
261 return 0;
262 if (Cands.size() == 1)
263 return Cands[0].first;
264
265 unsigned Best = 0, N = Cands.size();
266 for (unsigned I = 1; I != N; ++I)
267 if (Cands[Best].second.isSupersetOf(Cands[I].second))
268 Best = I;
269
270 for (unsigned I = 1; I != N; ++I)
271 if (Cands[Best].second.isSupersetOf(Cands[I].second))
272 return 0;
273
274 return Cands[Best].first;
275}
276
Jay Foad4ba2a172011-01-12 09:06:06 +0000277CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(const ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000278 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000279 QualType ClassType
280 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000281 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000282 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000283 Context.getCanonicalType(ClassType));
284 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000285 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000286 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000287 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000288 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000289 // C++ [class.copy]p2:
290 // A non-template constructor for class X is a copy constructor if [...]
291 if (isa<FunctionTemplateDecl>(*Con))
292 continue;
293
Douglas Gregor0d405db2010-07-01 20:59:04 +0000294 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
295 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000296 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
297 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000298 Found.push_back(std::make_pair(
299 const_cast<CXXConstructorDecl *>(Constructor),
300 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000301 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000302 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000303
304 return cast_or_null<CXXConstructorDecl>(
305 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000306}
307
Douglas Gregorb87786f2010-07-01 17:48:08 +0000308CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
309 ASTContext &Context = getASTContext();
310 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
311 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
312
313 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
314 DeclContext::lookup_const_iterator Op, OpEnd;
315 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
316 // C++ [class.copy]p9:
317 // A user-declared copy assignment operator is a non-static non-template
318 // member function of class X with exactly one parameter of type X, X&,
319 // const X&, volatile X& or const volatile X&.
320 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
321 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
322 continue;
323
324 const FunctionProtoType *FnType
325 = Method->getType()->getAs<FunctionProtoType>();
326 assert(FnType && "Overloaded operator has no prototype.");
327 // Don't assert on this; an invalid decl might have been left in the AST.
328 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
329 continue;
330
331 QualType ArgType = FnType->getArgType(0);
332 Qualifiers Quals;
333 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
334 ArgType = Ref->getPointeeType();
335 // If we have a const argument and we have a reference to a non-const,
336 // this function does not match.
337 if (ArgIsConst && !ArgType.isConstQualified())
338 continue;
339
340 Quals = ArgType.getQualifiers();
341 } else {
342 // By-value copy-assignment operators are treated like const X&
343 // copy-assignment operators.
344 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
345 }
346
347 if (!Context.hasSameUnqualifiedType(ArgType, Class))
348 continue;
349
350 // Save this copy-assignment operator. It might be "the one".
351 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
352 }
353
354 // Use a simplistic form of overload resolution to find the candidate.
355 return GetBestOverloadCandidateSimple(Found);
356}
357
Douglas Gregor21386642010-09-28 21:55:22 +0000358void CXXRecordDecl::markedVirtualFunctionPure() {
359 // C++ [class.abstract]p2:
360 // A class is abstract if it has at least one pure virtual function.
361 data().Abstract = true;
362}
363
364void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000365 // Ignore friends and invalid declarations.
366 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000367 return;
368
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000369 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
370 if (FunTmpl)
371 D = FunTmpl->getTemplatedDecl();
372
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000373 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
374 if (Method->isVirtual()) {
375 // C++ [dcl.init.aggr]p1:
376 // An aggregate is an array or a class with [...] no virtual functions.
377 data().Aggregate = false;
378
379 // C++ [class]p4:
380 // A POD-struct is an aggregate class...
381 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000382
383 // Virtual functions make the class non-empty.
384 // FIXME: Standard ref?
385 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000386
387 // C++ [class.virtual]p1:
388 // A class that declares or inherits a virtual function is called a
389 // polymorphic class.
390 data().Polymorphic = true;
391
392 // None of the special member functions are trivial.
393 data().HasTrivialConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000394
395 // C++0x [class.copy]p13:
396 // A copy/move constructor for class X is trivial if [...]
397 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000398 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000399 data().HasTrivialMoveConstructor = false;
400
401 // C++0x [class.copy]p27:
402 // A copy/move assignment operator for class X is trivial if [...]
403 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000404 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000405 data().HasTrivialMoveAssignment = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000406 // FIXME: Destructor?
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000407 }
408 }
409
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000410 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000411 // Notify that an implicit member was added after the definition
412 // was completed.
413 if (!isBeingDefined())
414 if (ASTMutationListener *L = getASTMutationListener())
415 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000416
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000417 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
418 // If this is the implicit default constructor, note that we have now
419 // declared it.
420 if (Constructor->isDefaultConstructor())
421 data().DeclaredDefaultConstructor = true;
422 // If this is the implicit copy constructor, note that we have now
423 // declared it.
424 else if (Constructor->isCopyConstructor())
425 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000426 return;
427 }
428
429 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000430 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000431 return;
432 }
433
434 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000435 // If this is the implicit copy constructor, note that we have now
436 // declared it.
437 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000438 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000439 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000440 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000441 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000442
443 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000444 }
445
446 // Handle (user-declared) constructors.
447 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
448 // Note that we have a user-declared constructor.
449 data().UserDeclaredConstructor = true;
450
451 // Note that we have no need of an implicitly-declared default constructor.
452 data().DeclaredDefaultConstructor = true;
453
454 // C++ [dcl.init.aggr]p1:
455 // An aggregate is an array or a class (clause 9) with no
456 // user-declared constructors (12.1) [...].
457 data().Aggregate = false;
458
459 // C++ [class]p4:
460 // A POD-struct is an aggregate class [...]
461 data().PlainOldData = false;
462
463 // C++ [class.ctor]p5:
464 // A constructor is trivial if it is an implicitly-declared default
465 // constructor.
466 // FIXME: C++0x: don't do this for "= default" default constructors.
467 data().HasTrivialConstructor = false;
468
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000469 // Note when we have a user-declared copy or move constructor, which will
470 // suppress the implicit declaration of those constructors.
471 if (!FunTmpl) {
472 if (Constructor->isCopyConstructor()) {
473 data().UserDeclaredCopyConstructor = true;
474 data().DeclaredCopyConstructor = true;
475
476 // C++0x [class.copy]p13:
477 // A copy/move constructor for class X is trivial if it is neither
478 // user-provided nor deleted
479 // FIXME: C++0x: don't do this for "= default" copy constructors.
480 data().HasTrivialCopyConstructor = false;
481 } else if (Constructor->isMoveConstructor()) {
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" move constructors.
486 data().HasTrivialMoveConstructor = false;
487 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000488 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000489
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000490 return;
491 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000492
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000493 // Handle (user-declared) destructors.
494 if (isa<CXXDestructorDecl>(D)) {
495 data().DeclaredDestructor = true;
496 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000497
498 // C++ [class]p4:
499 // A POD-struct is an aggregate class that has [...] no user-defined
500 // destructor.
501 data().PlainOldData = false;
502
Douglas Gregor85606eb2010-09-28 20:50:54 +0000503 // C++ [class.dtor]p3:
504 // A destructor is trivial if it is an implicitly-declared destructor and
505 // [...].
506 //
507 // FIXME: C++0x: don't do this for "= default" destructors
508 data().HasTrivialDestructor = false;
509
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000510 return;
511 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000512
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000513 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000514 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
515 if (Method->getOverloadedOperator() == OO_Equal) {
516 // We're interested specifically in copy assignment operators.
517 const FunctionProtoType *FnType
518 = Method->getType()->getAs<FunctionProtoType>();
519 assert(FnType && "Overloaded operator has no proto function type.");
520 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
521
522 // Copy assignment operators must be non-templates.
523 if (Method->getPrimaryTemplate() || FunTmpl)
524 return;
525
526 ASTContext &Context = getASTContext();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000527 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
528 const_cast<CXXRecordDecl*>(this)));
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000529
530 bool isRValueRefArg = false;
531 QualType ArgType = FnType->getArgType(0);
532 if (const LValueReferenceType *Ref =
533 ArgType->getAs<LValueReferenceType>()) {
534 ArgType = Ref->getPointeeType();
535 } else if (const RValueReferenceType *Ref =
536 ArgType->getAs<RValueReferenceType>()) {
537 ArgType = Ref->getPointeeType();
538 isRValueRefArg = true;
539 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000540 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
541 return;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000542
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000543 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000544 // A POD-struct is an aggregate class that [...] has no user-defined
545 // copy assignment operator [...].
546 // FIXME: This should be probably determined dynamically in terms of
547 // other more precise attributes to correctly model how it is specified
548 // in C++0x. Setting it here happens to do the right thing.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000549 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000550
551 if (!isRValueRefArg) {
552 // This is a copy assignment operator.
553
554 // Suppress the implicit declaration of a copy constructor.
555 data().UserDeclaredCopyAssignment = true;
556 data().DeclaredCopyAssignment = true;
557
558 // C++0x [class.copy]p27:
559 // A copy/move assignment operator for class X is trivial if it is
560 // neither user-provided nor deleted [...]
561 // FIXME: C++0x: don't do this for "= default" copy operators.
562 data().HasTrivialCopyAssignment = false;
563 } else {
564 // This is a move assignment operator.
565
566 // C++0x [class.copy]p27:
567 // A copy/move assignment operator for class X is trivial if it is
568 // neither user-provided nor deleted [...]
569 // FIXME: C++0x: don't do this for "= default" copy operators.
570 data().HasTrivialMoveAssignment = false;
571 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000572 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000573
Douglas Gregore80622f2010-09-29 04:25:11 +0000574 // Keep the list of conversion functions up-to-date.
575 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
576 // We don't record specializations.
577 if (Conversion->getPrimaryTemplate())
578 return;
579
580 // FIXME: We intentionally don't use the decl's access here because it
581 // hasn't been set yet. That's really just a misdesign in Sema.
582
583 if (FunTmpl) {
584 if (FunTmpl->getPreviousDeclaration())
585 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
586 FunTmpl);
587 else
588 data().Conversions.addDecl(FunTmpl);
589 } else {
590 if (Conversion->getPreviousDeclaration())
591 data().Conversions.replace(Conversion->getPreviousDeclaration(),
592 Conversion);
593 else
594 data().Conversions.addDecl(Conversion);
595 }
596 }
597
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000598 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000599 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000600
601 // Handle non-static data members.
602 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
603 // C++ [dcl.init.aggr]p1:
604 // An aggregate is an array or a class (clause 9) with [...] no
605 // private or protected non-static data members (clause 11).
606 //
607 // A POD must be an aggregate.
608 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
609 data().Aggregate = false;
610 data().PlainOldData = false;
611 }
612
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000613 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000614 // A POD struct is a class that is both a trivial class and a
615 // standard-layout class, and has no non-static data members of type
616 // non-POD struct, non-POD union (or array of such types).
617 ASTContext &Context = getASTContext();
618 QualType T = Context.getBaseElementType(Field->getType());
619 if (!T->isPODType())
620 data().PlainOldData = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000621 if (T->isReferenceType())
622 data().HasTrivialConstructor = false;
623
624 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
625 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
626 if (FieldRec->getDefinition()) {
627 if (!FieldRec->hasTrivialConstructor())
628 data().HasTrivialConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000629
630 // C++0x [class.copy]p13:
631 // A copy/move constructor for class X is trivial if [...]
632 // [...]
633 // -- for each non-static data member of X that is of class type (or
634 // an array thereof), the constructor selected to copy/move that
635 // member is trivial;
636 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000637 if (!FieldRec->hasTrivialCopyConstructor())
638 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000639 if (!FieldRec->hasTrivialMoveConstructor())
640 data().HasTrivialMoveConstructor = false;
641
642 // C++0x [class.copy]p27:
643 // A copy/move assignment operator for class X is trivial if [...]
644 // [...]
645 // -- for each non-static data member of X that is of class type (or
646 // an array thereof), the assignment operator selected to
647 // copy/move that member is trivial;
648 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000649 if (!FieldRec->hasTrivialCopyAssignment())
650 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000651 if (!FieldRec->hasTrivialMoveAssignment())
652 data().HasTrivialMoveAssignment = false;
653
Douglas Gregor85606eb2010-09-28 20:50:54 +0000654 if (!FieldRec->hasTrivialDestructor())
655 data().HasTrivialDestructor = false;
656 }
657 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000658
659 // If this is not a zero-length bit-field, then the class is not empty.
660 if (data().Empty) {
661 if (!Field->getBitWidth())
662 data().Empty = false;
663 else if (!Field->getBitWidth()->isTypeDependent() &&
664 !Field->getBitWidth()->isValueDependent()) {
665 llvm::APSInt Bits;
666 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
667 if (!!Bits)
668 data().Empty = false;
669 }
670 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000671 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000672
673 // Handle using declarations of conversion functions.
674 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
675 if (Shadow->getDeclName().getNameKind()
676 == DeclarationName::CXXConversionFunctionName)
677 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000678}
679
John McCallb05b5f32010-03-15 09:07:48 +0000680static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
681 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000682 if (isa<UsingShadowDecl>(Conv))
683 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000684 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
685 T = ConvTemp->getTemplatedDecl()->getResultType();
686 else
687 T = cast<CXXConversionDecl>(Conv)->getConversionType();
688 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000689}
690
John McCallb05b5f32010-03-15 09:07:48 +0000691/// Collect the visible conversions of a base class.
692///
693/// \param Base a base class of the class we're considering
694/// \param InVirtual whether this base class is a virtual base (or a base
695/// of a virtual base)
696/// \param Access the access along the inheritance path to this base
697/// \param ParentHiddenTypes the conversions provided by the inheritors
698/// of this base
699/// \param Output the set to which to add conversions from non-virtual bases
700/// \param VOutput the set to which to add conversions from virtual bases
701/// \param HiddenVBaseCs the set of conversions which were hidden in a
702/// virtual base along some inheritance path
703static void CollectVisibleConversions(ASTContext &Context,
704 CXXRecordDecl *Record,
705 bool InVirtual,
706 AccessSpecifier Access,
707 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
708 UnresolvedSetImpl &Output,
709 UnresolvedSetImpl &VOutput,
710 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
711 // The set of types which have conversions in this class or its
712 // subclasses. As an optimization, we don't copy the derived set
713 // unless it might change.
714 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
715 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
716
717 // Collect the direct conversions and figure out which conversions
718 // will be hidden in the subclasses.
719 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
720 if (!Cs.empty()) {
721 HiddenTypesBuffer = ParentHiddenTypes;
722 HiddenTypes = &HiddenTypesBuffer;
723
724 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
725 bool Hidden =
726 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
727
728 // If this conversion is hidden and we're in a virtual base,
729 // remember that it's hidden along some inheritance path.
730 if (Hidden && InVirtual)
731 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
732
733 // If this conversion isn't hidden, add it to the appropriate output.
734 else if (!Hidden) {
735 AccessSpecifier IAccess
736 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
737
738 if (InVirtual)
739 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000740 else
John McCallb05b5f32010-03-15 09:07:48 +0000741 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000742 }
743 }
744 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000745
John McCallb05b5f32010-03-15 09:07:48 +0000746 // Collect information recursively from any base classes.
747 for (CXXRecordDecl::base_class_iterator
748 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
749 const RecordType *RT = I->getType()->getAs<RecordType>();
750 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000751
John McCallb05b5f32010-03-15 09:07:48 +0000752 AccessSpecifier BaseAccess
753 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
754 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000755
John McCallb05b5f32010-03-15 09:07:48 +0000756 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
757 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
758 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000759 }
John McCallb05b5f32010-03-15 09:07:48 +0000760}
Sebastian Redl9994a342009-10-25 17:03:50 +0000761
John McCallb05b5f32010-03-15 09:07:48 +0000762/// Collect the visible conversions of a class.
763///
764/// This would be extremely straightforward if it weren't for virtual
765/// bases. It might be worth special-casing that, really.
766static void CollectVisibleConversions(ASTContext &Context,
767 CXXRecordDecl *Record,
768 UnresolvedSetImpl &Output) {
769 // The collection of all conversions in virtual bases that we've
770 // found. These will be added to the output as long as they don't
771 // appear in the hidden-conversions set.
772 UnresolvedSet<8> VBaseCs;
773
774 // The set of conversions in virtual bases that we've determined to
775 // be hidden.
776 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
777
778 // The set of types hidden by classes derived from this one.
779 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
780
781 // Go ahead and collect the direct conversions and add them to the
782 // hidden-types set.
783 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
784 Output.append(Cs.begin(), Cs.end());
785 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
786 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
787
788 // Recursively collect conversions from base classes.
789 for (CXXRecordDecl::base_class_iterator
790 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
791 const RecordType *RT = I->getType()->getAs<RecordType>();
792 if (!RT) continue;
793
794 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
795 I->isVirtual(), I->getAccessSpecifier(),
796 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
797 }
798
799 // Add any unhidden conversions provided by virtual bases.
800 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
801 I != E; ++I) {
802 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
803 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000804 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000805}
806
807/// getVisibleConversionFunctions - get all conversion functions visible
808/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000809const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000810 // If root class, all conversions are visible.
811 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000812 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000813 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000814 if (data().ComputedVisibleConversions)
815 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000816 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000817 data().ComputedVisibleConversions = true;
818 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000819}
820
John McCall32daa422010-03-31 01:36:47 +0000821void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
822 // This operation is O(N) but extremely rare. Sema only uses it to
823 // remove UsingShadowDecls in a class that were followed by a direct
824 // declaration, e.g.:
825 // class A : B {
826 // using B::operator int;
827 // operator int();
828 // };
829 // This is uncommon by itself and even more uncommon in conjunction
830 // with sufficiently large numbers of directly-declared conversions
831 // that asymptotic behavior matters.
832
833 UnresolvedSetImpl &Convs = *getConversionFunctions();
834 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
835 if (Convs[I].getDecl() == ConvDecl) {
836 Convs.erase(I);
837 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
838 && "conversion was found multiple times in unresolved set");
839 return;
840 }
841 }
842
843 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000844}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000845
Douglas Gregorf6b11852009-10-08 15:14:33 +0000846CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000847 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000848 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
849
850 return 0;
851}
852
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000853MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
854 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
855}
856
Douglas Gregorf6b11852009-10-08 15:14:33 +0000857void
858CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
859 TemplateSpecializationKind TSK) {
860 assert(TemplateOrInstantiation.isNull() &&
861 "Previous template or instantiation?");
862 assert(!isa<ClassTemplateSpecializationDecl>(this));
863 TemplateOrInstantiation
864 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
865}
866
Anders Carlssonb13e3572009-12-07 06:33:48 +0000867TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
868 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000869 = dyn_cast<ClassTemplateSpecializationDecl>(this))
870 return Spec->getSpecializationKind();
871
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000872 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000873 return MSInfo->getTemplateSpecializationKind();
874
875 return TSK_Undeclared;
876}
877
878void
879CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
880 if (ClassTemplateSpecializationDecl *Spec
881 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
882 Spec->setSpecializationKind(TSK);
883 return;
884 }
885
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000886 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000887 MSInfo->setTemplateSpecializationKind(TSK);
888 return;
889 }
890
891 assert(false && "Not a class template or member class specialization");
892}
893
Douglas Gregor1d110e02010-07-01 14:13:13 +0000894CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
895 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000896 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000897
898 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000899 = Context.DeclarationNames.getCXXDestructorName(
900 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000901
John McCallc0bf4622010-02-23 00:48:20 +0000902 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000903 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000904 if (I == E)
905 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000907 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000908 return Dtor;
909}
910
Douglas Gregorda2142f2011-02-19 18:51:44 +0000911void CXXRecordDecl::completeDefinition() {
912 completeDefinition(0);
913}
914
915void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
916 RecordDecl::completeDefinition();
917
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000918 // If the class may be abstract (but hasn't been marked as such), check for
919 // any pure final overriders.
920 if (mayBeAbstract()) {
921 CXXFinalOverriderMap MyFinalOverriders;
922 if (!FinalOverriders) {
923 getFinalOverriders(MyFinalOverriders);
924 FinalOverriders = &MyFinalOverriders;
925 }
926
927 bool Done = false;
928 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
929 MEnd = FinalOverriders->end();
930 M != MEnd && !Done; ++M) {
931 for (OverridingMethods::iterator SO = M->second.begin(),
932 SOEnd = M->second.end();
933 SO != SOEnd && !Done; ++SO) {
934 assert(SO->second.size() > 0 &&
935 "All virtual functions have overridding virtual functions");
936
937 // C++ [class.abstract]p4:
938 // A class is abstract if it contains or inherits at least one
939 // pure virtual function for which the final overrider is pure
940 // virtual.
941 if (SO->second.front().Method->isPure()) {
942 data().Abstract = true;
943 Done = true;
944 break;
945 }
946 }
947 }
948 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000949
950 // Set access bits correctly on the directly-declared conversions.
951 for (UnresolvedSetIterator I = data().Conversions.begin(),
952 E = data().Conversions.end();
953 I != E; ++I)
954 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000955}
956
957bool CXXRecordDecl::mayBeAbstract() const {
958 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
959 isDependentContext())
960 return false;
961
962 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
963 BEnd = bases_end();
964 B != BEnd; ++B) {
965 CXXRecordDecl *BaseDecl
966 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
967 if (BaseDecl->isAbstract())
968 return true;
969 }
970
971 return false;
972}
973
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000974CXXMethodDecl *
975CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000976 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000977 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000978 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +0000979 bool isStatic, StorageClass SCAsWritten, bool isInline,
980 SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000981 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +0000982 isStatic, SCAsWritten, isInline, EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000983}
984
Douglas Gregor90916562009-09-29 18:16:17 +0000985bool CXXMethodDecl::isUsualDeallocationFunction() const {
986 if (getOverloadedOperator() != OO_Delete &&
987 getOverloadedOperator() != OO_Array_Delete)
988 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000989
990 // C++ [basic.stc.dynamic.deallocation]p2:
991 // A template instance is never a usual deallocation function,
992 // regardless of its signature.
993 if (getPrimaryTemplate())
994 return false;
995
Douglas Gregor90916562009-09-29 18:16:17 +0000996 // C++ [basic.stc.dynamic.deallocation]p2:
997 // If a class T has a member deallocation function named operator delete
998 // with exactly one parameter, then that function is a usual (non-placement)
999 // deallocation function. [...]
1000 if (getNumParams() == 1)
1001 return true;
1002
1003 // C++ [basic.stc.dynamic.deallocation]p2:
1004 // [...] If class T does not declare such an operator delete but does
1005 // declare a member deallocation function named operator delete with
1006 // exactly two parameters, the second of which has type std::size_t (18.1),
1007 // then this function is a usual deallocation function.
1008 ASTContext &Context = getASTContext();
1009 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001010 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1011 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001012 return false;
1013
1014 // This function is a usual deallocation function if there are no
1015 // single-parameter deallocation functions of the same kind.
1016 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1017 R.first != R.second; ++R.first) {
1018 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1019 if (FD->getNumParams() == 1)
1020 return false;
1021 }
1022
1023 return true;
1024}
1025
Douglas Gregor06a9f362010-05-01 20:49:11 +00001026bool CXXMethodDecl::isCopyAssignmentOperator() const {
1027 // C++0x [class.copy]p19:
1028 // A user-declared copy assignment operator X::operator= is a non-static
1029 // non-template member function of class X with exactly one parameter of
1030 // type X, X&, const X&, volatile X& or const volatile X&.
1031 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1032 /*non-static*/ isStatic() ||
1033 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1034 /*exactly one parameter*/getNumParams() != 1)
1035 return false;
1036
1037 QualType ParamType = getParamDecl(0)->getType();
1038 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1039 ParamType = Ref->getPointeeType();
1040
1041 ASTContext &Context = getASTContext();
1042 QualType ClassType
1043 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1044 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1045}
1046
Anders Carlsson05eb2442009-05-16 23:58:37 +00001047void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001048 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001049 assert(!MD->getParent()->isDependentContext() &&
1050 "Can't add an overridden method to a class template!");
1051
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001052 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001053}
1054
1055CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001056 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001057}
1058
1059CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001060 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001061}
1062
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001063unsigned CXXMethodDecl::size_overridden_methods() const {
1064 return getASTContext().overridden_methods_size(this);
1065}
1066
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001067QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001068 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1069 // If the member function is declared const, the type of this is const X*,
1070 // if the member function is declared volatile, the type of this is
1071 // volatile X*, and if the member function is declared const volatile,
1072 // the type of this is const volatile X*.
1073
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001074 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001075
John McCall3cb0ebd2010-03-10 03:28:59 +00001076 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001077 ClassTy = C.getQualifiedType(ClassTy,
1078 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001079 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001080}
1081
Eli Friedmand7d7f672009-12-06 20:50:05 +00001082bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001083 // If this function is a template instantiation, look at the template from
1084 // which it was instantiated.
1085 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1086 if (!CheckFn)
1087 CheckFn = this;
1088
Eli Friedmand7d7f672009-12-06 20:50:05 +00001089 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001090 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001091}
1092
Sean Huntcbb67482011-01-08 20:30:50 +00001093CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1094 TypeSourceInfo *TInfo, bool IsVirtual,
1095 SourceLocation L, Expr *Init,
1096 SourceLocation R,
1097 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001098 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001099 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1100 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001101{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001102}
1103
Sean Huntcbb67482011-01-08 20:30:50 +00001104CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1105 FieldDecl *Member,
1106 SourceLocation MemberLoc,
1107 SourceLocation L, Expr *Init,
1108 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001109 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001110 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1111 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1112{
1113}
1114
Sean Huntcbb67482011-01-08 20:30:50 +00001115CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1116 IndirectFieldDecl *Member,
1117 SourceLocation MemberLoc,
1118 SourceLocation L, Expr *Init,
1119 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001120 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001121 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001122 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001123{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001124}
1125
Sean Huntcbb67482011-01-08 20:30:50 +00001126CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Hunt41717662011-02-26 19:13:13 +00001127 SourceLocation D, SourceLocation L,
1128 CXXConstructorDecl *Target, Expr *Init,
1129 SourceLocation R)
1130 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1131 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1132 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1133{
1134}
1135
1136CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001137 FieldDecl *Member,
1138 SourceLocation MemberLoc,
1139 SourceLocation L, Expr *Init,
1140 SourceLocation R,
1141 VarDecl **Indices,
1142 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001143 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001144 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001145 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001146{
1147 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1148 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1149}
1150
Sean Huntcbb67482011-01-08 20:30:50 +00001151CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1152 FieldDecl *Member,
1153 SourceLocation MemberLoc,
1154 SourceLocation L, Expr *Init,
1155 SourceLocation R,
1156 VarDecl **Indices,
1157 unsigned NumIndices) {
1158 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001159 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001160 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001161 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1162 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001163}
1164
Sean Huntcbb67482011-01-08 20:30:50 +00001165TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001166 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001167 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001168 else
1169 return TypeLoc();
1170}
1171
Sean Huntcbb67482011-01-08 20:30:50 +00001172const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001173 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001174 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001175 else
1176 return 0;
1177}
1178
Sean Huntcbb67482011-01-08 20:30:50 +00001179SourceLocation CXXCtorInitializer::getSourceLocation() const {
Sean Hunt41717662011-02-26 19:13:13 +00001180 if (isAnyMemberInitializer() || isDelegatingInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001181 return getMemberLocation();
1182
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001183 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001184}
1185
Sean Huntcbb67482011-01-08 20:30:50 +00001186SourceRange CXXCtorInitializer::getSourceRange() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001187 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001188}
1189
Douglas Gregorb48fe382008-10-31 09:07:45 +00001190CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001191CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001192 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001193 QualType(), 0, false, false, false);
1194}
1195
1196CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001197CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001198 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001199 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001200 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001201 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001202 bool isInline,
1203 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001204 assert(NameInfo.getName().getNameKind()
1205 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001206 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001207 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
1208 isExplicit, isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001209}
1210
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001211bool CXXConstructorDecl::isDefaultConstructor() const {
1212 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001213 // A default constructor for a class X is a constructor of class
1214 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001215 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001216 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001217}
1218
Mike Stump1eb44332009-09-09 15:08:12 +00001219bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001220CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001221 return isCopyOrMoveConstructor(TypeQuals) &&
1222 getParamDecl(0)->getType()->isLValueReferenceType();
1223}
1224
1225bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1226 return isCopyOrMoveConstructor(TypeQuals) &&
1227 getParamDecl(0)->getType()->isRValueReferenceType();
1228}
1229
1230/// \brief Determine whether this is a copy or move constructor.
1231bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001232 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001233 // A non-template constructor for class X is a copy constructor
1234 // if its first parameter is of type X&, const X&, volatile X& or
1235 // const volatile X&, and either there are no other parameters
1236 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001237 // C++0x [class.copy]p3:
1238 // A non-template constructor for class X is a move constructor if its
1239 // first parameter is of type X&&, const X&&, volatile X&&, or
1240 // const volatile X&&, and either there are no other parameters or else
1241 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001242 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001243 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001244 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001245 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001246 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001247
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001248 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001249
1250 // Do we have a reference type?
1251 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001252 if (!ParamRefType)
1253 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001254
Douglas Gregorfd476482009-11-13 23:59:09 +00001255 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001256 ASTContext &Context = getASTContext();
1257
Douglas Gregorfd476482009-11-13 23:59:09 +00001258 CanQualType PointeeType
1259 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001260 CanQualType ClassTy
1261 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001262 if (PointeeType.getUnqualifiedType() != ClassTy)
1263 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001264
John McCall0953e762009-09-24 19:53:00 +00001265 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001266
1267 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001268 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001269 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001270}
1271
Anders Carlssonfaccd722009-08-28 16:57:08 +00001272bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001273 // C++ [class.conv.ctor]p1:
1274 // A constructor declared without the function-specifier explicit
1275 // that can be called with a single parameter specifies a
1276 // conversion from the type of its first parameter to the type of
1277 // its class. Such a constructor is called a converting
1278 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001279 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001280 return false;
1281
Mike Stump1eb44332009-09-09 15:08:12 +00001282 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001283 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001284 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001285 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001286}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001287
Douglas Gregor6493cc52010-11-08 17:16:59 +00001288bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001289 if ((getNumParams() < 1) ||
1290 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1291 (getPrimaryTemplate() == 0) ||
1292 (getDescribedFunctionTemplate() != 0))
1293 return false;
1294
1295 const ParmVarDecl *Param = getParamDecl(0);
1296
1297 ASTContext &Context = getASTContext();
1298 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1299
Douglas Gregor66724ea2009-11-14 01:20:54 +00001300 // Is it the same as our our class type?
1301 CanQualType ClassTy
1302 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1303 if (ParamType.getUnqualifiedType() != ClassTy)
1304 return false;
1305
1306 return true;
1307}
1308
Sebastian Redlf677ea32011-02-05 19:23:19 +00001309const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1310 // Hack: we store the inherited constructor in the overridden method table
1311 method_iterator It = begin_overridden_methods();
1312 if (It == end_overridden_methods())
1313 return 0;
1314
1315 return cast<CXXConstructorDecl>(*It);
1316}
1317
1318void
1319CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1320 // Hack: we store the inherited constructor in the overridden method table
1321 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1322 addOverriddenMethod(BaseCtor);
1323}
1324
Douglas Gregor42a552f2008-11-05 20:51:48 +00001325CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001326CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001327 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001328 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001329}
1330
1331CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001332CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001333 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001334 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001335 QualType T, TypeSourceInfo *TInfo,
1336 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001337 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001338 assert(NameInfo.getName().getNameKind()
1339 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001340 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001341 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001342 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001343}
1344
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001345CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001346CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001347 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001348 QualType(), 0, false, false,
1349 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001350}
1351
1352CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001353CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001354 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001355 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001356 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001357 bool isInline, bool isExplicit,
1358 SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001359 assert(NameInfo.getName().getNameKind()
1360 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001361 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001362 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001363 isInline, isExplicit, EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001364}
1365
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001366LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001367 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001368 SourceLocation ExternLoc,
1369 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001370 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001371 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001372 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001373}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001374
1375UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1376 SourceLocation L,
1377 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001378 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001379 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001380 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001381 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001382 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1383 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001384 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1385 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001386}
1387
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001388NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1389 if (NamespaceAliasDecl *NA =
1390 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1391 return NA->getNamespace();
1392 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1393}
1394
Mike Stump1eb44332009-09-09 15:08:12 +00001395NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001396 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001397 SourceLocation AliasLoc,
1398 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001399 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001400 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001401 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001402 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1403 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001404 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1405 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001406}
1407
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001408UsingDecl *UsingShadowDecl::getUsingDecl() const {
1409 const UsingShadowDecl *Shadow = this;
1410 while (const UsingShadowDecl *NextShadow =
1411 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1412 Shadow = NextShadow;
1413 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1414}
1415
1416void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1417 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1418 "declaration already in set");
1419 assert(S->getUsingDecl() == this);
1420
1421 if (FirstUsingShadow)
1422 S->UsingOrNextShadow = FirstUsingShadow;
1423 FirstUsingShadow = S;
1424}
1425
1426void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1427 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1428 "declaration not in set");
1429 assert(S->getUsingDecl() == this);
1430
1431 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1432
1433 if (FirstUsingShadow == S) {
1434 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1435 S->UsingOrNextShadow = this;
1436 return;
1437 }
1438
1439 UsingShadowDecl *Prev = FirstUsingShadow;
1440 while (Prev->UsingOrNextShadow != S)
1441 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1442 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1443 S->UsingOrNextShadow = this;
1444}
1445
Douglas Gregordc355712011-02-25 00:36:19 +00001446UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1447 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001448 const DeclarationNameInfo &NameInfo,
1449 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001450 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001451}
1452
John McCall7ba107a2009-11-18 02:36:19 +00001453UnresolvedUsingValueDecl *
1454UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1455 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001456 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001457 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001458 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001459 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001460}
1461
1462UnresolvedUsingTypenameDecl *
1463UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1464 SourceLocation UsingLoc,
1465 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001466 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001467 SourceLocation TargetNameLoc,
1468 DeclarationName TargetName) {
1469 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001470 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001471 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001472}
1473
Anders Carlssonfb311762009-03-14 00:25:26 +00001474StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001475 SourceLocation StaticAssertLoc,
1476 Expr *AssertExpr,
1477 StringLiteral *Message,
1478 SourceLocation RParenLoc) {
1479 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1480 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001481}
1482
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001483static const char *getAccessName(AccessSpecifier AS) {
1484 switch (AS) {
1485 default:
1486 case AS_none:
1487 assert("Invalid access specifier!");
1488 return 0;
1489 case AS_public:
1490 return "public";
1491 case AS_private:
1492 return "private";
1493 case AS_protected:
1494 return "protected";
1495 }
1496}
1497
1498const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1499 AccessSpecifier AS) {
1500 return DB << getAccessName(AS);
1501}