blob: 73f47d9bc06815dfa576388d01d8c7cbbe85e3d1 [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000017#include "clang/AST/Expr.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000018#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000019#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000020#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000021#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Decl Allocation/Deallocation Method Implementations
26//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000027
Douglas Gregor3e00bad2009-02-17 01:05:43 +000028CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000029 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000030 CXXRecordDecl *PrevDecl,
Mike Stump1eb44332009-09-09 15:08:12 +000031 SourceLocation TKL)
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000032 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
Douglas Gregor7d7e6722008-11-12 23:21:09 +000033 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000034 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000035 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
36 Abstract(false), HasTrivialConstructor(true),
37 HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true),
Fariborz Jahanian62509212009-09-12 18:26:03 +000038 HasTrivialDestructor(true), ComputedVisibleConversions(false),
39 Bases(0), NumBases(0), VBases(0), NumVBases(0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000040 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000041
Ted Kremenek4b7c9832008-09-05 17:16:31 +000042CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
43 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000044 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000045 CXXRecordDecl* PrevDecl,
46 bool DelayTypeCreation) {
Mike Stump1eb44332009-09-09 15:08:12 +000047 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000048 PrevDecl, TKL);
Mike Stump1eb44332009-09-09 15:08:12 +000049
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000050 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000051 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000052 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000053 return R;
54}
55
Douglas Gregorf8268ae2008-10-22 17:49:05 +000056CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000057}
58
59void CXXRecordDecl::Destroy(ASTContext &C) {
60 C.Deallocate(Bases);
Fariborz Jahanian71c6e712009-07-22 17:41:53 +000061 C.Deallocate(VBases);
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000062 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000063}
64
Mike Stump1eb44332009-09-09 15:08:12 +000065void
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000066CXXRecordDecl::setBases(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +000067 CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000068 unsigned NumBases) {
Mike Stump1eb44332009-09-09 15:08:12 +000069 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000070 // An aggregate is an array or a class (clause 9) with [...]
71 // no base classes [...].
72 Aggregate = false;
73
Douglas Gregor57c856b2008-10-23 18:13:27 +000074 if (this->Bases)
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000075 C.Deallocate(this->Bases);
Mike Stump1eb44332009-09-09 15:08:12 +000076
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000077 int vbaseCount = 0;
78 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
79 bool hasDirectVirtualBase = false;
Mike Stump1eb44332009-09-09 15:08:12 +000080
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000081 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor57c856b2008-10-23 18:13:27 +000082 this->NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000083 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor57c856b2008-10-23 18:13:27 +000084 this->Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000085 // Keep track of inherited vbases for this base class.
86 const CXXBaseSpecifier *Base = Bases[i];
87 QualType BaseType = Base->getType();
Mike Stump1eb44332009-09-09 15:08:12 +000088 // Skip template types.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000089 // FIXME. This means that this list must be rebuilt during template
90 // instantiation.
91 if (BaseType->isDependentType())
92 continue;
93 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +000094 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000095 if (Base->isVirtual())
96 hasDirectVirtualBase = true;
Mike Stump1eb44332009-09-09 15:08:12 +000097 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000098 BaseClassDecl->vbases_begin(),
99 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Mike Stump1eb44332009-09-09 15:08:12 +0000100 // Add this vbase to the array of vbases for current class if it is
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000101 // not already in the list.
102 // FIXME. Note that we do a linear search as number of such classes are
103 // very few.
104 int i;
105 for (i = 0; i < vbaseCount; ++i)
106 if (UniqueVbases[i]->getType() == VBase->getType())
107 break;
108 if (i == vbaseCount) {
109 UniqueVbases.push_back(VBase);
110 ++vbaseCount;
111 }
112 }
113 }
114 if (hasDirectVirtualBase) {
115 // Iterate one more time through the direct bases and add the virtual
116 // base to the list of vritual bases for current class.
117 for (unsigned i = 0; i < NumBases; ++i) {
118 const CXXBaseSpecifier *VBase = Bases[i];
119 if (!VBase->isVirtual())
120 continue;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000121 int j;
122 for (j = 0; j < vbaseCount; ++j)
123 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000124 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000125 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000126 UniqueVbases.push_back(VBase);
127 ++vbaseCount;
128 }
129 }
130 }
131 if (vbaseCount > 0) {
132 // build AST for inhireted, direct or indirect, virtual bases.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000133 this->VBases = new (C) CXXBaseSpecifier [vbaseCount];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000134 this->NumVBases = vbaseCount;
135 for (int i = 0; i < vbaseCount; i++) {
136 QualType QT = UniqueVbases[i]->getType();
137 CXXRecordDecl *VBaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000138 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000139 this->VBases[i] =
Douglas Gregor2aef06d2009-07-22 20:55:49 +0000140 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
141 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
142 UniqueVbases[i]->getAccessSpecifier(), QT);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000143 }
144 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000145}
146
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000147/// Callback function for CXXRecordDecl::forallBases that acknowledges
148/// that it saw a base class.
149static bool SawBase(const CXXRecordDecl *, void *) {
150 return true;
151}
152
153bool CXXRecordDecl::hasAnyDependentBases() const {
154 if (!isDependentContext())
155 return false;
156
157 return !forallBases(SawBase, 0);
158}
159
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000160bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000161 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000162}
163
Mike Stump1eb44332009-09-09 15:08:12 +0000164CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000165 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000166 QualType ClassType
167 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000168 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000169 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000170 Context.getCanonicalType(ClassType));
171 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000172 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000173 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000174 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000175 // C++ [class.copy]p2:
176 // A non-template constructor for class X is a copy constructor if [...]
177 if (isa<FunctionTemplateDecl>(*Con))
178 continue;
179
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000180 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000181 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
182 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000183 return cast<CXXConstructorDecl>(*Con);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000185 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000186 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000187 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000188}
189
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000190bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
191 const CXXMethodDecl *& MD) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000192 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
193 const_cast<CXXRecordDecl*>(this)));
194 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
195
196 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000197 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000198 Op != OpEnd; ++Op) {
199 // C++ [class.copy]p9:
200 // A user-declared copy assignment operator is a non-static non-template
201 // member function of class X with exactly one parameter of type X, X&,
202 // const X&, volatile X& or const volatile X&.
Douglas Gregor682054c2009-10-30 22:48:49 +0000203 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
204 if (!Method)
205 continue;
206
Sebastian Redl64b45f72009-01-05 20:52:13 +0000207 if (Method->isStatic())
208 continue;
Douglas Gregor77da3f42009-10-13 23:45:19 +0000209 if (Method->getPrimaryTemplate())
210 continue;
Douglas Gregor72564e72009-02-26 23:50:07 +0000211 const FunctionProtoType *FnType =
John McCall183700f2009-09-21 23:43:11 +0000212 Method->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000213 assert(FnType && "Overloaded operator has no prototype.");
214 // Don't assert on this; an invalid decl might have been left in the AST.
215 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
216 continue;
217 bool AcceptsConst = true;
218 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000219 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000220 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000221 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000222 if (!ArgType.isConstQualified())
223 AcceptsConst = false;
224 }
Douglas Gregora4923eb2009-11-16 21:35:15 +0000225 if (!Context.hasSameUnqualifiedType(ArgType, ClassType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000226 continue;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000227 MD = Method;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000228 // We have a single argument of type cv X or cv X&, i.e. we've found the
229 // copy assignment operator. Return whether it accepts const arguments.
230 return AcceptsConst;
231 }
232 assert(isInvalidDecl() &&
233 "No copy assignment operator declared in valid code.");
234 return false;
235}
236
237void
Mike Stump1eb44332009-09-09 15:08:12 +0000238CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000239 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000240 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
241 // Note that we have a user-declared constructor.
242 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000243
Mike Stump1eb44332009-09-09 15:08:12 +0000244 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000245 // An aggregate is an array or a class (clause 9) with no
246 // user-declared constructors (12.1) [...].
247 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000248
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000249 // C++ [class]p4:
250 // A POD-struct is an aggregate class [...]
251 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000252
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000253 // C++ [class.ctor]p5:
254 // A constructor is trivial if it is an implicitly-declared default
255 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000256 // FIXME: C++0x: don't do this for "= default" default constructors.
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000257 HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000259 // Note when we have a user-declared copy constructor, which will
260 // suppress the implicit declaration of a copy constructor.
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000261 if (ConDecl->isCopyConstructor()) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000262 UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000263
264 // C++ [class.copy]p6:
265 // A copy constructor is trivial if it is implicitly declared.
266 // FIXME: C++0x: don't do this for "= default" copy constructors.
267 HasTrivialCopyConstructor = false;
268 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000269}
270
Sebastian Redl64b45f72009-01-05 20:52:13 +0000271void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
272 CXXMethodDecl *OpDecl) {
273 // We're interested specifically in copy assignment operators.
John McCall183700f2009-09-21 23:43:11 +0000274 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000275 assert(FnType && "Overloaded operator has no proto function type.");
276 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregor77da3f42009-10-13 23:45:19 +0000277
278 // Copy assignment operators must be non-templates.
279 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
280 return;
281
Sebastian Redl64b45f72009-01-05 20:52:13 +0000282 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000283 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000284 ArgType = Ref->getPointeeType();
285
286 ArgType = ArgType.getUnqualifiedType();
287 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
288 const_cast<CXXRecordDecl*>(this)));
289
Douglas Gregora4923eb2009-11-16 21:35:15 +0000290 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000291 return;
292
293 // This is a copy assignment operator.
Eli Friedman88fad632009-11-07 00:02:45 +0000294 // Note on the decl that it is a copy assignment operator.
295 OpDecl->setCopyAssignment(true);
296
Sebastian Redl64b45f72009-01-05 20:52:13 +0000297 // Suppress the implicit declaration of a copy constructor.
298 UserDeclaredCopyAssignment = true;
299
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000300 // C++ [class.copy]p11:
301 // A copy assignment operator is trivial if it is implicitly declared.
302 // FIXME: C++0x: don't do this for "= default" copy operators.
303 HasTrivialCopyAssignment = false;
304
Sebastian Redl64b45f72009-01-05 20:52:13 +0000305 // C++ [class]p4:
306 // A POD-struct is an aggregate class that [...] has no user-defined copy
307 // assignment operator [...].
308 PlainOldData = false;
309}
310
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000311void
312CXXRecordDecl::collectConversionFunctions(
John McCallba135432009-11-21 08:51:07 +0000313 llvm::SmallPtrSet<CanQualType, 8>& ConversionsTypeSet) const
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000314{
John McCalleec51cf2010-01-20 00:46:10 +0000315 const UnresolvedSetImpl *Cs = getConversionFunctions();
316 for (UnresolvedSetImpl::iterator I = Cs->begin(), E = Cs->end();
317 I != E; ++I) {
John McCallba135432009-11-21 08:51:07 +0000318 NamedDecl *TopConv = *I;
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000319 CanQualType TConvType;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000320 if (FunctionTemplateDecl *TConversionTemplate =
321 dyn_cast<FunctionTemplateDecl>(TopConv))
322 TConvType =
323 getASTContext().getCanonicalType(
324 TConversionTemplate->getTemplatedDecl()->getResultType());
325 else
326 TConvType =
327 getASTContext().getCanonicalType(
328 cast<CXXConversionDecl>(TopConv)->getConversionType());
329 ConversionsTypeSet.insert(TConvType);
330 }
331}
332
Fariborz Jahanian62509212009-09-12 18:26:03 +0000333/// getNestedVisibleConversionFunctions - imports unique conversion
334/// functions from base classes into the visible conversion function
335/// list of the class 'RD'. This is a private helper method.
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000336/// TopConversionsTypeSet is the set of conversion functions of the class
337/// we are interested in. HiddenConversionTypes is set of conversion functions
338/// of the immediate derived class which hides the conversion functions found
339/// in current class.
Fariborz Jahanian62509212009-09-12 18:26:03 +0000340void
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000341CXXRecordDecl::getNestedVisibleConversionFunctions(CXXRecordDecl *RD,
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000342 const llvm::SmallPtrSet<CanQualType, 8> &TopConversionsTypeSet,
343 const llvm::SmallPtrSet<CanQualType, 8> &HiddenConversionTypes)
344{
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000345 bool inTopClass = (RD == this);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000346 QualType ClassType = getASTContext().getTypeDeclType(this);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000347 if (const RecordType *Record = ClassType->getAs<RecordType>()) {
John McCalleec51cf2010-01-20 00:46:10 +0000348 const UnresolvedSetImpl *Cs
Fariborz Jahanian53462782009-09-11 21:44:33 +0000349 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000350
John McCalleec51cf2010-01-20 00:46:10 +0000351 for (UnresolvedSetImpl::iterator I = Cs->begin(), E = Cs->end();
352 I != E; ++I) {
John McCallba135432009-11-21 08:51:07 +0000353 NamedDecl *Conv = *I;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000354 // Only those conversions not exact match of conversions in current
355 // class are candidateconversion routines.
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000356 CanQualType ConvType;
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000357 if (FunctionTemplateDecl *ConversionTemplate =
358 dyn_cast<FunctionTemplateDecl>(Conv))
359 ConvType =
360 getASTContext().getCanonicalType(
Fariborz Jahaniana5c12942009-09-15 23:02:16 +0000361 ConversionTemplate->getTemplatedDecl()->getResultType());
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000362 else
363 ConvType =
Fariborz Jahanian8b915e72009-09-15 22:15:23 +0000364 getASTContext().getCanonicalType(
365 cast<CXXConversionDecl>(Conv)->getConversionType());
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000366 // We only add conversion functions found in the base class if they
367 // are not hidden by those found in HiddenConversionTypes which are
368 // the conversion functions in its derived class.
369 if (inTopClass ||
370 (!TopConversionsTypeSet.count(ConvType) &&
371 !HiddenConversionTypes.count(ConvType)) ) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000372 if (FunctionTemplateDecl *ConversionTemplate =
373 dyn_cast<FunctionTemplateDecl>(Conv))
374 RD->addVisibleConversionFunction(ConversionTemplate);
375 else
376 RD->addVisibleConversionFunction(cast<CXXConversionDecl>(Conv));
Fariborz Jahanian53462782009-09-11 21:44:33 +0000377 }
378 }
379 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000380
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000381 if (getNumBases() == 0 && getNumVBases() == 0)
382 return;
Sebastian Redl9994a342009-10-25 17:03:50 +0000383
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000384 llvm::SmallPtrSet<CanQualType, 8> ConversionFunctions;
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000385 if (!inTopClass)
386 collectConversionFunctions(ConversionFunctions);
Sebastian Redl9994a342009-10-25 17:03:50 +0000387
Fariborz Jahanian53462782009-09-11 21:44:33 +0000388 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(),
389 E = vbases_end(); VBase != E; ++VBase) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000390 if (const RecordType *RT = VBase->getType()->getAs<RecordType>()) {
391 CXXRecordDecl *VBaseClassDecl
392 = cast<CXXRecordDecl>(RT->getDecl());
393 VBaseClassDecl->getNestedVisibleConversionFunctions(RD,
394 TopConversionsTypeSet,
395 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
396 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000397 }
398 for (CXXRecordDecl::base_class_iterator Base = bases_begin(),
399 E = bases_end(); Base != E; ++Base) {
400 if (Base->isVirtual())
401 continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000402 if (const RecordType *RT = Base->getType()->getAs<RecordType>()) {
403 CXXRecordDecl *BaseClassDecl
404 = cast<CXXRecordDecl>(RT->getDecl());
405
406 BaseClassDecl->getNestedVisibleConversionFunctions(RD,
407 TopConversionsTypeSet,
408 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
409 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000410 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000411}
412
413/// getVisibleConversionFunctions - get all conversion functions visible
414/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000415const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000416 // If root class, all conversions are visible.
417 if (bases_begin() == bases_end())
418 return &Conversions;
419 // If visible conversion list is already evaluated, return it.
420 if (ComputedVisibleConversions)
421 return &VisibleConversions;
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000422 llvm::SmallPtrSet<CanQualType, 8> TopConversionsTypeSet;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000423 collectConversionFunctions(TopConversionsTypeSet);
424 getNestedVisibleConversionFunctions(this, TopConversionsTypeSet,
425 TopConversionsTypeSet);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000426 ComputedVisibleConversions = true;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000427 return &VisibleConversions;
428}
429
Fariborz Jahanian62509212009-09-12 18:26:03 +0000430void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000431 CXXConversionDecl *ConvDecl) {
432 assert(!ConvDecl->getDescribedFunctionTemplate() &&
433 "Conversion function templates should cast to FunctionTemplateDecl.");
John McCallba135432009-11-21 08:51:07 +0000434 VisibleConversions.addDecl(ConvDecl);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000435}
436
Fariborz Jahanian62509212009-09-12 18:26:03 +0000437void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000438 FunctionTemplateDecl *ConvDecl) {
439 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
440 "Function template is not a conversion function template");
John McCallba135432009-11-21 08:51:07 +0000441 VisibleConversions.addDecl(ConvDecl);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000442}
443
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000444void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000445 assert(!ConvDecl->getDescribedFunctionTemplate() &&
446 "Conversion function templates should cast to FunctionTemplateDecl.");
John McCallba135432009-11-21 08:51:07 +0000447 Conversions.addDecl(ConvDecl);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000448}
449
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000450void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000451 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
452 "Function template is not a conversion function template");
John McCallba135432009-11-21 08:51:07 +0000453 Conversions.addDecl(ConvDecl);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000454}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000455
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000456
457void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
458 Method->setVirtualAsWritten(true);
459 setAggregate(false);
460 setPOD(false);
461 setEmpty(false);
462 setPolymorphic(true);
463 setHasTrivialConstructor(false);
464 setHasTrivialCopyConstructor(false);
465 setHasTrivialCopyAssignment(false);
466}
467
Douglas Gregorf6b11852009-10-08 15:14:33 +0000468CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000469 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000470 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
471
472 return 0;
473}
474
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000475MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
476 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
477}
478
Douglas Gregorf6b11852009-10-08 15:14:33 +0000479void
480CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
481 TemplateSpecializationKind TSK) {
482 assert(TemplateOrInstantiation.isNull() &&
483 "Previous template or instantiation?");
484 assert(!isa<ClassTemplateSpecializationDecl>(this));
485 TemplateOrInstantiation
486 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
487}
488
Anders Carlssonb13e3572009-12-07 06:33:48 +0000489TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
490 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000491 = dyn_cast<ClassTemplateSpecializationDecl>(this))
492 return Spec->getSpecializationKind();
493
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000494 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000495 return MSInfo->getTemplateSpecializationKind();
496
497 return TSK_Undeclared;
498}
499
500void
501CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
502 if (ClassTemplateSpecializationDecl *Spec
503 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
504 Spec->setSpecializationKind(TSK);
505 return;
506 }
507
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000508 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000509 MSInfo->setTemplateSpecializationKind(TSK);
510 return;
511 }
512
513 assert(false && "Not a class template or member class specialization");
514}
515
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000516CXXConstructorDecl *
517CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
518 QualType ClassType = Context.getTypeDeclType(this);
519 DeclarationName ConstructorName
520 = Context.DeclarationNames.getCXXConstructorName(
521 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000523 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000524 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000525 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000526 // FIXME: In C++0x, a constructor template can be a default constructor.
527 if (isa<FunctionTemplateDecl>(*Con))
528 continue;
529
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000530 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
531 if (Constructor->isDefaultConstructor())
532 return Constructor;
533 }
534 return 0;
535}
536
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000537CXXDestructorDecl *CXXRecordDecl::getDestructor(ASTContext &Context) {
Anders Carlsson7267c162009-05-29 21:03:38 +0000538 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000539
540 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000541 = Context.DeclarationNames.getCXXDestructorName(
542 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000543
544 DeclContext::lookup_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000545 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000546 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000548 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000549 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Anders Carlsson7267c162009-05-29 21:03:38 +0000551 return Dtor;
552}
553
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000554CXXMethodDecl *
555CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000556 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000557 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000558 bool isStatic, bool isInline) {
John McCalla93c9342009-12-07 02:54:59 +0000559 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000560 isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000561}
562
Douglas Gregor90916562009-09-29 18:16:17 +0000563bool CXXMethodDecl::isUsualDeallocationFunction() const {
564 if (getOverloadedOperator() != OO_Delete &&
565 getOverloadedOperator() != OO_Array_Delete)
566 return false;
567
568 // C++ [basic.stc.dynamic.deallocation]p2:
569 // If a class T has a member deallocation function named operator delete
570 // with exactly one parameter, then that function is a usual (non-placement)
571 // deallocation function. [...]
572 if (getNumParams() == 1)
573 return true;
574
575 // C++ [basic.stc.dynamic.deallocation]p2:
576 // [...] If class T does not declare such an operator delete but does
577 // declare a member deallocation function named operator delete with
578 // exactly two parameters, the second of which has type std::size_t (18.1),
579 // then this function is a usual deallocation function.
580 ASTContext &Context = getASTContext();
581 if (getNumParams() != 2 ||
582 !Context.hasSameType(getParamDecl(1)->getType(), Context.getSizeType()))
583 return false;
584
585 // This function is a usual deallocation function if there are no
586 // single-parameter deallocation functions of the same kind.
587 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
588 R.first != R.second; ++R.first) {
589 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
590 if (FD->getNumParams() == 1)
591 return false;
592 }
593
594 return true;
595}
596
Mike Stump1eb44332009-09-09 15:08:12 +0000597typedef llvm::DenseMap<const CXXMethodDecl*,
598 std::vector<const CXXMethodDecl *> *>
Anders Carlsson05eb2442009-05-16 23:58:37 +0000599 OverriddenMethodsMapTy;
600
Mike Stumpb9871a22009-08-21 01:45:00 +0000601// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
602// the vtable building code uses it at CG time.
Anders Carlsson05eb2442009-05-16 23:58:37 +0000603static OverriddenMethodsMapTy *OverriddenMethods = 0;
604
605void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000606 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000607 assert(!MD->getParent()->isDependentContext() &&
608 "Can't add an overridden method to a class template!");
609
Anders Carlsson05eb2442009-05-16 23:58:37 +0000610 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Anders Carlsson05eb2442009-05-16 23:58:37 +0000612 if (!OverriddenMethods)
613 OverriddenMethods = new OverriddenMethodsMapTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Anders Carlsson05eb2442009-05-16 23:58:37 +0000615 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
616 if (!Methods)
617 Methods = new std::vector<const CXXMethodDecl *>;
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Anders Carlsson05eb2442009-05-16 23:58:37 +0000619 Methods->push_back(MD);
620}
621
622CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
623 if (!OverriddenMethods)
624 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Anders Carlsson05eb2442009-05-16 23:58:37 +0000626 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000627 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000628 return 0;
Daniel Dunbar0908c332009-08-01 23:40:20 +0000629
Anders Carlsson05eb2442009-05-16 23:58:37 +0000630 return &(*it->second)[0];
631}
632
633CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
634 if (!OverriddenMethods)
635 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Anders Carlsson05eb2442009-05-16 23:58:37 +0000637 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000638 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000639 return 0;
640
Daniel Dunbar637ec322009-08-02 01:48:29 +0000641 return &(*it->second)[0] + it->second->size();
Anders Carlsson05eb2442009-05-16 23:58:37 +0000642}
643
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000644QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000645 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
646 // If the member function is declared const, the type of this is const X*,
647 // if the member function is declared volatile, the type of this is
648 // volatile X*, and if the member function is declared const volatile,
649 // the type of this is const volatile X*.
650
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000651 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000652
653 QualType ClassTy;
654 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
655 ClassTy = TD->getInjectedClassNameType(C);
656 else
Mike Stumpe607ed02009-08-07 18:05:12 +0000657 ClassTy = C.getTagDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000658 ClassTy = C.getQualifiedType(ClassTy,
659 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000660 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000661}
662
Eli Friedmand7d7f672009-12-06 20:50:05 +0000663bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000664 // If this function is a template instantiation, look at the template from
665 // which it was instantiated.
666 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
667 if (!CheckFn)
668 CheckFn = this;
669
Eli Friedmand7d7f672009-12-06 20:50:05 +0000670 const FunctionDecl *fn;
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000671 return CheckFn->getBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000672}
673
Douglas Gregor7ad83902008-11-05 04:29:56 +0000674CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000675CXXBaseOrMemberInitializer(ASTContext &Context,
John McCalla93c9342009-12-07 02:54:59 +0000676 TypeSourceInfo *TInfo, CXXConstructorDecl *C,
Douglas Gregor802ab452009-12-02 22:36:29 +0000677 SourceLocation L,
678 Expr **Args, unsigned NumArgs,
679 SourceLocation R)
John McCalla93c9342009-12-07 02:54:59 +0000680 : BaseOrMember(TInfo), Args(0), NumArgs(0), CtorOrAnonUnion(C),
Douglas Gregor802ab452009-12-02 22:36:29 +0000681 LParenLoc(L), RParenLoc(R)
682{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000683 if (NumArgs > 0) {
684 this->NumArgs = NumArgs;
Douglas Gregor802ab452009-12-02 22:36:29 +0000685 this->Args = new (Context) Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000686 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
687 this->Args[Idx] = Args[Idx];
688 }
689}
690
691CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000692CXXBaseOrMemberInitializer(ASTContext &Context,
693 FieldDecl *Member, SourceLocation MemberLoc,
694 CXXConstructorDecl *C, SourceLocation L,
695 Expr **Args, unsigned NumArgs,
696 SourceLocation R)
697 : BaseOrMember(Member), MemberLocation(MemberLoc), Args(0), NumArgs(0),
698 CtorOrAnonUnion(C), LParenLoc(L), RParenLoc(R)
699{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000700 if (NumArgs > 0) {
701 this->NumArgs = NumArgs;
Douglas Gregor802ab452009-12-02 22:36:29 +0000702 this->Args = new (Context) Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000703 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
704 this->Args[Idx] = Args[Idx];
705 }
706}
707
Douglas Gregor802ab452009-12-02 22:36:29 +0000708void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) {
709 for (unsigned I = 0; I != NumArgs; ++I)
710 Args[I]->Destroy(Context);
711 Context.Deallocate(Args);
712 this->~CXXBaseOrMemberInitializer();
713}
714
715TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
716 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000717 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000718 else
719 return TypeLoc();
720}
721
722Type *CXXBaseOrMemberInitializer::getBaseClass() {
723 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000724 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000725 else
726 return 0;
727}
728
729const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
730 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000731 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000732 else
733 return 0;
734}
735
736SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
737 if (isMemberInitializer())
738 return getMemberLocation();
739
740 return getBaseClassLoc().getSourceRange().getBegin();
741}
742
743SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
744 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000745}
746
Douglas Gregorb48fe382008-10-31 09:07:45 +0000747CXXConstructorDecl *
748CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000749 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000750 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000751 bool isExplicit,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000752 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000753 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
754 "Name must refer to a constructor");
John McCalla93c9342009-12-07 02:54:59 +0000755 return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000756 isImplicitlyDeclared);
757}
758
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000759bool CXXConstructorDecl::isDefaultConstructor() const {
760 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000761 // A default constructor for a class X is a constructor of class
762 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000763 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000764 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000765}
766
Mike Stump1eb44332009-09-09 15:08:12 +0000767bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000768CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000769 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000770 // A non-template constructor for class X is a copy constructor
771 // if its first parameter is of type X&, const X&, volatile X& or
772 // const volatile X&, and either there are no other parameters
773 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000774 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000775 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000776 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000777 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000778 return false;
779
780 const ParmVarDecl *Param = getParamDecl(0);
781
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000782 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000783 const LValueReferenceType *ParamRefType =
784 Param->getType()->getAs<LValueReferenceType>();
785 if (!ParamRefType)
786 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000787
Douglas Gregorfd476482009-11-13 23:59:09 +0000788 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000789 ASTContext &Context = getASTContext();
790
Douglas Gregorfd476482009-11-13 23:59:09 +0000791 CanQualType PointeeType
792 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000793 CanQualType ClassTy
794 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000795 if (PointeeType.getUnqualifiedType() != ClassTy)
796 return false;
797
John McCall0953e762009-09-24 19:53:00 +0000798 // FIXME: other qualifiers?
799
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000800 // We have a copy constructor.
801 TypeQuals = PointeeType.getCVRQualifiers();
802 return true;
803}
804
Anders Carlssonfaccd722009-08-28 16:57:08 +0000805bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000806 // C++ [class.conv.ctor]p1:
807 // A constructor declared without the function-specifier explicit
808 // that can be called with a single parameter specifies a
809 // conversion from the type of its first parameter to the type of
810 // its class. Such a constructor is called a converting
811 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000812 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000813 return false;
814
Mike Stump1eb44332009-09-09 15:08:12 +0000815 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000816 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000817 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000818 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000819}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000820
Douglas Gregor66724ea2009-11-14 01:20:54 +0000821bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
822 if ((getNumParams() < 1) ||
823 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
824 (getPrimaryTemplate() == 0) ||
825 (getDescribedFunctionTemplate() != 0))
826 return false;
827
828 const ParmVarDecl *Param = getParamDecl(0);
829
830 ASTContext &Context = getASTContext();
831 CanQualType ParamType = Context.getCanonicalType(Param->getType());
832
833 // Strip off the lvalue reference, if any.
834 if (CanQual<LValueReferenceType> ParamRefType
835 = ParamType->getAs<LValueReferenceType>())
836 ParamType = ParamRefType->getPointeeType();
837
838
839 // Is it the same as our our class type?
840 CanQualType ClassTy
841 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
842 if (ParamType.getUnqualifiedType() != ClassTy)
843 return false;
844
845 return true;
846}
847
Douglas Gregor42a552f2008-11-05 20:51:48 +0000848CXXDestructorDecl *
849CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000850 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000851 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000852 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000853 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
854 "Name must refer to a destructor");
Mike Stump1eb44332009-09-09 15:08:12 +0000855 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff3e970492009-01-27 21:25:57 +0000856 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000857}
858
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000859void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000860CXXConstructorDecl::Destroy(ASTContext& C) {
861 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000862 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000863}
864
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000865CXXConversionDecl *
866CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000867 SourceLocation L, DeclarationName N,
John McCalla93c9342009-12-07 02:54:59 +0000868 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000869 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000870 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
871 "Name must refer to a conversion function");
John McCalla93c9342009-12-07 02:54:59 +0000872 return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000873}
874
John McCall02cace72009-08-28 07:59:38 +0000875FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
876 SourceLocation L,
877 FriendUnion Friend,
878 SourceLocation FriendL) {
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000879#ifndef NDEBUG
John McCall02cace72009-08-28 07:59:38 +0000880 if (Friend.is<NamedDecl*>()) {
881 NamedDecl *D = Friend.get<NamedDecl*>();
882 assert(isa<FunctionDecl>(D) ||
883 isa<CXXRecordDecl>(D) ||
884 isa<FunctionTemplateDecl>(D) ||
885 isa<ClassTemplateDecl>(D));
John McCalle129d442009-12-17 23:21:11 +0000886
887 // As a temporary hack, we permit template instantiation to point
888 // to the original declaration when instantiating members.
889 assert(D->getFriendObjectKind() ||
890 (cast<CXXRecordDecl>(DC)->getTemplateSpecializationKind()));
John McCall02cace72009-08-28 07:59:38 +0000891 }
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000892#endif
John McCallc48fbdf2009-08-11 21:13:21 +0000893
John McCall02cace72009-08-28 07:59:38 +0000894 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump1eb44332009-09-09 15:08:12 +0000895}
John McCall3f9a8a62009-08-11 06:59:38 +0000896
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000897LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000898 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000899 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000900 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000901 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000902}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000903
904UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
905 SourceLocation L,
906 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000907 SourceRange QualifierRange,
908 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000909 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000910 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000911 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000912 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
913 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000914 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000915 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000916}
917
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000918NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
919 if (NamespaceAliasDecl *NA =
920 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
921 return NA->getNamespace();
922 return cast_or_null<NamespaceDecl>(NominatedNamespace);
923}
924
Mike Stump1eb44332009-09-09 15:08:12 +0000925NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
926 SourceLocation L,
927 SourceLocation AliasLoc,
928 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000929 SourceRange QualifierRange,
930 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +0000931 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +0000932 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000933 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
934 Namespace = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000935 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000936 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000937}
938
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000939UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
John McCall9488ea12009-11-17 05:59:44 +0000940 SourceLocation L, SourceRange NNR, SourceLocation UL,
941 NestedNameSpecifier* TargetNNS, DeclarationName Name,
942 bool IsTypeNameArg) {
943 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000944}
945
John McCall7ba107a2009-11-18 02:36:19 +0000946UnresolvedUsingValueDecl *
947UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
948 SourceLocation UsingLoc,
949 SourceRange TargetNNR,
950 NestedNameSpecifier *TargetNNS,
951 SourceLocation TargetNameLoc,
952 DeclarationName TargetName) {
953 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
954 TargetNNR, TargetNNS,
955 TargetNameLoc, TargetName);
956}
957
958UnresolvedUsingTypenameDecl *
959UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
960 SourceLocation UsingLoc,
961 SourceLocation TypenameLoc,
962 SourceRange TargetNNR,
963 NestedNameSpecifier *TargetNNS,
964 SourceLocation TargetNameLoc,
965 DeclarationName TargetName) {
966 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
967 TargetNNR, TargetNNS,
968 TargetNameLoc,
969 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +0000970}
971
Anders Carlssonfb311762009-03-14 00:25:26 +0000972StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
973 SourceLocation L, Expr *AssertExpr,
974 StringLiteral *Message) {
975 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
976}
977
978void StaticAssertDecl::Destroy(ASTContext& C) {
979 AssertExpr->Destroy(C);
980 Message->Destroy(C);
981 this->~StaticAssertDecl();
982 C.Deallocate((void *)this);
983}
984
985StaticAssertDecl::~StaticAssertDecl() {
986}
987
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000988static const char *getAccessName(AccessSpecifier AS) {
989 switch (AS) {
990 default:
991 case AS_none:
992 assert("Invalid access specifier!");
993 return 0;
994 case AS_public:
995 return "public";
996 case AS_private:
997 return "private";
998 case AS_protected:
999 return "protected";
1000 }
1001}
1002
1003const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1004 AccessSpecifier AS) {
1005 return DB << getAccessName(AS);
1006}
1007
1008