blob: aafe480e80c0b55bad9643767e6f8e01aaab2412 [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,
44 SourceLocation L, IdentifierInfo *Id,
45 CXXRecordDecl *PrevDecl,
46 SourceLocation TKL)
47 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
48 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000049 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000050
Jay Foad4ba2a172011-01-12 09:06:06 +000051CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
52 DeclContext *DC, SourceLocation L,
53 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000054 CXXRecordDecl* PrevDecl,
55 bool DelayTypeCreation) {
Mike Stump1eb44332009-09-09 15:08:12 +000056 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000057 PrevDecl, TKL);
Mike Stump1eb44332009-09-09 15:08:12 +000058
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000059 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000060 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000061 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000062 return R;
63}
64
Jay Foad4ba2a172011-01-12 09:06:06 +000065CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000066 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(), 0, 0,
67 SourceLocation());
68}
69
Mike Stump1eb44332009-09-09 15:08:12 +000070void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000071CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000072 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000073 ASTContext &C = getASTContext();
74
Mike Stump1eb44332009-09-09 15:08:12 +000075 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000076 // An aggregate is an array or a class (clause 9) with [...]
77 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000078 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000079
Douglas Gregor7c789c12010-10-29 22:39:52 +000080 if (!data().Bases.isOffset() && data().NumBases > 0)
81 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000082
Anders Carlsson6f6de732010-03-29 05:13:12 +000083 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000084 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000085
86 // The virtual bases of this class.
87 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000088
John McCall86ff3082010-02-04 22:26:26 +000089 data().Bases = new(C) CXXBaseSpecifier [NumBases];
90 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000091 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +000092 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000093 // Keep track of inherited vbases for this base class.
94 const CXXBaseSpecifier *Base = Bases[i];
95 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +000096 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000097 if (BaseType->isDependentType())
98 continue;
99 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000100 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000101
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000102 // C++ [dcl.init.aggr]p1:
103 // An aggregate is [...] a class with [...] no base classes [...].
104 data().Aggregate = false;
105
106 // C++ [class]p4:
107 // A POD-struct is an aggregate class...
108 data().PlainOldData = false;
109
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000110 // A class with a non-empty base class is not empty.
111 // FIXME: Standard ref?
112 if (!BaseClassDecl->isEmpty())
113 data().Empty = false;
114
Douglas Gregor85606eb2010-09-28 20:50:54 +0000115 // C++ [class.virtual]p1:
116 // A class that declares or inherits a virtual function is called a
117 // polymorphic class.
118 if (BaseClassDecl->isPolymorphic())
119 data().Polymorphic = true;
120
Anders Carlsson6f6de732010-03-29 05:13:12 +0000121 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000122 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000123 BaseClassDecl->vbases_begin(),
124 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000125 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000126 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000127 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000128 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000129
130 if (Base->isVirtual()) {
131 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000132 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000133 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000134
135 // C++0x [meta.unary.prop] is_empty:
136 // T is a class type, but not a union type, with ... no virtual base
137 // classes
138 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000139
140 // C++ [class.ctor]p5:
141 // A constructor is trivial if its class has no virtual base classes.
142 data().HasTrivialConstructor = false;
143
144 // C++ [class.copy]p6:
145 // A copy constructor is trivial if its class has no virtual base
146 // classes.
147 data().HasTrivialCopyConstructor = false;
148
149 // C++ [class.copy]p11:
150 // A copy assignment operator is trivial if its class has no virtual
151 // base classes.
152 data().HasTrivialCopyAssignment = false;
153 } else {
154 // C++ [class.ctor]p5:
155 // A constructor is trivial if all the direct base classes of its
156 // class have trivial constructors.
157 if (!BaseClassDecl->hasTrivialConstructor())
158 data().HasTrivialConstructor = false;
159
160 // C++ [class.copy]p6:
161 // A copy constructor is trivial if all the direct base classes of its
162 // class have trivial copy constructors.
163 if (!BaseClassDecl->hasTrivialCopyConstructor())
164 data().HasTrivialCopyConstructor = false;
165
166 // C++ [class.copy]p11:
167 // A copy assignment operator is trivial if all the direct base classes
168 // of its class have trivial copy assignment operators.
169 if (!BaseClassDecl->hasTrivialCopyAssignment())
170 data().HasTrivialCopyAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000171 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000172
173 // C++ [class.ctor]p3:
174 // A destructor is trivial if all the direct base classes of its class
175 // have trivial destructors.
176 if (!BaseClassDecl->hasTrivialDestructor())
177 data().HasTrivialDestructor = false;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000178 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000179
180 if (VBases.empty())
181 return;
182
183 // Create base specifier for any direct or indirect virtual bases.
184 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
185 data().NumVBases = VBases.size();
186 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000187 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
188
Anders Carlsson6f6de732010-03-29 05:13:12 +0000189 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000190 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000191 continue;
192
Nick Lewycky56062202010-07-26 16:56:01 +0000193 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
194 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000195
Douglas Gregor7c789c12010-10-29 22:39:52 +0000196 data().getVBases()[I] =
Anders Carlsson6f6de732010-03-29 05:13:12 +0000197 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000198 VBaseClassDecl->getTagKind() == TTK_Class,
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000199 VBases[I]->getAccessSpecifier(), VBaseTypeInfo,
200 SourceLocation());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000201 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000202}
203
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000204/// Callback function for CXXRecordDecl::forallBases that acknowledges
205/// that it saw a base class.
206static bool SawBase(const CXXRecordDecl *, void *) {
207 return true;
208}
209
210bool CXXRecordDecl::hasAnyDependentBases() const {
211 if (!isDependentContext())
212 return false;
213
214 return !forallBases(SawBase, 0);
215}
216
Jay Foad4ba2a172011-01-12 09:06:06 +0000217bool CXXRecordDecl::hasConstCopyConstructor(const ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000218 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000219}
220
Douglas Gregor0d405db2010-07-01 20:59:04 +0000221/// \brief Perform a simplistic form of overload resolution that only considers
222/// cv-qualifiers on a single parameter, and return the best overload candidate
223/// (if there is one).
224static CXXMethodDecl *
225GetBestOverloadCandidateSimple(
226 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
227 if (Cands.empty())
228 return 0;
229 if (Cands.size() == 1)
230 return Cands[0].first;
231
232 unsigned Best = 0, N = Cands.size();
233 for (unsigned I = 1; I != N; ++I)
234 if (Cands[Best].second.isSupersetOf(Cands[I].second))
235 Best = I;
236
237 for (unsigned I = 1; I != N; ++I)
238 if (Cands[Best].second.isSupersetOf(Cands[I].second))
239 return 0;
240
241 return Cands[Best].first;
242}
243
Jay Foad4ba2a172011-01-12 09:06:06 +0000244CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(const ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000245 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000246 QualType ClassType
247 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000248 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000249 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000250 Context.getCanonicalType(ClassType));
251 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000252 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000253 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000254 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000255 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000256 // C++ [class.copy]p2:
257 // A non-template constructor for class X is a copy constructor if [...]
258 if (isa<FunctionTemplateDecl>(*Con))
259 continue;
260
Douglas Gregor0d405db2010-07-01 20:59:04 +0000261 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
262 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000263 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
264 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000265 Found.push_back(std::make_pair(
266 const_cast<CXXConstructorDecl *>(Constructor),
267 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000268 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000269 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000270
271 return cast_or_null<CXXConstructorDecl>(
272 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000273}
274
Douglas Gregorb87786f2010-07-01 17:48:08 +0000275CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
276 ASTContext &Context = getASTContext();
277 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
278 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
279
280 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
281 DeclContext::lookup_const_iterator Op, OpEnd;
282 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
283 // C++ [class.copy]p9:
284 // A user-declared copy assignment operator is a non-static non-template
285 // member function of class X with exactly one parameter of type X, X&,
286 // const X&, volatile X& or const volatile X&.
287 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
288 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
289 continue;
290
291 const FunctionProtoType *FnType
292 = Method->getType()->getAs<FunctionProtoType>();
293 assert(FnType && "Overloaded operator has no prototype.");
294 // Don't assert on this; an invalid decl might have been left in the AST.
295 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
296 continue;
297
298 QualType ArgType = FnType->getArgType(0);
299 Qualifiers Quals;
300 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
301 ArgType = Ref->getPointeeType();
302 // If we have a const argument and we have a reference to a non-const,
303 // this function does not match.
304 if (ArgIsConst && !ArgType.isConstQualified())
305 continue;
306
307 Quals = ArgType.getQualifiers();
308 } else {
309 // By-value copy-assignment operators are treated like const X&
310 // copy-assignment operators.
311 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
312 }
313
314 if (!Context.hasSameUnqualifiedType(ArgType, Class))
315 continue;
316
317 // Save this copy-assignment operator. It might be "the one".
318 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
319 }
320
321 // Use a simplistic form of overload resolution to find the candidate.
322 return GetBestOverloadCandidateSimple(Found);
323}
324
Douglas Gregor21386642010-09-28 21:55:22 +0000325void CXXRecordDecl::markedVirtualFunctionPure() {
326 // C++ [class.abstract]p2:
327 // A class is abstract if it has at least one pure virtual function.
328 data().Abstract = true;
329}
330
331void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000332 // Ignore friends and invalid declarations.
333 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000334 return;
335
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000336 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
337 if (FunTmpl)
338 D = FunTmpl->getTemplatedDecl();
339
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000340 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
341 if (Method->isVirtual()) {
342 // C++ [dcl.init.aggr]p1:
343 // An aggregate is an array or a class with [...] no virtual functions.
344 data().Aggregate = false;
345
346 // C++ [class]p4:
347 // A POD-struct is an aggregate class...
348 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000349
350 // Virtual functions make the class non-empty.
351 // FIXME: Standard ref?
352 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000353
354 // C++ [class.virtual]p1:
355 // A class that declares or inherits a virtual function is called a
356 // polymorphic class.
357 data().Polymorphic = true;
358
359 // None of the special member functions are trivial.
360 data().HasTrivialConstructor = false;
361 data().HasTrivialCopyConstructor = false;
362 data().HasTrivialCopyAssignment = false;
363 // FIXME: Destructor?
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000364 }
365 }
366
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000367 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000368 // Notify that an implicit member was added after the definition
369 // was completed.
370 if (!isBeingDefined())
371 if (ASTMutationListener *L = getASTMutationListener())
372 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000373
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000374 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
375 // If this is the implicit default constructor, note that we have now
376 // declared it.
377 if (Constructor->isDefaultConstructor())
378 data().DeclaredDefaultConstructor = true;
379 // If this is the implicit copy constructor, note that we have now
380 // declared it.
381 else if (Constructor->isCopyConstructor())
382 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000383 return;
384 }
385
386 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000387 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000388 return;
389 }
390
391 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000392 // If this is the implicit copy constructor, note that we have now
393 // declared it.
394 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000395 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000396 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000397 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000398 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000399
400 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000401 }
402
403 // Handle (user-declared) constructors.
404 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
405 // Note that we have a user-declared constructor.
406 data().UserDeclaredConstructor = true;
407
408 // Note that we have no need of an implicitly-declared default constructor.
409 data().DeclaredDefaultConstructor = true;
410
411 // C++ [dcl.init.aggr]p1:
412 // An aggregate is an array or a class (clause 9) with no
413 // user-declared constructors (12.1) [...].
414 data().Aggregate = false;
415
416 // C++ [class]p4:
417 // A POD-struct is an aggregate class [...]
418 data().PlainOldData = false;
419
420 // C++ [class.ctor]p5:
421 // A constructor is trivial if it is an implicitly-declared default
422 // constructor.
423 // FIXME: C++0x: don't do this for "= default" default constructors.
424 data().HasTrivialConstructor = false;
425
426 // Note when we have a user-declared copy constructor, which will
427 // suppress the implicit declaration of a copy constructor.
428 if (!FunTmpl && Constructor->isCopyConstructor()) {
429 data().UserDeclaredCopyConstructor = true;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000430 data().DeclaredCopyConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000431
432 // C++ [class.copy]p6:
433 // A copy constructor is trivial if it is implicitly declared.
434 // FIXME: C++0x: don't do this for "= default" copy constructors.
435 data().HasTrivialCopyConstructor = false;
436 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000437
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000438 return;
439 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000440
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000441 // Handle (user-declared) destructors.
442 if (isa<CXXDestructorDecl>(D)) {
443 data().DeclaredDestructor = true;
444 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000445
446 // C++ [class]p4:
447 // A POD-struct is an aggregate class that has [...] no user-defined
448 // destructor.
449 data().PlainOldData = false;
450
Douglas Gregor85606eb2010-09-28 20:50:54 +0000451 // C++ [class.dtor]p3:
452 // A destructor is trivial if it is an implicitly-declared destructor and
453 // [...].
454 //
455 // FIXME: C++0x: don't do this for "= default" destructors
456 data().HasTrivialDestructor = false;
457
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000458 return;
459 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000460
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000461 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000462 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
463 if (Method->getOverloadedOperator() == OO_Equal) {
464 // We're interested specifically in copy assignment operators.
465 const FunctionProtoType *FnType
466 = Method->getType()->getAs<FunctionProtoType>();
467 assert(FnType && "Overloaded operator has no proto function type.");
468 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
469
470 // Copy assignment operators must be non-templates.
471 if (Method->getPrimaryTemplate() || FunTmpl)
472 return;
473
474 ASTContext &Context = getASTContext();
475 QualType ArgType = FnType->getArgType(0);
476 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
477 ArgType = Ref->getPointeeType();
478
479 ArgType = ArgType.getUnqualifiedType();
480 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
481 const_cast<CXXRecordDecl*>(this)));
482
483 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
484 return;
485
486 // This is a copy assignment operator.
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000487 // FIXME: Move assignment operators.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000488
489 // Suppress the implicit declaration of a copy constructor.
490 data().UserDeclaredCopyAssignment = true;
491 data().DeclaredCopyAssignment = true;
492
493 // C++ [class.copy]p11:
494 // A copy assignment operator is trivial if it is implicitly declared.
495 // FIXME: C++0x: don't do this for "= default" copy operators.
496 data().HasTrivialCopyAssignment = false;
497
498 // C++ [class]p4:
499 // A POD-struct is an aggregate class that [...] has no user-defined copy
500 // assignment operator [...].
501 data().PlainOldData = false;
502 }
Douglas Gregor22584312010-07-02 23:41:54 +0000503
Douglas Gregore80622f2010-09-29 04:25:11 +0000504 // Keep the list of conversion functions up-to-date.
505 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
506 // We don't record specializations.
507 if (Conversion->getPrimaryTemplate())
508 return;
509
510 // FIXME: We intentionally don't use the decl's access here because it
511 // hasn't been set yet. That's really just a misdesign in Sema.
512
513 if (FunTmpl) {
514 if (FunTmpl->getPreviousDeclaration())
515 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
516 FunTmpl);
517 else
518 data().Conversions.addDecl(FunTmpl);
519 } else {
520 if (Conversion->getPreviousDeclaration())
521 data().Conversions.replace(Conversion->getPreviousDeclaration(),
522 Conversion);
523 else
524 data().Conversions.addDecl(Conversion);
525 }
526 }
527
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000528 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000529 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000530
531 // Handle non-static data members.
532 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
533 // C++ [dcl.init.aggr]p1:
534 // An aggregate is an array or a class (clause 9) with [...] no
535 // private or protected non-static data members (clause 11).
536 //
537 // A POD must be an aggregate.
538 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
539 data().Aggregate = false;
540 data().PlainOldData = false;
541 }
542
543 // C++ [class]p9:
544 // A POD struct is a class that is both a trivial class and a
545 // standard-layout class, and has no non-static data members of type
546 // non-POD struct, non-POD union (or array of such types).
547 ASTContext &Context = getASTContext();
548 QualType T = Context.getBaseElementType(Field->getType());
549 if (!T->isPODType())
550 data().PlainOldData = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000551 if (T->isReferenceType())
552 data().HasTrivialConstructor = false;
553
554 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
555 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
556 if (FieldRec->getDefinition()) {
557 if (!FieldRec->hasTrivialConstructor())
558 data().HasTrivialConstructor = false;
559 if (!FieldRec->hasTrivialCopyConstructor())
560 data().HasTrivialCopyConstructor = false;
561 if (!FieldRec->hasTrivialCopyAssignment())
562 data().HasTrivialCopyAssignment = false;
563 if (!FieldRec->hasTrivialDestructor())
564 data().HasTrivialDestructor = false;
565 }
566 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000567
568 // If this is not a zero-length bit-field, then the class is not empty.
569 if (data().Empty) {
570 if (!Field->getBitWidth())
571 data().Empty = false;
572 else if (!Field->getBitWidth()->isTypeDependent() &&
573 !Field->getBitWidth()->isValueDependent()) {
574 llvm::APSInt Bits;
575 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
576 if (!!Bits)
577 data().Empty = false;
578 }
579 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000580 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000581
582 // Handle using declarations of conversion functions.
583 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
584 if (Shadow->getDeclName().getNameKind()
585 == DeclarationName::CXXConversionFunctionName)
586 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000587}
588
John McCallb05b5f32010-03-15 09:07:48 +0000589static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
590 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000591 if (isa<UsingShadowDecl>(Conv))
592 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000593 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
594 T = ConvTemp->getTemplatedDecl()->getResultType();
595 else
596 T = cast<CXXConversionDecl>(Conv)->getConversionType();
597 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000598}
599
John McCallb05b5f32010-03-15 09:07:48 +0000600/// Collect the visible conversions of a base class.
601///
602/// \param Base a base class of the class we're considering
603/// \param InVirtual whether this base class is a virtual base (or a base
604/// of a virtual base)
605/// \param Access the access along the inheritance path to this base
606/// \param ParentHiddenTypes the conversions provided by the inheritors
607/// of this base
608/// \param Output the set to which to add conversions from non-virtual bases
609/// \param VOutput the set to which to add conversions from virtual bases
610/// \param HiddenVBaseCs the set of conversions which were hidden in a
611/// virtual base along some inheritance path
612static void CollectVisibleConversions(ASTContext &Context,
613 CXXRecordDecl *Record,
614 bool InVirtual,
615 AccessSpecifier Access,
616 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
617 UnresolvedSetImpl &Output,
618 UnresolvedSetImpl &VOutput,
619 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
620 // The set of types which have conversions in this class or its
621 // subclasses. As an optimization, we don't copy the derived set
622 // unless it might change.
623 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
624 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
625
626 // Collect the direct conversions and figure out which conversions
627 // will be hidden in the subclasses.
628 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
629 if (!Cs.empty()) {
630 HiddenTypesBuffer = ParentHiddenTypes;
631 HiddenTypes = &HiddenTypesBuffer;
632
633 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
634 bool Hidden =
635 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
636
637 // If this conversion is hidden and we're in a virtual base,
638 // remember that it's hidden along some inheritance path.
639 if (Hidden && InVirtual)
640 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
641
642 // If this conversion isn't hidden, add it to the appropriate output.
643 else if (!Hidden) {
644 AccessSpecifier IAccess
645 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
646
647 if (InVirtual)
648 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000649 else
John McCallb05b5f32010-03-15 09:07:48 +0000650 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000651 }
652 }
653 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000654
John McCallb05b5f32010-03-15 09:07:48 +0000655 // Collect information recursively from any base classes.
656 for (CXXRecordDecl::base_class_iterator
657 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
658 const RecordType *RT = I->getType()->getAs<RecordType>();
659 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000660
John McCallb05b5f32010-03-15 09:07:48 +0000661 AccessSpecifier BaseAccess
662 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
663 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000664
John McCallb05b5f32010-03-15 09:07:48 +0000665 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
666 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
667 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000668 }
John McCallb05b5f32010-03-15 09:07:48 +0000669}
Sebastian Redl9994a342009-10-25 17:03:50 +0000670
John McCallb05b5f32010-03-15 09:07:48 +0000671/// Collect the visible conversions of a class.
672///
673/// This would be extremely straightforward if it weren't for virtual
674/// bases. It might be worth special-casing that, really.
675static void CollectVisibleConversions(ASTContext &Context,
676 CXXRecordDecl *Record,
677 UnresolvedSetImpl &Output) {
678 // The collection of all conversions in virtual bases that we've
679 // found. These will be added to the output as long as they don't
680 // appear in the hidden-conversions set.
681 UnresolvedSet<8> VBaseCs;
682
683 // The set of conversions in virtual bases that we've determined to
684 // be hidden.
685 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
686
687 // The set of types hidden by classes derived from this one.
688 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
689
690 // Go ahead and collect the direct conversions and add them to the
691 // hidden-types set.
692 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
693 Output.append(Cs.begin(), Cs.end());
694 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
695 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
696
697 // Recursively collect conversions from base classes.
698 for (CXXRecordDecl::base_class_iterator
699 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
700 const RecordType *RT = I->getType()->getAs<RecordType>();
701 if (!RT) continue;
702
703 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
704 I->isVirtual(), I->getAccessSpecifier(),
705 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
706 }
707
708 // Add any unhidden conversions provided by virtual bases.
709 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
710 I != E; ++I) {
711 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
712 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000713 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000714}
715
716/// getVisibleConversionFunctions - get all conversion functions visible
717/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000718const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000719 // If root class, all conversions are visible.
720 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000721 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000722 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000723 if (data().ComputedVisibleConversions)
724 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000725 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000726 data().ComputedVisibleConversions = true;
727 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000728}
729
John McCall32daa422010-03-31 01:36:47 +0000730void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
731 // This operation is O(N) but extremely rare. Sema only uses it to
732 // remove UsingShadowDecls in a class that were followed by a direct
733 // declaration, e.g.:
734 // class A : B {
735 // using B::operator int;
736 // operator int();
737 // };
738 // This is uncommon by itself and even more uncommon in conjunction
739 // with sufficiently large numbers of directly-declared conversions
740 // that asymptotic behavior matters.
741
742 UnresolvedSetImpl &Convs = *getConversionFunctions();
743 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
744 if (Convs[I].getDecl() == ConvDecl) {
745 Convs.erase(I);
746 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
747 && "conversion was found multiple times in unresolved set");
748 return;
749 }
750 }
751
752 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000753}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000754
Douglas Gregorf6b11852009-10-08 15:14:33 +0000755CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000756 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000757 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
758
759 return 0;
760}
761
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000762MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
763 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
764}
765
Douglas Gregorf6b11852009-10-08 15:14:33 +0000766void
767CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
768 TemplateSpecializationKind TSK) {
769 assert(TemplateOrInstantiation.isNull() &&
770 "Previous template or instantiation?");
771 assert(!isa<ClassTemplateSpecializationDecl>(this));
772 TemplateOrInstantiation
773 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
774}
775
Anders Carlssonb13e3572009-12-07 06:33:48 +0000776TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
777 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000778 = dyn_cast<ClassTemplateSpecializationDecl>(this))
779 return Spec->getSpecializationKind();
780
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000781 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000782 return MSInfo->getTemplateSpecializationKind();
783
784 return TSK_Undeclared;
785}
786
787void
788CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
789 if (ClassTemplateSpecializationDecl *Spec
790 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
791 Spec->setSpecializationKind(TSK);
792 return;
793 }
794
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000795 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000796 MSInfo->setTemplateSpecializationKind(TSK);
797 return;
798 }
799
800 assert(false && "Not a class template or member class specialization");
801}
802
Douglas Gregor1d110e02010-07-01 14:13:13 +0000803CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
804 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000805 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000806
807 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000808 = Context.DeclarationNames.getCXXDestructorName(
809 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000810
John McCallc0bf4622010-02-23 00:48:20 +0000811 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000812 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000813 if (I == E)
814 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000816 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000817 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Anders Carlsson7267c162009-05-29 21:03:38 +0000819 return Dtor;
820}
821
Douglas Gregorda2142f2011-02-19 18:51:44 +0000822void CXXRecordDecl::completeDefinition() {
823 completeDefinition(0);
824}
825
826void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
827 RecordDecl::completeDefinition();
828
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000829 // If the class may be abstract (but hasn't been marked as such), check for
830 // any pure final overriders.
831 if (mayBeAbstract()) {
832 CXXFinalOverriderMap MyFinalOverriders;
833 if (!FinalOverriders) {
834 getFinalOverriders(MyFinalOverriders);
835 FinalOverriders = &MyFinalOverriders;
836 }
837
838 bool Done = false;
839 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
840 MEnd = FinalOverriders->end();
841 M != MEnd && !Done; ++M) {
842 for (OverridingMethods::iterator SO = M->second.begin(),
843 SOEnd = M->second.end();
844 SO != SOEnd && !Done; ++SO) {
845 assert(SO->second.size() > 0 &&
846 "All virtual functions have overridding virtual functions");
847
848 // C++ [class.abstract]p4:
849 // A class is abstract if it contains or inherits at least one
850 // pure virtual function for which the final overrider is pure
851 // virtual.
852 if (SO->second.front().Method->isPure()) {
853 data().Abstract = true;
854 Done = true;
855 break;
856 }
857 }
858 }
859 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000860
861 // Set access bits correctly on the directly-declared conversions.
862 for (UnresolvedSetIterator I = data().Conversions.begin(),
863 E = data().Conversions.end();
864 I != E; ++I)
865 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000866}
867
868bool CXXRecordDecl::mayBeAbstract() const {
869 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
870 isDependentContext())
871 return false;
872
873 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
874 BEnd = bases_end();
875 B != BEnd; ++B) {
876 CXXRecordDecl *BaseDecl
877 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
878 if (BaseDecl->isAbstract())
879 return true;
880 }
881
882 return false;
883}
884
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000885CXXMethodDecl *
886CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000887 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000888 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000889 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000890 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000891 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000892}
893
Douglas Gregor90916562009-09-29 18:16:17 +0000894bool CXXMethodDecl::isUsualDeallocationFunction() const {
895 if (getOverloadedOperator() != OO_Delete &&
896 getOverloadedOperator() != OO_Array_Delete)
897 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000898
899 // C++ [basic.stc.dynamic.deallocation]p2:
900 // A template instance is never a usual deallocation function,
901 // regardless of its signature.
902 if (getPrimaryTemplate())
903 return false;
904
Douglas Gregor90916562009-09-29 18:16:17 +0000905 // C++ [basic.stc.dynamic.deallocation]p2:
906 // If a class T has a member deallocation function named operator delete
907 // with exactly one parameter, then that function is a usual (non-placement)
908 // deallocation function. [...]
909 if (getNumParams() == 1)
910 return true;
911
912 // C++ [basic.stc.dynamic.deallocation]p2:
913 // [...] If class T does not declare such an operator delete but does
914 // declare a member deallocation function named operator delete with
915 // exactly two parameters, the second of which has type std::size_t (18.1),
916 // then this function is a usual deallocation function.
917 ASTContext &Context = getASTContext();
918 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000919 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
920 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000921 return false;
922
923 // This function is a usual deallocation function if there are no
924 // single-parameter deallocation functions of the same kind.
925 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
926 R.first != R.second; ++R.first) {
927 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
928 if (FD->getNumParams() == 1)
929 return false;
930 }
931
932 return true;
933}
934
Douglas Gregor06a9f362010-05-01 20:49:11 +0000935bool CXXMethodDecl::isCopyAssignmentOperator() const {
936 // C++0x [class.copy]p19:
937 // A user-declared copy assignment operator X::operator= is a non-static
938 // non-template member function of class X with exactly one parameter of
939 // type X, X&, const X&, volatile X& or const volatile X&.
940 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
941 /*non-static*/ isStatic() ||
942 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
943 /*exactly one parameter*/getNumParams() != 1)
944 return false;
945
946 QualType ParamType = getParamDecl(0)->getType();
947 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
948 ParamType = Ref->getPointeeType();
949
950 ASTContext &Context = getASTContext();
951 QualType ClassType
952 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
953 return Context.hasSameUnqualifiedType(ClassType, ParamType);
954}
955
Anders Carlsson05eb2442009-05-16 23:58:37 +0000956void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000957 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000958 assert(!MD->getParent()->isDependentContext() &&
959 "Can't add an overridden method to a class template!");
960
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000961 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000962}
963
964CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000965 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000966}
967
968CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000969 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000970}
971
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000972unsigned CXXMethodDecl::size_overridden_methods() const {
973 return getASTContext().overridden_methods_size(this);
974}
975
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000976QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000977 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
978 // If the member function is declared const, the type of this is const X*,
979 // if the member function is declared volatile, the type of this is
980 // volatile X*, and if the member function is declared const volatile,
981 // the type of this is const volatile X*.
982
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000983 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000984
John McCall3cb0ebd2010-03-10 03:28:59 +0000985 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000986 ClassTy = C.getQualifiedType(ClassTy,
987 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000988 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000989}
990
Eli Friedmand7d7f672009-12-06 20:50:05 +0000991bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000992 // If this function is a template instantiation, look at the template from
993 // which it was instantiated.
994 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
995 if (!CheckFn)
996 CheckFn = this;
997
Eli Friedmand7d7f672009-12-06 20:50:05 +0000998 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000999 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001000}
1001
Sean Huntcbb67482011-01-08 20:30:50 +00001002CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1003 TypeSourceInfo *TInfo, bool IsVirtual,
1004 SourceLocation L, Expr *Init,
1005 SourceLocation R,
1006 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001007 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001008 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1009 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001010{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001011}
1012
Sean Huntcbb67482011-01-08 20:30:50 +00001013CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1014 FieldDecl *Member,
1015 SourceLocation MemberLoc,
1016 SourceLocation L, Expr *Init,
1017 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001018 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001019 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1020 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1021{
1022}
1023
Sean Huntcbb67482011-01-08 20:30:50 +00001024CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1025 IndirectFieldDecl *Member,
1026 SourceLocation MemberLoc,
1027 SourceLocation L, Expr *Init,
1028 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001029 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001030 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001031 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001032{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001033}
1034
Sean Huntcbb67482011-01-08 20:30:50 +00001035CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1036 FieldDecl *Member,
1037 SourceLocation MemberLoc,
1038 SourceLocation L, Expr *Init,
1039 SourceLocation R,
1040 VarDecl **Indices,
1041 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001042 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001043 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001044 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001045{
1046 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1047 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1048}
1049
Sean Huntcbb67482011-01-08 20:30:50 +00001050CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1051 FieldDecl *Member,
1052 SourceLocation MemberLoc,
1053 SourceLocation L, Expr *Init,
1054 SourceLocation R,
1055 VarDecl **Indices,
1056 unsigned NumIndices) {
1057 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001058 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001059 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001060 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1061 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001062}
1063
Sean Huntcbb67482011-01-08 20:30:50 +00001064TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001065 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001066 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001067 else
1068 return TypeLoc();
1069}
1070
Sean Huntcbb67482011-01-08 20:30:50 +00001071const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001072 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001073 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001074 else
1075 return 0;
1076}
1077
Sean Huntcbb67482011-01-08 20:30:50 +00001078SourceLocation CXXCtorInitializer::getSourceLocation() const {
Francois Pichet00eb3f92010-12-04 09:14:42 +00001079 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001080 return getMemberLocation();
1081
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001082 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001083}
1084
Sean Huntcbb67482011-01-08 20:30:50 +00001085SourceRange CXXCtorInitializer::getSourceRange() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001086 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001087}
1088
Douglas Gregorb48fe382008-10-31 09:07:45 +00001089CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001090CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001091 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001092 QualType(), 0, false, false, false);
1093}
1094
1095CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001096CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001097 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001098 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001099 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001100 bool isInline,
1101 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001102 assert(NameInfo.getName().getNameKind()
1103 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001104 "Name must refer to a constructor");
Abramo Bagnara25777432010-08-11 22:01:17 +00001105 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001106 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001107}
1108
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001109bool CXXConstructorDecl::isDefaultConstructor() const {
1110 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001111 // A default constructor for a class X is a constructor of class
1112 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001113 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001114 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001115}
1116
Mike Stump1eb44332009-09-09 15:08:12 +00001117bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001118CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001119 return isCopyOrMoveConstructor(TypeQuals) &&
1120 getParamDecl(0)->getType()->isLValueReferenceType();
1121}
1122
1123bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1124 return isCopyOrMoveConstructor(TypeQuals) &&
1125 getParamDecl(0)->getType()->isRValueReferenceType();
1126}
1127
1128/// \brief Determine whether this is a copy or move constructor.
1129bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001130 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001131 // A non-template constructor for class X is a copy constructor
1132 // if its first parameter is of type X&, const X&, volatile X& or
1133 // const volatile X&, and either there are no other parameters
1134 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001135 // C++0x [class.copy]p3:
1136 // A non-template constructor for class X is a move constructor if its
1137 // first parameter is of type X&&, const X&&, volatile X&&, or
1138 // const volatile X&&, and either there are no other parameters or else
1139 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001140 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001141 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001142 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001143 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001144 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001145
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001146 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001147
1148 // Do we have a reference type?
1149 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001150 if (!ParamRefType)
1151 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001152
Douglas Gregorfd476482009-11-13 23:59:09 +00001153 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001154 ASTContext &Context = getASTContext();
1155
Douglas Gregorfd476482009-11-13 23:59:09 +00001156 CanQualType PointeeType
1157 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001158 CanQualType ClassTy
1159 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001160 if (PointeeType.getUnqualifiedType() != ClassTy)
1161 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001162
John McCall0953e762009-09-24 19:53:00 +00001163 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001164
1165 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001166 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001167 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001168}
1169
Anders Carlssonfaccd722009-08-28 16:57:08 +00001170bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001171 // C++ [class.conv.ctor]p1:
1172 // A constructor declared without the function-specifier explicit
1173 // that can be called with a single parameter specifies a
1174 // conversion from the type of its first parameter to the type of
1175 // its class. Such a constructor is called a converting
1176 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001177 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001178 return false;
1179
Mike Stump1eb44332009-09-09 15:08:12 +00001180 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001181 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001182 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001183 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001184}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001185
Douglas Gregor6493cc52010-11-08 17:16:59 +00001186bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001187 if ((getNumParams() < 1) ||
1188 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1189 (getPrimaryTemplate() == 0) ||
1190 (getDescribedFunctionTemplate() != 0))
1191 return false;
1192
1193 const ParmVarDecl *Param = getParamDecl(0);
1194
1195 ASTContext &Context = getASTContext();
1196 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1197
Douglas Gregor66724ea2009-11-14 01:20:54 +00001198 // Is it the same as our our class type?
1199 CanQualType ClassTy
1200 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1201 if (ParamType.getUnqualifiedType() != ClassTy)
1202 return false;
1203
1204 return true;
1205}
1206
Sebastian Redlf677ea32011-02-05 19:23:19 +00001207const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1208 // Hack: we store the inherited constructor in the overridden method table
1209 method_iterator It = begin_overridden_methods();
1210 if (It == end_overridden_methods())
1211 return 0;
1212
1213 return cast<CXXConstructorDecl>(*It);
1214}
1215
1216void
1217CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1218 // Hack: we store the inherited constructor in the overridden method table
1219 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1220 addOverriddenMethod(BaseCtor);
1221}
1222
Douglas Gregor42a552f2008-11-05 20:51:48 +00001223CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001224CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001225 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001226 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001227}
1228
1229CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001230CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001231 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001232 QualType T, TypeSourceInfo *TInfo,
1233 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001234 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001235 assert(NameInfo.getName().getNameKind()
1236 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001237 "Name must refer to a destructor");
Craig Silversteinb41d8992010-10-21 00:44:50 +00001238 return new (C) CXXDestructorDecl(RD, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001239 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001240}
1241
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001242CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001243CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001244 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001245 QualType(), 0, false, false);
1246}
1247
1248CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001249CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001250 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001251 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001252 bool isInline, bool isExplicit) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001253 assert(NameInfo.getName().getNameKind()
1254 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001255 "Name must refer to a conversion function");
Abramo Bagnara25777432010-08-11 22:01:17 +00001256 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
1257 isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001258}
1259
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001260LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001261 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001262 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +00001263 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +00001264 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001265}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001266
1267UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1268 SourceLocation L,
1269 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001270 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001271 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001272 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001273 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001274 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1275 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001276 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1277 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001278}
1279
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001280NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1281 if (NamespaceAliasDecl *NA =
1282 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1283 return NA->getNamespace();
1284 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1285}
1286
Mike Stump1eb44332009-09-09 15:08:12 +00001287NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001288 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001289 SourceLocation AliasLoc,
1290 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001291 SourceRange QualifierRange,
1292 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001293 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001294 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001295 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1296 Namespace = NS->getOriginalNamespace();
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001297 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001298 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001299}
1300
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001301UsingDecl *UsingShadowDecl::getUsingDecl() const {
1302 const UsingShadowDecl *Shadow = this;
1303 while (const UsingShadowDecl *NextShadow =
1304 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1305 Shadow = NextShadow;
1306 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1307}
1308
1309void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1310 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1311 "declaration already in set");
1312 assert(S->getUsingDecl() == this);
1313
1314 if (FirstUsingShadow)
1315 S->UsingOrNextShadow = FirstUsingShadow;
1316 FirstUsingShadow = S;
1317}
1318
1319void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1320 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1321 "declaration not in set");
1322 assert(S->getUsingDecl() == this);
1323
1324 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1325
1326 if (FirstUsingShadow == S) {
1327 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1328 S->UsingOrNextShadow = this;
1329 return;
1330 }
1331
1332 UsingShadowDecl *Prev = FirstUsingShadow;
1333 while (Prev->UsingOrNextShadow != S)
1334 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1335 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1336 S->UsingOrNextShadow = this;
1337}
1338
Douglas Gregordc355712011-02-25 00:36:19 +00001339UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1340 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001341 const DeclarationNameInfo &NameInfo,
1342 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001343 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001344}
1345
John McCall7ba107a2009-11-18 02:36:19 +00001346UnresolvedUsingValueDecl *
1347UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1348 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001349 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001350 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001351 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001352 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001353}
1354
1355UnresolvedUsingTypenameDecl *
1356UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1357 SourceLocation UsingLoc,
1358 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001359 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001360 SourceLocation TargetNameLoc,
1361 DeclarationName TargetName) {
1362 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001363 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001364 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001365}
1366
Anders Carlssonfb311762009-03-14 00:25:26 +00001367StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1368 SourceLocation L, Expr *AssertExpr,
1369 StringLiteral *Message) {
1370 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1371}
1372
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001373static const char *getAccessName(AccessSpecifier AS) {
1374 switch (AS) {
1375 default:
1376 case AS_none:
1377 assert("Invalid access specifier!");
1378 return 0;
1379 case AS_public:
1380 return "public";
1381 case AS_private:
1382 return "private";
1383 case AS_protected:
1384 return "protected";
1385 }
1386}
1387
1388const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1389 AccessSpecifier AS) {
1390 return DB << getAccessName(AS);
1391}