blob: f2f0694826c6ab82c564e9474b2f661f5c1c34f7 [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000017#include "clang/AST/Expr.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000018#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000019#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000020#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000021#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Decl Allocation/Deallocation Method Implementations
26//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000027
John McCall86ff3082010-02-04 22:26:26 +000028CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
29 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000030 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000031 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
32 Abstract(false), HasTrivialConstructor(true),
33 HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true),
Fariborz Jahanian62509212009-09-12 18:26:03 +000034 HasTrivialDestructor(true), ComputedVisibleConversions(false),
Douglas Gregor18274032010-07-03 00:47:00 +000035 DeclaredDefaultConstructor(false), DeclaredCopyConstructor(false),
Douglas Gregora376d102010-07-02 21:50:04 +000036 DeclaredCopyAssignment(false), DeclaredDestructor(false),
Fariborz Jahanian62509212009-09-12 18:26:03 +000037 Bases(0), NumBases(0), VBases(0), NumVBases(0),
John McCalld60e22e2010-03-12 01:19:31 +000038 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000039}
40
41CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
42 SourceLocation L, IdentifierInfo *Id,
43 CXXRecordDecl *PrevDecl,
44 SourceLocation TKL)
45 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
46 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000047 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000048
Ted Kremenek4b7c9832008-09-05 17:16:31 +000049CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
50 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000051 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000052 CXXRecordDecl* PrevDecl,
53 bool DelayTypeCreation) {
Mike Stump1eb44332009-09-09 15:08:12 +000054 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000055 PrevDecl, TKL);
Mike Stump1eb44332009-09-09 15:08:12 +000056
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000057 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000058 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000059 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000060 return R;
61}
62
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000063CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, EmptyShell Empty) {
64 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(), 0, 0,
65 SourceLocation());
66}
67
Mike Stump1eb44332009-09-09 15:08:12 +000068void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000069CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000070 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000071 ASTContext &C = getASTContext();
72
Mike Stump1eb44332009-09-09 15:08:12 +000073 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000074 // An aggregate is an array or a class (clause 9) with [...]
75 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000076 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000077
John McCall86ff3082010-02-04 22:26:26 +000078 if (data().Bases)
79 C.Deallocate(data().Bases);
Mike Stump1eb44332009-09-09 15:08:12 +000080
Anders Carlsson6f6de732010-03-29 05:13:12 +000081 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000082 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000083
84 // The virtual bases of this class.
85 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000086
John McCall86ff3082010-02-04 22:26:26 +000087 data().Bases = new(C) CXXBaseSpecifier [NumBases];
88 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000089 for (unsigned i = 0; i < NumBases; ++i) {
John McCall86ff3082010-02-04 22:26:26 +000090 data().Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000091 // Keep track of inherited vbases for this base class.
92 const CXXBaseSpecifier *Base = Bases[i];
93 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +000094 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000095 if (BaseType->isDependentType())
96 continue;
97 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +000098 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +000099
100 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000101 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000102 BaseClassDecl->vbases_begin(),
103 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000104 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000105 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000106 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000107 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000108
109 if (Base->isVirtual()) {
110 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000111 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000112 VBases.push_back(Base);
113 }
114
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000115 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000116
117 if (VBases.empty())
118 return;
119
120 // Create base specifier for any direct or indirect virtual bases.
121 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
122 data().NumVBases = VBases.size();
123 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000124 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
125
Anders Carlsson6f6de732010-03-29 05:13:12 +0000126 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000127 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000128 continue;
129
Nick Lewycky56062202010-07-26 16:56:01 +0000130 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
131 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000132
133 data().VBases[I] =
134 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000135 VBaseClassDecl->getTagKind() == TTK_Class,
Nick Lewycky56062202010-07-26 16:56:01 +0000136 VBases[I]->getAccessSpecifier(), VBaseTypeInfo);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000137 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000138}
139
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000140/// Callback function for CXXRecordDecl::forallBases that acknowledges
141/// that it saw a base class.
142static bool SawBase(const CXXRecordDecl *, void *) {
143 return true;
144}
145
146bool CXXRecordDecl::hasAnyDependentBases() const {
147 if (!isDependentContext())
148 return false;
149
150 return !forallBases(SawBase, 0);
151}
152
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000153bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000154 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000155}
156
Douglas Gregor0d405db2010-07-01 20:59:04 +0000157/// \brief Perform a simplistic form of overload resolution that only considers
158/// cv-qualifiers on a single parameter, and return the best overload candidate
159/// (if there is one).
160static CXXMethodDecl *
161GetBestOverloadCandidateSimple(
162 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
163 if (Cands.empty())
164 return 0;
165 if (Cands.size() == 1)
166 return Cands[0].first;
167
168 unsigned Best = 0, N = Cands.size();
169 for (unsigned I = 1; I != N; ++I)
170 if (Cands[Best].second.isSupersetOf(Cands[I].second))
171 Best = I;
172
173 for (unsigned I = 1; I != N; ++I)
174 if (Cands[Best].second.isSupersetOf(Cands[I].second))
175 return 0;
176
177 return Cands[Best].first;
178}
179
Mike Stump1eb44332009-09-09 15:08:12 +0000180CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000181 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000182 QualType ClassType
183 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000184 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000185 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000186 Context.getCanonicalType(ClassType));
187 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000188 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000189 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000190 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000191 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000192 // C++ [class.copy]p2:
193 // A non-template constructor for class X is a copy constructor if [...]
194 if (isa<FunctionTemplateDecl>(*Con))
195 continue;
196
Douglas Gregor0d405db2010-07-01 20:59:04 +0000197 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
198 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000199 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
200 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000201 Found.push_back(std::make_pair(
202 const_cast<CXXConstructorDecl *>(Constructor),
203 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000204 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000205 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000206
207 return cast_or_null<CXXConstructorDecl>(
208 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000209}
210
Douglas Gregorb87786f2010-07-01 17:48:08 +0000211CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
212 ASTContext &Context = getASTContext();
213 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
214 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
215
216 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
217 DeclContext::lookup_const_iterator Op, OpEnd;
218 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
219 // C++ [class.copy]p9:
220 // A user-declared copy assignment operator is a non-static non-template
221 // member function of class X with exactly one parameter of type X, X&,
222 // const X&, volatile X& or const volatile X&.
223 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
224 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
225 continue;
226
227 const FunctionProtoType *FnType
228 = Method->getType()->getAs<FunctionProtoType>();
229 assert(FnType && "Overloaded operator has no prototype.");
230 // Don't assert on this; an invalid decl might have been left in the AST.
231 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
232 continue;
233
234 QualType ArgType = FnType->getArgType(0);
235 Qualifiers Quals;
236 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
237 ArgType = Ref->getPointeeType();
238 // If we have a const argument and we have a reference to a non-const,
239 // this function does not match.
240 if (ArgIsConst && !ArgType.isConstQualified())
241 continue;
242
243 Quals = ArgType.getQualifiers();
244 } else {
245 // By-value copy-assignment operators are treated like const X&
246 // copy-assignment operators.
247 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
248 }
249
250 if (!Context.hasSameUnqualifiedType(ArgType, Class))
251 continue;
252
253 // Save this copy-assignment operator. It might be "the one".
254 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
255 }
256
257 // Use a simplistic form of overload resolution to find the candidate.
258 return GetBestOverloadCandidateSimple(Found);
259}
260
Sebastian Redl64b45f72009-01-05 20:52:13 +0000261void
Mike Stump1eb44332009-09-09 15:08:12 +0000262CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000263 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000264 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
265 // Note that we have a user-declared constructor.
John McCall86ff3082010-02-04 22:26:26 +0000266 data().UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000267
Douglas Gregor18274032010-07-03 00:47:00 +0000268 // Note that we have no need of an implicitly-declared default constructor.
269 data().DeclaredDefaultConstructor = true;
270
Mike Stump1eb44332009-09-09 15:08:12 +0000271 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000272 // An aggregate is an array or a class (clause 9) with no
273 // user-declared constructors (12.1) [...].
John McCall86ff3082010-02-04 22:26:26 +0000274 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000275
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000276 // C++ [class]p4:
277 // A POD-struct is an aggregate class [...]
John McCall86ff3082010-02-04 22:26:26 +0000278 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000279
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000280 // C++ [class.ctor]p5:
281 // A constructor is trivial if it is an implicitly-declared default
282 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000283 // FIXME: C++0x: don't do this for "= default" default constructors.
John McCall86ff3082010-02-04 22:26:26 +0000284 data().HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000286 // Note when we have a user-declared copy constructor, which will
287 // suppress the implicit declaration of a copy constructor.
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000288 if (ConDecl->isCopyConstructor()) {
John McCall86ff3082010-02-04 22:26:26 +0000289 data().UserDeclaredCopyConstructor = true;
Douglas Gregor22584312010-07-02 23:41:54 +0000290 data().DeclaredCopyConstructor = true;
291
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000292 // C++ [class.copy]p6:
293 // A copy constructor is trivial if it is implicitly declared.
294 // FIXME: C++0x: don't do this for "= default" copy constructors.
John McCall86ff3082010-02-04 22:26:26 +0000295 data().HasTrivialCopyConstructor = false;
Douglas Gregor22584312010-07-02 23:41:54 +0000296
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000297 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000298}
299
Sebastian Redl64b45f72009-01-05 20:52:13 +0000300void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
301 CXXMethodDecl *OpDecl) {
302 // We're interested specifically in copy assignment operators.
John McCall183700f2009-09-21 23:43:11 +0000303 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000304 assert(FnType && "Overloaded operator has no proto function type.");
305 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregor77da3f42009-10-13 23:45:19 +0000306
307 // Copy assignment operators must be non-templates.
308 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
309 return;
310
Sebastian Redl64b45f72009-01-05 20:52:13 +0000311 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000312 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000313 ArgType = Ref->getPointeeType();
314
315 ArgType = ArgType.getUnqualifiedType();
316 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
317 const_cast<CXXRecordDecl*>(this)));
318
Douglas Gregora4923eb2009-11-16 21:35:15 +0000319 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000320 return;
321
322 // This is a copy assignment operator.
Eli Friedman88fad632009-11-07 00:02:45 +0000323 // Note on the decl that it is a copy assignment operator.
324 OpDecl->setCopyAssignment(true);
325
Sebastian Redl64b45f72009-01-05 20:52:13 +0000326 // Suppress the implicit declaration of a copy constructor.
John McCall86ff3082010-02-04 22:26:26 +0000327 data().UserDeclaredCopyAssignment = true;
Douglas Gregora376d102010-07-02 21:50:04 +0000328 data().DeclaredCopyAssignment = true;
329
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000330 // C++ [class.copy]p11:
331 // A copy assignment operator is trivial if it is implicitly declared.
332 // FIXME: C++0x: don't do this for "= default" copy operators.
John McCall86ff3082010-02-04 22:26:26 +0000333 data().HasTrivialCopyAssignment = false;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000334
Sebastian Redl64b45f72009-01-05 20:52:13 +0000335 // C++ [class]p4:
336 // A POD-struct is an aggregate class that [...] has no user-defined copy
337 // assignment operator [...].
John McCall86ff3082010-02-04 22:26:26 +0000338 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000339}
340
John McCallb05b5f32010-03-15 09:07:48 +0000341static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
342 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000343 if (isa<UsingShadowDecl>(Conv))
344 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000345 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
346 T = ConvTemp->getTemplatedDecl()->getResultType();
347 else
348 T = cast<CXXConversionDecl>(Conv)->getConversionType();
349 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000350}
351
John McCallb05b5f32010-03-15 09:07:48 +0000352/// Collect the visible conversions of a base class.
353///
354/// \param Base a base class of the class we're considering
355/// \param InVirtual whether this base class is a virtual base (or a base
356/// of a virtual base)
357/// \param Access the access along the inheritance path to this base
358/// \param ParentHiddenTypes the conversions provided by the inheritors
359/// of this base
360/// \param Output the set to which to add conversions from non-virtual bases
361/// \param VOutput the set to which to add conversions from virtual bases
362/// \param HiddenVBaseCs the set of conversions which were hidden in a
363/// virtual base along some inheritance path
364static void CollectVisibleConversions(ASTContext &Context,
365 CXXRecordDecl *Record,
366 bool InVirtual,
367 AccessSpecifier Access,
368 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
369 UnresolvedSetImpl &Output,
370 UnresolvedSetImpl &VOutput,
371 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
372 // The set of types which have conversions in this class or its
373 // subclasses. As an optimization, we don't copy the derived set
374 // unless it might change.
375 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
376 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
377
378 // Collect the direct conversions and figure out which conversions
379 // will be hidden in the subclasses.
380 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
381 if (!Cs.empty()) {
382 HiddenTypesBuffer = ParentHiddenTypes;
383 HiddenTypes = &HiddenTypesBuffer;
384
385 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
386 bool Hidden =
387 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
388
389 // If this conversion is hidden and we're in a virtual base,
390 // remember that it's hidden along some inheritance path.
391 if (Hidden && InVirtual)
392 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
393
394 // If this conversion isn't hidden, add it to the appropriate output.
395 else if (!Hidden) {
396 AccessSpecifier IAccess
397 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
398
399 if (InVirtual)
400 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000401 else
John McCallb05b5f32010-03-15 09:07:48 +0000402 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000403 }
404 }
405 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000406
John McCallb05b5f32010-03-15 09:07:48 +0000407 // Collect information recursively from any base classes.
408 for (CXXRecordDecl::base_class_iterator
409 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
410 const RecordType *RT = I->getType()->getAs<RecordType>();
411 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000412
John McCallb05b5f32010-03-15 09:07:48 +0000413 AccessSpecifier BaseAccess
414 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
415 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000416
John McCallb05b5f32010-03-15 09:07:48 +0000417 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
418 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
419 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000420 }
John McCallb05b5f32010-03-15 09:07:48 +0000421}
Sebastian Redl9994a342009-10-25 17:03:50 +0000422
John McCallb05b5f32010-03-15 09:07:48 +0000423/// Collect the visible conversions of a class.
424///
425/// This would be extremely straightforward if it weren't for virtual
426/// bases. It might be worth special-casing that, really.
427static void CollectVisibleConversions(ASTContext &Context,
428 CXXRecordDecl *Record,
429 UnresolvedSetImpl &Output) {
430 // The collection of all conversions in virtual bases that we've
431 // found. These will be added to the output as long as they don't
432 // appear in the hidden-conversions set.
433 UnresolvedSet<8> VBaseCs;
434
435 // The set of conversions in virtual bases that we've determined to
436 // be hidden.
437 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
438
439 // The set of types hidden by classes derived from this one.
440 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
441
442 // Go ahead and collect the direct conversions and add them to the
443 // hidden-types set.
444 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
445 Output.append(Cs.begin(), Cs.end());
446 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
447 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
448
449 // Recursively collect conversions from base classes.
450 for (CXXRecordDecl::base_class_iterator
451 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
452 const RecordType *RT = I->getType()->getAs<RecordType>();
453 if (!RT) continue;
454
455 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
456 I->isVirtual(), I->getAccessSpecifier(),
457 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
458 }
459
460 // Add any unhidden conversions provided by virtual bases.
461 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
462 I != E; ++I) {
463 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
464 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000465 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000466}
467
468/// getVisibleConversionFunctions - get all conversion functions visible
469/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000470const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000471 // If root class, all conversions are visible.
472 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000473 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000474 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000475 if (data().ComputedVisibleConversions)
476 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000477 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000478 data().ComputedVisibleConversions = true;
479 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000480}
481
John McCall32daa422010-03-31 01:36:47 +0000482#ifndef NDEBUG
483void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000484 assert(ConvDecl->getDeclContext() == this &&
485 "conversion function does not belong to this record");
486
John McCall32daa422010-03-31 01:36:47 +0000487 ConvDecl = ConvDecl->getUnderlyingDecl();
488 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
489 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
490 } else {
491 assert(isa<CXXConversionDecl>(ConvDecl));
492 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000493}
John McCall32daa422010-03-31 01:36:47 +0000494#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000495
John McCall32daa422010-03-31 01:36:47 +0000496void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
497 // This operation is O(N) but extremely rare. Sema only uses it to
498 // remove UsingShadowDecls in a class that were followed by a direct
499 // declaration, e.g.:
500 // class A : B {
501 // using B::operator int;
502 // operator int();
503 // };
504 // This is uncommon by itself and even more uncommon in conjunction
505 // with sufficiently large numbers of directly-declared conversions
506 // that asymptotic behavior matters.
507
508 UnresolvedSetImpl &Convs = *getConversionFunctions();
509 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
510 if (Convs[I].getDecl() == ConvDecl) {
511 Convs.erase(I);
512 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
513 && "conversion was found multiple times in unresolved set");
514 return;
515 }
516 }
517
518 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000519}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000520
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000521void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
522 Method->setVirtualAsWritten(true);
523 setAggregate(false);
524 setPOD(false);
525 setEmpty(false);
526 setPolymorphic(true);
527 setHasTrivialConstructor(false);
528 setHasTrivialCopyConstructor(false);
529 setHasTrivialCopyAssignment(false);
530}
531
Douglas Gregorf6b11852009-10-08 15:14:33 +0000532CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000533 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000534 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
535
536 return 0;
537}
538
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000539MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
540 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
541}
542
Douglas Gregorf6b11852009-10-08 15:14:33 +0000543void
544CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
545 TemplateSpecializationKind TSK) {
546 assert(TemplateOrInstantiation.isNull() &&
547 "Previous template or instantiation?");
548 assert(!isa<ClassTemplateSpecializationDecl>(this));
549 TemplateOrInstantiation
550 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
551}
552
Anders Carlssonb13e3572009-12-07 06:33:48 +0000553TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
554 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000555 = dyn_cast<ClassTemplateSpecializationDecl>(this))
556 return Spec->getSpecializationKind();
557
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000558 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000559 return MSInfo->getTemplateSpecializationKind();
560
561 return TSK_Undeclared;
562}
563
564void
565CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
566 if (ClassTemplateSpecializationDecl *Spec
567 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
568 Spec->setSpecializationKind(TSK);
569 return;
570 }
571
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000572 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000573 MSInfo->setTemplateSpecializationKind(TSK);
574 return;
575 }
576
577 assert(false && "Not a class template or member class specialization");
578}
579
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000580CXXConstructorDecl *
Douglas Gregoreb8c6702010-07-01 22:31:05 +0000581CXXRecordDecl::getDefaultConstructor() {
582 ASTContext &Context = getASTContext();
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000583 QualType ClassType = Context.getTypeDeclType(this);
584 DeclarationName ConstructorName
585 = Context.DeclarationNames.getCXXConstructorName(
586 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000588 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000589 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000590 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000591 // FIXME: In C++0x, a constructor template can be a default constructor.
592 if (isa<FunctionTemplateDecl>(*Con))
593 continue;
594
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000595 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
596 if (Constructor->isDefaultConstructor())
597 return Constructor;
598 }
599 return 0;
600}
601
Douglas Gregor1d110e02010-07-01 14:13:13 +0000602CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
603 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000604 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000605
606 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000607 = Context.DeclarationNames.getCXXDestructorName(
608 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000609
John McCallc0bf4622010-02-23 00:48:20 +0000610 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000611 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000612 if (I == E)
613 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000615 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000616 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Anders Carlsson7267c162009-05-29 21:03:38 +0000618 return Dtor;
619}
620
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000621CXXMethodDecl *
622CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000623 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000624 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000625 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000626 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000627 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000628}
629
Douglas Gregor90916562009-09-29 18:16:17 +0000630bool CXXMethodDecl::isUsualDeallocationFunction() const {
631 if (getOverloadedOperator() != OO_Delete &&
632 getOverloadedOperator() != OO_Array_Delete)
633 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000634
635 // C++ [basic.stc.dynamic.deallocation]p2:
636 // A template instance is never a usual deallocation function,
637 // regardless of its signature.
638 if (getPrimaryTemplate())
639 return false;
640
Douglas Gregor90916562009-09-29 18:16:17 +0000641 // C++ [basic.stc.dynamic.deallocation]p2:
642 // If a class T has a member deallocation function named operator delete
643 // with exactly one parameter, then that function is a usual (non-placement)
644 // deallocation function. [...]
645 if (getNumParams() == 1)
646 return true;
647
648 // C++ [basic.stc.dynamic.deallocation]p2:
649 // [...] If class T does not declare such an operator delete but does
650 // declare a member deallocation function named operator delete with
651 // exactly two parameters, the second of which has type std::size_t (18.1),
652 // then this function is a usual deallocation function.
653 ASTContext &Context = getASTContext();
654 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000655 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
656 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000657 return false;
658
659 // This function is a usual deallocation function if there are no
660 // single-parameter deallocation functions of the same kind.
661 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
662 R.first != R.second; ++R.first) {
663 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
664 if (FD->getNumParams() == 1)
665 return false;
666 }
667
668 return true;
669}
670
Douglas Gregor06a9f362010-05-01 20:49:11 +0000671bool CXXMethodDecl::isCopyAssignmentOperator() const {
672 // C++0x [class.copy]p19:
673 // A user-declared copy assignment operator X::operator= is a non-static
674 // non-template member function of class X with exactly one parameter of
675 // type X, X&, const X&, volatile X& or const volatile X&.
676 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
677 /*non-static*/ isStatic() ||
678 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
679 /*exactly one parameter*/getNumParams() != 1)
680 return false;
681
682 QualType ParamType = getParamDecl(0)->getType();
683 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
684 ParamType = Ref->getPointeeType();
685
686 ASTContext &Context = getASTContext();
687 QualType ClassType
688 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
689 return Context.hasSameUnqualifiedType(ClassType, ParamType);
690}
691
Anders Carlsson05eb2442009-05-16 23:58:37 +0000692void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000693 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000694 assert(!MD->getParent()->isDependentContext() &&
695 "Can't add an overridden method to a class template!");
696
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000697 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000698}
699
700CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000701 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000702}
703
704CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000705 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000706}
707
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000708unsigned CXXMethodDecl::size_overridden_methods() const {
709 return getASTContext().overridden_methods_size(this);
710}
711
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000712QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000713 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
714 // If the member function is declared const, the type of this is const X*,
715 // if the member function is declared volatile, the type of this is
716 // volatile X*, and if the member function is declared const volatile,
717 // the type of this is const volatile X*.
718
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000719 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000720
John McCall3cb0ebd2010-03-10 03:28:59 +0000721 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000722 ClassTy = C.getQualifiedType(ClassTy,
723 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000724 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000725}
726
Eli Friedmand7d7f672009-12-06 20:50:05 +0000727bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000728 // If this function is a template instantiation, look at the template from
729 // which it was instantiated.
730 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
731 if (!CheckFn)
732 CheckFn = this;
733
Eli Friedmand7d7f672009-12-06 20:50:05 +0000734 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000735 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000736}
737
Douglas Gregor7ad83902008-11-05 04:29:56 +0000738CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000739CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +0000740 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000741 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000742 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
743 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
744 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000745{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000746}
747
748CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000749CXXBaseOrMemberInitializer(ASTContext &Context,
750 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000751 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000752 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
753 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
754 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000755{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000756}
757
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000758CXXBaseOrMemberInitializer::
759CXXBaseOrMemberInitializer(ASTContext &Context,
760 FieldDecl *Member, SourceLocation MemberLoc,
761 SourceLocation L, Expr *Init, SourceLocation R,
762 VarDecl **Indices,
763 unsigned NumIndices)
764 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000765 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
766 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000767{
768 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
769 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
770}
771
772CXXBaseOrMemberInitializer *
773CXXBaseOrMemberInitializer::Create(ASTContext &Context,
774 FieldDecl *Member,
775 SourceLocation MemberLoc,
776 SourceLocation L,
777 Expr *Init,
778 SourceLocation R,
779 VarDecl **Indices,
780 unsigned NumIndices) {
781 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
782 sizeof(VarDecl *) * NumIndices,
783 llvm::alignof<CXXBaseOrMemberInitializer>());
784 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
785 L, Init, R, Indices, NumIndices);
786}
787
Douglas Gregor802ab452009-12-02 22:36:29 +0000788TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
789 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000790 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000791 else
792 return TypeLoc();
793}
794
795Type *CXXBaseOrMemberInitializer::getBaseClass() {
796 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000797 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000798 else
799 return 0;
800}
801
802const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
803 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000804 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000805 else
806 return 0;
807}
808
809SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
810 if (isMemberInitializer())
811 return getMemberLocation();
812
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000813 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +0000814}
815
816SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
817 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000818}
819
Douglas Gregorb48fe382008-10-31 09:07:45 +0000820CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000821CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000822 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000823 QualType(), 0, false, false, false);
824}
825
826CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +0000827CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000828 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000829 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000830 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000831 bool isInline,
832 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000833 assert(NameInfo.getName().getNameKind()
834 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000835 "Name must refer to a constructor");
Abramo Bagnara25777432010-08-11 22:01:17 +0000836 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000837 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000838}
839
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000840bool CXXConstructorDecl::isDefaultConstructor() const {
841 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000842 // A default constructor for a class X is a constructor of class
843 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000844 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000845 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000846}
847
Mike Stump1eb44332009-09-09 15:08:12 +0000848bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000849CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000850 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000851 // A non-template constructor for class X is a copy constructor
852 // if its first parameter is of type X&, const X&, volatile X& or
853 // const volatile X&, and either there are no other parameters
854 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000855 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000856 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000857 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000858 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000859 return false;
860
861 const ParmVarDecl *Param = getParamDecl(0);
862
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000863 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000864 const LValueReferenceType *ParamRefType =
865 Param->getType()->getAs<LValueReferenceType>();
866 if (!ParamRefType)
867 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000868
Douglas Gregorfd476482009-11-13 23:59:09 +0000869 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000870 ASTContext &Context = getASTContext();
871
Douglas Gregorfd476482009-11-13 23:59:09 +0000872 CanQualType PointeeType
873 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000874 CanQualType ClassTy
875 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000876 if (PointeeType.getUnqualifiedType() != ClassTy)
877 return false;
878
John McCall0953e762009-09-24 19:53:00 +0000879 // FIXME: other qualifiers?
880
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000881 // We have a copy constructor.
882 TypeQuals = PointeeType.getCVRQualifiers();
883 return true;
884}
885
Anders Carlssonfaccd722009-08-28 16:57:08 +0000886bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000887 // C++ [class.conv.ctor]p1:
888 // A constructor declared without the function-specifier explicit
889 // that can be called with a single parameter specifies a
890 // conversion from the type of its first parameter to the type of
891 // its class. Such a constructor is called a converting
892 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000893 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000894 return false;
895
Mike Stump1eb44332009-09-09 15:08:12 +0000896 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000897 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000898 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000899 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000900}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000901
Douglas Gregor66724ea2009-11-14 01:20:54 +0000902bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
903 if ((getNumParams() < 1) ||
904 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
905 (getPrimaryTemplate() == 0) ||
906 (getDescribedFunctionTemplate() != 0))
907 return false;
908
909 const ParmVarDecl *Param = getParamDecl(0);
910
911 ASTContext &Context = getASTContext();
912 CanQualType ParamType = Context.getCanonicalType(Param->getType());
913
914 // Strip off the lvalue reference, if any.
915 if (CanQual<LValueReferenceType> ParamRefType
916 = ParamType->getAs<LValueReferenceType>())
917 ParamType = ParamRefType->getPointeeType();
918
919
920 // Is it the same as our our class type?
921 CanQualType ClassTy
922 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
923 if (ParamType.getUnqualifiedType() != ClassTy)
924 return false;
925
926 return true;
927}
928
Douglas Gregor42a552f2008-11-05 20:51:48 +0000929CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000930CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000931 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000932 QualType(), false, false);
933}
934
935CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +0000936CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000937 const DeclarationNameInfo &NameInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000938 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000939 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000940 assert(NameInfo.getName().getNameKind()
941 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000942 "Name must refer to a destructor");
Abramo Bagnara25777432010-08-11 22:01:17 +0000943 return new (C) CXXDestructorDecl(RD, NameInfo, T, isInline,
944 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000945}
946
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000947CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000948CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000949 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000950 QualType(), 0, false, false);
951}
952
953CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000954CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000955 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000956 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000957 bool isInline, bool isExplicit) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000958 assert(NameInfo.getName().getNameKind()
959 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000960 "Name must refer to a conversion function");
Abramo Bagnara25777432010-08-11 22:01:17 +0000961 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
962 isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000963}
964
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000965LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000966 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000967 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000968 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000969 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000970}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000971
972UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
973 SourceLocation L,
974 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000975 SourceRange QualifierRange,
976 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000977 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000978 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000979 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000980 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
981 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000982 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000983 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000984}
985
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000986NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
987 if (NamespaceAliasDecl *NA =
988 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
989 return NA->getNamespace();
990 return cast_or_null<NamespaceDecl>(NominatedNamespace);
991}
992
Mike Stump1eb44332009-09-09 15:08:12 +0000993NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +0000994 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000995 SourceLocation AliasLoc,
996 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000997 SourceRange QualifierRange,
998 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +0000999 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001000 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001001 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1002 Namespace = NS->getOriginalNamespace();
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001003 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001004 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001005}
1006
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001007UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001008 SourceRange NNR, SourceLocation UL,
1009 NestedNameSpecifier* TargetNNS,
1010 const DeclarationNameInfo &NameInfo,
1011 bool IsTypeNameArg) {
1012 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001013}
1014
John McCall7ba107a2009-11-18 02:36:19 +00001015UnresolvedUsingValueDecl *
1016UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1017 SourceLocation UsingLoc,
1018 SourceRange TargetNNR,
1019 NestedNameSpecifier *TargetNNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001020 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001021 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001022 TargetNNR, TargetNNS, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001023}
1024
1025UnresolvedUsingTypenameDecl *
1026UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1027 SourceLocation UsingLoc,
1028 SourceLocation TypenameLoc,
1029 SourceRange TargetNNR,
1030 NestedNameSpecifier *TargetNNS,
1031 SourceLocation TargetNameLoc,
1032 DeclarationName TargetName) {
1033 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1034 TargetNNR, TargetNNS,
1035 TargetNameLoc,
1036 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001037}
1038
Anders Carlssonfb311762009-03-14 00:25:26 +00001039StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1040 SourceLocation L, Expr *AssertExpr,
1041 StringLiteral *Message) {
1042 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1043}
1044
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001045static const char *getAccessName(AccessSpecifier AS) {
1046 switch (AS) {
1047 default:
1048 case AS_none:
1049 assert("Invalid access specifier!");
1050 return 0;
1051 case AS_public:
1052 return "public";
1053 case AS_private:
1054 return "private";
1055 case AS_protected:
1056 return "protected";
1057 }
1058}
1059
1060const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1061 AccessSpecifier AS) {
1062 return DB << getAccessName(AS);
1063}