blob: 370cbfc0f1b6e4f55db9f54f8375b7cfdfb3f36e [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 Carlssonb76cc4d2011-01-22 17:22:48 +000039 IsMarkedFinal(false), IsMarkedExplicit(false),
Douglas Gregor7c789c12010-10-29 22:39:52 +000040 NumBases(0), NumVBases(0), Bases(), VBases(),
John McCalld60e22e2010-03-12 01:19:31 +000041 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000042}
43
44CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
45 SourceLocation L, IdentifierInfo *Id,
46 CXXRecordDecl *PrevDecl,
47 SourceLocation TKL)
48 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
49 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000050 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000051
Jay Foad4ba2a172011-01-12 09:06:06 +000052CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
53 DeclContext *DC, SourceLocation L,
54 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000055 CXXRecordDecl* PrevDecl,
56 bool DelayTypeCreation) {
Mike Stump1eb44332009-09-09 15:08:12 +000057 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000058 PrevDecl, TKL);
Mike Stump1eb44332009-09-09 15:08:12 +000059
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000060 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000061 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000062 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000063 return R;
64}
65
Jay Foad4ba2a172011-01-12 09:06:06 +000066CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000067 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(), 0, 0,
68 SourceLocation());
69}
70
Mike Stump1eb44332009-09-09 15:08:12 +000071void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000072CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000073 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000074 ASTContext &C = getASTContext();
75
Mike Stump1eb44332009-09-09 15:08:12 +000076 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000077 // An aggregate is an array or a class (clause 9) with [...]
78 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000079 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000080
Douglas Gregor7c789c12010-10-29 22:39:52 +000081 if (!data().Bases.isOffset() && data().NumBases > 0)
82 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000083
Anders Carlsson6f6de732010-03-29 05:13:12 +000084 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000085 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000086
87 // The virtual bases of this class.
88 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000089
John McCall86ff3082010-02-04 22:26:26 +000090 data().Bases = new(C) CXXBaseSpecifier [NumBases];
91 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000092 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +000093 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000094 // Keep track of inherited vbases for this base class.
95 const CXXBaseSpecifier *Base = Bases[i];
96 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +000097 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000098 if (BaseType->isDependentType())
99 continue;
100 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000101 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000102
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000103 // C++ [dcl.init.aggr]p1:
104 // An aggregate is [...] a class with [...] no base classes [...].
105 data().Aggregate = false;
106
107 // C++ [class]p4:
108 // A POD-struct is an aggregate class...
109 data().PlainOldData = false;
110
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000111 // A class with a non-empty base class is not empty.
112 // FIXME: Standard ref?
113 if (!BaseClassDecl->isEmpty())
114 data().Empty = false;
115
Douglas Gregor85606eb2010-09-28 20:50:54 +0000116 // C++ [class.virtual]p1:
117 // A class that declares or inherits a virtual function is called a
118 // polymorphic class.
119 if (BaseClassDecl->isPolymorphic())
120 data().Polymorphic = true;
121
Anders Carlsson6f6de732010-03-29 05:13:12 +0000122 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000123 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000124 BaseClassDecl->vbases_begin(),
125 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000126 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000127 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000128 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000129 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000130
131 if (Base->isVirtual()) {
132 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000133 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000134 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000135
136 // C++0x [meta.unary.prop] is_empty:
137 // T is a class type, but not a union type, with ... no virtual base
138 // classes
139 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000140
141 // C++ [class.ctor]p5:
142 // A constructor is trivial if its class has no virtual base classes.
143 data().HasTrivialConstructor = false;
144
145 // C++ [class.copy]p6:
146 // A copy constructor is trivial if its class has no virtual base
147 // classes.
148 data().HasTrivialCopyConstructor = false;
149
150 // C++ [class.copy]p11:
151 // A copy assignment operator is trivial if its class has no virtual
152 // base classes.
153 data().HasTrivialCopyAssignment = false;
154 } else {
155 // C++ [class.ctor]p5:
156 // A constructor is trivial if all the direct base classes of its
157 // class have trivial constructors.
158 if (!BaseClassDecl->hasTrivialConstructor())
159 data().HasTrivialConstructor = false;
160
161 // C++ [class.copy]p6:
162 // A copy constructor is trivial if all the direct base classes of its
163 // class have trivial copy constructors.
164 if (!BaseClassDecl->hasTrivialCopyConstructor())
165 data().HasTrivialCopyConstructor = false;
166
167 // C++ [class.copy]p11:
168 // A copy assignment operator is trivial if all the direct base classes
169 // of its class have trivial copy assignment operators.
170 if (!BaseClassDecl->hasTrivialCopyAssignment())
171 data().HasTrivialCopyAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000172 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000173
174 // C++ [class.ctor]p3:
175 // A destructor is trivial if all the direct base classes of its class
176 // have trivial destructors.
177 if (!BaseClassDecl->hasTrivialDestructor())
178 data().HasTrivialDestructor = false;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000179 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000180
181 if (VBases.empty())
182 return;
183
184 // Create base specifier for any direct or indirect virtual bases.
185 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
186 data().NumVBases = VBases.size();
187 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000188 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
189
Anders Carlsson6f6de732010-03-29 05:13:12 +0000190 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000191 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000192 continue;
193
Nick Lewycky56062202010-07-26 16:56:01 +0000194 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
195 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000196
Douglas Gregor7c789c12010-10-29 22:39:52 +0000197 data().getVBases()[I] =
Anders Carlsson6f6de732010-03-29 05:13:12 +0000198 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000199 VBaseClassDecl->getTagKind() == TTK_Class,
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000200 VBases[I]->getAccessSpecifier(), VBaseTypeInfo,
201 SourceLocation());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000202 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000203}
204
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000205/// Callback function for CXXRecordDecl::forallBases that acknowledges
206/// that it saw a base class.
207static bool SawBase(const CXXRecordDecl *, void *) {
208 return true;
209}
210
211bool CXXRecordDecl::hasAnyDependentBases() const {
212 if (!isDependentContext())
213 return false;
214
215 return !forallBases(SawBase, 0);
216}
217
Jay Foad4ba2a172011-01-12 09:06:06 +0000218bool CXXRecordDecl::hasConstCopyConstructor(const ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000219 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000220}
221
Douglas Gregor0d405db2010-07-01 20:59:04 +0000222/// \brief Perform a simplistic form of overload resolution that only considers
223/// cv-qualifiers on a single parameter, and return the best overload candidate
224/// (if there is one).
225static CXXMethodDecl *
226GetBestOverloadCandidateSimple(
227 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
228 if (Cands.empty())
229 return 0;
230 if (Cands.size() == 1)
231 return Cands[0].first;
232
233 unsigned Best = 0, N = Cands.size();
234 for (unsigned I = 1; I != N; ++I)
235 if (Cands[Best].second.isSupersetOf(Cands[I].second))
236 Best = I;
237
238 for (unsigned I = 1; I != N; ++I)
239 if (Cands[Best].second.isSupersetOf(Cands[I].second))
240 return 0;
241
242 return Cands[Best].first;
243}
244
Jay Foad4ba2a172011-01-12 09:06:06 +0000245CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(const ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000246 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000247 QualType ClassType
248 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000249 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000250 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000251 Context.getCanonicalType(ClassType));
252 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000253 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000254 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000255 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000256 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000257 // C++ [class.copy]p2:
258 // A non-template constructor for class X is a copy constructor if [...]
259 if (isa<FunctionTemplateDecl>(*Con))
260 continue;
261
Douglas Gregor0d405db2010-07-01 20:59:04 +0000262 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
263 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000264 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
265 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000266 Found.push_back(std::make_pair(
267 const_cast<CXXConstructorDecl *>(Constructor),
268 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000269 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000270 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000271
272 return cast_or_null<CXXConstructorDecl>(
273 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000274}
275
Douglas Gregorb87786f2010-07-01 17:48:08 +0000276CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
277 ASTContext &Context = getASTContext();
278 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
279 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
280
281 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
282 DeclContext::lookup_const_iterator Op, OpEnd;
283 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
284 // C++ [class.copy]p9:
285 // A user-declared copy assignment operator is a non-static non-template
286 // member function of class X with exactly one parameter of type X, X&,
287 // const X&, volatile X& or const volatile X&.
288 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
289 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
290 continue;
291
292 const FunctionProtoType *FnType
293 = Method->getType()->getAs<FunctionProtoType>();
294 assert(FnType && "Overloaded operator has no prototype.");
295 // Don't assert on this; an invalid decl might have been left in the AST.
296 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
297 continue;
298
299 QualType ArgType = FnType->getArgType(0);
300 Qualifiers Quals;
301 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
302 ArgType = Ref->getPointeeType();
303 // If we have a const argument and we have a reference to a non-const,
304 // this function does not match.
305 if (ArgIsConst && !ArgType.isConstQualified())
306 continue;
307
308 Quals = ArgType.getQualifiers();
309 } else {
310 // By-value copy-assignment operators are treated like const X&
311 // copy-assignment operators.
312 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
313 }
314
315 if (!Context.hasSameUnqualifiedType(ArgType, Class))
316 continue;
317
318 // Save this copy-assignment operator. It might be "the one".
319 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
320 }
321
322 // Use a simplistic form of overload resolution to find the candidate.
323 return GetBestOverloadCandidateSimple(Found);
324}
325
Douglas Gregor21386642010-09-28 21:55:22 +0000326void CXXRecordDecl::markedVirtualFunctionPure() {
327 // C++ [class.abstract]p2:
328 // A class is abstract if it has at least one pure virtual function.
329 data().Abstract = true;
330}
331
332void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000333 // Ignore friends and invalid declarations.
334 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000335 return;
336
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000337 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
338 if (FunTmpl)
339 D = FunTmpl->getTemplatedDecl();
340
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000341 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
342 if (Method->isVirtual()) {
343 // C++ [dcl.init.aggr]p1:
344 // An aggregate is an array or a class with [...] no virtual functions.
345 data().Aggregate = false;
346
347 // C++ [class]p4:
348 // A POD-struct is an aggregate class...
349 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000350
351 // Virtual functions make the class non-empty.
352 // FIXME: Standard ref?
353 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000354
355 // C++ [class.virtual]p1:
356 // A class that declares or inherits a virtual function is called a
357 // polymorphic class.
358 data().Polymorphic = true;
359
360 // None of the special member functions are trivial.
361 data().HasTrivialConstructor = false;
362 data().HasTrivialCopyConstructor = false;
363 data().HasTrivialCopyAssignment = false;
364 // FIXME: Destructor?
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000365 }
366 }
367
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000368 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000369 // Notify that an implicit member was added after the definition
370 // was completed.
371 if (!isBeingDefined())
372 if (ASTMutationListener *L = getASTMutationListener())
373 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000374
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000375 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
376 // If this is the implicit default constructor, note that we have now
377 // declared it.
378 if (Constructor->isDefaultConstructor())
379 data().DeclaredDefaultConstructor = true;
380 // If this is the implicit copy constructor, note that we have now
381 // declared it.
382 else if (Constructor->isCopyConstructor())
383 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000384 return;
385 }
386
387 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000388 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000389 return;
390 }
391
392 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000393 // If this is the implicit copy constructor, note that we have now
394 // declared it.
395 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000396 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000397 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000398 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000399 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000400
401 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000402 }
403
404 // Handle (user-declared) constructors.
405 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
406 // Note that we have a user-declared constructor.
407 data().UserDeclaredConstructor = true;
408
409 // Note that we have no need of an implicitly-declared default constructor.
410 data().DeclaredDefaultConstructor = true;
411
412 // C++ [dcl.init.aggr]p1:
413 // An aggregate is an array or a class (clause 9) with no
414 // user-declared constructors (12.1) [...].
415 data().Aggregate = false;
416
417 // C++ [class]p4:
418 // A POD-struct is an aggregate class [...]
419 data().PlainOldData = false;
420
421 // C++ [class.ctor]p5:
422 // A constructor is trivial if it is an implicitly-declared default
423 // constructor.
424 // FIXME: C++0x: don't do this for "= default" default constructors.
425 data().HasTrivialConstructor = false;
426
427 // Note when we have a user-declared copy constructor, which will
428 // suppress the implicit declaration of a copy constructor.
429 if (!FunTmpl && Constructor->isCopyConstructor()) {
430 data().UserDeclaredCopyConstructor = true;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000431 data().DeclaredCopyConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000432
433 // C++ [class.copy]p6:
434 // A copy constructor is trivial if it is implicitly declared.
435 // FIXME: C++0x: don't do this for "= default" copy constructors.
436 data().HasTrivialCopyConstructor = false;
437 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000438
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000439 return;
440 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000441
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000442 // Handle (user-declared) destructors.
443 if (isa<CXXDestructorDecl>(D)) {
444 data().DeclaredDestructor = true;
445 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000446
447 // C++ [class]p4:
448 // A POD-struct is an aggregate class that has [...] no user-defined
449 // destructor.
450 data().PlainOldData = false;
451
Douglas Gregor85606eb2010-09-28 20:50:54 +0000452 // C++ [class.dtor]p3:
453 // A destructor is trivial if it is an implicitly-declared destructor and
454 // [...].
455 //
456 // FIXME: C++0x: don't do this for "= default" destructors
457 data().HasTrivialDestructor = false;
458
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000459 return;
460 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000461
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000462 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000463 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
464 if (Method->getOverloadedOperator() == OO_Equal) {
465 // We're interested specifically in copy assignment operators.
466 const FunctionProtoType *FnType
467 = Method->getType()->getAs<FunctionProtoType>();
468 assert(FnType && "Overloaded operator has no proto function type.");
469 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
470
471 // Copy assignment operators must be non-templates.
472 if (Method->getPrimaryTemplate() || FunTmpl)
473 return;
474
475 ASTContext &Context = getASTContext();
476 QualType ArgType = FnType->getArgType(0);
477 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
478 ArgType = Ref->getPointeeType();
479
480 ArgType = ArgType.getUnqualifiedType();
481 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
482 const_cast<CXXRecordDecl*>(this)));
483
484 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
485 return;
486
487 // This is a copy assignment operator.
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000488 // FIXME: Move assignment operators.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000489
490 // Suppress the implicit declaration of a copy constructor.
491 data().UserDeclaredCopyAssignment = true;
492 data().DeclaredCopyAssignment = true;
493
494 // C++ [class.copy]p11:
495 // A copy assignment operator is trivial if it is implicitly declared.
496 // FIXME: C++0x: don't do this for "= default" copy operators.
497 data().HasTrivialCopyAssignment = false;
498
499 // C++ [class]p4:
500 // A POD-struct is an aggregate class that [...] has no user-defined copy
501 // assignment operator [...].
502 data().PlainOldData = false;
503 }
Douglas Gregor22584312010-07-02 23:41:54 +0000504
Douglas Gregore80622f2010-09-29 04:25:11 +0000505 // Keep the list of conversion functions up-to-date.
506 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
507 // We don't record specializations.
508 if (Conversion->getPrimaryTemplate())
509 return;
510
511 // FIXME: We intentionally don't use the decl's access here because it
512 // hasn't been set yet. That's really just a misdesign in Sema.
513
514 if (FunTmpl) {
515 if (FunTmpl->getPreviousDeclaration())
516 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
517 FunTmpl);
518 else
519 data().Conversions.addDecl(FunTmpl);
520 } else {
521 if (Conversion->getPreviousDeclaration())
522 data().Conversions.replace(Conversion->getPreviousDeclaration(),
523 Conversion);
524 else
525 data().Conversions.addDecl(Conversion);
526 }
527 }
528
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000529 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000530 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000531
532 // Handle non-static data members.
533 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
534 // C++ [dcl.init.aggr]p1:
535 // An aggregate is an array or a class (clause 9) with [...] no
536 // private or protected non-static data members (clause 11).
537 //
538 // A POD must be an aggregate.
539 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
540 data().Aggregate = false;
541 data().PlainOldData = false;
542 }
543
544 // C++ [class]p9:
545 // A POD struct is a class that is both a trivial class and a
546 // standard-layout class, and has no non-static data members of type
547 // non-POD struct, non-POD union (or array of such types).
548 ASTContext &Context = getASTContext();
549 QualType T = Context.getBaseElementType(Field->getType());
550 if (!T->isPODType())
551 data().PlainOldData = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000552 if (T->isReferenceType())
553 data().HasTrivialConstructor = false;
554
555 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
556 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
557 if (FieldRec->getDefinition()) {
558 if (!FieldRec->hasTrivialConstructor())
559 data().HasTrivialConstructor = false;
560 if (!FieldRec->hasTrivialCopyConstructor())
561 data().HasTrivialCopyConstructor = false;
562 if (!FieldRec->hasTrivialCopyAssignment())
563 data().HasTrivialCopyAssignment = false;
564 if (!FieldRec->hasTrivialDestructor())
565 data().HasTrivialDestructor = false;
566 }
567 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000568
569 // If this is not a zero-length bit-field, then the class is not empty.
570 if (data().Empty) {
571 if (!Field->getBitWidth())
572 data().Empty = false;
573 else if (!Field->getBitWidth()->isTypeDependent() &&
574 !Field->getBitWidth()->isValueDependent()) {
575 llvm::APSInt Bits;
576 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
577 if (!!Bits)
578 data().Empty = false;
579 }
580 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000581 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000582
583 // Handle using declarations of conversion functions.
584 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
585 if (Shadow->getDeclName().getNameKind()
586 == DeclarationName::CXXConversionFunctionName)
587 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000588}
589
John McCallb05b5f32010-03-15 09:07:48 +0000590static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
591 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000592 if (isa<UsingShadowDecl>(Conv))
593 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000594 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
595 T = ConvTemp->getTemplatedDecl()->getResultType();
596 else
597 T = cast<CXXConversionDecl>(Conv)->getConversionType();
598 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000599}
600
John McCallb05b5f32010-03-15 09:07:48 +0000601/// Collect the visible conversions of a base class.
602///
603/// \param Base a base class of the class we're considering
604/// \param InVirtual whether this base class is a virtual base (or a base
605/// of a virtual base)
606/// \param Access the access along the inheritance path to this base
607/// \param ParentHiddenTypes the conversions provided by the inheritors
608/// of this base
609/// \param Output the set to which to add conversions from non-virtual bases
610/// \param VOutput the set to which to add conversions from virtual bases
611/// \param HiddenVBaseCs the set of conversions which were hidden in a
612/// virtual base along some inheritance path
613static void CollectVisibleConversions(ASTContext &Context,
614 CXXRecordDecl *Record,
615 bool InVirtual,
616 AccessSpecifier Access,
617 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
618 UnresolvedSetImpl &Output,
619 UnresolvedSetImpl &VOutput,
620 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
621 // The set of types which have conversions in this class or its
622 // subclasses. As an optimization, we don't copy the derived set
623 // unless it might change.
624 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
625 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
626
627 // Collect the direct conversions and figure out which conversions
628 // will be hidden in the subclasses.
629 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
630 if (!Cs.empty()) {
631 HiddenTypesBuffer = ParentHiddenTypes;
632 HiddenTypes = &HiddenTypesBuffer;
633
634 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
635 bool Hidden =
636 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
637
638 // If this conversion is hidden and we're in a virtual base,
639 // remember that it's hidden along some inheritance path.
640 if (Hidden && InVirtual)
641 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
642
643 // If this conversion isn't hidden, add it to the appropriate output.
644 else if (!Hidden) {
645 AccessSpecifier IAccess
646 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
647
648 if (InVirtual)
649 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000650 else
John McCallb05b5f32010-03-15 09:07:48 +0000651 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000652 }
653 }
654 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000655
John McCallb05b5f32010-03-15 09:07:48 +0000656 // Collect information recursively from any base classes.
657 for (CXXRecordDecl::base_class_iterator
658 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
659 const RecordType *RT = I->getType()->getAs<RecordType>();
660 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000661
John McCallb05b5f32010-03-15 09:07:48 +0000662 AccessSpecifier BaseAccess
663 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
664 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000665
John McCallb05b5f32010-03-15 09:07:48 +0000666 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
667 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
668 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000669 }
John McCallb05b5f32010-03-15 09:07:48 +0000670}
Sebastian Redl9994a342009-10-25 17:03:50 +0000671
John McCallb05b5f32010-03-15 09:07:48 +0000672/// Collect the visible conversions of a class.
673///
674/// This would be extremely straightforward if it weren't for virtual
675/// bases. It might be worth special-casing that, really.
676static void CollectVisibleConversions(ASTContext &Context,
677 CXXRecordDecl *Record,
678 UnresolvedSetImpl &Output) {
679 // The collection of all conversions in virtual bases that we've
680 // found. These will be added to the output as long as they don't
681 // appear in the hidden-conversions set.
682 UnresolvedSet<8> VBaseCs;
683
684 // The set of conversions in virtual bases that we've determined to
685 // be hidden.
686 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
687
688 // The set of types hidden by classes derived from this one.
689 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
690
691 // Go ahead and collect the direct conversions and add them to the
692 // hidden-types set.
693 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
694 Output.append(Cs.begin(), Cs.end());
695 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
696 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
697
698 // Recursively collect conversions from base classes.
699 for (CXXRecordDecl::base_class_iterator
700 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
701 const RecordType *RT = I->getType()->getAs<RecordType>();
702 if (!RT) continue;
703
704 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
705 I->isVirtual(), I->getAccessSpecifier(),
706 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
707 }
708
709 // Add any unhidden conversions provided by virtual bases.
710 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
711 I != E; ++I) {
712 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
713 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000714 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000715}
716
717/// getVisibleConversionFunctions - get all conversion functions visible
718/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000719const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000720 // If root class, all conversions are visible.
721 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000722 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000723 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000724 if (data().ComputedVisibleConversions)
725 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000726 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000727 data().ComputedVisibleConversions = true;
728 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000729}
730
John McCall32daa422010-03-31 01:36:47 +0000731void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
732 // This operation is O(N) but extremely rare. Sema only uses it to
733 // remove UsingShadowDecls in a class that were followed by a direct
734 // declaration, e.g.:
735 // class A : B {
736 // using B::operator int;
737 // operator int();
738 // };
739 // This is uncommon by itself and even more uncommon in conjunction
740 // with sufficiently large numbers of directly-declared conversions
741 // that asymptotic behavior matters.
742
743 UnresolvedSetImpl &Convs = *getConversionFunctions();
744 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
745 if (Convs[I].getDecl() == ConvDecl) {
746 Convs.erase(I);
747 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
748 && "conversion was found multiple times in unresolved set");
749 return;
750 }
751 }
752
753 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000754}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000755
Douglas Gregorf6b11852009-10-08 15:14:33 +0000756CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000757 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000758 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
759
760 return 0;
761}
762
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000763MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
764 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
765}
766
Douglas Gregorf6b11852009-10-08 15:14:33 +0000767void
768CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
769 TemplateSpecializationKind TSK) {
770 assert(TemplateOrInstantiation.isNull() &&
771 "Previous template or instantiation?");
772 assert(!isa<ClassTemplateSpecializationDecl>(this));
773 TemplateOrInstantiation
774 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
775}
776
Anders Carlssonb13e3572009-12-07 06:33:48 +0000777TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
778 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000779 = dyn_cast<ClassTemplateSpecializationDecl>(this))
780 return Spec->getSpecializationKind();
781
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000782 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000783 return MSInfo->getTemplateSpecializationKind();
784
785 return TSK_Undeclared;
786}
787
788void
789CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
790 if (ClassTemplateSpecializationDecl *Spec
791 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
792 Spec->setSpecializationKind(TSK);
793 return;
794 }
795
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000796 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000797 MSInfo->setTemplateSpecializationKind(TSK);
798 return;
799 }
800
801 assert(false && "Not a class template or member class specialization");
802}
803
Douglas Gregor1d110e02010-07-01 14:13:13 +0000804CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
805 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000806 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000807
808 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000809 = Context.DeclarationNames.getCXXDestructorName(
810 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000811
John McCallc0bf4622010-02-23 00:48:20 +0000812 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000813 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000814 if (I == E)
815 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000817 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000818 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Anders Carlsson7267c162009-05-29 21:03:38 +0000820 return Dtor;
821}
822
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000823void CXXRecordDecl::completeDefinition() {
824 completeDefinition(0);
825}
826
827void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
828 RecordDecl::completeDefinition();
829
830 // If the class may be abstract (but hasn't been marked as such), check for
831 // any pure final overriders.
832 if (mayBeAbstract()) {
833 CXXFinalOverriderMap MyFinalOverriders;
834 if (!FinalOverriders) {
835 getFinalOverriders(MyFinalOverriders);
836 FinalOverriders = &MyFinalOverriders;
837 }
838
839 bool Done = false;
840 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
841 MEnd = FinalOverriders->end();
842 M != MEnd && !Done; ++M) {
843 for (OverridingMethods::iterator SO = M->second.begin(),
844 SOEnd = M->second.end();
845 SO != SOEnd && !Done; ++SO) {
846 assert(SO->second.size() > 0 &&
847 "All virtual functions have overridding virtual functions");
848
849 // C++ [class.abstract]p4:
850 // A class is abstract if it contains or inherits at least one
851 // pure virtual function for which the final overrider is pure
852 // virtual.
853 if (SO->second.front().Method->isPure()) {
854 data().Abstract = true;
855 Done = true;
856 break;
857 }
858 }
859 }
860 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000861
862 // Set access bits correctly on the directly-declared conversions.
863 for (UnresolvedSetIterator I = data().Conversions.begin(),
864 E = data().Conversions.end();
865 I != E; ++I)
866 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000867}
868
869bool CXXRecordDecl::mayBeAbstract() const {
870 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
871 isDependentContext())
872 return false;
873
874 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
875 BEnd = bases_end();
876 B != BEnd; ++B) {
877 CXXRecordDecl *BaseDecl
878 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
879 if (BaseDecl->isAbstract())
880 return true;
881 }
882
883 return false;
884}
885
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000886CXXMethodDecl *
887CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000888 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000889 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000890 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000891 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000892 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000893}
894
Douglas Gregor90916562009-09-29 18:16:17 +0000895bool CXXMethodDecl::isUsualDeallocationFunction() const {
896 if (getOverloadedOperator() != OO_Delete &&
897 getOverloadedOperator() != OO_Array_Delete)
898 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000899
900 // C++ [basic.stc.dynamic.deallocation]p2:
901 // A template instance is never a usual deallocation function,
902 // regardless of its signature.
903 if (getPrimaryTemplate())
904 return false;
905
Douglas Gregor90916562009-09-29 18:16:17 +0000906 // C++ [basic.stc.dynamic.deallocation]p2:
907 // If a class T has a member deallocation function named operator delete
908 // with exactly one parameter, then that function is a usual (non-placement)
909 // deallocation function. [...]
910 if (getNumParams() == 1)
911 return true;
912
913 // C++ [basic.stc.dynamic.deallocation]p2:
914 // [...] If class T does not declare such an operator delete but does
915 // declare a member deallocation function named operator delete with
916 // exactly two parameters, the second of which has type std::size_t (18.1),
917 // then this function is a usual deallocation function.
918 ASTContext &Context = getASTContext();
919 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000920 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
921 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000922 return false;
923
924 // This function is a usual deallocation function if there are no
925 // single-parameter deallocation functions of the same kind.
926 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
927 R.first != R.second; ++R.first) {
928 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
929 if (FD->getNumParams() == 1)
930 return false;
931 }
932
933 return true;
934}
935
Douglas Gregor06a9f362010-05-01 20:49:11 +0000936bool CXXMethodDecl::isCopyAssignmentOperator() const {
937 // C++0x [class.copy]p19:
938 // A user-declared copy assignment operator X::operator= is a non-static
939 // non-template member function of class X with exactly one parameter of
940 // type X, X&, const X&, volatile X& or const volatile X&.
941 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
942 /*non-static*/ isStatic() ||
943 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
944 /*exactly one parameter*/getNumParams() != 1)
945 return false;
946
947 QualType ParamType = getParamDecl(0)->getType();
948 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
949 ParamType = Ref->getPointeeType();
950
951 ASTContext &Context = getASTContext();
952 QualType ClassType
953 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
954 return Context.hasSameUnqualifiedType(ClassType, ParamType);
955}
956
Anders Carlsson05eb2442009-05-16 23:58:37 +0000957void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000958 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000959 assert(!MD->getParent()->isDependentContext() &&
960 "Can't add an overridden method to a class template!");
961
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000962 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000963}
964
965CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000966 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000967}
968
969CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000970 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000971}
972
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000973unsigned CXXMethodDecl::size_overridden_methods() const {
974 return getASTContext().overridden_methods_size(this);
975}
976
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000977QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000978 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
979 // If the member function is declared const, the type of this is const X*,
980 // if the member function is declared volatile, the type of this is
981 // volatile X*, and if the member function is declared const volatile,
982 // the type of this is const volatile X*.
983
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000984 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000985
John McCall3cb0ebd2010-03-10 03:28:59 +0000986 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000987 ClassTy = C.getQualifiedType(ClassTy,
988 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000989 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000990}
991
Eli Friedmand7d7f672009-12-06 20:50:05 +0000992bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000993 // If this function is a template instantiation, look at the template from
994 // which it was instantiated.
995 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
996 if (!CheckFn)
997 CheckFn = this;
998
Eli Friedmand7d7f672009-12-06 20:50:05 +0000999 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001000 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001001}
1002
Sean Huntcbb67482011-01-08 20:30:50 +00001003CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1004 TypeSourceInfo *TInfo, bool IsVirtual,
1005 SourceLocation L, Expr *Init,
1006 SourceLocation R,
1007 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001008 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001009 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1010 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001011{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001012}
1013
Sean Huntcbb67482011-01-08 20:30:50 +00001014CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1015 FieldDecl *Member,
1016 SourceLocation MemberLoc,
1017 SourceLocation L, Expr *Init,
1018 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001019 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001020 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1021 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1022{
1023}
1024
Sean Huntcbb67482011-01-08 20:30:50 +00001025CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1026 IndirectFieldDecl *Member,
1027 SourceLocation MemberLoc,
1028 SourceLocation L, Expr *Init,
1029 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001030 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001031 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001032 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001033{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001034}
1035
Sean Huntcbb67482011-01-08 20:30:50 +00001036CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1037 FieldDecl *Member,
1038 SourceLocation MemberLoc,
1039 SourceLocation L, Expr *Init,
1040 SourceLocation R,
1041 VarDecl **Indices,
1042 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001043 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001044 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001045 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001046{
1047 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1048 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1049}
1050
Sean Huntcbb67482011-01-08 20:30:50 +00001051CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1052 FieldDecl *Member,
1053 SourceLocation MemberLoc,
1054 SourceLocation L, Expr *Init,
1055 SourceLocation R,
1056 VarDecl **Indices,
1057 unsigned NumIndices) {
1058 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001059 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001060 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001061 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1062 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001063}
1064
Sean Huntcbb67482011-01-08 20:30:50 +00001065TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001066 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001067 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001068 else
1069 return TypeLoc();
1070}
1071
Sean Huntcbb67482011-01-08 20:30:50 +00001072const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001073 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001074 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001075 else
1076 return 0;
1077}
1078
Sean Huntcbb67482011-01-08 20:30:50 +00001079SourceLocation CXXCtorInitializer::getSourceLocation() const {
Francois Pichet00eb3f92010-12-04 09:14:42 +00001080 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001081 return getMemberLocation();
1082
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001083 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001084}
1085
Sean Huntcbb67482011-01-08 20:30:50 +00001086SourceRange CXXCtorInitializer::getSourceRange() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001087 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001088}
1089
Douglas Gregorb48fe382008-10-31 09:07:45 +00001090CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001091CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001092 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001093 QualType(), 0, false, false, false);
1094}
1095
1096CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001097CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001098 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001099 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001100 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001101 bool isInline,
1102 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001103 assert(NameInfo.getName().getNameKind()
1104 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001105 "Name must refer to a constructor");
Abramo Bagnara25777432010-08-11 22:01:17 +00001106 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001107 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001108}
1109
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001110bool CXXConstructorDecl::isDefaultConstructor() const {
1111 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001112 // A default constructor for a class X is a constructor of class
1113 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001114 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001115 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001116}
1117
Mike Stump1eb44332009-09-09 15:08:12 +00001118bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001119CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001120 return isCopyOrMoveConstructor(TypeQuals) &&
1121 getParamDecl(0)->getType()->isLValueReferenceType();
1122}
1123
1124bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1125 return isCopyOrMoveConstructor(TypeQuals) &&
1126 getParamDecl(0)->getType()->isRValueReferenceType();
1127}
1128
1129/// \brief Determine whether this is a copy or move constructor.
1130bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001131 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001132 // A non-template constructor for class X is a copy constructor
1133 // if its first parameter is of type X&, const X&, volatile X& or
1134 // const volatile X&, and either there are no other parameters
1135 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001136 // C++0x [class.copy]p3:
1137 // A non-template constructor for class X is a move constructor if its
1138 // first parameter is of type X&&, const X&&, volatile X&&, or
1139 // const volatile X&&, and either there are no other parameters or else
1140 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001141 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001142 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001143 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001144 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001145 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001146
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001147 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001148
1149 // Do we have a reference type?
1150 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001151 if (!ParamRefType)
1152 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001153
Douglas Gregorfd476482009-11-13 23:59:09 +00001154 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001155 ASTContext &Context = getASTContext();
1156
Douglas Gregorfd476482009-11-13 23:59:09 +00001157 CanQualType PointeeType
1158 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001159 CanQualType ClassTy
1160 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001161 if (PointeeType.getUnqualifiedType() != ClassTy)
1162 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001163
John McCall0953e762009-09-24 19:53:00 +00001164 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001165
1166 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001167 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001168 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001169}
1170
Anders Carlssonfaccd722009-08-28 16:57:08 +00001171bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001172 // C++ [class.conv.ctor]p1:
1173 // A constructor declared without the function-specifier explicit
1174 // that can be called with a single parameter specifies a
1175 // conversion from the type of its first parameter to the type of
1176 // its class. Such a constructor is called a converting
1177 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001178 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001179 return false;
1180
Mike Stump1eb44332009-09-09 15:08:12 +00001181 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001182 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001183 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001184 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001185}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001186
Douglas Gregor6493cc52010-11-08 17:16:59 +00001187bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001188 if ((getNumParams() < 1) ||
1189 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1190 (getPrimaryTemplate() == 0) ||
1191 (getDescribedFunctionTemplate() != 0))
1192 return false;
1193
1194 const ParmVarDecl *Param = getParamDecl(0);
1195
1196 ASTContext &Context = getASTContext();
1197 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1198
Douglas Gregor66724ea2009-11-14 01:20:54 +00001199 // Is it the same as our our class type?
1200 CanQualType ClassTy
1201 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1202 if (ParamType.getUnqualifiedType() != ClassTy)
1203 return false;
1204
1205 return true;
1206}
1207
Douglas Gregor42a552f2008-11-05 20:51:48 +00001208CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001209CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001210 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001211 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001212}
1213
1214CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001215CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001216 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001217 QualType T, TypeSourceInfo *TInfo,
1218 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001219 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001220 assert(NameInfo.getName().getNameKind()
1221 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001222 "Name must refer to a destructor");
Craig Silversteinb41d8992010-10-21 00:44:50 +00001223 return new (C) CXXDestructorDecl(RD, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001224 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001225}
1226
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001227CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001228CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001229 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001230 QualType(), 0, false, false);
1231}
1232
1233CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001234CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001235 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001236 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001237 bool isInline, bool isExplicit) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001238 assert(NameInfo.getName().getNameKind()
1239 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001240 "Name must refer to a conversion function");
Abramo Bagnara25777432010-08-11 22:01:17 +00001241 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
1242 isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001243}
1244
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001245LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001246 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001247 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +00001248 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +00001249 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001250}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001251
1252UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1253 SourceLocation L,
1254 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001255 SourceRange QualifierRange,
1256 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001257 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001258 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001259 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001260 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1261 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001262 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001263 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001264}
1265
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001266NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1267 if (NamespaceAliasDecl *NA =
1268 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1269 return NA->getNamespace();
1270 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1271}
1272
Mike Stump1eb44332009-09-09 15:08:12 +00001273NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001274 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001275 SourceLocation AliasLoc,
1276 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001277 SourceRange QualifierRange,
1278 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001279 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001280 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001281 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1282 Namespace = NS->getOriginalNamespace();
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001283 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001284 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001285}
1286
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001287UsingDecl *UsingShadowDecl::getUsingDecl() const {
1288 const UsingShadowDecl *Shadow = this;
1289 while (const UsingShadowDecl *NextShadow =
1290 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1291 Shadow = NextShadow;
1292 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1293}
1294
1295void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1296 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1297 "declaration already in set");
1298 assert(S->getUsingDecl() == this);
1299
1300 if (FirstUsingShadow)
1301 S->UsingOrNextShadow = FirstUsingShadow;
1302 FirstUsingShadow = S;
1303}
1304
1305void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1306 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1307 "declaration not in set");
1308 assert(S->getUsingDecl() == this);
1309
1310 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1311
1312 if (FirstUsingShadow == S) {
1313 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1314 S->UsingOrNextShadow = this;
1315 return;
1316 }
1317
1318 UsingShadowDecl *Prev = FirstUsingShadow;
1319 while (Prev->UsingOrNextShadow != S)
1320 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1321 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1322 S->UsingOrNextShadow = this;
1323}
1324
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001325UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001326 SourceRange NNR, SourceLocation UL,
1327 NestedNameSpecifier* TargetNNS,
1328 const DeclarationNameInfo &NameInfo,
1329 bool IsTypeNameArg) {
1330 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001331}
1332
John McCall7ba107a2009-11-18 02:36:19 +00001333UnresolvedUsingValueDecl *
1334UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1335 SourceLocation UsingLoc,
1336 SourceRange TargetNNR,
1337 NestedNameSpecifier *TargetNNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001338 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001339 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001340 TargetNNR, TargetNNS, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001341}
1342
1343UnresolvedUsingTypenameDecl *
1344UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1345 SourceLocation UsingLoc,
1346 SourceLocation TypenameLoc,
1347 SourceRange TargetNNR,
1348 NestedNameSpecifier *TargetNNS,
1349 SourceLocation TargetNameLoc,
1350 DeclarationName TargetName) {
1351 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1352 TargetNNR, TargetNNS,
1353 TargetNameLoc,
1354 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001355}
1356
Anders Carlssonfb311762009-03-14 00:25:26 +00001357StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1358 SourceLocation L, Expr *AssertExpr,
1359 StringLiteral *Message) {
1360 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1361}
1362
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001363static const char *getAccessName(AccessSpecifier AS) {
1364 switch (AS) {
1365 default:
1366 case AS_none:
1367 assert("Invalid access specifier!");
1368 return 0;
1369 case AS_public:
1370 return "public";
1371 case AS_private:
1372 return "private";
1373 case AS_protected:
1374 return "protected";
1375 }
1376}
1377
1378const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1379 AccessSpecifier AS) {
1380 return DB << getAccessName(AS);
1381}