blob: 1b89ab6bedaa7fd3e0b3e5f56dc4dfe778b42d90 [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
287 if (Method->getOverloadedOperator() == OO_Equal) {
288 data().DeclaredCopyAssignment = true;
289 Method->setCopyAssignment(true);
290 }
291 }
292
293 // Nothing else to do for implicitly-declared members.
294 return;
295 }
296
297 // Handle (user-declared) constructors.
298 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
299 // Note that we have a user-declared constructor.
300 data().UserDeclaredConstructor = true;
301
302 // Note that we have no need of an implicitly-declared default constructor.
303 data().DeclaredDefaultConstructor = true;
304
305 // C++ [dcl.init.aggr]p1:
306 // An aggregate is an array or a class (clause 9) with no
307 // user-declared constructors (12.1) [...].
308 data().Aggregate = false;
309
310 // C++ [class]p4:
311 // A POD-struct is an aggregate class [...]
312 data().PlainOldData = false;
313
314 // C++ [class.ctor]p5:
315 // A constructor is trivial if it is an implicitly-declared default
316 // constructor.
317 // FIXME: C++0x: don't do this for "= default" default constructors.
318 data().HasTrivialConstructor = false;
319
320 // Note when we have a user-declared copy constructor, which will
321 // suppress the implicit declaration of a copy constructor.
322 if (!FunTmpl && Constructor->isCopyConstructor()) {
323 data().UserDeclaredCopyConstructor = true;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000324 data().DeclaredCopyConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000325
326 // C++ [class.copy]p6:
327 // A copy constructor is trivial if it is implicitly declared.
328 // FIXME: C++0x: don't do this for "= default" copy constructors.
329 data().HasTrivialCopyConstructor = false;
330 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000331
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000332 return;
333 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000334
335 // FIXME: Destructors.
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000336
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000337 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
338 if (Method->getOverloadedOperator() == OO_Equal) {
339 // We're interested specifically in copy assignment operators.
340 const FunctionProtoType *FnType
341 = Method->getType()->getAs<FunctionProtoType>();
342 assert(FnType && "Overloaded operator has no proto function type.");
343 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
344
345 // Copy assignment operators must be non-templates.
346 if (Method->getPrimaryTemplate() || FunTmpl)
347 return;
348
349 ASTContext &Context = getASTContext();
350 QualType ArgType = FnType->getArgType(0);
351 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
352 ArgType = Ref->getPointeeType();
353
354 ArgType = ArgType.getUnqualifiedType();
355 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
356 const_cast<CXXRecordDecl*>(this)));
357
358 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
359 return;
360
361 // This is a copy assignment operator.
362 // Note on the decl that it is a copy assignment operator.
363 Method->setCopyAssignment(true);
364
365 // Suppress the implicit declaration of a copy constructor.
366 data().UserDeclaredCopyAssignment = true;
367 data().DeclaredCopyAssignment = true;
368
369 // C++ [class.copy]p11:
370 // A copy assignment operator is trivial if it is implicitly declared.
371 // FIXME: C++0x: don't do this for "= default" copy operators.
372 data().HasTrivialCopyAssignment = false;
373
374 // C++ [class]p4:
375 // A POD-struct is an aggregate class that [...] has no user-defined copy
376 // assignment operator [...].
377 data().PlainOldData = false;
378 }
Douglas Gregor22584312010-07-02 23:41:54 +0000379
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000380 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000381 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000382}
383
John McCallb05b5f32010-03-15 09:07:48 +0000384static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
385 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000386 if (isa<UsingShadowDecl>(Conv))
387 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000388 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
389 T = ConvTemp->getTemplatedDecl()->getResultType();
390 else
391 T = cast<CXXConversionDecl>(Conv)->getConversionType();
392 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000393}
394
John McCallb05b5f32010-03-15 09:07:48 +0000395/// Collect the visible conversions of a base class.
396///
397/// \param Base a base class of the class we're considering
398/// \param InVirtual whether this base class is a virtual base (or a base
399/// of a virtual base)
400/// \param Access the access along the inheritance path to this base
401/// \param ParentHiddenTypes the conversions provided by the inheritors
402/// of this base
403/// \param Output the set to which to add conversions from non-virtual bases
404/// \param VOutput the set to which to add conversions from virtual bases
405/// \param HiddenVBaseCs the set of conversions which were hidden in a
406/// virtual base along some inheritance path
407static void CollectVisibleConversions(ASTContext &Context,
408 CXXRecordDecl *Record,
409 bool InVirtual,
410 AccessSpecifier Access,
411 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
412 UnresolvedSetImpl &Output,
413 UnresolvedSetImpl &VOutput,
414 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
415 // The set of types which have conversions in this class or its
416 // subclasses. As an optimization, we don't copy the derived set
417 // unless it might change.
418 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
419 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
420
421 // Collect the direct conversions and figure out which conversions
422 // will be hidden in the subclasses.
423 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
424 if (!Cs.empty()) {
425 HiddenTypesBuffer = ParentHiddenTypes;
426 HiddenTypes = &HiddenTypesBuffer;
427
428 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
429 bool Hidden =
430 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
431
432 // If this conversion is hidden and we're in a virtual base,
433 // remember that it's hidden along some inheritance path.
434 if (Hidden && InVirtual)
435 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
436
437 // If this conversion isn't hidden, add it to the appropriate output.
438 else if (!Hidden) {
439 AccessSpecifier IAccess
440 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
441
442 if (InVirtual)
443 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000444 else
John McCallb05b5f32010-03-15 09:07:48 +0000445 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000446 }
447 }
448 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000449
John McCallb05b5f32010-03-15 09:07:48 +0000450 // Collect information recursively from any base classes.
451 for (CXXRecordDecl::base_class_iterator
452 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
453 const RecordType *RT = I->getType()->getAs<RecordType>();
454 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000455
John McCallb05b5f32010-03-15 09:07:48 +0000456 AccessSpecifier BaseAccess
457 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
458 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000459
John McCallb05b5f32010-03-15 09:07:48 +0000460 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
461 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
462 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000463 }
John McCallb05b5f32010-03-15 09:07:48 +0000464}
Sebastian Redl9994a342009-10-25 17:03:50 +0000465
John McCallb05b5f32010-03-15 09:07:48 +0000466/// Collect the visible conversions of a class.
467///
468/// This would be extremely straightforward if it weren't for virtual
469/// bases. It might be worth special-casing that, really.
470static void CollectVisibleConversions(ASTContext &Context,
471 CXXRecordDecl *Record,
472 UnresolvedSetImpl &Output) {
473 // The collection of all conversions in virtual bases that we've
474 // found. These will be added to the output as long as they don't
475 // appear in the hidden-conversions set.
476 UnresolvedSet<8> VBaseCs;
477
478 // The set of conversions in virtual bases that we've determined to
479 // be hidden.
480 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
481
482 // The set of types hidden by classes derived from this one.
483 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
484
485 // Go ahead and collect the direct conversions and add them to the
486 // hidden-types set.
487 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
488 Output.append(Cs.begin(), Cs.end());
489 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
490 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
491
492 // Recursively collect conversions from base classes.
493 for (CXXRecordDecl::base_class_iterator
494 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
495 const RecordType *RT = I->getType()->getAs<RecordType>();
496 if (!RT) continue;
497
498 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
499 I->isVirtual(), I->getAccessSpecifier(),
500 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
501 }
502
503 // Add any unhidden conversions provided by virtual bases.
504 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
505 I != E; ++I) {
506 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
507 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000508 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000509}
510
511/// getVisibleConversionFunctions - get all conversion functions visible
512/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000513const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000514 // If root class, all conversions are visible.
515 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000516 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000517 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000518 if (data().ComputedVisibleConversions)
519 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000520 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000521 data().ComputedVisibleConversions = true;
522 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000523}
524
John McCall32daa422010-03-31 01:36:47 +0000525#ifndef NDEBUG
526void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000527 assert(ConvDecl->getDeclContext() == this &&
528 "conversion function does not belong to this record");
529
John McCall32daa422010-03-31 01:36:47 +0000530 ConvDecl = ConvDecl->getUnderlyingDecl();
531 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
532 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
533 } else {
534 assert(isa<CXXConversionDecl>(ConvDecl));
535 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000536}
John McCall32daa422010-03-31 01:36:47 +0000537#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000538
John McCall32daa422010-03-31 01:36:47 +0000539void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
540 // This operation is O(N) but extremely rare. Sema only uses it to
541 // remove UsingShadowDecls in a class that were followed by a direct
542 // declaration, e.g.:
543 // class A : B {
544 // using B::operator int;
545 // operator int();
546 // };
547 // This is uncommon by itself and even more uncommon in conjunction
548 // with sufficiently large numbers of directly-declared conversions
549 // that asymptotic behavior matters.
550
551 UnresolvedSetImpl &Convs = *getConversionFunctions();
552 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
553 if (Convs[I].getDecl() == ConvDecl) {
554 Convs.erase(I);
555 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
556 && "conversion was found multiple times in unresolved set");
557 return;
558 }
559 }
560
561 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000562}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000563
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000564void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
565 Method->setVirtualAsWritten(true);
566 setAggregate(false);
567 setPOD(false);
568 setEmpty(false);
569 setPolymorphic(true);
570 setHasTrivialConstructor(false);
571 setHasTrivialCopyConstructor(false);
572 setHasTrivialCopyAssignment(false);
573}
574
Douglas Gregorf6b11852009-10-08 15:14:33 +0000575CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000576 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000577 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
578
579 return 0;
580}
581
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000582MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
583 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
584}
585
Douglas Gregorf6b11852009-10-08 15:14:33 +0000586void
587CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
588 TemplateSpecializationKind TSK) {
589 assert(TemplateOrInstantiation.isNull() &&
590 "Previous template or instantiation?");
591 assert(!isa<ClassTemplateSpecializationDecl>(this));
592 TemplateOrInstantiation
593 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
594}
595
Anders Carlssonb13e3572009-12-07 06:33:48 +0000596TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
597 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000598 = dyn_cast<ClassTemplateSpecializationDecl>(this))
599 return Spec->getSpecializationKind();
600
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000601 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000602 return MSInfo->getTemplateSpecializationKind();
603
604 return TSK_Undeclared;
605}
606
607void
608CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
609 if (ClassTemplateSpecializationDecl *Spec
610 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
611 Spec->setSpecializationKind(TSK);
612 return;
613 }
614
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000615 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000616 MSInfo->setTemplateSpecializationKind(TSK);
617 return;
618 }
619
620 assert(false && "Not a class template or member class specialization");
621}
622
Douglas Gregor1d110e02010-07-01 14:13:13 +0000623CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
624 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000625 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000626
627 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000628 = Context.DeclarationNames.getCXXDestructorName(
629 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000630
John McCallc0bf4622010-02-23 00:48:20 +0000631 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000632 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000633 if (I == E)
634 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000636 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000637 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Anders Carlsson7267c162009-05-29 21:03:38 +0000639 return Dtor;
640}
641
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000642CXXMethodDecl *
643CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000644 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000645 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000646 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000647 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000648 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000649}
650
Douglas Gregor90916562009-09-29 18:16:17 +0000651bool CXXMethodDecl::isUsualDeallocationFunction() const {
652 if (getOverloadedOperator() != OO_Delete &&
653 getOverloadedOperator() != OO_Array_Delete)
654 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000655
656 // C++ [basic.stc.dynamic.deallocation]p2:
657 // A template instance is never a usual deallocation function,
658 // regardless of its signature.
659 if (getPrimaryTemplate())
660 return false;
661
Douglas Gregor90916562009-09-29 18:16:17 +0000662 // C++ [basic.stc.dynamic.deallocation]p2:
663 // If a class T has a member deallocation function named operator delete
664 // with exactly one parameter, then that function is a usual (non-placement)
665 // deallocation function. [...]
666 if (getNumParams() == 1)
667 return true;
668
669 // C++ [basic.stc.dynamic.deallocation]p2:
670 // [...] If class T does not declare such an operator delete but does
671 // declare a member deallocation function named operator delete with
672 // exactly two parameters, the second of which has type std::size_t (18.1),
673 // then this function is a usual deallocation function.
674 ASTContext &Context = getASTContext();
675 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000676 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
677 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000678 return false;
679
680 // This function is a usual deallocation function if there are no
681 // single-parameter deallocation functions of the same kind.
682 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
683 R.first != R.second; ++R.first) {
684 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
685 if (FD->getNumParams() == 1)
686 return false;
687 }
688
689 return true;
690}
691
Douglas Gregor06a9f362010-05-01 20:49:11 +0000692bool CXXMethodDecl::isCopyAssignmentOperator() const {
693 // C++0x [class.copy]p19:
694 // A user-declared copy assignment operator X::operator= is a non-static
695 // non-template member function of class X with exactly one parameter of
696 // type X, X&, const X&, volatile X& or const volatile X&.
697 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
698 /*non-static*/ isStatic() ||
699 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
700 /*exactly one parameter*/getNumParams() != 1)
701 return false;
702
703 QualType ParamType = getParamDecl(0)->getType();
704 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
705 ParamType = Ref->getPointeeType();
706
707 ASTContext &Context = getASTContext();
708 QualType ClassType
709 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
710 return Context.hasSameUnqualifiedType(ClassType, ParamType);
711}
712
Anders Carlsson05eb2442009-05-16 23:58:37 +0000713void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000714 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000715 assert(!MD->getParent()->isDependentContext() &&
716 "Can't add an overridden method to a class template!");
717
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000718 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000719}
720
721CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000722 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000723}
724
725CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000726 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000727}
728
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000729unsigned CXXMethodDecl::size_overridden_methods() const {
730 return getASTContext().overridden_methods_size(this);
731}
732
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000733QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000734 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
735 // If the member function is declared const, the type of this is const X*,
736 // if the member function is declared volatile, the type of this is
737 // volatile X*, and if the member function is declared const volatile,
738 // the type of this is const volatile X*.
739
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000740 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000741
John McCall3cb0ebd2010-03-10 03:28:59 +0000742 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000743 ClassTy = C.getQualifiedType(ClassTy,
744 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000745 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000746}
747
Eli Friedmand7d7f672009-12-06 20:50:05 +0000748bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000749 // If this function is a template instantiation, look at the template from
750 // which it was instantiated.
751 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
752 if (!CheckFn)
753 CheckFn = this;
754
Eli Friedmand7d7f672009-12-06 20:50:05 +0000755 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000756 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000757}
758
Douglas Gregor7ad83902008-11-05 04:29:56 +0000759CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000760CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +0000761 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000762 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000763 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
764 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
765 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000766{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000767}
768
769CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000770CXXBaseOrMemberInitializer(ASTContext &Context,
771 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000772 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000773 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
774 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
775 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000776{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000777}
778
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000779CXXBaseOrMemberInitializer::
780CXXBaseOrMemberInitializer(ASTContext &Context,
781 FieldDecl *Member, SourceLocation MemberLoc,
782 SourceLocation L, Expr *Init, SourceLocation R,
783 VarDecl **Indices,
784 unsigned NumIndices)
785 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000786 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
787 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000788{
789 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
790 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
791}
792
793CXXBaseOrMemberInitializer *
794CXXBaseOrMemberInitializer::Create(ASTContext &Context,
795 FieldDecl *Member,
796 SourceLocation MemberLoc,
797 SourceLocation L,
798 Expr *Init,
799 SourceLocation R,
800 VarDecl **Indices,
801 unsigned NumIndices) {
802 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
803 sizeof(VarDecl *) * NumIndices,
804 llvm::alignof<CXXBaseOrMemberInitializer>());
805 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
806 L, Init, R, Indices, NumIndices);
807}
808
Douglas Gregor802ab452009-12-02 22:36:29 +0000809TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
810 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000811 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000812 else
813 return TypeLoc();
814}
815
816Type *CXXBaseOrMemberInitializer::getBaseClass() {
817 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000818 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000819 else
820 return 0;
821}
822
823const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
824 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000825 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000826 else
827 return 0;
828}
829
830SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
831 if (isMemberInitializer())
832 return getMemberLocation();
833
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000834 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +0000835}
836
837SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
838 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000839}
840
Douglas Gregorb48fe382008-10-31 09:07:45 +0000841CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000842CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000843 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000844 QualType(), 0, false, false, false);
845}
846
847CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +0000848CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000849 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000850 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000851 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000852 bool isInline,
853 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000854 assert(NameInfo.getName().getNameKind()
855 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000856 "Name must refer to a constructor");
Abramo Bagnara25777432010-08-11 22:01:17 +0000857 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000858 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000859}
860
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000861bool CXXConstructorDecl::isDefaultConstructor() const {
862 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000863 // A default constructor for a class X is a constructor of class
864 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000865 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000866 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000867}
868
Mike Stump1eb44332009-09-09 15:08:12 +0000869bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000870CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000871 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000872 // A non-template constructor for class X is a copy constructor
873 // if its first parameter is of type X&, const X&, volatile X& or
874 // const volatile X&, and either there are no other parameters
875 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000876 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000877 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000878 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000879 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000880 return false;
881
882 const ParmVarDecl *Param = getParamDecl(0);
883
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000884 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000885 const LValueReferenceType *ParamRefType =
886 Param->getType()->getAs<LValueReferenceType>();
887 if (!ParamRefType)
888 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000889
Douglas Gregorfd476482009-11-13 23:59:09 +0000890 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000891 ASTContext &Context = getASTContext();
892
Douglas Gregorfd476482009-11-13 23:59:09 +0000893 CanQualType PointeeType
894 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000895 CanQualType ClassTy
896 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000897 if (PointeeType.getUnqualifiedType() != ClassTy)
898 return false;
899
John McCall0953e762009-09-24 19:53:00 +0000900 // FIXME: other qualifiers?
901
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000902 // We have a copy constructor.
903 TypeQuals = PointeeType.getCVRQualifiers();
904 return true;
905}
906
Anders Carlssonfaccd722009-08-28 16:57:08 +0000907bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000908 // C++ [class.conv.ctor]p1:
909 // A constructor declared without the function-specifier explicit
910 // that can be called with a single parameter specifies a
911 // conversion from the type of its first parameter to the type of
912 // its class. Such a constructor is called a converting
913 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000914 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000915 return false;
916
Mike Stump1eb44332009-09-09 15:08:12 +0000917 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000918 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000919 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000920 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000921}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000922
Douglas Gregor66724ea2009-11-14 01:20:54 +0000923bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
924 if ((getNumParams() < 1) ||
925 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
926 (getPrimaryTemplate() == 0) ||
927 (getDescribedFunctionTemplate() != 0))
928 return false;
929
930 const ParmVarDecl *Param = getParamDecl(0);
931
932 ASTContext &Context = getASTContext();
933 CanQualType ParamType = Context.getCanonicalType(Param->getType());
934
935 // Strip off the lvalue reference, if any.
936 if (CanQual<LValueReferenceType> ParamRefType
937 = ParamType->getAs<LValueReferenceType>())
938 ParamType = ParamRefType->getPointeeType();
939
940
941 // Is it the same as our our class type?
942 CanQualType ClassTy
943 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
944 if (ParamType.getUnqualifiedType() != ClassTy)
945 return false;
946
947 return true;
948}
949
Douglas Gregor42a552f2008-11-05 20:51:48 +0000950CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000951CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000952 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000953 QualType(), false, false);
954}
955
956CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +0000957CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000958 const DeclarationNameInfo &NameInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000959 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000960 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000961 assert(NameInfo.getName().getNameKind()
962 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000963 "Name must refer to a destructor");
Abramo Bagnara25777432010-08-11 22:01:17 +0000964 return new (C) CXXDestructorDecl(RD, NameInfo, T, isInline,
965 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000966}
967
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000968CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000969CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000970 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000971 QualType(), 0, false, false);
972}
973
974CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000975CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000976 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000977 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000978 bool isInline, bool isExplicit) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000979 assert(NameInfo.getName().getNameKind()
980 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000981 "Name must refer to a conversion function");
Abramo Bagnara25777432010-08-11 22:01:17 +0000982 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
983 isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000984}
985
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000986LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000987 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000988 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000989 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000990 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000991}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000992
993UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
994 SourceLocation L,
995 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000996 SourceRange QualifierRange,
997 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000998 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000999 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001000 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001001 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1002 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001003 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001004 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001005}
1006
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001007NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1008 if (NamespaceAliasDecl *NA =
1009 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1010 return NA->getNamespace();
1011 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1012}
1013
Mike Stump1eb44332009-09-09 15:08:12 +00001014NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001015 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001016 SourceLocation AliasLoc,
1017 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001018 SourceRange QualifierRange,
1019 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001020 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001021 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001022 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1023 Namespace = NS->getOriginalNamespace();
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001024 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001025 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001026}
1027
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001028UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001029 SourceRange NNR, SourceLocation UL,
1030 NestedNameSpecifier* TargetNNS,
1031 const DeclarationNameInfo &NameInfo,
1032 bool IsTypeNameArg) {
1033 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001034}
1035
John McCall7ba107a2009-11-18 02:36:19 +00001036UnresolvedUsingValueDecl *
1037UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1038 SourceLocation UsingLoc,
1039 SourceRange TargetNNR,
1040 NestedNameSpecifier *TargetNNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001041 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001042 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001043 TargetNNR, TargetNNS, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001044}
1045
1046UnresolvedUsingTypenameDecl *
1047UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1048 SourceLocation UsingLoc,
1049 SourceLocation TypenameLoc,
1050 SourceRange TargetNNR,
1051 NestedNameSpecifier *TargetNNS,
1052 SourceLocation TargetNameLoc,
1053 DeclarationName TargetName) {
1054 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1055 TargetNNR, TargetNNS,
1056 TargetNameLoc,
1057 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001058}
1059
Anders Carlssonfb311762009-03-14 00:25:26 +00001060StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1061 SourceLocation L, Expr *AssertExpr,
1062 StringLiteral *Message) {
1063 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1064}
1065
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001066static const char *getAccessName(AccessSpecifier AS) {
1067 switch (AS) {
1068 default:
1069 case AS_none:
1070 assert("Invalid access specifier!");
1071 return 0;
1072 case AS_public:
1073 return "public";
1074 case AS_private:
1075 return "private";
1076 case AS_protected:
1077 return "protected";
1078 }
1079}
1080
1081const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1082 AccessSpecifier AS) {
1083 return DB << getAccessName(AS);
1084}