blob: 2e3bbce9e42ff7f8ebb7938aa9204dabb5af7044 [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),
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000039 Conversions(DC, DeclarationName()),
Fariborz Jahanian53462782009-09-11 21:44:33 +000040 VisibleConversions(DC, DeclarationName()),
Douglas Gregord475b8d2009-03-25 21:17:03 +000041 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000042
Ted Kremenek4b7c9832008-09-05 17:16:31 +000043CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
44 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000045 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000046 CXXRecordDecl* PrevDecl,
47 bool DelayTypeCreation) {
Mike Stump1eb44332009-09-09 15:08:12 +000048 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000049 PrevDecl, TKL);
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000051 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000052 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000053 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000054 return R;
55}
56
Douglas Gregorf8268ae2008-10-22 17:49:05 +000057CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000058}
59
60void CXXRecordDecl::Destroy(ASTContext &C) {
61 C.Deallocate(Bases);
Fariborz Jahanian71c6e712009-07-22 17:41:53 +000062 C.Deallocate(VBases);
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000063 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000064}
65
Mike Stump1eb44332009-09-09 15:08:12 +000066void
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000067CXXRecordDecl::setBases(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +000068 CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000069 unsigned NumBases) {
Mike Stump1eb44332009-09-09 15:08:12 +000070 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000071 // An aggregate is an array or a class (clause 9) with [...]
72 // no base classes [...].
73 Aggregate = false;
74
Douglas Gregor57c856b2008-10-23 18:13:27 +000075 if (this->Bases)
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000076 C.Deallocate(this->Bases);
Mike Stump1eb44332009-09-09 15:08:12 +000077
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000078 int vbaseCount = 0;
79 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
80 bool hasDirectVirtualBase = false;
Mike Stump1eb44332009-09-09 15:08:12 +000081
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000082 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor57c856b2008-10-23 18:13:27 +000083 this->NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000084 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor57c856b2008-10-23 18:13:27 +000085 this->Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000086 // Keep track of inherited vbases for this base class.
87 const CXXBaseSpecifier *Base = Bases[i];
88 QualType BaseType = Base->getType();
Mike Stump1eb44332009-09-09 15:08:12 +000089 // Skip template types.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000090 // FIXME. This means that this list must be rebuilt during template
91 // instantiation.
92 if (BaseType->isDependentType())
93 continue;
94 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +000095 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000096 if (Base->isVirtual())
97 hasDirectVirtualBase = true;
Mike Stump1eb44332009-09-09 15:08:12 +000098 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000099 BaseClassDecl->vbases_begin(),
100 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Mike Stump1eb44332009-09-09 15:08:12 +0000101 // Add this vbase to the array of vbases for current class if it is
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000102 // not already in the list.
103 // FIXME. Note that we do a linear search as number of such classes are
104 // very few.
105 int i;
106 for (i = 0; i < vbaseCount; ++i)
107 if (UniqueVbases[i]->getType() == VBase->getType())
108 break;
109 if (i == vbaseCount) {
110 UniqueVbases.push_back(VBase);
111 ++vbaseCount;
112 }
113 }
114 }
115 if (hasDirectVirtualBase) {
116 // Iterate one more time through the direct bases and add the virtual
117 // base to the list of vritual bases for current class.
118 for (unsigned i = 0; i < NumBases; ++i) {
119 const CXXBaseSpecifier *VBase = Bases[i];
120 if (!VBase->isVirtual())
121 continue;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000122 int j;
123 for (j = 0; j < vbaseCount; ++j)
124 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000125 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000126 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000127 UniqueVbases.push_back(VBase);
128 ++vbaseCount;
129 }
130 }
131 }
132 if (vbaseCount > 0) {
133 // build AST for inhireted, direct or indirect, virtual bases.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000134 this->VBases = new (C) CXXBaseSpecifier [vbaseCount];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000135 this->NumVBases = vbaseCount;
136 for (int i = 0; i < vbaseCount; i++) {
137 QualType QT = UniqueVbases[i]->getType();
138 CXXRecordDecl *VBaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000139 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000140 this->VBases[i] =
Douglas Gregor2aef06d2009-07-22 20:55:49 +0000141 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
142 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
143 UniqueVbases[i]->getAccessSpecifier(), QT);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000144 }
145 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000146}
147
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000148bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000149 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000150}
151
Mike Stump1eb44332009-09-09 15:08:12 +0000152CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000153 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000154 QualType ClassType
155 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000156 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000157 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000158 Context.getCanonicalType(ClassType));
159 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000160 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000161 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000162 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000163 // C++ [class.copy]p2:
164 // A non-template constructor for class X is a copy constructor if [...]
165 if (isa<FunctionTemplateDecl>(*Con))
166 continue;
167
Mike Stump1eb44332009-09-09 15:08:12 +0000168 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000169 FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000170 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
171 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000172 return cast<CXXConstructorDecl>(*Con);
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000174 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000175 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000176 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000177}
178
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000179bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
180 const CXXMethodDecl *& MD) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000181 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
182 const_cast<CXXRecordDecl*>(this)));
183 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
184
185 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000186 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000187 Op != OpEnd; ++Op) {
188 // C++ [class.copy]p9:
189 // A user-declared copy assignment operator is a non-static non-template
190 // member function of class X with exactly one parameter of type X, X&,
191 // const X&, volatile X& or const volatile X&.
192 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
193 if (Method->isStatic())
194 continue;
Douglas Gregor77da3f42009-10-13 23:45:19 +0000195 if (Method->getPrimaryTemplate())
196 continue;
Douglas Gregor72564e72009-02-26 23:50:07 +0000197 const FunctionProtoType *FnType =
John McCall183700f2009-09-21 23:43:11 +0000198 Method->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000199 assert(FnType && "Overloaded operator has no prototype.");
200 // Don't assert on this; an invalid decl might have been left in the AST.
201 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
202 continue;
203 bool AcceptsConst = true;
204 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000205 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000206 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000207 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000208 if (!ArgType.isConstQualified())
209 AcceptsConst = false;
210 }
211 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
212 continue;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000213 MD = Method;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000214 // We have a single argument of type cv X or cv X&, i.e. we've found the
215 // copy assignment operator. Return whether it accepts const arguments.
216 return AcceptsConst;
217 }
218 assert(isInvalidDecl() &&
219 "No copy assignment operator declared in valid code.");
220 return false;
221}
222
223void
Mike Stump1eb44332009-09-09 15:08:12 +0000224CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000225 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000226 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
227 // Note that we have a user-declared constructor.
228 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000229
Mike Stump1eb44332009-09-09 15:08:12 +0000230 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000231 // An aggregate is an array or a class (clause 9) with no
232 // user-declared constructors (12.1) [...].
233 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000234
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000235 // C++ [class]p4:
236 // A POD-struct is an aggregate class [...]
237 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000238
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000239 // C++ [class.ctor]p5:
240 // A constructor is trivial if it is an implicitly-declared default
241 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000242 // FIXME: C++0x: don't do this for "= default" default constructors.
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000243 HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000245 // Note when we have a user-declared copy constructor, which will
246 // suppress the implicit declaration of a copy constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000247 if (ConDecl->isCopyConstructor(Context)) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000248 UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000249
250 // C++ [class.copy]p6:
251 // A copy constructor is trivial if it is implicitly declared.
252 // FIXME: C++0x: don't do this for "= default" copy constructors.
253 HasTrivialCopyConstructor = false;
254 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000255}
256
Sebastian Redl64b45f72009-01-05 20:52:13 +0000257void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
258 CXXMethodDecl *OpDecl) {
259 // We're interested specifically in copy assignment operators.
John McCall183700f2009-09-21 23:43:11 +0000260 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000261 assert(FnType && "Overloaded operator has no proto function type.");
262 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregor77da3f42009-10-13 23:45:19 +0000263
264 // Copy assignment operators must be non-templates.
265 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
266 return;
267
Sebastian Redl64b45f72009-01-05 20:52:13 +0000268 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000269 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000270 ArgType = Ref->getPointeeType();
271
272 ArgType = ArgType.getUnqualifiedType();
273 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
274 const_cast<CXXRecordDecl*>(this)));
275
276 if (ClassType != Context.getCanonicalType(ArgType))
277 return;
278
279 // This is a copy assignment operator.
280 // Suppress the implicit declaration of a copy constructor.
281 UserDeclaredCopyAssignment = true;
282
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000283 // C++ [class.copy]p11:
284 // A copy assignment operator is trivial if it is implicitly declared.
285 // FIXME: C++0x: don't do this for "= default" copy operators.
286 HasTrivialCopyAssignment = false;
287
Sebastian Redl64b45f72009-01-05 20:52:13 +0000288 // C++ [class]p4:
289 // A POD-struct is an aggregate class that [...] has no user-defined copy
290 // assignment operator [...].
291 PlainOldData = false;
292}
293
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000294void
295CXXRecordDecl::collectConversionFunctions(
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000296 llvm::SmallPtrSet<CanQualType, 8>& ConversionsTypeSet)
297{
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000298 OverloadedFunctionDecl *TopConversions = getConversionFunctions();
299 for (OverloadedFunctionDecl::function_iterator
300 TFunc = TopConversions->function_begin(),
301 TFuncEnd = TopConversions->function_end();
302 TFunc != TFuncEnd; ++TFunc) {
303 NamedDecl *TopConv = TFunc->get();
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000304 CanQualType TConvType;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000305 if (FunctionTemplateDecl *TConversionTemplate =
306 dyn_cast<FunctionTemplateDecl>(TopConv))
307 TConvType =
308 getASTContext().getCanonicalType(
309 TConversionTemplate->getTemplatedDecl()->getResultType());
310 else
311 TConvType =
312 getASTContext().getCanonicalType(
313 cast<CXXConversionDecl>(TopConv)->getConversionType());
314 ConversionsTypeSet.insert(TConvType);
315 }
316}
317
Fariborz Jahanian62509212009-09-12 18:26:03 +0000318/// getNestedVisibleConversionFunctions - imports unique conversion
319/// functions from base classes into the visible conversion function
320/// list of the class 'RD'. This is a private helper method.
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000321/// TopConversionsTypeSet is the set of conversion functions of the class
322/// we are interested in. HiddenConversionTypes is set of conversion functions
323/// of the immediate derived class which hides the conversion functions found
324/// in current class.
Fariborz Jahanian62509212009-09-12 18:26:03 +0000325void
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000326CXXRecordDecl::getNestedVisibleConversionFunctions(CXXRecordDecl *RD,
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000327 const llvm::SmallPtrSet<CanQualType, 8> &TopConversionsTypeSet,
328 const llvm::SmallPtrSet<CanQualType, 8> &HiddenConversionTypes)
329{
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000330 bool inTopClass = (RD == this);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000331 QualType ClassType = getASTContext().getTypeDeclType(this);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000332 if (const RecordType *Record = ClassType->getAs<RecordType>()) {
333 OverloadedFunctionDecl *Conversions
334 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000335
Fariborz Jahanian53462782009-09-11 21:44:33 +0000336 for (OverloadedFunctionDecl::function_iterator
337 Func = Conversions->function_begin(),
338 FuncEnd = Conversions->function_end();
339 Func != FuncEnd; ++Func) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000340 NamedDecl *Conv = Func->get();
Fariborz Jahanian53462782009-09-11 21:44:33 +0000341 // Only those conversions not exact match of conversions in current
342 // class are candidateconversion routines.
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000343 CanQualType ConvType;
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000344 if (FunctionTemplateDecl *ConversionTemplate =
345 dyn_cast<FunctionTemplateDecl>(Conv))
346 ConvType =
347 getASTContext().getCanonicalType(
Fariborz Jahaniana5c12942009-09-15 23:02:16 +0000348 ConversionTemplate->getTemplatedDecl()->getResultType());
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000349 else
350 ConvType =
Fariborz Jahanian8b915e72009-09-15 22:15:23 +0000351 getASTContext().getCanonicalType(
352 cast<CXXConversionDecl>(Conv)->getConversionType());
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000353 // We only add conversion functions found in the base class if they
354 // are not hidden by those found in HiddenConversionTypes which are
355 // the conversion functions in its derived class.
356 if (inTopClass ||
357 (!TopConversionsTypeSet.count(ConvType) &&
358 !HiddenConversionTypes.count(ConvType)) ) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000359 if (FunctionTemplateDecl *ConversionTemplate =
360 dyn_cast<FunctionTemplateDecl>(Conv))
361 RD->addVisibleConversionFunction(ConversionTemplate);
362 else
363 RD->addVisibleConversionFunction(cast<CXXConversionDecl>(Conv));
Fariborz Jahanian53462782009-09-11 21:44:33 +0000364 }
365 }
366 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000367
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000368 if (getNumBases() == 0 && getNumVBases() == 0)
369 return;
Sebastian Redl9994a342009-10-25 17:03:50 +0000370
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000371 llvm::SmallPtrSet<CanQualType, 8> ConversionFunctions;
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000372 if (!inTopClass)
373 collectConversionFunctions(ConversionFunctions);
Sebastian Redl9994a342009-10-25 17:03:50 +0000374
Fariborz Jahanian53462782009-09-11 21:44:33 +0000375 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(),
376 E = vbases_end(); VBase != E; ++VBase) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000377 if (const RecordType *RT = VBase->getType()->getAs<RecordType>()) {
378 CXXRecordDecl *VBaseClassDecl
379 = cast<CXXRecordDecl>(RT->getDecl());
380 VBaseClassDecl->getNestedVisibleConversionFunctions(RD,
381 TopConversionsTypeSet,
382 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
383 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000384 }
385 for (CXXRecordDecl::base_class_iterator Base = bases_begin(),
386 E = bases_end(); Base != E; ++Base) {
387 if (Base->isVirtual())
388 continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000389 if (const RecordType *RT = Base->getType()->getAs<RecordType>()) {
390 CXXRecordDecl *BaseClassDecl
391 = cast<CXXRecordDecl>(RT->getDecl());
392
393 BaseClassDecl->getNestedVisibleConversionFunctions(RD,
394 TopConversionsTypeSet,
395 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
396 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000397 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000398}
399
400/// getVisibleConversionFunctions - get all conversion functions visible
401/// in current class; including conversion function templates.
402OverloadedFunctionDecl *
403CXXRecordDecl::getVisibleConversionFunctions() {
404 // If root class, all conversions are visible.
405 if (bases_begin() == bases_end())
406 return &Conversions;
407 // If visible conversion list is already evaluated, return it.
408 if (ComputedVisibleConversions)
409 return &VisibleConversions;
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000410 llvm::SmallPtrSet<CanQualType, 8> TopConversionsTypeSet;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000411 collectConversionFunctions(TopConversionsTypeSet);
412 getNestedVisibleConversionFunctions(this, TopConversionsTypeSet,
413 TopConversionsTypeSet);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000414 ComputedVisibleConversions = true;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000415 return &VisibleConversions;
416}
417
Fariborz Jahanian62509212009-09-12 18:26:03 +0000418void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000419 CXXConversionDecl *ConvDecl) {
420 assert(!ConvDecl->getDescribedFunctionTemplate() &&
421 "Conversion function templates should cast to FunctionTemplateDecl.");
422 VisibleConversions.addOverload(ConvDecl);
423}
424
Fariborz Jahanian62509212009-09-12 18:26:03 +0000425void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000426 FunctionTemplateDecl *ConvDecl) {
427 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
428 "Function template is not a conversion function template");
429 VisibleConversions.addOverload(ConvDecl);
430}
431
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000432void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000433 assert(!ConvDecl->getDescribedFunctionTemplate() &&
434 "Conversion function templates should cast to FunctionTemplateDecl.");
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000435 Conversions.addOverload(ConvDecl);
436}
437
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000438void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000439 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
440 "Function template is not a conversion function template");
441 Conversions.addOverload(ConvDecl);
442}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000443
Douglas Gregorf6b11852009-10-08 15:14:33 +0000444CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000445 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000446 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
447
448 return 0;
449}
450
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000451MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
452 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
453}
454
Douglas Gregorf6b11852009-10-08 15:14:33 +0000455void
456CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
457 TemplateSpecializationKind TSK) {
458 assert(TemplateOrInstantiation.isNull() &&
459 "Previous template or instantiation?");
460 assert(!isa<ClassTemplateSpecializationDecl>(this));
461 TemplateOrInstantiation
462 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
463}
464
465TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() {
466 if (ClassTemplateSpecializationDecl *Spec
467 = dyn_cast<ClassTemplateSpecializationDecl>(this))
468 return Spec->getSpecializationKind();
469
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000470 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000471 return MSInfo->getTemplateSpecializationKind();
472
473 return TSK_Undeclared;
474}
475
476void
477CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
478 if (ClassTemplateSpecializationDecl *Spec
479 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
480 Spec->setSpecializationKind(TSK);
481 return;
482 }
483
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000484 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000485 MSInfo->setTemplateSpecializationKind(TSK);
486 return;
487 }
488
489 assert(false && "Not a class template or member class specialization");
490}
491
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000492CXXConstructorDecl *
493CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
494 QualType ClassType = Context.getTypeDeclType(this);
495 DeclarationName ConstructorName
496 = Context.DeclarationNames.getCXXConstructorName(
497 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000499 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000500 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000501 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000502 // FIXME: In C++0x, a constructor template can be a default constructor.
503 if (isa<FunctionTemplateDecl>(*Con))
504 continue;
505
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000506 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
507 if (Constructor->isDefaultConstructor())
508 return Constructor;
509 }
510 return 0;
511}
512
Anders Carlsson7267c162009-05-29 21:03:38 +0000513const CXXDestructorDecl *
514CXXRecordDecl::getDestructor(ASTContext &Context) {
515 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
517 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000518 = Context.DeclarationNames.getCXXDestructorName(
519 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000520
521 DeclContext::lookup_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000522 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000523 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Anders Carlsson7267c162009-05-29 21:03:38 +0000525 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
526 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Anders Carlsson7267c162009-05-29 21:03:38 +0000528 return Dtor;
529}
530
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000531CXXMethodDecl *
532CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000533 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000534 QualType T, DeclaratorInfo *DInfo,
535 bool isStatic, bool isInline) {
536 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, DInfo,
537 isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000538}
539
Douglas Gregor90916562009-09-29 18:16:17 +0000540bool CXXMethodDecl::isUsualDeallocationFunction() const {
541 if (getOverloadedOperator() != OO_Delete &&
542 getOverloadedOperator() != OO_Array_Delete)
543 return false;
544
545 // C++ [basic.stc.dynamic.deallocation]p2:
546 // If a class T has a member deallocation function named operator delete
547 // with exactly one parameter, then that function is a usual (non-placement)
548 // deallocation function. [...]
549 if (getNumParams() == 1)
550 return true;
551
552 // C++ [basic.stc.dynamic.deallocation]p2:
553 // [...] If class T does not declare such an operator delete but does
554 // declare a member deallocation function named operator delete with
555 // exactly two parameters, the second of which has type std::size_t (18.1),
556 // then this function is a usual deallocation function.
557 ASTContext &Context = getASTContext();
558 if (getNumParams() != 2 ||
559 !Context.hasSameType(getParamDecl(1)->getType(), Context.getSizeType()))
560 return false;
561
562 // This function is a usual deallocation function if there are no
563 // single-parameter deallocation functions of the same kind.
564 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
565 R.first != R.second; ++R.first) {
566 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
567 if (FD->getNumParams() == 1)
568 return false;
569 }
570
571 return true;
572}
573
Mike Stump1eb44332009-09-09 15:08:12 +0000574typedef llvm::DenseMap<const CXXMethodDecl*,
575 std::vector<const CXXMethodDecl *> *>
Anders Carlsson05eb2442009-05-16 23:58:37 +0000576 OverriddenMethodsMapTy;
577
Mike Stumpb9871a22009-08-21 01:45:00 +0000578// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
579// the vtable building code uses it at CG time.
Anders Carlsson05eb2442009-05-16 23:58:37 +0000580static OverriddenMethodsMapTy *OverriddenMethods = 0;
581
582void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
583 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Anders Carlsson05eb2442009-05-16 23:58:37 +0000585 if (!OverriddenMethods)
586 OverriddenMethods = new OverriddenMethodsMapTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Anders Carlsson05eb2442009-05-16 23:58:37 +0000588 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
589 if (!Methods)
590 Methods = new std::vector<const CXXMethodDecl *>;
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Anders Carlsson05eb2442009-05-16 23:58:37 +0000592 Methods->push_back(MD);
593}
594
595CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
596 if (!OverriddenMethods)
597 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Anders Carlsson05eb2442009-05-16 23:58:37 +0000599 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000600 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000601 return 0;
Daniel Dunbar0908c332009-08-01 23:40:20 +0000602
Anders Carlsson05eb2442009-05-16 23:58:37 +0000603 return &(*it->second)[0];
604}
605
606CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
607 if (!OverriddenMethods)
608 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Anders Carlsson05eb2442009-05-16 23:58:37 +0000610 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000611 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000612 return 0;
613
Daniel Dunbar637ec322009-08-02 01:48:29 +0000614 return &(*it->second)[0] + it->second->size();
Anders Carlsson05eb2442009-05-16 23:58:37 +0000615}
616
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000617QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000618 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
619 // If the member function is declared const, the type of this is const X*,
620 // if the member function is declared volatile, the type of this is
621 // volatile X*, and if the member function is declared const volatile,
622 // the type of this is const volatile X*.
623
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000624 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000625
626 QualType ClassTy;
627 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
628 ClassTy = TD->getInjectedClassNameType(C);
629 else
Mike Stumpe607ed02009-08-07 18:05:12 +0000630 ClassTy = C.getTagDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000631 ClassTy = C.getQualifiedType(ClassTy,
632 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000633 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000634}
635
Douglas Gregor7ad83902008-11-05 04:29:56 +0000636CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000637CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000638 CXXConstructorDecl *C,
Mike Stump1eb44332009-09-09 15:08:12 +0000639 SourceLocation L, SourceLocation R)
Douglas Gregor72f6d672009-09-01 21:04:42 +0000640 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000641 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
642 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
643 BaseOrMember |= 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Douglas Gregor7ad83902008-11-05 04:29:56 +0000645 if (NumArgs > 0) {
646 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000647 // FIXME. Allocation via Context
648 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000649 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
650 this->Args[Idx] = Args[Idx];
651 }
Douglas Gregor72f6d672009-09-01 21:04:42 +0000652 CtorOrAnonUnion = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000653}
654
655CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000656CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000657 CXXConstructorDecl *C,
Anders Carlsson8c57a662009-08-29 01:31:33 +0000658 SourceLocation L, SourceLocation R)
Douglas Gregor72f6d672009-09-01 21:04:42 +0000659 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000660 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
Mike Stump1eb44332009-09-09 15:08:12 +0000661 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
Douglas Gregor7ad83902008-11-05 04:29:56 +0000662
663 if (NumArgs > 0) {
664 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000665 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000666 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
667 this->Args[Idx] = Args[Idx];
668 }
Douglas Gregor72f6d672009-09-01 21:04:42 +0000669 CtorOrAnonUnion = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000670}
671
672CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
673 delete [] Args;
674}
675
Douglas Gregorb48fe382008-10-31 09:07:45 +0000676CXXConstructorDecl *
677CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000678 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000679 QualType T, DeclaratorInfo *DInfo,
680 bool isExplicit,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000681 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000682 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
683 "Name must refer to a constructor");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000684 return new (C) CXXConstructorDecl(RD, L, N, T, DInfo, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000685 isImplicitlyDeclared);
686}
687
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000688bool CXXConstructorDecl::isDefaultConstructor() const {
689 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000690 // A default constructor for a class X is a constructor of class
691 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000692 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000693 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000694}
695
Mike Stump1eb44332009-09-09 15:08:12 +0000696bool
697CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000698 unsigned &TypeQuals) const {
699 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000700 // A non-template constructor for class X is a copy constructor
701 // if its first parameter is of type X&, const X&, volatile X& or
702 // const volatile X&, and either there are no other parameters
703 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000704 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000705 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
706 (getPrimaryTemplate() != 0) ||
707 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000708 return false;
709
710 const ParmVarDecl *Param = getParamDecl(0);
711
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000712 // Do we have a reference type? Rvalue references don't count.
713 const LValueReferenceType *ParamRefType =
Ted Kremenek6217b802009-07-29 21:53:49 +0000714 Param->getType()->getAs<LValueReferenceType>();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000715 if (!ParamRefType)
716 return false;
717
718 // Is it a reference to our class type?
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000719 CanQualType PointeeType
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000720 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000721 CanQualType ClassTy
722 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000723 if (PointeeType.getUnqualifiedType() != ClassTy)
724 return false;
725
John McCall0953e762009-09-24 19:53:00 +0000726 // FIXME: other qualifiers?
727
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000728 // We have a copy constructor.
729 TypeQuals = PointeeType.getCVRQualifiers();
730 return true;
731}
732
Anders Carlssonfaccd722009-08-28 16:57:08 +0000733bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000734 // C++ [class.conv.ctor]p1:
735 // A constructor declared without the function-specifier explicit
736 // that can be called with a single parameter specifies a
737 // conversion from the type of its first parameter to the type of
738 // its class. Such a constructor is called a converting
739 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000740 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000741 return false;
742
Mike Stump1eb44332009-09-09 15:08:12 +0000743 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000744 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000745 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000746 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000747}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000748
Douglas Gregor42a552f2008-11-05 20:51:48 +0000749CXXDestructorDecl *
750CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000751 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000752 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000753 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000754 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
755 "Name must refer to a destructor");
Mike Stump1eb44332009-09-09 15:08:12 +0000756 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff3e970492009-01-27 21:25:57 +0000757 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000758}
759
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000760void
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000761CXXDestructorDecl::Destroy(ASTContext& C) {
762 C.Deallocate(BaseOrMemberDestructions);
763 CXXMethodDecl::Destroy(C);
764}
765
766void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000767CXXConstructorDecl::Destroy(ASTContext& C) {
768 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000769 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000770}
771
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000772CXXConversionDecl *
773CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000774 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000775 QualType T, DeclaratorInfo *DInfo,
776 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000777 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
778 "Name must refer to a conversion function");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000779 return new (C) CXXConversionDecl(RD, L, N, T, DInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000780}
781
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000782OverloadedFunctionDecl *
783OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000784 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000785 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000786}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000787
Douglas Gregordec06662009-08-21 18:42:58 +0000788OverloadIterator::OverloadIterator(NamedDecl *ND) : D(0) {
789 if (!ND)
790 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Douglas Gregordec06662009-08-21 18:42:58 +0000792 if (isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND))
793 D = ND;
794 else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(ND)) {
Douglas Gregor8f1d89e2009-09-01 16:58:52 +0000795 if (Ovl->size() != 0) {
796 D = ND;
797 Iter = Ovl->function_begin();
798 }
Douglas Gregordec06662009-08-21 18:42:58 +0000799 }
800}
801
Douglas Gregor364e0212009-06-27 21:05:07 +0000802void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
803 Functions.push_back(F);
804 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000805}
806
Douglas Gregordaa439a2009-07-08 10:57:20 +0000807OverloadIterator::reference OverloadIterator::operator*() const {
808 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
809 return FD;
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Douglas Gregordaa439a2009-07-08 10:57:20 +0000811 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
812 return FTD;
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Douglas Gregordaa439a2009-07-08 10:57:20 +0000814 assert(isa<OverloadedFunctionDecl>(D));
815 return *Iter;
816}
817
818OverloadIterator &OverloadIterator::operator++() {
819 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
820 D = 0;
821 return *this;
822 }
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Douglas Gregordaa439a2009-07-08 10:57:20 +0000824 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
825 D = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Douglas Gregordaa439a2009-07-08 10:57:20 +0000827 return *this;
828}
829
830bool OverloadIterator::Equals(const OverloadIterator &Other) const {
831 if (!D || !Other.D)
832 return D == Other.D;
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Douglas Gregordaa439a2009-07-08 10:57:20 +0000834 if (D != Other.D)
835 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Douglas Gregordaa439a2009-07-08 10:57:20 +0000837 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
838}
839
John McCall02cace72009-08-28 07:59:38 +0000840FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
841 SourceLocation L,
842 FriendUnion Friend,
843 SourceLocation FriendL) {
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000844#ifndef NDEBUG
John McCall02cace72009-08-28 07:59:38 +0000845 if (Friend.is<NamedDecl*>()) {
846 NamedDecl *D = Friend.get<NamedDecl*>();
847 assert(isa<FunctionDecl>(D) ||
848 isa<CXXRecordDecl>(D) ||
849 isa<FunctionTemplateDecl>(D) ||
850 isa<ClassTemplateDecl>(D));
851 assert(D->getFriendObjectKind());
852 }
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000853#endif
John McCallc48fbdf2009-08-11 21:13:21 +0000854
John McCall02cace72009-08-28 07:59:38 +0000855 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump1eb44332009-09-09 15:08:12 +0000856}
John McCall3f9a8a62009-08-11 06:59:38 +0000857
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000858LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000859 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000860 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000861 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000862 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000863}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000864
865UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
866 SourceLocation L,
867 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000868 SourceRange QualifierRange,
869 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000870 SourceLocation IdentLoc,
871 NamespaceDecl *Used,
872 DeclContext *CommonAncestor) {
Mike Stump1eb44332009-09-09 15:08:12 +0000873 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000874 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000875}
876
Mike Stump1eb44332009-09-09 15:08:12 +0000877NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
878 SourceLocation L,
879 SourceLocation AliasLoc,
880 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000881 SourceRange QualifierRange,
882 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +0000883 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +0000884 NamedDecl *Namespace) {
Mike Stump1eb44332009-09-09 15:08:12 +0000885 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000886 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000887}
888
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000889UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
890 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
891 SourceLocation UL, NamedDecl* Target,
892 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
893 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
894 TargetNNS, IsTypeNameArg);
895}
896
Anders Carlsson665b49c2009-08-28 05:30:28 +0000897UnresolvedUsingDecl *UnresolvedUsingDecl::Create(ASTContext &C, DeclContext *DC,
898 SourceLocation UsingLoc,
899 SourceRange TargetNNR,
900 NestedNameSpecifier *TargetNNS,
901 SourceLocation TargetNameLoc,
902 DeclarationName TargetName,
903 bool IsTypeNameArg) {
904 return new (C) UnresolvedUsingDecl(DC, UsingLoc, TargetNNR, TargetNNS,
905 TargetNameLoc, TargetName, IsTypeNameArg);
906}
907
Anders Carlssonfb311762009-03-14 00:25:26 +0000908StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
909 SourceLocation L, Expr *AssertExpr,
910 StringLiteral *Message) {
911 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
912}
913
914void StaticAssertDecl::Destroy(ASTContext& C) {
915 AssertExpr->Destroy(C);
916 Message->Destroy(C);
917 this->~StaticAssertDecl();
918 C.Deallocate((void *)this);
919}
920
921StaticAssertDecl::~StaticAssertDecl() {
922}
923
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000924static const char *getAccessName(AccessSpecifier AS) {
925 switch (AS) {
926 default:
927 case AS_none:
928 assert("Invalid access specifier!");
929 return 0;
930 case AS_public:
931 return "public";
932 case AS_private:
933 return "private";
934 case AS_protected:
935 return "protected";
936 }
937}
938
939const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
940 AccessSpecifier AS) {
941 return DB << getAccessName(AS);
942}
943
944