blob: 015b49aed6fdb883cd3c776ddfa8d0f89abc21ab [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),
35 HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true),
Fariborz Jahanian62509212009-09-12 18:26:03 +000036 HasTrivialDestructor(true), ComputedVisibleConversions(false),
Douglas Gregor18274032010-07-03 00:47:00 +000037 DeclaredDefaultConstructor(false), DeclaredCopyConstructor(false),
Douglas Gregora376d102010-07-02 21:50:04 +000038 DeclaredCopyAssignment(false), DeclaredDestructor(false),
Anders Carlssoncb88a1f2011-01-24 16:26:15 +000039 NumBases(0), NumVBases(0), Bases(), VBases(),
40 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000041}
42
43CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000044 SourceLocation StartLoc, SourceLocation IdLoc,
45 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
46 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000047 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000048 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000049
Jay Foad4ba2a172011-01-12 09:06:06 +000050CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000051 DeclContext *DC, SourceLocation StartLoc,
52 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000053 CXXRecordDecl* PrevDecl,
54 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000055 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
56 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000057
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000058 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000059 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000060 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000061 return R;
62}
63
Jay Foad4ba2a172011-01-12 09:06:06 +000064CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000065 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
66 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000067}
68
Mike Stump1eb44332009-09-09 15:08:12 +000069void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000070CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000071 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000072 ASTContext &C = getASTContext();
73
Mike Stump1eb44332009-09-09 15:08:12 +000074 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000075 // An aggregate is an array or a class (clause 9) with [...]
76 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000077 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000078
Douglas Gregor7c789c12010-10-29 22:39:52 +000079 if (!data().Bases.isOffset() && data().NumBases > 0)
80 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000081
Anders Carlsson6f6de732010-03-29 05:13:12 +000082 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000083 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000084
85 // The virtual bases of this class.
86 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000087
John McCall86ff3082010-02-04 22:26:26 +000088 data().Bases = new(C) CXXBaseSpecifier [NumBases];
89 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000090 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +000091 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000092 // Keep track of inherited vbases for this base class.
93 const CXXBaseSpecifier *Base = Bases[i];
94 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +000095 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000096 if (BaseType->isDependentType())
97 continue;
98 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +000099 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000100
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000101 // C++ [dcl.init.aggr]p1:
102 // An aggregate is [...] a class with [...] no base classes [...].
103 data().Aggregate = false;
104
105 // C++ [class]p4:
106 // A POD-struct is an aggregate class...
107 data().PlainOldData = false;
108
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000109 // A class with a non-empty base class is not empty.
110 // FIXME: Standard ref?
111 if (!BaseClassDecl->isEmpty())
112 data().Empty = false;
113
Douglas Gregor85606eb2010-09-28 20:50:54 +0000114 // C++ [class.virtual]p1:
115 // A class that declares or inherits a virtual function is called a
116 // polymorphic class.
117 if (BaseClassDecl->isPolymorphic())
118 data().Polymorphic = true;
119
Anders Carlsson6f6de732010-03-29 05:13:12 +0000120 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000121 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000122 BaseClassDecl->vbases_begin(),
123 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000124 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000125 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000126 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000127 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000128
129 if (Base->isVirtual()) {
130 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000131 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000132 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000133
134 // C++0x [meta.unary.prop] is_empty:
135 // T is a class type, but not a union type, with ... no virtual base
136 // classes
137 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000138
139 // C++ [class.ctor]p5:
140 // A constructor is trivial if its class has no virtual base classes.
141 data().HasTrivialConstructor = false;
142
143 // C++ [class.copy]p6:
144 // A copy constructor is trivial if its class has no virtual base
145 // classes.
146 data().HasTrivialCopyConstructor = false;
147
148 // C++ [class.copy]p11:
149 // A copy assignment operator is trivial if its class has no virtual
150 // base classes.
151 data().HasTrivialCopyAssignment = false;
152 } else {
153 // C++ [class.ctor]p5:
154 // A constructor is trivial if all the direct base classes of its
155 // class have trivial constructors.
156 if (!BaseClassDecl->hasTrivialConstructor())
157 data().HasTrivialConstructor = false;
158
159 // C++ [class.copy]p6:
160 // A copy constructor is trivial if all the direct base classes of its
161 // class have trivial copy constructors.
162 if (!BaseClassDecl->hasTrivialCopyConstructor())
163 data().HasTrivialCopyConstructor = false;
164
165 // C++ [class.copy]p11:
166 // A copy assignment operator is trivial if all the direct base classes
167 // of its class have trivial copy assignment operators.
168 if (!BaseClassDecl->hasTrivialCopyAssignment())
169 data().HasTrivialCopyAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000170 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000171
172 // C++ [class.ctor]p3:
173 // A destructor is trivial if all the direct base classes of its class
174 // have trivial destructors.
175 if (!BaseClassDecl->hasTrivialDestructor())
176 data().HasTrivialDestructor = false;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000177 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000178
179 if (VBases.empty())
180 return;
181
182 // Create base specifier for any direct or indirect virtual bases.
183 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
184 data().NumVBases = VBases.size();
185 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000186 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
187
Anders Carlsson6f6de732010-03-29 05:13:12 +0000188 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000189 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000190 continue;
191
Nick Lewycky56062202010-07-26 16:56:01 +0000192 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
193 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000194
Douglas Gregor7c789c12010-10-29 22:39:52 +0000195 data().getVBases()[I] =
Anders Carlsson6f6de732010-03-29 05:13:12 +0000196 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000197 VBaseClassDecl->getTagKind() == TTK_Class,
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000198 VBases[I]->getAccessSpecifier(), VBaseTypeInfo,
199 SourceLocation());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000200 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000201}
202
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000203/// Callback function for CXXRecordDecl::forallBases that acknowledges
204/// that it saw a base class.
205static bool SawBase(const CXXRecordDecl *, void *) {
206 return true;
207}
208
209bool CXXRecordDecl::hasAnyDependentBases() const {
210 if (!isDependentContext())
211 return false;
212
213 return !forallBases(SawBase, 0);
214}
215
Jay Foad4ba2a172011-01-12 09:06:06 +0000216bool CXXRecordDecl::hasConstCopyConstructor(const ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000217 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000218}
219
Chandler Carruthb7e95892011-04-23 10:47:28 +0000220bool CXXRecordDecl::isTriviallyCopyable() const {
221 // C++0x [class]p5:
222 // A trivially copyable class is a class that:
223 // -- has no non-trivial copy constructors,
224 if (!hasTrivialCopyConstructor()) return false;
225 // -- has no non-trivial move constructors,
226 // FIXME: C++0x: Track and check trivial move constructors.
227 // -- has no non-trivial copy assignment operators,
228 if (!hasTrivialCopyAssignment()) return false;
229 // -- has no non-trivial move assignment operators, and
230 // FIXME: C++0x: Track and check trivial move assignment operators.
231 // -- has a trivial destructor.
232 if (!hasTrivialDestructor()) return false;
233
234 return true;
235}
236
Douglas Gregor0d405db2010-07-01 20:59:04 +0000237/// \brief Perform a simplistic form of overload resolution that only considers
238/// cv-qualifiers on a single parameter, and return the best overload candidate
239/// (if there is one).
240static CXXMethodDecl *
241GetBestOverloadCandidateSimple(
242 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
243 if (Cands.empty())
244 return 0;
245 if (Cands.size() == 1)
246 return Cands[0].first;
247
248 unsigned Best = 0, N = Cands.size();
249 for (unsigned I = 1; I != N; ++I)
250 if (Cands[Best].second.isSupersetOf(Cands[I].second))
251 Best = I;
252
253 for (unsigned I = 1; I != N; ++I)
254 if (Cands[Best].second.isSupersetOf(Cands[I].second))
255 return 0;
256
257 return Cands[Best].first;
258}
259
Jay Foad4ba2a172011-01-12 09:06:06 +0000260CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(const ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000261 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000262 QualType ClassType
263 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000264 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000265 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000266 Context.getCanonicalType(ClassType));
267 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000268 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000269 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000270 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000271 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000272 // C++ [class.copy]p2:
273 // A non-template constructor for class X is a copy constructor if [...]
274 if (isa<FunctionTemplateDecl>(*Con))
275 continue;
276
Douglas Gregor0d405db2010-07-01 20:59:04 +0000277 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
278 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000279 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
280 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000281 Found.push_back(std::make_pair(
282 const_cast<CXXConstructorDecl *>(Constructor),
283 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000284 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000285 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000286
287 return cast_or_null<CXXConstructorDecl>(
288 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000289}
290
Douglas Gregorb87786f2010-07-01 17:48:08 +0000291CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
292 ASTContext &Context = getASTContext();
293 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
294 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
295
296 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
297 DeclContext::lookup_const_iterator Op, OpEnd;
298 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
299 // C++ [class.copy]p9:
300 // A user-declared copy assignment operator is a non-static non-template
301 // member function of class X with exactly one parameter of type X, X&,
302 // const X&, volatile X& or const volatile X&.
303 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
304 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
305 continue;
306
307 const FunctionProtoType *FnType
308 = Method->getType()->getAs<FunctionProtoType>();
309 assert(FnType && "Overloaded operator has no prototype.");
310 // Don't assert on this; an invalid decl might have been left in the AST.
311 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
312 continue;
313
314 QualType ArgType = FnType->getArgType(0);
315 Qualifiers Quals;
316 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
317 ArgType = Ref->getPointeeType();
318 // If we have a const argument and we have a reference to a non-const,
319 // this function does not match.
320 if (ArgIsConst && !ArgType.isConstQualified())
321 continue;
322
323 Quals = ArgType.getQualifiers();
324 } else {
325 // By-value copy-assignment operators are treated like const X&
326 // copy-assignment operators.
327 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
328 }
329
330 if (!Context.hasSameUnqualifiedType(ArgType, Class))
331 continue;
332
333 // Save this copy-assignment operator. It might be "the one".
334 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
335 }
336
337 // Use a simplistic form of overload resolution to find the candidate.
338 return GetBestOverloadCandidateSimple(Found);
339}
340
Douglas Gregor21386642010-09-28 21:55:22 +0000341void CXXRecordDecl::markedVirtualFunctionPure() {
342 // C++ [class.abstract]p2:
343 // A class is abstract if it has at least one pure virtual function.
344 data().Abstract = true;
345}
346
347void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000348 // Ignore friends and invalid declarations.
349 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000350 return;
351
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000352 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
353 if (FunTmpl)
354 D = FunTmpl->getTemplatedDecl();
355
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000356 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
357 if (Method->isVirtual()) {
358 // C++ [dcl.init.aggr]p1:
359 // An aggregate is an array or a class with [...] no virtual functions.
360 data().Aggregate = false;
361
362 // C++ [class]p4:
363 // A POD-struct is an aggregate class...
364 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000365
366 // Virtual functions make the class non-empty.
367 // FIXME: Standard ref?
368 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000369
370 // C++ [class.virtual]p1:
371 // A class that declares or inherits a virtual function is called a
372 // polymorphic class.
373 data().Polymorphic = true;
374
375 // None of the special member functions are trivial.
376 data().HasTrivialConstructor = false;
377 data().HasTrivialCopyConstructor = false;
378 data().HasTrivialCopyAssignment = false;
379 // FIXME: Destructor?
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000380 }
381 }
382
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000383 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000384 // Notify that an implicit member was added after the definition
385 // was completed.
386 if (!isBeingDefined())
387 if (ASTMutationListener *L = getASTMutationListener())
388 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000389
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000390 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
391 // If this is the implicit default constructor, note that we have now
392 // declared it.
393 if (Constructor->isDefaultConstructor())
394 data().DeclaredDefaultConstructor = true;
395 // If this is the implicit copy constructor, note that we have now
396 // declared it.
397 else if (Constructor->isCopyConstructor())
398 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000399 return;
400 }
401
402 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000403 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000404 return;
405 }
406
407 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000408 // If this is the implicit copy constructor, note that we have now
409 // declared it.
410 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000411 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000412 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000413 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000414 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000415
416 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000417 }
418
419 // Handle (user-declared) constructors.
420 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
421 // Note that we have a user-declared constructor.
422 data().UserDeclaredConstructor = true;
423
424 // Note that we have no need of an implicitly-declared default constructor.
425 data().DeclaredDefaultConstructor = true;
426
427 // C++ [dcl.init.aggr]p1:
428 // An aggregate is an array or a class (clause 9) with no
429 // user-declared constructors (12.1) [...].
430 data().Aggregate = false;
431
432 // C++ [class]p4:
433 // A POD-struct is an aggregate class [...]
434 data().PlainOldData = false;
435
436 // C++ [class.ctor]p5:
437 // A constructor is trivial if it is an implicitly-declared default
438 // constructor.
439 // FIXME: C++0x: don't do this for "= default" default constructors.
440 data().HasTrivialConstructor = false;
441
442 // Note when we have a user-declared copy constructor, which will
443 // suppress the implicit declaration of a copy constructor.
444 if (!FunTmpl && Constructor->isCopyConstructor()) {
445 data().UserDeclaredCopyConstructor = true;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000446 data().DeclaredCopyConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000447
448 // C++ [class.copy]p6:
449 // A copy constructor is trivial if it is implicitly declared.
450 // FIXME: C++0x: don't do this for "= default" copy constructors.
451 data().HasTrivialCopyConstructor = false;
452 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000453
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000454 return;
455 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000456
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000457 // Handle (user-declared) destructors.
458 if (isa<CXXDestructorDecl>(D)) {
459 data().DeclaredDestructor = true;
460 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000461
462 // C++ [class]p4:
463 // A POD-struct is an aggregate class that has [...] no user-defined
464 // destructor.
465 data().PlainOldData = false;
466
Douglas Gregor85606eb2010-09-28 20:50:54 +0000467 // C++ [class.dtor]p3:
468 // A destructor is trivial if it is an implicitly-declared destructor and
469 // [...].
470 //
471 // FIXME: C++0x: don't do this for "= default" destructors
472 data().HasTrivialDestructor = false;
473
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000474 return;
475 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000476
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000477 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000478 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
479 if (Method->getOverloadedOperator() == OO_Equal) {
480 // We're interested specifically in copy assignment operators.
481 const FunctionProtoType *FnType
482 = Method->getType()->getAs<FunctionProtoType>();
483 assert(FnType && "Overloaded operator has no proto function type.");
484 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
485
486 // Copy assignment operators must be non-templates.
487 if (Method->getPrimaryTemplate() || FunTmpl)
488 return;
489
490 ASTContext &Context = getASTContext();
491 QualType ArgType = FnType->getArgType(0);
492 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
493 ArgType = Ref->getPointeeType();
494
495 ArgType = ArgType.getUnqualifiedType();
496 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
497 const_cast<CXXRecordDecl*>(this)));
498
499 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
500 return;
501
502 // This is a copy assignment operator.
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000503 // FIXME: Move assignment operators.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000504
505 // Suppress the implicit declaration of a copy constructor.
506 data().UserDeclaredCopyAssignment = true;
507 data().DeclaredCopyAssignment = true;
508
509 // C++ [class.copy]p11:
510 // A copy assignment operator is trivial if it is implicitly declared.
511 // FIXME: C++0x: don't do this for "= default" copy operators.
512 data().HasTrivialCopyAssignment = false;
513
514 // C++ [class]p4:
515 // A POD-struct is an aggregate class that [...] has no user-defined copy
516 // assignment operator [...].
517 data().PlainOldData = false;
518 }
Douglas Gregor22584312010-07-02 23:41:54 +0000519
Douglas Gregore80622f2010-09-29 04:25:11 +0000520 // Keep the list of conversion functions up-to-date.
521 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
522 // We don't record specializations.
523 if (Conversion->getPrimaryTemplate())
524 return;
525
526 // FIXME: We intentionally don't use the decl's access here because it
527 // hasn't been set yet. That's really just a misdesign in Sema.
528
529 if (FunTmpl) {
530 if (FunTmpl->getPreviousDeclaration())
531 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
532 FunTmpl);
533 else
534 data().Conversions.addDecl(FunTmpl);
535 } else {
536 if (Conversion->getPreviousDeclaration())
537 data().Conversions.replace(Conversion->getPreviousDeclaration(),
538 Conversion);
539 else
540 data().Conversions.addDecl(Conversion);
541 }
542 }
543
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000544 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000545 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000546
547 // Handle non-static data members.
548 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
549 // C++ [dcl.init.aggr]p1:
550 // An aggregate is an array or a class (clause 9) with [...] no
551 // private or protected non-static data members (clause 11).
552 //
553 // A POD must be an aggregate.
554 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
555 data().Aggregate = false;
556 data().PlainOldData = false;
557 }
558
559 // C++ [class]p9:
560 // A POD struct is a class that is both a trivial class and a
561 // standard-layout class, and has no non-static data members of type
562 // non-POD struct, non-POD union (or array of such types).
563 ASTContext &Context = getASTContext();
564 QualType T = Context.getBaseElementType(Field->getType());
565 if (!T->isPODType())
566 data().PlainOldData = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000567 if (T->isReferenceType())
568 data().HasTrivialConstructor = false;
569
570 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
571 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
572 if (FieldRec->getDefinition()) {
573 if (!FieldRec->hasTrivialConstructor())
574 data().HasTrivialConstructor = false;
575 if (!FieldRec->hasTrivialCopyConstructor())
576 data().HasTrivialCopyConstructor = false;
577 if (!FieldRec->hasTrivialCopyAssignment())
578 data().HasTrivialCopyAssignment = false;
579 if (!FieldRec->hasTrivialDestructor())
580 data().HasTrivialDestructor = false;
581 }
582 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000583
584 // If this is not a zero-length bit-field, then the class is not empty.
585 if (data().Empty) {
586 if (!Field->getBitWidth())
587 data().Empty = false;
588 else if (!Field->getBitWidth()->isTypeDependent() &&
589 !Field->getBitWidth()->isValueDependent()) {
590 llvm::APSInt Bits;
591 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
592 if (!!Bits)
593 data().Empty = false;
594 }
595 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000596 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000597
598 // Handle using declarations of conversion functions.
599 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
600 if (Shadow->getDeclName().getNameKind()
601 == DeclarationName::CXXConversionFunctionName)
602 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000603}
604
John McCallb05b5f32010-03-15 09:07:48 +0000605static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
606 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000607 if (isa<UsingShadowDecl>(Conv))
608 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000609 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
610 T = ConvTemp->getTemplatedDecl()->getResultType();
611 else
612 T = cast<CXXConversionDecl>(Conv)->getConversionType();
613 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000614}
615
John McCallb05b5f32010-03-15 09:07:48 +0000616/// Collect the visible conversions of a base class.
617///
618/// \param Base a base class of the class we're considering
619/// \param InVirtual whether this base class is a virtual base (or a base
620/// of a virtual base)
621/// \param Access the access along the inheritance path to this base
622/// \param ParentHiddenTypes the conversions provided by the inheritors
623/// of this base
624/// \param Output the set to which to add conversions from non-virtual bases
625/// \param VOutput the set to which to add conversions from virtual bases
626/// \param HiddenVBaseCs the set of conversions which were hidden in a
627/// virtual base along some inheritance path
628static void CollectVisibleConversions(ASTContext &Context,
629 CXXRecordDecl *Record,
630 bool InVirtual,
631 AccessSpecifier Access,
632 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
633 UnresolvedSetImpl &Output,
634 UnresolvedSetImpl &VOutput,
635 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
636 // The set of types which have conversions in this class or its
637 // subclasses. As an optimization, we don't copy the derived set
638 // unless it might change.
639 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
640 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
641
642 // Collect the direct conversions and figure out which conversions
643 // will be hidden in the subclasses.
644 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
645 if (!Cs.empty()) {
646 HiddenTypesBuffer = ParentHiddenTypes;
647 HiddenTypes = &HiddenTypesBuffer;
648
649 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
650 bool Hidden =
651 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
652
653 // If this conversion is hidden and we're in a virtual base,
654 // remember that it's hidden along some inheritance path.
655 if (Hidden && InVirtual)
656 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
657
658 // If this conversion isn't hidden, add it to the appropriate output.
659 else if (!Hidden) {
660 AccessSpecifier IAccess
661 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
662
663 if (InVirtual)
664 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000665 else
John McCallb05b5f32010-03-15 09:07:48 +0000666 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000667 }
668 }
669 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000670
John McCallb05b5f32010-03-15 09:07:48 +0000671 // Collect information recursively from any base classes.
672 for (CXXRecordDecl::base_class_iterator
673 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
674 const RecordType *RT = I->getType()->getAs<RecordType>();
675 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000676
John McCallb05b5f32010-03-15 09:07:48 +0000677 AccessSpecifier BaseAccess
678 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
679 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000680
John McCallb05b5f32010-03-15 09:07:48 +0000681 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
682 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
683 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000684 }
John McCallb05b5f32010-03-15 09:07:48 +0000685}
Sebastian Redl9994a342009-10-25 17:03:50 +0000686
John McCallb05b5f32010-03-15 09:07:48 +0000687/// Collect the visible conversions of a class.
688///
689/// This would be extremely straightforward if it weren't for virtual
690/// bases. It might be worth special-casing that, really.
691static void CollectVisibleConversions(ASTContext &Context,
692 CXXRecordDecl *Record,
693 UnresolvedSetImpl &Output) {
694 // The collection of all conversions in virtual bases that we've
695 // found. These will be added to the output as long as they don't
696 // appear in the hidden-conversions set.
697 UnresolvedSet<8> VBaseCs;
698
699 // The set of conversions in virtual bases that we've determined to
700 // be hidden.
701 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
702
703 // The set of types hidden by classes derived from this one.
704 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
705
706 // Go ahead and collect the direct conversions and add them to the
707 // hidden-types set.
708 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
709 Output.append(Cs.begin(), Cs.end());
710 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
711 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
712
713 // Recursively collect conversions from base classes.
714 for (CXXRecordDecl::base_class_iterator
715 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
716 const RecordType *RT = I->getType()->getAs<RecordType>();
717 if (!RT) continue;
718
719 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
720 I->isVirtual(), I->getAccessSpecifier(),
721 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
722 }
723
724 // Add any unhidden conversions provided by virtual bases.
725 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
726 I != E; ++I) {
727 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
728 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000729 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000730}
731
732/// getVisibleConversionFunctions - get all conversion functions visible
733/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000734const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000735 // If root class, all conversions are visible.
736 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000737 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000738 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000739 if (data().ComputedVisibleConversions)
740 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000741 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000742 data().ComputedVisibleConversions = true;
743 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000744}
745
John McCall32daa422010-03-31 01:36:47 +0000746void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
747 // This operation is O(N) but extremely rare. Sema only uses it to
748 // remove UsingShadowDecls in a class that were followed by a direct
749 // declaration, e.g.:
750 // class A : B {
751 // using B::operator int;
752 // operator int();
753 // };
754 // This is uncommon by itself and even more uncommon in conjunction
755 // with sufficiently large numbers of directly-declared conversions
756 // that asymptotic behavior matters.
757
758 UnresolvedSetImpl &Convs = *getConversionFunctions();
759 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
760 if (Convs[I].getDecl() == ConvDecl) {
761 Convs.erase(I);
762 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
763 && "conversion was found multiple times in unresolved set");
764 return;
765 }
766 }
767
768 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000769}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000770
Douglas Gregorf6b11852009-10-08 15:14:33 +0000771CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000772 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000773 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
774
775 return 0;
776}
777
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000778MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
779 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
780}
781
Douglas Gregorf6b11852009-10-08 15:14:33 +0000782void
783CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
784 TemplateSpecializationKind TSK) {
785 assert(TemplateOrInstantiation.isNull() &&
786 "Previous template or instantiation?");
787 assert(!isa<ClassTemplateSpecializationDecl>(this));
788 TemplateOrInstantiation
789 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
790}
791
Anders Carlssonb13e3572009-12-07 06:33:48 +0000792TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
793 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000794 = dyn_cast<ClassTemplateSpecializationDecl>(this))
795 return Spec->getSpecializationKind();
796
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000797 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000798 return MSInfo->getTemplateSpecializationKind();
799
800 return TSK_Undeclared;
801}
802
803void
804CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
805 if (ClassTemplateSpecializationDecl *Spec
806 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
807 Spec->setSpecializationKind(TSK);
808 return;
809 }
810
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000811 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000812 MSInfo->setTemplateSpecializationKind(TSK);
813 return;
814 }
815
816 assert(false && "Not a class template or member class specialization");
817}
818
Douglas Gregor1d110e02010-07-01 14:13:13 +0000819CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
820 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000821 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000822
823 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000824 = Context.DeclarationNames.getCXXDestructorName(
825 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000826
John McCallc0bf4622010-02-23 00:48:20 +0000827 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000828 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000829 if (I == E)
830 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000832 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000833 return Dtor;
834}
835
Douglas Gregorda2142f2011-02-19 18:51:44 +0000836void CXXRecordDecl::completeDefinition() {
837 completeDefinition(0);
838}
839
840void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
841 RecordDecl::completeDefinition();
842
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000843 // If the class may be abstract (but hasn't been marked as such), check for
844 // any pure final overriders.
845 if (mayBeAbstract()) {
846 CXXFinalOverriderMap MyFinalOverriders;
847 if (!FinalOverriders) {
848 getFinalOverriders(MyFinalOverriders);
849 FinalOverriders = &MyFinalOverriders;
850 }
851
852 bool Done = false;
853 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
854 MEnd = FinalOverriders->end();
855 M != MEnd && !Done; ++M) {
856 for (OverridingMethods::iterator SO = M->second.begin(),
857 SOEnd = M->second.end();
858 SO != SOEnd && !Done; ++SO) {
859 assert(SO->second.size() > 0 &&
860 "All virtual functions have overridding virtual functions");
861
862 // C++ [class.abstract]p4:
863 // A class is abstract if it contains or inherits at least one
864 // pure virtual function for which the final overrider is pure
865 // virtual.
866 if (SO->second.front().Method->isPure()) {
867 data().Abstract = true;
868 Done = true;
869 break;
870 }
871 }
872 }
873 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000874
875 // Set access bits correctly on the directly-declared conversions.
876 for (UnresolvedSetIterator I = data().Conversions.begin(),
877 E = data().Conversions.end();
878 I != E; ++I)
879 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000880}
881
882bool CXXRecordDecl::mayBeAbstract() const {
883 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
884 isDependentContext())
885 return false;
886
887 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
888 BEnd = bases_end();
889 B != BEnd; ++B) {
890 CXXRecordDecl *BaseDecl
891 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
892 if (BaseDecl->isAbstract())
893 return true;
894 }
895
896 return false;
897}
898
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000899CXXMethodDecl *
900CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000901 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000902 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000903 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +0000904 bool isStatic, StorageClass SCAsWritten, bool isInline,
905 SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000906 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +0000907 isStatic, SCAsWritten, isInline, EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000908}
909
Douglas Gregor90916562009-09-29 18:16:17 +0000910bool CXXMethodDecl::isUsualDeallocationFunction() const {
911 if (getOverloadedOperator() != OO_Delete &&
912 getOverloadedOperator() != OO_Array_Delete)
913 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000914
915 // C++ [basic.stc.dynamic.deallocation]p2:
916 // A template instance is never a usual deallocation function,
917 // regardless of its signature.
918 if (getPrimaryTemplate())
919 return false;
920
Douglas Gregor90916562009-09-29 18:16:17 +0000921 // C++ [basic.stc.dynamic.deallocation]p2:
922 // If a class T has a member deallocation function named operator delete
923 // with exactly one parameter, then that function is a usual (non-placement)
924 // deallocation function. [...]
925 if (getNumParams() == 1)
926 return true;
927
928 // C++ [basic.stc.dynamic.deallocation]p2:
929 // [...] If class T does not declare such an operator delete but does
930 // declare a member deallocation function named operator delete with
931 // exactly two parameters, the second of which has type std::size_t (18.1),
932 // then this function is a usual deallocation function.
933 ASTContext &Context = getASTContext();
934 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000935 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
936 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000937 return false;
938
939 // This function is a usual deallocation function if there are no
940 // single-parameter deallocation functions of the same kind.
941 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
942 R.first != R.second; ++R.first) {
943 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
944 if (FD->getNumParams() == 1)
945 return false;
946 }
947
948 return true;
949}
950
Douglas Gregor06a9f362010-05-01 20:49:11 +0000951bool CXXMethodDecl::isCopyAssignmentOperator() const {
952 // C++0x [class.copy]p19:
953 // A user-declared copy assignment operator X::operator= is a non-static
954 // non-template member function of class X with exactly one parameter of
955 // type X, X&, const X&, volatile X& or const volatile X&.
956 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
957 /*non-static*/ isStatic() ||
958 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
959 /*exactly one parameter*/getNumParams() != 1)
960 return false;
961
962 QualType ParamType = getParamDecl(0)->getType();
963 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
964 ParamType = Ref->getPointeeType();
965
966 ASTContext &Context = getASTContext();
967 QualType ClassType
968 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
969 return Context.hasSameUnqualifiedType(ClassType, ParamType);
970}
971
Anders Carlsson05eb2442009-05-16 23:58:37 +0000972void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000973 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000974 assert(!MD->getParent()->isDependentContext() &&
975 "Can't add an overridden method to a class template!");
976
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000977 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000978}
979
980CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000981 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000982}
983
984CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000985 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000986}
987
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000988unsigned CXXMethodDecl::size_overridden_methods() const {
989 return getASTContext().overridden_methods_size(this);
990}
991
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000992QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000993 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
994 // If the member function is declared const, the type of this is const X*,
995 // if the member function is declared volatile, the type of this is
996 // volatile X*, and if the member function is declared const volatile,
997 // the type of this is const volatile X*.
998
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000999 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001000
John McCall3cb0ebd2010-03-10 03:28:59 +00001001 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001002 ClassTy = C.getQualifiedType(ClassTy,
1003 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001004 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001005}
1006
Eli Friedmand7d7f672009-12-06 20:50:05 +00001007bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001008 // If this function is a template instantiation, look at the template from
1009 // which it was instantiated.
1010 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1011 if (!CheckFn)
1012 CheckFn = this;
1013
Eli Friedmand7d7f672009-12-06 20:50:05 +00001014 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001015 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001016}
1017
Sean Huntcbb67482011-01-08 20:30:50 +00001018CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1019 TypeSourceInfo *TInfo, bool IsVirtual,
1020 SourceLocation L, Expr *Init,
1021 SourceLocation R,
1022 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001023 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001024 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1025 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001026{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001027}
1028
Sean Huntcbb67482011-01-08 20:30:50 +00001029CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1030 FieldDecl *Member,
1031 SourceLocation MemberLoc,
1032 SourceLocation L, Expr *Init,
1033 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001034 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001035 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1036 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1037{
1038}
1039
Sean Huntcbb67482011-01-08 20:30:50 +00001040CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1041 IndirectFieldDecl *Member,
1042 SourceLocation MemberLoc,
1043 SourceLocation L, Expr *Init,
1044 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001045 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001046 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001047 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001048{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001049}
1050
Sean Huntcbb67482011-01-08 20:30:50 +00001051CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Hunt41717662011-02-26 19:13:13 +00001052 SourceLocation D, SourceLocation L,
1053 CXXConstructorDecl *Target, Expr *Init,
1054 SourceLocation R)
1055 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1056 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1057 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1058{
1059}
1060
1061CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001062 FieldDecl *Member,
1063 SourceLocation MemberLoc,
1064 SourceLocation L, Expr *Init,
1065 SourceLocation R,
1066 VarDecl **Indices,
1067 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001068 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001069 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001070 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001071{
1072 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1073 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1074}
1075
Sean Huntcbb67482011-01-08 20:30:50 +00001076CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1077 FieldDecl *Member,
1078 SourceLocation MemberLoc,
1079 SourceLocation L, Expr *Init,
1080 SourceLocation R,
1081 VarDecl **Indices,
1082 unsigned NumIndices) {
1083 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001084 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001085 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001086 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1087 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001088}
1089
Sean Huntcbb67482011-01-08 20:30:50 +00001090TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001091 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001092 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001093 else
1094 return TypeLoc();
1095}
1096
Sean Huntcbb67482011-01-08 20:30:50 +00001097const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001098 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001099 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001100 else
1101 return 0;
1102}
1103
Sean Huntcbb67482011-01-08 20:30:50 +00001104SourceLocation CXXCtorInitializer::getSourceLocation() const {
Sean Hunt41717662011-02-26 19:13:13 +00001105 if (isAnyMemberInitializer() || isDelegatingInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001106 return getMemberLocation();
1107
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001108 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001109}
1110
Sean Huntcbb67482011-01-08 20:30:50 +00001111SourceRange CXXCtorInitializer::getSourceRange() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001112 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001113}
1114
Douglas Gregorb48fe382008-10-31 09:07:45 +00001115CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001116CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001117 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001118 QualType(), 0, false, false, false);
1119}
1120
1121CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001122CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001123 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001124 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001125 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001126 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001127 bool isInline,
1128 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001129 assert(NameInfo.getName().getNameKind()
1130 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001131 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001132 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
1133 isExplicit, isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001134}
1135
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001136bool CXXConstructorDecl::isDefaultConstructor() const {
1137 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001138 // A default constructor for a class X is a constructor of class
1139 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001140 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001141 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001142}
1143
Mike Stump1eb44332009-09-09 15:08:12 +00001144bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001145CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001146 return isCopyOrMoveConstructor(TypeQuals) &&
1147 getParamDecl(0)->getType()->isLValueReferenceType();
1148}
1149
1150bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1151 return isCopyOrMoveConstructor(TypeQuals) &&
1152 getParamDecl(0)->getType()->isRValueReferenceType();
1153}
1154
1155/// \brief Determine whether this is a copy or move constructor.
1156bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001157 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001158 // A non-template constructor for class X is a copy constructor
1159 // if its first parameter is of type X&, const X&, volatile X& or
1160 // const volatile X&, and either there are no other parameters
1161 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001162 // C++0x [class.copy]p3:
1163 // A non-template constructor for class X is a move constructor if its
1164 // first parameter is of type X&&, const X&&, volatile X&&, or
1165 // const volatile X&&, and either there are no other parameters or else
1166 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001167 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001168 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001169 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001170 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001171 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001172
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001173 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001174
1175 // Do we have a reference type?
1176 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001177 if (!ParamRefType)
1178 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001179
Douglas Gregorfd476482009-11-13 23:59:09 +00001180 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001181 ASTContext &Context = getASTContext();
1182
Douglas Gregorfd476482009-11-13 23:59:09 +00001183 CanQualType PointeeType
1184 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001185 CanQualType ClassTy
1186 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001187 if (PointeeType.getUnqualifiedType() != ClassTy)
1188 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001189
John McCall0953e762009-09-24 19:53:00 +00001190 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001191
1192 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001193 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001194 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001195}
1196
Anders Carlssonfaccd722009-08-28 16:57:08 +00001197bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001198 // C++ [class.conv.ctor]p1:
1199 // A constructor declared without the function-specifier explicit
1200 // that can be called with a single parameter specifies a
1201 // conversion from the type of its first parameter to the type of
1202 // its class. Such a constructor is called a converting
1203 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001204 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001205 return false;
1206
Mike Stump1eb44332009-09-09 15:08:12 +00001207 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001208 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001209 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001210 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001211}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001212
Douglas Gregor6493cc52010-11-08 17:16:59 +00001213bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001214 if ((getNumParams() < 1) ||
1215 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1216 (getPrimaryTemplate() == 0) ||
1217 (getDescribedFunctionTemplate() != 0))
1218 return false;
1219
1220 const ParmVarDecl *Param = getParamDecl(0);
1221
1222 ASTContext &Context = getASTContext();
1223 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1224
Douglas Gregor66724ea2009-11-14 01:20:54 +00001225 // Is it the same as our our class type?
1226 CanQualType ClassTy
1227 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1228 if (ParamType.getUnqualifiedType() != ClassTy)
1229 return false;
1230
1231 return true;
1232}
1233
Sebastian Redlf677ea32011-02-05 19:23:19 +00001234const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1235 // Hack: we store the inherited constructor in the overridden method table
1236 method_iterator It = begin_overridden_methods();
1237 if (It == end_overridden_methods())
1238 return 0;
1239
1240 return cast<CXXConstructorDecl>(*It);
1241}
1242
1243void
1244CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1245 // Hack: we store the inherited constructor in the overridden method table
1246 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1247 addOverriddenMethod(BaseCtor);
1248}
1249
Douglas Gregor42a552f2008-11-05 20:51:48 +00001250CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001251CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001252 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001253 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001254}
1255
1256CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001257CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001258 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001259 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001260 QualType T, TypeSourceInfo *TInfo,
1261 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001262 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001263 assert(NameInfo.getName().getNameKind()
1264 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001265 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001266 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001267 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001268}
1269
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001270CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001271CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001272 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001273 QualType(), 0, false, false,
1274 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001275}
1276
1277CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001278CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001279 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001280 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001281 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001282 bool isInline, bool isExplicit,
1283 SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001284 assert(NameInfo.getName().getNameKind()
1285 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001286 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001287 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001288 isInline, isExplicit, EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001289}
1290
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001291LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001292 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001293 SourceLocation ExternLoc,
1294 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001295 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001296 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001297 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001298}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001299
1300UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1301 SourceLocation L,
1302 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001303 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001304 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001305 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001306 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001307 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1308 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001309 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1310 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001311}
1312
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001313NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1314 if (NamespaceAliasDecl *NA =
1315 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1316 return NA->getNamespace();
1317 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1318}
1319
Mike Stump1eb44332009-09-09 15:08:12 +00001320NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001321 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001322 SourceLocation AliasLoc,
1323 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001324 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001325 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001326 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001327 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1328 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001329 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1330 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001331}
1332
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001333UsingDecl *UsingShadowDecl::getUsingDecl() const {
1334 const UsingShadowDecl *Shadow = this;
1335 while (const UsingShadowDecl *NextShadow =
1336 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1337 Shadow = NextShadow;
1338 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1339}
1340
1341void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1342 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1343 "declaration already in set");
1344 assert(S->getUsingDecl() == this);
1345
1346 if (FirstUsingShadow)
1347 S->UsingOrNextShadow = FirstUsingShadow;
1348 FirstUsingShadow = S;
1349}
1350
1351void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1352 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1353 "declaration not in set");
1354 assert(S->getUsingDecl() == this);
1355
1356 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1357
1358 if (FirstUsingShadow == S) {
1359 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1360 S->UsingOrNextShadow = this;
1361 return;
1362 }
1363
1364 UsingShadowDecl *Prev = FirstUsingShadow;
1365 while (Prev->UsingOrNextShadow != S)
1366 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1367 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1368 S->UsingOrNextShadow = this;
1369}
1370
Douglas Gregordc355712011-02-25 00:36:19 +00001371UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1372 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001373 const DeclarationNameInfo &NameInfo,
1374 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001375 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001376}
1377
John McCall7ba107a2009-11-18 02:36:19 +00001378UnresolvedUsingValueDecl *
1379UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1380 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001381 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001382 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001383 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001384 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001385}
1386
1387UnresolvedUsingTypenameDecl *
1388UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1389 SourceLocation UsingLoc,
1390 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001391 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001392 SourceLocation TargetNameLoc,
1393 DeclarationName TargetName) {
1394 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001395 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001396 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001397}
1398
Anders Carlssonfb311762009-03-14 00:25:26 +00001399StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001400 SourceLocation StaticAssertLoc,
1401 Expr *AssertExpr,
1402 StringLiteral *Message,
1403 SourceLocation RParenLoc) {
1404 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1405 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001406}
1407
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001408static const char *getAccessName(AccessSpecifier AS) {
1409 switch (AS) {
1410 default:
1411 case AS_none:
1412 assert("Invalid access specifier!");
1413 return 0;
1414 case AS_public:
1415 return "public";
1416 case AS_private:
1417 return "private";
1418 case AS_protected:
1419 return "protected";
1420 }
1421}
1422
1423const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1424 AccessSpecifier AS) {
1425 return DB << getAccessName(AS);
1426}