blob: 5064ec5c7377f07442900e7de2ab68cb136274ef [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 Gregor396b7cd2008-11-03 17:51:48 +0000147bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000148 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000149}
150
Mike Stump1eb44332009-09-09 15:08:12 +0000151CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000152 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000153 QualType ClassType
154 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000155 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000156 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000157 Context.getCanonicalType(ClassType));
158 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000159 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000160 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000161 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000162 // C++ [class.copy]p2:
163 // A non-template constructor for class X is a copy constructor if [...]
164 if (isa<FunctionTemplateDecl>(*Con))
165 continue;
166
Mike Stump1eb44332009-09-09 15:08:12 +0000167 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000168 FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000169 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
170 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000171 return cast<CXXConstructorDecl>(*Con);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000173 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000174 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000175 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000176}
177
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000178bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
179 const CXXMethodDecl *& MD) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000180 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
181 const_cast<CXXRecordDecl*>(this)));
182 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
183
184 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000185 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000186 Op != OpEnd; ++Op) {
187 // C++ [class.copy]p9:
188 // A user-declared copy assignment operator is a non-static non-template
189 // member function of class X with exactly one parameter of type X, X&,
190 // const X&, volatile X& or const volatile X&.
Douglas Gregor682054c2009-10-30 22:48:49 +0000191 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
192 if (!Method)
193 continue;
194
Sebastian Redl64b45f72009-01-05 20:52:13 +0000195 if (Method->isStatic())
196 continue;
Douglas Gregor77da3f42009-10-13 23:45:19 +0000197 if (Method->getPrimaryTemplate())
198 continue;
Douglas Gregor72564e72009-02-26 23:50:07 +0000199 const FunctionProtoType *FnType =
John McCall183700f2009-09-21 23:43:11 +0000200 Method->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000201 assert(FnType && "Overloaded operator has no prototype.");
202 // Don't assert on this; an invalid decl might have been left in the AST.
203 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
204 continue;
205 bool AcceptsConst = true;
206 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000207 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000208 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000209 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000210 if (!ArgType.isConstQualified())
211 AcceptsConst = false;
212 }
Douglas Gregora4923eb2009-11-16 21:35:15 +0000213 if (!Context.hasSameUnqualifiedType(ArgType, ClassType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000214 continue;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000215 MD = Method;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000216 // We have a single argument of type cv X or cv X&, i.e. we've found the
217 // copy assignment operator. Return whether it accepts const arguments.
218 return AcceptsConst;
219 }
220 assert(isInvalidDecl() &&
221 "No copy assignment operator declared in valid code.");
222 return false;
223}
224
225void
Mike Stump1eb44332009-09-09 15:08:12 +0000226CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000227 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000228 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
229 // Note that we have a user-declared constructor.
230 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000231
Mike Stump1eb44332009-09-09 15:08:12 +0000232 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000233 // An aggregate is an array or a class (clause 9) with no
234 // user-declared constructors (12.1) [...].
235 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000236
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000237 // C++ [class]p4:
238 // A POD-struct is an aggregate class [...]
239 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000240
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000241 // C++ [class.ctor]p5:
242 // A constructor is trivial if it is an implicitly-declared default
243 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000244 // FIXME: C++0x: don't do this for "= default" default constructors.
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000245 HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000247 // Note when we have a user-declared copy constructor, which will
248 // suppress the implicit declaration of a copy constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000249 if (ConDecl->isCopyConstructor(Context)) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000250 UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000251
252 // C++ [class.copy]p6:
253 // A copy constructor is trivial if it is implicitly declared.
254 // FIXME: C++0x: don't do this for "= default" copy constructors.
255 HasTrivialCopyConstructor = false;
256 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000257}
258
Sebastian Redl64b45f72009-01-05 20:52:13 +0000259void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
260 CXXMethodDecl *OpDecl) {
261 // We're interested specifically in copy assignment operators.
John McCall183700f2009-09-21 23:43:11 +0000262 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000263 assert(FnType && "Overloaded operator has no proto function type.");
264 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregor77da3f42009-10-13 23:45:19 +0000265
266 // Copy assignment operators must be non-templates.
267 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
268 return;
269
Sebastian Redl64b45f72009-01-05 20:52:13 +0000270 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000271 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000272 ArgType = Ref->getPointeeType();
273
274 ArgType = ArgType.getUnqualifiedType();
275 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
276 const_cast<CXXRecordDecl*>(this)));
277
Douglas Gregora4923eb2009-11-16 21:35:15 +0000278 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redl64b45f72009-01-05 20:52:13 +0000279 return;
280
281 // This is a copy assignment operator.
Eli Friedman88fad632009-11-07 00:02:45 +0000282 // Note on the decl that it is a copy assignment operator.
283 OpDecl->setCopyAssignment(true);
284
Sebastian Redl64b45f72009-01-05 20:52:13 +0000285 // Suppress the implicit declaration of a copy constructor.
286 UserDeclaredCopyAssignment = true;
287
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000288 // C++ [class.copy]p11:
289 // A copy assignment operator is trivial if it is implicitly declared.
290 // FIXME: C++0x: don't do this for "= default" copy operators.
291 HasTrivialCopyAssignment = false;
292
Sebastian Redl64b45f72009-01-05 20:52:13 +0000293 // C++ [class]p4:
294 // A POD-struct is an aggregate class that [...] has no user-defined copy
295 // assignment operator [...].
296 PlainOldData = false;
297}
298
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000299void
300CXXRecordDecl::collectConversionFunctions(
John McCallba135432009-11-21 08:51:07 +0000301 llvm::SmallPtrSet<CanQualType, 8>& ConversionsTypeSet) const
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000302{
John McCallba135432009-11-21 08:51:07 +0000303 const UnresolvedSet *Cs = getConversionFunctions();
304 for (UnresolvedSet::iterator I = Cs->begin(), E = Cs->end(); I != E; ++I) {
305 NamedDecl *TopConv = *I;
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000306 CanQualType TConvType;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000307 if (FunctionTemplateDecl *TConversionTemplate =
308 dyn_cast<FunctionTemplateDecl>(TopConv))
309 TConvType =
310 getASTContext().getCanonicalType(
311 TConversionTemplate->getTemplatedDecl()->getResultType());
312 else
313 TConvType =
314 getASTContext().getCanonicalType(
315 cast<CXXConversionDecl>(TopConv)->getConversionType());
316 ConversionsTypeSet.insert(TConvType);
317 }
318}
319
Fariborz Jahanian62509212009-09-12 18:26:03 +0000320/// getNestedVisibleConversionFunctions - imports unique conversion
321/// functions from base classes into the visible conversion function
322/// list of the class 'RD'. This is a private helper method.
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000323/// TopConversionsTypeSet is the set of conversion functions of the class
324/// we are interested in. HiddenConversionTypes is set of conversion functions
325/// of the immediate derived class which hides the conversion functions found
326/// in current class.
Fariborz Jahanian62509212009-09-12 18:26:03 +0000327void
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000328CXXRecordDecl::getNestedVisibleConversionFunctions(CXXRecordDecl *RD,
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000329 const llvm::SmallPtrSet<CanQualType, 8> &TopConversionsTypeSet,
330 const llvm::SmallPtrSet<CanQualType, 8> &HiddenConversionTypes)
331{
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000332 bool inTopClass = (RD == this);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000333 QualType ClassType = getASTContext().getTypeDeclType(this);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000334 if (const RecordType *Record = ClassType->getAs<RecordType>()) {
John McCallba135432009-11-21 08:51:07 +0000335 const UnresolvedSet *Cs
Fariborz Jahanian53462782009-09-11 21:44:33 +0000336 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000337
John McCallba135432009-11-21 08:51:07 +0000338 for (UnresolvedSet::iterator I = Cs->begin(), E = Cs->end(); I != E; ++I) {
339 NamedDecl *Conv = *I;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000340 // Only those conversions not exact match of conversions in current
341 // class are candidateconversion routines.
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000342 CanQualType ConvType;
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000343 if (FunctionTemplateDecl *ConversionTemplate =
344 dyn_cast<FunctionTemplateDecl>(Conv))
345 ConvType =
346 getASTContext().getCanonicalType(
Fariborz Jahaniana5c12942009-09-15 23:02:16 +0000347 ConversionTemplate->getTemplatedDecl()->getResultType());
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000348 else
349 ConvType =
Fariborz Jahanian8b915e72009-09-15 22:15:23 +0000350 getASTContext().getCanonicalType(
351 cast<CXXConversionDecl>(Conv)->getConversionType());
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000352 // We only add conversion functions found in the base class if they
353 // are not hidden by those found in HiddenConversionTypes which are
354 // the conversion functions in its derived class.
355 if (inTopClass ||
356 (!TopConversionsTypeSet.count(ConvType) &&
357 !HiddenConversionTypes.count(ConvType)) ) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000358 if (FunctionTemplateDecl *ConversionTemplate =
359 dyn_cast<FunctionTemplateDecl>(Conv))
360 RD->addVisibleConversionFunction(ConversionTemplate);
361 else
362 RD->addVisibleConversionFunction(cast<CXXConversionDecl>(Conv));
Fariborz Jahanian53462782009-09-11 21:44:33 +0000363 }
364 }
365 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000366
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000367 if (getNumBases() == 0 && getNumVBases() == 0)
368 return;
Sebastian Redl9994a342009-10-25 17:03:50 +0000369
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000370 llvm::SmallPtrSet<CanQualType, 8> ConversionFunctions;
Fariborz Jahanian12af63b2009-10-08 16:33:37 +0000371 if (!inTopClass)
372 collectConversionFunctions(ConversionFunctions);
Sebastian Redl9994a342009-10-25 17:03:50 +0000373
Fariborz Jahanian53462782009-09-11 21:44:33 +0000374 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(),
375 E = vbases_end(); VBase != E; ++VBase) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000376 if (const RecordType *RT = VBase->getType()->getAs<RecordType>()) {
377 CXXRecordDecl *VBaseClassDecl
378 = cast<CXXRecordDecl>(RT->getDecl());
379 VBaseClassDecl->getNestedVisibleConversionFunctions(RD,
380 TopConversionsTypeSet,
381 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
382 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000383 }
384 for (CXXRecordDecl::base_class_iterator Base = bases_begin(),
385 E = bases_end(); Base != E; ++Base) {
386 if (Base->isVirtual())
387 continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000388 if (const RecordType *RT = Base->getType()->getAs<RecordType>()) {
389 CXXRecordDecl *BaseClassDecl
390 = cast<CXXRecordDecl>(RT->getDecl());
391
392 BaseClassDecl->getNestedVisibleConversionFunctions(RD,
393 TopConversionsTypeSet,
394 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
395 }
Fariborz Jahanian53462782009-09-11 21:44:33 +0000396 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000397}
398
399/// getVisibleConversionFunctions - get all conversion functions visible
400/// in current class; including conversion function templates.
John McCallba135432009-11-21 08:51:07 +0000401const UnresolvedSet *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000402 // If root class, all conversions are visible.
403 if (bases_begin() == bases_end())
404 return &Conversions;
405 // If visible conversion list is already evaluated, return it.
406 if (ComputedVisibleConversions)
407 return &VisibleConversions;
Fariborz Jahanianf4e462c2009-10-12 18:36:50 +0000408 llvm::SmallPtrSet<CanQualType, 8> TopConversionsTypeSet;
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000409 collectConversionFunctions(TopConversionsTypeSet);
410 getNestedVisibleConversionFunctions(this, TopConversionsTypeSet,
411 TopConversionsTypeSet);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000412 ComputedVisibleConversions = true;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000413 return &VisibleConversions;
414}
415
Fariborz Jahanian62509212009-09-12 18:26:03 +0000416void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000417 CXXConversionDecl *ConvDecl) {
418 assert(!ConvDecl->getDescribedFunctionTemplate() &&
419 "Conversion function templates should cast to FunctionTemplateDecl.");
John McCallba135432009-11-21 08:51:07 +0000420 VisibleConversions.addDecl(ConvDecl);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000421}
422
Fariborz Jahanian62509212009-09-12 18:26:03 +0000423void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000424 FunctionTemplateDecl *ConvDecl) {
425 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
426 "Function template is not a conversion function template");
John McCallba135432009-11-21 08:51:07 +0000427 VisibleConversions.addDecl(ConvDecl);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000428}
429
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000430void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000431 assert(!ConvDecl->getDescribedFunctionTemplate() &&
432 "Conversion function templates should cast to FunctionTemplateDecl.");
John McCallba135432009-11-21 08:51:07 +0000433 Conversions.addDecl(ConvDecl);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000434}
435
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000436void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000437 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
438 "Function template is not a conversion function template");
John McCallba135432009-11-21 08:51:07 +0000439 Conversions.addDecl(ConvDecl);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000440}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000441
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000442
443void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
444 Method->setVirtualAsWritten(true);
445 setAggregate(false);
446 setPOD(false);
447 setEmpty(false);
448 setPolymorphic(true);
449 setHasTrivialConstructor(false);
450 setHasTrivialCopyConstructor(false);
451 setHasTrivialCopyAssignment(false);
452}
453
Douglas Gregorf6b11852009-10-08 15:14:33 +0000454CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000455 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000456 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
457
458 return 0;
459}
460
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000461MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
462 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
463}
464
Douglas Gregorf6b11852009-10-08 15:14:33 +0000465void
466CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
467 TemplateSpecializationKind TSK) {
468 assert(TemplateOrInstantiation.isNull() &&
469 "Previous template or instantiation?");
470 assert(!isa<ClassTemplateSpecializationDecl>(this));
471 TemplateOrInstantiation
472 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
473}
474
475TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() {
476 if (ClassTemplateSpecializationDecl *Spec
477 = dyn_cast<ClassTemplateSpecializationDecl>(this))
478 return Spec->getSpecializationKind();
479
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000480 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000481 return MSInfo->getTemplateSpecializationKind();
482
483 return TSK_Undeclared;
484}
485
486void
487CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
488 if (ClassTemplateSpecializationDecl *Spec
489 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
490 Spec->setSpecializationKind(TSK);
491 return;
492 }
493
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000494 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000495 MSInfo->setTemplateSpecializationKind(TSK);
496 return;
497 }
498
499 assert(false && "Not a class template or member class specialization");
500}
501
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000502CXXConstructorDecl *
503CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
504 QualType ClassType = Context.getTypeDeclType(this);
505 DeclarationName ConstructorName
506 = Context.DeclarationNames.getCXXConstructorName(
507 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000509 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000510 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000511 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000512 // FIXME: In C++0x, a constructor template can be a default constructor.
513 if (isa<FunctionTemplateDecl>(*Con))
514 continue;
515
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000516 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
517 if (Constructor->isDefaultConstructor())
518 return Constructor;
519 }
520 return 0;
521}
522
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000523CXXDestructorDecl *CXXRecordDecl::getDestructor(ASTContext &Context) {
Anders Carlsson7267c162009-05-29 21:03:38 +0000524 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000525
526 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000527 = Context.DeclarationNames.getCXXDestructorName(
528 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000529
530 DeclContext::lookup_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000531 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000532 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000534 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000535 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Anders Carlsson7267c162009-05-29 21:03:38 +0000537 return Dtor;
538}
539
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000540CXXMethodDecl *
541CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000542 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000543 QualType T, DeclaratorInfo *DInfo,
544 bool isStatic, bool isInline) {
545 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, DInfo,
546 isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000547}
548
Douglas Gregor90916562009-09-29 18:16:17 +0000549bool CXXMethodDecl::isUsualDeallocationFunction() const {
550 if (getOverloadedOperator() != OO_Delete &&
551 getOverloadedOperator() != OO_Array_Delete)
552 return false;
553
554 // C++ [basic.stc.dynamic.deallocation]p2:
555 // If a class T has a member deallocation function named operator delete
556 // with exactly one parameter, then that function is a usual (non-placement)
557 // deallocation function. [...]
558 if (getNumParams() == 1)
559 return true;
560
561 // C++ [basic.stc.dynamic.deallocation]p2:
562 // [...] If class T does not declare such an operator delete but does
563 // declare a member deallocation function named operator delete with
564 // exactly two parameters, the second of which has type std::size_t (18.1),
565 // then this function is a usual deallocation function.
566 ASTContext &Context = getASTContext();
567 if (getNumParams() != 2 ||
568 !Context.hasSameType(getParamDecl(1)->getType(), Context.getSizeType()))
569 return false;
570
571 // This function is a usual deallocation function if there are no
572 // single-parameter deallocation functions of the same kind.
573 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
574 R.first != R.second; ++R.first) {
575 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
576 if (FD->getNumParams() == 1)
577 return false;
578 }
579
580 return true;
581}
582
Mike Stump1eb44332009-09-09 15:08:12 +0000583typedef llvm::DenseMap<const CXXMethodDecl*,
584 std::vector<const CXXMethodDecl *> *>
Anders Carlsson05eb2442009-05-16 23:58:37 +0000585 OverriddenMethodsMapTy;
586
Mike Stumpb9871a22009-08-21 01:45:00 +0000587// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
588// the vtable building code uses it at CG time.
Anders Carlsson05eb2442009-05-16 23:58:37 +0000589static OverriddenMethodsMapTy *OverriddenMethods = 0;
590
591void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000592 assert(MD->isCanonicalDecl() && "Method is not canonical!");
593
Anders Carlsson05eb2442009-05-16 23:58:37 +0000594 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Anders Carlsson05eb2442009-05-16 23:58:37 +0000596 if (!OverriddenMethods)
597 OverriddenMethods = new OverriddenMethodsMapTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Anders Carlsson05eb2442009-05-16 23:58:37 +0000599 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
600 if (!Methods)
601 Methods = new std::vector<const CXXMethodDecl *>;
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Anders Carlsson05eb2442009-05-16 23:58:37 +0000603 Methods->push_back(MD);
604}
605
606CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
607 if (!OverriddenMethods)
608 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Anders Carlsson05eb2442009-05-16 23:58:37 +0000610 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000611 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000612 return 0;
Daniel Dunbar0908c332009-08-01 23:40:20 +0000613
Anders Carlsson05eb2442009-05-16 23:58:37 +0000614 return &(*it->second)[0];
615}
616
617CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
618 if (!OverriddenMethods)
619 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Anders Carlsson05eb2442009-05-16 23:58:37 +0000621 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000622 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000623 return 0;
624
Daniel Dunbar637ec322009-08-02 01:48:29 +0000625 return &(*it->second)[0] + it->second->size();
Anders Carlsson05eb2442009-05-16 23:58:37 +0000626}
627
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000628QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000629 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
630 // If the member function is declared const, the type of this is const X*,
631 // if the member function is declared volatile, the type of this is
632 // volatile X*, and if the member function is declared const volatile,
633 // the type of this is const volatile X*.
634
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000635 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000636
637 QualType ClassTy;
638 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
639 ClassTy = TD->getInjectedClassNameType(C);
640 else
Mike Stumpe607ed02009-08-07 18:05:12 +0000641 ClassTy = C.getTagDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000642 ClassTy = C.getQualifiedType(ClassTy,
643 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000644 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000645}
646
Douglas Gregor7ad83902008-11-05 04:29:56 +0000647CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000648CXXBaseOrMemberInitializer(ASTContext &Context,
649 DeclaratorInfo *DInfo, CXXConstructorDecl *C,
650 SourceLocation L,
651 Expr **Args, unsigned NumArgs,
652 SourceLocation R)
653 : BaseOrMember(DInfo), Args(0), NumArgs(0), CtorOrAnonUnion(C),
654 LParenLoc(L), RParenLoc(R)
655{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000656 if (NumArgs > 0) {
657 this->NumArgs = NumArgs;
Douglas Gregor802ab452009-12-02 22:36:29 +0000658 this->Args = new (Context) Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000659 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
660 this->Args[Idx] = Args[Idx];
661 }
662}
663
664CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000665CXXBaseOrMemberInitializer(ASTContext &Context,
666 FieldDecl *Member, SourceLocation MemberLoc,
667 CXXConstructorDecl *C, SourceLocation L,
668 Expr **Args, unsigned NumArgs,
669 SourceLocation R)
670 : BaseOrMember(Member), MemberLocation(MemberLoc), Args(0), NumArgs(0),
671 CtorOrAnonUnion(C), LParenLoc(L), RParenLoc(R)
672{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000673 if (NumArgs > 0) {
674 this->NumArgs = NumArgs;
Douglas Gregor802ab452009-12-02 22:36:29 +0000675 this->Args = new (Context) Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000676 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
677 this->Args[Idx] = Args[Idx];
678 }
679}
680
Douglas Gregor802ab452009-12-02 22:36:29 +0000681void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) {
682 for (unsigned I = 0; I != NumArgs; ++I)
683 Args[I]->Destroy(Context);
684 Context.Deallocate(Args);
685 this->~CXXBaseOrMemberInitializer();
686}
687
688TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
689 if (isBaseInitializer())
690 return BaseOrMember.get<DeclaratorInfo*>()->getTypeLoc();
691 else
692 return TypeLoc();
693}
694
695Type *CXXBaseOrMemberInitializer::getBaseClass() {
696 if (isBaseInitializer())
697 return BaseOrMember.get<DeclaratorInfo*>()->getType().getTypePtr();
698 else
699 return 0;
700}
701
702const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
703 if (isBaseInitializer())
704 return BaseOrMember.get<DeclaratorInfo*>()->getType().getTypePtr();
705 else
706 return 0;
707}
708
709SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
710 if (isMemberInitializer())
711 return getMemberLocation();
712
713 return getBaseClassLoc().getSourceRange().getBegin();
714}
715
716SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
717 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000718}
719
Douglas Gregorb48fe382008-10-31 09:07:45 +0000720CXXConstructorDecl *
721CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000722 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000723 QualType T, DeclaratorInfo *DInfo,
724 bool isExplicit,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000725 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000726 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
727 "Name must refer to a constructor");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000728 return new (C) CXXConstructorDecl(RD, L, N, T, DInfo, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000729 isImplicitlyDeclared);
730}
731
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000732bool CXXConstructorDecl::isDefaultConstructor() const {
733 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000734 // A default constructor for a class X is a constructor of class
735 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000736 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000737 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000738}
739
Mike Stump1eb44332009-09-09 15:08:12 +0000740bool
741CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000742 unsigned &TypeQuals) const {
743 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000744 // A non-template constructor for class X is a copy constructor
745 // if its first parameter is of type X&, const X&, volatile X& or
746 // const volatile X&, and either there are no other parameters
747 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000748 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000749 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000750 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000751 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000752 return false;
753
754 const ParmVarDecl *Param = getParamDecl(0);
755
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000756 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000757 const LValueReferenceType *ParamRefType =
758 Param->getType()->getAs<LValueReferenceType>();
759 if (!ParamRefType)
760 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000761
Douglas Gregorfd476482009-11-13 23:59:09 +0000762 // Is it a reference to our class type?
763 CanQualType PointeeType
764 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000765 CanQualType ClassTy
766 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000767 if (PointeeType.getUnqualifiedType() != ClassTy)
768 return false;
769
John McCall0953e762009-09-24 19:53:00 +0000770 // FIXME: other qualifiers?
771
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000772 // We have a copy constructor.
773 TypeQuals = PointeeType.getCVRQualifiers();
774 return true;
775}
776
Anders Carlssonfaccd722009-08-28 16:57:08 +0000777bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000778 // C++ [class.conv.ctor]p1:
779 // A constructor declared without the function-specifier explicit
780 // that can be called with a single parameter specifies a
781 // conversion from the type of its first parameter to the type of
782 // its class. Such a constructor is called a converting
783 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000784 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000785 return false;
786
Mike Stump1eb44332009-09-09 15:08:12 +0000787 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000788 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000789 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000790 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000791}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000792
Douglas Gregor66724ea2009-11-14 01:20:54 +0000793bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
794 if ((getNumParams() < 1) ||
795 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
796 (getPrimaryTemplate() == 0) ||
797 (getDescribedFunctionTemplate() != 0))
798 return false;
799
800 const ParmVarDecl *Param = getParamDecl(0);
801
802 ASTContext &Context = getASTContext();
803 CanQualType ParamType = Context.getCanonicalType(Param->getType());
804
805 // Strip off the lvalue reference, if any.
806 if (CanQual<LValueReferenceType> ParamRefType
807 = ParamType->getAs<LValueReferenceType>())
808 ParamType = ParamRefType->getPointeeType();
809
810
811 // Is it the same as our our class type?
812 CanQualType ClassTy
813 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
814 if (ParamType.getUnqualifiedType() != ClassTy)
815 return false;
816
817 return true;
818}
819
Douglas Gregor42a552f2008-11-05 20:51:48 +0000820CXXDestructorDecl *
821CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000822 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000823 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000824 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000825 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
826 "Name must refer to a destructor");
Mike Stump1eb44332009-09-09 15:08:12 +0000827 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff3e970492009-01-27 21:25:57 +0000828 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000829}
830
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000831void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000832CXXConstructorDecl::Destroy(ASTContext& C) {
833 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000834 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000835}
836
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000837CXXConversionDecl *
838CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000839 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000840 QualType T, DeclaratorInfo *DInfo,
841 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000842 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
843 "Name must refer to a conversion function");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000844 return new (C) CXXConversionDecl(RD, L, N, T, DInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000845}
846
John McCall02cace72009-08-28 07:59:38 +0000847FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
848 SourceLocation L,
849 FriendUnion Friend,
850 SourceLocation FriendL) {
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000851#ifndef NDEBUG
John McCall02cace72009-08-28 07:59:38 +0000852 if (Friend.is<NamedDecl*>()) {
853 NamedDecl *D = Friend.get<NamedDecl*>();
854 assert(isa<FunctionDecl>(D) ||
855 isa<CXXRecordDecl>(D) ||
856 isa<FunctionTemplateDecl>(D) ||
857 isa<ClassTemplateDecl>(D));
858 assert(D->getFriendObjectKind());
859 }
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000860#endif
John McCallc48fbdf2009-08-11 21:13:21 +0000861
John McCall02cace72009-08-28 07:59:38 +0000862 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump1eb44332009-09-09 15:08:12 +0000863}
John McCall3f9a8a62009-08-11 06:59:38 +0000864
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000865LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000866 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000867 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000868 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000869 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000870}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000871
872UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
873 SourceLocation L,
874 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000875 SourceRange QualifierRange,
876 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000877 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000878 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000879 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000880 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
881 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000882 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000883 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000884}
885
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000886NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
887 if (NamespaceAliasDecl *NA =
888 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
889 return NA->getNamespace();
890 return cast_or_null<NamespaceDecl>(NominatedNamespace);
891}
892
Mike Stump1eb44332009-09-09 15:08:12 +0000893NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
894 SourceLocation L,
895 SourceLocation AliasLoc,
896 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000897 SourceRange QualifierRange,
898 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +0000899 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +0000900 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +0000901 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
902 Namespace = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +0000903 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000904 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000905}
906
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000907UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
John McCall9488ea12009-11-17 05:59:44 +0000908 SourceLocation L, SourceRange NNR, SourceLocation UL,
909 NestedNameSpecifier* TargetNNS, DeclarationName Name,
910 bool IsTypeNameArg) {
911 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000912}
913
John McCall7ba107a2009-11-18 02:36:19 +0000914UnresolvedUsingValueDecl *
915UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
916 SourceLocation UsingLoc,
917 SourceRange TargetNNR,
918 NestedNameSpecifier *TargetNNS,
919 SourceLocation TargetNameLoc,
920 DeclarationName TargetName) {
921 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
922 TargetNNR, TargetNNS,
923 TargetNameLoc, TargetName);
924}
925
926UnresolvedUsingTypenameDecl *
927UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
928 SourceLocation UsingLoc,
929 SourceLocation TypenameLoc,
930 SourceRange TargetNNR,
931 NestedNameSpecifier *TargetNNS,
932 SourceLocation TargetNameLoc,
933 DeclarationName TargetName) {
934 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
935 TargetNNR, TargetNNS,
936 TargetNameLoc,
937 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +0000938}
939
Anders Carlssonfb311762009-03-14 00:25:26 +0000940StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
941 SourceLocation L, Expr *AssertExpr,
942 StringLiteral *Message) {
943 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
944}
945
946void StaticAssertDecl::Destroy(ASTContext& C) {
947 AssertExpr->Destroy(C);
948 Message->Destroy(C);
949 this->~StaticAssertDecl();
950 C.Deallocate((void *)this);
951}
952
953StaticAssertDecl::~StaticAssertDecl() {
954}
955
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000956static const char *getAccessName(AccessSpecifier AS) {
957 switch (AS) {
958 default:
959 case AS_none:
960 assert("Invalid access specifier!");
961 return 0;
962 case AS_public:
963 return "public";
964 case AS_private:
965 return "private";
966 case AS_protected:
967 return "protected";
968 }
969}
970
971const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
972 AccessSpecifier AS) {
973 return DB << getAccessName(AS);
974}
975
976