blob: bf1e58c7c964642b28c38593b02a1b142757746d [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"
Anders Carlssonfb311762009-03-14 00:25:26 +000017#include "clang/AST/Expr.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000018#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000019#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000020#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000021#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Decl Allocation/Deallocation Method Implementations
26//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000027
John McCall86ff3082010-02-04 22:26:26 +000028CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
29 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000030 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000031 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
32 Abstract(false), HasTrivialConstructor(true),
33 HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true),
Fariborz Jahanian62509212009-09-12 18:26:03 +000034 HasTrivialDestructor(true), ComputedVisibleConversions(false),
Douglas Gregor18274032010-07-03 00:47:00 +000035 DeclaredDefaultConstructor(false), DeclaredCopyConstructor(false),
Douglas Gregora376d102010-07-02 21:50:04 +000036 DeclaredCopyAssignment(false), DeclaredDestructor(false),
Fariborz Jahanian62509212009-09-12 18:26:03 +000037 Bases(0), NumBases(0), VBases(0), NumVBases(0),
John McCalld60e22e2010-03-12 01:19:31 +000038 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000039}
40
41CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
42 SourceLocation L, IdentifierInfo *Id,
43 CXXRecordDecl *PrevDecl,
44 SourceLocation TKL)
45 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
46 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000047 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000048
Ted Kremenek4b7c9832008-09-05 17:16:31 +000049CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
50 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000051 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000052 CXXRecordDecl* PrevDecl,
53 bool DelayTypeCreation) {
Mike Stump1eb44332009-09-09 15:08:12 +000054 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000055 PrevDecl, TKL);
Mike Stump1eb44332009-09-09 15:08:12 +000056
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000057 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000058 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000059 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000060 return R;
61}
62
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000063CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, EmptyShell Empty) {
64 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(), 0, 0,
65 SourceLocation());
66}
67
Mike Stump1eb44332009-09-09 15:08:12 +000068void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000069CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000070 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000071 ASTContext &C = getASTContext();
72
Mike Stump1eb44332009-09-09 15:08:12 +000073 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000074 // An aggregate is an array or a class (clause 9) with [...]
75 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000076 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000077
John McCall86ff3082010-02-04 22:26:26 +000078 if (data().Bases)
79 C.Deallocate(data().Bases);
Mike Stump1eb44332009-09-09 15:08:12 +000080
Anders Carlsson6f6de732010-03-29 05:13:12 +000081 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000082 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000083
84 // The virtual bases of this class.
85 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000086
John McCall86ff3082010-02-04 22:26:26 +000087 data().Bases = new(C) CXXBaseSpecifier [NumBases];
88 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000089 for (unsigned i = 0; i < NumBases; ++i) {
John McCall86ff3082010-02-04 22:26:26 +000090 data().Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000091 // Keep track of inherited vbases for this base class.
92 const CXXBaseSpecifier *Base = Bases[i];
93 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +000094 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000095 if (BaseType->isDependentType())
96 continue;
97 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +000098 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +000099
100 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000101 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000102 BaseClassDecl->vbases_begin(),
103 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000104 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000105 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000106 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000107 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000108
109 if (Base->isVirtual()) {
110 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000111 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000112 VBases.push_back(Base);
113 }
114
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000115 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000116
117 if (VBases.empty())
118 return;
119
120 // Create base specifier for any direct or indirect virtual bases.
121 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
122 data().NumVBases = VBases.size();
123 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000124 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
125
Anders Carlsson6f6de732010-03-29 05:13:12 +0000126 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000127 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000128 continue;
129
Nick Lewycky56062202010-07-26 16:56:01 +0000130 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
131 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000132
133 data().VBases[I] =
134 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000135 VBaseClassDecl->getTagKind() == TTK_Class,
Nick Lewycky56062202010-07-26 16:56:01 +0000136 VBases[I]->getAccessSpecifier(), VBaseTypeInfo);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000137 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000138}
139
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000140/// Callback function for CXXRecordDecl::forallBases that acknowledges
141/// that it saw a base class.
142static bool SawBase(const CXXRecordDecl *, void *) {
143 return true;
144}
145
146bool CXXRecordDecl::hasAnyDependentBases() const {
147 if (!isDependentContext())
148 return false;
149
150 return !forallBases(SawBase, 0);
151}
152
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000153bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000154 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000155}
156
Douglas Gregor0d405db2010-07-01 20:59:04 +0000157/// \brief Perform a simplistic form of overload resolution that only considers
158/// cv-qualifiers on a single parameter, and return the best overload candidate
159/// (if there is one).
160static CXXMethodDecl *
161GetBestOverloadCandidateSimple(
162 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
163 if (Cands.empty())
164 return 0;
165 if (Cands.size() == 1)
166 return Cands[0].first;
167
168 unsigned Best = 0, N = Cands.size();
169 for (unsigned I = 1; I != N; ++I)
170 if (Cands[Best].second.isSupersetOf(Cands[I].second))
171 Best = I;
172
173 for (unsigned I = 1; I != N; ++I)
174 if (Cands[Best].second.isSupersetOf(Cands[I].second))
175 return 0;
176
177 return Cands[Best].first;
178}
179
Mike Stump1eb44332009-09-09 15:08:12 +0000180CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000181 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000182 QualType ClassType
183 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000184 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000185 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000186 Context.getCanonicalType(ClassType));
187 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000188 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000189 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000190 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000191 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000192 // C++ [class.copy]p2:
193 // A non-template constructor for class X is a copy constructor if [...]
194 if (isa<FunctionTemplateDecl>(*Con))
195 continue;
196
Douglas Gregor0d405db2010-07-01 20:59:04 +0000197 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
198 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000199 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
200 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000201 Found.push_back(std::make_pair(
202 const_cast<CXXConstructorDecl *>(Constructor),
203 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000204 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000205 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000206
207 return cast_or_null<CXXConstructorDecl>(
208 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000209}
210
Douglas Gregorb87786f2010-07-01 17:48:08 +0000211CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
212 ASTContext &Context = getASTContext();
213 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
214 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
215
216 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
217 DeclContext::lookup_const_iterator Op, OpEnd;
218 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
219 // C++ [class.copy]p9:
220 // A user-declared copy assignment operator is a non-static non-template
221 // member function of class X with exactly one parameter of type X, X&,
222 // const X&, volatile X& or const volatile X&.
223 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
224 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
225 continue;
226
227 const FunctionProtoType *FnType
228 = Method->getType()->getAs<FunctionProtoType>();
229 assert(FnType && "Overloaded operator has no prototype.");
230 // Don't assert on this; an invalid decl might have been left in the AST.
231 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
232 continue;
233
234 QualType ArgType = FnType->getArgType(0);
235 Qualifiers Quals;
236 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
237 ArgType = Ref->getPointeeType();
238 // If we have a const argument and we have a reference to a non-const,
239 // this function does not match.
240 if (ArgIsConst && !ArgType.isConstQualified())
241 continue;
242
243 Quals = ArgType.getQualifiers();
244 } else {
245 // By-value copy-assignment operators are treated like const X&
246 // copy-assignment operators.
247 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
248 }
249
250 if (!Context.hasSameUnqualifiedType(ArgType, Class))
251 continue;
252
253 // Save this copy-assignment operator. It might be "the one".
254 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
255 }
256
257 // Use a simplistic form of overload resolution to find the candidate.
258 return GetBestOverloadCandidateSimple(Found);
259}
260
Sebastian Redl64b45f72009-01-05 20:52:13 +0000261void
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000262CXXRecordDecl::addedMember(Decl *D) {
263 // Ignore friends and invalid declarations.
264 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000265 return;
266
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000267 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
268 if (FunTmpl)
269 D = FunTmpl->getTemplatedDecl();
270
271 if (D->isImplicit()) {
272 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
273 // If this is the implicit default constructor, note that we have now
274 // declared it.
275 if (Constructor->isDefaultConstructor())
276 data().DeclaredDefaultConstructor = true;
277 // If this is the implicit copy constructor, note that we have now
278 // declared it.
279 else if (Constructor->isCopyConstructor())
280 data().DeclaredCopyConstructor = true;
281 }
282 // FIXME: Destructors
283 else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
284 // If this is the implicit copy constructor, note that we have now
285 // declared it.
286 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000287 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000288 data().DeclaredCopyAssignment = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000289 }
290
291 // Nothing else to do for implicitly-declared members.
292 return;
293 }
294
295 // Handle (user-declared) constructors.
296 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
297 // Note that we have a user-declared constructor.
298 data().UserDeclaredConstructor = true;
299
300 // Note that we have no need of an implicitly-declared default constructor.
301 data().DeclaredDefaultConstructor = true;
302
303 // C++ [dcl.init.aggr]p1:
304 // An aggregate is an array or a class (clause 9) with no
305 // user-declared constructors (12.1) [...].
306 data().Aggregate = false;
307
308 // C++ [class]p4:
309 // A POD-struct is an aggregate class [...]
310 data().PlainOldData = false;
311
312 // C++ [class.ctor]p5:
313 // A constructor is trivial if it is an implicitly-declared default
314 // constructor.
315 // FIXME: C++0x: don't do this for "= default" default constructors.
316 data().HasTrivialConstructor = false;
317
318 // Note when we have a user-declared copy constructor, which will
319 // suppress the implicit declaration of a copy constructor.
320 if (!FunTmpl && Constructor->isCopyConstructor()) {
321 data().UserDeclaredCopyConstructor = true;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000322 data().DeclaredCopyConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000323
324 // C++ [class.copy]p6:
325 // A copy constructor is trivial if it is implicitly declared.
326 // FIXME: C++0x: don't do this for "= default" copy constructors.
327 data().HasTrivialCopyConstructor = false;
328 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000329
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000330 return;
331 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000332
333 // FIXME: Destructors.
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000334
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000335 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
336 if (Method->getOverloadedOperator() == OO_Equal) {
337 // We're interested specifically in copy assignment operators.
338 const FunctionProtoType *FnType
339 = Method->getType()->getAs<FunctionProtoType>();
340 assert(FnType && "Overloaded operator has no proto function type.");
341 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
342
343 // Copy assignment operators must be non-templates.
344 if (Method->getPrimaryTemplate() || FunTmpl)
345 return;
346
347 ASTContext &Context = getASTContext();
348 QualType ArgType = FnType->getArgType(0);
349 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
350 ArgType = Ref->getPointeeType();
351
352 ArgType = ArgType.getUnqualifiedType();
353 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
354 const_cast<CXXRecordDecl*>(this)));
355
356 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
357 return;
358
359 // This is a copy assignment operator.
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000360 // FIXME: Move assignment operators.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000361
362 // Suppress the implicit declaration of a copy constructor.
363 data().UserDeclaredCopyAssignment = true;
364 data().DeclaredCopyAssignment = true;
365
366 // C++ [class.copy]p11:
367 // A copy assignment operator is trivial if it is implicitly declared.
368 // FIXME: C++0x: don't do this for "= default" copy operators.
369 data().HasTrivialCopyAssignment = false;
370
371 // C++ [class]p4:
372 // A POD-struct is an aggregate class that [...] has no user-defined copy
373 // assignment operator [...].
374 data().PlainOldData = false;
375 }
Douglas Gregor22584312010-07-02 23:41:54 +0000376
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000377 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000378 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000379}
380
John McCallb05b5f32010-03-15 09:07:48 +0000381static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
382 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000383 if (isa<UsingShadowDecl>(Conv))
384 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000385 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
386 T = ConvTemp->getTemplatedDecl()->getResultType();
387 else
388 T = cast<CXXConversionDecl>(Conv)->getConversionType();
389 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000390}
391
John McCallb05b5f32010-03-15 09:07:48 +0000392/// Collect the visible conversions of a base class.
393///
394/// \param Base a base class of the class we're considering
395/// \param InVirtual whether this base class is a virtual base (or a base
396/// of a virtual base)
397/// \param Access the access along the inheritance path to this base
398/// \param ParentHiddenTypes the conversions provided by the inheritors
399/// of this base
400/// \param Output the set to which to add conversions from non-virtual bases
401/// \param VOutput the set to which to add conversions from virtual bases
402/// \param HiddenVBaseCs the set of conversions which were hidden in a
403/// virtual base along some inheritance path
404static void CollectVisibleConversions(ASTContext &Context,
405 CXXRecordDecl *Record,
406 bool InVirtual,
407 AccessSpecifier Access,
408 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
409 UnresolvedSetImpl &Output,
410 UnresolvedSetImpl &VOutput,
411 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
412 // The set of types which have conversions in this class or its
413 // subclasses. As an optimization, we don't copy the derived set
414 // unless it might change.
415 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
416 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
417
418 // Collect the direct conversions and figure out which conversions
419 // will be hidden in the subclasses.
420 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
421 if (!Cs.empty()) {
422 HiddenTypesBuffer = ParentHiddenTypes;
423 HiddenTypes = &HiddenTypesBuffer;
424
425 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
426 bool Hidden =
427 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
428
429 // If this conversion is hidden and we're in a virtual base,
430 // remember that it's hidden along some inheritance path.
431 if (Hidden && InVirtual)
432 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
433
434 // If this conversion isn't hidden, add it to the appropriate output.
435 else if (!Hidden) {
436 AccessSpecifier IAccess
437 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
438
439 if (InVirtual)
440 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000441 else
John McCallb05b5f32010-03-15 09:07:48 +0000442 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000443 }
444 }
445 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000446
John McCallb05b5f32010-03-15 09:07:48 +0000447 // Collect information recursively from any base classes.
448 for (CXXRecordDecl::base_class_iterator
449 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
450 const RecordType *RT = I->getType()->getAs<RecordType>();
451 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000452
John McCallb05b5f32010-03-15 09:07:48 +0000453 AccessSpecifier BaseAccess
454 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
455 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000456
John McCallb05b5f32010-03-15 09:07:48 +0000457 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
458 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
459 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000460 }
John McCallb05b5f32010-03-15 09:07:48 +0000461}
Sebastian Redl9994a342009-10-25 17:03:50 +0000462
John McCallb05b5f32010-03-15 09:07:48 +0000463/// Collect the visible conversions of a class.
464///
465/// This would be extremely straightforward if it weren't for virtual
466/// bases. It might be worth special-casing that, really.
467static void CollectVisibleConversions(ASTContext &Context,
468 CXXRecordDecl *Record,
469 UnresolvedSetImpl &Output) {
470 // The collection of all conversions in virtual bases that we've
471 // found. These will be added to the output as long as they don't
472 // appear in the hidden-conversions set.
473 UnresolvedSet<8> VBaseCs;
474
475 // The set of conversions in virtual bases that we've determined to
476 // be hidden.
477 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
478
479 // The set of types hidden by classes derived from this one.
480 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
481
482 // Go ahead and collect the direct conversions and add them to the
483 // hidden-types set.
484 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
485 Output.append(Cs.begin(), Cs.end());
486 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
487 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
488
489 // Recursively collect conversions from base classes.
490 for (CXXRecordDecl::base_class_iterator
491 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
492 const RecordType *RT = I->getType()->getAs<RecordType>();
493 if (!RT) continue;
494
495 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
496 I->isVirtual(), I->getAccessSpecifier(),
497 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
498 }
499
500 // Add any unhidden conversions provided by virtual bases.
501 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
502 I != E; ++I) {
503 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
504 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000505 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000506}
507
508/// getVisibleConversionFunctions - get all conversion functions visible
509/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000510const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000511 // If root class, all conversions are visible.
512 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000513 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000514 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000515 if (data().ComputedVisibleConversions)
516 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000517 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000518 data().ComputedVisibleConversions = true;
519 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000520}
521
John McCall32daa422010-03-31 01:36:47 +0000522#ifndef NDEBUG
523void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000524 assert(ConvDecl->getDeclContext() == this &&
525 "conversion function does not belong to this record");
526
John McCall32daa422010-03-31 01:36:47 +0000527 ConvDecl = ConvDecl->getUnderlyingDecl();
528 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
529 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
530 } else {
531 assert(isa<CXXConversionDecl>(ConvDecl));
532 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000533}
John McCall32daa422010-03-31 01:36:47 +0000534#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000535
John McCall32daa422010-03-31 01:36:47 +0000536void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
537 // This operation is O(N) but extremely rare. Sema only uses it to
538 // remove UsingShadowDecls in a class that were followed by a direct
539 // declaration, e.g.:
540 // class A : B {
541 // using B::operator int;
542 // operator int();
543 // };
544 // This is uncommon by itself and even more uncommon in conjunction
545 // with sufficiently large numbers of directly-declared conversions
546 // that asymptotic behavior matters.
547
548 UnresolvedSetImpl &Convs = *getConversionFunctions();
549 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
550 if (Convs[I].getDecl() == ConvDecl) {
551 Convs.erase(I);
552 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
553 && "conversion was found multiple times in unresolved set");
554 return;
555 }
556 }
557
558 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000559}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000560
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000561void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
562 Method->setVirtualAsWritten(true);
563 setAggregate(false);
564 setPOD(false);
565 setEmpty(false);
566 setPolymorphic(true);
567 setHasTrivialConstructor(false);
568 setHasTrivialCopyConstructor(false);
569 setHasTrivialCopyAssignment(false);
570}
571
Douglas Gregorf6b11852009-10-08 15:14:33 +0000572CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000573 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000574 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
575
576 return 0;
577}
578
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000579MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
580 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
581}
582
Douglas Gregorf6b11852009-10-08 15:14:33 +0000583void
584CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
585 TemplateSpecializationKind TSK) {
586 assert(TemplateOrInstantiation.isNull() &&
587 "Previous template or instantiation?");
588 assert(!isa<ClassTemplateSpecializationDecl>(this));
589 TemplateOrInstantiation
590 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
591}
592
Anders Carlssonb13e3572009-12-07 06:33:48 +0000593TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
594 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000595 = dyn_cast<ClassTemplateSpecializationDecl>(this))
596 return Spec->getSpecializationKind();
597
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000598 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000599 return MSInfo->getTemplateSpecializationKind();
600
601 return TSK_Undeclared;
602}
603
604void
605CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
606 if (ClassTemplateSpecializationDecl *Spec
607 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
608 Spec->setSpecializationKind(TSK);
609 return;
610 }
611
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000612 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000613 MSInfo->setTemplateSpecializationKind(TSK);
614 return;
615 }
616
617 assert(false && "Not a class template or member class specialization");
618}
619
Douglas Gregor1d110e02010-07-01 14:13:13 +0000620CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
621 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000622 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000623
624 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000625 = Context.DeclarationNames.getCXXDestructorName(
626 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000627
John McCallc0bf4622010-02-23 00:48:20 +0000628 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000629 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000630 if (I == E)
631 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000633 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000634 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Anders Carlsson7267c162009-05-29 21:03:38 +0000636 return Dtor;
637}
638
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000639CXXMethodDecl *
640CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000641 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000642 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000643 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000644 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000645 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000646}
647
Douglas Gregor90916562009-09-29 18:16:17 +0000648bool CXXMethodDecl::isUsualDeallocationFunction() const {
649 if (getOverloadedOperator() != OO_Delete &&
650 getOverloadedOperator() != OO_Array_Delete)
651 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000652
653 // C++ [basic.stc.dynamic.deallocation]p2:
654 // A template instance is never a usual deallocation function,
655 // regardless of its signature.
656 if (getPrimaryTemplate())
657 return false;
658
Douglas Gregor90916562009-09-29 18:16:17 +0000659 // C++ [basic.stc.dynamic.deallocation]p2:
660 // If a class T has a member deallocation function named operator delete
661 // with exactly one parameter, then that function is a usual (non-placement)
662 // deallocation function. [...]
663 if (getNumParams() == 1)
664 return true;
665
666 // C++ [basic.stc.dynamic.deallocation]p2:
667 // [...] If class T does not declare such an operator delete but does
668 // declare a member deallocation function named operator delete with
669 // exactly two parameters, the second of which has type std::size_t (18.1),
670 // then this function is a usual deallocation function.
671 ASTContext &Context = getASTContext();
672 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000673 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
674 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000675 return false;
676
677 // This function is a usual deallocation function if there are no
678 // single-parameter deallocation functions of the same kind.
679 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
680 R.first != R.second; ++R.first) {
681 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
682 if (FD->getNumParams() == 1)
683 return false;
684 }
685
686 return true;
687}
688
Douglas Gregor06a9f362010-05-01 20:49:11 +0000689bool CXXMethodDecl::isCopyAssignmentOperator() const {
690 // C++0x [class.copy]p19:
691 // A user-declared copy assignment operator X::operator= is a non-static
692 // non-template member function of class X with exactly one parameter of
693 // type X, X&, const X&, volatile X& or const volatile X&.
694 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
695 /*non-static*/ isStatic() ||
696 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
697 /*exactly one parameter*/getNumParams() != 1)
698 return false;
699
700 QualType ParamType = getParamDecl(0)->getType();
701 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
702 ParamType = Ref->getPointeeType();
703
704 ASTContext &Context = getASTContext();
705 QualType ClassType
706 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
707 return Context.hasSameUnqualifiedType(ClassType, ParamType);
708}
709
Anders Carlsson05eb2442009-05-16 23:58:37 +0000710void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000711 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000712 assert(!MD->getParent()->isDependentContext() &&
713 "Can't add an overridden method to a class template!");
714
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000715 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000716}
717
718CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000719 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000720}
721
722CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000723 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000724}
725
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000726unsigned CXXMethodDecl::size_overridden_methods() const {
727 return getASTContext().overridden_methods_size(this);
728}
729
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000730QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000731 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
732 // If the member function is declared const, the type of this is const X*,
733 // if the member function is declared volatile, the type of this is
734 // volatile X*, and if the member function is declared const volatile,
735 // the type of this is const volatile X*.
736
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000737 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000738
John McCall3cb0ebd2010-03-10 03:28:59 +0000739 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000740 ClassTy = C.getQualifiedType(ClassTy,
741 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000742 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000743}
744
Eli Friedmand7d7f672009-12-06 20:50:05 +0000745bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000746 // If this function is a template instantiation, look at the template from
747 // which it was instantiated.
748 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
749 if (!CheckFn)
750 CheckFn = this;
751
Eli Friedmand7d7f672009-12-06 20:50:05 +0000752 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000753 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000754}
755
Douglas Gregor7ad83902008-11-05 04:29:56 +0000756CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000757CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +0000758 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000759 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000760 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
761 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
762 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000763{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000764}
765
766CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000767CXXBaseOrMemberInitializer(ASTContext &Context,
768 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000769 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000770 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
771 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
772 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000773{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000774}
775
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000776CXXBaseOrMemberInitializer::
777CXXBaseOrMemberInitializer(ASTContext &Context,
778 FieldDecl *Member, SourceLocation MemberLoc,
779 SourceLocation L, Expr *Init, SourceLocation R,
780 VarDecl **Indices,
781 unsigned NumIndices)
782 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000783 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
784 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000785{
786 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
787 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
788}
789
790CXXBaseOrMemberInitializer *
791CXXBaseOrMemberInitializer::Create(ASTContext &Context,
792 FieldDecl *Member,
793 SourceLocation MemberLoc,
794 SourceLocation L,
795 Expr *Init,
796 SourceLocation R,
797 VarDecl **Indices,
798 unsigned NumIndices) {
799 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
800 sizeof(VarDecl *) * NumIndices,
801 llvm::alignof<CXXBaseOrMemberInitializer>());
802 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
803 L, Init, R, Indices, NumIndices);
804}
805
Douglas Gregor802ab452009-12-02 22:36:29 +0000806TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
807 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000808 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000809 else
810 return TypeLoc();
811}
812
813Type *CXXBaseOrMemberInitializer::getBaseClass() {
814 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000815 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000816 else
817 return 0;
818}
819
820const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
821 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000822 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000823 else
824 return 0;
825}
826
827SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
828 if (isMemberInitializer())
829 return getMemberLocation();
830
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000831 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +0000832}
833
834SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
835 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000836}
837
Douglas Gregorb48fe382008-10-31 09:07:45 +0000838CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000839CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000840 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000841 QualType(), 0, false, false, false);
842}
843
844CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +0000845CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000846 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000847 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000848 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000849 bool isInline,
850 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000851 assert(NameInfo.getName().getNameKind()
852 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000853 "Name must refer to a constructor");
Abramo Bagnara25777432010-08-11 22:01:17 +0000854 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000855 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000856}
857
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000858bool CXXConstructorDecl::isDefaultConstructor() const {
859 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000860 // A default constructor for a class X is a constructor of class
861 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000862 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000863 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000864}
865
Mike Stump1eb44332009-09-09 15:08:12 +0000866bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000867CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000868 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000869 // A non-template constructor for class X is a copy constructor
870 // if its first parameter is of type X&, const X&, volatile X& or
871 // const volatile X&, and either there are no other parameters
872 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000873 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000874 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000875 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000876 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000877 return false;
878
879 const ParmVarDecl *Param = getParamDecl(0);
880
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000881 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000882 const LValueReferenceType *ParamRefType =
883 Param->getType()->getAs<LValueReferenceType>();
884 if (!ParamRefType)
885 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000886
Douglas Gregorfd476482009-11-13 23:59:09 +0000887 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000888 ASTContext &Context = getASTContext();
889
Douglas Gregorfd476482009-11-13 23:59:09 +0000890 CanQualType PointeeType
891 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000892 CanQualType ClassTy
893 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000894 if (PointeeType.getUnqualifiedType() != ClassTy)
895 return false;
896
John McCall0953e762009-09-24 19:53:00 +0000897 // FIXME: other qualifiers?
898
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000899 // We have a copy constructor.
900 TypeQuals = PointeeType.getCVRQualifiers();
901 return true;
902}
903
Anders Carlssonfaccd722009-08-28 16:57:08 +0000904bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000905 // C++ [class.conv.ctor]p1:
906 // A constructor declared without the function-specifier explicit
907 // that can be called with a single parameter specifies a
908 // conversion from the type of its first parameter to the type of
909 // its class. Such a constructor is called a converting
910 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000911 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000912 return false;
913
Mike Stump1eb44332009-09-09 15:08:12 +0000914 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000915 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000916 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000917 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000918}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000919
Douglas Gregor66724ea2009-11-14 01:20:54 +0000920bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
921 if ((getNumParams() < 1) ||
922 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
923 (getPrimaryTemplate() == 0) ||
924 (getDescribedFunctionTemplate() != 0))
925 return false;
926
927 const ParmVarDecl *Param = getParamDecl(0);
928
929 ASTContext &Context = getASTContext();
930 CanQualType ParamType = Context.getCanonicalType(Param->getType());
931
932 // Strip off the lvalue reference, if any.
933 if (CanQual<LValueReferenceType> ParamRefType
934 = ParamType->getAs<LValueReferenceType>())
935 ParamType = ParamRefType->getPointeeType();
936
937
938 // Is it the same as our our class type?
939 CanQualType ClassTy
940 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
941 if (ParamType.getUnqualifiedType() != ClassTy)
942 return false;
943
944 return true;
945}
946
Douglas Gregor42a552f2008-11-05 20:51:48 +0000947CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000948CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000949 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000950 QualType(), false, false);
951}
952
953CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +0000954CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000955 const DeclarationNameInfo &NameInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000956 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000957 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000958 assert(NameInfo.getName().getNameKind()
959 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000960 "Name must refer to a destructor");
Abramo Bagnara25777432010-08-11 22:01:17 +0000961 return new (C) CXXDestructorDecl(RD, NameInfo, T, isInline,
962 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000963}
964
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000965CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000966CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000967 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000968 QualType(), 0, false, false);
969}
970
971CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000972CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000973 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000974 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000975 bool isInline, bool isExplicit) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000976 assert(NameInfo.getName().getNameKind()
977 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000978 "Name must refer to a conversion function");
Abramo Bagnara25777432010-08-11 22:01:17 +0000979 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
980 isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000981}
982
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000983LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000984 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000985 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000986 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000987 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000988}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000989
990UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
991 SourceLocation L,
992 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000993 SourceRange QualifierRange,
994 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000995 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000996 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000997 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000998 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
999 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001000 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001001 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001002}
1003
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001004NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1005 if (NamespaceAliasDecl *NA =
1006 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1007 return NA->getNamespace();
1008 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1009}
1010
Mike Stump1eb44332009-09-09 15:08:12 +00001011NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001012 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001013 SourceLocation AliasLoc,
1014 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001015 SourceRange QualifierRange,
1016 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001017 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001018 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001019 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1020 Namespace = NS->getOriginalNamespace();
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001021 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001022 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001023}
1024
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001025UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001026 SourceRange NNR, SourceLocation UL,
1027 NestedNameSpecifier* TargetNNS,
1028 const DeclarationNameInfo &NameInfo,
1029 bool IsTypeNameArg) {
1030 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001031}
1032
John McCall7ba107a2009-11-18 02:36:19 +00001033UnresolvedUsingValueDecl *
1034UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1035 SourceLocation UsingLoc,
1036 SourceRange TargetNNR,
1037 NestedNameSpecifier *TargetNNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001038 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001039 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001040 TargetNNR, TargetNNS, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001041}
1042
1043UnresolvedUsingTypenameDecl *
1044UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1045 SourceLocation UsingLoc,
1046 SourceLocation TypenameLoc,
1047 SourceRange TargetNNR,
1048 NestedNameSpecifier *TargetNNS,
1049 SourceLocation TargetNameLoc,
1050 DeclarationName TargetName) {
1051 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1052 TargetNNR, TargetNNS,
1053 TargetNameLoc,
1054 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001055}
1056
Anders Carlssonfb311762009-03-14 00:25:26 +00001057StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1058 SourceLocation L, Expr *AssertExpr,
1059 StringLiteral *Message) {
1060 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1061}
1062
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001063static const char *getAccessName(AccessSpecifier AS) {
1064 switch (AS) {
1065 default:
1066 case AS_none:
1067 assert("Invalid access specifier!");
1068 return 0;
1069 case AS_public:
1070 return "public";
1071 case AS_private:
1072 return "private";
1073 case AS_protected:
1074 return "protected";
1075 }
1076}
1077
1078const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1079 AccessSpecifier AS) {
1080 return DB << getAccessName(AS);
1081}