blob: ab8a20994ee15ca804eb4c4b1ddc97b9efbcfce8 [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
Mike Stump1eb44332009-09-09 15:08:12 +0000162CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000163 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000164 QualType ClassType
165 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000166 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000167 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000168 Context.getCanonicalType(ClassType));
169 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000170 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000171 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000172 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000173 // C++ [class.copy]p2:
174 // A non-template constructor for class X is a copy constructor if [...]
175 if (isa<FunctionTemplateDecl>(*Con))
176 continue;
177
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000178 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000179 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
180 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000181 return cast<CXXConstructorDecl>(*Con);
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000183 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000184 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000185 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000186}
187
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000188bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
189 const CXXMethodDecl *& MD) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000190 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
191 const_cast<CXXRecordDecl*>(this)));
192 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
193
194 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000195 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000196 Op != OpEnd; ++Op) {
197 // C++ [class.copy]p9:
198 // A user-declared copy assignment operator is a non-static non-template
199 // member function of class X with exactly one parameter of type X, X&,
200 // const X&, volatile X& or const volatile X&.
Douglas Gregor682054c2009-10-30 22:48:49 +0000201 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
202 if (!Method)
203 continue;
204
Sebastian Redl64b45f72009-01-05 20:52:13 +0000205 if (Method->isStatic())
206 continue;
Douglas Gregor77da3f42009-10-13 23:45:19 +0000207 if (Method->getPrimaryTemplate())
208 continue;
Douglas Gregor72564e72009-02-26 23:50:07 +0000209 const FunctionProtoType *FnType =
John McCall183700f2009-09-21 23:43:11 +0000210 Method->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000211 assert(FnType && "Overloaded operator has no prototype.");
212 // Don't assert on this; an invalid decl might have been left in the AST.
213 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
214 continue;
215 bool AcceptsConst = true;
216 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000217 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000218 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000219 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000220 if (!ArgType.isConstQualified())
221 AcceptsConst = false;
222 }
Douglas Gregora4923eb2009-11-16 21:35:15 +0000223 if (!Context.hasSameUnqualifiedType(ArgType, ClassType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000224 continue;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000225 MD = Method;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000226 // We have a single argument of type cv X or cv X&, i.e. we've found the
227 // copy assignment operator. Return whether it accepts const arguments.
228 return AcceptsConst;
229 }
230 assert(isInvalidDecl() &&
231 "No copy assignment operator declared in valid code.");
232 return false;
233}
234
Douglas Gregorb87786f2010-07-01 17:48:08 +0000235/// \brief Perform a simplistic form of overload resolution that only considers
236/// cv-qualifiers on a single parameter, and return the best overload candidate
237/// (if there is one).
238static CXXMethodDecl *
239GetBestOverloadCandidateSimple(
240 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
241 if (Cands.empty())
242 return 0;
243 if (Cands.size() == 1)
244 return Cands[0].first;
245
246 unsigned Best = 0, N = Cands.size();
247 for (unsigned I = 1; I != N; ++I)
248 if (Cands[Best].second.isSupersetOf(Cands[I].second))
249 Best = I;
250
251 for (unsigned I = 1; I != N; ++I)
252 if (Cands[Best].second.isSupersetOf(Cands[I].second))
253 return 0;
254
255 return Cands[Best].first;
256}
257
258CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
259 ASTContext &Context = getASTContext();
260 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
261 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
262
263 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
264 DeclContext::lookup_const_iterator Op, OpEnd;
265 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
266 // C++ [class.copy]p9:
267 // A user-declared copy assignment operator is a non-static non-template
268 // member function of class X with exactly one parameter of type X, X&,
269 // const X&, volatile X& or const volatile X&.
270 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
271 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
272 continue;
273
274 const FunctionProtoType *FnType
275 = Method->getType()->getAs<FunctionProtoType>();
276 assert(FnType && "Overloaded operator has no prototype.");
277 // Don't assert on this; an invalid decl might have been left in the AST.
278 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
279 continue;
280
281 QualType ArgType = FnType->getArgType(0);
282 Qualifiers Quals;
283 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
284 ArgType = Ref->getPointeeType();
285 // If we have a const argument and we have a reference to a non-const,
286 // this function does not match.
287 if (ArgIsConst && !ArgType.isConstQualified())
288 continue;
289
290 Quals = ArgType.getQualifiers();
291 } else {
292 // By-value copy-assignment operators are treated like const X&
293 // copy-assignment operators.
294 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
295 }
296
297 if (!Context.hasSameUnqualifiedType(ArgType, Class))
298 continue;
299
300 // Save this copy-assignment operator. It might be "the one".
301 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
302 }
303
304 // Use a simplistic form of overload resolution to find the candidate.
305 return GetBestOverloadCandidateSimple(Found);
306}
307
Sebastian Redl64b45f72009-01-05 20:52:13 +0000308void
Mike Stump1eb44332009-09-09 15:08:12 +0000309CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000310 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000311 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
312 // Note that we have a user-declared constructor.
John McCall86ff3082010-02-04 22:26:26 +0000313 data().UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000314
Mike Stump1eb44332009-09-09 15:08:12 +0000315 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000316 // An aggregate is an array or a class (clause 9) with no
317 // user-declared constructors (12.1) [...].
John McCall86ff3082010-02-04 22:26:26 +0000318 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000319
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000320 // C++ [class]p4:
321 // A POD-struct is an aggregate class [...]
John McCall86ff3082010-02-04 22:26:26 +0000322 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000323
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000324 // C++ [class.ctor]p5:
325 // A constructor is trivial if it is an implicitly-declared default
326 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000327 // FIXME: C++0x: don't do this for "= default" default constructors.
John McCall86ff3082010-02-04 22:26:26 +0000328 data().HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000330 // Note when we have a user-declared copy constructor, which will
331 // suppress the implicit declaration of a copy constructor.
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000332 if (ConDecl->isCopyConstructor()) {
John McCall86ff3082010-02-04 22:26:26 +0000333 data().UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000334
335 // C++ [class.copy]p6:
336 // A copy constructor is trivial if it is implicitly declared.
337 // FIXME: C++0x: don't do this for "= default" copy constructors.
John McCall86ff3082010-02-04 22:26:26 +0000338 data().HasTrivialCopyConstructor = false;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000339 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000340}
341
Sebastian Redl64b45f72009-01-05 20:52:13 +0000342void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
343 CXXMethodDecl *OpDecl) {
344 // We're interested specifically in copy assignment operators.
John McCall183700f2009-09-21 23:43:11 +0000345 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000346 assert(FnType && "Overloaded operator has no proto function type.");
347 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregor77da3f42009-10-13 23:45:19 +0000348
349 // Copy assignment operators must be non-templates.
350 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
351 return;
352
Sebastian Redl64b45f72009-01-05 20:52:13 +0000353 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000354 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000355 ArgType = Ref->getPointeeType();
356
357 ArgType = ArgType.getUnqualifiedType();
358 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
359 const_cast<CXXRecordDecl*>(this)));
360
Douglas Gregora4923eb2009-11-16 21:35:15 +0000361 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000362 return;
363
364 // This is a copy assignment operator.
Eli Friedman88fad632009-11-07 00:02:45 +0000365 // Note on the decl that it is a copy assignment operator.
366 OpDecl->setCopyAssignment(true);
367
Sebastian Redl64b45f72009-01-05 20:52:13 +0000368 // Suppress the implicit declaration of a copy constructor.
John McCall86ff3082010-02-04 22:26:26 +0000369 data().UserDeclaredCopyAssignment = true;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000370
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000371 // C++ [class.copy]p11:
372 // A copy assignment operator is trivial if it is implicitly declared.
373 // FIXME: C++0x: don't do this for "= default" copy operators.
John McCall86ff3082010-02-04 22:26:26 +0000374 data().HasTrivialCopyAssignment = false;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000375
Sebastian Redl64b45f72009-01-05 20:52:13 +0000376 // C++ [class]p4:
377 // A POD-struct is an aggregate class that [...] has no user-defined copy
378 // assignment operator [...].
John McCall86ff3082010-02-04 22:26:26 +0000379 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000380}
381
John McCallb05b5f32010-03-15 09:07:48 +0000382static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
383 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000384 if (isa<UsingShadowDecl>(Conv))
385 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000386 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
387 T = ConvTemp->getTemplatedDecl()->getResultType();
388 else
389 T = cast<CXXConversionDecl>(Conv)->getConversionType();
390 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000391}
392
John McCallb05b5f32010-03-15 09:07:48 +0000393/// Collect the visible conversions of a base class.
394///
395/// \param Base a base class of the class we're considering
396/// \param InVirtual whether this base class is a virtual base (or a base
397/// of a virtual base)
398/// \param Access the access along the inheritance path to this base
399/// \param ParentHiddenTypes the conversions provided by the inheritors
400/// of this base
401/// \param Output the set to which to add conversions from non-virtual bases
402/// \param VOutput the set to which to add conversions from virtual bases
403/// \param HiddenVBaseCs the set of conversions which were hidden in a
404/// virtual base along some inheritance path
405static void CollectVisibleConversions(ASTContext &Context,
406 CXXRecordDecl *Record,
407 bool InVirtual,
408 AccessSpecifier Access,
409 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
410 UnresolvedSetImpl &Output,
411 UnresolvedSetImpl &VOutput,
412 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
413 // The set of types which have conversions in this class or its
414 // subclasses. As an optimization, we don't copy the derived set
415 // unless it might change.
416 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
417 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
418
419 // Collect the direct conversions and figure out which conversions
420 // will be hidden in the subclasses.
421 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
422 if (!Cs.empty()) {
423 HiddenTypesBuffer = ParentHiddenTypes;
424 HiddenTypes = &HiddenTypesBuffer;
425
426 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
427 bool Hidden =
428 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
429
430 // If this conversion is hidden and we're in a virtual base,
431 // remember that it's hidden along some inheritance path.
432 if (Hidden && InVirtual)
433 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
434
435 // If this conversion isn't hidden, add it to the appropriate output.
436 else if (!Hidden) {
437 AccessSpecifier IAccess
438 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
439
440 if (InVirtual)
441 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000442 else
John McCallb05b5f32010-03-15 09:07:48 +0000443 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000444 }
445 }
446 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000447
John McCallb05b5f32010-03-15 09:07:48 +0000448 // Collect information recursively from any base classes.
449 for (CXXRecordDecl::base_class_iterator
450 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
451 const RecordType *RT = I->getType()->getAs<RecordType>();
452 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000453
John McCallb05b5f32010-03-15 09:07:48 +0000454 AccessSpecifier BaseAccess
455 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
456 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000457
John McCallb05b5f32010-03-15 09:07:48 +0000458 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
459 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
460 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000461 }
John McCallb05b5f32010-03-15 09:07:48 +0000462}
Sebastian Redl9994a342009-10-25 17:03:50 +0000463
John McCallb05b5f32010-03-15 09:07:48 +0000464/// Collect the visible conversions of a class.
465///
466/// This would be extremely straightforward if it weren't for virtual
467/// bases. It might be worth special-casing that, really.
468static void CollectVisibleConversions(ASTContext &Context,
469 CXXRecordDecl *Record,
470 UnresolvedSetImpl &Output) {
471 // The collection of all conversions in virtual bases that we've
472 // found. These will be added to the output as long as they don't
473 // appear in the hidden-conversions set.
474 UnresolvedSet<8> VBaseCs;
475
476 // The set of conversions in virtual bases that we've determined to
477 // be hidden.
478 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
479
480 // The set of types hidden by classes derived from this one.
481 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
482
483 // Go ahead and collect the direct conversions and add them to the
484 // hidden-types set.
485 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
486 Output.append(Cs.begin(), Cs.end());
487 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
488 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
489
490 // Recursively collect conversions from base classes.
491 for (CXXRecordDecl::base_class_iterator
492 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
493 const RecordType *RT = I->getType()->getAs<RecordType>();
494 if (!RT) continue;
495
496 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
497 I->isVirtual(), I->getAccessSpecifier(),
498 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
499 }
500
501 // Add any unhidden conversions provided by virtual bases.
502 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
503 I != E; ++I) {
504 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
505 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000506 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000507}
508
509/// getVisibleConversionFunctions - get all conversion functions visible
510/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000511const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000512 // If root class, all conversions are visible.
513 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000514 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000515 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000516 if (data().ComputedVisibleConversions)
517 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000518 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000519 data().ComputedVisibleConversions = true;
520 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000521}
522
John McCall32daa422010-03-31 01:36:47 +0000523#ifndef NDEBUG
524void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000525 assert(ConvDecl->getDeclContext() == this &&
526 "conversion function does not belong to this record");
527
John McCall32daa422010-03-31 01:36:47 +0000528 ConvDecl = ConvDecl->getUnderlyingDecl();
529 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
530 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
531 } else {
532 assert(isa<CXXConversionDecl>(ConvDecl));
533 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000534}
John McCall32daa422010-03-31 01:36:47 +0000535#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000536
John McCall32daa422010-03-31 01:36:47 +0000537void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
538 // This operation is O(N) but extremely rare. Sema only uses it to
539 // remove UsingShadowDecls in a class that were followed by a direct
540 // declaration, e.g.:
541 // class A : B {
542 // using B::operator int;
543 // operator int();
544 // };
545 // This is uncommon by itself and even more uncommon in conjunction
546 // with sufficiently large numbers of directly-declared conversions
547 // that asymptotic behavior matters.
548
549 UnresolvedSetImpl &Convs = *getConversionFunctions();
550 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
551 if (Convs[I].getDecl() == ConvDecl) {
552 Convs.erase(I);
553 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
554 && "conversion was found multiple times in unresolved set");
555 return;
556 }
557 }
558
559 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000560}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000561
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000562void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
563 Method->setVirtualAsWritten(true);
564 setAggregate(false);
565 setPOD(false);
566 setEmpty(false);
567 setPolymorphic(true);
568 setHasTrivialConstructor(false);
569 setHasTrivialCopyConstructor(false);
570 setHasTrivialCopyAssignment(false);
571}
572
Douglas Gregorf6b11852009-10-08 15:14:33 +0000573CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000574 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000575 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
576
577 return 0;
578}
579
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000580MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
581 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
582}
583
Douglas Gregorf6b11852009-10-08 15:14:33 +0000584void
585CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
586 TemplateSpecializationKind TSK) {
587 assert(TemplateOrInstantiation.isNull() &&
588 "Previous template or instantiation?");
589 assert(!isa<ClassTemplateSpecializationDecl>(this));
590 TemplateOrInstantiation
591 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
592}
593
Anders Carlssonb13e3572009-12-07 06:33:48 +0000594TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
595 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000596 = dyn_cast<ClassTemplateSpecializationDecl>(this))
597 return Spec->getSpecializationKind();
598
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000599 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000600 return MSInfo->getTemplateSpecializationKind();
601
602 return TSK_Undeclared;
603}
604
605void
606CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
607 if (ClassTemplateSpecializationDecl *Spec
608 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
609 Spec->setSpecializationKind(TSK);
610 return;
611 }
612
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000613 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000614 MSInfo->setTemplateSpecializationKind(TSK);
615 return;
616 }
617
618 assert(false && "Not a class template or member class specialization");
619}
620
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000621CXXConstructorDecl *
622CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
623 QualType ClassType = Context.getTypeDeclType(this);
624 DeclarationName ConstructorName
625 = Context.DeclarationNames.getCXXConstructorName(
626 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000628 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000629 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000630 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000631 // FIXME: In C++0x, a constructor template can be a default constructor.
632 if (isa<FunctionTemplateDecl>(*Con))
633 continue;
634
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000635 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
636 if (Constructor->isDefaultConstructor())
637 return Constructor;
638 }
639 return 0;
640}
641
Douglas Gregor1d110e02010-07-01 14:13:13 +0000642CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
643 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000644 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000645
646 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000647 = Context.DeclarationNames.getCXXDestructorName(
648 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000649
John McCallc0bf4622010-02-23 00:48:20 +0000650 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000651 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000652 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000654 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000655 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Anders Carlsson7267c162009-05-29 21:03:38 +0000657 return Dtor;
658}
659
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000660CXXMethodDecl *
661CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000662 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000663 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000664 bool isStatic, StorageClass SCAsWritten, bool isInline) {
John McCalla93c9342009-12-07 02:54:59 +0000665 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000666 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000667}
668
Douglas Gregor90916562009-09-29 18:16:17 +0000669bool CXXMethodDecl::isUsualDeallocationFunction() const {
670 if (getOverloadedOperator() != OO_Delete &&
671 getOverloadedOperator() != OO_Array_Delete)
672 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000673
674 // C++ [basic.stc.dynamic.deallocation]p2:
675 // A template instance is never a usual deallocation function,
676 // regardless of its signature.
677 if (getPrimaryTemplate())
678 return false;
679
Douglas Gregor90916562009-09-29 18:16:17 +0000680 // C++ [basic.stc.dynamic.deallocation]p2:
681 // If a class T has a member deallocation function named operator delete
682 // with exactly one parameter, then that function is a usual (non-placement)
683 // deallocation function. [...]
684 if (getNumParams() == 1)
685 return true;
686
687 // C++ [basic.stc.dynamic.deallocation]p2:
688 // [...] If class T does not declare such an operator delete but does
689 // declare a member deallocation function named operator delete with
690 // exactly two parameters, the second of which has type std::size_t (18.1),
691 // then this function is a usual deallocation function.
692 ASTContext &Context = getASTContext();
693 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000694 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
695 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000696 return false;
697
698 // This function is a usual deallocation function if there are no
699 // single-parameter deallocation functions of the same kind.
700 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
701 R.first != R.second; ++R.first) {
702 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
703 if (FD->getNumParams() == 1)
704 return false;
705 }
706
707 return true;
708}
709
Douglas Gregor06a9f362010-05-01 20:49:11 +0000710bool CXXMethodDecl::isCopyAssignmentOperator() const {
711 // C++0x [class.copy]p19:
712 // A user-declared copy assignment operator X::operator= is a non-static
713 // non-template member function of class X with exactly one parameter of
714 // type X, X&, const X&, volatile X& or const volatile X&.
715 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
716 /*non-static*/ isStatic() ||
717 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
718 /*exactly one parameter*/getNumParams() != 1)
719 return false;
720
721 QualType ParamType = getParamDecl(0)->getType();
722 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
723 ParamType = Ref->getPointeeType();
724
725 ASTContext &Context = getASTContext();
726 QualType ClassType
727 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
728 return Context.hasSameUnqualifiedType(ClassType, ParamType);
729}
730
Anders Carlsson05eb2442009-05-16 23:58:37 +0000731void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000732 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000733 assert(!MD->getParent()->isDependentContext() &&
734 "Can't add an overridden method to a class template!");
735
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000736 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000737}
738
739CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000740 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000741}
742
743CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000744 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000745}
746
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000747QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000748 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
749 // If the member function is declared const, the type of this is const X*,
750 // if the member function is declared volatile, the type of this is
751 // volatile X*, and if the member function is declared const volatile,
752 // the type of this is const volatile X*.
753
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000754 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000755
John McCall3cb0ebd2010-03-10 03:28:59 +0000756 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000757 ClassTy = C.getQualifiedType(ClassTy,
758 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000759 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000760}
761
Eli Friedmand7d7f672009-12-06 20:50:05 +0000762bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000763 // If this function is a template instantiation, look at the template from
764 // which it was instantiated.
765 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
766 if (!CheckFn)
767 CheckFn = this;
768
Eli Friedmand7d7f672009-12-06 20:50:05 +0000769 const FunctionDecl *fn;
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000770 return CheckFn->getBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000771}
772
Douglas Gregor7ad83902008-11-05 04:29:56 +0000773CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000774CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +0000775 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000776 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000777 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
778 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
779 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000780{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000781}
782
783CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000784CXXBaseOrMemberInitializer(ASTContext &Context,
785 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000786 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000787 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
788 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
789 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000790{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000791}
792
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000793CXXBaseOrMemberInitializer::
794CXXBaseOrMemberInitializer(ASTContext &Context,
795 FieldDecl *Member, SourceLocation MemberLoc,
796 SourceLocation L, Expr *Init, SourceLocation R,
797 VarDecl **Indices,
798 unsigned NumIndices)
799 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000800 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
801 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000802{
803 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
804 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
805}
806
807CXXBaseOrMemberInitializer *
808CXXBaseOrMemberInitializer::Create(ASTContext &Context,
809 FieldDecl *Member,
810 SourceLocation MemberLoc,
811 SourceLocation L,
812 Expr *Init,
813 SourceLocation R,
814 VarDecl **Indices,
815 unsigned NumIndices) {
816 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
817 sizeof(VarDecl *) * NumIndices,
818 llvm::alignof<CXXBaseOrMemberInitializer>());
819 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
820 L, Init, R, Indices, NumIndices);
821}
822
Douglas Gregor802ab452009-12-02 22:36:29 +0000823void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000824 if (Init)
825 Init->Destroy(Context);
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000826 // FIXME: Destroy indices
Douglas Gregor802ab452009-12-02 22:36:29 +0000827 this->~CXXBaseOrMemberInitializer();
828}
829
830TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
831 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000832 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000833 else
834 return TypeLoc();
835}
836
837Type *CXXBaseOrMemberInitializer::getBaseClass() {
838 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000839 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000840 else
841 return 0;
842}
843
844const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
845 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000846 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000847 else
848 return 0;
849}
850
851SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
852 if (isMemberInitializer())
853 return getMemberLocation();
854
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000855 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +0000856}
857
858SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
859 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000860}
861
Douglas Gregorb48fe382008-10-31 09:07:45 +0000862CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000863CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
864 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationName(),
865 QualType(), 0, false, false, false);
866}
867
868CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +0000869CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000870 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000871 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000872 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000873 bool isInline,
874 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000875 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
876 "Name must refer to a constructor");
Douglas Gregor16573fa2010-04-19 22:54:31 +0000877 return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit,
878 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000879}
880
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000881bool CXXConstructorDecl::isDefaultConstructor() const {
882 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000883 // A default constructor for a class X is a constructor of class
884 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000885 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000886 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000887}
888
Mike Stump1eb44332009-09-09 15:08:12 +0000889bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000890CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000891 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000892 // A non-template constructor for class X is a copy constructor
893 // if its first parameter is of type X&, const X&, volatile X& or
894 // const volatile X&, and either there are no other parameters
895 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000896 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000897 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000898 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000899 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000900 return false;
901
902 const ParmVarDecl *Param = getParamDecl(0);
903
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000904 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000905 const LValueReferenceType *ParamRefType =
906 Param->getType()->getAs<LValueReferenceType>();
907 if (!ParamRefType)
908 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000909
Douglas Gregorfd476482009-11-13 23:59:09 +0000910 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000911 ASTContext &Context = getASTContext();
912
Douglas Gregorfd476482009-11-13 23:59:09 +0000913 CanQualType PointeeType
914 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000915 CanQualType ClassTy
916 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000917 if (PointeeType.getUnqualifiedType() != ClassTy)
918 return false;
919
John McCall0953e762009-09-24 19:53:00 +0000920 // FIXME: other qualifiers?
921
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000922 // We have a copy constructor.
923 TypeQuals = PointeeType.getCVRQualifiers();
924 return true;
925}
926
Anders Carlssonfaccd722009-08-28 16:57:08 +0000927bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000928 // C++ [class.conv.ctor]p1:
929 // A constructor declared without the function-specifier explicit
930 // that can be called with a single parameter specifies a
931 // conversion from the type of its first parameter to the type of
932 // its class. Such a constructor is called a converting
933 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000934 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000935 return false;
936
Mike Stump1eb44332009-09-09 15:08:12 +0000937 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000938 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000939 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000940 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000941}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000942
Douglas Gregor66724ea2009-11-14 01:20:54 +0000943bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
944 if ((getNumParams() < 1) ||
945 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
946 (getPrimaryTemplate() == 0) ||
947 (getDescribedFunctionTemplate() != 0))
948 return false;
949
950 const ParmVarDecl *Param = getParamDecl(0);
951
952 ASTContext &Context = getASTContext();
953 CanQualType ParamType = Context.getCanonicalType(Param->getType());
954
955 // Strip off the lvalue reference, if any.
956 if (CanQual<LValueReferenceType> ParamRefType
957 = ParamType->getAs<LValueReferenceType>())
958 ParamType = ParamRefType->getPointeeType();
959
960
961 // Is it the same as our our class type?
962 CanQualType ClassTy
963 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
964 if (ParamType.getUnqualifiedType() != ClassTy)
965 return false;
966
967 return true;
968}
969
Douglas Gregor42a552f2008-11-05 20:51:48 +0000970CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000971CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
972 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationName(),
973 QualType(), false, false);
974}
975
976CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +0000977CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000978 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000979 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000980 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000981 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
982 "Name must refer to a destructor");
Douglas Gregor16573fa2010-04-19 22:54:31 +0000983 return new (C) CXXDestructorDecl(RD, L, N, T, isInline, isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000984}
985
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000986void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000987CXXConstructorDecl::Destroy(ASTContext& C) {
988 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000989 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000990}
991
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000992CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000993CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
994 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationName(),
995 QualType(), 0, false, false);
996}
997
998CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000999CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001000 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +00001001 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001002 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001003 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
1004 "Name must refer to a conversion function");
John McCalla93c9342009-12-07 02:54:59 +00001005 return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001006}
1007
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001008LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001009 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001010 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +00001011 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +00001012 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001013}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001014
1015UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1016 SourceLocation L,
1017 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001018 SourceRange QualifierRange,
1019 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001020 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001021 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001022 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001023 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1024 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001025 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001026 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001027}
1028
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001029NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1030 if (NamespaceAliasDecl *NA =
1031 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1032 return NA->getNamespace();
1033 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1034}
1035
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001036void UsingDirectiveDecl::setNominatedNamespace(NamedDecl* ND) {
1037 assert((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) &&
1038 "expected a NamespaceDecl or NamespaceAliasDecl");
1039 NominatedNamespace = ND;
1040}
1041
Mike Stump1eb44332009-09-09 15:08:12 +00001042NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
1043 SourceLocation L,
1044 SourceLocation AliasLoc,
1045 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001046 SourceRange QualifierRange,
1047 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001048 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001049 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001050 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1051 Namespace = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001052 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001053 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001054}
1055
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001056UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
John McCall9488ea12009-11-17 05:59:44 +00001057 SourceLocation L, SourceRange NNR, SourceLocation UL,
1058 NestedNameSpecifier* TargetNNS, DeclarationName Name,
1059 bool IsTypeNameArg) {
1060 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001061}
1062
John McCall7ba107a2009-11-18 02:36:19 +00001063UnresolvedUsingValueDecl *
1064UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1065 SourceLocation UsingLoc,
1066 SourceRange TargetNNR,
1067 NestedNameSpecifier *TargetNNS,
1068 SourceLocation TargetNameLoc,
1069 DeclarationName TargetName) {
1070 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
1071 TargetNNR, TargetNNS,
1072 TargetNameLoc, TargetName);
1073}
1074
1075UnresolvedUsingTypenameDecl *
1076UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1077 SourceLocation UsingLoc,
1078 SourceLocation TypenameLoc,
1079 SourceRange TargetNNR,
1080 NestedNameSpecifier *TargetNNS,
1081 SourceLocation TargetNameLoc,
1082 DeclarationName TargetName) {
1083 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1084 TargetNNR, TargetNNS,
1085 TargetNameLoc,
1086 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001087}
1088
Anders Carlssonfb311762009-03-14 00:25:26 +00001089StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1090 SourceLocation L, Expr *AssertExpr,
1091 StringLiteral *Message) {
1092 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1093}
1094
1095void StaticAssertDecl::Destroy(ASTContext& C) {
1096 AssertExpr->Destroy(C);
1097 Message->Destroy(C);
John McCallb6217662010-03-15 10:12:16 +00001098 Decl::Destroy(C);
Anders Carlssonfb311762009-03-14 00:25:26 +00001099}
1100
1101StaticAssertDecl::~StaticAssertDecl() {
1102}
1103
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001104static const char *getAccessName(AccessSpecifier AS) {
1105 switch (AS) {
1106 default:
1107 case AS_none:
1108 assert("Invalid access specifier!");
1109 return 0;
1110 case AS_public:
1111 return "public";
1112 case AS_private:
1113 return "private";
1114 case AS_protected:
1115 return "protected";
1116 }
1117}
1118
1119const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1120 AccessSpecifier AS) {
1121 return DB << getAccessName(AS);
1122}
1123
1124