blob: b0569d68015f8d9961aa77e03acfa5ece06b0643 [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();
Mike Stump1eb44332009-09-09 15:08:12 +000097 // Skip template types.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000098 // FIXME. This means that this list must be rebuilt during template
99 // instantiation.
100 if (BaseType->isDependentType())
101 continue;
102 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000103 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000104 if (Base->isVirtual())
105 hasDirectVirtualBase = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000106 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000107 BaseClassDecl->vbases_begin(),
108 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Mike Stump1eb44332009-09-09 15:08:12 +0000109 // Add this vbase to the array of vbases for current class if it is
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000110 // not already in the list.
111 // FIXME. Note that we do a linear search as number of such classes are
112 // very few.
113 int i;
114 for (i = 0; i < vbaseCount; ++i)
115 if (UniqueVbases[i]->getType() == VBase->getType())
116 break;
117 if (i == vbaseCount) {
118 UniqueVbases.push_back(VBase);
119 ++vbaseCount;
120 }
121 }
122 }
123 if (hasDirectVirtualBase) {
124 // Iterate one more time through the direct bases and add the virtual
125 // base to the list of vritual bases for current class.
126 for (unsigned i = 0; i < NumBases; ++i) {
127 const CXXBaseSpecifier *VBase = Bases[i];
128 if (!VBase->isVirtual())
129 continue;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000130 int j;
131 for (j = 0; j < vbaseCount; ++j)
132 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000133 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000134 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000135 UniqueVbases.push_back(VBase);
136 ++vbaseCount;
137 }
138 }
139 }
140 if (vbaseCount > 0) {
141 // build AST for inhireted, direct or indirect, virtual bases.
John McCall86ff3082010-02-04 22:26:26 +0000142 data().VBases = new (C) CXXBaseSpecifier [vbaseCount];
143 data().NumVBases = vbaseCount;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000144 for (int i = 0; i < vbaseCount; i++) {
145 QualType QT = UniqueVbases[i]->getType();
146 CXXRecordDecl *VBaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000147 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000148 data().VBases[i] =
Douglas Gregor2aef06d2009-07-22 20:55:49 +0000149 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
150 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
151 UniqueVbases[i]->getAccessSpecifier(), QT);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000152 }
153 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000154}
155
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000156/// Callback function for CXXRecordDecl::forallBases that acknowledges
157/// that it saw a base class.
158static bool SawBase(const CXXRecordDecl *, void *) {
159 return true;
160}
161
162bool CXXRecordDecl::hasAnyDependentBases() const {
163 if (!isDependentContext())
164 return false;
165
166 return !forallBases(SawBase, 0);
167}
168
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000169bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000170 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000171}
172
Mike Stump1eb44332009-09-09 15:08:12 +0000173CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000174 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000175 QualType ClassType
176 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000177 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000178 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000179 Context.getCanonicalType(ClassType));
180 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000181 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000182 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000183 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000184 // C++ [class.copy]p2:
185 // A non-template constructor for class X is a copy constructor if [...]
186 if (isa<FunctionTemplateDecl>(*Con))
187 continue;
188
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000189 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000190 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
191 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000192 return cast<CXXConstructorDecl>(*Con);
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000194 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000195 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000196 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000197}
198
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000199bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
200 const CXXMethodDecl *& MD) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000201 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
202 const_cast<CXXRecordDecl*>(this)));
203 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
204
205 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000206 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000207 Op != OpEnd; ++Op) {
208 // C++ [class.copy]p9:
209 // A user-declared copy assignment operator is a non-static non-template
210 // member function of class X with exactly one parameter of type X, X&,
211 // const X&, volatile X& or const volatile X&.
Douglas Gregor682054c2009-10-30 22:48:49 +0000212 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
213 if (!Method)
214 continue;
215
Sebastian Redl64b45f72009-01-05 20:52:13 +0000216 if (Method->isStatic())
217 continue;
Douglas Gregor77da3f42009-10-13 23:45:19 +0000218 if (Method->getPrimaryTemplate())
219 continue;
Douglas Gregor72564e72009-02-26 23:50:07 +0000220 const FunctionProtoType *FnType =
John McCall183700f2009-09-21 23:43:11 +0000221 Method->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000222 assert(FnType && "Overloaded operator has no prototype.");
223 // Don't assert on this; an invalid decl might have been left in the AST.
224 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
225 continue;
226 bool AcceptsConst = true;
227 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000228 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000229 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000230 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000231 if (!ArgType.isConstQualified())
232 AcceptsConst = false;
233 }
Douglas Gregora4923eb2009-11-16 21:35:15 +0000234 if (!Context.hasSameUnqualifiedType(ArgType, ClassType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000235 continue;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000236 MD = Method;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000237 // We have a single argument of type cv X or cv X&, i.e. we've found the
238 // copy assignment operator. Return whether it accepts const arguments.
239 return AcceptsConst;
240 }
241 assert(isInvalidDecl() &&
242 "No copy assignment operator declared in valid code.");
243 return false;
244}
245
246void
Mike Stump1eb44332009-09-09 15:08:12 +0000247CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000248 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000249 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
250 // Note that we have a user-declared constructor.
John McCall86ff3082010-02-04 22:26:26 +0000251 data().UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000252
Mike Stump1eb44332009-09-09 15:08:12 +0000253 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000254 // An aggregate is an array or a class (clause 9) with no
255 // user-declared constructors (12.1) [...].
John McCall86ff3082010-02-04 22:26:26 +0000256 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000257
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000258 // C++ [class]p4:
259 // A POD-struct is an aggregate class [...]
John McCall86ff3082010-02-04 22:26:26 +0000260 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000261
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000262 // C++ [class.ctor]p5:
263 // A constructor is trivial if it is an implicitly-declared default
264 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000265 // FIXME: C++0x: don't do this for "= default" default constructors.
John McCall86ff3082010-02-04 22:26:26 +0000266 data().HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000268 // Note when we have a user-declared copy constructor, which will
269 // suppress the implicit declaration of a copy constructor.
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000270 if (ConDecl->isCopyConstructor()) {
John McCall86ff3082010-02-04 22:26:26 +0000271 data().UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000272
273 // C++ [class.copy]p6:
274 // A copy constructor is trivial if it is implicitly declared.
275 // FIXME: C++0x: don't do this for "= default" copy constructors.
John McCall86ff3082010-02-04 22:26:26 +0000276 data().HasTrivialCopyConstructor = false;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000277 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000278}
279
Sebastian Redl64b45f72009-01-05 20:52:13 +0000280void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
281 CXXMethodDecl *OpDecl) {
282 // We're interested specifically in copy assignment operators.
John McCall183700f2009-09-21 23:43:11 +0000283 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000284 assert(FnType && "Overloaded operator has no proto function type.");
285 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregor77da3f42009-10-13 23:45:19 +0000286
287 // Copy assignment operators must be non-templates.
288 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
289 return;
290
Sebastian Redl64b45f72009-01-05 20:52:13 +0000291 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000292 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000293 ArgType = Ref->getPointeeType();
294
295 ArgType = ArgType.getUnqualifiedType();
296 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
297 const_cast<CXXRecordDecl*>(this)));
298
Douglas Gregora4923eb2009-11-16 21:35:15 +0000299 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000300 return;
301
302 // This is a copy assignment operator.
Eli Friedman88fad632009-11-07 00:02:45 +0000303 // Note on the decl that it is a copy assignment operator.
304 OpDecl->setCopyAssignment(true);
305
Sebastian Redl64b45f72009-01-05 20:52:13 +0000306 // Suppress the implicit declaration of a copy constructor.
John McCall86ff3082010-02-04 22:26:26 +0000307 data().UserDeclaredCopyAssignment = true;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000308
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000309 // C++ [class.copy]p11:
310 // A copy assignment operator is trivial if it is implicitly declared.
311 // FIXME: C++0x: don't do this for "= default" copy operators.
John McCall86ff3082010-02-04 22:26:26 +0000312 data().HasTrivialCopyAssignment = false;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000313
Sebastian Redl64b45f72009-01-05 20:52:13 +0000314 // C++ [class]p4:
315 // A POD-struct is an aggregate class that [...] has no user-defined copy
316 // assignment operator [...].
John McCall86ff3082010-02-04 22:26:26 +0000317 data().PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000318}
319
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000320void
321CXXRecordDecl::collectConversionFunctions(
John McCallba135432009-11-21 08:51:07 +0000322 llvm::SmallPtrSet<CanQualType, 8>& ConversionsTypeSet) const
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000323{
John McCalleec51cf2010-01-20 00:46:10 +0000324 const UnresolvedSetImpl *Cs = getConversionFunctions();
325 for (UnresolvedSetImpl::iterator I = Cs->begin(), E = Cs->end();
326 I != E; ++I) {
John McCallba135432009-11-21 08:51:07 +0000327 NamedDecl *TopConv = *I;
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000328 CanQualType TConvType;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000329 if (FunctionTemplateDecl *TConversionTemplate =
330 dyn_cast<FunctionTemplateDecl>(TopConv))
331 TConvType =
332 getASTContext().getCanonicalType(
333 TConversionTemplate->getTemplatedDecl()->getResultType());
334 else
335 TConvType =
336 getASTContext().getCanonicalType(
337 cast<CXXConversionDecl>(TopConv)->getConversionType());
338 ConversionsTypeSet.insert(TConvType);
339 }
340}
341
Fariborz Jahanian62509212009-09-12 18:26:03 +0000342/// getNestedVisibleConversionFunctions - imports unique conversion
343/// functions from base classes into the visible conversion function
344/// list of the class 'RD'. This is a private helper method.
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000345/// TopConversionsTypeSet is the set of conversion functions of the class
346/// we are interested in. HiddenConversionTypes is set of conversion functions
347/// of the immediate derived class which hides the conversion functions found
348/// in current class.
Fariborz Jahanian62509212009-09-12 18:26:03 +0000349void
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000350CXXRecordDecl::getNestedVisibleConversionFunctions(CXXRecordDecl *RD,
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000351 const llvm::SmallPtrSet<CanQualType, 8> &TopConversionsTypeSet,
352 const llvm::SmallPtrSet<CanQualType, 8> &HiddenConversionTypes)
353{
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000354 bool inTopClass = (RD == this);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000355 QualType ClassType = getASTContext().getTypeDeclType(this);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000356 if (const RecordType *Record = ClassType->getAs<RecordType>()) {
John McCalleec51cf2010-01-20 00:46:10 +0000357 const UnresolvedSetImpl *Cs
Fariborz Jahanian53462782009-09-11 21:44:33 +0000358 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000359
John McCalleec51cf2010-01-20 00:46:10 +0000360 for (UnresolvedSetImpl::iterator I = Cs->begin(), E = Cs->end();
361 I != E; ++I) {
John McCallba135432009-11-21 08:51:07 +0000362 NamedDecl *Conv = *I;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000363 // Only those conversions not exact match of conversions in current
364 // class are candidateconversion routines.
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000365 CanQualType ConvType;
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000366 if (FunctionTemplateDecl *ConversionTemplate =
367 dyn_cast<FunctionTemplateDecl>(Conv))
368 ConvType =
369 getASTContext().getCanonicalType(
Fariborz Jahaniana5c12942009-09-15 23:02:16 +0000370 ConversionTemplate->getTemplatedDecl()->getResultType());
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000371 else
372 ConvType =
Fariborz Jahanian8b915e72009-09-15 22:15:23 +0000373 getASTContext().getCanonicalType(
374 cast<CXXConversionDecl>(Conv)->getConversionType());
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000375 // We only add conversion functions found in the base class if they
376 // are not hidden by those found in HiddenConversionTypes which are
377 // the conversion functions in its derived class.
378 if (inTopClass ||
379 (!TopConversionsTypeSet.count(ConvType) &&
380 !HiddenConversionTypes.count(ConvType)) ) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000381 if (FunctionTemplateDecl *ConversionTemplate =
382 dyn_cast<FunctionTemplateDecl>(Conv))
383 RD->addVisibleConversionFunction(ConversionTemplate);
384 else
385 RD->addVisibleConversionFunction(cast<CXXConversionDecl>(Conv));
Fariborz Jahanian53462782009-09-11 21:44:33 +0000386 }
387 }
388 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000389
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000390 if (getNumBases() == 0 && getNumVBases() == 0)
391 return;
Sebastian Redl9994a342009-10-25 17:03:50 +0000392
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000393 llvm::SmallPtrSet<CanQualType, 8> ConversionFunctions;
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000394 if (!inTopClass)
395 collectConversionFunctions(ConversionFunctions);
Sebastian Redl9994a342009-10-25 17:03:50 +0000396
Fariborz Jahanian53462782009-09-11 21:44:33 +0000397 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(),
398 E = vbases_end(); VBase != E; ++VBase) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000399 if (const RecordType *RT = VBase->getType()->getAs<RecordType>()) {
400 CXXRecordDecl *VBaseClassDecl
401 = cast<CXXRecordDecl>(RT->getDecl());
402 VBaseClassDecl->getNestedVisibleConversionFunctions(RD,
403 TopConversionsTypeSet,
404 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
405 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000406 }
407 for (CXXRecordDecl::base_class_iterator Base = bases_begin(),
408 E = bases_end(); Base != E; ++Base) {
409 if (Base->isVirtual())
410 continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000411 if (const RecordType *RT = Base->getType()->getAs<RecordType>()) {
412 CXXRecordDecl *BaseClassDecl
413 = cast<CXXRecordDecl>(RT->getDecl());
414
415 BaseClassDecl->getNestedVisibleConversionFunctions(RD,
416 TopConversionsTypeSet,
417 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
418 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000419 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000420}
421
422/// getVisibleConversionFunctions - get all conversion functions visible
423/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000424const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000425 // If root class, all conversions are visible.
426 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000427 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000428 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000429 if (data().ComputedVisibleConversions)
430 return &data().VisibleConversions;
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000431 llvm::SmallPtrSet<CanQualType, 8> TopConversionsTypeSet;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000432 collectConversionFunctions(TopConversionsTypeSet);
433 getNestedVisibleConversionFunctions(this, TopConversionsTypeSet,
434 TopConversionsTypeSet);
John McCall86ff3082010-02-04 22:26:26 +0000435 data().ComputedVisibleConversions = true;
436 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000437}
438
Fariborz Jahanian62509212009-09-12 18:26:03 +0000439void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000440 CXXConversionDecl *ConvDecl) {
441 assert(!ConvDecl->getDescribedFunctionTemplate() &&
442 "Conversion function templates should cast to FunctionTemplateDecl.");
John McCall86ff3082010-02-04 22:26:26 +0000443 data().VisibleConversions.addDecl(ConvDecl);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000444}
445
Fariborz Jahanian62509212009-09-12 18:26:03 +0000446void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000447 FunctionTemplateDecl *ConvDecl) {
448 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
449 "Function template is not a conversion function template");
John McCall86ff3082010-02-04 22:26:26 +0000450 data().VisibleConversions.addDecl(ConvDecl);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000451}
452
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000453void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000454 assert(!ConvDecl->getDescribedFunctionTemplate() &&
455 "Conversion function templates should cast to FunctionTemplateDecl.");
John McCall86ff3082010-02-04 22:26:26 +0000456 data().Conversions.addDecl(ConvDecl);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000457}
458
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000459void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000460 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
461 "Function template is not a conversion function template");
John McCall86ff3082010-02-04 22:26:26 +0000462 data().Conversions.addDecl(ConvDecl);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000463}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000464
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000465
466void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
467 Method->setVirtualAsWritten(true);
468 setAggregate(false);
469 setPOD(false);
470 setEmpty(false);
471 setPolymorphic(true);
472 setHasTrivialConstructor(false);
473 setHasTrivialCopyConstructor(false);
474 setHasTrivialCopyAssignment(false);
475}
476
Douglas Gregorf6b11852009-10-08 15:14:33 +0000477CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000478 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000479 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
480
481 return 0;
482}
483
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000484MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
485 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
486}
487
Douglas Gregorf6b11852009-10-08 15:14:33 +0000488void
489CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
490 TemplateSpecializationKind TSK) {
491 assert(TemplateOrInstantiation.isNull() &&
492 "Previous template or instantiation?");
493 assert(!isa<ClassTemplateSpecializationDecl>(this));
494 TemplateOrInstantiation
495 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
496}
497
Anders Carlssonb13e3572009-12-07 06:33:48 +0000498TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
499 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000500 = dyn_cast<ClassTemplateSpecializationDecl>(this))
501 return Spec->getSpecializationKind();
502
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000503 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000504 return MSInfo->getTemplateSpecializationKind();
505
506 return TSK_Undeclared;
507}
508
509void
510CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
511 if (ClassTemplateSpecializationDecl *Spec
512 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
513 Spec->setSpecializationKind(TSK);
514 return;
515 }
516
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000517 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000518 MSInfo->setTemplateSpecializationKind(TSK);
519 return;
520 }
521
522 assert(false && "Not a class template or member class specialization");
523}
524
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000525CXXConstructorDecl *
526CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
527 QualType ClassType = Context.getTypeDeclType(this);
528 DeclarationName ConstructorName
529 = Context.DeclarationNames.getCXXConstructorName(
530 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000532 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000533 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000534 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000535 // FIXME: In C++0x, a constructor template can be a default constructor.
536 if (isa<FunctionTemplateDecl>(*Con))
537 continue;
538
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000539 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
540 if (Constructor->isDefaultConstructor())
541 return Constructor;
542 }
543 return 0;
544}
545
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000546CXXDestructorDecl *CXXRecordDecl::getDestructor(ASTContext &Context) {
Anders Carlsson7267c162009-05-29 21:03:38 +0000547 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000548
549 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000550 = Context.DeclarationNames.getCXXDestructorName(
551 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000552
553 DeclContext::lookup_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000554 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000555 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000557 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000558 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Anders Carlsson7267c162009-05-29 21:03:38 +0000560 return Dtor;
561}
562
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000563CXXMethodDecl *
564CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000565 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000566 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000567 bool isStatic, bool isInline) {
John McCalla93c9342009-12-07 02:54:59 +0000568 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000569 isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000570}
571
Douglas Gregor90916562009-09-29 18:16:17 +0000572bool CXXMethodDecl::isUsualDeallocationFunction() const {
573 if (getOverloadedOperator() != OO_Delete &&
574 getOverloadedOperator() != OO_Array_Delete)
575 return false;
576
577 // C++ [basic.stc.dynamic.deallocation]p2:
578 // If a class T has a member deallocation function named operator delete
579 // with exactly one parameter, then that function is a usual (non-placement)
580 // deallocation function. [...]
581 if (getNumParams() == 1)
582 return true;
583
584 // C++ [basic.stc.dynamic.deallocation]p2:
585 // [...] If class T does not declare such an operator delete but does
586 // declare a member deallocation function named operator delete with
587 // exactly two parameters, the second of which has type std::size_t (18.1),
588 // then this function is a usual deallocation function.
589 ASTContext &Context = getASTContext();
590 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000591 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
592 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000593 return false;
594
595 // This function is a usual deallocation function if there are no
596 // single-parameter deallocation functions of the same kind.
597 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
598 R.first != R.second; ++R.first) {
599 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
600 if (FD->getNumParams() == 1)
601 return false;
602 }
603
604 return true;
605}
606
Mike Stump1eb44332009-09-09 15:08:12 +0000607typedef llvm::DenseMap<const CXXMethodDecl*,
608 std::vector<const CXXMethodDecl *> *>
Anders Carlsson05eb2442009-05-16 23:58:37 +0000609 OverriddenMethodsMapTy;
610
Mike Stumpb9871a22009-08-21 01:45:00 +0000611// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
612// the vtable building code uses it at CG time.
Anders Carlsson05eb2442009-05-16 23:58:37 +0000613static OverriddenMethodsMapTy *OverriddenMethods = 0;
614
615void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000616 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000617 assert(!MD->getParent()->isDependentContext() &&
618 "Can't add an overridden method to a class template!");
619
Anders Carlsson05eb2442009-05-16 23:58:37 +0000620 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Anders Carlsson05eb2442009-05-16 23:58:37 +0000622 if (!OverriddenMethods)
623 OverriddenMethods = new OverriddenMethodsMapTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Anders Carlsson05eb2442009-05-16 23:58:37 +0000625 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
626 if (!Methods)
627 Methods = new std::vector<const CXXMethodDecl *>;
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Anders Carlsson05eb2442009-05-16 23:58:37 +0000629 Methods->push_back(MD);
630}
631
632CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
633 if (!OverriddenMethods)
634 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Anders Carlsson05eb2442009-05-16 23:58:37 +0000636 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000637 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000638 return 0;
Daniel Dunbar0908c332009-08-01 23:40:20 +0000639
Anders Carlsson05eb2442009-05-16 23:58:37 +0000640 return &(*it->second)[0];
641}
642
643CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
644 if (!OverriddenMethods)
645 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Anders Carlsson05eb2442009-05-16 23:58:37 +0000647 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000648 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000649 return 0;
650
Daniel Dunbar637ec322009-08-02 01:48:29 +0000651 return &(*it->second)[0] + it->second->size();
Anders Carlsson05eb2442009-05-16 23:58:37 +0000652}
653
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000654QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000655 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
656 // If the member function is declared const, the type of this is const X*,
657 // if the member function is declared volatile, the type of this is
658 // volatile X*, and if the member function is declared const volatile,
659 // the type of this is const volatile X*.
660
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000661 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000662
663 QualType ClassTy;
664 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
665 ClassTy = TD->getInjectedClassNameType(C);
666 else
Mike Stumpe607ed02009-08-07 18:05:12 +0000667 ClassTy = C.getTagDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000668 ClassTy = C.getQualifiedType(ClassTy,
669 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000670 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000671}
672
Eli Friedmand7d7f672009-12-06 20:50:05 +0000673bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000674 // If this function is a template instantiation, look at the template from
675 // which it was instantiated.
676 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
677 if (!CheckFn)
678 CheckFn = this;
679
Eli Friedmand7d7f672009-12-06 20:50:05 +0000680 const FunctionDecl *fn;
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000681 return CheckFn->getBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000682}
683
Douglas Gregor7ad83902008-11-05 04:29:56 +0000684CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000685CXXBaseOrMemberInitializer(ASTContext &Context,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000686 TypeSourceInfo *TInfo,
687 SourceLocation L, Expr *Init, SourceLocation R)
688 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
Douglas Gregor802ab452009-12-02 22:36:29 +0000689 LParenLoc(L), RParenLoc(R)
690{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000691}
692
693CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000694CXXBaseOrMemberInitializer(ASTContext &Context,
695 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000696 SourceLocation L, Expr *Init, SourceLocation R)
697 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
698 AnonUnionMember(0), LParenLoc(L), RParenLoc(R)
Douglas Gregor802ab452009-12-02 22:36:29 +0000699{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000700}
701
Douglas Gregor802ab452009-12-02 22:36:29 +0000702void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000703 if (Init)
704 Init->Destroy(Context);
Douglas Gregor802ab452009-12-02 22:36:29 +0000705 this->~CXXBaseOrMemberInitializer();
706}
707
708TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
709 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000710 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000711 else
712 return TypeLoc();
713}
714
715Type *CXXBaseOrMemberInitializer::getBaseClass() {
716 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000717 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000718 else
719 return 0;
720}
721
722const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
723 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000724 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000725 else
726 return 0;
727}
728
729SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
730 if (isMemberInitializer())
731 return getMemberLocation();
732
733 return getBaseClassLoc().getSourceRange().getBegin();
734}
735
736SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
737 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000738}
739
Douglas Gregorb48fe382008-10-31 09:07:45 +0000740CXXConstructorDecl *
741CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000742 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000743 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000744 bool isExplicit,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000745 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000746 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
747 "Name must refer to a constructor");
John McCalla93c9342009-12-07 02:54:59 +0000748 return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000749 isImplicitlyDeclared);
750}
751
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000752bool CXXConstructorDecl::isDefaultConstructor() const {
753 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000754 // A default constructor for a class X is a constructor of class
755 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000756 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000757 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000758}
759
Mike Stump1eb44332009-09-09 15:08:12 +0000760bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000761CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000762 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000763 // A non-template constructor for class X is a copy constructor
764 // if its first parameter is of type X&, const X&, volatile X& or
765 // const volatile X&, and either there are no other parameters
766 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000767 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000768 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000769 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000770 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000771 return false;
772
773 const ParmVarDecl *Param = getParamDecl(0);
774
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000775 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000776 const LValueReferenceType *ParamRefType =
777 Param->getType()->getAs<LValueReferenceType>();
778 if (!ParamRefType)
779 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000780
Douglas Gregorfd476482009-11-13 23:59:09 +0000781 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000782 ASTContext &Context = getASTContext();
783
Douglas Gregorfd476482009-11-13 23:59:09 +0000784 CanQualType PointeeType
785 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000786 CanQualType ClassTy
787 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000788 if (PointeeType.getUnqualifiedType() != ClassTy)
789 return false;
790
John McCall0953e762009-09-24 19:53:00 +0000791 // FIXME: other qualifiers?
792
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000793 // We have a copy constructor.
794 TypeQuals = PointeeType.getCVRQualifiers();
795 return true;
796}
797
Anders Carlssonfaccd722009-08-28 16:57:08 +0000798bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000799 // C++ [class.conv.ctor]p1:
800 // A constructor declared without the function-specifier explicit
801 // that can be called with a single parameter specifies a
802 // conversion from the type of its first parameter to the type of
803 // its class. Such a constructor is called a converting
804 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000805 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000806 return false;
807
Mike Stump1eb44332009-09-09 15:08:12 +0000808 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000809 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000810 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000811 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000812}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000813
Douglas Gregor66724ea2009-11-14 01:20:54 +0000814bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
815 if ((getNumParams() < 1) ||
816 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
817 (getPrimaryTemplate() == 0) ||
818 (getDescribedFunctionTemplate() != 0))
819 return false;
820
821 const ParmVarDecl *Param = getParamDecl(0);
822
823 ASTContext &Context = getASTContext();
824 CanQualType ParamType = Context.getCanonicalType(Param->getType());
825
826 // Strip off the lvalue reference, if any.
827 if (CanQual<LValueReferenceType> ParamRefType
828 = ParamType->getAs<LValueReferenceType>())
829 ParamType = ParamRefType->getPointeeType();
830
831
832 // Is it the same as our our class type?
833 CanQualType ClassTy
834 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
835 if (ParamType.getUnqualifiedType() != ClassTy)
836 return false;
837
838 return true;
839}
840
Douglas Gregor42a552f2008-11-05 20:51:48 +0000841CXXDestructorDecl *
842CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000843 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000844 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000845 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000846 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
847 "Name must refer to a destructor");
Mike Stump1eb44332009-09-09 15:08:12 +0000848 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff3e970492009-01-27 21:25:57 +0000849 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000850}
851
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000852void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000853CXXConstructorDecl::Destroy(ASTContext& C) {
854 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000855 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000856}
857
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000858CXXConversionDecl *
859CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000860 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000861 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000862 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000863 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
864 "Name must refer to a conversion function");
John McCalla93c9342009-12-07 02:54:59 +0000865 return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000866}
867
John McCall02cace72009-08-28 07:59:38 +0000868FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
869 SourceLocation L,
870 FriendUnion Friend,
871 SourceLocation FriendL) {
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000872#ifndef NDEBUG
John McCall02cace72009-08-28 07:59:38 +0000873 if (Friend.is<NamedDecl*>()) {
874 NamedDecl *D = Friend.get<NamedDecl*>();
875 assert(isa<FunctionDecl>(D) ||
876 isa<CXXRecordDecl>(D) ||
877 isa<FunctionTemplateDecl>(D) ||
878 isa<ClassTemplateDecl>(D));
John McCalle129d442009-12-17 23:21:11 +0000879
880 // As a temporary hack, we permit template instantiation to point
881 // to the original declaration when instantiating members.
882 assert(D->getFriendObjectKind() ||
883 (cast<CXXRecordDecl>(DC)->getTemplateSpecializationKind()));
John McCall02cace72009-08-28 07:59:38 +0000884 }
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000885#endif
John McCallc48fbdf2009-08-11 21:13:21 +0000886
John McCall02cace72009-08-28 07:59:38 +0000887 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump1eb44332009-09-09 15:08:12 +0000888}
John McCall3f9a8a62009-08-11 06:59:38 +0000889
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000890LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000891 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000892 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000893 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000894 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000895}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000896
897UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
898 SourceLocation L,
899 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000900 SourceRange QualifierRange,
901 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000902 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000903 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000904 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000905 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
906 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000907 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000908 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000909}
910
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000911NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
912 if (NamespaceAliasDecl *NA =
913 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
914 return NA->getNamespace();
915 return cast_or_null<NamespaceDecl>(NominatedNamespace);
916}
917
Mike Stump1eb44332009-09-09 15:08:12 +0000918NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
919 SourceLocation L,
920 SourceLocation AliasLoc,
921 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000922 SourceRange QualifierRange,
923 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +0000924 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +0000925 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000926 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
927 Namespace = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000928 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000929 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000930}
931
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000932UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
John McCall9488ea12009-11-17 05:59:44 +0000933 SourceLocation L, SourceRange NNR, SourceLocation UL,
934 NestedNameSpecifier* TargetNNS, DeclarationName Name,
935 bool IsTypeNameArg) {
936 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000937}
938
John McCall7ba107a2009-11-18 02:36:19 +0000939UnresolvedUsingValueDecl *
940UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
941 SourceLocation UsingLoc,
942 SourceRange TargetNNR,
943 NestedNameSpecifier *TargetNNS,
944 SourceLocation TargetNameLoc,
945 DeclarationName TargetName) {
946 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
947 TargetNNR, TargetNNS,
948 TargetNameLoc, TargetName);
949}
950
951UnresolvedUsingTypenameDecl *
952UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
953 SourceLocation UsingLoc,
954 SourceLocation TypenameLoc,
955 SourceRange TargetNNR,
956 NestedNameSpecifier *TargetNNS,
957 SourceLocation TargetNameLoc,
958 DeclarationName TargetName) {
959 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
960 TargetNNR, TargetNNS,
961 TargetNameLoc,
962 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +0000963}
964
Anders Carlssonfb311762009-03-14 00:25:26 +0000965StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
966 SourceLocation L, Expr *AssertExpr,
967 StringLiteral *Message) {
968 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
969}
970
971void StaticAssertDecl::Destroy(ASTContext& C) {
972 AssertExpr->Destroy(C);
973 Message->Destroy(C);
974 this->~StaticAssertDecl();
975 C.Deallocate((void *)this);
976}
977
978StaticAssertDecl::~StaticAssertDecl() {
979}
980
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000981static const char *getAccessName(AccessSpecifier AS) {
982 switch (AS) {
983 default:
984 case AS_none:
985 assert("Invalid access specifier!");
986 return 0;
987 case AS_public:
988 return "public";
989 case AS_private:
990 return "private";
991 case AS_protected:
992 return "protected";
993 }
994}
995
996const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
997 AccessSpecifier AS) {
998 return DB << getAccessName(AS);
999}
1000
1001