blob: b30d335918ab1e2bc5db94cd178f34bdc39544fd [file] [log] [blame]
Ted Kremenek21475702008-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 Gregor8ea8fd42009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek21475702008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Anders Carlsson5bbe1d72009-03-14 00:25:26 +000017#include "clang/AST/Expr.h"
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +000018#include "clang/AST/TypeLoc.h"
Douglas Gregorb6acda02008-11-12 23:21:09 +000019#include "clang/Basic/IdentifierTable.h"
Douglas Gregor74a34442008-12-23 21:31:30 +000020#include "llvm/ADT/STLExtras.h"
Fariborz Jahaniana9540492009-09-12 19:52:10 +000021#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek21475702008-09-05 17:16:31 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Decl Allocation/Deallocation Method Implementations
26//===----------------------------------------------------------------------===//
Douglas Gregor5101c242008-12-05 18:15:24 +000027
Douglas Gregor264ec4f2009-02-17 01:05:43 +000028CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Douglas Gregor82fe3e32009-07-21 14:46:17 +000029 SourceLocation L, IdentifierInfo *Id,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +000030 CXXRecordDecl *PrevDecl,
Mike Stump11289f42009-09-09 15:08:12 +000031 SourceLocation TKL)
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +000032 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
Douglas Gregorb6acda02008-11-12 23:21:09 +000033 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redlbaad4e72009-01-05 20:52:13 +000034 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedmanc80ca682009-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 Jahanianb394f502009-09-12 18:26:03 +000038 HasTrivialDestructor(true), ComputedVisibleConversions(false),
39 Bases(0), NumBases(0), VBases(0), NumVBases(0),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +000040 TemplateOrInstantiation() { }
Douglas Gregorb6acda02008-11-12 23:21:09 +000041
Ted Kremenek21475702008-09-05 17:16:31 +000042CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
43 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +000044 SourceLocation TKL,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000045 CXXRecordDecl* PrevDecl,
46 bool DelayTypeCreation) {
Mike Stump11289f42009-09-09 15:08:12 +000047 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +000048 PrevDecl, TKL);
Mike Stump11289f42009-09-09 15:08:12 +000049
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +000050 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000051 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +000052 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +000053 return R;
54}
55
Douglas Gregor29a92472008-10-22 17:49:05 +000056CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian9fa077c2009-07-02 18:26:15 +000057}
58
59void CXXRecordDecl::Destroy(ASTContext &C) {
60 C.Deallocate(Bases);
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +000061 C.Deallocate(VBases);
Fariborz Jahanian9fa077c2009-07-02 18:26:15 +000062 this->RecordDecl::Destroy(C);
Douglas Gregor29a92472008-10-22 17:49:05 +000063}
64
Mike Stump11289f42009-09-09 15:08:12 +000065void
Fariborz Jahanian9fa077c2009-07-02 18:26:15 +000066CXXRecordDecl::setBases(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +000067 CXXBaseSpecifier const * const *Bases,
Douglas Gregor9d6290b2008-10-23 18:13:27 +000068 unsigned NumBases) {
Mike Stump11289f42009-09-09 15:08:12 +000069 // C++ [dcl.init.aggr]p1:
Douglas Gregorcfd8ddc2008-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 Gregor9d6290b2008-10-23 18:13:27 +000074 if (this->Bases)
Fariborz Jahanian9fa077c2009-07-02 18:26:15 +000075 C.Deallocate(this->Bases);
Mike Stump11289f42009-09-09 15:08:12 +000076
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +000077 int vbaseCount = 0;
78 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
79 bool hasDirectVirtualBase = false;
Mike Stump11289f42009-09-09 15:08:12 +000080
Fariborz Jahanian9fa077c2009-07-02 18:26:15 +000081 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor9d6290b2008-10-23 18:13:27 +000082 this->NumBases = NumBases;
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +000083 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor9d6290b2008-10-23 18:13:27 +000084 this->Bases[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-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 Stump11289f42009-09-09 15:08:12 +000088 // Skip template types.
Fariborz Jahanian3554b5a2009-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 Kremenekc23c7e62009-07-29 21:53:49 +000094 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +000095 if (Base->isVirtual())
96 hasDirectVirtualBase = true;
Mike Stump11289f42009-09-09 15:08:12 +000097 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +000098 BaseClassDecl->vbases_begin(),
99 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Mike Stump11289f42009-09-09 15:08:12 +0000100 // Add this vbase to the array of vbases for current class if it is
Fariborz Jahanian3554b5a2009-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 Meredithf6eb60a2009-07-11 14:32:10 +0000121 int j;
122 for (j = 0; j < vbaseCount; ++j)
123 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000124 break;
Alisdair Meredithf6eb60a2009-07-11 14:32:10 +0000125 if (j == vbaseCount) {
Fariborz Jahanian3554b5a2009-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 Gregor8a273912009-07-22 18:25:24 +0000133 this->VBases = new (C) CXXBaseSpecifier [vbaseCount];
Fariborz Jahanian3554b5a2009-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 Kremenekc23c7e62009-07-29 21:53:49 +0000138 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000139 this->VBases[i] =
Douglas Gregorb77af8f2009-07-22 20:55:49 +0000140 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
141 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
142 UniqueVbases[i]->getAccessSpecifier(), QT);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000143 }
144 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000145}
146
Douglas Gregor05379422008-11-03 17:51:48 +0000147bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall8ccfcb52009-09-24 19:53:00 +0000148 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000149}
150
Mike Stump11289f42009-09-09 15:08:12 +0000151CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000152 unsigned TypeQuals) const{
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000153 QualType ClassType
154 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump11289f42009-09-09 15:08:12 +0000155 DeclarationName ConstructorName
Douglas Gregor1349b452008-12-15 21:24:18 +0000156 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000157 Context.getCanonicalType(ClassType));
158 unsigned FoundTQs;
Douglas Gregor74a34442008-12-23 21:31:30 +0000159 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000160 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregor74a34442008-12-23 21:31:30 +0000161 Con != ConEnd; ++Con) {
Douglas Gregor2d2282e2009-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
Douglas Gregor507eb872009-12-22 00:34:07 +0000167 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(FoundTQs)) {
John McCall8ccfcb52009-09-24 19:53:00 +0000168 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
169 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000170 return cast<CXXConstructorDecl>(*Con);
Mike Stump11289f42009-09-09 15:08:12 +0000171
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000172 }
Douglas Gregor05379422008-11-03 17:51:48 +0000173 }
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000174 return 0;
Douglas Gregor05379422008-11-03 17:51:48 +0000175}
176
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +0000177bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
178 const CXXMethodDecl *& MD) const {
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000179 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
180 const_cast<CXXRecordDecl*>(this)));
181 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
182
183 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000184 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000185 Op != OpEnd; ++Op) {
186 // C++ [class.copy]p9:
187 // A user-declared copy assignment operator is a non-static non-template
188 // member function of class X with exactly one parameter of type X, X&,
189 // const X&, volatile X& or const volatile X&.
Douglas Gregor62b885d2009-10-30 22:48:49 +0000190 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
191 if (!Method)
192 continue;
193
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000194 if (Method->isStatic())
195 continue;
Douglas Gregora14b43b2009-10-13 23:45:19 +0000196 if (Method->getPrimaryTemplate())
197 continue;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000198 const FunctionProtoType *FnType =
John McCall9dd450b2009-09-21 23:43:11 +0000199 Method->getType()->getAs<FunctionProtoType>();
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000200 assert(FnType && "Overloaded operator has no prototype.");
201 // Don't assert on this; an invalid decl might have been left in the AST.
202 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
203 continue;
204 bool AcceptsConst = true;
205 QualType ArgType = FnType->getArgType(0);
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000206 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000207 ArgType = Ref->getPointeeType();
Douglas Gregor63b4ff62009-03-20 20:21:37 +0000208 // Is it a non-const lvalue reference?
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000209 if (!ArgType.isConstQualified())
210 AcceptsConst = false;
211 }
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000212 if (!Context.hasSameUnqualifiedType(ArgType, ClassType))
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000213 continue;
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +0000214 MD = Method;
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000215 // We have a single argument of type cv X or cv X&, i.e. we've found the
216 // copy assignment operator. Return whether it accepts const arguments.
217 return AcceptsConst;
218 }
219 assert(isInvalidDecl() &&
220 "No copy assignment operator declared in valid code.");
221 return false;
222}
223
224void
Mike Stump11289f42009-09-09 15:08:12 +0000225CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor1349b452008-12-15 21:24:18 +0000226 CXXConstructorDecl *ConDecl) {
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000227 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
228 // Note that we have a user-declared constructor.
229 UserDeclaredConstructor = true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000230
Mike Stump11289f42009-09-09 15:08:12 +0000231 // C++ [dcl.init.aggr]p1:
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000232 // An aggregate is an array or a class (clause 9) with no
233 // user-declared constructors (12.1) [...].
234 Aggregate = false;
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000235
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000236 // C++ [class]p4:
237 // A POD-struct is an aggregate class [...]
238 PlainOldData = false;
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000239
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000240 // C++ [class.ctor]p5:
241 // A constructor is trivial if it is an implicitly-declared default
242 // constructor.
Douglas Gregor8a273912009-07-22 18:25:24 +0000243 // FIXME: C++0x: don't do this for "= default" default constructors.
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000244 HasTrivialConstructor = false;
Mike Stump11289f42009-09-09 15:08:12 +0000245
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000246 // Note when we have a user-declared copy constructor, which will
247 // suppress the implicit declaration of a copy constructor.
Douglas Gregor507eb872009-12-22 00:34:07 +0000248 if (ConDecl->isCopyConstructor()) {
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000249 UserDeclaredCopyConstructor = true;
Douglas Gregor8a273912009-07-22 18:25:24 +0000250
251 // C++ [class.copy]p6:
252 // A copy constructor is trivial if it is implicitly declared.
253 // FIXME: C++0x: don't do this for "= default" copy constructors.
254 HasTrivialCopyConstructor = false;
255 }
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000256}
257
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000258void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
259 CXXMethodDecl *OpDecl) {
260 // We're interested specifically in copy assignment operators.
John McCall9dd450b2009-09-21 23:43:11 +0000261 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000262 assert(FnType && "Overloaded operator has no proto function type.");
263 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregora14b43b2009-10-13 23:45:19 +0000264
265 // Copy assignment operators must be non-templates.
266 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
267 return;
268
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000269 QualType ArgType = FnType->getArgType(0);
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000270 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000271 ArgType = Ref->getPointeeType();
272
273 ArgType = ArgType.getUnqualifiedType();
274 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
275 const_cast<CXXRecordDecl*>(this)));
276
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000277 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000278 return;
279
280 // This is a copy assignment operator.
Eli Friedman01cad4c2009-11-07 00:02:45 +0000281 // Note on the decl that it is a copy assignment operator.
282 OpDecl->setCopyAssignment(true);
283
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000284 // Suppress the implicit declaration of a copy constructor.
285 UserDeclaredCopyAssignment = true;
286
Douglas Gregor8a273912009-07-22 18:25:24 +0000287 // C++ [class.copy]p11:
288 // A copy assignment operator is trivial if it is implicitly declared.
289 // FIXME: C++0x: don't do this for "= default" copy operators.
290 HasTrivialCopyAssignment = false;
291
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000292 // C++ [class]p4:
293 // A POD-struct is an aggregate class that [...] has no user-defined copy
294 // assignment operator [...].
295 PlainOldData = false;
296}
297
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +0000298void
299CXXRecordDecl::collectConversionFunctions(
John McCalld14a8642009-11-21 08:51:07 +0000300 llvm::SmallPtrSet<CanQualType, 8>& ConversionsTypeSet) const
Fariborz Jahanian65694b42009-10-12 18:36:50 +0000301{
John McCalld14a8642009-11-21 08:51:07 +0000302 const UnresolvedSet *Cs = getConversionFunctions();
303 for (UnresolvedSet::iterator I = Cs->begin(), E = Cs->end(); I != E; ++I) {
304 NamedDecl *TopConv = *I;
Fariborz Jahanian65694b42009-10-12 18:36:50 +0000305 CanQualType TConvType;
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +0000306 if (FunctionTemplateDecl *TConversionTemplate =
307 dyn_cast<FunctionTemplateDecl>(TopConv))
308 TConvType =
309 getASTContext().getCanonicalType(
310 TConversionTemplate->getTemplatedDecl()->getResultType());
311 else
312 TConvType =
313 getASTContext().getCanonicalType(
314 cast<CXXConversionDecl>(TopConv)->getConversionType());
315 ConversionsTypeSet.insert(TConvType);
316 }
317}
318
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000319/// getNestedVisibleConversionFunctions - imports unique conversion
320/// functions from base classes into the visible conversion function
321/// list of the class 'RD'. This is a private helper method.
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +0000322/// TopConversionsTypeSet is the set of conversion functions of the class
323/// we are interested in. HiddenConversionTypes is set of conversion functions
324/// of the immediate derived class which hides the conversion functions found
325/// in current class.
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000326void
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +0000327CXXRecordDecl::getNestedVisibleConversionFunctions(CXXRecordDecl *RD,
Fariborz Jahanian65694b42009-10-12 18:36:50 +0000328 const llvm::SmallPtrSet<CanQualType, 8> &TopConversionsTypeSet,
329 const llvm::SmallPtrSet<CanQualType, 8> &HiddenConversionTypes)
330{
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +0000331 bool inTopClass = (RD == this);
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000332 QualType ClassType = getASTContext().getTypeDeclType(this);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000333 if (const RecordType *Record = ClassType->getAs<RecordType>()) {
John McCalld14a8642009-11-21 08:51:07 +0000334 const UnresolvedSet *Cs
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000335 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
Fariborz Jahaniana9540492009-09-12 19:52:10 +0000336
John McCalld14a8642009-11-21 08:51:07 +0000337 for (UnresolvedSet::iterator I = Cs->begin(), E = Cs->end(); I != E; ++I) {
338 NamedDecl *Conv = *I;
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000339 // Only those conversions not exact match of conversions in current
340 // class are candidateconversion routines.
Fariborz Jahanian65694b42009-10-12 18:36:50 +0000341 CanQualType ConvType;
Fariborz Jahaniana9540492009-09-12 19:52:10 +0000342 if (FunctionTemplateDecl *ConversionTemplate =
343 dyn_cast<FunctionTemplateDecl>(Conv))
344 ConvType =
345 getASTContext().getCanonicalType(
Fariborz Jahaniana9c40412009-09-15 23:02:16 +0000346 ConversionTemplate->getTemplatedDecl()->getResultType());
Fariborz Jahaniana9540492009-09-12 19:52:10 +0000347 else
348 ConvType =
Fariborz Jahanianadcea102009-09-15 22:15:23 +0000349 getASTContext().getCanonicalType(
350 cast<CXXConversionDecl>(Conv)->getConversionType());
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +0000351 // We only add conversion functions found in the base class if they
352 // are not hidden by those found in HiddenConversionTypes which are
353 // the conversion functions in its derived class.
354 if (inTopClass ||
355 (!TopConversionsTypeSet.count(ConvType) &&
356 !HiddenConversionTypes.count(ConvType)) ) {
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000357 if (FunctionTemplateDecl *ConversionTemplate =
358 dyn_cast<FunctionTemplateDecl>(Conv))
359 RD->addVisibleConversionFunction(ConversionTemplate);
360 else
361 RD->addVisibleConversionFunction(cast<CXXConversionDecl>(Conv));
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000362 }
363 }
364 }
Sebastian Redl1054fae2009-10-25 17:03:50 +0000365
Fariborz Jahanian4ff5f052009-10-08 16:33:37 +0000366 if (getNumBases() == 0 && getNumVBases() == 0)
367 return;
Sebastian Redl1054fae2009-10-25 17:03:50 +0000368
Fariborz Jahanian65694b42009-10-12 18:36:50 +0000369 llvm::SmallPtrSet<CanQualType, 8> ConversionFunctions;
Fariborz Jahanian4ff5f052009-10-08 16:33:37 +0000370 if (!inTopClass)
371 collectConversionFunctions(ConversionFunctions);
Sebastian Redl1054fae2009-10-25 17:03:50 +0000372
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000373 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(),
374 E = vbases_end(); VBase != E; ++VBase) {
Sebastian Redl1054fae2009-10-25 17:03:50 +0000375 if (const RecordType *RT = VBase->getType()->getAs<RecordType>()) {
376 CXXRecordDecl *VBaseClassDecl
377 = cast<CXXRecordDecl>(RT->getDecl());
378 VBaseClassDecl->getNestedVisibleConversionFunctions(RD,
379 TopConversionsTypeSet,
380 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
381 }
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000382 }
383 for (CXXRecordDecl::base_class_iterator Base = bases_begin(),
384 E = bases_end(); Base != E; ++Base) {
385 if (Base->isVirtual())
386 continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +0000387 if (const RecordType *RT = Base->getType()->getAs<RecordType>()) {
388 CXXRecordDecl *BaseClassDecl
389 = cast<CXXRecordDecl>(RT->getDecl());
390
391 BaseClassDecl->getNestedVisibleConversionFunctions(RD,
392 TopConversionsTypeSet,
393 (inTopClass ? TopConversionsTypeSet : ConversionFunctions));
394 }
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000395 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000396}
397
398/// getVisibleConversionFunctions - get all conversion functions visible
399/// in current class; including conversion function templates.
John McCalld14a8642009-11-21 08:51:07 +0000400const UnresolvedSet *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000401 // If root class, all conversions are visible.
402 if (bases_begin() == bases_end())
403 return &Conversions;
404 // If visible conversion list is already evaluated, return it.
405 if (ComputedVisibleConversions)
406 return &VisibleConversions;
Fariborz Jahanian65694b42009-10-12 18:36:50 +0000407 llvm::SmallPtrSet<CanQualType, 8> TopConversionsTypeSet;
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +0000408 collectConversionFunctions(TopConversionsTypeSet);
409 getNestedVisibleConversionFunctions(this, TopConversionsTypeSet,
410 TopConversionsTypeSet);
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000411 ComputedVisibleConversions = true;
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000412 return &VisibleConversions;
413}
414
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000415void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000416 CXXConversionDecl *ConvDecl) {
417 assert(!ConvDecl->getDescribedFunctionTemplate() &&
418 "Conversion function templates should cast to FunctionTemplateDecl.");
John McCalld14a8642009-11-21 08:51:07 +0000419 VisibleConversions.addDecl(ConvDecl);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000420}
421
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000422void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000423 FunctionTemplateDecl *ConvDecl) {
424 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
425 "Function template is not a conversion function template");
John McCalld14a8642009-11-21 08:51:07 +0000426 VisibleConversions.addDecl(ConvDecl);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000427}
428
Fariborz Jahanianedca0bc2009-09-12 19:02:34 +0000429void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) {
Douglas Gregor05155d82009-08-21 23:19:43 +0000430 assert(!ConvDecl->getDescribedFunctionTemplate() &&
431 "Conversion function templates should cast to FunctionTemplateDecl.");
John McCalld14a8642009-11-21 08:51:07 +0000432 Conversions.addDecl(ConvDecl);
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000433}
434
Fariborz Jahanianedca0bc2009-09-12 19:02:34 +0000435void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) {
Douglas Gregor05155d82009-08-21 23:19:43 +0000436 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
437 "Function template is not a conversion function template");
John McCalld14a8642009-11-21 08:51:07 +0000438 Conversions.addDecl(ConvDecl);
Douglas Gregor05155d82009-08-21 23:19:43 +0000439}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000440
Fariborz Jahanian6dfc1972009-12-03 18:44:40 +0000441
442void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
443 Method->setVirtualAsWritten(true);
444 setAggregate(false);
445 setPOD(false);
446 setEmpty(false);
447 setPolymorphic(true);
448 setHasTrivialConstructor(false);
449 setHasTrivialCopyConstructor(false);
450 setHasTrivialCopyAssignment(false);
451}
452
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000453CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000454 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000455 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
456
457 return 0;
458}
459
Douglas Gregor06db9f52009-10-12 20:18:28 +0000460MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
461 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
462}
463
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000464void
465CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
466 TemplateSpecializationKind TSK) {
467 assert(TemplateOrInstantiation.isNull() &&
468 "Previous template or instantiation?");
469 assert(!isa<ClassTemplateSpecializationDecl>(this));
470 TemplateOrInstantiation
471 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
472}
473
Anders Carlsson27cfc6e2009-12-07 06:33:48 +0000474TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
475 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000476 = dyn_cast<ClassTemplateSpecializationDecl>(this))
477 return Spec->getSpecializationKind();
478
Douglas Gregor06db9f52009-10-12 20:18:28 +0000479 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000480 return MSInfo->getTemplateSpecializationKind();
481
482 return TSK_Undeclared;
483}
484
485void
486CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
487 if (ClassTemplateSpecializationDecl *Spec
488 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
489 Spec->setSpecializationKind(TSK);
490 return;
491 }
492
Douglas Gregor06db9f52009-10-12 20:18:28 +0000493 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000494 MSInfo->setTemplateSpecializationKind(TSK);
495 return;
496 }
497
498 assert(false && "Not a class template or member class specialization");
499}
500
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000501CXXConstructorDecl *
502CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
503 QualType ClassType = Context.getTypeDeclType(this);
504 DeclarationName ConstructorName
505 = Context.DeclarationNames.getCXXConstructorName(
506 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump11289f42009-09-09 15:08:12 +0000507
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000508 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000509 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000510 Con != ConEnd; ++Con) {
Douglas Gregor2d2282e2009-09-04 14:46:39 +0000511 // FIXME: In C++0x, a constructor template can be a default constructor.
512 if (isa<FunctionTemplateDecl>(*Con))
513 continue;
514
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000515 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
516 if (Constructor->isDefaultConstructor())
517 return Constructor;
518 }
519 return 0;
520}
521
Anders Carlssonf98849e2009-12-02 17:15:43 +0000522CXXDestructorDecl *CXXRecordDecl::getDestructor(ASTContext &Context) {
Anders Carlsson0a637412009-05-29 21:03:38 +0000523 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +0000524
525 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +0000526 = Context.DeclarationNames.getCXXDestructorName(
527 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +0000528
529 DeclContext::lookup_iterator I, E;
Mike Stump11289f42009-09-09 15:08:12 +0000530 llvm::tie(I, E) = lookup(Name);
Anders Carlsson0a637412009-05-29 21:03:38 +0000531 assert(I != E && "Did not find a destructor!");
Mike Stump11289f42009-09-09 15:08:12 +0000532
Anders Carlssonf98849e2009-12-02 17:15:43 +0000533 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson0a637412009-05-29 21:03:38 +0000534 assert(++I == E && "Found more than one destructor!");
Mike Stump11289f42009-09-09 15:08:12 +0000535
Anders Carlsson0a637412009-05-29 21:03:38 +0000536 return Dtor;
537}
538
Ted Kremenek21475702008-09-05 17:16:31 +0000539CXXMethodDecl *
540CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor92751d42008-11-17 22:58:34 +0000541 SourceLocation L, DeclarationName N,
John McCallbcd03502009-12-07 02:54:59 +0000542 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000543 bool isStatic, bool isInline) {
John McCallbcd03502009-12-07 02:54:59 +0000544 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000545 isStatic, isInline);
Ted Kremenek21475702008-09-05 17:16:31 +0000546}
547
Douglas Gregorbb3e12f2009-09-29 18:16:17 +0000548bool CXXMethodDecl::isUsualDeallocationFunction() const {
549 if (getOverloadedOperator() != OO_Delete &&
550 getOverloadedOperator() != OO_Array_Delete)
551 return false;
552
553 // C++ [basic.stc.dynamic.deallocation]p2:
554 // If a class T has a member deallocation function named operator delete
555 // with exactly one parameter, then that function is a usual (non-placement)
556 // deallocation function. [...]
557 if (getNumParams() == 1)
558 return true;
559
560 // C++ [basic.stc.dynamic.deallocation]p2:
561 // [...] If class T does not declare such an operator delete but does
562 // declare a member deallocation function named operator delete with
563 // exactly two parameters, the second of which has type std::size_t (18.1),
564 // then this function is a usual deallocation function.
565 ASTContext &Context = getASTContext();
566 if (getNumParams() != 2 ||
567 !Context.hasSameType(getParamDecl(1)->getType(), Context.getSizeType()))
568 return false;
569
570 // This function is a usual deallocation function if there are no
571 // single-parameter deallocation functions of the same kind.
572 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
573 R.first != R.second; ++R.first) {
574 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
575 if (FD->getNumParams() == 1)
576 return false;
577 }
578
579 return true;
580}
581
Mike Stump11289f42009-09-09 15:08:12 +0000582typedef llvm::DenseMap<const CXXMethodDecl*,
583 std::vector<const CXXMethodDecl *> *>
Anders Carlsson36d87e12009-05-16 23:58:37 +0000584 OverriddenMethodsMapTy;
585
Mike Stumpa0029452009-08-21 01:45:00 +0000586// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
587// the vtable building code uses it at CG time.
Anders Carlsson36d87e12009-05-16 23:58:37 +0000588static OverriddenMethodsMapTy *OverriddenMethods = 0;
589
590void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +0000591 assert(MD->isCanonicalDecl() && "Method is not canonical!");
592
Anders Carlsson36d87e12009-05-16 23:58:37 +0000593 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
Mike Stump11289f42009-09-09 15:08:12 +0000594
Anders Carlsson36d87e12009-05-16 23:58:37 +0000595 if (!OverriddenMethods)
596 OverriddenMethods = new OverriddenMethodsMapTy();
Mike Stump11289f42009-09-09 15:08:12 +0000597
Anders Carlsson36d87e12009-05-16 23:58:37 +0000598 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
599 if (!Methods)
600 Methods = new std::vector<const CXXMethodDecl *>;
Mike Stump11289f42009-09-09 15:08:12 +0000601
Anders Carlsson36d87e12009-05-16 23:58:37 +0000602 Methods->push_back(MD);
603}
604
605CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
606 if (!OverriddenMethods)
607 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000608
Anders Carlsson36d87e12009-05-16 23:58:37 +0000609 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar36741c82009-08-01 23:40:20 +0000610 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson36d87e12009-05-16 23:58:37 +0000611 return 0;
Daniel Dunbar36741c82009-08-01 23:40:20 +0000612
Anders Carlsson36d87e12009-05-16 23:58:37 +0000613 return &(*it->second)[0];
614}
615
616CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
617 if (!OverriddenMethods)
618 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000619
Anders Carlsson36d87e12009-05-16 23:58:37 +0000620 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar36741c82009-08-01 23:40:20 +0000621 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson36d87e12009-05-16 23:58:37 +0000622 return 0;
623
Daniel Dunbara4a341b92009-08-02 01:48:29 +0000624 return &(*it->second)[0] + it->second->size();
Anders Carlsson36d87e12009-05-16 23:58:37 +0000625}
626
Ted Kremenek21475702008-09-05 17:16:31 +0000627QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +0000628 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
629 // If the member function is declared const, the type of this is const X*,
630 // if the member function is declared volatile, the type of this is
631 // volatile X*, and if the member function is declared const volatile,
632 // the type of this is const volatile X*.
633
Ted Kremenek21475702008-09-05 17:16:31 +0000634 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +0000635
636 QualType ClassTy;
637 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
638 ClassTy = TD->getInjectedClassNameType(C);
639 else
Mike Stumpb93185d2009-08-07 18:05:12 +0000640 ClassTy = C.getTagDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +0000641 ClassTy = C.getQualifiedType(ClassTy,
642 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +0000643 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +0000644}
645
Eli Friedman71a26d82009-12-06 20:50:05 +0000646bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +0000647 // If this function is a template instantiation, look at the template from
648 // which it was instantiated.
649 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
650 if (!CheckFn)
651 CheckFn = this;
652
Eli Friedman71a26d82009-12-06 20:50:05 +0000653 const FunctionDecl *fn;
Douglas Gregora318efd2010-01-05 19:06:31 +0000654 return CheckFn->getBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +0000655}
656
Douglas Gregore8381c02008-11-05 04:29:56 +0000657CXXBaseOrMemberInitializer::
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000658CXXBaseOrMemberInitializer(ASTContext &Context,
John McCallbcd03502009-12-07 02:54:59 +0000659 TypeSourceInfo *TInfo, CXXConstructorDecl *C,
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000660 SourceLocation L,
661 Expr **Args, unsigned NumArgs,
662 SourceLocation R)
John McCallbcd03502009-12-07 02:54:59 +0000663 : BaseOrMember(TInfo), Args(0), NumArgs(0), CtorOrAnonUnion(C),
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000664 LParenLoc(L), RParenLoc(R)
665{
Douglas Gregore8381c02008-11-05 04:29:56 +0000666 if (NumArgs > 0) {
667 this->NumArgs = NumArgs;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000668 this->Args = new (Context) Stmt*[NumArgs];
Douglas Gregore8381c02008-11-05 04:29:56 +0000669 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
670 this->Args[Idx] = Args[Idx];
671 }
672}
673
674CXXBaseOrMemberInitializer::
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000675CXXBaseOrMemberInitializer(ASTContext &Context,
676 FieldDecl *Member, SourceLocation MemberLoc,
677 CXXConstructorDecl *C, SourceLocation L,
678 Expr **Args, unsigned NumArgs,
679 SourceLocation R)
680 : BaseOrMember(Member), MemberLocation(MemberLoc), Args(0), NumArgs(0),
681 CtorOrAnonUnion(C), LParenLoc(L), RParenLoc(R)
682{
Douglas Gregore8381c02008-11-05 04:29:56 +0000683 if (NumArgs > 0) {
684 this->NumArgs = NumArgs;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000685 this->Args = new (Context) Stmt*[NumArgs];
Douglas Gregore8381c02008-11-05 04:29:56 +0000686 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
687 this->Args[Idx] = Args[Idx];
688 }
689}
690
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000691void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) {
692 for (unsigned I = 0; I != NumArgs; ++I)
693 Args[I]->Destroy(Context);
694 Context.Deallocate(Args);
695 this->~CXXBaseOrMemberInitializer();
696}
697
698TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
699 if (isBaseInitializer())
John McCallbcd03502009-12-07 02:54:59 +0000700 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000701 else
702 return TypeLoc();
703}
704
705Type *CXXBaseOrMemberInitializer::getBaseClass() {
706 if (isBaseInitializer())
John McCallbcd03502009-12-07 02:54:59 +0000707 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000708 else
709 return 0;
710}
711
712const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
713 if (isBaseInitializer())
John McCallbcd03502009-12-07 02:54:59 +0000714 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000715 else
716 return 0;
717}
718
719SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
720 if (isMemberInitializer())
721 return getMemberLocation();
722
723 return getBaseClassLoc().getSourceRange().getBegin();
724}
725
726SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
727 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +0000728}
729
Douglas Gregor61956c42008-10-31 09:07:45 +0000730CXXConstructorDecl *
731CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor77324f32008-11-17 14:58:09 +0000732 SourceLocation L, DeclarationName N,
John McCallbcd03502009-12-07 02:54:59 +0000733 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000734 bool isExplicit,
Douglas Gregor61956c42008-10-31 09:07:45 +0000735 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000736 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
737 "Name must refer to a constructor");
John McCallbcd03502009-12-07 02:54:59 +0000738 return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit, isInline,
Douglas Gregor61956c42008-10-31 09:07:45 +0000739 isImplicitlyDeclared);
740}
741
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000742bool CXXConstructorDecl::isDefaultConstructor() const {
743 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000744 // A default constructor for a class X is a constructor of class
745 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000746 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +0000747 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000748}
749
Mike Stump11289f42009-09-09 15:08:12 +0000750bool
Douglas Gregor507eb872009-12-22 00:34:07 +0000751CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000752 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000753 // A non-template constructor for class X is a copy constructor
754 // if its first parameter is of type X&, const X&, volatile X& or
755 // const volatile X&, and either there are no other parameters
756 // or else all other parameters have default arguments (8.3.6).
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000757 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +0000758 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorff7028a2009-11-13 23:59:09 +0000759 (getPrimaryTemplate() != 0) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +0000760 (getDescribedFunctionTemplate() != 0))
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000761 return false;
762
763 const ParmVarDecl *Param = getParamDecl(0);
764
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000765 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorff7028a2009-11-13 23:59:09 +0000766 const LValueReferenceType *ParamRefType =
767 Param->getType()->getAs<LValueReferenceType>();
768 if (!ParamRefType)
769 return false;
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000770
Douglas Gregorff7028a2009-11-13 23:59:09 +0000771 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +0000772 ASTContext &Context = getASTContext();
773
Douglas Gregorff7028a2009-11-13 23:59:09 +0000774 CanQualType PointeeType
775 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +0000776 CanQualType ClassTy
777 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000778 if (PointeeType.getUnqualifiedType() != ClassTy)
779 return false;
780
John McCall8ccfcb52009-09-24 19:53:00 +0000781 // FIXME: other qualifiers?
782
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000783 // We have a copy constructor.
784 TypeQuals = PointeeType.getCVRQualifiers();
785 return true;
786}
787
Anders Carlssond20e7952009-08-28 16:57:08 +0000788bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000789 // C++ [class.conv.ctor]p1:
790 // A constructor declared without the function-specifier explicit
791 // that can be called with a single parameter specifies a
792 // conversion from the type of its first parameter to the type of
793 // its class. Such a constructor is called a converting
794 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +0000795 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000796 return false;
797
Mike Stump11289f42009-09-09 15:08:12 +0000798 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +0000799 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000800 (getNumParams() == 1) ||
Anders Carlsson85446472009-06-06 04:14:07 +0000801 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000802}
Douglas Gregor61956c42008-10-31 09:07:45 +0000803
Douglas Gregorffe14e32009-11-14 01:20:54 +0000804bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
805 if ((getNumParams() < 1) ||
806 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
807 (getPrimaryTemplate() == 0) ||
808 (getDescribedFunctionTemplate() != 0))
809 return false;
810
811 const ParmVarDecl *Param = getParamDecl(0);
812
813 ASTContext &Context = getASTContext();
814 CanQualType ParamType = Context.getCanonicalType(Param->getType());
815
816 // Strip off the lvalue reference, if any.
817 if (CanQual<LValueReferenceType> ParamRefType
818 = ParamType->getAs<LValueReferenceType>())
819 ParamType = ParamRefType->getPointeeType();
820
821
822 // Is it the same as our our class type?
823 CanQualType ClassTy
824 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
825 if (ParamType.getUnqualifiedType() != ClassTy)
826 return false;
827
828 return true;
829}
830
Douglas Gregor831c93f2008-11-05 20:51:48 +0000831CXXDestructorDecl *
832CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor77324f32008-11-17 14:58:09 +0000833 SourceLocation L, DeclarationName N,
Mike Stump11289f42009-09-09 15:08:12 +0000834 QualType T, bool isInline,
Douglas Gregor831c93f2008-11-05 20:51:48 +0000835 bool isImplicitlyDeclared) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000836 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
837 "Name must refer to a destructor");
Mike Stump11289f42009-09-09 15:08:12 +0000838 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff13ae6f42009-01-27 21:25:57 +0000839 isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000840}
841
Fariborz Jahanian52337412009-07-01 21:05:43 +0000842void
Fariborz Jahanian5c6af0a2009-07-01 23:35:25 +0000843CXXConstructorDecl::Destroy(ASTContext& C) {
844 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian003a8802009-07-07 16:24:08 +0000845 CXXMethodDecl::Destroy(C);
Fariborz Jahanian5c6af0a2009-07-01 23:35:25 +0000846}
847
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000848CXXConversionDecl *
849CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor77324f32008-11-17 14:58:09 +0000850 SourceLocation L, DeclarationName N,
John McCallbcd03502009-12-07 02:54:59 +0000851 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000852 bool isInline, bool isExplicit) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000853 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
854 "Name must refer to a conversion function");
John McCallbcd03502009-12-07 02:54:59 +0000855 return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit);
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000856}
857
John McCallaa74a0c2009-08-28 07:59:38 +0000858FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
859 SourceLocation L,
860 FriendUnion Friend,
861 SourceLocation FriendL) {
Daniel Dunbar6733a7e2009-08-31 19:16:38 +0000862#ifndef NDEBUG
John McCallaa74a0c2009-08-28 07:59:38 +0000863 if (Friend.is<NamedDecl*>()) {
864 NamedDecl *D = Friend.get<NamedDecl*>();
865 assert(isa<FunctionDecl>(D) ||
866 isa<CXXRecordDecl>(D) ||
867 isa<FunctionTemplateDecl>(D) ||
868 isa<ClassTemplateDecl>(D));
John McCall90d3bb92009-12-17 23:21:11 +0000869
870 // As a temporary hack, we permit template instantiation to point
871 // to the original declaration when instantiating members.
872 assert(D->getFriendObjectKind() ||
873 (cast<CXXRecordDecl>(DC)->getTemplateSpecializationKind()));
John McCallaa74a0c2009-08-28 07:59:38 +0000874 }
Daniel Dunbar6733a7e2009-08-31 19:16:38 +0000875#endif
John McCall2658c4e2009-08-11 21:13:21 +0000876
John McCallaa74a0c2009-08-28 07:59:38 +0000877 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump11289f42009-09-09 15:08:12 +0000878}
John McCalld1e9d832009-08-11 06:59:38 +0000879
Chris Lattnerb8c18fa2008-11-04 16:51:42 +0000880LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +0000881 DeclContext *DC,
Chris Lattnerb8c18fa2008-11-04 16:51:42 +0000882 SourceLocation L,
Douglas Gregor07665a62009-01-05 19:45:36 +0000883 LanguageIDs Lang, bool Braces) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000884 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000885}
Douglas Gregor889ceb72009-02-03 19:21:40 +0000886
887UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
888 SourceLocation L,
889 SourceLocation NamespaceLoc,
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000890 SourceRange QualifierRange,
891 NestedNameSpecifier *Qualifier,
Douglas Gregor889ceb72009-02-03 19:21:40 +0000892 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +0000893 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +0000894 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +0000895 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
896 Used = NS->getOriginalNamespace();
Mike Stump11289f42009-09-09 15:08:12 +0000897 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000898 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +0000899}
900
Sebastian Redla6602e92009-11-23 15:34:23 +0000901NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
902 if (NamespaceAliasDecl *NA =
903 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
904 return NA->getNamespace();
905 return cast_or_null<NamespaceDecl>(NominatedNamespace);
906}
907
Mike Stump11289f42009-09-09 15:08:12 +0000908NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
909 SourceLocation L,
910 SourceLocation AliasLoc,
911 IdentifierInfo *Alias,
Douglas Gregor18231932009-05-30 06:48:27 +0000912 SourceRange QualifierRange,
913 NestedNameSpecifier *Qualifier,
Mike Stump11289f42009-09-09 15:08:12 +0000914 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +0000915 NamedDecl *Namespace) {
Sebastian Redla6602e92009-11-23 15:34:23 +0000916 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
917 Namespace = NS->getOriginalNamespace();
Mike Stump11289f42009-09-09 15:08:12 +0000918 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor18231932009-05-30 06:48:27 +0000919 Qualifier, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +0000920}
921
Douglas Gregorfec52632009-06-20 00:51:54 +0000922UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
John McCall3f746822009-11-17 05:59:44 +0000923 SourceLocation L, SourceRange NNR, SourceLocation UL,
924 NestedNameSpecifier* TargetNNS, DeclarationName Name,
925 bool IsTypeNameArg) {
926 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg);
Douglas Gregorfec52632009-06-20 00:51:54 +0000927}
928
John McCalle61f2ba2009-11-18 02:36:19 +0000929UnresolvedUsingValueDecl *
930UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
931 SourceLocation UsingLoc,
932 SourceRange TargetNNR,
933 NestedNameSpecifier *TargetNNS,
934 SourceLocation TargetNameLoc,
935 DeclarationName TargetName) {
936 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
937 TargetNNR, TargetNNS,
938 TargetNameLoc, TargetName);
939}
940
941UnresolvedUsingTypenameDecl *
942UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
943 SourceLocation UsingLoc,
944 SourceLocation TypenameLoc,
945 SourceRange TargetNNR,
946 NestedNameSpecifier *TargetNNS,
947 SourceLocation TargetNameLoc,
948 DeclarationName TargetName) {
949 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
950 TargetNNR, TargetNNS,
951 TargetNameLoc,
952 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +0000953}
954
Anders Carlsson5bbe1d72009-03-14 00:25:26 +0000955StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
956 SourceLocation L, Expr *AssertExpr,
957 StringLiteral *Message) {
958 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
959}
960
961void StaticAssertDecl::Destroy(ASTContext& C) {
962 AssertExpr->Destroy(C);
963 Message->Destroy(C);
964 this->~StaticAssertDecl();
965 C.Deallocate((void *)this);
966}
967
968StaticAssertDecl::~StaticAssertDecl() {
969}
970
Anders Carlsson6750d162009-03-26 23:46:50 +0000971static const char *getAccessName(AccessSpecifier AS) {
972 switch (AS) {
973 default:
974 case AS_none:
975 assert("Invalid access specifier!");
976 return 0;
977 case AS_public:
978 return "public";
979 case AS_private:
980 return "private";
981 case AS_protected:
982 return "protected";
983 }
984}
985
986const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
987 AccessSpecifier AS) {
988 return DB << getAccessName(AS);
989}
990
991