blob: e900165353cee470f7b381a4dc08f9228db0182a [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),
35 Bases(0), NumBases(0), VBases(0), NumVBases(0),
John McCalld60e22e2010-03-12 01:19:31 +000036 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000037}
38
39CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
40 SourceLocation L, IdentifierInfo *Id,
41 CXXRecordDecl *PrevDecl,
42 SourceLocation TKL)
43 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
44 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000045 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000046
Ted Kremenek4b7c9832008-09-05 17:16:31 +000047CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
48 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000049 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000050 CXXRecordDecl* PrevDecl,
51 bool DelayTypeCreation) {
Mike Stump1eb44332009-09-09 15:08:12 +000052 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000053 PrevDecl, TKL);
Mike Stump1eb44332009-09-09 15:08:12 +000054
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000055 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000056 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000057 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000058 return R;
59}
60
Douglas Gregorf8268ae2008-10-22 17:49:05 +000061CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000062}
63
64void CXXRecordDecl::Destroy(ASTContext &C) {
John McCall86ff3082010-02-04 22:26:26 +000065 if (data().Definition == this) {
66 C.Deallocate(data().Bases);
67 C.Deallocate(data().VBases);
68 C.Deallocate(&data());
69 }
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000070 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000071}
72
Mike Stump1eb44332009-09-09 15:08:12 +000073void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000074CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000075 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000076 ASTContext &C = getASTContext();
77
Mike Stump1eb44332009-09-09 15:08:12 +000078 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000079 // An aggregate is an array or a class (clause 9) with [...]
80 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000081 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000082
John McCall86ff3082010-02-04 22:26:26 +000083 if (data().Bases)
84 C.Deallocate(data().Bases);
Mike Stump1eb44332009-09-09 15:08:12 +000085
Anders Carlsson6f6de732010-03-29 05:13:12 +000086 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000087 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000088
89 // The virtual bases of this class.
90 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000091
John McCall86ff3082010-02-04 22:26:26 +000092 data().Bases = new(C) CXXBaseSpecifier [NumBases];
93 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000094 for (unsigned i = 0; i < NumBases; ++i) {
John McCall86ff3082010-02-04 22:26:26 +000095 data().Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000096 // Keep track of inherited vbases for this base class.
97 const CXXBaseSpecifier *Base = Bases[i];
98 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +000099 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000100 if (BaseType->isDependentType())
101 continue;
102 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000103 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000104
105 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000106 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000107 BaseClassDecl->vbases_begin(),
108 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000109 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000110 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000111 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000112 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000113
114 if (Base->isVirtual()) {
115 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000116 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000117 VBases.push_back(Base);
118 }
119
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000120 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000121
122 if (VBases.empty())
123 return;
124
125 // Create base specifier for any direct or indirect virtual bases.
126 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
127 data().NumVBases = VBases.size();
128 for (int I = 0, E = VBases.size(); I != E; ++I) {
129 QualType VBaseType = VBases[I]->getType();
130
131 // Skip dependent types; we can't do any checking on them now.
132 if (VBaseType->isDependentType())
133 continue;
134
135 CXXRecordDecl *VBaseClassDecl
136 = cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
137
138 data().VBases[I] =
139 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000140 VBaseClassDecl->getTagKind() == TTK_Class,
Anders Carlsson6f6de732010-03-29 05:13:12 +0000141 VBases[I]->getAccessSpecifier(), VBaseType);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000142 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000143}
144
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000145/// Callback function for CXXRecordDecl::forallBases that acknowledges
146/// that it saw a base class.
147static bool SawBase(const CXXRecordDecl *, void *) {
148 return true;
149}
150
151bool CXXRecordDecl::hasAnyDependentBases() const {
152 if (!isDependentContext())
153 return false;
154
155 return !forallBases(SawBase, 0);
156}
157
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000158bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000159 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000160}
161
Douglas Gregor0d405db2010-07-01 20:59:04 +0000162/// \brief Perform a simplistic form of overload resolution that only considers
163/// cv-qualifiers on a single parameter, and return the best overload candidate
164/// (if there is one).
165static CXXMethodDecl *
166GetBestOverloadCandidateSimple(
167 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
168 if (Cands.empty())
169 return 0;
170 if (Cands.size() == 1)
171 return Cands[0].first;
172
173 unsigned Best = 0, N = Cands.size();
174 for (unsigned I = 1; I != N; ++I)
175 if (Cands[Best].second.isSupersetOf(Cands[I].second))
176 Best = I;
177
178 for (unsigned I = 1; I != N; ++I)
179 if (Cands[Best].second.isSupersetOf(Cands[I].second))
180 return 0;
181
182 return Cands[Best].first;
183}
184
Mike Stump1eb44332009-09-09 15:08:12 +0000185CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000186 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000187 QualType ClassType
188 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000189 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000190 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000191 Context.getCanonicalType(ClassType));
192 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000193 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000194 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000195 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000196 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000197 // C++ [class.copy]p2:
198 // A non-template constructor for class X is a copy constructor if [...]
199 if (isa<FunctionTemplateDecl>(*Con))
200 continue;
201
Douglas Gregor0d405db2010-07-01 20:59:04 +0000202 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
203 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000204 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
205 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000206 Found.push_back(std::make_pair(
207 const_cast<CXXConstructorDecl *>(Constructor),
208 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000209 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000210 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000211
212 return cast_or_null<CXXConstructorDecl>(
213 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000214}
215
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000216bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
217 const CXXMethodDecl *& MD) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000218 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
219 const_cast<CXXRecordDecl*>(this)));
220 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
221
222 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000223 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000224 Op != OpEnd; ++Op) {
225 // C++ [class.copy]p9:
226 // A user-declared copy assignment operator is a non-static non-template
227 // member function of class X with exactly one parameter of type X, X&,
228 // const X&, volatile X& or const volatile X&.
Douglas Gregor682054c2009-10-30 22:48:49 +0000229 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
230 if (!Method)
231 continue;
232
Sebastian Redl64b45f72009-01-05 20:52:13 +0000233 if (Method->isStatic())
234 continue;
Douglas Gregor77da3f42009-10-13 23:45:19 +0000235 if (Method->getPrimaryTemplate())
236 continue;
Douglas Gregor72564e72009-02-26 23:50:07 +0000237 const FunctionProtoType *FnType =
John McCall183700f2009-09-21 23:43:11 +0000238 Method->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000239 assert(FnType && "Overloaded operator has no prototype.");
240 // Don't assert on this; an invalid decl might have been left in the AST.
241 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
242 continue;
243 bool AcceptsConst = true;
244 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000245 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000246 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000247 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000248 if (!ArgType.isConstQualified())
249 AcceptsConst = false;
250 }
Douglas Gregora4923eb2009-11-16 21:35:15 +0000251 if (!Context.hasSameUnqualifiedType(ArgType, ClassType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000252 continue;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000253 MD = Method;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000254 // We have a single argument of type cv X or cv X&, i.e. we've found the
255 // copy assignment operator. Return whether it accepts const arguments.
256 return AcceptsConst;
257 }
258 assert(isInvalidDecl() &&
259 "No copy assignment operator declared in valid code.");
260 return false;
261}
262
Douglas Gregorb87786f2010-07-01 17:48:08 +0000263CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
264 ASTContext &Context = getASTContext();
265 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
266 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
267
268 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
269 DeclContext::lookup_const_iterator Op, OpEnd;
270 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
271 // C++ [class.copy]p9:
272 // A user-declared copy assignment operator is a non-static non-template
273 // member function of class X with exactly one parameter of type X, X&,
274 // const X&, volatile X& or const volatile X&.
275 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
276 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
277 continue;
278
279 const FunctionProtoType *FnType
280 = Method->getType()->getAs<FunctionProtoType>();
281 assert(FnType && "Overloaded operator has no prototype.");
282 // Don't assert on this; an invalid decl might have been left in the AST.
283 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
284 continue;
285
286 QualType ArgType = FnType->getArgType(0);
287 Qualifiers Quals;
288 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
289 ArgType = Ref->getPointeeType();
290 // If we have a const argument and we have a reference to a non-const,
291 // this function does not match.
292 if (ArgIsConst && !ArgType.isConstQualified())
293 continue;
294
295 Quals = ArgType.getQualifiers();
296 } else {
297 // By-value copy-assignment operators are treated like const X&
298 // copy-assignment operators.
299 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
300 }
301
302 if (!Context.hasSameUnqualifiedType(ArgType, Class))
303 continue;
304
305 // Save this copy-assignment operator. It might be "the one".
306 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
307 }
308
309 // Use a simplistic form of overload resolution to find the candidate.
310 return GetBestOverloadCandidateSimple(Found);
311}
312
Sebastian Redl64b45f72009-01-05 20:52:13 +0000313void
Mike Stump1eb44332009-09-09 15:08:12 +0000314CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000315 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000316 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
317 // Note that we have a user-declared constructor.
John McCall86ff3082010-02-04 22:26:26 +0000318 data().UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000319
Mike Stump1eb44332009-09-09 15:08:12 +0000320 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000321 // An aggregate is an array or a class (clause 9) with no
322 // user-declared constructors (12.1) [...].
John McCall86ff3082010-02-04 22:26:26 +0000323 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000324
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000325 // C++ [class]p4:
326 // A POD-struct is an aggregate class [...]
John McCall86ff3082010-02-04 22:26:26 +0000327 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000328
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000329 // C++ [class.ctor]p5:
330 // A constructor is trivial if it is an implicitly-declared default
331 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000332 // FIXME: C++0x: don't do this for "= default" default constructors.
John McCall86ff3082010-02-04 22:26:26 +0000333 data().HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000335 // Note when we have a user-declared copy constructor, which will
336 // suppress the implicit declaration of a copy constructor.
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000337 if (ConDecl->isCopyConstructor()) {
John McCall86ff3082010-02-04 22:26:26 +0000338 data().UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000339
340 // C++ [class.copy]p6:
341 // A copy constructor is trivial if it is implicitly declared.
342 // FIXME: C++0x: don't do this for "= default" copy constructors.
John McCall86ff3082010-02-04 22:26:26 +0000343 data().HasTrivialCopyConstructor = false;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000344 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000345}
346
Sebastian Redl64b45f72009-01-05 20:52:13 +0000347void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
348 CXXMethodDecl *OpDecl) {
349 // We're interested specifically in copy assignment operators.
John McCall183700f2009-09-21 23:43:11 +0000350 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000351 assert(FnType && "Overloaded operator has no proto function type.");
352 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregor77da3f42009-10-13 23:45:19 +0000353
354 // Copy assignment operators must be non-templates.
355 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
356 return;
357
Sebastian Redl64b45f72009-01-05 20:52:13 +0000358 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000359 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000360 ArgType = Ref->getPointeeType();
361
362 ArgType = ArgType.getUnqualifiedType();
363 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
364 const_cast<CXXRecordDecl*>(this)));
365
Douglas Gregora4923eb2009-11-16 21:35:15 +0000366 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000367 return;
368
369 // This is a copy assignment operator.
Eli Friedman88fad632009-11-07 00:02:45 +0000370 // Note on the decl that it is a copy assignment operator.
371 OpDecl->setCopyAssignment(true);
372
Sebastian Redl64b45f72009-01-05 20:52:13 +0000373 // Suppress the implicit declaration of a copy constructor.
John McCall86ff3082010-02-04 22:26:26 +0000374 data().UserDeclaredCopyAssignment = true;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000375
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000376 // C++ [class.copy]p11:
377 // A copy assignment operator is trivial if it is implicitly declared.
378 // FIXME: C++0x: don't do this for "= default" copy operators.
John McCall86ff3082010-02-04 22:26:26 +0000379 data().HasTrivialCopyAssignment = false;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000380
Sebastian Redl64b45f72009-01-05 20:52:13 +0000381 // C++ [class]p4:
382 // A POD-struct is an aggregate class that [...] has no user-defined copy
383 // assignment operator [...].
John McCall86ff3082010-02-04 22:26:26 +0000384 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000385}
386
John McCallb05b5f32010-03-15 09:07:48 +0000387static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
388 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000389 if (isa<UsingShadowDecl>(Conv))
390 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000391 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
392 T = ConvTemp->getTemplatedDecl()->getResultType();
393 else
394 T = cast<CXXConversionDecl>(Conv)->getConversionType();
395 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000396}
397
John McCallb05b5f32010-03-15 09:07:48 +0000398/// Collect the visible conversions of a base class.
399///
400/// \param Base a base class of the class we're considering
401/// \param InVirtual whether this base class is a virtual base (or a base
402/// of a virtual base)
403/// \param Access the access along the inheritance path to this base
404/// \param ParentHiddenTypes the conversions provided by the inheritors
405/// of this base
406/// \param Output the set to which to add conversions from non-virtual bases
407/// \param VOutput the set to which to add conversions from virtual bases
408/// \param HiddenVBaseCs the set of conversions which were hidden in a
409/// virtual base along some inheritance path
410static void CollectVisibleConversions(ASTContext &Context,
411 CXXRecordDecl *Record,
412 bool InVirtual,
413 AccessSpecifier Access,
414 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
415 UnresolvedSetImpl &Output,
416 UnresolvedSetImpl &VOutput,
417 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
418 // The set of types which have conversions in this class or its
419 // subclasses. As an optimization, we don't copy the derived set
420 // unless it might change.
421 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
422 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
423
424 // Collect the direct conversions and figure out which conversions
425 // will be hidden in the subclasses.
426 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
427 if (!Cs.empty()) {
428 HiddenTypesBuffer = ParentHiddenTypes;
429 HiddenTypes = &HiddenTypesBuffer;
430
431 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
432 bool Hidden =
433 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
434
435 // If this conversion is hidden and we're in a virtual base,
436 // remember that it's hidden along some inheritance path.
437 if (Hidden && InVirtual)
438 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
439
440 // If this conversion isn't hidden, add it to the appropriate output.
441 else if (!Hidden) {
442 AccessSpecifier IAccess
443 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
444
445 if (InVirtual)
446 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000447 else
John McCallb05b5f32010-03-15 09:07:48 +0000448 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000449 }
450 }
451 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000452
John McCallb05b5f32010-03-15 09:07:48 +0000453 // Collect information recursively from any base classes.
454 for (CXXRecordDecl::base_class_iterator
455 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
456 const RecordType *RT = I->getType()->getAs<RecordType>();
457 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000458
John McCallb05b5f32010-03-15 09:07:48 +0000459 AccessSpecifier BaseAccess
460 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
461 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000462
John McCallb05b5f32010-03-15 09:07:48 +0000463 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
464 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
465 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000466 }
John McCallb05b5f32010-03-15 09:07:48 +0000467}
Sebastian Redl9994a342009-10-25 17:03:50 +0000468
John McCallb05b5f32010-03-15 09:07:48 +0000469/// Collect the visible conversions of a class.
470///
471/// This would be extremely straightforward if it weren't for virtual
472/// bases. It might be worth special-casing that, really.
473static void CollectVisibleConversions(ASTContext &Context,
474 CXXRecordDecl *Record,
475 UnresolvedSetImpl &Output) {
476 // The collection of all conversions in virtual bases that we've
477 // found. These will be added to the output as long as they don't
478 // appear in the hidden-conversions set.
479 UnresolvedSet<8> VBaseCs;
480
481 // The set of conversions in virtual bases that we've determined to
482 // be hidden.
483 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
484
485 // The set of types hidden by classes derived from this one.
486 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
487
488 // Go ahead and collect the direct conversions and add them to the
489 // hidden-types set.
490 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
491 Output.append(Cs.begin(), Cs.end());
492 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
493 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
494
495 // Recursively collect conversions from base classes.
496 for (CXXRecordDecl::base_class_iterator
497 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
498 const RecordType *RT = I->getType()->getAs<RecordType>();
499 if (!RT) continue;
500
501 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
502 I->isVirtual(), I->getAccessSpecifier(),
503 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
504 }
505
506 // Add any unhidden conversions provided by virtual bases.
507 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
508 I != E; ++I) {
509 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
510 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000511 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000512}
513
514/// getVisibleConversionFunctions - get all conversion functions visible
515/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000516const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000517 // If root class, all conversions are visible.
518 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000519 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000520 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000521 if (data().ComputedVisibleConversions)
522 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000523 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000524 data().ComputedVisibleConversions = true;
525 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000526}
527
John McCall32daa422010-03-31 01:36:47 +0000528#ifndef NDEBUG
529void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000530 assert(ConvDecl->getDeclContext() == this &&
531 "conversion function does not belong to this record");
532
John McCall32daa422010-03-31 01:36:47 +0000533 ConvDecl = ConvDecl->getUnderlyingDecl();
534 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
535 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
536 } else {
537 assert(isa<CXXConversionDecl>(ConvDecl));
538 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000539}
John McCall32daa422010-03-31 01:36:47 +0000540#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000541
John McCall32daa422010-03-31 01:36:47 +0000542void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
543 // This operation is O(N) but extremely rare. Sema only uses it to
544 // remove UsingShadowDecls in a class that were followed by a direct
545 // declaration, e.g.:
546 // class A : B {
547 // using B::operator int;
548 // operator int();
549 // };
550 // This is uncommon by itself and even more uncommon in conjunction
551 // with sufficiently large numbers of directly-declared conversions
552 // that asymptotic behavior matters.
553
554 UnresolvedSetImpl &Convs = *getConversionFunctions();
555 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
556 if (Convs[I].getDecl() == ConvDecl) {
557 Convs.erase(I);
558 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
559 && "conversion was found multiple times in unresolved set");
560 return;
561 }
562 }
563
564 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000565}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000566
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000567void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
568 Method->setVirtualAsWritten(true);
569 setAggregate(false);
570 setPOD(false);
571 setEmpty(false);
572 setPolymorphic(true);
573 setHasTrivialConstructor(false);
574 setHasTrivialCopyConstructor(false);
575 setHasTrivialCopyAssignment(false);
576}
577
Douglas Gregorf6b11852009-10-08 15:14:33 +0000578CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000579 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000580 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
581
582 return 0;
583}
584
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000585MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
586 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
587}
588
Douglas Gregorf6b11852009-10-08 15:14:33 +0000589void
590CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
591 TemplateSpecializationKind TSK) {
592 assert(TemplateOrInstantiation.isNull() &&
593 "Previous template or instantiation?");
594 assert(!isa<ClassTemplateSpecializationDecl>(this));
595 TemplateOrInstantiation
596 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
597}
598
Anders Carlssonb13e3572009-12-07 06:33:48 +0000599TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
600 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000601 = dyn_cast<ClassTemplateSpecializationDecl>(this))
602 return Spec->getSpecializationKind();
603
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000604 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000605 return MSInfo->getTemplateSpecializationKind();
606
607 return TSK_Undeclared;
608}
609
610void
611CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
612 if (ClassTemplateSpecializationDecl *Spec
613 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
614 Spec->setSpecializationKind(TSK);
615 return;
616 }
617
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000618 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000619 MSInfo->setTemplateSpecializationKind(TSK);
620 return;
621 }
622
623 assert(false && "Not a class template or member class specialization");
624}
625
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000626CXXConstructorDecl *
627CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
628 QualType ClassType = Context.getTypeDeclType(this);
629 DeclarationName ConstructorName
630 = Context.DeclarationNames.getCXXConstructorName(
631 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000633 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000634 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000635 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000636 // FIXME: In C++0x, a constructor template can be a default constructor.
637 if (isa<FunctionTemplateDecl>(*Con))
638 continue;
639
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000640 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
641 if (Constructor->isDefaultConstructor())
642 return Constructor;
643 }
644 return 0;
645}
646
Douglas Gregor1d110e02010-07-01 14:13:13 +0000647CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
648 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000649 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000650
651 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000652 = Context.DeclarationNames.getCXXDestructorName(
653 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000654
John McCallc0bf4622010-02-23 00:48:20 +0000655 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000656 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000657 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000659 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000660 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Anders Carlsson7267c162009-05-29 21:03:38 +0000662 return Dtor;
663}
664
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000665CXXMethodDecl *
666CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000667 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000668 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000669 bool isStatic, StorageClass SCAsWritten, bool isInline) {
John McCalla93c9342009-12-07 02:54:59 +0000670 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000671 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000672}
673
Douglas Gregor90916562009-09-29 18:16:17 +0000674bool CXXMethodDecl::isUsualDeallocationFunction() const {
675 if (getOverloadedOperator() != OO_Delete &&
676 getOverloadedOperator() != OO_Array_Delete)
677 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000678
679 // C++ [basic.stc.dynamic.deallocation]p2:
680 // A template instance is never a usual deallocation function,
681 // regardless of its signature.
682 if (getPrimaryTemplate())
683 return false;
684
Douglas Gregor90916562009-09-29 18:16:17 +0000685 // C++ [basic.stc.dynamic.deallocation]p2:
686 // If a class T has a member deallocation function named operator delete
687 // with exactly one parameter, then that function is a usual (non-placement)
688 // deallocation function. [...]
689 if (getNumParams() == 1)
690 return true;
691
692 // C++ [basic.stc.dynamic.deallocation]p2:
693 // [...] If class T does not declare such an operator delete but does
694 // declare a member deallocation function named operator delete with
695 // exactly two parameters, the second of which has type std::size_t (18.1),
696 // then this function is a usual deallocation function.
697 ASTContext &Context = getASTContext();
698 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000699 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
700 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000701 return false;
702
703 // This function is a usual deallocation function if there are no
704 // single-parameter deallocation functions of the same kind.
705 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
706 R.first != R.second; ++R.first) {
707 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
708 if (FD->getNumParams() == 1)
709 return false;
710 }
711
712 return true;
713}
714
Douglas Gregor06a9f362010-05-01 20:49:11 +0000715bool CXXMethodDecl::isCopyAssignmentOperator() const {
716 // C++0x [class.copy]p19:
717 // A user-declared copy assignment operator X::operator= is a non-static
718 // non-template member function of class X with exactly one parameter of
719 // type X, X&, const X&, volatile X& or const volatile X&.
720 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
721 /*non-static*/ isStatic() ||
722 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
723 /*exactly one parameter*/getNumParams() != 1)
724 return false;
725
726 QualType ParamType = getParamDecl(0)->getType();
727 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
728 ParamType = Ref->getPointeeType();
729
730 ASTContext &Context = getASTContext();
731 QualType ClassType
732 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
733 return Context.hasSameUnqualifiedType(ClassType, ParamType);
734}
735
Anders Carlsson05eb2442009-05-16 23:58:37 +0000736void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000737 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000738 assert(!MD->getParent()->isDependentContext() &&
739 "Can't add an overridden method to a class template!");
740
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000741 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000742}
743
744CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000745 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000746}
747
748CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000749 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000750}
751
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000752QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000753 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
754 // If the member function is declared const, the type of this is const X*,
755 // if the member function is declared volatile, the type of this is
756 // volatile X*, and if the member function is declared const volatile,
757 // the type of this is const volatile X*.
758
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000759 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000760
John McCall3cb0ebd2010-03-10 03:28:59 +0000761 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000762 ClassTy = C.getQualifiedType(ClassTy,
763 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000764 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000765}
766
Eli Friedmand7d7f672009-12-06 20:50:05 +0000767bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000768 // If this function is a template instantiation, look at the template from
769 // which it was instantiated.
770 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
771 if (!CheckFn)
772 CheckFn = this;
773
Eli Friedmand7d7f672009-12-06 20:50:05 +0000774 const FunctionDecl *fn;
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000775 return CheckFn->getBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000776}
777
Douglas Gregor7ad83902008-11-05 04:29:56 +0000778CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000779CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +0000780 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000781 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000782 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
783 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
784 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000785{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000786}
787
788CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000789CXXBaseOrMemberInitializer(ASTContext &Context,
790 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000791 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000792 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
793 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
794 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000795{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000796}
797
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000798CXXBaseOrMemberInitializer::
799CXXBaseOrMemberInitializer(ASTContext &Context,
800 FieldDecl *Member, SourceLocation MemberLoc,
801 SourceLocation L, Expr *Init, SourceLocation R,
802 VarDecl **Indices,
803 unsigned NumIndices)
804 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000805 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
806 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000807{
808 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
809 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
810}
811
812CXXBaseOrMemberInitializer *
813CXXBaseOrMemberInitializer::Create(ASTContext &Context,
814 FieldDecl *Member,
815 SourceLocation MemberLoc,
816 SourceLocation L,
817 Expr *Init,
818 SourceLocation R,
819 VarDecl **Indices,
820 unsigned NumIndices) {
821 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
822 sizeof(VarDecl *) * NumIndices,
823 llvm::alignof<CXXBaseOrMemberInitializer>());
824 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
825 L, Init, R, Indices, NumIndices);
826}
827
Douglas Gregor802ab452009-12-02 22:36:29 +0000828void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000829 if (Init)
830 Init->Destroy(Context);
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000831 // FIXME: Destroy indices
Douglas Gregor802ab452009-12-02 22:36:29 +0000832 this->~CXXBaseOrMemberInitializer();
833}
834
835TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
836 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000837 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000838 else
839 return TypeLoc();
840}
841
842Type *CXXBaseOrMemberInitializer::getBaseClass() {
843 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000844 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000845 else
846 return 0;
847}
848
849const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
850 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000851 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000852 else
853 return 0;
854}
855
856SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
857 if (isMemberInitializer())
858 return getMemberLocation();
859
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000860 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +0000861}
862
863SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
864 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000865}
866
Douglas Gregorb48fe382008-10-31 09:07:45 +0000867CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000868CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
869 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationName(),
870 QualType(), 0, false, false, false);
871}
872
873CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +0000874CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000875 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000876 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000877 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000878 bool isInline,
879 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000880 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
881 "Name must refer to a constructor");
Douglas Gregor16573fa2010-04-19 22:54:31 +0000882 return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit,
883 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000884}
885
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000886bool CXXConstructorDecl::isDefaultConstructor() const {
887 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000888 // A default constructor for a class X is a constructor of class
889 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000890 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000891 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000892}
893
Mike Stump1eb44332009-09-09 15:08:12 +0000894bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000895CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000896 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000897 // A non-template constructor for class X is a copy constructor
898 // if its first parameter is of type X&, const X&, volatile X& or
899 // const volatile X&, and either there are no other parameters
900 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000901 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000902 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000903 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000904 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000905 return false;
906
907 const ParmVarDecl *Param = getParamDecl(0);
908
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000909 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000910 const LValueReferenceType *ParamRefType =
911 Param->getType()->getAs<LValueReferenceType>();
912 if (!ParamRefType)
913 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000914
Douglas Gregorfd476482009-11-13 23:59:09 +0000915 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000916 ASTContext &Context = getASTContext();
917
Douglas Gregorfd476482009-11-13 23:59:09 +0000918 CanQualType PointeeType
919 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000920 CanQualType ClassTy
921 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000922 if (PointeeType.getUnqualifiedType() != ClassTy)
923 return false;
924
John McCall0953e762009-09-24 19:53:00 +0000925 // FIXME: other qualifiers?
926
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000927 // We have a copy constructor.
928 TypeQuals = PointeeType.getCVRQualifiers();
929 return true;
930}
931
Anders Carlssonfaccd722009-08-28 16:57:08 +0000932bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000933 // C++ [class.conv.ctor]p1:
934 // A constructor declared without the function-specifier explicit
935 // that can be called with a single parameter specifies a
936 // conversion from the type of its first parameter to the type of
937 // its class. Such a constructor is called a converting
938 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000939 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000940 return false;
941
Mike Stump1eb44332009-09-09 15:08:12 +0000942 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000943 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000944 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000945 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000946}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000947
Douglas Gregor66724ea2009-11-14 01:20:54 +0000948bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
949 if ((getNumParams() < 1) ||
950 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
951 (getPrimaryTemplate() == 0) ||
952 (getDescribedFunctionTemplate() != 0))
953 return false;
954
955 const ParmVarDecl *Param = getParamDecl(0);
956
957 ASTContext &Context = getASTContext();
958 CanQualType ParamType = Context.getCanonicalType(Param->getType());
959
960 // Strip off the lvalue reference, if any.
961 if (CanQual<LValueReferenceType> ParamRefType
962 = ParamType->getAs<LValueReferenceType>())
963 ParamType = ParamRefType->getPointeeType();
964
965
966 // Is it the same as our our class type?
967 CanQualType ClassTy
968 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
969 if (ParamType.getUnqualifiedType() != ClassTy)
970 return false;
971
972 return true;
973}
974
Douglas Gregor42a552f2008-11-05 20:51:48 +0000975CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000976CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
977 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationName(),
978 QualType(), false, false);
979}
980
981CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +0000982CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000983 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000984 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000985 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000986 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
987 "Name must refer to a destructor");
Douglas Gregor16573fa2010-04-19 22:54:31 +0000988 return new (C) CXXDestructorDecl(RD, L, N, T, isInline, isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000989}
990
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000991void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000992CXXConstructorDecl::Destroy(ASTContext& C) {
993 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000994 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000995}
996
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000997CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000998CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
999 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationName(),
1000 QualType(), 0, false, false);
1001}
1002
1003CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001004CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001005 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +00001006 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001007 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001008 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
1009 "Name must refer to a conversion function");
John McCalla93c9342009-12-07 02:54:59 +00001010 return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001011}
1012
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001013LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001014 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001015 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +00001016 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +00001017 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001018}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001019
1020UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1021 SourceLocation L,
1022 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001023 SourceRange QualifierRange,
1024 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001025 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001026 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001027 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001028 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1029 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001030 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001031 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001032}
1033
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001034NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1035 if (NamespaceAliasDecl *NA =
1036 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1037 return NA->getNamespace();
1038 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1039}
1040
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001041void UsingDirectiveDecl::setNominatedNamespace(NamedDecl* ND) {
1042 assert((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) &&
1043 "expected a NamespaceDecl or NamespaceAliasDecl");
1044 NominatedNamespace = ND;
1045}
1046
Mike Stump1eb44332009-09-09 15:08:12 +00001047NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
1048 SourceLocation L,
1049 SourceLocation AliasLoc,
1050 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001051 SourceRange QualifierRange,
1052 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001053 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001054 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001055 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1056 Namespace = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001057 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001058 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001059}
1060
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001061UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
John McCall9488ea12009-11-17 05:59:44 +00001062 SourceLocation L, SourceRange NNR, SourceLocation UL,
1063 NestedNameSpecifier* TargetNNS, DeclarationName Name,
1064 bool IsTypeNameArg) {
1065 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001066}
1067
John McCall7ba107a2009-11-18 02:36:19 +00001068UnresolvedUsingValueDecl *
1069UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1070 SourceLocation UsingLoc,
1071 SourceRange TargetNNR,
1072 NestedNameSpecifier *TargetNNS,
1073 SourceLocation TargetNameLoc,
1074 DeclarationName TargetName) {
1075 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
1076 TargetNNR, TargetNNS,
1077 TargetNameLoc, TargetName);
1078}
1079
1080UnresolvedUsingTypenameDecl *
1081UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1082 SourceLocation UsingLoc,
1083 SourceLocation TypenameLoc,
1084 SourceRange TargetNNR,
1085 NestedNameSpecifier *TargetNNS,
1086 SourceLocation TargetNameLoc,
1087 DeclarationName TargetName) {
1088 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1089 TargetNNR, TargetNNS,
1090 TargetNameLoc,
1091 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001092}
1093
Anders Carlssonfb311762009-03-14 00:25:26 +00001094StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1095 SourceLocation L, Expr *AssertExpr,
1096 StringLiteral *Message) {
1097 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1098}
1099
1100void StaticAssertDecl::Destroy(ASTContext& C) {
1101 AssertExpr->Destroy(C);
1102 Message->Destroy(C);
John McCallb6217662010-03-15 10:12:16 +00001103 Decl::Destroy(C);
Anders Carlssonfb311762009-03-14 00:25:26 +00001104}
1105
1106StaticAssertDecl::~StaticAssertDecl() {
1107}
1108
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001109static const char *getAccessName(AccessSpecifier AS) {
1110 switch (AS) {
1111 default:
1112 case AS_none:
1113 assert("Invalid access specifier!");
1114 return 0;
1115 case AS_public:
1116 return "public";
1117 case AS_private:
1118 return "private";
1119 case AS_protected:
1120 return "protected";
1121 }
1122}
1123
1124const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1125 AccessSpecifier AS) {
1126 return DB << getAccessName(AS);
1127}
1128
1129