blob: e325a25c76278e7114309483951b4fd4bd3262c4 [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&.
Douglas Gregor682054c2009-10-30 22:48:49 +0000192 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
193 if (!Method)
194 continue;
195
Sebastian Redl64b45f72009-01-05 20:52:13 +0000196 if (Method->isStatic())
197 continue;
Douglas Gregor77da3f42009-10-13 23:45:19 +0000198 if (Method->getPrimaryTemplate())
199 continue;
Douglas Gregor72564e72009-02-26 23:50:07 +0000200 const FunctionProtoType *FnType =
John McCall183700f2009-09-21 23:43:11 +0000201 Method->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000202 assert(FnType && "Overloaded operator has no prototype.");
203 // Don't assert on this; an invalid decl might have been left in the AST.
204 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
205 continue;
206 bool AcceptsConst = true;
207 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000208 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000209 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000210 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000211 if (!ArgType.isConstQualified())
212 AcceptsConst = false;
213 }
214 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
215 continue;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000216 MD = Method;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000217 // We have a single argument of type cv X or cv X&, i.e. we've found the
218 // copy assignment operator. Return whether it accepts const arguments.
219 return AcceptsConst;
220 }
221 assert(isInvalidDecl() &&
222 "No copy assignment operator declared in valid code.");
223 return false;
224}
225
226void
Mike Stump1eb44332009-09-09 15:08:12 +0000227CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000228 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000229 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
230 // Note that we have a user-declared constructor.
231 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000232
Mike Stump1eb44332009-09-09 15:08:12 +0000233 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000234 // An aggregate is an array or a class (clause 9) with no
235 // user-declared constructors (12.1) [...].
236 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000237
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000238 // C++ [class]p4:
239 // A POD-struct is an aggregate class [...]
240 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000241
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000242 // C++ [class.ctor]p5:
243 // A constructor is trivial if it is an implicitly-declared default
244 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000245 // FIXME: C++0x: don't do this for "= default" default constructors.
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000246 HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000248 // Note when we have a user-declared copy constructor, which will
249 // suppress the implicit declaration of a copy constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000250 if (ConDecl->isCopyConstructor(Context)) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000251 UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000252
253 // C++ [class.copy]p6:
254 // A copy constructor is trivial if it is implicitly declared.
255 // FIXME: C++0x: don't do this for "= default" copy constructors.
256 HasTrivialCopyConstructor = false;
257 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000258}
259
Sebastian Redl64b45f72009-01-05 20:52:13 +0000260void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
261 CXXMethodDecl *OpDecl) {
262 // We're interested specifically in copy assignment operators.
John McCall183700f2009-09-21 23:43:11 +0000263 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000264 assert(FnType && "Overloaded operator has no proto function type.");
265 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregor77da3f42009-10-13 23:45:19 +0000266
267 // Copy assignment operators must be non-templates.
268 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
269 return;
270
Sebastian Redl64b45f72009-01-05 20:52:13 +0000271 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000272 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000273 ArgType = Ref->getPointeeType();
274
275 ArgType = ArgType.getUnqualifiedType();
276 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
277 const_cast<CXXRecordDecl*>(this)));
278
279 if (ClassType != Context.getCanonicalType(ArgType))
280 return;
281
282 // This is a copy assignment operator.
Eli Friedman88fad632009-11-07 00:02:45 +0000283 // Note on the decl that it is a copy assignment operator.
284 OpDecl->setCopyAssignment(true);
285
Sebastian Redl64b45f72009-01-05 20:52:13 +0000286 // Suppress the implicit declaration of a copy constructor.
287 UserDeclaredCopyAssignment = true;
288
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000289 // C++ [class.copy]p11:
290 // A copy assignment operator is trivial if it is implicitly declared.
291 // FIXME: C++0x: don't do this for "= default" copy operators.
292 HasTrivialCopyAssignment = false;
293
Sebastian Redl64b45f72009-01-05 20:52:13 +0000294 // C++ [class]p4:
295 // A POD-struct is an aggregate class that [...] has no user-defined copy
296 // assignment operator [...].
297 PlainOldData = false;
298}
299
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000300void
301CXXRecordDecl::collectConversionFunctions(
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000302 llvm::SmallPtrSet<CanQualType, 8>& ConversionsTypeSet)
303{
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000304 OverloadedFunctionDecl *TopConversions = getConversionFunctions();
305 for (OverloadedFunctionDecl::function_iterator
306 TFunc = TopConversions->function_begin(),
307 TFuncEnd = TopConversions->function_end();
308 TFunc != TFuncEnd; ++TFunc) {
309 NamedDecl *TopConv = TFunc->get();
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000310 CanQualType TConvType;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000311 if (FunctionTemplateDecl *TConversionTemplate =
312 dyn_cast<FunctionTemplateDecl>(TopConv))
313 TConvType =
314 getASTContext().getCanonicalType(
315 TConversionTemplate->getTemplatedDecl()->getResultType());
316 else
317 TConvType =
318 getASTContext().getCanonicalType(
319 cast<CXXConversionDecl>(TopConv)->getConversionType());
320 ConversionsTypeSet.insert(TConvType);
321 }
322}
323
Fariborz Jahanian62509212009-09-12 18:26:03 +0000324/// getNestedVisibleConversionFunctions - imports unique conversion
325/// functions from base classes into the visible conversion function
326/// list of the class 'RD'. This is a private helper method.
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000327/// TopConversionsTypeSet is the set of conversion functions of the class
328/// we are interested in. HiddenConversionTypes is set of conversion functions
329/// of the immediate derived class which hides the conversion functions found
330/// in current class.
Fariborz Jahanian62509212009-09-12 18:26:03 +0000331void
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000332CXXRecordDecl::getNestedVisibleConversionFunctions(CXXRecordDecl *RD,
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000333 const llvm::SmallPtrSet<CanQualType, 8> &TopConversionsTypeSet,
334 const llvm::SmallPtrSet<CanQualType, 8> &HiddenConversionTypes)
335{
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000336 bool inTopClass = (RD == this);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000337 QualType ClassType = getASTContext().getTypeDeclType(this);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000338 if (const RecordType *Record = ClassType->getAs<RecordType>()) {
339 OverloadedFunctionDecl *Conversions
340 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000341
Fariborz Jahanian53462782009-09-11 21:44:33 +0000342 for (OverloadedFunctionDecl::function_iterator
343 Func = Conversions->function_begin(),
344 FuncEnd = Conversions->function_end();
345 Func != FuncEnd; ++Func) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000346 NamedDecl *Conv = Func->get();
Fariborz Jahanian53462782009-09-11 21:44:33 +0000347 // Only those conversions not exact match of conversions in current
348 // class are candidateconversion routines.
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000349 CanQualType ConvType;
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000350 if (FunctionTemplateDecl *ConversionTemplate =
351 dyn_cast<FunctionTemplateDecl>(Conv))
352 ConvType =
353 getASTContext().getCanonicalType(
Fariborz Jahaniana5c12942009-09-15 23:02:16 +0000354 ConversionTemplate->getTemplatedDecl()->getResultType());
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000355 else
356 ConvType =
Fariborz Jahanian8b915e72009-09-15 22:15:23 +0000357 getASTContext().getCanonicalType(
358 cast<CXXConversionDecl>(Conv)->getConversionType());
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000359 // We only add conversion functions found in the base class if they
360 // are not hidden by those found in HiddenConversionTypes which are
361 // the conversion functions in its derived class.
362 if (inTopClass ||
363 (!TopConversionsTypeSet.count(ConvType) &&
364 !HiddenConversionTypes.count(ConvType)) ) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000365 if (FunctionTemplateDecl *ConversionTemplate =
366 dyn_cast<FunctionTemplateDecl>(Conv))
367 RD->addVisibleConversionFunction(ConversionTemplate);
368 else
369 RD->addVisibleConversionFunction(cast<CXXConversionDecl>(Conv));
Fariborz Jahanian53462782009-09-11 21:44:33 +0000370 }
371 }
372 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000373
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000374 if (getNumBases() == 0 && getNumVBases() == 0)
375 return;
Sebastian Redl9994a342009-10-25 17:03:50 +0000376
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000377 llvm::SmallPtrSet<CanQualType, 8> ConversionFunctions;
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000378 if (!inTopClass)
379 collectConversionFunctions(ConversionFunctions);
Sebastian Redl9994a342009-10-25 17:03:50 +0000380
Fariborz Jahanian53462782009-09-11 21:44:33 +0000381 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(),
382 E = vbases_end(); VBase != E; ++VBase) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000383 if (const RecordType *RT = VBase->getType()->getAs<RecordType>()) {
384 CXXRecordDecl *VBaseClassDecl
385 = cast<CXXRecordDecl>(RT->getDecl());
386 VBaseClassDecl->getNestedVisibleConversionFunctions(RD,
387 TopConversionsTypeSet,
388 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
389 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000390 }
391 for (CXXRecordDecl::base_class_iterator Base = bases_begin(),
392 E = bases_end(); Base != E; ++Base) {
393 if (Base->isVirtual())
394 continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000395 if (const RecordType *RT = Base->getType()->getAs<RecordType>()) {
396 CXXRecordDecl *BaseClassDecl
397 = cast<CXXRecordDecl>(RT->getDecl());
398
399 BaseClassDecl->getNestedVisibleConversionFunctions(RD,
400 TopConversionsTypeSet,
401 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
402 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000403 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000404}
405
406/// getVisibleConversionFunctions - get all conversion functions visible
407/// in current class; including conversion function templates.
408OverloadedFunctionDecl *
409CXXRecordDecl::getVisibleConversionFunctions() {
410 // If root class, all conversions are visible.
411 if (bases_begin() == bases_end())
412 return &Conversions;
413 // If visible conversion list is already evaluated, return it.
414 if (ComputedVisibleConversions)
415 return &VisibleConversions;
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000416 llvm::SmallPtrSet<CanQualType, 8> TopConversionsTypeSet;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000417 collectConversionFunctions(TopConversionsTypeSet);
418 getNestedVisibleConversionFunctions(this, TopConversionsTypeSet,
419 TopConversionsTypeSet);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000420 ComputedVisibleConversions = true;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000421 return &VisibleConversions;
422}
423
Fariborz Jahanian62509212009-09-12 18:26:03 +0000424void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000425 CXXConversionDecl *ConvDecl) {
426 assert(!ConvDecl->getDescribedFunctionTemplate() &&
427 "Conversion function templates should cast to FunctionTemplateDecl.");
428 VisibleConversions.addOverload(ConvDecl);
429}
430
Fariborz Jahanian62509212009-09-12 18:26:03 +0000431void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000432 FunctionTemplateDecl *ConvDecl) {
433 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
434 "Function template is not a conversion function template");
435 VisibleConversions.addOverload(ConvDecl);
436}
437
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000438void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000439 assert(!ConvDecl->getDescribedFunctionTemplate() &&
440 "Conversion function templates should cast to FunctionTemplateDecl.");
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000441 Conversions.addOverload(ConvDecl);
442}
443
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000444void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000445 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
446 "Function template is not a conversion function template");
447 Conversions.addOverload(ConvDecl);
448}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000449
Douglas Gregorf6b11852009-10-08 15:14:33 +0000450CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000451 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000452 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
453
454 return 0;
455}
456
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000457MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
458 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
459}
460
Douglas Gregorf6b11852009-10-08 15:14:33 +0000461void
462CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
463 TemplateSpecializationKind TSK) {
464 assert(TemplateOrInstantiation.isNull() &&
465 "Previous template or instantiation?");
466 assert(!isa<ClassTemplateSpecializationDecl>(this));
467 TemplateOrInstantiation
468 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
469}
470
471TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() {
472 if (ClassTemplateSpecializationDecl *Spec
473 = dyn_cast<ClassTemplateSpecializationDecl>(this))
474 return Spec->getSpecializationKind();
475
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000476 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000477 return MSInfo->getTemplateSpecializationKind();
478
479 return TSK_Undeclared;
480}
481
482void
483CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
484 if (ClassTemplateSpecializationDecl *Spec
485 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
486 Spec->setSpecializationKind(TSK);
487 return;
488 }
489
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000490 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000491 MSInfo->setTemplateSpecializationKind(TSK);
492 return;
493 }
494
495 assert(false && "Not a class template or member class specialization");
496}
497
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000498CXXConstructorDecl *
499CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
500 QualType ClassType = Context.getTypeDeclType(this);
501 DeclarationName ConstructorName
502 = Context.DeclarationNames.getCXXConstructorName(
503 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000505 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000506 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000507 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000508 // FIXME: In C++0x, a constructor template can be a default constructor.
509 if (isa<FunctionTemplateDecl>(*Con))
510 continue;
511
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000512 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
513 if (Constructor->isDefaultConstructor())
514 return Constructor;
515 }
516 return 0;
517}
518
Anders Carlsson7267c162009-05-29 21:03:38 +0000519const CXXDestructorDecl *
520CXXRecordDecl::getDestructor(ASTContext &Context) {
521 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000522
523 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000524 = Context.DeclarationNames.getCXXDestructorName(
525 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000526
527 DeclContext::lookup_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000528 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000529 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Anders Carlsson7267c162009-05-29 21:03:38 +0000531 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
532 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Anders Carlsson7267c162009-05-29 21:03:38 +0000534 return Dtor;
535}
536
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000537CXXMethodDecl *
538CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000539 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000540 QualType T, DeclaratorInfo *DInfo,
541 bool isStatic, bool isInline) {
542 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, DInfo,
543 isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000544}
545
Douglas Gregor90916562009-09-29 18:16:17 +0000546bool CXXMethodDecl::isUsualDeallocationFunction() const {
547 if (getOverloadedOperator() != OO_Delete &&
548 getOverloadedOperator() != OO_Array_Delete)
549 return false;
550
551 // C++ [basic.stc.dynamic.deallocation]p2:
552 // If a class T has a member deallocation function named operator delete
553 // with exactly one parameter, then that function is a usual (non-placement)
554 // deallocation function. [...]
555 if (getNumParams() == 1)
556 return true;
557
558 // C++ [basic.stc.dynamic.deallocation]p2:
559 // [...] If class T does not declare such an operator delete but does
560 // declare a member deallocation function named operator delete with
561 // exactly two parameters, the second of which has type std::size_t (18.1),
562 // then this function is a usual deallocation function.
563 ASTContext &Context = getASTContext();
564 if (getNumParams() != 2 ||
565 !Context.hasSameType(getParamDecl(1)->getType(), Context.getSizeType()))
566 return false;
567
568 // This function is a usual deallocation function if there are no
569 // single-parameter deallocation functions of the same kind.
570 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
571 R.first != R.second; ++R.first) {
572 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
573 if (FD->getNumParams() == 1)
574 return false;
575 }
576
577 return true;
578}
579
Mike Stump1eb44332009-09-09 15:08:12 +0000580typedef llvm::DenseMap<const CXXMethodDecl*,
581 std::vector<const CXXMethodDecl *> *>
Anders Carlsson05eb2442009-05-16 23:58:37 +0000582 OverriddenMethodsMapTy;
583
Mike Stumpb9871a22009-08-21 01:45:00 +0000584// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
585// the vtable building code uses it at CG time.
Anders Carlsson05eb2442009-05-16 23:58:37 +0000586static OverriddenMethodsMapTy *OverriddenMethods = 0;
587
588void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
589 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Anders Carlsson05eb2442009-05-16 23:58:37 +0000591 if (!OverriddenMethods)
592 OverriddenMethods = new OverriddenMethodsMapTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Anders Carlsson05eb2442009-05-16 23:58:37 +0000594 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
595 if (!Methods)
596 Methods = new std::vector<const CXXMethodDecl *>;
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Anders Carlsson05eb2442009-05-16 23:58:37 +0000598 Methods->push_back(MD);
599}
600
601CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
602 if (!OverriddenMethods)
603 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Anders Carlsson05eb2442009-05-16 23:58:37 +0000605 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000606 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000607 return 0;
Daniel Dunbar0908c332009-08-01 23:40:20 +0000608
Anders Carlsson05eb2442009-05-16 23:58:37 +0000609 return &(*it->second)[0];
610}
611
612CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
613 if (!OverriddenMethods)
614 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Anders Carlsson05eb2442009-05-16 23:58:37 +0000616 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000617 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000618 return 0;
619
Daniel Dunbar637ec322009-08-02 01:48:29 +0000620 return &(*it->second)[0] + it->second->size();
Anders Carlsson05eb2442009-05-16 23:58:37 +0000621}
622
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000623QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000624 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
625 // If the member function is declared const, the type of this is const X*,
626 // if the member function is declared volatile, the type of this is
627 // volatile X*, and if the member function is declared const volatile,
628 // the type of this is const volatile X*.
629
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000630 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000631
632 QualType ClassTy;
633 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
634 ClassTy = TD->getInjectedClassNameType(C);
635 else
Mike Stumpe607ed02009-08-07 18:05:12 +0000636 ClassTy = C.getTagDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000637 ClassTy = C.getQualifiedType(ClassTy,
638 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000639 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000640}
641
Douglas Gregor7ad83902008-11-05 04:29:56 +0000642CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000643CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000644 CXXConstructorDecl *C,
Mike Stump1eb44332009-09-09 15:08:12 +0000645 SourceLocation L, SourceLocation R)
Douglas Gregor72f6d672009-09-01 21:04:42 +0000646 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000647 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
648 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
649 BaseOrMember |= 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Douglas Gregor7ad83902008-11-05 04:29:56 +0000651 if (NumArgs > 0) {
652 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000653 // FIXME. Allocation via Context
654 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000655 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
656 this->Args[Idx] = Args[Idx];
657 }
Douglas Gregor72f6d672009-09-01 21:04:42 +0000658 CtorOrAnonUnion = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000659}
660
661CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000662CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000663 CXXConstructorDecl *C,
Anders Carlsson8c57a662009-08-29 01:31:33 +0000664 SourceLocation L, SourceLocation R)
Douglas Gregor72f6d672009-09-01 21:04:42 +0000665 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000666 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
Mike Stump1eb44332009-09-09 15:08:12 +0000667 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
Douglas Gregor7ad83902008-11-05 04:29:56 +0000668
669 if (NumArgs > 0) {
670 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000671 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000672 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
673 this->Args[Idx] = Args[Idx];
674 }
Douglas Gregor72f6d672009-09-01 21:04:42 +0000675 CtorOrAnonUnion = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000676}
677
678CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
679 delete [] Args;
680}
681
Douglas Gregorb48fe382008-10-31 09:07:45 +0000682CXXConstructorDecl *
683CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000684 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000685 QualType T, DeclaratorInfo *DInfo,
686 bool isExplicit,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000687 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000688 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
689 "Name must refer to a constructor");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000690 return new (C) CXXConstructorDecl(RD, L, N, T, DInfo, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000691 isImplicitlyDeclared);
692}
693
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000694bool CXXConstructorDecl::isDefaultConstructor() const {
695 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000696 // A default constructor for a class X is a constructor of class
697 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000698 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000699 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000700}
701
Mike Stump1eb44332009-09-09 15:08:12 +0000702bool
703CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000704 unsigned &TypeQuals) const {
705 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000706 // A non-template constructor for class X is a copy constructor
707 // if its first parameter is of type X&, const X&, volatile X& or
708 // const volatile X&, and either there are no other parameters
709 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000710 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000711 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000712 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000713 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000714 return false;
715
716 const ParmVarDecl *Param = getParamDecl(0);
717
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000718 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000719 const LValueReferenceType *ParamRefType =
720 Param->getType()->getAs<LValueReferenceType>();
721 if (!ParamRefType)
722 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000723
Douglas Gregorfd476482009-11-13 23:59:09 +0000724 // Is it a reference to our class type?
725 CanQualType PointeeType
726 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000727 CanQualType ClassTy
728 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000729 if (PointeeType.getUnqualifiedType() != ClassTy)
730 return false;
731
John McCall0953e762009-09-24 19:53:00 +0000732 // FIXME: other qualifiers?
733
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000734 // We have a copy constructor.
735 TypeQuals = PointeeType.getCVRQualifiers();
736 return true;
737}
738
Anders Carlssonfaccd722009-08-28 16:57:08 +0000739bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000740 // C++ [class.conv.ctor]p1:
741 // A constructor declared without the function-specifier explicit
742 // that can be called with a single parameter specifies a
743 // conversion from the type of its first parameter to the type of
744 // its class. Such a constructor is called a converting
745 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000746 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000747 return false;
748
Mike Stump1eb44332009-09-09 15:08:12 +0000749 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000750 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000751 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000752 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000753}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000754
Douglas Gregor42a552f2008-11-05 20:51:48 +0000755CXXDestructorDecl *
756CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000757 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000758 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000759 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000760 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
761 "Name must refer to a destructor");
Mike Stump1eb44332009-09-09 15:08:12 +0000762 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff3e970492009-01-27 21:25:57 +0000763 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000764}
765
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000766void
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000767CXXDestructorDecl::Destroy(ASTContext& C) {
768 C.Deallocate(BaseOrMemberDestructions);
769 CXXMethodDecl::Destroy(C);
770}
771
772void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000773CXXConstructorDecl::Destroy(ASTContext& C) {
774 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000775 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000776}
777
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000778CXXConversionDecl *
779CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000780 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000781 QualType T, DeclaratorInfo *DInfo,
782 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000783 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
784 "Name must refer to a conversion function");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000785 return new (C) CXXConversionDecl(RD, L, N, T, DInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000786}
787
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000788OverloadedFunctionDecl *
789OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000790 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000791 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000792}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000793
Douglas Gregordec06662009-08-21 18:42:58 +0000794OverloadIterator::OverloadIterator(NamedDecl *ND) : D(0) {
795 if (!ND)
796 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Douglas Gregordec06662009-08-21 18:42:58 +0000798 if (isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND))
799 D = ND;
800 else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(ND)) {
Douglas Gregor8f1d89e2009-09-01 16:58:52 +0000801 if (Ovl->size() != 0) {
802 D = ND;
803 Iter = Ovl->function_begin();
804 }
Douglas Gregordec06662009-08-21 18:42:58 +0000805 }
806}
807
Douglas Gregor364e0212009-06-27 21:05:07 +0000808void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
809 Functions.push_back(F);
810 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000811}
812
Douglas Gregordaa439a2009-07-08 10:57:20 +0000813OverloadIterator::reference OverloadIterator::operator*() const {
814 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
815 return FD;
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Douglas Gregordaa439a2009-07-08 10:57:20 +0000817 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
818 return FTD;
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Douglas Gregordaa439a2009-07-08 10:57:20 +0000820 assert(isa<OverloadedFunctionDecl>(D));
821 return *Iter;
822}
823
824OverloadIterator &OverloadIterator::operator++() {
825 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
826 D = 0;
827 return *this;
828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Douglas Gregordaa439a2009-07-08 10:57:20 +0000830 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
831 D = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Douglas Gregordaa439a2009-07-08 10:57:20 +0000833 return *this;
834}
835
836bool OverloadIterator::Equals(const OverloadIterator &Other) const {
837 if (!D || !Other.D)
838 return D == Other.D;
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Douglas Gregordaa439a2009-07-08 10:57:20 +0000840 if (D != Other.D)
841 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Douglas Gregordaa439a2009-07-08 10:57:20 +0000843 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
844}
845
John McCall02cace72009-08-28 07:59:38 +0000846FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
847 SourceLocation L,
848 FriendUnion Friend,
849 SourceLocation FriendL) {
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000850#ifndef NDEBUG
John McCall02cace72009-08-28 07:59:38 +0000851 if (Friend.is<NamedDecl*>()) {
852 NamedDecl *D = Friend.get<NamedDecl*>();
853 assert(isa<FunctionDecl>(D) ||
854 isa<CXXRecordDecl>(D) ||
855 isa<FunctionTemplateDecl>(D) ||
856 isa<ClassTemplateDecl>(D));
857 assert(D->getFriendObjectKind());
858 }
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000859#endif
John McCallc48fbdf2009-08-11 21:13:21 +0000860
John McCall02cace72009-08-28 07:59:38 +0000861 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump1eb44332009-09-09 15:08:12 +0000862}
John McCall3f9a8a62009-08-11 06:59:38 +0000863
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000864LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000865 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000866 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000867 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000868 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000869}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000870
871UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
872 SourceLocation L,
873 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000874 SourceRange QualifierRange,
875 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000876 SourceLocation IdentLoc,
877 NamespaceDecl *Used,
878 DeclContext *CommonAncestor) {
Mike Stump1eb44332009-09-09 15:08:12 +0000879 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000880 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000881}
882
Mike Stump1eb44332009-09-09 15:08:12 +0000883NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
884 SourceLocation L,
885 SourceLocation AliasLoc,
886 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000887 SourceRange QualifierRange,
888 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +0000889 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +0000890 NamedDecl *Namespace) {
Mike Stump1eb44332009-09-09 15:08:12 +0000891 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000892 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000893}
894
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000895UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
896 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
897 SourceLocation UL, NamedDecl* Target,
898 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
899 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
900 TargetNNS, IsTypeNameArg);
901}
902
Anders Carlsson665b49c2009-08-28 05:30:28 +0000903UnresolvedUsingDecl *UnresolvedUsingDecl::Create(ASTContext &C, DeclContext *DC,
904 SourceLocation UsingLoc,
905 SourceRange TargetNNR,
906 NestedNameSpecifier *TargetNNS,
907 SourceLocation TargetNameLoc,
908 DeclarationName TargetName,
909 bool IsTypeNameArg) {
910 return new (C) UnresolvedUsingDecl(DC, UsingLoc, TargetNNR, TargetNNS,
911 TargetNameLoc, TargetName, IsTypeNameArg);
912}
913
Anders Carlssonfb311762009-03-14 00:25:26 +0000914StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
915 SourceLocation L, Expr *AssertExpr,
916 StringLiteral *Message) {
917 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
918}
919
920void StaticAssertDecl::Destroy(ASTContext& C) {
921 AssertExpr->Destroy(C);
922 Message->Destroy(C);
923 this->~StaticAssertDecl();
924 C.Deallocate((void *)this);
925}
926
927StaticAssertDecl::~StaticAssertDecl() {
928}
929
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000930static const char *getAccessName(AccessSpecifier AS) {
931 switch (AS) {
932 default:
933 case AS_none:
934 assert("Invalid access specifier!");
935 return 0;
936 case AS_public:
937 return "public";
938 case AS_private:
939 return "private";
940 case AS_protected:
941 return "protected";
942 }
943}
944
945const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
946 AccessSpecifier AS) {
947 return DB << getAccessName(AS);
948}
949
950