blob: 88ede82d929a5efa5d5dd88ba7be2794fbb6db95 [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),
Douglas Gregor7c789c12010-10-29 22:39:52 +000039 NumBases(0), NumVBases(0), Bases(), VBases(),
John McCalld60e22e2010-03-12 01:19:31 +000040 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
Ted Kremenek4b7c9832008-09-05 17:16:31 +000051CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
52 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000053 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
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000065CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, EmptyShell Empty) {
66 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,
Nick Lewycky56062202010-07-26 16:56:01 +0000199 VBases[I]->getAccessSpecifier(), VBaseTypeInfo);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000200 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000201}
202
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000203/// Callback function for CXXRecordDecl::forallBases that acknowledges
204/// that it saw a base class.
205static bool SawBase(const CXXRecordDecl *, void *) {
206 return true;
207}
208
209bool CXXRecordDecl::hasAnyDependentBases() const {
210 if (!isDependentContext())
211 return false;
212
213 return !forallBases(SawBase, 0);
214}
215
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000216bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000217 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000218}
219
Douglas Gregor0d405db2010-07-01 20:59:04 +0000220/// \brief Perform a simplistic form of overload resolution that only considers
221/// cv-qualifiers on a single parameter, and return the best overload candidate
222/// (if there is one).
223static CXXMethodDecl *
224GetBestOverloadCandidateSimple(
225 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
226 if (Cands.empty())
227 return 0;
228 if (Cands.size() == 1)
229 return Cands[0].first;
230
231 unsigned Best = 0, N = Cands.size();
232 for (unsigned I = 1; I != N; ++I)
233 if (Cands[Best].second.isSupersetOf(Cands[I].second))
234 Best = I;
235
236 for (unsigned I = 1; I != N; ++I)
237 if (Cands[Best].second.isSupersetOf(Cands[I].second))
238 return 0;
239
240 return Cands[Best].first;
241}
242
Mike Stump1eb44332009-09-09 15:08:12 +0000243CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000244 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000245 QualType ClassType
246 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000247 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000248 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000249 Context.getCanonicalType(ClassType));
250 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000251 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000252 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000253 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000254 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000255 // C++ [class.copy]p2:
256 // A non-template constructor for class X is a copy constructor if [...]
257 if (isa<FunctionTemplateDecl>(*Con))
258 continue;
259
Douglas Gregor0d405db2010-07-01 20:59:04 +0000260 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
261 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000262 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
263 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000264 Found.push_back(std::make_pair(
265 const_cast<CXXConstructorDecl *>(Constructor),
266 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000267 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000268 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000269
270 return cast_or_null<CXXConstructorDecl>(
271 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000272}
273
Douglas Gregorb87786f2010-07-01 17:48:08 +0000274CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
275 ASTContext &Context = getASTContext();
276 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
277 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
278
279 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
280 DeclContext::lookup_const_iterator Op, OpEnd;
281 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
282 // C++ [class.copy]p9:
283 // A user-declared copy assignment operator is a non-static non-template
284 // member function of class X with exactly one parameter of type X, X&,
285 // const X&, volatile X& or const volatile X&.
286 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
287 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
288 continue;
289
290 const FunctionProtoType *FnType
291 = Method->getType()->getAs<FunctionProtoType>();
292 assert(FnType && "Overloaded operator has no prototype.");
293 // Don't assert on this; an invalid decl might have been left in the AST.
294 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
295 continue;
296
297 QualType ArgType = FnType->getArgType(0);
298 Qualifiers Quals;
299 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
300 ArgType = Ref->getPointeeType();
301 // If we have a const argument and we have a reference to a non-const,
302 // this function does not match.
303 if (ArgIsConst && !ArgType.isConstQualified())
304 continue;
305
306 Quals = ArgType.getQualifiers();
307 } else {
308 // By-value copy-assignment operators are treated like const X&
309 // copy-assignment operators.
310 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
311 }
312
313 if (!Context.hasSameUnqualifiedType(ArgType, Class))
314 continue;
315
316 // Save this copy-assignment operator. It might be "the one".
317 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
318 }
319
320 // Use a simplistic form of overload resolution to find the candidate.
321 return GetBestOverloadCandidateSimple(Found);
322}
323
Douglas Gregor21386642010-09-28 21:55:22 +0000324void CXXRecordDecl::markedVirtualFunctionPure() {
325 // C++ [class.abstract]p2:
326 // A class is abstract if it has at least one pure virtual function.
327 data().Abstract = true;
328}
329
330void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000331 // Ignore friends and invalid declarations.
332 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000333 return;
334
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000335 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
336 if (FunTmpl)
337 D = FunTmpl->getTemplatedDecl();
338
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000339 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
340 if (Method->isVirtual()) {
341 // C++ [dcl.init.aggr]p1:
342 // An aggregate is an array or a class with [...] no virtual functions.
343 data().Aggregate = false;
344
345 // C++ [class]p4:
346 // A POD-struct is an aggregate class...
347 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000348
349 // Virtual functions make the class non-empty.
350 // FIXME: Standard ref?
351 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000352
353 // C++ [class.virtual]p1:
354 // A class that declares or inherits a virtual function is called a
355 // polymorphic class.
356 data().Polymorphic = true;
357
358 // None of the special member functions are trivial.
359 data().HasTrivialConstructor = false;
360 data().HasTrivialCopyConstructor = false;
361 data().HasTrivialCopyAssignment = false;
362 // FIXME: Destructor?
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000363 }
364 }
365
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000366 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000367 // Notify that an implicit member was added after the definition
368 // was completed.
369 if (!isBeingDefined())
370 if (ASTMutationListener *L = getASTMutationListener())
371 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000372
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000373 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
374 // If this is the implicit default constructor, note that we have now
375 // declared it.
376 if (Constructor->isDefaultConstructor())
377 data().DeclaredDefaultConstructor = true;
378 // If this is the implicit copy constructor, note that we have now
379 // declared it.
380 else if (Constructor->isCopyConstructor())
381 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000382 return;
383 }
384
385 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000386 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000387 return;
388 }
389
390 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000391 // If this is the implicit copy constructor, note that we have now
392 // declared it.
393 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000394 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000395 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000396 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000397 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000398
399 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000400 }
401
402 // Handle (user-declared) constructors.
403 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
404 // Note that we have a user-declared constructor.
405 data().UserDeclaredConstructor = true;
406
407 // Note that we have no need of an implicitly-declared default constructor.
408 data().DeclaredDefaultConstructor = true;
409
410 // C++ [dcl.init.aggr]p1:
411 // An aggregate is an array or a class (clause 9) with no
412 // user-declared constructors (12.1) [...].
413 data().Aggregate = false;
414
415 // C++ [class]p4:
416 // A POD-struct is an aggregate class [...]
417 data().PlainOldData = false;
418
419 // C++ [class.ctor]p5:
420 // A constructor is trivial if it is an implicitly-declared default
421 // constructor.
422 // FIXME: C++0x: don't do this for "= default" default constructors.
423 data().HasTrivialConstructor = false;
424
425 // Note when we have a user-declared copy constructor, which will
426 // suppress the implicit declaration of a copy constructor.
427 if (!FunTmpl && Constructor->isCopyConstructor()) {
428 data().UserDeclaredCopyConstructor = true;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000429 data().DeclaredCopyConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000430
431 // C++ [class.copy]p6:
432 // A copy constructor is trivial if it is implicitly declared.
433 // FIXME: C++0x: don't do this for "= default" copy constructors.
434 data().HasTrivialCopyConstructor = false;
435 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000436
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000437 return;
438 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000439
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000440 // Handle (user-declared) destructors.
441 if (isa<CXXDestructorDecl>(D)) {
442 data().DeclaredDestructor = true;
443 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000444
445 // C++ [class]p4:
446 // A POD-struct is an aggregate class that has [...] no user-defined
447 // destructor.
448 data().PlainOldData = false;
449
Douglas Gregor85606eb2010-09-28 20:50:54 +0000450 // C++ [class.dtor]p3:
451 // A destructor is trivial if it is an implicitly-declared destructor and
452 // [...].
453 //
454 // FIXME: C++0x: don't do this for "= default" destructors
455 data().HasTrivialDestructor = false;
456
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000457 return;
458 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000459
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000460 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000461 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
462 if (Method->getOverloadedOperator() == OO_Equal) {
463 // We're interested specifically in copy assignment operators.
464 const FunctionProtoType *FnType
465 = Method->getType()->getAs<FunctionProtoType>();
466 assert(FnType && "Overloaded operator has no proto function type.");
467 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
468
469 // Copy assignment operators must be non-templates.
470 if (Method->getPrimaryTemplate() || FunTmpl)
471 return;
472
473 ASTContext &Context = getASTContext();
474 QualType ArgType = FnType->getArgType(0);
475 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
476 ArgType = Ref->getPointeeType();
477
478 ArgType = ArgType.getUnqualifiedType();
479 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
480 const_cast<CXXRecordDecl*>(this)));
481
482 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
483 return;
484
485 // This is a copy assignment operator.
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000486 // FIXME: Move assignment operators.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000487
488 // Suppress the implicit declaration of a copy constructor.
489 data().UserDeclaredCopyAssignment = true;
490 data().DeclaredCopyAssignment = true;
491
492 // C++ [class.copy]p11:
493 // A copy assignment operator is trivial if it is implicitly declared.
494 // FIXME: C++0x: don't do this for "= default" copy operators.
495 data().HasTrivialCopyAssignment = false;
496
497 // C++ [class]p4:
498 // A POD-struct is an aggregate class that [...] has no user-defined copy
499 // assignment operator [...].
500 data().PlainOldData = false;
501 }
Douglas Gregor22584312010-07-02 23:41:54 +0000502
Douglas Gregore80622f2010-09-29 04:25:11 +0000503 // Keep the list of conversion functions up-to-date.
504 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
505 // We don't record specializations.
506 if (Conversion->getPrimaryTemplate())
507 return;
508
509 // FIXME: We intentionally don't use the decl's access here because it
510 // hasn't been set yet. That's really just a misdesign in Sema.
511
512 if (FunTmpl) {
513 if (FunTmpl->getPreviousDeclaration())
514 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
515 FunTmpl);
516 else
517 data().Conversions.addDecl(FunTmpl);
518 } else {
519 if (Conversion->getPreviousDeclaration())
520 data().Conversions.replace(Conversion->getPreviousDeclaration(),
521 Conversion);
522 else
523 data().Conversions.addDecl(Conversion);
524 }
525 }
526
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000527 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000528 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000529
530 // Handle non-static data members.
531 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
532 // C++ [dcl.init.aggr]p1:
533 // An aggregate is an array or a class (clause 9) with [...] no
534 // private or protected non-static data members (clause 11).
535 //
536 // A POD must be an aggregate.
537 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
538 data().Aggregate = false;
539 data().PlainOldData = false;
540 }
541
542 // C++ [class]p9:
543 // A POD struct is a class that is both a trivial class and a
544 // standard-layout class, and has no non-static data members of type
545 // non-POD struct, non-POD union (or array of such types).
546 ASTContext &Context = getASTContext();
547 QualType T = Context.getBaseElementType(Field->getType());
548 if (!T->isPODType())
549 data().PlainOldData = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000550 if (T->isReferenceType())
551 data().HasTrivialConstructor = false;
552
553 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
554 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
555 if (FieldRec->getDefinition()) {
556 if (!FieldRec->hasTrivialConstructor())
557 data().HasTrivialConstructor = false;
558 if (!FieldRec->hasTrivialCopyConstructor())
559 data().HasTrivialCopyConstructor = false;
560 if (!FieldRec->hasTrivialCopyAssignment())
561 data().HasTrivialCopyAssignment = false;
562 if (!FieldRec->hasTrivialDestructor())
563 data().HasTrivialDestructor = false;
564 }
565 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000566
567 // If this is not a zero-length bit-field, then the class is not empty.
568 if (data().Empty) {
569 if (!Field->getBitWidth())
570 data().Empty = false;
571 else if (!Field->getBitWidth()->isTypeDependent() &&
572 !Field->getBitWidth()->isValueDependent()) {
573 llvm::APSInt Bits;
574 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
575 if (!!Bits)
576 data().Empty = false;
577 }
578 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000579 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000580
581 // Handle using declarations of conversion functions.
582 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
583 if (Shadow->getDeclName().getNameKind()
584 == DeclarationName::CXXConversionFunctionName)
585 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000586}
587
John McCallb05b5f32010-03-15 09:07:48 +0000588static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
589 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000590 if (isa<UsingShadowDecl>(Conv))
591 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000592 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
593 T = ConvTemp->getTemplatedDecl()->getResultType();
594 else
595 T = cast<CXXConversionDecl>(Conv)->getConversionType();
596 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000597}
598
John McCallb05b5f32010-03-15 09:07:48 +0000599/// Collect the visible conversions of a base class.
600///
601/// \param Base a base class of the class we're considering
602/// \param InVirtual whether this base class is a virtual base (or a base
603/// of a virtual base)
604/// \param Access the access along the inheritance path to this base
605/// \param ParentHiddenTypes the conversions provided by the inheritors
606/// of this base
607/// \param Output the set to which to add conversions from non-virtual bases
608/// \param VOutput the set to which to add conversions from virtual bases
609/// \param HiddenVBaseCs the set of conversions which were hidden in a
610/// virtual base along some inheritance path
611static void CollectVisibleConversions(ASTContext &Context,
612 CXXRecordDecl *Record,
613 bool InVirtual,
614 AccessSpecifier Access,
615 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
616 UnresolvedSetImpl &Output,
617 UnresolvedSetImpl &VOutput,
618 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
619 // The set of types which have conversions in this class or its
620 // subclasses. As an optimization, we don't copy the derived set
621 // unless it might change.
622 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
623 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
624
625 // Collect the direct conversions and figure out which conversions
626 // will be hidden in the subclasses.
627 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
628 if (!Cs.empty()) {
629 HiddenTypesBuffer = ParentHiddenTypes;
630 HiddenTypes = &HiddenTypesBuffer;
631
632 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
633 bool Hidden =
634 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
635
636 // If this conversion is hidden and we're in a virtual base,
637 // remember that it's hidden along some inheritance path.
638 if (Hidden && InVirtual)
639 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
640
641 // If this conversion isn't hidden, add it to the appropriate output.
642 else if (!Hidden) {
643 AccessSpecifier IAccess
644 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
645
646 if (InVirtual)
647 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000648 else
John McCallb05b5f32010-03-15 09:07:48 +0000649 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000650 }
651 }
652 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000653
John McCallb05b5f32010-03-15 09:07:48 +0000654 // Collect information recursively from any base classes.
655 for (CXXRecordDecl::base_class_iterator
656 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
657 const RecordType *RT = I->getType()->getAs<RecordType>();
658 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000659
John McCallb05b5f32010-03-15 09:07:48 +0000660 AccessSpecifier BaseAccess
661 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
662 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000663
John McCallb05b5f32010-03-15 09:07:48 +0000664 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
665 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
666 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000667 }
John McCallb05b5f32010-03-15 09:07:48 +0000668}
Sebastian Redl9994a342009-10-25 17:03:50 +0000669
John McCallb05b5f32010-03-15 09:07:48 +0000670/// Collect the visible conversions of a class.
671///
672/// This would be extremely straightforward if it weren't for virtual
673/// bases. It might be worth special-casing that, really.
674static void CollectVisibleConversions(ASTContext &Context,
675 CXXRecordDecl *Record,
676 UnresolvedSetImpl &Output) {
677 // The collection of all conversions in virtual bases that we've
678 // found. These will be added to the output as long as they don't
679 // appear in the hidden-conversions set.
680 UnresolvedSet<8> VBaseCs;
681
682 // The set of conversions in virtual bases that we've determined to
683 // be hidden.
684 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
685
686 // The set of types hidden by classes derived from this one.
687 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
688
689 // Go ahead and collect the direct conversions and add them to the
690 // hidden-types set.
691 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
692 Output.append(Cs.begin(), Cs.end());
693 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
694 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
695
696 // Recursively collect conversions from base classes.
697 for (CXXRecordDecl::base_class_iterator
698 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
699 const RecordType *RT = I->getType()->getAs<RecordType>();
700 if (!RT) continue;
701
702 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
703 I->isVirtual(), I->getAccessSpecifier(),
704 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
705 }
706
707 // Add any unhidden conversions provided by virtual bases.
708 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
709 I != E; ++I) {
710 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
711 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000712 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000713}
714
715/// getVisibleConversionFunctions - get all conversion functions visible
716/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000717const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000718 // If root class, all conversions are visible.
719 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000720 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000721 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000722 if (data().ComputedVisibleConversions)
723 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000724 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000725 data().ComputedVisibleConversions = true;
726 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000727}
728
John McCall32daa422010-03-31 01:36:47 +0000729#ifndef NDEBUG
730void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000731 assert(ConvDecl->getDeclContext() == this &&
732 "conversion function does not belong to this record");
733
John McCall32daa422010-03-31 01:36:47 +0000734 ConvDecl = ConvDecl->getUnderlyingDecl();
735 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
736 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
737 } else {
738 assert(isa<CXXConversionDecl>(ConvDecl));
739 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000740}
John McCall32daa422010-03-31 01:36:47 +0000741#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000742
John McCall32daa422010-03-31 01:36:47 +0000743void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
744 // This operation is O(N) but extremely rare. Sema only uses it to
745 // remove UsingShadowDecls in a class that were followed by a direct
746 // declaration, e.g.:
747 // class A : B {
748 // using B::operator int;
749 // operator int();
750 // };
751 // This is uncommon by itself and even more uncommon in conjunction
752 // with sufficiently large numbers of directly-declared conversions
753 // that asymptotic behavior matters.
754
755 UnresolvedSetImpl &Convs = *getConversionFunctions();
756 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
757 if (Convs[I].getDecl() == ConvDecl) {
758 Convs.erase(I);
759 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
760 && "conversion was found multiple times in unresolved set");
761 return;
762 }
763 }
764
765 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000766}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000767
Douglas Gregorf6b11852009-10-08 15:14:33 +0000768CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000769 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000770 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
771
772 return 0;
773}
774
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000775MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
776 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
777}
778
Douglas Gregorf6b11852009-10-08 15:14:33 +0000779void
780CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
781 TemplateSpecializationKind TSK) {
782 assert(TemplateOrInstantiation.isNull() &&
783 "Previous template or instantiation?");
784 assert(!isa<ClassTemplateSpecializationDecl>(this));
785 TemplateOrInstantiation
786 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
787}
788
Anders Carlssonb13e3572009-12-07 06:33:48 +0000789TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
790 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000791 = dyn_cast<ClassTemplateSpecializationDecl>(this))
792 return Spec->getSpecializationKind();
793
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000794 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000795 return MSInfo->getTemplateSpecializationKind();
796
797 return TSK_Undeclared;
798}
799
800void
801CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
802 if (ClassTemplateSpecializationDecl *Spec
803 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
804 Spec->setSpecializationKind(TSK);
805 return;
806 }
807
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000808 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000809 MSInfo->setTemplateSpecializationKind(TSK);
810 return;
811 }
812
813 assert(false && "Not a class template or member class specialization");
814}
815
Douglas Gregor1d110e02010-07-01 14:13:13 +0000816CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
817 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000818 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000819
820 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000821 = Context.DeclarationNames.getCXXDestructorName(
822 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000823
John McCallc0bf4622010-02-23 00:48:20 +0000824 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000825 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000826 if (I == E)
827 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000829 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000830 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Anders Carlsson7267c162009-05-29 21:03:38 +0000832 return Dtor;
833}
834
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000835void CXXRecordDecl::completeDefinition() {
836 completeDefinition(0);
837}
838
839void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
840 RecordDecl::completeDefinition();
841
842 // If the class may be abstract (but hasn't been marked as such), check for
843 // any pure final overriders.
844 if (mayBeAbstract()) {
845 CXXFinalOverriderMap MyFinalOverriders;
846 if (!FinalOverriders) {
847 getFinalOverriders(MyFinalOverriders);
848 FinalOverriders = &MyFinalOverriders;
849 }
850
851 bool Done = false;
852 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
853 MEnd = FinalOverriders->end();
854 M != MEnd && !Done; ++M) {
855 for (OverridingMethods::iterator SO = M->second.begin(),
856 SOEnd = M->second.end();
857 SO != SOEnd && !Done; ++SO) {
858 assert(SO->second.size() > 0 &&
859 "All virtual functions have overridding virtual functions");
860
861 // C++ [class.abstract]p4:
862 // A class is abstract if it contains or inherits at least one
863 // pure virtual function for which the final overrider is pure
864 // virtual.
865 if (SO->second.front().Method->isPure()) {
866 data().Abstract = true;
867 Done = true;
868 break;
869 }
870 }
871 }
872 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000873
874 // Set access bits correctly on the directly-declared conversions.
875 for (UnresolvedSetIterator I = data().Conversions.begin(),
876 E = data().Conversions.end();
877 I != E; ++I)
878 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000879}
880
881bool CXXRecordDecl::mayBeAbstract() const {
882 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
883 isDependentContext())
884 return false;
885
886 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
887 BEnd = bases_end();
888 B != BEnd; ++B) {
889 CXXRecordDecl *BaseDecl
890 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
891 if (BaseDecl->isAbstract())
892 return true;
893 }
894
895 return false;
896}
897
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000898CXXMethodDecl *
899CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000900 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000901 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000902 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000903 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000904 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000905}
906
Douglas Gregor90916562009-09-29 18:16:17 +0000907bool CXXMethodDecl::isUsualDeallocationFunction() const {
908 if (getOverloadedOperator() != OO_Delete &&
909 getOverloadedOperator() != OO_Array_Delete)
910 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000911
912 // C++ [basic.stc.dynamic.deallocation]p2:
913 // A template instance is never a usual deallocation function,
914 // regardless of its signature.
915 if (getPrimaryTemplate())
916 return false;
917
Douglas Gregor90916562009-09-29 18:16:17 +0000918 // C++ [basic.stc.dynamic.deallocation]p2:
919 // If a class T has a member deallocation function named operator delete
920 // with exactly one parameter, then that function is a usual (non-placement)
921 // deallocation function. [...]
922 if (getNumParams() == 1)
923 return true;
924
925 // C++ [basic.stc.dynamic.deallocation]p2:
926 // [...] If class T does not declare such an operator delete but does
927 // declare a member deallocation function named operator delete with
928 // exactly two parameters, the second of which has type std::size_t (18.1),
929 // then this function is a usual deallocation function.
930 ASTContext &Context = getASTContext();
931 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000932 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
933 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000934 return false;
935
936 // This function is a usual deallocation function if there are no
937 // single-parameter deallocation functions of the same kind.
938 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
939 R.first != R.second; ++R.first) {
940 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
941 if (FD->getNumParams() == 1)
942 return false;
943 }
944
945 return true;
946}
947
Douglas Gregor06a9f362010-05-01 20:49:11 +0000948bool CXXMethodDecl::isCopyAssignmentOperator() const {
949 // C++0x [class.copy]p19:
950 // A user-declared copy assignment operator X::operator= is a non-static
951 // non-template member function of class X with exactly one parameter of
952 // type X, X&, const X&, volatile X& or const volatile X&.
953 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
954 /*non-static*/ isStatic() ||
955 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
956 /*exactly one parameter*/getNumParams() != 1)
957 return false;
958
959 QualType ParamType = getParamDecl(0)->getType();
960 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
961 ParamType = Ref->getPointeeType();
962
963 ASTContext &Context = getASTContext();
964 QualType ClassType
965 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
966 return Context.hasSameUnqualifiedType(ClassType, ParamType);
967}
968
Anders Carlsson05eb2442009-05-16 23:58:37 +0000969void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000970 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000971 assert(!MD->getParent()->isDependentContext() &&
972 "Can't add an overridden method to a class template!");
973
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000974 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000975}
976
977CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000978 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000979}
980
981CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000982 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000983}
984
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000985unsigned CXXMethodDecl::size_overridden_methods() const {
986 return getASTContext().overridden_methods_size(this);
987}
988
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000989QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000990 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
991 // If the member function is declared const, the type of this is const X*,
992 // if the member function is declared volatile, the type of this is
993 // volatile X*, and if the member function is declared const volatile,
994 // the type of this is const volatile X*.
995
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000996 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000997
John McCall3cb0ebd2010-03-10 03:28:59 +0000998 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000999 ClassTy = C.getQualifiedType(ClassTy,
1000 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001001 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001002}
1003
Eli Friedmand7d7f672009-12-06 20:50:05 +00001004bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001005 // If this function is a template instantiation, look at the template from
1006 // which it was instantiated.
1007 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1008 if (!CheckFn)
1009 CheckFn = this;
1010
Eli Friedmand7d7f672009-12-06 20:50:05 +00001011 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001012 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001013}
1014
Douglas Gregor7ad83902008-11-05 04:29:56 +00001015CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +00001016CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +00001017 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001018 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001019 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
1020 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1021 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001022{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001023}
1024
1025CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +00001026CXXBaseOrMemberInitializer(ASTContext &Context,
1027 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001028 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001029 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
1030 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
1031 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001032{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001033}
1034
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001035CXXBaseOrMemberInitializer::
1036CXXBaseOrMemberInitializer(ASTContext &Context,
1037 FieldDecl *Member, SourceLocation MemberLoc,
1038 SourceLocation L, Expr *Init, SourceLocation R,
1039 VarDecl **Indices,
1040 unsigned NumIndices)
1041 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001042 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
1043 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001044{
1045 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1046 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1047}
1048
1049CXXBaseOrMemberInitializer *
1050CXXBaseOrMemberInitializer::Create(ASTContext &Context,
1051 FieldDecl *Member,
1052 SourceLocation MemberLoc,
1053 SourceLocation L,
1054 Expr *Init,
1055 SourceLocation R,
1056 VarDecl **Indices,
1057 unsigned NumIndices) {
1058 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
1059 sizeof(VarDecl *) * NumIndices,
1060 llvm::alignof<CXXBaseOrMemberInitializer>());
1061 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
1062 L, Init, R, Indices, NumIndices);
1063}
1064
Douglas Gregor802ab452009-12-02 22:36:29 +00001065TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
1066 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +00001067 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001068 else
1069 return TypeLoc();
1070}
1071
1072Type *CXXBaseOrMemberInitializer::getBaseClass() {
1073 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +00001074 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001075 else
1076 return 0;
1077}
1078
1079const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
1080 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +00001081 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001082 else
1083 return 0;
1084}
1085
1086SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
1087 if (isMemberInitializer())
1088 return getMemberLocation();
1089
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001090 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001091}
1092
1093SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
1094 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001095}
1096
Douglas Gregorb48fe382008-10-31 09:07:45 +00001097CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001098CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001099 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001100 QualType(), 0, false, false, false);
1101}
1102
1103CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001104CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001105 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001106 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001107 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001108 bool isInline,
1109 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001110 assert(NameInfo.getName().getNameKind()
1111 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001112 "Name must refer to a constructor");
Abramo Bagnara25777432010-08-11 22:01:17 +00001113 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001114 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001115}
1116
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001117bool CXXConstructorDecl::isDefaultConstructor() const {
1118 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001119 // A default constructor for a class X is a constructor of class
1120 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001121 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001122 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001123}
1124
Mike Stump1eb44332009-09-09 15:08:12 +00001125bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001126CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001127 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001128 // A non-template constructor for class X is a copy constructor
1129 // if its first parameter is of type X&, const X&, volatile X& or
1130 // const volatile X&, and either there are no other parameters
1131 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001132 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001133 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001134 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001135 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001136 return false;
1137
1138 const ParmVarDecl *Param = getParamDecl(0);
1139
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001140 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +00001141 const LValueReferenceType *ParamRefType =
1142 Param->getType()->getAs<LValueReferenceType>();
1143 if (!ParamRefType)
1144 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001145
Douglas Gregorfd476482009-11-13 23:59:09 +00001146 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001147 ASTContext &Context = getASTContext();
1148
Douglas Gregorfd476482009-11-13 23:59:09 +00001149 CanQualType PointeeType
1150 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001151 CanQualType ClassTy
1152 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001153 if (PointeeType.getUnqualifiedType() != ClassTy)
1154 return false;
1155
John McCall0953e762009-09-24 19:53:00 +00001156 // FIXME: other qualifiers?
1157
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001158 // We have a copy constructor.
1159 TypeQuals = PointeeType.getCVRQualifiers();
1160 return true;
1161}
1162
Anders Carlssonfaccd722009-08-28 16:57:08 +00001163bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001164 // C++ [class.conv.ctor]p1:
1165 // A constructor declared without the function-specifier explicit
1166 // that can be called with a single parameter specifies a
1167 // conversion from the type of its first parameter to the type of
1168 // its class. Such a constructor is called a converting
1169 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001170 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001171 return false;
1172
Mike Stump1eb44332009-09-09 15:08:12 +00001173 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001174 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001175 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001176 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001177}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001178
Douglas Gregor66724ea2009-11-14 01:20:54 +00001179bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
1180 if ((getNumParams() < 1) ||
1181 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1182 (getPrimaryTemplate() == 0) ||
1183 (getDescribedFunctionTemplate() != 0))
1184 return false;
1185
1186 const ParmVarDecl *Param = getParamDecl(0);
1187
1188 ASTContext &Context = getASTContext();
1189 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1190
1191 // Strip off the lvalue reference, if any.
1192 if (CanQual<LValueReferenceType> ParamRefType
1193 = ParamType->getAs<LValueReferenceType>())
1194 ParamType = ParamRefType->getPointeeType();
1195
1196
1197 // Is it the same as our our class type?
1198 CanQualType ClassTy
1199 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1200 if (ParamType.getUnqualifiedType() != ClassTy)
1201 return false;
1202
1203 return true;
1204}
1205
Douglas Gregor42a552f2008-11-05 20:51:48 +00001206CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001207CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001208 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001209 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001210}
1211
1212CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001213CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001214 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001215 QualType T, TypeSourceInfo *TInfo,
1216 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001217 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001218 assert(NameInfo.getName().getNameKind()
1219 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001220 "Name must refer to a destructor");
Craig Silversteinb41d8992010-10-21 00:44:50 +00001221 return new (C) CXXDestructorDecl(RD, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001222 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001223}
1224
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001225CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001226CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001227 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001228 QualType(), 0, false, false);
1229}
1230
1231CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001232CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001233 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001234 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001235 bool isInline, bool isExplicit) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001236 assert(NameInfo.getName().getNameKind()
1237 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001238 "Name must refer to a conversion function");
Abramo Bagnara25777432010-08-11 22:01:17 +00001239 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
1240 isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001241}
1242
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001243LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001244 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001245 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +00001246 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +00001247 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001248}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001249
1250UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1251 SourceLocation L,
1252 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001253 SourceRange QualifierRange,
1254 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001255 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001256 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001257 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001258 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1259 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001260 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001261 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001262}
1263
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001264NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1265 if (NamespaceAliasDecl *NA =
1266 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1267 return NA->getNamespace();
1268 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1269}
1270
Mike Stump1eb44332009-09-09 15:08:12 +00001271NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001272 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001273 SourceLocation AliasLoc,
1274 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001275 SourceRange QualifierRange,
1276 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001277 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001278 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001279 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1280 Namespace = NS->getOriginalNamespace();
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001281 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001282 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001283}
1284
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001285UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001286 SourceRange NNR, SourceLocation UL,
1287 NestedNameSpecifier* TargetNNS,
1288 const DeclarationNameInfo &NameInfo,
1289 bool IsTypeNameArg) {
1290 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001291}
1292
John McCall7ba107a2009-11-18 02:36:19 +00001293UnresolvedUsingValueDecl *
1294UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1295 SourceLocation UsingLoc,
1296 SourceRange TargetNNR,
1297 NestedNameSpecifier *TargetNNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001298 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001299 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001300 TargetNNR, TargetNNS, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001301}
1302
1303UnresolvedUsingTypenameDecl *
1304UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1305 SourceLocation UsingLoc,
1306 SourceLocation TypenameLoc,
1307 SourceRange TargetNNR,
1308 NestedNameSpecifier *TargetNNS,
1309 SourceLocation TargetNameLoc,
1310 DeclarationName TargetName) {
1311 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1312 TargetNNR, TargetNNS,
1313 TargetNameLoc,
1314 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001315}
1316
Anders Carlssonfb311762009-03-14 00:25:26 +00001317StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1318 SourceLocation L, Expr *AssertExpr,
1319 StringLiteral *Message) {
1320 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1321}
1322
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001323static const char *getAccessName(AccessSpecifier AS) {
1324 switch (AS) {
1325 default:
1326 case AS_none:
1327 assert("Invalid access specifier!");
1328 return 0;
1329 case AS_public:
1330 return "public";
1331 case AS_private:
1332 return "private";
1333 case AS_protected:
1334 return "protected";
1335 }
1336}
1337
1338const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1339 AccessSpecifier AS) {
1340 return DB << getAccessName(AS);
1341}