blob: 5d9a09da702083fc779082c57417b3ab6da3aa23 [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;
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000281 } else if (isa<CXXDestructorDecl>(D)) {
282 data().DeclaredDestructor = true;
283 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000284 // 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
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000333 // Handle (user-declared) destructors.
334 if (isa<CXXDestructorDecl>(D)) {
335 data().DeclaredDestructor = true;
336 data().UserDeclaredDestructor = true;
337 return;
338 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000339
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000340 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000341 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
342 if (Method->getOverloadedOperator() == OO_Equal) {
343 // We're interested specifically in copy assignment operators.
344 const FunctionProtoType *FnType
345 = Method->getType()->getAs<FunctionProtoType>();
346 assert(FnType && "Overloaded operator has no proto function type.");
347 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
348
349 // Copy assignment operators must be non-templates.
350 if (Method->getPrimaryTemplate() || FunTmpl)
351 return;
352
353 ASTContext &Context = getASTContext();
354 QualType ArgType = FnType->getArgType(0);
355 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
356 ArgType = Ref->getPointeeType();
357
358 ArgType = ArgType.getUnqualifiedType();
359 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
360 const_cast<CXXRecordDecl*>(this)));
361
362 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
363 return;
364
365 // This is a copy assignment operator.
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000366 // FIXME: Move assignment operators.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000367
368 // Suppress the implicit declaration of a copy constructor.
369 data().UserDeclaredCopyAssignment = true;
370 data().DeclaredCopyAssignment = true;
371
372 // C++ [class.copy]p11:
373 // A copy assignment operator is trivial if it is implicitly declared.
374 // FIXME: C++0x: don't do this for "= default" copy operators.
375 data().HasTrivialCopyAssignment = false;
376
377 // C++ [class]p4:
378 // A POD-struct is an aggregate class that [...] has no user-defined copy
379 // assignment operator [...].
380 data().PlainOldData = false;
381 }
Douglas Gregor22584312010-07-02 23:41:54 +0000382
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000383 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000384 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000385}
386
John McCallb05b5f32010-03-15 09:07:48 +0000387static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
388 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000389 if (isa<UsingShadowDecl>(Conv))
390 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000391 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
392 T = ConvTemp->getTemplatedDecl()->getResultType();
393 else
394 T = cast<CXXConversionDecl>(Conv)->getConversionType();
395 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000396}
397
John McCallb05b5f32010-03-15 09:07:48 +0000398/// Collect the visible conversions of a base class.
399///
400/// \param Base a base class of the class we're considering
401/// \param InVirtual whether this base class is a virtual base (or a base
402/// of a virtual base)
403/// \param Access the access along the inheritance path to this base
404/// \param ParentHiddenTypes the conversions provided by the inheritors
405/// of this base
406/// \param Output the set to which to add conversions from non-virtual bases
407/// \param VOutput the set to which to add conversions from virtual bases
408/// \param HiddenVBaseCs the set of conversions which were hidden in a
409/// virtual base along some inheritance path
410static void CollectVisibleConversions(ASTContext &Context,
411 CXXRecordDecl *Record,
412 bool InVirtual,
413 AccessSpecifier Access,
414 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
415 UnresolvedSetImpl &Output,
416 UnresolvedSetImpl &VOutput,
417 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
418 // The set of types which have conversions in this class or its
419 // subclasses. As an optimization, we don't copy the derived set
420 // unless it might change.
421 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
422 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
423
424 // Collect the direct conversions and figure out which conversions
425 // will be hidden in the subclasses.
426 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
427 if (!Cs.empty()) {
428 HiddenTypesBuffer = ParentHiddenTypes;
429 HiddenTypes = &HiddenTypesBuffer;
430
431 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
432 bool Hidden =
433 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
434
435 // If this conversion is hidden and we're in a virtual base,
436 // remember that it's hidden along some inheritance path.
437 if (Hidden && InVirtual)
438 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
439
440 // If this conversion isn't hidden, add it to the appropriate output.
441 else if (!Hidden) {
442 AccessSpecifier IAccess
443 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
444
445 if (InVirtual)
446 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000447 else
John McCallb05b5f32010-03-15 09:07:48 +0000448 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000449 }
450 }
451 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000452
John McCallb05b5f32010-03-15 09:07:48 +0000453 // Collect information recursively from any base classes.
454 for (CXXRecordDecl::base_class_iterator
455 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
456 const RecordType *RT = I->getType()->getAs<RecordType>();
457 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000458
John McCallb05b5f32010-03-15 09:07:48 +0000459 AccessSpecifier BaseAccess
460 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
461 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000462
John McCallb05b5f32010-03-15 09:07:48 +0000463 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
464 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
465 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000466 }
John McCallb05b5f32010-03-15 09:07:48 +0000467}
Sebastian Redl9994a342009-10-25 17:03:50 +0000468
John McCallb05b5f32010-03-15 09:07:48 +0000469/// Collect the visible conversions of a class.
470///
471/// This would be extremely straightforward if it weren't for virtual
472/// bases. It might be worth special-casing that, really.
473static void CollectVisibleConversions(ASTContext &Context,
474 CXXRecordDecl *Record,
475 UnresolvedSetImpl &Output) {
476 // The collection of all conversions in virtual bases that we've
477 // found. These will be added to the output as long as they don't
478 // appear in the hidden-conversions set.
479 UnresolvedSet<8> VBaseCs;
480
481 // The set of conversions in virtual bases that we've determined to
482 // be hidden.
483 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
484
485 // The set of types hidden by classes derived from this one.
486 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
487
488 // Go ahead and collect the direct conversions and add them to the
489 // hidden-types set.
490 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
491 Output.append(Cs.begin(), Cs.end());
492 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
493 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
494
495 // Recursively collect conversions from base classes.
496 for (CXXRecordDecl::base_class_iterator
497 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
498 const RecordType *RT = I->getType()->getAs<RecordType>();
499 if (!RT) continue;
500
501 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
502 I->isVirtual(), I->getAccessSpecifier(),
503 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
504 }
505
506 // Add any unhidden conversions provided by virtual bases.
507 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
508 I != E; ++I) {
509 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
510 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000511 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000512}
513
514/// getVisibleConversionFunctions - get all conversion functions visible
515/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000516const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000517 // If root class, all conversions are visible.
518 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000519 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000520 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000521 if (data().ComputedVisibleConversions)
522 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000523 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000524 data().ComputedVisibleConversions = true;
525 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000526}
527
John McCall32daa422010-03-31 01:36:47 +0000528#ifndef NDEBUG
529void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000530 assert(ConvDecl->getDeclContext() == this &&
531 "conversion function does not belong to this record");
532
John McCall32daa422010-03-31 01:36:47 +0000533 ConvDecl = ConvDecl->getUnderlyingDecl();
534 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
535 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
536 } else {
537 assert(isa<CXXConversionDecl>(ConvDecl));
538 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000539}
John McCall32daa422010-03-31 01:36:47 +0000540#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000541
John McCall32daa422010-03-31 01:36:47 +0000542void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
543 // This operation is O(N) but extremely rare. Sema only uses it to
544 // remove UsingShadowDecls in a class that were followed by a direct
545 // declaration, e.g.:
546 // class A : B {
547 // using B::operator int;
548 // operator int();
549 // };
550 // This is uncommon by itself and even more uncommon in conjunction
551 // with sufficiently large numbers of directly-declared conversions
552 // that asymptotic behavior matters.
553
554 UnresolvedSetImpl &Convs = *getConversionFunctions();
555 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
556 if (Convs[I].getDecl() == ConvDecl) {
557 Convs.erase(I);
558 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
559 && "conversion was found multiple times in unresolved set");
560 return;
561 }
562 }
563
564 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000565}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000566
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000567void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
568 Method->setVirtualAsWritten(true);
569 setAggregate(false);
570 setPOD(false);
571 setEmpty(false);
572 setPolymorphic(true);
573 setHasTrivialConstructor(false);
574 setHasTrivialCopyConstructor(false);
575 setHasTrivialCopyAssignment(false);
576}
577
Douglas Gregorf6b11852009-10-08 15:14:33 +0000578CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000579 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000580 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
581
582 return 0;
583}
584
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000585MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
586 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
587}
588
Douglas Gregorf6b11852009-10-08 15:14:33 +0000589void
590CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
591 TemplateSpecializationKind TSK) {
592 assert(TemplateOrInstantiation.isNull() &&
593 "Previous template or instantiation?");
594 assert(!isa<ClassTemplateSpecializationDecl>(this));
595 TemplateOrInstantiation
596 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
597}
598
Anders Carlssonb13e3572009-12-07 06:33:48 +0000599TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
600 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000601 = dyn_cast<ClassTemplateSpecializationDecl>(this))
602 return Spec->getSpecializationKind();
603
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000604 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000605 return MSInfo->getTemplateSpecializationKind();
606
607 return TSK_Undeclared;
608}
609
610void
611CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
612 if (ClassTemplateSpecializationDecl *Spec
613 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
614 Spec->setSpecializationKind(TSK);
615 return;
616 }
617
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000618 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000619 MSInfo->setTemplateSpecializationKind(TSK);
620 return;
621 }
622
623 assert(false && "Not a class template or member class specialization");
624}
625
Douglas Gregor1d110e02010-07-01 14:13:13 +0000626CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
627 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000628 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000629
630 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000631 = Context.DeclarationNames.getCXXDestructorName(
632 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000633
John McCallc0bf4622010-02-23 00:48:20 +0000634 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000635 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000636 if (I == E)
637 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000639 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000640 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Anders Carlsson7267c162009-05-29 21:03:38 +0000642 return Dtor;
643}
644
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000645CXXMethodDecl *
646CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000647 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000648 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000649 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000650 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000651 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000652}
653
Douglas Gregor90916562009-09-29 18:16:17 +0000654bool CXXMethodDecl::isUsualDeallocationFunction() const {
655 if (getOverloadedOperator() != OO_Delete &&
656 getOverloadedOperator() != OO_Array_Delete)
657 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000658
659 // C++ [basic.stc.dynamic.deallocation]p2:
660 // A template instance is never a usual deallocation function,
661 // regardless of its signature.
662 if (getPrimaryTemplate())
663 return false;
664
Douglas Gregor90916562009-09-29 18:16:17 +0000665 // C++ [basic.stc.dynamic.deallocation]p2:
666 // If a class T has a member deallocation function named operator delete
667 // with exactly one parameter, then that function is a usual (non-placement)
668 // deallocation function. [...]
669 if (getNumParams() == 1)
670 return true;
671
672 // C++ [basic.stc.dynamic.deallocation]p2:
673 // [...] If class T does not declare such an operator delete but does
674 // declare a member deallocation function named operator delete with
675 // exactly two parameters, the second of which has type std::size_t (18.1),
676 // then this function is a usual deallocation function.
677 ASTContext &Context = getASTContext();
678 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000679 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
680 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000681 return false;
682
683 // This function is a usual deallocation function if there are no
684 // single-parameter deallocation functions of the same kind.
685 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
686 R.first != R.second; ++R.first) {
687 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
688 if (FD->getNumParams() == 1)
689 return false;
690 }
691
692 return true;
693}
694
Douglas Gregor06a9f362010-05-01 20:49:11 +0000695bool CXXMethodDecl::isCopyAssignmentOperator() const {
696 // C++0x [class.copy]p19:
697 // A user-declared copy assignment operator X::operator= is a non-static
698 // non-template member function of class X with exactly one parameter of
699 // type X, X&, const X&, volatile X& or const volatile X&.
700 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
701 /*non-static*/ isStatic() ||
702 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
703 /*exactly one parameter*/getNumParams() != 1)
704 return false;
705
706 QualType ParamType = getParamDecl(0)->getType();
707 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
708 ParamType = Ref->getPointeeType();
709
710 ASTContext &Context = getASTContext();
711 QualType ClassType
712 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
713 return Context.hasSameUnqualifiedType(ClassType, ParamType);
714}
715
Anders Carlsson05eb2442009-05-16 23:58:37 +0000716void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000717 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000718 assert(!MD->getParent()->isDependentContext() &&
719 "Can't add an overridden method to a class template!");
720
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000721 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000722}
723
724CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000725 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000726}
727
728CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000729 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000730}
731
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000732unsigned CXXMethodDecl::size_overridden_methods() const {
733 return getASTContext().overridden_methods_size(this);
734}
735
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000736QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000737 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
738 // If the member function is declared const, the type of this is const X*,
739 // if the member function is declared volatile, the type of this is
740 // volatile X*, and if the member function is declared const volatile,
741 // the type of this is const volatile X*.
742
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000743 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000744
John McCall3cb0ebd2010-03-10 03:28:59 +0000745 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000746 ClassTy = C.getQualifiedType(ClassTy,
747 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000748 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000749}
750
Eli Friedmand7d7f672009-12-06 20:50:05 +0000751bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000752 // If this function is a template instantiation, look at the template from
753 // which it was instantiated.
754 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
755 if (!CheckFn)
756 CheckFn = this;
757
Eli Friedmand7d7f672009-12-06 20:50:05 +0000758 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000759 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000760}
761
Douglas Gregor7ad83902008-11-05 04:29:56 +0000762CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000763CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +0000764 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000765 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000766 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
767 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
768 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000769{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000770}
771
772CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000773CXXBaseOrMemberInitializer(ASTContext &Context,
774 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000775 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000776 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
777 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
778 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000779{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000780}
781
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000782CXXBaseOrMemberInitializer::
783CXXBaseOrMemberInitializer(ASTContext &Context,
784 FieldDecl *Member, SourceLocation MemberLoc,
785 SourceLocation L, Expr *Init, SourceLocation R,
786 VarDecl **Indices,
787 unsigned NumIndices)
788 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000789 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
790 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000791{
792 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
793 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
794}
795
796CXXBaseOrMemberInitializer *
797CXXBaseOrMemberInitializer::Create(ASTContext &Context,
798 FieldDecl *Member,
799 SourceLocation MemberLoc,
800 SourceLocation L,
801 Expr *Init,
802 SourceLocation R,
803 VarDecl **Indices,
804 unsigned NumIndices) {
805 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
806 sizeof(VarDecl *) * NumIndices,
807 llvm::alignof<CXXBaseOrMemberInitializer>());
808 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
809 L, Init, R, Indices, NumIndices);
810}
811
Douglas Gregor802ab452009-12-02 22:36:29 +0000812TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
813 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000814 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000815 else
816 return TypeLoc();
817}
818
819Type *CXXBaseOrMemberInitializer::getBaseClass() {
820 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000821 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000822 else
823 return 0;
824}
825
826const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
827 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000828 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000829 else
830 return 0;
831}
832
833SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
834 if (isMemberInitializer())
835 return getMemberLocation();
836
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000837 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +0000838}
839
840SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
841 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000842}
843
Douglas Gregorb48fe382008-10-31 09:07:45 +0000844CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000845CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000846 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000847 QualType(), 0, false, false, false);
848}
849
850CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +0000851CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000852 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000853 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000854 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000855 bool isInline,
856 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000857 assert(NameInfo.getName().getNameKind()
858 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000859 "Name must refer to a constructor");
Abramo Bagnara25777432010-08-11 22:01:17 +0000860 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000861 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000862}
863
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000864bool CXXConstructorDecl::isDefaultConstructor() const {
865 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000866 // A default constructor for a class X is a constructor of class
867 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000868 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000869 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000870}
871
Mike Stump1eb44332009-09-09 15:08:12 +0000872bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000873CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000874 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000875 // A non-template constructor for class X is a copy constructor
876 // if its first parameter is of type X&, const X&, volatile X& or
877 // const volatile X&, and either there are no other parameters
878 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000879 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000880 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000881 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000882 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000883 return false;
884
885 const ParmVarDecl *Param = getParamDecl(0);
886
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000887 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000888 const LValueReferenceType *ParamRefType =
889 Param->getType()->getAs<LValueReferenceType>();
890 if (!ParamRefType)
891 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000892
Douglas Gregorfd476482009-11-13 23:59:09 +0000893 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000894 ASTContext &Context = getASTContext();
895
Douglas Gregorfd476482009-11-13 23:59:09 +0000896 CanQualType PointeeType
897 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000898 CanQualType ClassTy
899 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000900 if (PointeeType.getUnqualifiedType() != ClassTy)
901 return false;
902
John McCall0953e762009-09-24 19:53:00 +0000903 // FIXME: other qualifiers?
904
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000905 // We have a copy constructor.
906 TypeQuals = PointeeType.getCVRQualifiers();
907 return true;
908}
909
Anders Carlssonfaccd722009-08-28 16:57:08 +0000910bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000911 // C++ [class.conv.ctor]p1:
912 // A constructor declared without the function-specifier explicit
913 // that can be called with a single parameter specifies a
914 // conversion from the type of its first parameter to the type of
915 // its class. Such a constructor is called a converting
916 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000917 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000918 return false;
919
Mike Stump1eb44332009-09-09 15:08:12 +0000920 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000921 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000922 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000923 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000924}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000925
Douglas Gregor66724ea2009-11-14 01:20:54 +0000926bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
927 if ((getNumParams() < 1) ||
928 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
929 (getPrimaryTemplate() == 0) ||
930 (getDescribedFunctionTemplate() != 0))
931 return false;
932
933 const ParmVarDecl *Param = getParamDecl(0);
934
935 ASTContext &Context = getASTContext();
936 CanQualType ParamType = Context.getCanonicalType(Param->getType());
937
938 // Strip off the lvalue reference, if any.
939 if (CanQual<LValueReferenceType> ParamRefType
940 = ParamType->getAs<LValueReferenceType>())
941 ParamType = ParamRefType->getPointeeType();
942
943
944 // Is it the same as our our class type?
945 CanQualType ClassTy
946 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
947 if (ParamType.getUnqualifiedType() != ClassTy)
948 return false;
949
950 return true;
951}
952
Douglas Gregor42a552f2008-11-05 20:51:48 +0000953CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000954CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000955 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000956 QualType(), false, false);
957}
958
959CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +0000960CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000961 const DeclarationNameInfo &NameInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000962 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000963 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000964 assert(NameInfo.getName().getNameKind()
965 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000966 "Name must refer to a destructor");
Abramo Bagnara25777432010-08-11 22:01:17 +0000967 return new (C) CXXDestructorDecl(RD, NameInfo, T, isInline,
968 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000969}
970
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000971CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000972CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000973 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000974 QualType(), 0, false, false);
975}
976
977CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000978CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000979 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000980 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000981 bool isInline, bool isExplicit) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000982 assert(NameInfo.getName().getNameKind()
983 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000984 "Name must refer to a conversion function");
Abramo Bagnara25777432010-08-11 22:01:17 +0000985 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
986 isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000987}
988
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000989LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000990 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000991 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000992 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000993 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000994}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000995
996UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
997 SourceLocation L,
998 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000999 SourceRange QualifierRange,
1000 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001001 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001002 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001003 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001004 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1005 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001006 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001007 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001008}
1009
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001010NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1011 if (NamespaceAliasDecl *NA =
1012 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1013 return NA->getNamespace();
1014 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1015}
1016
Mike Stump1eb44332009-09-09 15:08:12 +00001017NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001018 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001019 SourceLocation AliasLoc,
1020 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001021 SourceRange QualifierRange,
1022 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001023 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001024 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001025 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1026 Namespace = NS->getOriginalNamespace();
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001027 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001028 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001029}
1030
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001031UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001032 SourceRange NNR, SourceLocation UL,
1033 NestedNameSpecifier* TargetNNS,
1034 const DeclarationNameInfo &NameInfo,
1035 bool IsTypeNameArg) {
1036 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001037}
1038
John McCall7ba107a2009-11-18 02:36:19 +00001039UnresolvedUsingValueDecl *
1040UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1041 SourceLocation UsingLoc,
1042 SourceRange TargetNNR,
1043 NestedNameSpecifier *TargetNNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001044 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001045 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001046 TargetNNR, TargetNNS, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001047}
1048
1049UnresolvedUsingTypenameDecl *
1050UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1051 SourceLocation UsingLoc,
1052 SourceLocation TypenameLoc,
1053 SourceRange TargetNNR,
1054 NestedNameSpecifier *TargetNNS,
1055 SourceLocation TargetNameLoc,
1056 DeclarationName TargetName) {
1057 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1058 TargetNNR, TargetNNS,
1059 TargetNameLoc,
1060 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001061}
1062
Anders Carlssonfb311762009-03-14 00:25:26 +00001063StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1064 SourceLocation L, Expr *AssertExpr,
1065 StringLiteral *Message) {
1066 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1067}
1068
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001069static const char *getAccessName(AccessSpecifier AS) {
1070 switch (AS) {
1071 default:
1072 case AS_none:
1073 assert("Invalid access specifier!");
1074 return 0;
1075 case AS_public:
1076 return "public";
1077 case AS_private:
1078 return "private";
1079 case AS_protected:
1080 return "protected";
1081 }
1082}
1083
1084const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1085 AccessSpecifier AS) {
1086 return DB << getAccessName(AS);
1087}