blob: 19c3ac10119eb11e42f99da34785ea7fe6161d51 [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
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
Douglas Gregor18274032010-07-03 00:47:00 +0000280 // Note that we have no need of an implicitly-declared default constructor.
281 data().DeclaredDefaultConstructor = true;
282
Mike Stump1eb44332009-09-09 15:08:12 +0000283 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000284 // An aggregate is an array or a class (clause 9) with no
285 // user-declared constructors (12.1) [...].
John McCall86ff3082010-02-04 22:26:26 +0000286 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000287
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000288 // C++ [class]p4:
289 // A POD-struct is an aggregate class [...]
John McCall86ff3082010-02-04 22:26:26 +0000290 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000291
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000292 // C++ [class.ctor]p5:
293 // A constructor is trivial if it is an implicitly-declared default
294 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000295 // FIXME: C++0x: don't do this for "= default" default constructors.
John McCall86ff3082010-02-04 22:26:26 +0000296 data().HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000298 // Note when we have a user-declared copy constructor, which will
299 // suppress the implicit declaration of a copy constructor.
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000300 if (ConDecl->isCopyConstructor()) {
John McCall86ff3082010-02-04 22:26:26 +0000301 data().UserDeclaredCopyConstructor = true;
Douglas Gregor22584312010-07-02 23:41:54 +0000302 data().DeclaredCopyConstructor = true;
303
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000304 // C++ [class.copy]p6:
305 // A copy constructor is trivial if it is implicitly declared.
306 // FIXME: C++0x: don't do this for "= default" copy constructors.
John McCall86ff3082010-02-04 22:26:26 +0000307 data().HasTrivialCopyConstructor = false;
Douglas Gregor22584312010-07-02 23:41:54 +0000308
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000309 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000310}
311
Sebastian Redl64b45f72009-01-05 20:52:13 +0000312void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
313 CXXMethodDecl *OpDecl) {
314 // We're interested specifically in copy assignment operators.
John McCall183700f2009-09-21 23:43:11 +0000315 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000316 assert(FnType && "Overloaded operator has no proto function type.");
317 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregor77da3f42009-10-13 23:45:19 +0000318
319 // Copy assignment operators must be non-templates.
320 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
321 return;
322
Sebastian Redl64b45f72009-01-05 20:52:13 +0000323 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000324 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000325 ArgType = Ref->getPointeeType();
326
327 ArgType = ArgType.getUnqualifiedType();
328 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
329 const_cast<CXXRecordDecl*>(this)));
330
Douglas Gregora4923eb2009-11-16 21:35:15 +0000331 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000332 return;
333
334 // This is a copy assignment operator.
Eli Friedman88fad632009-11-07 00:02:45 +0000335 // Note on the decl that it is a copy assignment operator.
336 OpDecl->setCopyAssignment(true);
337
Sebastian Redl64b45f72009-01-05 20:52:13 +0000338 // Suppress the implicit declaration of a copy constructor.
John McCall86ff3082010-02-04 22:26:26 +0000339 data().UserDeclaredCopyAssignment = true;
Douglas Gregora376d102010-07-02 21:50:04 +0000340 data().DeclaredCopyAssignment = true;
341
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000342 // C++ [class.copy]p11:
343 // A copy assignment operator is trivial if it is implicitly declared.
344 // FIXME: C++0x: don't do this for "= default" copy operators.
John McCall86ff3082010-02-04 22:26:26 +0000345 data().HasTrivialCopyAssignment = false;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000346
Sebastian Redl64b45f72009-01-05 20:52:13 +0000347 // C++ [class]p4:
348 // A POD-struct is an aggregate class that [...] has no user-defined copy
349 // assignment operator [...].
John McCall86ff3082010-02-04 22:26:26 +0000350 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000351}
352
John McCallb05b5f32010-03-15 09:07:48 +0000353static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
354 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000355 if (isa<UsingShadowDecl>(Conv))
356 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000357 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
358 T = ConvTemp->getTemplatedDecl()->getResultType();
359 else
360 T = cast<CXXConversionDecl>(Conv)->getConversionType();
361 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000362}
363
John McCallb05b5f32010-03-15 09:07:48 +0000364/// Collect the visible conversions of a base class.
365///
366/// \param Base a base class of the class we're considering
367/// \param InVirtual whether this base class is a virtual base (or a base
368/// of a virtual base)
369/// \param Access the access along the inheritance path to this base
370/// \param ParentHiddenTypes the conversions provided by the inheritors
371/// of this base
372/// \param Output the set to which to add conversions from non-virtual bases
373/// \param VOutput the set to which to add conversions from virtual bases
374/// \param HiddenVBaseCs the set of conversions which were hidden in a
375/// virtual base along some inheritance path
376static void CollectVisibleConversions(ASTContext &Context,
377 CXXRecordDecl *Record,
378 bool InVirtual,
379 AccessSpecifier Access,
380 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
381 UnresolvedSetImpl &Output,
382 UnresolvedSetImpl &VOutput,
383 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
384 // The set of types which have conversions in this class or its
385 // subclasses. As an optimization, we don't copy the derived set
386 // unless it might change.
387 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
388 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
389
390 // Collect the direct conversions and figure out which conversions
391 // will be hidden in the subclasses.
392 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
393 if (!Cs.empty()) {
394 HiddenTypesBuffer = ParentHiddenTypes;
395 HiddenTypes = &HiddenTypesBuffer;
396
397 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
398 bool Hidden =
399 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
400
401 // If this conversion is hidden and we're in a virtual base,
402 // remember that it's hidden along some inheritance path.
403 if (Hidden && InVirtual)
404 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
405
406 // If this conversion isn't hidden, add it to the appropriate output.
407 else if (!Hidden) {
408 AccessSpecifier IAccess
409 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
410
411 if (InVirtual)
412 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000413 else
John McCallb05b5f32010-03-15 09:07:48 +0000414 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000415 }
416 }
417 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000418
John McCallb05b5f32010-03-15 09:07:48 +0000419 // Collect information recursively from any base classes.
420 for (CXXRecordDecl::base_class_iterator
421 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
422 const RecordType *RT = I->getType()->getAs<RecordType>();
423 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000424
John McCallb05b5f32010-03-15 09:07:48 +0000425 AccessSpecifier BaseAccess
426 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
427 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000428
John McCallb05b5f32010-03-15 09:07:48 +0000429 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
430 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
431 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000432 }
John McCallb05b5f32010-03-15 09:07:48 +0000433}
Sebastian Redl9994a342009-10-25 17:03:50 +0000434
John McCallb05b5f32010-03-15 09:07:48 +0000435/// Collect the visible conversions of a class.
436///
437/// This would be extremely straightforward if it weren't for virtual
438/// bases. It might be worth special-casing that, really.
439static void CollectVisibleConversions(ASTContext &Context,
440 CXXRecordDecl *Record,
441 UnresolvedSetImpl &Output) {
442 // The collection of all conversions in virtual bases that we've
443 // found. These will be added to the output as long as they don't
444 // appear in the hidden-conversions set.
445 UnresolvedSet<8> VBaseCs;
446
447 // The set of conversions in virtual bases that we've determined to
448 // be hidden.
449 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
450
451 // The set of types hidden by classes derived from this one.
452 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
453
454 // Go ahead and collect the direct conversions and add them to the
455 // hidden-types set.
456 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
457 Output.append(Cs.begin(), Cs.end());
458 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
459 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
460
461 // Recursively collect conversions from base classes.
462 for (CXXRecordDecl::base_class_iterator
463 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
464 const RecordType *RT = I->getType()->getAs<RecordType>();
465 if (!RT) continue;
466
467 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
468 I->isVirtual(), I->getAccessSpecifier(),
469 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
470 }
471
472 // Add any unhidden conversions provided by virtual bases.
473 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
474 I != E; ++I) {
475 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
476 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000477 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000478}
479
480/// getVisibleConversionFunctions - get all conversion functions visible
481/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000482const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000483 // If root class, all conversions are visible.
484 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000485 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000486 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000487 if (data().ComputedVisibleConversions)
488 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000489 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000490 data().ComputedVisibleConversions = true;
491 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000492}
493
John McCall32daa422010-03-31 01:36:47 +0000494#ifndef NDEBUG
495void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000496 assert(ConvDecl->getDeclContext() == this &&
497 "conversion function does not belong to this record");
498
John McCall32daa422010-03-31 01:36:47 +0000499 ConvDecl = ConvDecl->getUnderlyingDecl();
500 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
501 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
502 } else {
503 assert(isa<CXXConversionDecl>(ConvDecl));
504 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000505}
John McCall32daa422010-03-31 01:36:47 +0000506#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000507
John McCall32daa422010-03-31 01:36:47 +0000508void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
509 // This operation is O(N) but extremely rare. Sema only uses it to
510 // remove UsingShadowDecls in a class that were followed by a direct
511 // declaration, e.g.:
512 // class A : B {
513 // using B::operator int;
514 // operator int();
515 // };
516 // This is uncommon by itself and even more uncommon in conjunction
517 // with sufficiently large numbers of directly-declared conversions
518 // that asymptotic behavior matters.
519
520 UnresolvedSetImpl &Convs = *getConversionFunctions();
521 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
522 if (Convs[I].getDecl() == ConvDecl) {
523 Convs.erase(I);
524 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
525 && "conversion was found multiple times in unresolved set");
526 return;
527 }
528 }
529
530 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000531}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000532
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000533void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
534 Method->setVirtualAsWritten(true);
535 setAggregate(false);
536 setPOD(false);
537 setEmpty(false);
538 setPolymorphic(true);
539 setHasTrivialConstructor(false);
540 setHasTrivialCopyConstructor(false);
541 setHasTrivialCopyAssignment(false);
542}
543
Douglas Gregorf6b11852009-10-08 15:14:33 +0000544CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000545 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000546 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
547
548 return 0;
549}
550
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000551MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
552 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
553}
554
Douglas Gregorf6b11852009-10-08 15:14:33 +0000555void
556CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
557 TemplateSpecializationKind TSK) {
558 assert(TemplateOrInstantiation.isNull() &&
559 "Previous template or instantiation?");
560 assert(!isa<ClassTemplateSpecializationDecl>(this));
561 TemplateOrInstantiation
562 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
563}
564
Anders Carlssonb13e3572009-12-07 06:33:48 +0000565TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
566 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000567 = dyn_cast<ClassTemplateSpecializationDecl>(this))
568 return Spec->getSpecializationKind();
569
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000570 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000571 return MSInfo->getTemplateSpecializationKind();
572
573 return TSK_Undeclared;
574}
575
576void
577CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
578 if (ClassTemplateSpecializationDecl *Spec
579 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
580 Spec->setSpecializationKind(TSK);
581 return;
582 }
583
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000584 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000585 MSInfo->setTemplateSpecializationKind(TSK);
586 return;
587 }
588
589 assert(false && "Not a class template or member class specialization");
590}
591
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000592CXXConstructorDecl *
Douglas Gregoreb8c6702010-07-01 22:31:05 +0000593CXXRecordDecl::getDefaultConstructor() {
594 ASTContext &Context = getASTContext();
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000595 QualType ClassType = Context.getTypeDeclType(this);
596 DeclarationName ConstructorName
597 = Context.DeclarationNames.getCXXConstructorName(
598 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000600 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000601 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000602 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000603 // FIXME: In C++0x, a constructor template can be a default constructor.
604 if (isa<FunctionTemplateDecl>(*Con))
605 continue;
606
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000607 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
608 if (Constructor->isDefaultConstructor())
609 return Constructor;
610 }
611 return 0;
612}
613
Douglas Gregor1d110e02010-07-01 14:13:13 +0000614CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
615 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000616 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000617
618 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000619 = Context.DeclarationNames.getCXXDestructorName(
620 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000621
John McCallc0bf4622010-02-23 00:48:20 +0000622 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000623 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000624 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000626 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000627 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Anders Carlsson7267c162009-05-29 21:03:38 +0000629 return Dtor;
630}
631
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000632CXXMethodDecl *
633CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000634 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000635 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000636 bool isStatic, StorageClass SCAsWritten, bool isInline) {
John McCalla93c9342009-12-07 02:54:59 +0000637 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000638 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000639}
640
Douglas Gregor90916562009-09-29 18:16:17 +0000641bool CXXMethodDecl::isUsualDeallocationFunction() const {
642 if (getOverloadedOperator() != OO_Delete &&
643 getOverloadedOperator() != OO_Array_Delete)
644 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000645
646 // C++ [basic.stc.dynamic.deallocation]p2:
647 // A template instance is never a usual deallocation function,
648 // regardless of its signature.
649 if (getPrimaryTemplate())
650 return false;
651
Douglas Gregor90916562009-09-29 18:16:17 +0000652 // C++ [basic.stc.dynamic.deallocation]p2:
653 // If a class T has a member deallocation function named operator delete
654 // with exactly one parameter, then that function is a usual (non-placement)
655 // deallocation function. [...]
656 if (getNumParams() == 1)
657 return true;
658
659 // C++ [basic.stc.dynamic.deallocation]p2:
660 // [...] If class T does not declare such an operator delete but does
661 // declare a member deallocation function named operator delete with
662 // exactly two parameters, the second of which has type std::size_t (18.1),
663 // then this function is a usual deallocation function.
664 ASTContext &Context = getASTContext();
665 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000666 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
667 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000668 return false;
669
670 // This function is a usual deallocation function if there are no
671 // single-parameter deallocation functions of the same kind.
672 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
673 R.first != R.second; ++R.first) {
674 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
675 if (FD->getNumParams() == 1)
676 return false;
677 }
678
679 return true;
680}
681
Douglas Gregor06a9f362010-05-01 20:49:11 +0000682bool CXXMethodDecl::isCopyAssignmentOperator() const {
683 // C++0x [class.copy]p19:
684 // A user-declared copy assignment operator X::operator= is a non-static
685 // non-template member function of class X with exactly one parameter of
686 // type X, X&, const X&, volatile X& or const volatile X&.
687 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
688 /*non-static*/ isStatic() ||
689 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
690 /*exactly one parameter*/getNumParams() != 1)
691 return false;
692
693 QualType ParamType = getParamDecl(0)->getType();
694 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
695 ParamType = Ref->getPointeeType();
696
697 ASTContext &Context = getASTContext();
698 QualType ClassType
699 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
700 return Context.hasSameUnqualifiedType(ClassType, ParamType);
701}
702
Anders Carlsson05eb2442009-05-16 23:58:37 +0000703void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000704 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000705 assert(!MD->getParent()->isDependentContext() &&
706 "Can't add an overridden method to a class template!");
707
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000708 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000709}
710
711CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000712 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000713}
714
715CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000716 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000717}
718
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000719unsigned CXXMethodDecl::size_overridden_methods() const {
720 return getASTContext().overridden_methods_size(this);
721}
722
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000723QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000724 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
725 // If the member function is declared const, the type of this is const X*,
726 // if the member function is declared volatile, the type of this is
727 // volatile X*, and if the member function is declared const volatile,
728 // the type of this is const volatile X*.
729
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000730 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000731
John McCall3cb0ebd2010-03-10 03:28:59 +0000732 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000733 ClassTy = C.getQualifiedType(ClassTy,
734 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000735 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000736}
737
Eli Friedmand7d7f672009-12-06 20:50:05 +0000738bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000739 // If this function is a template instantiation, look at the template from
740 // which it was instantiated.
741 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
742 if (!CheckFn)
743 CheckFn = this;
744
Eli Friedmand7d7f672009-12-06 20:50:05 +0000745 const FunctionDecl *fn;
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000746 return CheckFn->getBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000747}
748
Douglas Gregor7ad83902008-11-05 04:29:56 +0000749CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000750CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +0000751 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000752 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000753 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
754 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
755 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000756{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000757}
758
759CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000760CXXBaseOrMemberInitializer(ASTContext &Context,
761 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000762 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000763 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
764 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
765 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000766{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000767}
768
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000769CXXBaseOrMemberInitializer::
770CXXBaseOrMemberInitializer(ASTContext &Context,
771 FieldDecl *Member, SourceLocation MemberLoc,
772 SourceLocation L, Expr *Init, SourceLocation R,
773 VarDecl **Indices,
774 unsigned NumIndices)
775 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000776 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
777 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000778{
779 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
780 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
781}
782
783CXXBaseOrMemberInitializer *
784CXXBaseOrMemberInitializer::Create(ASTContext &Context,
785 FieldDecl *Member,
786 SourceLocation MemberLoc,
787 SourceLocation L,
788 Expr *Init,
789 SourceLocation R,
790 VarDecl **Indices,
791 unsigned NumIndices) {
792 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
793 sizeof(VarDecl *) * NumIndices,
794 llvm::alignof<CXXBaseOrMemberInitializer>());
795 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
796 L, Init, R, Indices, NumIndices);
797}
798
Douglas Gregor802ab452009-12-02 22:36:29 +0000799void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000800 if (Init)
801 Init->Destroy(Context);
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000802 // FIXME: Destroy indices
Douglas Gregor802ab452009-12-02 22:36:29 +0000803 this->~CXXBaseOrMemberInitializer();
804}
805
806TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
807 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000808 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000809 else
810 return TypeLoc();
811}
812
813Type *CXXBaseOrMemberInitializer::getBaseClass() {
814 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000815 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000816 else
817 return 0;
818}
819
820const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
821 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000822 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000823 else
824 return 0;
825}
826
827SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
828 if (isMemberInitializer())
829 return getMemberLocation();
830
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000831 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +0000832}
833
834SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
835 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000836}
837
Douglas Gregorb48fe382008-10-31 09:07:45 +0000838CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000839CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
840 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationName(),
841 QualType(), 0, false, false, false);
842}
843
844CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +0000845CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000846 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000847 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000848 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000849 bool isInline,
850 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000851 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
852 "Name must refer to a constructor");
Douglas Gregor16573fa2010-04-19 22:54:31 +0000853 return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit,
854 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000855}
856
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000857bool CXXConstructorDecl::isDefaultConstructor() const {
858 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000859 // A default constructor for a class X is a constructor of class
860 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000861 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000862 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000863}
864
Mike Stump1eb44332009-09-09 15:08:12 +0000865bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000866CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000867 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000868 // A non-template constructor for class X is a copy constructor
869 // if its first parameter is of type X&, const X&, volatile X& or
870 // const volatile X&, and either there are no other parameters
871 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000872 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000873 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000874 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000875 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000876 return false;
877
878 const ParmVarDecl *Param = getParamDecl(0);
879
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000880 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000881 const LValueReferenceType *ParamRefType =
882 Param->getType()->getAs<LValueReferenceType>();
883 if (!ParamRefType)
884 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000885
Douglas Gregorfd476482009-11-13 23:59:09 +0000886 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000887 ASTContext &Context = getASTContext();
888
Douglas Gregorfd476482009-11-13 23:59:09 +0000889 CanQualType PointeeType
890 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000891 CanQualType ClassTy
892 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000893 if (PointeeType.getUnqualifiedType() != ClassTy)
894 return false;
895
John McCall0953e762009-09-24 19:53:00 +0000896 // FIXME: other qualifiers?
897
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000898 // We have a copy constructor.
899 TypeQuals = PointeeType.getCVRQualifiers();
900 return true;
901}
902
Anders Carlssonfaccd722009-08-28 16:57:08 +0000903bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000904 // C++ [class.conv.ctor]p1:
905 // A constructor declared without the function-specifier explicit
906 // that can be called with a single parameter specifies a
907 // conversion from the type of its first parameter to the type of
908 // its class. Such a constructor is called a converting
909 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000910 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000911 return false;
912
Mike Stump1eb44332009-09-09 15:08:12 +0000913 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000914 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000915 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000916 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000917}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000918
Douglas Gregor66724ea2009-11-14 01:20:54 +0000919bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
920 if ((getNumParams() < 1) ||
921 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
922 (getPrimaryTemplate() == 0) ||
923 (getDescribedFunctionTemplate() != 0))
924 return false;
925
926 const ParmVarDecl *Param = getParamDecl(0);
927
928 ASTContext &Context = getASTContext();
929 CanQualType ParamType = Context.getCanonicalType(Param->getType());
930
931 // Strip off the lvalue reference, if any.
932 if (CanQual<LValueReferenceType> ParamRefType
933 = ParamType->getAs<LValueReferenceType>())
934 ParamType = ParamRefType->getPointeeType();
935
936
937 // Is it the same as our our class type?
938 CanQualType ClassTy
939 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
940 if (ParamType.getUnqualifiedType() != ClassTy)
941 return false;
942
943 return true;
944}
945
Douglas Gregor42a552f2008-11-05 20:51:48 +0000946CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000947CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
948 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationName(),
949 QualType(), false, false);
950}
951
952CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +0000953CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000954 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000955 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000956 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000957 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
958 "Name must refer to a destructor");
Douglas Gregor16573fa2010-04-19 22:54:31 +0000959 return new (C) CXXDestructorDecl(RD, L, N, T, isInline, isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000960}
961
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000962void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000963CXXConstructorDecl::Destroy(ASTContext& C) {
964 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000965 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000966}
967
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000968CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000969CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
970 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationName(),
971 QualType(), 0, false, false);
972}
973
974CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000975CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000976 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000977 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000978 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000979 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
980 "Name must refer to a conversion function");
John McCalla93c9342009-12-07 02:54:59 +0000981 return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000982}
983
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000984LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000985 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000986 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000987 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000988 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000989}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000990
991UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
992 SourceLocation L,
993 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000994 SourceRange QualifierRange,
995 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000996 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000997 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000998 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000999 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1000 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001001 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001002 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001003}
1004
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001005NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1006 if (NamespaceAliasDecl *NA =
1007 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1008 return NA->getNamespace();
1009 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1010}
1011
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001012void UsingDirectiveDecl::setNominatedNamespace(NamedDecl* ND) {
1013 assert((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) &&
1014 "expected a NamespaceDecl or NamespaceAliasDecl");
1015 NominatedNamespace = ND;
1016}
1017
Mike Stump1eb44332009-09-09 15:08:12 +00001018NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
1019 SourceLocation L,
1020 SourceLocation AliasLoc,
1021 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001022 SourceRange QualifierRange,
1023 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001024 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001025 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001026 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1027 Namespace = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001028 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001029 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001030}
1031
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001032UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
John McCall9488ea12009-11-17 05:59:44 +00001033 SourceLocation L, SourceRange NNR, SourceLocation UL,
1034 NestedNameSpecifier* TargetNNS, DeclarationName Name,
1035 bool IsTypeNameArg) {
1036 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, 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,
1044 SourceLocation TargetNameLoc,
1045 DeclarationName TargetName) {
1046 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
1047 TargetNNR, TargetNNS,
1048 TargetNameLoc, TargetName);
1049}
1050
1051UnresolvedUsingTypenameDecl *
1052UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1053 SourceLocation UsingLoc,
1054 SourceLocation TypenameLoc,
1055 SourceRange TargetNNR,
1056 NestedNameSpecifier *TargetNNS,
1057 SourceLocation TargetNameLoc,
1058 DeclarationName TargetName) {
1059 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1060 TargetNNR, TargetNNS,
1061 TargetNameLoc,
1062 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001063}
1064
Anders Carlssonfb311762009-03-14 00:25:26 +00001065StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1066 SourceLocation L, Expr *AssertExpr,
1067 StringLiteral *Message) {
1068 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1069}
1070
1071void StaticAssertDecl::Destroy(ASTContext& C) {
1072 AssertExpr->Destroy(C);
1073 Message->Destroy(C);
John McCallb6217662010-03-15 10:12:16 +00001074 Decl::Destroy(C);
Anders Carlssonfb311762009-03-14 00:25:26 +00001075}
1076
1077StaticAssertDecl::~StaticAssertDecl() {
1078}
1079
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001080static const char *getAccessName(AccessSpecifier AS) {
1081 switch (AS) {
1082 default:
1083 case AS_none:
1084 assert("Invalid access specifier!");
1085 return 0;
1086 case AS_public:
1087 return "public";
1088 case AS_private:
1089 return "private";
1090 case AS_protected:
1091 return "protected";
1092 }
1093}
1094
1095const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1096 AccessSpecifier AS) {
1097 return DB << getAccessName(AS);
1098}
1099
1100