blob: 7f4ad34fb7f820a1fb09a6f5716dbb99787b979e [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
Anders Carlsson05eb2442009-05-16 23:58:37 +0000614void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000615 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000616 assert(!MD->getParent()->isDependentContext() &&
617 "Can't add an overridden method to a class template!");
618
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000619 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000620}
621
622CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000623 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000624}
625
626CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000627 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000628}
629
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000630QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000631 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
632 // If the member function is declared const, the type of this is const X*,
633 // if the member function is declared volatile, the type of this is
634 // volatile X*, and if the member function is declared const volatile,
635 // the type of this is const volatile X*.
636
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000637 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000638
John McCall3cb0ebd2010-03-10 03:28:59 +0000639 QualType ClassTy = C.getTypeDeclType(getParent());
640
641 // Aesthetically we prefer not to synthesize a type as the
642 // InjectedClassNameType of a template pattern: injected class names
643 // are printed without template arguments, which might
644 // surprise/confuse/distract our poor users if they didn't
645 // explicitly write one.
646 if (isa<InjectedClassNameType>(ClassTy))
647 ClassTy = cast<InjectedClassNameType>(ClassTy)->getUnderlyingType();
648
John McCall0953e762009-09-24 19:53:00 +0000649 ClassTy = C.getQualifiedType(ClassTy,
650 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000651 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000652}
653
Eli Friedmand7d7f672009-12-06 20:50:05 +0000654bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000655 // If this function is a template instantiation, look at the template from
656 // which it was instantiated.
657 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
658 if (!CheckFn)
659 CheckFn = this;
660
Eli Friedmand7d7f672009-12-06 20:50:05 +0000661 const FunctionDecl *fn;
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000662 return CheckFn->getBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000663}
664
Douglas Gregor7ad83902008-11-05 04:29:56 +0000665CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000666CXXBaseOrMemberInitializer(ASTContext &Context,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000667 TypeSourceInfo *TInfo,
668 SourceLocation L, Expr *Init, SourceLocation R)
669 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
Douglas Gregor802ab452009-12-02 22:36:29 +0000670 LParenLoc(L), RParenLoc(R)
671{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000672}
673
674CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000675CXXBaseOrMemberInitializer(ASTContext &Context,
676 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000677 SourceLocation L, Expr *Init, SourceLocation R)
678 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
679 AnonUnionMember(0), LParenLoc(L), RParenLoc(R)
Douglas Gregor802ab452009-12-02 22:36:29 +0000680{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000681}
682
Douglas Gregor802ab452009-12-02 22:36:29 +0000683void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000684 if (Init)
685 Init->Destroy(Context);
Douglas Gregor802ab452009-12-02 22:36:29 +0000686 this->~CXXBaseOrMemberInitializer();
687}
688
689TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
690 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000691 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000692 else
693 return TypeLoc();
694}
695
696Type *CXXBaseOrMemberInitializer::getBaseClass() {
697 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000698 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000699 else
700 return 0;
701}
702
703const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
704 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000705 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000706 else
707 return 0;
708}
709
710SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
711 if (isMemberInitializer())
712 return getMemberLocation();
713
714 return getBaseClassLoc().getSourceRange().getBegin();
715}
716
717SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
718 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000719}
720
Douglas Gregorb48fe382008-10-31 09:07:45 +0000721CXXConstructorDecl *
722CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000723 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000724 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000725 bool isExplicit,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000726 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000727 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
728 "Name must refer to a constructor");
John McCalla93c9342009-12-07 02:54:59 +0000729 return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000730 isImplicitlyDeclared);
731}
732
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000733bool CXXConstructorDecl::isDefaultConstructor() const {
734 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000735 // A default constructor for a class X is a constructor of class
736 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000737 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000738 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000739}
740
Mike Stump1eb44332009-09-09 15:08:12 +0000741bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000742CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000743 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000744 // A non-template constructor for class X is a copy constructor
745 // if its first parameter is of type X&, const X&, volatile X& or
746 // const volatile X&, and either there are no other parameters
747 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000748 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000749 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000750 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000751 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000752 return false;
753
754 const ParmVarDecl *Param = getParamDecl(0);
755
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000756 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000757 const LValueReferenceType *ParamRefType =
758 Param->getType()->getAs<LValueReferenceType>();
759 if (!ParamRefType)
760 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000761
Douglas Gregorfd476482009-11-13 23:59:09 +0000762 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000763 ASTContext &Context = getASTContext();
764
Douglas Gregorfd476482009-11-13 23:59:09 +0000765 CanQualType PointeeType
766 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000767 CanQualType ClassTy
768 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000769 if (PointeeType.getUnqualifiedType() != ClassTy)
770 return false;
771
John McCall0953e762009-09-24 19:53:00 +0000772 // FIXME: other qualifiers?
773
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000774 // We have a copy constructor.
775 TypeQuals = PointeeType.getCVRQualifiers();
776 return true;
777}
778
Anders Carlssonfaccd722009-08-28 16:57:08 +0000779bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000780 // C++ [class.conv.ctor]p1:
781 // A constructor declared without the function-specifier explicit
782 // that can be called with a single parameter specifies a
783 // conversion from the type of its first parameter to the type of
784 // its class. Such a constructor is called a converting
785 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000786 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000787 return false;
788
Mike Stump1eb44332009-09-09 15:08:12 +0000789 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000790 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000791 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000792 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000793}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000794
Douglas Gregor66724ea2009-11-14 01:20:54 +0000795bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
796 if ((getNumParams() < 1) ||
797 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
798 (getPrimaryTemplate() == 0) ||
799 (getDescribedFunctionTemplate() != 0))
800 return false;
801
802 const ParmVarDecl *Param = getParamDecl(0);
803
804 ASTContext &Context = getASTContext();
805 CanQualType ParamType = Context.getCanonicalType(Param->getType());
806
807 // Strip off the lvalue reference, if any.
808 if (CanQual<LValueReferenceType> ParamRefType
809 = ParamType->getAs<LValueReferenceType>())
810 ParamType = ParamRefType->getPointeeType();
811
812
813 // Is it the same as our our class type?
814 CanQualType ClassTy
815 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
816 if (ParamType.getUnqualifiedType() != ClassTy)
817 return false;
818
819 return true;
820}
821
Douglas Gregor42a552f2008-11-05 20:51:48 +0000822CXXDestructorDecl *
823CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000824 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000825 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000826 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000827 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
828 "Name must refer to a destructor");
Mike Stump1eb44332009-09-09 15:08:12 +0000829 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff3e970492009-01-27 21:25:57 +0000830 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000831}
832
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000833void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000834CXXConstructorDecl::Destroy(ASTContext& C) {
835 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000836 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000837}
838
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000839CXXConversionDecl *
840CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000841 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000842 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000843 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000844 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
845 "Name must refer to a conversion function");
John McCalla93c9342009-12-07 02:54:59 +0000846 return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000847}
848
John McCall02cace72009-08-28 07:59:38 +0000849FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
850 SourceLocation L,
851 FriendUnion Friend,
852 SourceLocation FriendL) {
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000853#ifndef NDEBUG
John McCall02cace72009-08-28 07:59:38 +0000854 if (Friend.is<NamedDecl*>()) {
855 NamedDecl *D = Friend.get<NamedDecl*>();
856 assert(isa<FunctionDecl>(D) ||
857 isa<CXXRecordDecl>(D) ||
858 isa<FunctionTemplateDecl>(D) ||
859 isa<ClassTemplateDecl>(D));
John McCalle129d442009-12-17 23:21:11 +0000860
861 // As a temporary hack, we permit template instantiation to point
862 // to the original declaration when instantiating members.
863 assert(D->getFriendObjectKind() ||
864 (cast<CXXRecordDecl>(DC)->getTemplateSpecializationKind()));
John McCall02cace72009-08-28 07:59:38 +0000865 }
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000866#endif
John McCallc48fbdf2009-08-11 21:13:21 +0000867
John McCall02cace72009-08-28 07:59:38 +0000868 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump1eb44332009-09-09 15:08:12 +0000869}
John McCall3f9a8a62009-08-11 06:59:38 +0000870
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000871LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000872 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000873 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000874 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000875 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000876}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000877
878UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
879 SourceLocation L,
880 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000881 SourceRange QualifierRange,
882 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000883 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000884 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000885 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000886 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
887 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000888 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000889 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000890}
891
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000892NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
893 if (NamespaceAliasDecl *NA =
894 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
895 return NA->getNamespace();
896 return cast_or_null<NamespaceDecl>(NominatedNamespace);
897}
898
Mike Stump1eb44332009-09-09 15:08:12 +0000899NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
900 SourceLocation L,
901 SourceLocation AliasLoc,
902 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000903 SourceRange QualifierRange,
904 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +0000905 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +0000906 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000907 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
908 Namespace = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000909 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000910 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000911}
912
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000913UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
John McCall9488ea12009-11-17 05:59:44 +0000914 SourceLocation L, SourceRange NNR, SourceLocation UL,
915 NestedNameSpecifier* TargetNNS, DeclarationName Name,
916 bool IsTypeNameArg) {
917 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000918}
919
John McCall7ba107a2009-11-18 02:36:19 +0000920UnresolvedUsingValueDecl *
921UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
922 SourceLocation UsingLoc,
923 SourceRange TargetNNR,
924 NestedNameSpecifier *TargetNNS,
925 SourceLocation TargetNameLoc,
926 DeclarationName TargetName) {
927 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
928 TargetNNR, TargetNNS,
929 TargetNameLoc, TargetName);
930}
931
932UnresolvedUsingTypenameDecl *
933UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
934 SourceLocation UsingLoc,
935 SourceLocation TypenameLoc,
936 SourceRange TargetNNR,
937 NestedNameSpecifier *TargetNNS,
938 SourceLocation TargetNameLoc,
939 DeclarationName TargetName) {
940 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
941 TargetNNR, TargetNNS,
942 TargetNameLoc,
943 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +0000944}
945
Anders Carlssonfb311762009-03-14 00:25:26 +0000946StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
947 SourceLocation L, Expr *AssertExpr,
948 StringLiteral *Message) {
949 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
950}
951
952void StaticAssertDecl::Destroy(ASTContext& C) {
953 AssertExpr->Destroy(C);
954 Message->Destroy(C);
955 this->~StaticAssertDecl();
956 C.Deallocate((void *)this);
957}
958
959StaticAssertDecl::~StaticAssertDecl() {
960}
961
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000962static const char *getAccessName(AccessSpecifier AS) {
963 switch (AS) {
964 default:
965 case AS_none:
966 assert("Invalid access specifier!");
967 return 0;
968 case AS_public:
969 return "public";
970 case AS_private:
971 return "private";
972 case AS_protected:
973 return "protected";
974 }
975}
976
977const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
978 AccessSpecifier AS) {
979 return DB << getAccessName(AS);
980}
981
982