blob: 40019880074a77e8633fd94fe92e201a2ef68292 [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 Gregor7d7e6722008-11-12 23:21:09 +000018#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000019#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000020#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// Decl Allocation/Deallocation Method Implementations
25//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000026
Douglas Gregor3e00bad2009-02-17 01:05:43 +000027CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000028 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000029 CXXRecordDecl *PrevDecl,
Mike Stump1eb44332009-09-09 15:08:12 +000030 SourceLocation TKL)
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000031 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
Douglas Gregor7d7e6722008-11-12 23:21:09 +000032 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000033 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000034 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
35 Abstract(false), HasTrivialConstructor(true),
36 HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true),
Fariborz Jahanian62509212009-09-12 18:26:03 +000037 HasTrivialDestructor(true), ComputedVisibleConversions(false),
38 Bases(0), NumBases(0), VBases(0), NumVBases(0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000039 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000040
Ted Kremenek4b7c9832008-09-05 17:16:31 +000041CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
42 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000043 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000044 CXXRecordDecl* PrevDecl,
45 bool DelayTypeCreation) {
Mike Stump1eb44332009-09-09 15:08:12 +000046 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000047 PrevDecl, TKL);
Mike Stump1eb44332009-09-09 15:08:12 +000048
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000049 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000050 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000051 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000052 return R;
53}
54
Douglas Gregorf8268ae2008-10-22 17:49:05 +000055CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000056}
57
58void CXXRecordDecl::Destroy(ASTContext &C) {
59 C.Deallocate(Bases);
Fariborz Jahanian71c6e712009-07-22 17:41:53 +000060 C.Deallocate(VBases);
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000061 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000062}
63
Mike Stump1eb44332009-09-09 15:08:12 +000064void
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000065CXXRecordDecl::setBases(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +000066 CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000067 unsigned NumBases) {
Mike Stump1eb44332009-09-09 15:08:12 +000068 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000069 // An aggregate is an array or a class (clause 9) with [...]
70 // no base classes [...].
71 Aggregate = false;
72
Douglas Gregor57c856b2008-10-23 18:13:27 +000073 if (this->Bases)
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000074 C.Deallocate(this->Bases);
Mike Stump1eb44332009-09-09 15:08:12 +000075
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000076 int vbaseCount = 0;
77 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
78 bool hasDirectVirtualBase = false;
Mike Stump1eb44332009-09-09 15:08:12 +000079
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000080 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor57c856b2008-10-23 18:13:27 +000081 this->NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000082 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor57c856b2008-10-23 18:13:27 +000083 this->Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000084 // Keep track of inherited vbases for this base class.
85 const CXXBaseSpecifier *Base = Bases[i];
86 QualType BaseType = Base->getType();
Mike Stump1eb44332009-09-09 15:08:12 +000087 // Skip template types.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000088 // FIXME. This means that this list must be rebuilt during template
89 // instantiation.
90 if (BaseType->isDependentType())
91 continue;
92 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +000093 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000094 if (Base->isVirtual())
95 hasDirectVirtualBase = true;
Mike Stump1eb44332009-09-09 15:08:12 +000096 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000097 BaseClassDecl->vbases_begin(),
98 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Mike Stump1eb44332009-09-09 15:08:12 +000099 // Add this vbase to the array of vbases for current class if it is
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000100 // not already in the list.
101 // FIXME. Note that we do a linear search as number of such classes are
102 // very few.
103 int i;
104 for (i = 0; i < vbaseCount; ++i)
105 if (UniqueVbases[i]->getType() == VBase->getType())
106 break;
107 if (i == vbaseCount) {
108 UniqueVbases.push_back(VBase);
109 ++vbaseCount;
110 }
111 }
112 }
113 if (hasDirectVirtualBase) {
114 // Iterate one more time through the direct bases and add the virtual
115 // base to the list of vritual bases for current class.
116 for (unsigned i = 0; i < NumBases; ++i) {
117 const CXXBaseSpecifier *VBase = Bases[i];
118 if (!VBase->isVirtual())
119 continue;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000120 int j;
121 for (j = 0; j < vbaseCount; ++j)
122 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000123 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000124 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000125 UniqueVbases.push_back(VBase);
126 ++vbaseCount;
127 }
128 }
129 }
130 if (vbaseCount > 0) {
131 // build AST for inhireted, direct or indirect, virtual bases.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000132 this->VBases = new (C) CXXBaseSpecifier [vbaseCount];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000133 this->NumVBases = vbaseCount;
134 for (int i = 0; i < vbaseCount; i++) {
135 QualType QT = UniqueVbases[i]->getType();
136 CXXRecordDecl *VBaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000137 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000138 this->VBases[i] =
Douglas Gregor2aef06d2009-07-22 20:55:49 +0000139 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
140 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
141 UniqueVbases[i]->getAccessSpecifier(), QT);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000142 }
143 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000144}
145
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000146bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000147 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000148}
149
Mike Stump1eb44332009-09-09 15:08:12 +0000150CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000151 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000152 QualType ClassType
153 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000154 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000155 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000156 Context.getCanonicalType(ClassType));
157 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000158 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000159 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000160 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000161 // C++ [class.copy]p2:
162 // A non-template constructor for class X is a copy constructor if [...]
163 if (isa<FunctionTemplateDecl>(*Con))
164 continue;
165
Mike Stump1eb44332009-09-09 15:08:12 +0000166 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000167 FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000168 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
169 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000170 return cast<CXXConstructorDecl>(*Con);
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000172 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000173 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000174 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000175}
176
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000177bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
178 const CXXMethodDecl *& MD) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000179 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
180 const_cast<CXXRecordDecl*>(this)));
181 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
182
183 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000184 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000185 Op != OpEnd; ++Op) {
186 // C++ [class.copy]p9:
187 // A user-declared copy assignment operator is a non-static non-template
188 // member function of class X with exactly one parameter of type X, X&,
189 // const X&, volatile X& or const volatile X&.
Douglas Gregor682054c2009-10-30 22:48:49 +0000190 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
191 if (!Method)
192 continue;
193
Sebastian Redl64b45f72009-01-05 20:52:13 +0000194 if (Method->isStatic())
195 continue;
Douglas Gregor77da3f42009-10-13 23:45:19 +0000196 if (Method->getPrimaryTemplate())
197 continue;
Douglas Gregor72564e72009-02-26 23:50:07 +0000198 const FunctionProtoType *FnType =
John McCall183700f2009-09-21 23:43:11 +0000199 Method->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000200 assert(FnType && "Overloaded operator has no prototype.");
201 // Don't assert on this; an invalid decl might have been left in the AST.
202 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
203 continue;
204 bool AcceptsConst = true;
205 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000206 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000207 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000208 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000209 if (!ArgType.isConstQualified())
210 AcceptsConst = false;
211 }
Douglas Gregora4923eb2009-11-16 21:35:15 +0000212 if (!Context.hasSameUnqualifiedType(ArgType, ClassType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000213 continue;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000214 MD = Method;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000215 // We have a single argument of type cv X or cv X&, i.e. we've found the
216 // copy assignment operator. Return whether it accepts const arguments.
217 return AcceptsConst;
218 }
219 assert(isInvalidDecl() &&
220 "No copy assignment operator declared in valid code.");
221 return false;
222}
223
224void
Mike Stump1eb44332009-09-09 15:08:12 +0000225CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000226 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000227 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
228 // Note that we have a user-declared constructor.
229 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000230
Mike Stump1eb44332009-09-09 15:08:12 +0000231 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000232 // An aggregate is an array or a class (clause 9) with no
233 // user-declared constructors (12.1) [...].
234 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000235
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000236 // C++ [class]p4:
237 // A POD-struct is an aggregate class [...]
238 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000239
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000240 // C++ [class.ctor]p5:
241 // A constructor is trivial if it is an implicitly-declared default
242 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000243 // FIXME: C++0x: don't do this for "= default" default constructors.
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000244 HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000246 // Note when we have a user-declared copy constructor, which will
247 // suppress the implicit declaration of a copy constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000248 if (ConDecl->isCopyConstructor(Context)) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000249 UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000250
251 // C++ [class.copy]p6:
252 // A copy constructor is trivial if it is implicitly declared.
253 // FIXME: C++0x: don't do this for "= default" copy constructors.
254 HasTrivialCopyConstructor = false;
255 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000256}
257
Sebastian Redl64b45f72009-01-05 20:52:13 +0000258void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
259 CXXMethodDecl *OpDecl) {
260 // We're interested specifically in copy assignment operators.
John McCall183700f2009-09-21 23:43:11 +0000261 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000262 assert(FnType && "Overloaded operator has no proto function type.");
263 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregor77da3f42009-10-13 23:45:19 +0000264
265 // Copy assignment operators must be non-templates.
266 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
267 return;
268
Sebastian Redl64b45f72009-01-05 20:52:13 +0000269 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000270 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000271 ArgType = Ref->getPointeeType();
272
273 ArgType = ArgType.getUnqualifiedType();
274 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
275 const_cast<CXXRecordDecl*>(this)));
276
Douglas Gregora4923eb2009-11-16 21:35:15 +0000277 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000278 return;
279
280 // This is a copy assignment operator.
Eli Friedman88fad632009-11-07 00:02:45 +0000281 // Note on the decl that it is a copy assignment operator.
282 OpDecl->setCopyAssignment(true);
283
Sebastian Redl64b45f72009-01-05 20:52:13 +0000284 // Suppress the implicit declaration of a copy constructor.
285 UserDeclaredCopyAssignment = true;
286
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000287 // C++ [class.copy]p11:
288 // A copy assignment operator is trivial if it is implicitly declared.
289 // FIXME: C++0x: don't do this for "= default" copy operators.
290 HasTrivialCopyAssignment = false;
291
Sebastian Redl64b45f72009-01-05 20:52:13 +0000292 // C++ [class]p4:
293 // A POD-struct is an aggregate class that [...] has no user-defined copy
294 // assignment operator [...].
295 PlainOldData = false;
296}
297
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000298void
299CXXRecordDecl::collectConversionFunctions(
John McCallba135432009-11-21 08:51:07 +0000300 llvm::SmallPtrSet<CanQualType, 8>& ConversionsTypeSet) const
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000301{
John McCallba135432009-11-21 08:51:07 +0000302 const UnresolvedSet *Cs = getConversionFunctions();
303 for (UnresolvedSet::iterator I = Cs->begin(), E = Cs->end(); I != E; ++I) {
304 NamedDecl *TopConv = *I;
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000305 CanQualType TConvType;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000306 if (FunctionTemplateDecl *TConversionTemplate =
307 dyn_cast<FunctionTemplateDecl>(TopConv))
308 TConvType =
309 getASTContext().getCanonicalType(
310 TConversionTemplate->getTemplatedDecl()->getResultType());
311 else
312 TConvType =
313 getASTContext().getCanonicalType(
314 cast<CXXConversionDecl>(TopConv)->getConversionType());
315 ConversionsTypeSet.insert(TConvType);
316 }
317}
318
Fariborz Jahanian62509212009-09-12 18:26:03 +0000319/// getNestedVisibleConversionFunctions - imports unique conversion
320/// functions from base classes into the visible conversion function
321/// list of the class 'RD'. This is a private helper method.
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000322/// TopConversionsTypeSet is the set of conversion functions of the class
323/// we are interested in. HiddenConversionTypes is set of conversion functions
324/// of the immediate derived class which hides the conversion functions found
325/// in current class.
Fariborz Jahanian62509212009-09-12 18:26:03 +0000326void
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000327CXXRecordDecl::getNestedVisibleConversionFunctions(CXXRecordDecl *RD,
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000328 const llvm::SmallPtrSet<CanQualType, 8> &TopConversionsTypeSet,
329 const llvm::SmallPtrSet<CanQualType, 8> &HiddenConversionTypes)
330{
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000331 bool inTopClass = (RD == this);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000332 QualType ClassType = getASTContext().getTypeDeclType(this);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000333 if (const RecordType *Record = ClassType->getAs<RecordType>()) {
John McCallba135432009-11-21 08:51:07 +0000334 const UnresolvedSet *Cs
Fariborz Jahanian53462782009-09-11 21:44:33 +0000335 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000336
John McCallba135432009-11-21 08:51:07 +0000337 for (UnresolvedSet::iterator I = Cs->begin(), E = Cs->end(); I != E; ++I) {
338 NamedDecl *Conv = *I;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000339 // Only those conversions not exact match of conversions in current
340 // class are candidateconversion routines.
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000341 CanQualType ConvType;
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000342 if (FunctionTemplateDecl *ConversionTemplate =
343 dyn_cast<FunctionTemplateDecl>(Conv))
344 ConvType =
345 getASTContext().getCanonicalType(
Fariborz Jahaniana5c12942009-09-15 23:02:16 +0000346 ConversionTemplate->getTemplatedDecl()->getResultType());
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000347 else
348 ConvType =
Fariborz Jahanian8b915e72009-09-15 22:15:23 +0000349 getASTContext().getCanonicalType(
350 cast<CXXConversionDecl>(Conv)->getConversionType());
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000351 // We only add conversion functions found in the base class if they
352 // are not hidden by those found in HiddenConversionTypes which are
353 // the conversion functions in its derived class.
354 if (inTopClass ||
355 (!TopConversionsTypeSet.count(ConvType) &&
356 !HiddenConversionTypes.count(ConvType)) ) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000357 if (FunctionTemplateDecl *ConversionTemplate =
358 dyn_cast<FunctionTemplateDecl>(Conv))
359 RD->addVisibleConversionFunction(ConversionTemplate);
360 else
361 RD->addVisibleConversionFunction(cast<CXXConversionDecl>(Conv));
Fariborz Jahanian53462782009-09-11 21:44:33 +0000362 }
363 }
364 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000365
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000366 if (getNumBases() == 0 && getNumVBases() == 0)
367 return;
Sebastian Redl9994a342009-10-25 17:03:50 +0000368
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000369 llvm::SmallPtrSet<CanQualType, 8> ConversionFunctions;
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000370 if (!inTopClass)
371 collectConversionFunctions(ConversionFunctions);
Sebastian Redl9994a342009-10-25 17:03:50 +0000372
Fariborz Jahanian53462782009-09-11 21:44:33 +0000373 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(),
374 E = vbases_end(); VBase != E; ++VBase) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000375 if (const RecordType *RT = VBase->getType()->getAs<RecordType>()) {
376 CXXRecordDecl *VBaseClassDecl
377 = cast<CXXRecordDecl>(RT->getDecl());
378 VBaseClassDecl->getNestedVisibleConversionFunctions(RD,
379 TopConversionsTypeSet,
380 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
381 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000382 }
383 for (CXXRecordDecl::base_class_iterator Base = bases_begin(),
384 E = bases_end(); Base != E; ++Base) {
385 if (Base->isVirtual())
386 continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000387 if (const RecordType *RT = Base->getType()->getAs<RecordType>()) {
388 CXXRecordDecl *BaseClassDecl
389 = cast<CXXRecordDecl>(RT->getDecl());
390
391 BaseClassDecl->getNestedVisibleConversionFunctions(RD,
392 TopConversionsTypeSet,
393 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
394 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000395 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000396}
397
398/// getVisibleConversionFunctions - get all conversion functions visible
399/// in current class; including conversion function templates.
John McCallba135432009-11-21 08:51:07 +0000400const UnresolvedSet *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000401 // If root class, all conversions are visible.
402 if (bases_begin() == bases_end())
403 return &Conversions;
404 // If visible conversion list is already evaluated, return it.
405 if (ComputedVisibleConversions)
406 return &VisibleConversions;
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000407 llvm::SmallPtrSet<CanQualType, 8> TopConversionsTypeSet;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000408 collectConversionFunctions(TopConversionsTypeSet);
409 getNestedVisibleConversionFunctions(this, TopConversionsTypeSet,
410 TopConversionsTypeSet);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000411 ComputedVisibleConversions = true;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000412 return &VisibleConversions;
413}
414
Fariborz Jahanian62509212009-09-12 18:26:03 +0000415void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000416 CXXConversionDecl *ConvDecl) {
417 assert(!ConvDecl->getDescribedFunctionTemplate() &&
418 "Conversion function templates should cast to FunctionTemplateDecl.");
John McCallba135432009-11-21 08:51:07 +0000419 VisibleConversions.addDecl(ConvDecl);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000420}
421
Fariborz Jahanian62509212009-09-12 18:26:03 +0000422void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000423 FunctionTemplateDecl *ConvDecl) {
424 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
425 "Function template is not a conversion function template");
John McCallba135432009-11-21 08:51:07 +0000426 VisibleConversions.addDecl(ConvDecl);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000427}
428
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000429void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000430 assert(!ConvDecl->getDescribedFunctionTemplate() &&
431 "Conversion function templates should cast to FunctionTemplateDecl.");
John McCallba135432009-11-21 08:51:07 +0000432 Conversions.addDecl(ConvDecl);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000433}
434
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000435void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000436 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
437 "Function template is not a conversion function template");
John McCallba135432009-11-21 08:51:07 +0000438 Conversions.addDecl(ConvDecl);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000439}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000440
Douglas Gregorf6b11852009-10-08 15:14:33 +0000441CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000442 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000443 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
444
445 return 0;
446}
447
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000448MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
449 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
450}
451
Douglas Gregorf6b11852009-10-08 15:14:33 +0000452void
453CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
454 TemplateSpecializationKind TSK) {
455 assert(TemplateOrInstantiation.isNull() &&
456 "Previous template or instantiation?");
457 assert(!isa<ClassTemplateSpecializationDecl>(this));
458 TemplateOrInstantiation
459 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
460}
461
462TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() {
463 if (ClassTemplateSpecializationDecl *Spec
464 = dyn_cast<ClassTemplateSpecializationDecl>(this))
465 return Spec->getSpecializationKind();
466
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000467 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000468 return MSInfo->getTemplateSpecializationKind();
469
470 return TSK_Undeclared;
471}
472
473void
474CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
475 if (ClassTemplateSpecializationDecl *Spec
476 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
477 Spec->setSpecializationKind(TSK);
478 return;
479 }
480
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000481 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000482 MSInfo->setTemplateSpecializationKind(TSK);
483 return;
484 }
485
486 assert(false && "Not a class template or member class specialization");
487}
488
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000489CXXConstructorDecl *
490CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
491 QualType ClassType = Context.getTypeDeclType(this);
492 DeclarationName ConstructorName
493 = Context.DeclarationNames.getCXXConstructorName(
494 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000496 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000497 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000498 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000499 // FIXME: In C++0x, a constructor template can be a default constructor.
500 if (isa<FunctionTemplateDecl>(*Con))
501 continue;
502
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000503 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
504 if (Constructor->isDefaultConstructor())
505 return Constructor;
506 }
507 return 0;
508}
509
Anders Carlsson7267c162009-05-29 21:03:38 +0000510const CXXDestructorDecl *
511CXXRecordDecl::getDestructor(ASTContext &Context) {
512 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000513
514 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000515 = Context.DeclarationNames.getCXXDestructorName(
516 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000517
518 DeclContext::lookup_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000519 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000520 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Anders Carlsson7267c162009-05-29 21:03:38 +0000522 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
523 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Anders Carlsson7267c162009-05-29 21:03:38 +0000525 return Dtor;
526}
527
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000528CXXMethodDecl *
529CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000530 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000531 QualType T, DeclaratorInfo *DInfo,
532 bool isStatic, bool isInline) {
533 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, DInfo,
534 isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000535}
536
Douglas Gregor90916562009-09-29 18:16:17 +0000537bool CXXMethodDecl::isUsualDeallocationFunction() const {
538 if (getOverloadedOperator() != OO_Delete &&
539 getOverloadedOperator() != OO_Array_Delete)
540 return false;
541
542 // C++ [basic.stc.dynamic.deallocation]p2:
543 // If a class T has a member deallocation function named operator delete
544 // with exactly one parameter, then that function is a usual (non-placement)
545 // deallocation function. [...]
546 if (getNumParams() == 1)
547 return true;
548
549 // C++ [basic.stc.dynamic.deallocation]p2:
550 // [...] If class T does not declare such an operator delete but does
551 // declare a member deallocation function named operator delete with
552 // exactly two parameters, the second of which has type std::size_t (18.1),
553 // then this function is a usual deallocation function.
554 ASTContext &Context = getASTContext();
555 if (getNumParams() != 2 ||
556 !Context.hasSameType(getParamDecl(1)->getType(), Context.getSizeType()))
557 return false;
558
559 // This function is a usual deallocation function if there are no
560 // single-parameter deallocation functions of the same kind.
561 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
562 R.first != R.second; ++R.first) {
563 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
564 if (FD->getNumParams() == 1)
565 return false;
566 }
567
568 return true;
569}
570
Mike Stump1eb44332009-09-09 15:08:12 +0000571typedef llvm::DenseMap<const CXXMethodDecl*,
572 std::vector<const CXXMethodDecl *> *>
Anders Carlsson05eb2442009-05-16 23:58:37 +0000573 OverriddenMethodsMapTy;
574
Mike Stumpb9871a22009-08-21 01:45:00 +0000575// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
576// the vtable building code uses it at CG time.
Anders Carlsson05eb2442009-05-16 23:58:37 +0000577static OverriddenMethodsMapTy *OverriddenMethods = 0;
578
579void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
580 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Anders Carlsson05eb2442009-05-16 23:58:37 +0000582 if (!OverriddenMethods)
583 OverriddenMethods = new OverriddenMethodsMapTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Anders Carlsson05eb2442009-05-16 23:58:37 +0000585 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
586 if (!Methods)
587 Methods = new std::vector<const CXXMethodDecl *>;
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Anders Carlsson05eb2442009-05-16 23:58:37 +0000589 Methods->push_back(MD);
590}
591
592CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
593 if (!OverriddenMethods)
594 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Anders Carlsson05eb2442009-05-16 23:58:37 +0000596 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000597 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000598 return 0;
Daniel Dunbar0908c332009-08-01 23:40:20 +0000599
Anders Carlsson05eb2442009-05-16 23:58:37 +0000600 return &(*it->second)[0];
601}
602
603CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
604 if (!OverriddenMethods)
605 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Anders Carlsson05eb2442009-05-16 23:58:37 +0000607 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000608 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000609 return 0;
610
Daniel Dunbar637ec322009-08-02 01:48:29 +0000611 return &(*it->second)[0] + it->second->size();
Anders Carlsson05eb2442009-05-16 23:58:37 +0000612}
613
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000614QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000615 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
616 // If the member function is declared const, the type of this is const X*,
617 // if the member function is declared volatile, the type of this is
618 // volatile X*, and if the member function is declared const volatile,
619 // the type of this is const volatile X*.
620
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000621 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000622
623 QualType ClassTy;
624 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
625 ClassTy = TD->getInjectedClassNameType(C);
626 else
Mike Stumpe607ed02009-08-07 18:05:12 +0000627 ClassTy = C.getTagDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000628 ClassTy = C.getQualifiedType(ClassTy,
629 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000630 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000631}
632
Douglas Gregor7ad83902008-11-05 04:29:56 +0000633CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000634CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000635 CXXConstructorDecl *C,
Mike Stump1eb44332009-09-09 15:08:12 +0000636 SourceLocation L, SourceLocation R)
Douglas Gregor72f6d672009-09-01 21:04:42 +0000637 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000638 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
639 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
640 BaseOrMember |= 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Douglas Gregor7ad83902008-11-05 04:29:56 +0000642 if (NumArgs > 0) {
643 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000644 // FIXME. Allocation via Context
645 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000646 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
647 this->Args[Idx] = Args[Idx];
648 }
Douglas Gregor72f6d672009-09-01 21:04:42 +0000649 CtorOrAnonUnion = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000650}
651
652CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000653CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000654 CXXConstructorDecl *C,
Anders Carlsson8c57a662009-08-29 01:31:33 +0000655 SourceLocation L, SourceLocation R)
Douglas Gregor72f6d672009-09-01 21:04:42 +0000656 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000657 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
Mike Stump1eb44332009-09-09 15:08:12 +0000658 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
Douglas Gregor7ad83902008-11-05 04:29:56 +0000659
660 if (NumArgs > 0) {
661 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000662 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000663 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
664 this->Args[Idx] = Args[Idx];
665 }
Douglas Gregor72f6d672009-09-01 21:04:42 +0000666 CtorOrAnonUnion = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000667}
668
669CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
670 delete [] Args;
671}
672
Douglas Gregorb48fe382008-10-31 09:07:45 +0000673CXXConstructorDecl *
674CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000675 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000676 QualType T, DeclaratorInfo *DInfo,
677 bool isExplicit,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000678 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000679 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
680 "Name must refer to a constructor");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000681 return new (C) CXXConstructorDecl(RD, L, N, T, DInfo, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000682 isImplicitlyDeclared);
683}
684
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000685bool CXXConstructorDecl::isDefaultConstructor() const {
686 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000687 // A default constructor for a class X is a constructor of class
688 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000689 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000690 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000691}
692
Mike Stump1eb44332009-09-09 15:08:12 +0000693bool
694CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000695 unsigned &TypeQuals) const {
696 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000697 // A non-template constructor for class X is a copy constructor
698 // if its first parameter is of type X&, const X&, volatile X& or
699 // const volatile X&, and either there are no other parameters
700 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000701 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000702 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000703 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000704 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000705 return false;
706
707 const ParmVarDecl *Param = getParamDecl(0);
708
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000709 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000710 const LValueReferenceType *ParamRefType =
711 Param->getType()->getAs<LValueReferenceType>();
712 if (!ParamRefType)
713 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000714
Douglas Gregorfd476482009-11-13 23:59:09 +0000715 // Is it a reference to our class type?
716 CanQualType PointeeType
717 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000718 CanQualType ClassTy
719 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000720 if (PointeeType.getUnqualifiedType() != ClassTy)
721 return false;
722
John McCall0953e762009-09-24 19:53:00 +0000723 // FIXME: other qualifiers?
724
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000725 // We have a copy constructor.
726 TypeQuals = PointeeType.getCVRQualifiers();
727 return true;
728}
729
Anders Carlssonfaccd722009-08-28 16:57:08 +0000730bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000731 // C++ [class.conv.ctor]p1:
732 // A constructor declared without the function-specifier explicit
733 // that can be called with a single parameter specifies a
734 // conversion from the type of its first parameter to the type of
735 // its class. Such a constructor is called a converting
736 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000737 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000738 return false;
739
Mike Stump1eb44332009-09-09 15:08:12 +0000740 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000741 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000742 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000743 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000744}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000745
Douglas Gregor66724ea2009-11-14 01:20:54 +0000746bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
747 if ((getNumParams() < 1) ||
748 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
749 (getPrimaryTemplate() == 0) ||
750 (getDescribedFunctionTemplate() != 0))
751 return false;
752
753 const ParmVarDecl *Param = getParamDecl(0);
754
755 ASTContext &Context = getASTContext();
756 CanQualType ParamType = Context.getCanonicalType(Param->getType());
757
758 // Strip off the lvalue reference, if any.
759 if (CanQual<LValueReferenceType> ParamRefType
760 = ParamType->getAs<LValueReferenceType>())
761 ParamType = ParamRefType->getPointeeType();
762
763
764 // Is it the same as our our class type?
765 CanQualType ClassTy
766 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
767 if (ParamType.getUnqualifiedType() != ClassTy)
768 return false;
769
770 return true;
771}
772
Douglas Gregor42a552f2008-11-05 20:51:48 +0000773CXXDestructorDecl *
774CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000775 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000776 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000777 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000778 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
779 "Name must refer to a destructor");
Mike Stump1eb44332009-09-09 15:08:12 +0000780 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff3e970492009-01-27 21:25:57 +0000781 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000782}
783
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000784void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000785CXXConstructorDecl::Destroy(ASTContext& C) {
786 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000787 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000788}
789
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000790CXXConversionDecl *
791CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000792 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000793 QualType T, DeclaratorInfo *DInfo,
794 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000795 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
796 "Name must refer to a conversion function");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000797 return new (C) CXXConversionDecl(RD, L, N, T, DInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000798}
799
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000800OverloadedFunctionDecl *
801OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000802 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000803 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000804}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000805
Douglas Gregordec06662009-08-21 18:42:58 +0000806OverloadIterator::OverloadIterator(NamedDecl *ND) : D(0) {
807 if (!ND)
808 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Douglas Gregordec06662009-08-21 18:42:58 +0000810 if (isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND))
811 D = ND;
812 else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(ND)) {
Douglas Gregor8f1d89e2009-09-01 16:58:52 +0000813 if (Ovl->size() != 0) {
814 D = ND;
815 Iter = Ovl->function_begin();
816 }
Douglas Gregordec06662009-08-21 18:42:58 +0000817 }
818}
819
Douglas Gregor364e0212009-06-27 21:05:07 +0000820void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
821 Functions.push_back(F);
822 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000823}
824
Douglas Gregordaa439a2009-07-08 10:57:20 +0000825OverloadIterator::reference OverloadIterator::operator*() const {
826 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
827 return FD;
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Douglas Gregordaa439a2009-07-08 10:57:20 +0000829 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
830 return FTD;
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Douglas Gregordaa439a2009-07-08 10:57:20 +0000832 assert(isa<OverloadedFunctionDecl>(D));
833 return *Iter;
834}
835
836OverloadIterator &OverloadIterator::operator++() {
837 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
838 D = 0;
839 return *this;
840 }
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Douglas Gregordaa439a2009-07-08 10:57:20 +0000842 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
843 D = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Douglas Gregordaa439a2009-07-08 10:57:20 +0000845 return *this;
846}
847
848bool OverloadIterator::Equals(const OverloadIterator &Other) const {
849 if (!D || !Other.D)
850 return D == Other.D;
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Douglas Gregordaa439a2009-07-08 10:57:20 +0000852 if (D != Other.D)
853 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Douglas Gregordaa439a2009-07-08 10:57:20 +0000855 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
856}
857
John McCall02cace72009-08-28 07:59:38 +0000858FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
859 SourceLocation L,
860 FriendUnion Friend,
861 SourceLocation FriendL) {
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000862#ifndef NDEBUG
John McCall02cace72009-08-28 07:59:38 +0000863 if (Friend.is<NamedDecl*>()) {
864 NamedDecl *D = Friend.get<NamedDecl*>();
865 assert(isa<FunctionDecl>(D) ||
866 isa<CXXRecordDecl>(D) ||
867 isa<FunctionTemplateDecl>(D) ||
868 isa<ClassTemplateDecl>(D));
869 assert(D->getFriendObjectKind());
870 }
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000871#endif
John McCallc48fbdf2009-08-11 21:13:21 +0000872
John McCall02cace72009-08-28 07:59:38 +0000873 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump1eb44332009-09-09 15:08:12 +0000874}
John McCall3f9a8a62009-08-11 06:59:38 +0000875
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000876LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000877 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000878 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000879 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000880 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000881}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000882
883UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
884 SourceLocation L,
885 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000886 SourceRange QualifierRange,
887 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000888 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000889 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000890 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000891 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
892 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000893 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000894 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000895}
896
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000897NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
898 if (NamespaceAliasDecl *NA =
899 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
900 return NA->getNamespace();
901 return cast_or_null<NamespaceDecl>(NominatedNamespace);
902}
903
Mike Stump1eb44332009-09-09 15:08:12 +0000904NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
905 SourceLocation L,
906 SourceLocation AliasLoc,
907 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000908 SourceRange QualifierRange,
909 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +0000910 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +0000911 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000912 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
913 Namespace = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000914 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000915 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000916}
917
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000918UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
John McCall9488ea12009-11-17 05:59:44 +0000919 SourceLocation L, SourceRange NNR, SourceLocation UL,
920 NestedNameSpecifier* TargetNNS, DeclarationName Name,
921 bool IsTypeNameArg) {
922 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000923}
924
John McCall7ba107a2009-11-18 02:36:19 +0000925UnresolvedUsingValueDecl *
926UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
927 SourceLocation UsingLoc,
928 SourceRange TargetNNR,
929 NestedNameSpecifier *TargetNNS,
930 SourceLocation TargetNameLoc,
931 DeclarationName TargetName) {
932 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
933 TargetNNR, TargetNNS,
934 TargetNameLoc, TargetName);
935}
936
937UnresolvedUsingTypenameDecl *
938UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
939 SourceLocation UsingLoc,
940 SourceLocation TypenameLoc,
941 SourceRange TargetNNR,
942 NestedNameSpecifier *TargetNNS,
943 SourceLocation TargetNameLoc,
944 DeclarationName TargetName) {
945 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
946 TargetNNR, TargetNNS,
947 TargetNameLoc,
948 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +0000949}
950
Anders Carlssonfb311762009-03-14 00:25:26 +0000951StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
952 SourceLocation L, Expr *AssertExpr,
953 StringLiteral *Message) {
954 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
955}
956
957void StaticAssertDecl::Destroy(ASTContext& C) {
958 AssertExpr->Destroy(C);
959 Message->Destroy(C);
960 this->~StaticAssertDecl();
961 C.Deallocate((void *)this);
962}
963
964StaticAssertDecl::~StaticAssertDecl() {
965}
966
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000967static const char *getAccessName(AccessSpecifier AS) {
968 switch (AS) {
969 default:
970 case AS_none:
971 assert("Invalid access specifier!");
972 return 0;
973 case AS_public:
974 return "public";
975 case AS_private:
976 return "private";
977 case AS_protected:
978 return "protected";
979 }
980}
981
982const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
983 AccessSpecifier AS) {
984 return DB << getAccessName(AS);
985}
986
987