blob: 02ad36d47909e78132fcaa529406d928e2ea9285 [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
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000074CXXRecordDecl::setBases(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +000075 CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000076 unsigned NumBases) {
Mike Stump1eb44332009-09-09 15:08:12 +000077 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000078 // An aggregate is an array or a class (clause 9) with [...]
79 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000080 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000081
John McCall86ff3082010-02-04 22:26:26 +000082 if (data().Bases)
83 C.Deallocate(data().Bases);
Mike Stump1eb44332009-09-09 15:08:12 +000084
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000085 int vbaseCount = 0;
86 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
87 bool hasDirectVirtualBase = false;
Mike Stump1eb44332009-09-09 15:08:12 +000088
John McCall86ff3082010-02-04 22:26:26 +000089 data().Bases = new(C) CXXBaseSpecifier [NumBases];
90 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000091 for (unsigned i = 0; i < NumBases; ++i) {
John McCall86ff3082010-02-04 22:26:26 +000092 data().Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000093 // Keep track of inherited vbases for this base class.
94 const CXXBaseSpecifier *Base = Bases[i];
95 QualType BaseType = Base->getType();
Mike Stump1eb44332009-09-09 15:08:12 +000096 // Skip template types.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000097 // FIXME. This means that this list must be rebuilt during template
98 // instantiation.
99 if (BaseType->isDependentType())
100 continue;
101 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000102 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000103 if (Base->isVirtual())
104 hasDirectVirtualBase = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000105 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000106 BaseClassDecl->vbases_begin(),
107 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108 // Add this vbase to the array of vbases for current class if it is
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000109 // not already in the list.
110 // FIXME. Note that we do a linear search as number of such classes are
111 // very few.
112 int i;
113 for (i = 0; i < vbaseCount; ++i)
114 if (UniqueVbases[i]->getType() == VBase->getType())
115 break;
116 if (i == vbaseCount) {
117 UniqueVbases.push_back(VBase);
118 ++vbaseCount;
119 }
120 }
121 }
122 if (hasDirectVirtualBase) {
123 // Iterate one more time through the direct bases and add the virtual
124 // base to the list of vritual bases for current class.
125 for (unsigned i = 0; i < NumBases; ++i) {
126 const CXXBaseSpecifier *VBase = Bases[i];
127 if (!VBase->isVirtual())
128 continue;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000129 int j;
130 for (j = 0; j < vbaseCount; ++j)
131 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000132 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000133 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000134 UniqueVbases.push_back(VBase);
135 ++vbaseCount;
136 }
137 }
138 }
139 if (vbaseCount > 0) {
140 // build AST for inhireted, direct or indirect, virtual bases.
John McCall86ff3082010-02-04 22:26:26 +0000141 data().VBases = new (C) CXXBaseSpecifier [vbaseCount];
142 data().NumVBases = vbaseCount;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000143 for (int i = 0; i < vbaseCount; i++) {
144 QualType QT = UniqueVbases[i]->getType();
145 CXXRecordDecl *VBaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000146 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000147 data().VBases[i] =
Douglas Gregor2aef06d2009-07-22 20:55:49 +0000148 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
149 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
150 UniqueVbases[i]->getAccessSpecifier(), QT);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000151 }
152 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000153}
154
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000155/// Callback function for CXXRecordDecl::forallBases that acknowledges
156/// that it saw a base class.
157static bool SawBase(const CXXRecordDecl *, void *) {
158 return true;
159}
160
161bool CXXRecordDecl::hasAnyDependentBases() const {
162 if (!isDependentContext())
163 return false;
164
165 return !forallBases(SawBase, 0);
166}
167
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000168bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000169 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000170}
171
Mike Stump1eb44332009-09-09 15:08:12 +0000172CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000173 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000174 QualType ClassType
175 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000176 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000177 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000178 Context.getCanonicalType(ClassType));
179 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000180 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000181 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000182 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000183 // C++ [class.copy]p2:
184 // A non-template constructor for class X is a copy constructor if [...]
185 if (isa<FunctionTemplateDecl>(*Con))
186 continue;
187
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000188 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000189 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
190 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000191 return cast<CXXConstructorDecl>(*Con);
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000193 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000194 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000195 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000196}
197
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000198bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
199 const CXXMethodDecl *& MD) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000200 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
201 const_cast<CXXRecordDecl*>(this)));
202 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
203
204 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000205 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000206 Op != OpEnd; ++Op) {
207 // C++ [class.copy]p9:
208 // A user-declared copy assignment operator is a non-static non-template
209 // member function of class X with exactly one parameter of type X, X&,
210 // const X&, volatile X& or const volatile X&.
Douglas Gregor682054c2009-10-30 22:48:49 +0000211 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
212 if (!Method)
213 continue;
214
Sebastian Redl64b45f72009-01-05 20:52:13 +0000215 if (Method->isStatic())
216 continue;
Douglas Gregor77da3f42009-10-13 23:45:19 +0000217 if (Method->getPrimaryTemplate())
218 continue;
Douglas Gregor72564e72009-02-26 23:50:07 +0000219 const FunctionProtoType *FnType =
John McCall183700f2009-09-21 23:43:11 +0000220 Method->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000221 assert(FnType && "Overloaded operator has no prototype.");
222 // Don't assert on this; an invalid decl might have been left in the AST.
223 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
224 continue;
225 bool AcceptsConst = true;
226 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000227 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000228 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000229 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000230 if (!ArgType.isConstQualified())
231 AcceptsConst = false;
232 }
Douglas Gregora4923eb2009-11-16 21:35:15 +0000233 if (!Context.hasSameUnqualifiedType(ArgType, ClassType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000234 continue;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000235 MD = Method;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000236 // We have a single argument of type cv X or cv X&, i.e. we've found the
237 // copy assignment operator. Return whether it accepts const arguments.
238 return AcceptsConst;
239 }
240 assert(isInvalidDecl() &&
241 "No copy assignment operator declared in valid code.");
242 return false;
243}
244
245void
Mike Stump1eb44332009-09-09 15:08:12 +0000246CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000247 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000248 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
249 // Note that we have a user-declared constructor.
John McCall86ff3082010-02-04 22:26:26 +0000250 data().UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000251
Mike Stump1eb44332009-09-09 15:08:12 +0000252 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000253 // An aggregate is an array or a class (clause 9) with no
254 // user-declared constructors (12.1) [...].
John McCall86ff3082010-02-04 22:26:26 +0000255 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000256
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000257 // C++ [class]p4:
258 // A POD-struct is an aggregate class [...]
John McCall86ff3082010-02-04 22:26:26 +0000259 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000260
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000261 // C++ [class.ctor]p5:
262 // A constructor is trivial if it is an implicitly-declared default
263 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000264 // FIXME: C++0x: don't do this for "= default" default constructors.
John McCall86ff3082010-02-04 22:26:26 +0000265 data().HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000267 // Note when we have a user-declared copy constructor, which will
268 // suppress the implicit declaration of a copy constructor.
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000269 if (ConDecl->isCopyConstructor()) {
John McCall86ff3082010-02-04 22:26:26 +0000270 data().UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000271
272 // C++ [class.copy]p6:
273 // A copy constructor is trivial if it is implicitly declared.
274 // FIXME: C++0x: don't do this for "= default" copy constructors.
John McCall86ff3082010-02-04 22:26:26 +0000275 data().HasTrivialCopyConstructor = false;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000276 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000277}
278
Sebastian Redl64b45f72009-01-05 20:52:13 +0000279void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
280 CXXMethodDecl *OpDecl) {
281 // We're interested specifically in copy assignment operators.
John McCall183700f2009-09-21 23:43:11 +0000282 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000283 assert(FnType && "Overloaded operator has no proto function type.");
284 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregor77da3f42009-10-13 23:45:19 +0000285
286 // Copy assignment operators must be non-templates.
287 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
288 return;
289
Sebastian Redl64b45f72009-01-05 20:52:13 +0000290 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000291 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000292 ArgType = Ref->getPointeeType();
293
294 ArgType = ArgType.getUnqualifiedType();
295 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
296 const_cast<CXXRecordDecl*>(this)));
297
Douglas Gregora4923eb2009-11-16 21:35:15 +0000298 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000299 return;
300
301 // This is a copy assignment operator.
Eli Friedman88fad632009-11-07 00:02:45 +0000302 // Note on the decl that it is a copy assignment operator.
303 OpDecl->setCopyAssignment(true);
304
Sebastian Redl64b45f72009-01-05 20:52:13 +0000305 // Suppress the implicit declaration of a copy constructor.
John McCall86ff3082010-02-04 22:26:26 +0000306 data().UserDeclaredCopyAssignment = true;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000307
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000308 // C++ [class.copy]p11:
309 // A copy assignment operator is trivial if it is implicitly declared.
310 // FIXME: C++0x: don't do this for "= default" copy operators.
John McCall86ff3082010-02-04 22:26:26 +0000311 data().HasTrivialCopyAssignment = false;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000312
Sebastian Redl64b45f72009-01-05 20:52:13 +0000313 // C++ [class]p4:
314 // A POD-struct is an aggregate class that [...] has no user-defined copy
315 // assignment operator [...].
John McCall86ff3082010-02-04 22:26:26 +0000316 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000317}
318
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000319void
320CXXRecordDecl::collectConversionFunctions(
John McCallba135432009-11-21 08:51:07 +0000321 llvm::SmallPtrSet<CanQualType, 8>& ConversionsTypeSet) const
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000322{
John McCalleec51cf2010-01-20 00:46:10 +0000323 const UnresolvedSetImpl *Cs = getConversionFunctions();
324 for (UnresolvedSetImpl::iterator I = Cs->begin(), E = Cs->end();
325 I != E; ++I) {
John McCallba135432009-11-21 08:51:07 +0000326 NamedDecl *TopConv = *I;
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000327 CanQualType TConvType;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000328 if (FunctionTemplateDecl *TConversionTemplate =
329 dyn_cast<FunctionTemplateDecl>(TopConv))
330 TConvType =
331 getASTContext().getCanonicalType(
332 TConversionTemplate->getTemplatedDecl()->getResultType());
333 else
334 TConvType =
335 getASTContext().getCanonicalType(
336 cast<CXXConversionDecl>(TopConv)->getConversionType());
337 ConversionsTypeSet.insert(TConvType);
338 }
339}
340
Fariborz Jahanian62509212009-09-12 18:26:03 +0000341/// getNestedVisibleConversionFunctions - imports unique conversion
342/// functions from base classes into the visible conversion function
343/// list of the class 'RD'. This is a private helper method.
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000344/// TopConversionsTypeSet is the set of conversion functions of the class
345/// we are interested in. HiddenConversionTypes is set of conversion functions
346/// of the immediate derived class which hides the conversion functions found
347/// in current class.
Fariborz Jahanian62509212009-09-12 18:26:03 +0000348void
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000349CXXRecordDecl::getNestedVisibleConversionFunctions(CXXRecordDecl *RD,
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000350 const llvm::SmallPtrSet<CanQualType, 8> &TopConversionsTypeSet,
351 const llvm::SmallPtrSet<CanQualType, 8> &HiddenConversionTypes)
352{
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000353 bool inTopClass = (RD == this);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000354 QualType ClassType = getASTContext().getTypeDeclType(this);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000355 if (const RecordType *Record = ClassType->getAs<RecordType>()) {
John McCalleec51cf2010-01-20 00:46:10 +0000356 const UnresolvedSetImpl *Cs
Fariborz Jahanian53462782009-09-11 21:44:33 +0000357 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000358
John McCalleec51cf2010-01-20 00:46:10 +0000359 for (UnresolvedSetImpl::iterator I = Cs->begin(), E = Cs->end();
360 I != E; ++I) {
John McCallba135432009-11-21 08:51:07 +0000361 NamedDecl *Conv = *I;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000362 // Only those conversions not exact match of conversions in current
363 // class are candidateconversion routines.
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000364 CanQualType ConvType;
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000365 if (FunctionTemplateDecl *ConversionTemplate =
366 dyn_cast<FunctionTemplateDecl>(Conv))
367 ConvType =
368 getASTContext().getCanonicalType(
Fariborz Jahaniana5c12942009-09-15 23:02:16 +0000369 ConversionTemplate->getTemplatedDecl()->getResultType());
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000370 else
371 ConvType =
Fariborz Jahanian8b915e72009-09-15 22:15:23 +0000372 getASTContext().getCanonicalType(
373 cast<CXXConversionDecl>(Conv)->getConversionType());
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000374 // We only add conversion functions found in the base class if they
375 // are not hidden by those found in HiddenConversionTypes which are
376 // the conversion functions in its derived class.
377 if (inTopClass ||
378 (!TopConversionsTypeSet.count(ConvType) &&
379 !HiddenConversionTypes.count(ConvType)) ) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000380 if (FunctionTemplateDecl *ConversionTemplate =
381 dyn_cast<FunctionTemplateDecl>(Conv))
382 RD->addVisibleConversionFunction(ConversionTemplate);
383 else
384 RD->addVisibleConversionFunction(cast<CXXConversionDecl>(Conv));
Fariborz Jahanian53462782009-09-11 21:44:33 +0000385 }
386 }
387 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000388
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000389 if (getNumBases() == 0 && getNumVBases() == 0)
390 return;
Sebastian Redl9994a342009-10-25 17:03:50 +0000391
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000392 llvm::SmallPtrSet<CanQualType, 8> ConversionFunctions;
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000393 if (!inTopClass)
394 collectConversionFunctions(ConversionFunctions);
Sebastian Redl9994a342009-10-25 17:03:50 +0000395
Fariborz Jahanian53462782009-09-11 21:44:33 +0000396 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(),
397 E = vbases_end(); VBase != E; ++VBase) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000398 if (const RecordType *RT = VBase->getType()->getAs<RecordType>()) {
399 CXXRecordDecl *VBaseClassDecl
400 = cast<CXXRecordDecl>(RT->getDecl());
401 VBaseClassDecl->getNestedVisibleConversionFunctions(RD,
402 TopConversionsTypeSet,
403 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
404 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000405 }
406 for (CXXRecordDecl::base_class_iterator Base = bases_begin(),
407 E = bases_end(); Base != E; ++Base) {
408 if (Base->isVirtual())
409 continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000410 if (const RecordType *RT = Base->getType()->getAs<RecordType>()) {
411 CXXRecordDecl *BaseClassDecl
412 = cast<CXXRecordDecl>(RT->getDecl());
413
414 BaseClassDecl->getNestedVisibleConversionFunctions(RD,
415 TopConversionsTypeSet,
416 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
417 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000418 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000419}
420
421/// getVisibleConversionFunctions - get all conversion functions visible
422/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000423const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000424 // If root class, all conversions are visible.
425 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000426 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000427 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000428 if (data().ComputedVisibleConversions)
429 return &data().VisibleConversions;
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000430 llvm::SmallPtrSet<CanQualType, 8> TopConversionsTypeSet;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000431 collectConversionFunctions(TopConversionsTypeSet);
432 getNestedVisibleConversionFunctions(this, TopConversionsTypeSet,
433 TopConversionsTypeSet);
John McCall86ff3082010-02-04 22:26:26 +0000434 data().ComputedVisibleConversions = true;
435 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000436}
437
Fariborz Jahanian62509212009-09-12 18:26:03 +0000438void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000439 CXXConversionDecl *ConvDecl) {
440 assert(!ConvDecl->getDescribedFunctionTemplate() &&
441 "Conversion function templates should cast to FunctionTemplateDecl.");
John McCall86ff3082010-02-04 22:26:26 +0000442 data().VisibleConversions.addDecl(ConvDecl);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000443}
444
Fariborz Jahanian62509212009-09-12 18:26:03 +0000445void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000446 FunctionTemplateDecl *ConvDecl) {
447 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
448 "Function template is not a conversion function template");
John McCall86ff3082010-02-04 22:26:26 +0000449 data().VisibleConversions.addDecl(ConvDecl);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000450}
451
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000452void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000453 assert(!ConvDecl->getDescribedFunctionTemplate() &&
454 "Conversion function templates should cast to FunctionTemplateDecl.");
John McCall86ff3082010-02-04 22:26:26 +0000455 data().Conversions.addDecl(ConvDecl);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000456}
457
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000458void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000459 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
460 "Function template is not a conversion function template");
John McCall86ff3082010-02-04 22:26:26 +0000461 data().Conversions.addDecl(ConvDecl);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000462}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000463
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000464
465void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
466 Method->setVirtualAsWritten(true);
467 setAggregate(false);
468 setPOD(false);
469 setEmpty(false);
470 setPolymorphic(true);
471 setHasTrivialConstructor(false);
472 setHasTrivialCopyConstructor(false);
473 setHasTrivialCopyAssignment(false);
474}
475
Douglas Gregorf6b11852009-10-08 15:14:33 +0000476CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000477 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000478 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
479
480 return 0;
481}
482
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000483MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
484 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
485}
486
Douglas Gregorf6b11852009-10-08 15:14:33 +0000487void
488CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
489 TemplateSpecializationKind TSK) {
490 assert(TemplateOrInstantiation.isNull() &&
491 "Previous template or instantiation?");
492 assert(!isa<ClassTemplateSpecializationDecl>(this));
493 TemplateOrInstantiation
494 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
495}
496
Anders Carlssonb13e3572009-12-07 06:33:48 +0000497TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
498 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000499 = dyn_cast<ClassTemplateSpecializationDecl>(this))
500 return Spec->getSpecializationKind();
501
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000502 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000503 return MSInfo->getTemplateSpecializationKind();
504
505 return TSK_Undeclared;
506}
507
508void
509CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
510 if (ClassTemplateSpecializationDecl *Spec
511 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
512 Spec->setSpecializationKind(TSK);
513 return;
514 }
515
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000516 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000517 MSInfo->setTemplateSpecializationKind(TSK);
518 return;
519 }
520
521 assert(false && "Not a class template or member class specialization");
522}
523
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000524CXXConstructorDecl *
525CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
526 QualType ClassType = Context.getTypeDeclType(this);
527 DeclarationName ConstructorName
528 = Context.DeclarationNames.getCXXConstructorName(
529 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000531 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000532 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000533 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000534 // FIXME: In C++0x, a constructor template can be a default constructor.
535 if (isa<FunctionTemplateDecl>(*Con))
536 continue;
537
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000538 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
539 if (Constructor->isDefaultConstructor())
540 return Constructor;
541 }
542 return 0;
543}
544
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000545CXXDestructorDecl *CXXRecordDecl::getDestructor(ASTContext &Context) {
Anders Carlsson7267c162009-05-29 21:03:38 +0000546 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
548 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000549 = Context.DeclarationNames.getCXXDestructorName(
550 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000551
552 DeclContext::lookup_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000553 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000554 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000556 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000557 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Anders Carlsson7267c162009-05-29 21:03:38 +0000559 return Dtor;
560}
561
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000562CXXMethodDecl *
563CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000564 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000565 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000566 bool isStatic, bool isInline) {
John McCalla93c9342009-12-07 02:54:59 +0000567 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000568 isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000569}
570
Douglas Gregor90916562009-09-29 18:16:17 +0000571bool CXXMethodDecl::isUsualDeallocationFunction() const {
572 if (getOverloadedOperator() != OO_Delete &&
573 getOverloadedOperator() != OO_Array_Delete)
574 return false;
575
576 // C++ [basic.stc.dynamic.deallocation]p2:
577 // If a class T has a member deallocation function named operator delete
578 // with exactly one parameter, then that function is a usual (non-placement)
579 // deallocation function. [...]
580 if (getNumParams() == 1)
581 return true;
582
583 // C++ [basic.stc.dynamic.deallocation]p2:
584 // [...] If class T does not declare such an operator delete but does
585 // declare a member deallocation function named operator delete with
586 // exactly two parameters, the second of which has type std::size_t (18.1),
587 // then this function is a usual deallocation function.
588 ASTContext &Context = getASTContext();
589 if (getNumParams() != 2 ||
590 !Context.hasSameType(getParamDecl(1)->getType(), Context.getSizeType()))
591 return false;
592
593 // This function is a usual deallocation function if there are no
594 // single-parameter deallocation functions of the same kind.
595 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
596 R.first != R.second; ++R.first) {
597 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
598 if (FD->getNumParams() == 1)
599 return false;
600 }
601
602 return true;
603}
604
Mike Stump1eb44332009-09-09 15:08:12 +0000605typedef llvm::DenseMap<const CXXMethodDecl*,
606 std::vector<const CXXMethodDecl *> *>
Anders Carlsson05eb2442009-05-16 23:58:37 +0000607 OverriddenMethodsMapTy;
608
Mike Stumpb9871a22009-08-21 01:45:00 +0000609// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
610// the vtable building code uses it at CG time.
Anders Carlsson05eb2442009-05-16 23:58:37 +0000611static OverriddenMethodsMapTy *OverriddenMethods = 0;
612
613void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000614 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000615 assert(!MD->getParent()->isDependentContext() &&
616 "Can't add an overridden method to a class template!");
617
Anders Carlsson05eb2442009-05-16 23:58:37 +0000618 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Anders Carlsson05eb2442009-05-16 23:58:37 +0000620 if (!OverriddenMethods)
621 OverriddenMethods = new OverriddenMethodsMapTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Anders Carlsson05eb2442009-05-16 23:58:37 +0000623 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
624 if (!Methods)
625 Methods = new std::vector<const CXXMethodDecl *>;
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Anders Carlsson05eb2442009-05-16 23:58:37 +0000627 Methods->push_back(MD);
628}
629
630CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
631 if (!OverriddenMethods)
632 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Anders Carlsson05eb2442009-05-16 23:58:37 +0000634 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000635 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000636 return 0;
Daniel Dunbar0908c332009-08-01 23:40:20 +0000637
Anders Carlsson05eb2442009-05-16 23:58:37 +0000638 return &(*it->second)[0];
639}
640
641CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
642 if (!OverriddenMethods)
643 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Anders Carlsson05eb2442009-05-16 23:58:37 +0000645 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000646 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000647 return 0;
648
Daniel Dunbar637ec322009-08-02 01:48:29 +0000649 return &(*it->second)[0] + it->second->size();
Anders Carlsson05eb2442009-05-16 23:58:37 +0000650}
651
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000652QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000653 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
654 // If the member function is declared const, the type of this is const X*,
655 // if the member function is declared volatile, the type of this is
656 // volatile X*, and if the member function is declared const volatile,
657 // the type of this is const volatile X*.
658
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000659 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000660
661 QualType ClassTy;
662 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
663 ClassTy = TD->getInjectedClassNameType(C);
664 else
Mike Stumpe607ed02009-08-07 18:05:12 +0000665 ClassTy = C.getTagDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000666 ClassTy = C.getQualifiedType(ClassTy,
667 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000668 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000669}
670
Eli Friedmand7d7f672009-12-06 20:50:05 +0000671bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000672 // If this function is a template instantiation, look at the template from
673 // which it was instantiated.
674 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
675 if (!CheckFn)
676 CheckFn = this;
677
Eli Friedmand7d7f672009-12-06 20:50:05 +0000678 const FunctionDecl *fn;
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000679 return CheckFn->getBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000680}
681
Douglas Gregor7ad83902008-11-05 04:29:56 +0000682CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000683CXXBaseOrMemberInitializer(ASTContext &Context,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000684 TypeSourceInfo *TInfo,
685 SourceLocation L, Expr *Init, SourceLocation R)
686 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
Douglas Gregor802ab452009-12-02 22:36:29 +0000687 LParenLoc(L), RParenLoc(R)
688{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000689}
690
691CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000692CXXBaseOrMemberInitializer(ASTContext &Context,
693 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000694 SourceLocation L, Expr *Init, SourceLocation R)
695 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
696 AnonUnionMember(0), LParenLoc(L), RParenLoc(R)
Douglas Gregor802ab452009-12-02 22:36:29 +0000697{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000698}
699
Douglas Gregor802ab452009-12-02 22:36:29 +0000700void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000701 if (Init)
702 Init->Destroy(Context);
Douglas Gregor802ab452009-12-02 22:36:29 +0000703 this->~CXXBaseOrMemberInitializer();
704}
705
706TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
707 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000708 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000709 else
710 return TypeLoc();
711}
712
713Type *CXXBaseOrMemberInitializer::getBaseClass() {
714 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000715 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000716 else
717 return 0;
718}
719
720const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
721 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000722 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000723 else
724 return 0;
725}
726
727SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
728 if (isMemberInitializer())
729 return getMemberLocation();
730
731 return getBaseClassLoc().getSourceRange().getBegin();
732}
733
734SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
735 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000736}
737
Douglas Gregorb48fe382008-10-31 09:07:45 +0000738CXXConstructorDecl *
739CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000740 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000741 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000742 bool isExplicit,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000743 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000744 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
745 "Name must refer to a constructor");
John McCalla93c9342009-12-07 02:54:59 +0000746 return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000747 isImplicitlyDeclared);
748}
749
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000750bool CXXConstructorDecl::isDefaultConstructor() const {
751 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000752 // A default constructor for a class X is a constructor of class
753 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000754 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000755 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000756}
757
Mike Stump1eb44332009-09-09 15:08:12 +0000758bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000759CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000760 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000761 // A non-template constructor for class X is a copy constructor
762 // if its first parameter is of type X&, const X&, volatile X& or
763 // const volatile X&, and either there are no other parameters
764 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000765 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000766 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000767 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000768 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000769 return false;
770
771 const ParmVarDecl *Param = getParamDecl(0);
772
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000773 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000774 const LValueReferenceType *ParamRefType =
775 Param->getType()->getAs<LValueReferenceType>();
776 if (!ParamRefType)
777 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000778
Douglas Gregorfd476482009-11-13 23:59:09 +0000779 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000780 ASTContext &Context = getASTContext();
781
Douglas Gregorfd476482009-11-13 23:59:09 +0000782 CanQualType PointeeType
783 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000784 CanQualType ClassTy
785 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000786 if (PointeeType.getUnqualifiedType() != ClassTy)
787 return false;
788
John McCall0953e762009-09-24 19:53:00 +0000789 // FIXME: other qualifiers?
790
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000791 // We have a copy constructor.
792 TypeQuals = PointeeType.getCVRQualifiers();
793 return true;
794}
795
Anders Carlssonfaccd722009-08-28 16:57:08 +0000796bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000797 // C++ [class.conv.ctor]p1:
798 // A constructor declared without the function-specifier explicit
799 // that can be called with a single parameter specifies a
800 // conversion from the type of its first parameter to the type of
801 // its class. Such a constructor is called a converting
802 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000803 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000804 return false;
805
Mike Stump1eb44332009-09-09 15:08:12 +0000806 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000807 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000808 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000809 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000810}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000811
Douglas Gregor66724ea2009-11-14 01:20:54 +0000812bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
813 if ((getNumParams() < 1) ||
814 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
815 (getPrimaryTemplate() == 0) ||
816 (getDescribedFunctionTemplate() != 0))
817 return false;
818
819 const ParmVarDecl *Param = getParamDecl(0);
820
821 ASTContext &Context = getASTContext();
822 CanQualType ParamType = Context.getCanonicalType(Param->getType());
823
824 // Strip off the lvalue reference, if any.
825 if (CanQual<LValueReferenceType> ParamRefType
826 = ParamType->getAs<LValueReferenceType>())
827 ParamType = ParamRefType->getPointeeType();
828
829
830 // Is it the same as our our class type?
831 CanQualType ClassTy
832 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
833 if (ParamType.getUnqualifiedType() != ClassTy)
834 return false;
835
836 return true;
837}
838
Douglas Gregor42a552f2008-11-05 20:51:48 +0000839CXXDestructorDecl *
840CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000841 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000842 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000843 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000844 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
845 "Name must refer to a destructor");
Mike Stump1eb44332009-09-09 15:08:12 +0000846 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff3e970492009-01-27 21:25:57 +0000847 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000848}
849
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000850void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000851CXXConstructorDecl::Destroy(ASTContext& C) {
852 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000853 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000854}
855
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000856CXXConversionDecl *
857CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000858 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000859 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000860 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000861 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
862 "Name must refer to a conversion function");
John McCalla93c9342009-12-07 02:54:59 +0000863 return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000864}
865
John McCall02cace72009-08-28 07:59:38 +0000866FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
867 SourceLocation L,
868 FriendUnion Friend,
869 SourceLocation FriendL) {
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000870#ifndef NDEBUG
John McCall02cace72009-08-28 07:59:38 +0000871 if (Friend.is<NamedDecl*>()) {
872 NamedDecl *D = Friend.get<NamedDecl*>();
873 assert(isa<FunctionDecl>(D) ||
874 isa<CXXRecordDecl>(D) ||
875 isa<FunctionTemplateDecl>(D) ||
876 isa<ClassTemplateDecl>(D));
John McCalle129d442009-12-17 23:21:11 +0000877
878 // As a temporary hack, we permit template instantiation to point
879 // to the original declaration when instantiating members.
880 assert(D->getFriendObjectKind() ||
881 (cast<CXXRecordDecl>(DC)->getTemplateSpecializationKind()));
John McCall02cace72009-08-28 07:59:38 +0000882 }
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000883#endif
John McCallc48fbdf2009-08-11 21:13:21 +0000884
John McCall02cace72009-08-28 07:59:38 +0000885 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump1eb44332009-09-09 15:08:12 +0000886}
John McCall3f9a8a62009-08-11 06:59:38 +0000887
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000888LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000889 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000890 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000891 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000892 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000893}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000894
895UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
896 SourceLocation L,
897 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000898 SourceRange QualifierRange,
899 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000900 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000901 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000902 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000903 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
904 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000905 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000906 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000907}
908
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000909NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
910 if (NamespaceAliasDecl *NA =
911 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
912 return NA->getNamespace();
913 return cast_or_null<NamespaceDecl>(NominatedNamespace);
914}
915
Mike Stump1eb44332009-09-09 15:08:12 +0000916NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
917 SourceLocation L,
918 SourceLocation AliasLoc,
919 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000920 SourceRange QualifierRange,
921 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +0000922 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +0000923 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000924 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
925 Namespace = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000926 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000927 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000928}
929
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000930UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
John McCall9488ea12009-11-17 05:59:44 +0000931 SourceLocation L, SourceRange NNR, SourceLocation UL,
932 NestedNameSpecifier* TargetNNS, DeclarationName Name,
933 bool IsTypeNameArg) {
934 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000935}
936
John McCall7ba107a2009-11-18 02:36:19 +0000937UnresolvedUsingValueDecl *
938UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
939 SourceLocation UsingLoc,
940 SourceRange TargetNNR,
941 NestedNameSpecifier *TargetNNS,
942 SourceLocation TargetNameLoc,
943 DeclarationName TargetName) {
944 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
945 TargetNNR, TargetNNS,
946 TargetNameLoc, TargetName);
947}
948
949UnresolvedUsingTypenameDecl *
950UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
951 SourceLocation UsingLoc,
952 SourceLocation TypenameLoc,
953 SourceRange TargetNNR,
954 NestedNameSpecifier *TargetNNS,
955 SourceLocation TargetNameLoc,
956 DeclarationName TargetName) {
957 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
958 TargetNNR, TargetNNS,
959 TargetNameLoc,
960 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +0000961}
962
Anders Carlssonfb311762009-03-14 00:25:26 +0000963StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
964 SourceLocation L, Expr *AssertExpr,
965 StringLiteral *Message) {
966 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
967}
968
969void StaticAssertDecl::Destroy(ASTContext& C) {
970 AssertExpr->Destroy(C);
971 Message->Destroy(C);
972 this->~StaticAssertDecl();
973 C.Deallocate((void *)this);
974}
975
976StaticAssertDecl::~StaticAssertDecl() {
977}
978
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000979static const char *getAccessName(AccessSpecifier AS) {
980 switch (AS) {
981 default:
982 case AS_none:
983 assert("Invalid access specifier!");
984 return 0;
985 case AS_public:
986 return "public";
987 case AS_private:
988 return "private";
989 case AS_protected:
990 return "protected";
991 }
992}
993
994const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
995 AccessSpecifier AS) {
996 return DB << getAccessName(AS);
997}
998
999