blob: 4b1909e57c66fd7e0757712a0d706aa27b5f4a96 [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 Gregor22584312010-07-02 23:41:54 +000035 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
Douglas Gregorf8268ae2008-10-22 17:49:05 +000068CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000069}
70
71void CXXRecordDecl::Destroy(ASTContext &C) {
John McCall86ff3082010-02-04 22:26:26 +000072 if (data().Definition == this) {
73 C.Deallocate(data().Bases);
74 C.Deallocate(data().VBases);
75 C.Deallocate(&data());
76 }
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000077 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000078}
79
Mike Stump1eb44332009-09-09 15:08:12 +000080void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000081CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000082 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000083 ASTContext &C = getASTContext();
84
Mike Stump1eb44332009-09-09 15:08:12 +000085 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000086 // An aggregate is an array or a class (clause 9) with [...]
87 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000088 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000089
John McCall86ff3082010-02-04 22:26:26 +000090 if (data().Bases)
91 C.Deallocate(data().Bases);
Mike Stump1eb44332009-09-09 15:08:12 +000092
Anders Carlsson6f6de732010-03-29 05:13:12 +000093 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000094 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000095
96 // The virtual bases of this class.
97 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000098
John McCall86ff3082010-02-04 22:26:26 +000099 data().Bases = new(C) CXXBaseSpecifier [NumBases];
100 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000101 for (unsigned i = 0; i < NumBases; ++i) {
John McCall86ff3082010-02-04 22:26:26 +0000102 data().Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000103 // Keep track of inherited vbases for this base class.
104 const CXXBaseSpecifier *Base = Bases[i];
105 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000106 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000107 if (BaseType->isDependentType())
108 continue;
109 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000110 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000111
112 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000113 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000114 BaseClassDecl->vbases_begin(),
115 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000116 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000117 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000118 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000119 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000120
121 if (Base->isVirtual()) {
122 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000123 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000124 VBases.push_back(Base);
125 }
126
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000127 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000128
129 if (VBases.empty())
130 return;
131
132 // Create base specifier for any direct or indirect virtual bases.
133 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
134 data().NumVBases = VBases.size();
135 for (int I = 0, E = VBases.size(); I != E; ++I) {
136 QualType VBaseType = VBases[I]->getType();
137
138 // Skip dependent types; we can't do any checking on them now.
139 if (VBaseType->isDependentType())
140 continue;
141
142 CXXRecordDecl *VBaseClassDecl
143 = cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
144
145 data().VBases[I] =
146 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000147 VBaseClassDecl->getTagKind() == TTK_Class,
Anders Carlsson6f6de732010-03-29 05:13:12 +0000148 VBases[I]->getAccessSpecifier(), VBaseType);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000149 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000150}
151
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000152/// Callback function for CXXRecordDecl::forallBases that acknowledges
153/// that it saw a base class.
154static bool SawBase(const CXXRecordDecl *, void *) {
155 return true;
156}
157
158bool CXXRecordDecl::hasAnyDependentBases() const {
159 if (!isDependentContext())
160 return false;
161
162 return !forallBases(SawBase, 0);
163}
164
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000165bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000166 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000167}
168
Douglas Gregor0d405db2010-07-01 20:59:04 +0000169/// \brief Perform a simplistic form of overload resolution that only considers
170/// cv-qualifiers on a single parameter, and return the best overload candidate
171/// (if there is one).
172static CXXMethodDecl *
173GetBestOverloadCandidateSimple(
174 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
175 if (Cands.empty())
176 return 0;
177 if (Cands.size() == 1)
178 return Cands[0].first;
179
180 unsigned Best = 0, N = Cands.size();
181 for (unsigned I = 1; I != N; ++I)
182 if (Cands[Best].second.isSupersetOf(Cands[I].second))
183 Best = I;
184
185 for (unsigned I = 1; I != N; ++I)
186 if (Cands[Best].second.isSupersetOf(Cands[I].second))
187 return 0;
188
189 return Cands[Best].first;
190}
191
Mike Stump1eb44332009-09-09 15:08:12 +0000192CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000193 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000194 QualType ClassType
195 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000196 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000197 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000198 Context.getCanonicalType(ClassType));
199 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000200 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000201 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000202 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000203 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000204 // C++ [class.copy]p2:
205 // A non-template constructor for class X is a copy constructor if [...]
206 if (isa<FunctionTemplateDecl>(*Con))
207 continue;
208
Douglas Gregor0d405db2010-07-01 20:59:04 +0000209 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
210 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000211 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
212 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000213 Found.push_back(std::make_pair(
214 const_cast<CXXConstructorDecl *>(Constructor),
215 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000216 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000217 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000218
219 return cast_or_null<CXXConstructorDecl>(
220 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000221}
222
Douglas Gregorb87786f2010-07-01 17:48:08 +0000223CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
224 ASTContext &Context = getASTContext();
225 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
226 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
227
228 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
229 DeclContext::lookup_const_iterator Op, OpEnd;
230 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
231 // C++ [class.copy]p9:
232 // A user-declared copy assignment operator is a non-static non-template
233 // member function of class X with exactly one parameter of type X, X&,
234 // const X&, volatile X& or const volatile X&.
235 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
236 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
237 continue;
238
239 const FunctionProtoType *FnType
240 = Method->getType()->getAs<FunctionProtoType>();
241 assert(FnType && "Overloaded operator has no prototype.");
242 // Don't assert on this; an invalid decl might have been left in the AST.
243 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
244 continue;
245
246 QualType ArgType = FnType->getArgType(0);
247 Qualifiers Quals;
248 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
249 ArgType = Ref->getPointeeType();
250 // If we have a const argument and we have a reference to a non-const,
251 // this function does not match.
252 if (ArgIsConst && !ArgType.isConstQualified())
253 continue;
254
255 Quals = ArgType.getQualifiers();
256 } else {
257 // By-value copy-assignment operators are treated like const X&
258 // copy-assignment operators.
259 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
260 }
261
262 if (!Context.hasSameUnqualifiedType(ArgType, Class))
263 continue;
264
265 // Save this copy-assignment operator. It might be "the one".
266 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
267 }
268
269 // Use a simplistic form of overload resolution to find the candidate.
270 return GetBestOverloadCandidateSimple(Found);
271}
272
Sebastian Redl64b45f72009-01-05 20:52:13 +0000273void
Mike Stump1eb44332009-09-09 15:08:12 +0000274CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000275 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000276 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
277 // Note that we have a user-declared constructor.
John McCall86ff3082010-02-04 22:26:26 +0000278 data().UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000279
Mike Stump1eb44332009-09-09 15:08:12 +0000280 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000281 // An aggregate is an array or a class (clause 9) with no
282 // user-declared constructors (12.1) [...].
John McCall86ff3082010-02-04 22:26:26 +0000283 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000284
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000285 // C++ [class]p4:
286 // A POD-struct is an aggregate class [...]
John McCall86ff3082010-02-04 22:26:26 +0000287 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000288
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000289 // C++ [class.ctor]p5:
290 // A constructor is trivial if it is an implicitly-declared default
291 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000292 // FIXME: C++0x: don't do this for "= default" default constructors.
John McCall86ff3082010-02-04 22:26:26 +0000293 data().HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000295 // Note when we have a user-declared copy constructor, which will
296 // suppress the implicit declaration of a copy constructor.
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000297 if (ConDecl->isCopyConstructor()) {
John McCall86ff3082010-02-04 22:26:26 +0000298 data().UserDeclaredCopyConstructor = true;
Douglas Gregor22584312010-07-02 23:41:54 +0000299 data().DeclaredCopyConstructor = true;
300
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000301 // C++ [class.copy]p6:
302 // A copy constructor is trivial if it is implicitly declared.
303 // FIXME: C++0x: don't do this for "= default" copy constructors.
John McCall86ff3082010-02-04 22:26:26 +0000304 data().HasTrivialCopyConstructor = false;
Douglas Gregor22584312010-07-02 23:41:54 +0000305
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000306 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000307}
308
Sebastian Redl64b45f72009-01-05 20:52:13 +0000309void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
310 CXXMethodDecl *OpDecl) {
311 // We're interested specifically in copy assignment operators.
John McCall183700f2009-09-21 23:43:11 +0000312 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000313 assert(FnType && "Overloaded operator has no proto function type.");
314 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregor77da3f42009-10-13 23:45:19 +0000315
316 // Copy assignment operators must be non-templates.
317 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
318 return;
319
Sebastian Redl64b45f72009-01-05 20:52:13 +0000320 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000321 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000322 ArgType = Ref->getPointeeType();
323
324 ArgType = ArgType.getUnqualifiedType();
325 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
326 const_cast<CXXRecordDecl*>(this)));
327
Douglas Gregora4923eb2009-11-16 21:35:15 +0000328 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000329 return;
330
331 // This is a copy assignment operator.
Eli Friedman88fad632009-11-07 00:02:45 +0000332 // Note on the decl that it is a copy assignment operator.
333 OpDecl->setCopyAssignment(true);
334
Sebastian Redl64b45f72009-01-05 20:52:13 +0000335 // Suppress the implicit declaration of a copy constructor.
John McCall86ff3082010-02-04 22:26:26 +0000336 data().UserDeclaredCopyAssignment = true;
Douglas Gregora376d102010-07-02 21:50:04 +0000337 data().DeclaredCopyAssignment = true;
338
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000339 // C++ [class.copy]p11:
340 // A copy assignment operator is trivial if it is implicitly declared.
341 // FIXME: C++0x: don't do this for "= default" copy operators.
John McCall86ff3082010-02-04 22:26:26 +0000342 data().HasTrivialCopyAssignment = false;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000343
Sebastian Redl64b45f72009-01-05 20:52:13 +0000344 // C++ [class]p4:
345 // A POD-struct is an aggregate class that [...] has no user-defined copy
346 // assignment operator [...].
John McCall86ff3082010-02-04 22:26:26 +0000347 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000348}
349
John McCallb05b5f32010-03-15 09:07:48 +0000350static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
351 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000352 if (isa<UsingShadowDecl>(Conv))
353 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000354 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
355 T = ConvTemp->getTemplatedDecl()->getResultType();
356 else
357 T = cast<CXXConversionDecl>(Conv)->getConversionType();
358 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000359}
360
John McCallb05b5f32010-03-15 09:07:48 +0000361/// Collect the visible conversions of a base class.
362///
363/// \param Base a base class of the class we're considering
364/// \param InVirtual whether this base class is a virtual base (or a base
365/// of a virtual base)
366/// \param Access the access along the inheritance path to this base
367/// \param ParentHiddenTypes the conversions provided by the inheritors
368/// of this base
369/// \param Output the set to which to add conversions from non-virtual bases
370/// \param VOutput the set to which to add conversions from virtual bases
371/// \param HiddenVBaseCs the set of conversions which were hidden in a
372/// virtual base along some inheritance path
373static void CollectVisibleConversions(ASTContext &Context,
374 CXXRecordDecl *Record,
375 bool InVirtual,
376 AccessSpecifier Access,
377 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
378 UnresolvedSetImpl &Output,
379 UnresolvedSetImpl &VOutput,
380 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
381 // The set of types which have conversions in this class or its
382 // subclasses. As an optimization, we don't copy the derived set
383 // unless it might change.
384 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
385 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
386
387 // Collect the direct conversions and figure out which conversions
388 // will be hidden in the subclasses.
389 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
390 if (!Cs.empty()) {
391 HiddenTypesBuffer = ParentHiddenTypes;
392 HiddenTypes = &HiddenTypesBuffer;
393
394 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
395 bool Hidden =
396 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
397
398 // If this conversion is hidden and we're in a virtual base,
399 // remember that it's hidden along some inheritance path.
400 if (Hidden && InVirtual)
401 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
402
403 // If this conversion isn't hidden, add it to the appropriate output.
404 else if (!Hidden) {
405 AccessSpecifier IAccess
406 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
407
408 if (InVirtual)
409 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000410 else
John McCallb05b5f32010-03-15 09:07:48 +0000411 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000412 }
413 }
414 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000415
John McCallb05b5f32010-03-15 09:07:48 +0000416 // Collect information recursively from any base classes.
417 for (CXXRecordDecl::base_class_iterator
418 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
419 const RecordType *RT = I->getType()->getAs<RecordType>();
420 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000421
John McCallb05b5f32010-03-15 09:07:48 +0000422 AccessSpecifier BaseAccess
423 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
424 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000425
John McCallb05b5f32010-03-15 09:07:48 +0000426 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
427 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
428 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000429 }
John McCallb05b5f32010-03-15 09:07:48 +0000430}
Sebastian Redl9994a342009-10-25 17:03:50 +0000431
John McCallb05b5f32010-03-15 09:07:48 +0000432/// Collect the visible conversions of a class.
433///
434/// This would be extremely straightforward if it weren't for virtual
435/// bases. It might be worth special-casing that, really.
436static void CollectVisibleConversions(ASTContext &Context,
437 CXXRecordDecl *Record,
438 UnresolvedSetImpl &Output) {
439 // The collection of all conversions in virtual bases that we've
440 // found. These will be added to the output as long as they don't
441 // appear in the hidden-conversions set.
442 UnresolvedSet<8> VBaseCs;
443
444 // The set of conversions in virtual bases that we've determined to
445 // be hidden.
446 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
447
448 // The set of types hidden by classes derived from this one.
449 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
450
451 // Go ahead and collect the direct conversions and add them to the
452 // hidden-types set.
453 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
454 Output.append(Cs.begin(), Cs.end());
455 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
456 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
457
458 // Recursively collect conversions from base classes.
459 for (CXXRecordDecl::base_class_iterator
460 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
461 const RecordType *RT = I->getType()->getAs<RecordType>();
462 if (!RT) continue;
463
464 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
465 I->isVirtual(), I->getAccessSpecifier(),
466 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
467 }
468
469 // Add any unhidden conversions provided by virtual bases.
470 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
471 I != E; ++I) {
472 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
473 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000474 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000475}
476
477/// getVisibleConversionFunctions - get all conversion functions visible
478/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000479const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000480 // If root class, all conversions are visible.
481 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000482 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000483 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000484 if (data().ComputedVisibleConversions)
485 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000486 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000487 data().ComputedVisibleConversions = true;
488 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000489}
490
John McCall32daa422010-03-31 01:36:47 +0000491#ifndef NDEBUG
492void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000493 assert(ConvDecl->getDeclContext() == this &&
494 "conversion function does not belong to this record");
495
John McCall32daa422010-03-31 01:36:47 +0000496 ConvDecl = ConvDecl->getUnderlyingDecl();
497 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
498 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
499 } else {
500 assert(isa<CXXConversionDecl>(ConvDecl));
501 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000502}
John McCall32daa422010-03-31 01:36:47 +0000503#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000504
John McCall32daa422010-03-31 01:36:47 +0000505void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
506 // This operation is O(N) but extremely rare. Sema only uses it to
507 // remove UsingShadowDecls in a class that were followed by a direct
508 // declaration, e.g.:
509 // class A : B {
510 // using B::operator int;
511 // operator int();
512 // };
513 // This is uncommon by itself and even more uncommon in conjunction
514 // with sufficiently large numbers of directly-declared conversions
515 // that asymptotic behavior matters.
516
517 UnresolvedSetImpl &Convs = *getConversionFunctions();
518 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
519 if (Convs[I].getDecl() == ConvDecl) {
520 Convs.erase(I);
521 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
522 && "conversion was found multiple times in unresolved set");
523 return;
524 }
525 }
526
527 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000528}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000529
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000530void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
531 Method->setVirtualAsWritten(true);
532 setAggregate(false);
533 setPOD(false);
534 setEmpty(false);
535 setPolymorphic(true);
536 setHasTrivialConstructor(false);
537 setHasTrivialCopyConstructor(false);
538 setHasTrivialCopyAssignment(false);
539}
540
Douglas Gregorf6b11852009-10-08 15:14:33 +0000541CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000542 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000543 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
544
545 return 0;
546}
547
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000548MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
549 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
550}
551
Douglas Gregorf6b11852009-10-08 15:14:33 +0000552void
553CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
554 TemplateSpecializationKind TSK) {
555 assert(TemplateOrInstantiation.isNull() &&
556 "Previous template or instantiation?");
557 assert(!isa<ClassTemplateSpecializationDecl>(this));
558 TemplateOrInstantiation
559 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
560}
561
Anders Carlssonb13e3572009-12-07 06:33:48 +0000562TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
563 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000564 = dyn_cast<ClassTemplateSpecializationDecl>(this))
565 return Spec->getSpecializationKind();
566
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000567 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000568 return MSInfo->getTemplateSpecializationKind();
569
570 return TSK_Undeclared;
571}
572
573void
574CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
575 if (ClassTemplateSpecializationDecl *Spec
576 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
577 Spec->setSpecializationKind(TSK);
578 return;
579 }
580
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000581 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000582 MSInfo->setTemplateSpecializationKind(TSK);
583 return;
584 }
585
586 assert(false && "Not a class template or member class specialization");
587}
588
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000589CXXConstructorDecl *
Douglas Gregoreb8c6702010-07-01 22:31:05 +0000590CXXRecordDecl::getDefaultConstructor() {
591 ASTContext &Context = getASTContext();
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000592 QualType ClassType = Context.getTypeDeclType(this);
593 DeclarationName ConstructorName
594 = Context.DeclarationNames.getCXXConstructorName(
595 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000597 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000598 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000599 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000600 // FIXME: In C++0x, a constructor template can be a default constructor.
601 if (isa<FunctionTemplateDecl>(*Con))
602 continue;
603
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000604 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
605 if (Constructor->isDefaultConstructor())
606 return Constructor;
607 }
608 return 0;
609}
610
Douglas Gregor1d110e02010-07-01 14:13:13 +0000611CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
612 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000613 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000614
615 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000616 = Context.DeclarationNames.getCXXDestructorName(
617 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000618
John McCallc0bf4622010-02-23 00:48:20 +0000619 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000620 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000621 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000623 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000624 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Anders Carlsson7267c162009-05-29 21:03:38 +0000626 return Dtor;
627}
628
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000629CXXMethodDecl *
630CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000631 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000632 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000633 bool isStatic, StorageClass SCAsWritten, bool isInline) {
John McCalla93c9342009-12-07 02:54:59 +0000634 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000635 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000636}
637
Douglas Gregor90916562009-09-29 18:16:17 +0000638bool CXXMethodDecl::isUsualDeallocationFunction() const {
639 if (getOverloadedOperator() != OO_Delete &&
640 getOverloadedOperator() != OO_Array_Delete)
641 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000642
643 // C++ [basic.stc.dynamic.deallocation]p2:
644 // A template instance is never a usual deallocation function,
645 // regardless of its signature.
646 if (getPrimaryTemplate())
647 return false;
648
Douglas Gregor90916562009-09-29 18:16:17 +0000649 // C++ [basic.stc.dynamic.deallocation]p2:
650 // If a class T has a member deallocation function named operator delete
651 // with exactly one parameter, then that function is a usual (non-placement)
652 // deallocation function. [...]
653 if (getNumParams() == 1)
654 return true;
655
656 // C++ [basic.stc.dynamic.deallocation]p2:
657 // [...] If class T does not declare such an operator delete but does
658 // declare a member deallocation function named operator delete with
659 // exactly two parameters, the second of which has type std::size_t (18.1),
660 // then this function is a usual deallocation function.
661 ASTContext &Context = getASTContext();
662 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000663 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
664 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000665 return false;
666
667 // This function is a usual deallocation function if there are no
668 // single-parameter deallocation functions of the same kind.
669 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
670 R.first != R.second; ++R.first) {
671 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
672 if (FD->getNumParams() == 1)
673 return false;
674 }
675
676 return true;
677}
678
Douglas Gregor06a9f362010-05-01 20:49:11 +0000679bool CXXMethodDecl::isCopyAssignmentOperator() const {
680 // C++0x [class.copy]p19:
681 // A user-declared copy assignment operator X::operator= is a non-static
682 // non-template member function of class X with exactly one parameter of
683 // type X, X&, const X&, volatile X& or const volatile X&.
684 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
685 /*non-static*/ isStatic() ||
686 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
687 /*exactly one parameter*/getNumParams() != 1)
688 return false;
689
690 QualType ParamType = getParamDecl(0)->getType();
691 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
692 ParamType = Ref->getPointeeType();
693
694 ASTContext &Context = getASTContext();
695 QualType ClassType
696 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
697 return Context.hasSameUnqualifiedType(ClassType, ParamType);
698}
699
Anders Carlsson05eb2442009-05-16 23:58:37 +0000700void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000701 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000702 assert(!MD->getParent()->isDependentContext() &&
703 "Can't add an overridden method to a class template!");
704
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000705 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000706}
707
708CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000709 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000710}
711
712CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000713 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000714}
715
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000716QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000717 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
718 // If the member function is declared const, the type of this is const X*,
719 // if the member function is declared volatile, the type of this is
720 // volatile X*, and if the member function is declared const volatile,
721 // the type of this is const volatile X*.
722
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000723 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000724
John McCall3cb0ebd2010-03-10 03:28:59 +0000725 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000726 ClassTy = C.getQualifiedType(ClassTy,
727 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000728 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000729}
730
Eli Friedmand7d7f672009-12-06 20:50:05 +0000731bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000732 // If this function is a template instantiation, look at the template from
733 // which it was instantiated.
734 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
735 if (!CheckFn)
736 CheckFn = this;
737
Eli Friedmand7d7f672009-12-06 20:50:05 +0000738 const FunctionDecl *fn;
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000739 return CheckFn->getBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000740}
741
Douglas Gregor7ad83902008-11-05 04:29:56 +0000742CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000743CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +0000744 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000745 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000746 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
747 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
748 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000749{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000750}
751
752CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000753CXXBaseOrMemberInitializer(ASTContext &Context,
754 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000755 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000756 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
757 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
758 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000759{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000760}
761
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000762CXXBaseOrMemberInitializer::
763CXXBaseOrMemberInitializer(ASTContext &Context,
764 FieldDecl *Member, SourceLocation MemberLoc,
765 SourceLocation L, Expr *Init, SourceLocation R,
766 VarDecl **Indices,
767 unsigned NumIndices)
768 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000769 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
770 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000771{
772 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
773 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
774}
775
776CXXBaseOrMemberInitializer *
777CXXBaseOrMemberInitializer::Create(ASTContext &Context,
778 FieldDecl *Member,
779 SourceLocation MemberLoc,
780 SourceLocation L,
781 Expr *Init,
782 SourceLocation R,
783 VarDecl **Indices,
784 unsigned NumIndices) {
785 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
786 sizeof(VarDecl *) * NumIndices,
787 llvm::alignof<CXXBaseOrMemberInitializer>());
788 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
789 L, Init, R, Indices, NumIndices);
790}
791
Douglas Gregor802ab452009-12-02 22:36:29 +0000792void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000793 if (Init)
794 Init->Destroy(Context);
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000795 // FIXME: Destroy indices
Douglas Gregor802ab452009-12-02 22:36:29 +0000796 this->~CXXBaseOrMemberInitializer();
797}
798
799TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
800 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000801 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000802 else
803 return TypeLoc();
804}
805
806Type *CXXBaseOrMemberInitializer::getBaseClass() {
807 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000808 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000809 else
810 return 0;
811}
812
813const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
814 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000815 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000816 else
817 return 0;
818}
819
820SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
821 if (isMemberInitializer())
822 return getMemberLocation();
823
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000824 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +0000825}
826
827SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
828 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000829}
830
Douglas Gregorb48fe382008-10-31 09:07:45 +0000831CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000832CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
833 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationName(),
834 QualType(), 0, false, false, false);
835}
836
837CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +0000838CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000839 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000840 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000841 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000842 bool isInline,
843 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000844 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
845 "Name must refer to a constructor");
Douglas Gregor16573fa2010-04-19 22:54:31 +0000846 return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit,
847 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000848}
849
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000850bool CXXConstructorDecl::isDefaultConstructor() const {
851 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000852 // A default constructor for a class X is a constructor of class
853 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000854 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000855 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000856}
857
Mike Stump1eb44332009-09-09 15:08:12 +0000858bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000859CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000860 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000861 // A non-template constructor for class X is a copy constructor
862 // if its first parameter is of type X&, const X&, volatile X& or
863 // const volatile X&, and either there are no other parameters
864 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000865 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000866 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000867 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000868 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000869 return false;
870
871 const ParmVarDecl *Param = getParamDecl(0);
872
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000873 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000874 const LValueReferenceType *ParamRefType =
875 Param->getType()->getAs<LValueReferenceType>();
876 if (!ParamRefType)
877 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000878
Douglas Gregorfd476482009-11-13 23:59:09 +0000879 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000880 ASTContext &Context = getASTContext();
881
Douglas Gregorfd476482009-11-13 23:59:09 +0000882 CanQualType PointeeType
883 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000884 CanQualType ClassTy
885 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000886 if (PointeeType.getUnqualifiedType() != ClassTy)
887 return false;
888
John McCall0953e762009-09-24 19:53:00 +0000889 // FIXME: other qualifiers?
890
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000891 // We have a copy constructor.
892 TypeQuals = PointeeType.getCVRQualifiers();
893 return true;
894}
895
Anders Carlssonfaccd722009-08-28 16:57:08 +0000896bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000897 // C++ [class.conv.ctor]p1:
898 // A constructor declared without the function-specifier explicit
899 // that can be called with a single parameter specifies a
900 // conversion from the type of its first parameter to the type of
901 // its class. Such a constructor is called a converting
902 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000903 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000904 return false;
905
Mike Stump1eb44332009-09-09 15:08:12 +0000906 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000907 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000908 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000909 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000910}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000911
Douglas Gregor66724ea2009-11-14 01:20:54 +0000912bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
913 if ((getNumParams() < 1) ||
914 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
915 (getPrimaryTemplate() == 0) ||
916 (getDescribedFunctionTemplate() != 0))
917 return false;
918
919 const ParmVarDecl *Param = getParamDecl(0);
920
921 ASTContext &Context = getASTContext();
922 CanQualType ParamType = Context.getCanonicalType(Param->getType());
923
924 // Strip off the lvalue reference, if any.
925 if (CanQual<LValueReferenceType> ParamRefType
926 = ParamType->getAs<LValueReferenceType>())
927 ParamType = ParamRefType->getPointeeType();
928
929
930 // Is it the same as our our class type?
931 CanQualType ClassTy
932 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
933 if (ParamType.getUnqualifiedType() != ClassTy)
934 return false;
935
936 return true;
937}
938
Douglas Gregor42a552f2008-11-05 20:51:48 +0000939CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000940CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
941 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationName(),
942 QualType(), false, false);
943}
944
945CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +0000946CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000947 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000948 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000949 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000950 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
951 "Name must refer to a destructor");
Douglas Gregor16573fa2010-04-19 22:54:31 +0000952 return new (C) CXXDestructorDecl(RD, L, N, T, isInline, isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000953}
954
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000955void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000956CXXConstructorDecl::Destroy(ASTContext& C) {
957 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000958 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000959}
960
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000961CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000962CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
963 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationName(),
964 QualType(), 0, false, false);
965}
966
967CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000968CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000969 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000970 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000971 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000972 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
973 "Name must refer to a conversion function");
John McCalla93c9342009-12-07 02:54:59 +0000974 return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000975}
976
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000977LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000978 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000979 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000980 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000981 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000982}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000983
984UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
985 SourceLocation L,
986 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000987 SourceRange QualifierRange,
988 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000989 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000990 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000991 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000992 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
993 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000994 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000995 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000996}
997
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000998NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
999 if (NamespaceAliasDecl *NA =
1000 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1001 return NA->getNamespace();
1002 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1003}
1004
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001005void UsingDirectiveDecl::setNominatedNamespace(NamedDecl* ND) {
1006 assert((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) &&
1007 "expected a NamespaceDecl or NamespaceAliasDecl");
1008 NominatedNamespace = ND;
1009}
1010
Mike Stump1eb44332009-09-09 15:08:12 +00001011NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
1012 SourceLocation L,
1013 SourceLocation AliasLoc,
1014 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001015 SourceRange QualifierRange,
1016 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001017 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001018 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001019 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1020 Namespace = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001021 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001022 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001023}
1024
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001025UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
John McCall9488ea12009-11-17 05:59:44 +00001026 SourceLocation L, SourceRange NNR, SourceLocation UL,
1027 NestedNameSpecifier* TargetNNS, DeclarationName Name,
1028 bool IsTypeNameArg) {
1029 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001030}
1031
John McCall7ba107a2009-11-18 02:36:19 +00001032UnresolvedUsingValueDecl *
1033UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1034 SourceLocation UsingLoc,
1035 SourceRange TargetNNR,
1036 NestedNameSpecifier *TargetNNS,
1037 SourceLocation TargetNameLoc,
1038 DeclarationName TargetName) {
1039 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
1040 TargetNNR, TargetNNS,
1041 TargetNameLoc, TargetName);
1042}
1043
1044UnresolvedUsingTypenameDecl *
1045UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1046 SourceLocation UsingLoc,
1047 SourceLocation TypenameLoc,
1048 SourceRange TargetNNR,
1049 NestedNameSpecifier *TargetNNS,
1050 SourceLocation TargetNameLoc,
1051 DeclarationName TargetName) {
1052 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1053 TargetNNR, TargetNNS,
1054 TargetNameLoc,
1055 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001056}
1057
Anders Carlssonfb311762009-03-14 00:25:26 +00001058StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1059 SourceLocation L, Expr *AssertExpr,
1060 StringLiteral *Message) {
1061 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1062}
1063
1064void StaticAssertDecl::Destroy(ASTContext& C) {
1065 AssertExpr->Destroy(C);
1066 Message->Destroy(C);
John McCallb6217662010-03-15 10:12:16 +00001067 Decl::Destroy(C);
Anders Carlssonfb311762009-03-14 00:25:26 +00001068}
1069
1070StaticAssertDecl::~StaticAssertDecl() {
1071}
1072
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001073static const char *getAccessName(AccessSpecifier AS) {
1074 switch (AS) {
1075 default:
1076 case AS_none:
1077 assert("Invalid access specifier!");
1078 return 0;
1079 case AS_public:
1080 return "public";
1081 case AS_private:
1082 return "private";
1083 case AS_protected:
1084 return "protected";
1085 }
1086}
1087
1088const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1089 AccessSpecifier AS) {
1090 return DB << getAccessName(AS);
1091}
1092
1093