blob: aa4b2dd1d0813515762fd36a2d89a022bf86b5a4 [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 McCall86ff3082010-02-04 22:26:26 +000036 Definition(D) {
37}
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
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000086 int vbaseCount = 0;
87 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
88 bool hasDirectVirtualBase = false;
Mike Stump1eb44332009-09-09 15:08:12 +000089
John McCall86ff3082010-02-04 22:26:26 +000090 data().Bases = new(C) CXXBaseSpecifier [NumBases];
91 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000092 for (unsigned i = 0; i < NumBases; ++i) {
John McCall86ff3082010-02-04 22:26:26 +000093 data().Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000094 // Keep track of inherited vbases for this base class.
95 const CXXBaseSpecifier *Base = Bases[i];
96 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +000097 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000098 if (BaseType->isDependentType())
99 continue;
100 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000101 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000102 if (Base->isVirtual())
103 hasDirectVirtualBase = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000104 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000105 BaseClassDecl->vbases_begin(),
106 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Mike Stump1eb44332009-09-09 15:08:12 +0000107 // Add this vbase to the array of vbases for current class if it is
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000108 // not already in the list.
109 // FIXME. Note that we do a linear search as number of such classes are
110 // very few.
111 int i;
112 for (i = 0; i < vbaseCount; ++i)
113 if (UniqueVbases[i]->getType() == VBase->getType())
114 break;
115 if (i == vbaseCount) {
116 UniqueVbases.push_back(VBase);
117 ++vbaseCount;
118 }
119 }
120 }
121 if (hasDirectVirtualBase) {
122 // Iterate one more time through the direct bases and add the virtual
123 // base to the list of vritual bases for current class.
124 for (unsigned i = 0; i < NumBases; ++i) {
125 const CXXBaseSpecifier *VBase = Bases[i];
126 if (!VBase->isVirtual())
127 continue;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000128 int j;
129 for (j = 0; j < vbaseCount; ++j)
130 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000131 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000132 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000133 UniqueVbases.push_back(VBase);
134 ++vbaseCount;
135 }
136 }
137 }
138 if (vbaseCount > 0) {
139 // build AST for inhireted, direct or indirect, virtual bases.
John McCall86ff3082010-02-04 22:26:26 +0000140 data().VBases = new (C) CXXBaseSpecifier [vbaseCount];
141 data().NumVBases = vbaseCount;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000142 for (int i = 0; i < vbaseCount; i++) {
143 QualType QT = UniqueVbases[i]->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000144 // Skip dependent types; we can't do any checking on them now.
145 if (QT->isDependentType())
146 continue;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000147 CXXRecordDecl *VBaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000148 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000149 data().VBases[i] =
Douglas Gregor2aef06d2009-07-22 20:55:49 +0000150 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
151 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
152 UniqueVbases[i]->getAccessSpecifier(), QT);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000153 }
154 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000155}
156
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000157/// Callback function for CXXRecordDecl::forallBases that acknowledges
158/// that it saw a base class.
159static bool SawBase(const CXXRecordDecl *, void *) {
160 return true;
161}
162
163bool CXXRecordDecl::hasAnyDependentBases() const {
164 if (!isDependentContext())
165 return false;
166
167 return !forallBases(SawBase, 0);
168}
169
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000170bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000171 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000172}
173
Mike Stump1eb44332009-09-09 15:08:12 +0000174CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000175 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000176 QualType ClassType
177 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000178 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000179 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000180 Context.getCanonicalType(ClassType));
181 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000182 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000183 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000184 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000185 // C++ [class.copy]p2:
186 // A non-template constructor for class X is a copy constructor if [...]
187 if (isa<FunctionTemplateDecl>(*Con))
188 continue;
189
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000190 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000191 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
192 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000193 return cast<CXXConstructorDecl>(*Con);
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000195 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000196 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000197 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000198}
199
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000200bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
201 const CXXMethodDecl *& MD) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000202 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
203 const_cast<CXXRecordDecl*>(this)));
204 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
205
206 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000207 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000208 Op != OpEnd; ++Op) {
209 // C++ [class.copy]p9:
210 // A user-declared copy assignment operator is a non-static non-template
211 // member function of class X with exactly one parameter of type X, X&,
212 // const X&, volatile X& or const volatile X&.
Douglas Gregor682054c2009-10-30 22:48:49 +0000213 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
214 if (!Method)
215 continue;
216
Sebastian Redl64b45f72009-01-05 20:52:13 +0000217 if (Method->isStatic())
218 continue;
Douglas Gregor77da3f42009-10-13 23:45:19 +0000219 if (Method->getPrimaryTemplate())
220 continue;
Douglas Gregor72564e72009-02-26 23:50:07 +0000221 const FunctionProtoType *FnType =
John McCall183700f2009-09-21 23:43:11 +0000222 Method->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000223 assert(FnType && "Overloaded operator has no prototype.");
224 // Don't assert on this; an invalid decl might have been left in the AST.
225 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
226 continue;
227 bool AcceptsConst = true;
228 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000229 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000230 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000231 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000232 if (!ArgType.isConstQualified())
233 AcceptsConst = false;
234 }
Douglas Gregora4923eb2009-11-16 21:35:15 +0000235 if (!Context.hasSameUnqualifiedType(ArgType, ClassType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000236 continue;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000237 MD = Method;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000238 // We have a single argument of type cv X or cv X&, i.e. we've found the
239 // copy assignment operator. Return whether it accepts const arguments.
240 return AcceptsConst;
241 }
242 assert(isInvalidDecl() &&
243 "No copy assignment operator declared in valid code.");
244 return false;
245}
246
247void
Mike Stump1eb44332009-09-09 15:08:12 +0000248CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000249 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000250 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
251 // Note that we have a user-declared constructor.
John McCall86ff3082010-02-04 22:26:26 +0000252 data().UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000253
Mike Stump1eb44332009-09-09 15:08:12 +0000254 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000255 // An aggregate is an array or a class (clause 9) with no
256 // user-declared constructors (12.1) [...].
John McCall86ff3082010-02-04 22:26:26 +0000257 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000258
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000259 // C++ [class]p4:
260 // A POD-struct is an aggregate class [...]
John McCall86ff3082010-02-04 22:26:26 +0000261 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000262
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000263 // C++ [class.ctor]p5:
264 // A constructor is trivial if it is an implicitly-declared default
265 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000266 // FIXME: C++0x: don't do this for "= default" default constructors.
John McCall86ff3082010-02-04 22:26:26 +0000267 data().HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000269 // Note when we have a user-declared copy constructor, which will
270 // suppress the implicit declaration of a copy constructor.
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000271 if (ConDecl->isCopyConstructor()) {
John McCall86ff3082010-02-04 22:26:26 +0000272 data().UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000273
274 // C++ [class.copy]p6:
275 // A copy constructor is trivial if it is implicitly declared.
276 // FIXME: C++0x: don't do this for "= default" copy constructors.
John McCall86ff3082010-02-04 22:26:26 +0000277 data().HasTrivialCopyConstructor = false;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000278 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000279}
280
Sebastian Redl64b45f72009-01-05 20:52:13 +0000281void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
282 CXXMethodDecl *OpDecl) {
283 // We're interested specifically in copy assignment operators.
John McCall183700f2009-09-21 23:43:11 +0000284 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000285 assert(FnType && "Overloaded operator has no proto function type.");
286 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregor77da3f42009-10-13 23:45:19 +0000287
288 // Copy assignment operators must be non-templates.
289 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
290 return;
291
Sebastian Redl64b45f72009-01-05 20:52:13 +0000292 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000293 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000294 ArgType = Ref->getPointeeType();
295
296 ArgType = ArgType.getUnqualifiedType();
297 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
298 const_cast<CXXRecordDecl*>(this)));
299
Douglas Gregora4923eb2009-11-16 21:35:15 +0000300 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000301 return;
302
303 // This is a copy assignment operator.
Eli Friedman88fad632009-11-07 00:02:45 +0000304 // Note on the decl that it is a copy assignment operator.
305 OpDecl->setCopyAssignment(true);
306
Sebastian Redl64b45f72009-01-05 20:52:13 +0000307 // Suppress the implicit declaration of a copy constructor.
John McCall86ff3082010-02-04 22:26:26 +0000308 data().UserDeclaredCopyAssignment = true;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000309
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000310 // C++ [class.copy]p11:
311 // A copy assignment operator is trivial if it is implicitly declared.
312 // FIXME: C++0x: don't do this for "= default" copy operators.
John McCall86ff3082010-02-04 22:26:26 +0000313 data().HasTrivialCopyAssignment = false;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000314
Sebastian Redl64b45f72009-01-05 20:52:13 +0000315 // C++ [class]p4:
316 // A POD-struct is an aggregate class that [...] has no user-defined copy
317 // assignment operator [...].
John McCall86ff3082010-02-04 22:26:26 +0000318 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000319}
320
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000321void
322CXXRecordDecl::collectConversionFunctions(
John McCallba135432009-11-21 08:51:07 +0000323 llvm::SmallPtrSet<CanQualType, 8>& ConversionsTypeSet) const
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000324{
John McCalleec51cf2010-01-20 00:46:10 +0000325 const UnresolvedSetImpl *Cs = getConversionFunctions();
326 for (UnresolvedSetImpl::iterator I = Cs->begin(), E = Cs->end();
327 I != E; ++I) {
John McCallba135432009-11-21 08:51:07 +0000328 NamedDecl *TopConv = *I;
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000329 CanQualType TConvType;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000330 if (FunctionTemplateDecl *TConversionTemplate =
331 dyn_cast<FunctionTemplateDecl>(TopConv))
332 TConvType =
333 getASTContext().getCanonicalType(
334 TConversionTemplate->getTemplatedDecl()->getResultType());
335 else
336 TConvType =
337 getASTContext().getCanonicalType(
338 cast<CXXConversionDecl>(TopConv)->getConversionType());
339 ConversionsTypeSet.insert(TConvType);
340 }
341}
342
Fariborz Jahanian62509212009-09-12 18:26:03 +0000343/// getNestedVisibleConversionFunctions - imports unique conversion
344/// functions from base classes into the visible conversion function
345/// list of the class 'RD'. This is a private helper method.
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000346/// TopConversionsTypeSet is the set of conversion functions of the class
347/// we are interested in. HiddenConversionTypes is set of conversion functions
348/// of the immediate derived class which hides the conversion functions found
349/// in current class.
Fariborz Jahanian62509212009-09-12 18:26:03 +0000350void
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000351CXXRecordDecl::getNestedVisibleConversionFunctions(CXXRecordDecl *RD,
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000352 const llvm::SmallPtrSet<CanQualType, 8> &TopConversionsTypeSet,
353 const llvm::SmallPtrSet<CanQualType, 8> &HiddenConversionTypes)
354{
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000355 bool inTopClass = (RD == this);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000356 QualType ClassType = getASTContext().getTypeDeclType(this);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000357 if (const RecordType *Record = ClassType->getAs<RecordType>()) {
John McCalleec51cf2010-01-20 00:46:10 +0000358 const UnresolvedSetImpl *Cs
Fariborz Jahanian53462782009-09-11 21:44:33 +0000359 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000360
John McCalleec51cf2010-01-20 00:46:10 +0000361 for (UnresolvedSetImpl::iterator I = Cs->begin(), E = Cs->end();
362 I != E; ++I) {
John McCallba135432009-11-21 08:51:07 +0000363 NamedDecl *Conv = *I;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000364 // Only those conversions not exact match of conversions in current
365 // class are candidateconversion routines.
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000366 CanQualType ConvType;
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000367 if (FunctionTemplateDecl *ConversionTemplate =
368 dyn_cast<FunctionTemplateDecl>(Conv))
369 ConvType =
370 getASTContext().getCanonicalType(
Fariborz Jahaniana5c12942009-09-15 23:02:16 +0000371 ConversionTemplate->getTemplatedDecl()->getResultType());
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000372 else
373 ConvType =
Fariborz Jahanian8b915e72009-09-15 22:15:23 +0000374 getASTContext().getCanonicalType(
375 cast<CXXConversionDecl>(Conv)->getConversionType());
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000376 // We only add conversion functions found in the base class if they
377 // are not hidden by those found in HiddenConversionTypes which are
378 // the conversion functions in its derived class.
379 if (inTopClass ||
380 (!TopConversionsTypeSet.count(ConvType) &&
381 !HiddenConversionTypes.count(ConvType)) ) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000382 if (FunctionTemplateDecl *ConversionTemplate =
383 dyn_cast<FunctionTemplateDecl>(Conv))
384 RD->addVisibleConversionFunction(ConversionTemplate);
385 else
386 RD->addVisibleConversionFunction(cast<CXXConversionDecl>(Conv));
Fariborz Jahanian53462782009-09-11 21:44:33 +0000387 }
388 }
389 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000390
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000391 if (getNumBases() == 0 && getNumVBases() == 0)
392 return;
Sebastian Redl9994a342009-10-25 17:03:50 +0000393
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000394 llvm::SmallPtrSet<CanQualType, 8> ConversionFunctions;
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000395 if (!inTopClass)
396 collectConversionFunctions(ConversionFunctions);
Sebastian Redl9994a342009-10-25 17:03:50 +0000397
Fariborz Jahanian53462782009-09-11 21:44:33 +0000398 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(),
399 E = vbases_end(); VBase != E; ++VBase) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000400 if (const RecordType *RT = VBase->getType()->getAs<RecordType>()) {
401 CXXRecordDecl *VBaseClassDecl
402 = cast<CXXRecordDecl>(RT->getDecl());
403 VBaseClassDecl->getNestedVisibleConversionFunctions(RD,
404 TopConversionsTypeSet,
405 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
406 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000407 }
408 for (CXXRecordDecl::base_class_iterator Base = bases_begin(),
409 E = bases_end(); Base != E; ++Base) {
410 if (Base->isVirtual())
411 continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000412 if (const RecordType *RT = Base->getType()->getAs<RecordType>()) {
413 CXXRecordDecl *BaseClassDecl
414 = cast<CXXRecordDecl>(RT->getDecl());
415
416 BaseClassDecl->getNestedVisibleConversionFunctions(RD,
417 TopConversionsTypeSet,
418 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
419 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000420 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000421}
422
423/// getVisibleConversionFunctions - get all conversion functions visible
424/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000425const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000426 // If root class, all conversions are visible.
427 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000428 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000429 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000430 if (data().ComputedVisibleConversions)
431 return &data().VisibleConversions;
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000432 llvm::SmallPtrSet<CanQualType, 8> TopConversionsTypeSet;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000433 collectConversionFunctions(TopConversionsTypeSet);
434 getNestedVisibleConversionFunctions(this, TopConversionsTypeSet,
435 TopConversionsTypeSet);
John McCall86ff3082010-02-04 22:26:26 +0000436 data().ComputedVisibleConversions = true;
437 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000438}
439
Fariborz Jahanian62509212009-09-12 18:26:03 +0000440void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000441 CXXConversionDecl *ConvDecl) {
442 assert(!ConvDecl->getDescribedFunctionTemplate() &&
443 "Conversion function templates should cast to FunctionTemplateDecl.");
John McCall86ff3082010-02-04 22:26:26 +0000444 data().VisibleConversions.addDecl(ConvDecl);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000445}
446
Fariborz Jahanian62509212009-09-12 18:26:03 +0000447void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000448 FunctionTemplateDecl *ConvDecl) {
449 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
450 "Function template is not a conversion function template");
John McCall86ff3082010-02-04 22:26:26 +0000451 data().VisibleConversions.addDecl(ConvDecl);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000452}
453
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000454void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000455 assert(!ConvDecl->getDescribedFunctionTemplate() &&
456 "Conversion function templates should cast to FunctionTemplateDecl.");
John McCall86ff3082010-02-04 22:26:26 +0000457 data().Conversions.addDecl(ConvDecl);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000458}
459
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000460void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000461 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
462 "Function template is not a conversion function template");
John McCall86ff3082010-02-04 22:26:26 +0000463 data().Conversions.addDecl(ConvDecl);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000464}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000465
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000466
467void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
468 Method->setVirtualAsWritten(true);
469 setAggregate(false);
470 setPOD(false);
471 setEmpty(false);
472 setPolymorphic(true);
473 setHasTrivialConstructor(false);
474 setHasTrivialCopyConstructor(false);
475 setHasTrivialCopyAssignment(false);
476}
477
Douglas Gregorf6b11852009-10-08 15:14:33 +0000478CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000479 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000480 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
481
482 return 0;
483}
484
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000485MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
486 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
487}
488
Douglas Gregorf6b11852009-10-08 15:14:33 +0000489void
490CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
491 TemplateSpecializationKind TSK) {
492 assert(TemplateOrInstantiation.isNull() &&
493 "Previous template or instantiation?");
494 assert(!isa<ClassTemplateSpecializationDecl>(this));
495 TemplateOrInstantiation
496 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
497}
498
Anders Carlssonb13e3572009-12-07 06:33:48 +0000499TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
500 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000501 = dyn_cast<ClassTemplateSpecializationDecl>(this))
502 return Spec->getSpecializationKind();
503
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000504 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000505 return MSInfo->getTemplateSpecializationKind();
506
507 return TSK_Undeclared;
508}
509
510void
511CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
512 if (ClassTemplateSpecializationDecl *Spec
513 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
514 Spec->setSpecializationKind(TSK);
515 return;
516 }
517
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000518 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000519 MSInfo->setTemplateSpecializationKind(TSK);
520 return;
521 }
522
523 assert(false && "Not a class template or member class specialization");
524}
525
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000526CXXConstructorDecl *
527CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
528 QualType ClassType = Context.getTypeDeclType(this);
529 DeclarationName ConstructorName
530 = Context.DeclarationNames.getCXXConstructorName(
531 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000533 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000534 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000535 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000536 // FIXME: In C++0x, a constructor template can be a default constructor.
537 if (isa<FunctionTemplateDecl>(*Con))
538 continue;
539
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000540 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
541 if (Constructor->isDefaultConstructor())
542 return Constructor;
543 }
544 return 0;
545}
546
John McCallc0bf4622010-02-23 00:48:20 +0000547CXXDestructorDecl *CXXRecordDecl::getDestructor(ASTContext &Context) const {
Anders Carlsson7267c162009-05-29 21:03:38 +0000548 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
550 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000551 = Context.DeclarationNames.getCXXDestructorName(
552 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000553
John McCallc0bf4622010-02-23 00:48:20 +0000554 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000555 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000556 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000558 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000559 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Anders Carlsson7267c162009-05-29 21:03:38 +0000561 return Dtor;
562}
563
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000564CXXMethodDecl *
565CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000566 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000567 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000568 bool isStatic, bool isInline) {
John McCalla93c9342009-12-07 02:54:59 +0000569 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000570 isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000571}
572
Douglas Gregor90916562009-09-29 18:16:17 +0000573bool CXXMethodDecl::isUsualDeallocationFunction() const {
574 if (getOverloadedOperator() != OO_Delete &&
575 getOverloadedOperator() != OO_Array_Delete)
576 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000577
578 // C++ [basic.stc.dynamic.deallocation]p2:
579 // A template instance is never a usual deallocation function,
580 // regardless of its signature.
581 if (getPrimaryTemplate())
582 return false;
583
Douglas Gregor90916562009-09-29 18:16:17 +0000584 // C++ [basic.stc.dynamic.deallocation]p2:
585 // If a class T has a member deallocation function named operator delete
586 // with exactly one parameter, then that function is a usual (non-placement)
587 // deallocation function. [...]
588 if (getNumParams() == 1)
589 return true;
590
591 // C++ [basic.stc.dynamic.deallocation]p2:
592 // [...] If class T does not declare such an operator delete but does
593 // declare a member deallocation function named operator delete with
594 // exactly two parameters, the second of which has type std::size_t (18.1),
595 // then this function is a usual deallocation function.
596 ASTContext &Context = getASTContext();
597 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000598 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
599 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000600 return false;
601
602 // This function is a usual deallocation function if there are no
603 // single-parameter deallocation functions of the same kind.
604 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
605 R.first != R.second; ++R.first) {
606 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
607 if (FD->getNumParams() == 1)
608 return false;
609 }
610
611 return true;
612}
613
Mike Stump1eb44332009-09-09 15:08:12 +0000614typedef llvm::DenseMap<const CXXMethodDecl*,
615 std::vector<const CXXMethodDecl *> *>
Anders Carlsson05eb2442009-05-16 23:58:37 +0000616 OverriddenMethodsMapTy;
617
Mike Stumpb9871a22009-08-21 01:45:00 +0000618// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
619// the vtable building code uses it at CG time.
Anders Carlsson05eb2442009-05-16 23:58:37 +0000620static OverriddenMethodsMapTy *OverriddenMethods = 0;
621
622void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000623 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000624 assert(!MD->getParent()->isDependentContext() &&
625 "Can't add an overridden method to a class template!");
626
Anders Carlsson05eb2442009-05-16 23:58:37 +0000627 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Anders Carlsson05eb2442009-05-16 23:58:37 +0000629 if (!OverriddenMethods)
630 OverriddenMethods = new OverriddenMethodsMapTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Anders Carlsson05eb2442009-05-16 23:58:37 +0000632 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
633 if (!Methods)
634 Methods = new std::vector<const CXXMethodDecl *>;
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Anders Carlsson05eb2442009-05-16 23:58:37 +0000636 Methods->push_back(MD);
637}
638
639CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
640 if (!OverriddenMethods)
641 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Anders Carlsson05eb2442009-05-16 23:58:37 +0000643 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000644 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000645 return 0;
Daniel Dunbar0908c332009-08-01 23:40:20 +0000646
Anders Carlsson05eb2442009-05-16 23:58:37 +0000647 return &(*it->second)[0];
648}
649
650CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
651 if (!OverriddenMethods)
652 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Anders Carlsson05eb2442009-05-16 23:58:37 +0000654 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000655 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000656 return 0;
657
Daniel Dunbar637ec322009-08-02 01:48:29 +0000658 return &(*it->second)[0] + it->second->size();
Anders Carlsson05eb2442009-05-16 23:58:37 +0000659}
660
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000661QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000662 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
663 // If the member function is declared const, the type of this is const X*,
664 // if the member function is declared volatile, the type of this is
665 // volatile X*, and if the member function is declared const volatile,
666 // the type of this is const volatile X*.
667
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000668 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000669
670 QualType ClassTy;
671 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
672 ClassTy = TD->getInjectedClassNameType(C);
673 else
Mike Stumpe607ed02009-08-07 18:05:12 +0000674 ClassTy = C.getTagDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000675 ClassTy = C.getQualifiedType(ClassTy,
676 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000677 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000678}
679
Eli Friedmand7d7f672009-12-06 20:50:05 +0000680bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000681 // If this function is a template instantiation, look at the template from
682 // which it was instantiated.
683 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
684 if (!CheckFn)
685 CheckFn = this;
686
Eli Friedmand7d7f672009-12-06 20:50:05 +0000687 const FunctionDecl *fn;
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000688 return CheckFn->getBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000689}
690
Douglas Gregor7ad83902008-11-05 04:29:56 +0000691CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000692CXXBaseOrMemberInitializer(ASTContext &Context,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000693 TypeSourceInfo *TInfo,
694 SourceLocation L, Expr *Init, SourceLocation R)
695 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
Douglas Gregor802ab452009-12-02 22:36:29 +0000696 LParenLoc(L), RParenLoc(R)
697{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000698}
699
700CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000701CXXBaseOrMemberInitializer(ASTContext &Context,
702 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000703 SourceLocation L, Expr *Init, SourceLocation R)
704 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
705 AnonUnionMember(0), LParenLoc(L), RParenLoc(R)
Douglas Gregor802ab452009-12-02 22:36:29 +0000706{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000707}
708
Douglas Gregor802ab452009-12-02 22:36:29 +0000709void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000710 if (Init)
711 Init->Destroy(Context);
Douglas Gregor802ab452009-12-02 22:36:29 +0000712 this->~CXXBaseOrMemberInitializer();
713}
714
715TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
716 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000717 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000718 else
719 return TypeLoc();
720}
721
722Type *CXXBaseOrMemberInitializer::getBaseClass() {
723 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000724 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000725 else
726 return 0;
727}
728
729const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
730 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000731 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000732 else
733 return 0;
734}
735
736SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
737 if (isMemberInitializer())
738 return getMemberLocation();
739
740 return getBaseClassLoc().getSourceRange().getBegin();
741}
742
743SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
744 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000745}
746
Douglas Gregorb48fe382008-10-31 09:07:45 +0000747CXXConstructorDecl *
748CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000749 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000750 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000751 bool isExplicit,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000752 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000753 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
754 "Name must refer to a constructor");
John McCalla93c9342009-12-07 02:54:59 +0000755 return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000756 isImplicitlyDeclared);
757}
758
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000759bool CXXConstructorDecl::isDefaultConstructor() const {
760 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000761 // A default constructor for a class X is a constructor of class
762 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000763 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000764 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000765}
766
Mike Stump1eb44332009-09-09 15:08:12 +0000767bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000768CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000769 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000770 // A non-template constructor for class X is a copy constructor
771 // if its first parameter is of type X&, const X&, volatile X& or
772 // const volatile X&, and either there are no other parameters
773 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000774 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000775 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000776 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000777 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000778 return false;
779
780 const ParmVarDecl *Param = getParamDecl(0);
781
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000782 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000783 const LValueReferenceType *ParamRefType =
784 Param->getType()->getAs<LValueReferenceType>();
785 if (!ParamRefType)
786 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000787
Douglas Gregorfd476482009-11-13 23:59:09 +0000788 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000789 ASTContext &Context = getASTContext();
790
Douglas Gregorfd476482009-11-13 23:59:09 +0000791 CanQualType PointeeType
792 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000793 CanQualType ClassTy
794 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000795 if (PointeeType.getUnqualifiedType() != ClassTy)
796 return false;
797
John McCall0953e762009-09-24 19:53:00 +0000798 // FIXME: other qualifiers?
799
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000800 // We have a copy constructor.
801 TypeQuals = PointeeType.getCVRQualifiers();
802 return true;
803}
804
Anders Carlssonfaccd722009-08-28 16:57:08 +0000805bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000806 // C++ [class.conv.ctor]p1:
807 // A constructor declared without the function-specifier explicit
808 // that can be called with a single parameter specifies a
809 // conversion from the type of its first parameter to the type of
810 // its class. Such a constructor is called a converting
811 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000812 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000813 return false;
814
Mike Stump1eb44332009-09-09 15:08:12 +0000815 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000816 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000817 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000818 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000819}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000820
Douglas Gregor66724ea2009-11-14 01:20:54 +0000821bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
822 if ((getNumParams() < 1) ||
823 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
824 (getPrimaryTemplate() == 0) ||
825 (getDescribedFunctionTemplate() != 0))
826 return false;
827
828 const ParmVarDecl *Param = getParamDecl(0);
829
830 ASTContext &Context = getASTContext();
831 CanQualType ParamType = Context.getCanonicalType(Param->getType());
832
833 // Strip off the lvalue reference, if any.
834 if (CanQual<LValueReferenceType> ParamRefType
835 = ParamType->getAs<LValueReferenceType>())
836 ParamType = ParamRefType->getPointeeType();
837
838
839 // Is it the same as our our class type?
840 CanQualType ClassTy
841 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
842 if (ParamType.getUnqualifiedType() != ClassTy)
843 return false;
844
845 return true;
846}
847
Douglas Gregor42a552f2008-11-05 20:51:48 +0000848CXXDestructorDecl *
849CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000850 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000851 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000852 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000853 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
854 "Name must refer to a destructor");
Mike Stump1eb44332009-09-09 15:08:12 +0000855 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff3e970492009-01-27 21:25:57 +0000856 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000857}
858
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000859void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000860CXXConstructorDecl::Destroy(ASTContext& C) {
861 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000862 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000863}
864
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000865CXXConversionDecl *
866CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000867 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000868 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000869 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000870 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
871 "Name must refer to a conversion function");
John McCalla93c9342009-12-07 02:54:59 +0000872 return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000873}
874
John McCall02cace72009-08-28 07:59:38 +0000875FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
876 SourceLocation L,
877 FriendUnion Friend,
878 SourceLocation FriendL) {
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000879#ifndef NDEBUG
John McCall02cace72009-08-28 07:59:38 +0000880 if (Friend.is<NamedDecl*>()) {
881 NamedDecl *D = Friend.get<NamedDecl*>();
882 assert(isa<FunctionDecl>(D) ||
883 isa<CXXRecordDecl>(D) ||
884 isa<FunctionTemplateDecl>(D) ||
885 isa<ClassTemplateDecl>(D));
John McCalle129d442009-12-17 23:21:11 +0000886
887 // As a temporary hack, we permit template instantiation to point
888 // to the original declaration when instantiating members.
889 assert(D->getFriendObjectKind() ||
890 (cast<CXXRecordDecl>(DC)->getTemplateSpecializationKind()));
John McCall02cace72009-08-28 07:59:38 +0000891 }
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000892#endif
John McCallc48fbdf2009-08-11 21:13:21 +0000893
John McCall02cace72009-08-28 07:59:38 +0000894 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump1eb44332009-09-09 15:08:12 +0000895}
John McCall3f9a8a62009-08-11 06:59:38 +0000896
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000897LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000898 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000899 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000900 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000901 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000902}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000903
904UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
905 SourceLocation L,
906 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000907 SourceRange QualifierRange,
908 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000909 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000910 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000911 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000912 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
913 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000914 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000915 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000916}
917
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000918NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
919 if (NamespaceAliasDecl *NA =
920 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
921 return NA->getNamespace();
922 return cast_or_null<NamespaceDecl>(NominatedNamespace);
923}
924
Mike Stump1eb44332009-09-09 15:08:12 +0000925NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
926 SourceLocation L,
927 SourceLocation AliasLoc,
928 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000929 SourceRange QualifierRange,
930 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +0000931 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +0000932 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000933 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
934 Namespace = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000935 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000936 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000937}
938
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000939UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
John McCall9488ea12009-11-17 05:59:44 +0000940 SourceLocation L, SourceRange NNR, SourceLocation UL,
941 NestedNameSpecifier* TargetNNS, DeclarationName Name,
942 bool IsTypeNameArg) {
943 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000944}
945
John McCall7ba107a2009-11-18 02:36:19 +0000946UnresolvedUsingValueDecl *
947UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
948 SourceLocation UsingLoc,
949 SourceRange TargetNNR,
950 NestedNameSpecifier *TargetNNS,
951 SourceLocation TargetNameLoc,
952 DeclarationName TargetName) {
953 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
954 TargetNNR, TargetNNS,
955 TargetNameLoc, TargetName);
956}
957
958UnresolvedUsingTypenameDecl *
959UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
960 SourceLocation UsingLoc,
961 SourceLocation TypenameLoc,
962 SourceRange TargetNNR,
963 NestedNameSpecifier *TargetNNS,
964 SourceLocation TargetNameLoc,
965 DeclarationName TargetName) {
966 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
967 TargetNNR, TargetNNS,
968 TargetNameLoc,
969 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +0000970}
971
Anders Carlssonfb311762009-03-14 00:25:26 +0000972StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
973 SourceLocation L, Expr *AssertExpr,
974 StringLiteral *Message) {
975 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
976}
977
978void StaticAssertDecl::Destroy(ASTContext& C) {
979 AssertExpr->Destroy(C);
980 Message->Destroy(C);
981 this->~StaticAssertDecl();
982 C.Deallocate((void *)this);
983}
984
985StaticAssertDecl::~StaticAssertDecl() {
986}
987
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000988static const char *getAccessName(AccessSpecifier AS) {
989 switch (AS) {
990 default:
991 case AS_none:
992 assert("Invalid access specifier!");
993 return 0;
994 case AS_public:
995 return "public";
996 case AS_private:
997 return "private";
998 case AS_protected:
999 return "protected";
1000 }
1001}
1002
1003const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1004 AccessSpecifier AS) {
1005 return DB << getAccessName(AS);
1006}
1007
1008