blob: 29a7c7fad9e4b90a9c6c2f8e03a12b0db05dbbfd [file] [log] [blame]
Ted Kremenek46a837c2008-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 Gregorcc887972009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek46a837c2008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Anders Carlssoned691562009-03-14 00:25:26 +000017#include "clang/AST/Expr.h"
Douglas Gregorcbcb4c22008-11-12 23:21:09 +000018#include "clang/Basic/IdentifierTable.h"
Douglas Gregor6a202012008-12-23 21:31:30 +000019#include "llvm/ADT/STLExtras.h"
Ted Kremenek46a837c2008-09-05 17:16:31 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// Decl Allocation/Deallocation Method Implementations
24//===----------------------------------------------------------------------===//
Douglas Gregordd861062008-12-05 18:15:24 +000025
Douglas Gregorad964b32009-02-17 01:05:43 +000026CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Douglas Gregor9060d0e2009-07-21 14:46:17 +000027 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor19e567a2009-07-29 23:36:44 +000028 CXXRecordDecl *PrevDecl,
Douglas Gregor9060d0e2009-07-21 14:46:17 +000029 SourceLocation TKL)
Douglas Gregor19e567a2009-07-29 23:36:44 +000030 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
Douglas Gregorcbcb4c22008-11-12 23:21:09 +000031 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl39c0f6f2009-01-05 20:52:13 +000032 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedmanc9754d82009-08-15 22:23:00 +000033 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
34 Abstract(false), HasTrivialConstructor(true),
35 HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true),
36 HasTrivialDestructor(true), Bases(0), NumBases(0), VBases(0), NumVBases(0),
Fariborz Jahanianc1ce61b2009-07-10 20:13:23 +000037 Conversions(DC, DeclarationName()),
Douglas Gregorcc887972009-03-25 21:17:03 +000038 TemplateOrInstantiation() { }
Douglas Gregorcbcb4c22008-11-12 23:21:09 +000039
Ted Kremenek46a837c2008-09-05 17:16:31 +000040CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
41 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor9060d0e2009-07-21 14:46:17 +000042 SourceLocation TKL,
Douglas Gregor12aed0b2009-05-15 19:11:46 +000043 CXXRecordDecl* PrevDecl,
44 bool DelayTypeCreation) {
Douglas Gregor19e567a2009-07-29 23:36:44 +000045 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
46 PrevDecl, TKL);
47
48 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor12aed0b2009-05-15 19:11:46 +000049 if (!DelayTypeCreation)
50 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek46a837c2008-09-05 17:16:31 +000051 return R;
52}
53
Douglas Gregorabed2172008-10-22 17:49:05 +000054CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian9cd0a3c2009-07-02 18:26:15 +000055}
56
57void CXXRecordDecl::Destroy(ASTContext &C) {
58 C.Deallocate(Bases);
Fariborz Jahanian1373b6f2009-07-22 17:41:53 +000059 C.Deallocate(VBases);
Fariborz Jahanian9cd0a3c2009-07-02 18:26:15 +000060 this->RecordDecl::Destroy(C);
Douglas Gregorabed2172008-10-22 17:49:05 +000061}
62
Douglas Gregor4fd85902008-10-23 18:13:27 +000063void
Fariborz Jahanian9cd0a3c2009-07-02 18:26:15 +000064CXXRecordDecl::setBases(ASTContext &C,
65 CXXBaseSpecifier const * const *Bases,
Douglas Gregor4fd85902008-10-23 18:13:27 +000066 unsigned NumBases) {
Douglas Gregor15e04622008-11-05 16:20:31 +000067 // C++ [dcl.init.aggr]p1:
68 // An aggregate is an array or a class (clause 9) with [...]
69 // no base classes [...].
70 Aggregate = false;
71
Douglas Gregor4fd85902008-10-23 18:13:27 +000072 if (this->Bases)
Fariborz Jahanian9cd0a3c2009-07-02 18:26:15 +000073 C.Deallocate(this->Bases);
Fariborz Jahanianc1ce61b2009-07-10 20:13:23 +000074
75 int vbaseCount = 0;
76 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
77 bool hasDirectVirtualBase = false;
78
Fariborz Jahanian9cd0a3c2009-07-02 18:26:15 +000079 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor4fd85902008-10-23 18:13:27 +000080 this->NumBases = NumBases;
Fariborz Jahanianc1ce61b2009-07-10 20:13:23 +000081 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor4fd85902008-10-23 18:13:27 +000082 this->Bases[i] = *Bases[i];
Fariborz Jahanianc1ce61b2009-07-10 20:13:23 +000083 // Keep track of inherited vbases for this base class.
84 const CXXBaseSpecifier *Base = Bases[i];
85 QualType BaseType = Base->getType();
86 // Skip template types.
87 // FIXME. This means that this list must be rebuilt during template
88 // instantiation.
89 if (BaseType->isDependentType())
90 continue;
91 CXXRecordDecl *BaseClassDecl
Ted Kremenekd00cd9e2009-07-29 21:53:49 +000092 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Fariborz Jahanianc1ce61b2009-07-10 20:13:23 +000093 if (Base->isVirtual())
94 hasDirectVirtualBase = true;
95 for (CXXRecordDecl::base_class_iterator VBase =
96 BaseClassDecl->vbases_begin(),
97 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
98 // Add this vbase to the array of vbases for current class if it is
99 // not already in the list.
100 // FIXME. Note that we do a linear search as number of such classes are
101 // very few.
102 int i;
103 for (i = 0; i < vbaseCount; ++i)
104 if (UniqueVbases[i]->getType() == VBase->getType())
105 break;
106 if (i == vbaseCount) {
107 UniqueVbases.push_back(VBase);
108 ++vbaseCount;
109 }
110 }
111 }
112 if (hasDirectVirtualBase) {
113 // Iterate one more time through the direct bases and add the virtual
114 // base to the list of vritual bases for current class.
115 for (unsigned i = 0; i < NumBases; ++i) {
116 const CXXBaseSpecifier *VBase = Bases[i];
117 if (!VBase->isVirtual())
118 continue;
Alisdair Meredithe0baa1e2009-07-11 14:32:10 +0000119 int j;
120 for (j = 0; j < vbaseCount; ++j)
121 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanianc1ce61b2009-07-10 20:13:23 +0000122 break;
Alisdair Meredithe0baa1e2009-07-11 14:32:10 +0000123 if (j == vbaseCount) {
Fariborz Jahanianc1ce61b2009-07-10 20:13:23 +0000124 UniqueVbases.push_back(VBase);
125 ++vbaseCount;
126 }
127 }
128 }
129 if (vbaseCount > 0) {
130 // build AST for inhireted, direct or indirect, virtual bases.
Douglas Gregorf73c8512009-07-22 18:25:24 +0000131 this->VBases = new (C) CXXBaseSpecifier [vbaseCount];
Fariborz Jahanianc1ce61b2009-07-10 20:13:23 +0000132 this->NumVBases = vbaseCount;
133 for (int i = 0; i < vbaseCount; i++) {
134 QualType QT = UniqueVbases[i]->getType();
135 CXXRecordDecl *VBaseClassDecl
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000136 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl());
Fariborz Jahanianc1ce61b2009-07-10 20:13:23 +0000137 this->VBases[i] =
Douglas Gregorf2fedc62009-07-22 20:55:49 +0000138 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
139 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
140 UniqueVbases[i]->getAccessSpecifier(), QT);
Fariborz Jahanianc1ce61b2009-07-10 20:13:23 +0000141 }
142 }
Douglas Gregor4fd85902008-10-23 18:13:27 +0000143}
144
Douglas Gregore640ab62008-11-03 17:51:48 +0000145bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Fariborz Jahanian599778e2009-06-22 23:34:40 +0000146 return getCopyConstructor(Context, QualType::Const) != 0;
147}
148
149CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
150 unsigned TypeQuals) const{
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000151 QualType ClassType
152 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Douglas Gregorb9213832008-12-15 21:24:18 +0000153 DeclarationName ConstructorName
154 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian599778e2009-06-22 23:34:40 +0000155 Context.getCanonicalType(ClassType));
156 unsigned FoundTQs;
Douglas Gregor6a202012008-12-23 21:31:30 +0000157 DeclContext::lookup_const_iterator Con, ConEnd;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000158 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregor6a202012008-12-23 21:31:30 +0000159 Con != ConEnd; ++Con) {
Fariborz Jahanian599778e2009-06-22 23:34:40 +0000160 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context,
161 FoundTQs)) {
162 if (((TypeQuals & QualType::Const) == (FoundTQs & QualType::Const)) ||
163 (!(TypeQuals & QualType::Const) && (FoundTQs & QualType::Const)))
164 return cast<CXXConstructorDecl>(*Con);
165
166 }
Douglas Gregore640ab62008-11-03 17:51:48 +0000167 }
Fariborz Jahanian599778e2009-06-22 23:34:40 +0000168 return 0;
Douglas Gregore640ab62008-11-03 17:51:48 +0000169}
170
Fariborz Jahanian04500242009-08-12 23:34:46 +0000171bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
172 const CXXMethodDecl *& MD) const {
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000173 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
174 const_cast<CXXRecordDecl*>(this)));
175 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
176
177 DeclContext::lookup_const_iterator Op, OpEnd;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000178 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000179 Op != OpEnd; ++Op) {
180 // C++ [class.copy]p9:
181 // A user-declared copy assignment operator is a non-static non-template
182 // member function of class X with exactly one parameter of type X, X&,
183 // const X&, volatile X& or const volatile X&.
184 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
185 if (Method->isStatic())
186 continue;
187 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor4fa58902009-02-26 23:50:07 +0000188 const FunctionProtoType *FnType =
189 Method->getType()->getAsFunctionProtoType();
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000190 assert(FnType && "Overloaded operator has no prototype.");
191 // Don't assert on this; an invalid decl might have been left in the AST.
192 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
193 continue;
194 bool AcceptsConst = true;
195 QualType ArgType = FnType->getArgType(0);
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000196 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000197 ArgType = Ref->getPointeeType();
Douglas Gregor92334522009-03-20 20:21:37 +0000198 // Is it a non-const lvalue reference?
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000199 if (!ArgType.isConstQualified())
200 AcceptsConst = false;
201 }
202 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
203 continue;
Fariborz Jahanian04500242009-08-12 23:34:46 +0000204 MD = Method;
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000205 // We have a single argument of type cv X or cv X&, i.e. we've found the
206 // copy assignment operator. Return whether it accepts const arguments.
207 return AcceptsConst;
208 }
209 assert(isInvalidDecl() &&
210 "No copy assignment operator declared in valid code.");
211 return false;
212}
213
214void
Douglas Gregorb9213832008-12-15 21:24:18 +0000215CXXRecordDecl::addedConstructor(ASTContext &Context,
216 CXXConstructorDecl *ConDecl) {
Fariborz Jahanianf27ef732009-06-17 22:44:31 +0000217 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
218 // Note that we have a user-declared constructor.
219 UserDeclaredConstructor = true;
Douglas Gregorccabf082008-10-31 20:25:05 +0000220
Fariborz Jahanianf27ef732009-06-17 22:44:31 +0000221 // C++ [dcl.init.aggr]p1:
222 // An aggregate is an array or a class (clause 9) with no
223 // user-declared constructors (12.1) [...].
224 Aggregate = false;
Douglas Gregor15e04622008-11-05 16:20:31 +0000225
Fariborz Jahanianf27ef732009-06-17 22:44:31 +0000226 // C++ [class]p4:
227 // A POD-struct is an aggregate class [...]
228 PlainOldData = false;
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000229
Fariborz Jahanianf27ef732009-06-17 22:44:31 +0000230 // C++ [class.ctor]p5:
231 // A constructor is trivial if it is an implicitly-declared default
232 // constructor.
Douglas Gregorf73c8512009-07-22 18:25:24 +0000233 // FIXME: C++0x: don't do this for "= default" default constructors.
Fariborz Jahanianf27ef732009-06-17 22:44:31 +0000234 HasTrivialConstructor = false;
Anders Carlssonc6363712009-04-16 00:08:20 +0000235
Fariborz Jahanianf27ef732009-06-17 22:44:31 +0000236 // Note when we have a user-declared copy constructor, which will
237 // suppress the implicit declaration of a copy constructor.
Douglas Gregorf73c8512009-07-22 18:25:24 +0000238 if (ConDecl->isCopyConstructor(Context)) {
Fariborz Jahanianf27ef732009-06-17 22:44:31 +0000239 UserDeclaredCopyConstructor = true;
Douglas Gregorf73c8512009-07-22 18:25:24 +0000240
241 // C++ [class.copy]p6:
242 // A copy constructor is trivial if it is implicitly declared.
243 // FIXME: C++0x: don't do this for "= default" copy constructors.
244 HasTrivialCopyConstructor = false;
245 }
Douglas Gregorccabf082008-10-31 20:25:05 +0000246}
247
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000248void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
249 CXXMethodDecl *OpDecl) {
250 // We're interested specifically in copy assignment operators.
Douglas Gregor4fa58902009-02-26 23:50:07 +0000251 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000252 assert(FnType && "Overloaded operator has no proto function type.");
253 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
254 QualType ArgType = FnType->getArgType(0);
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000255 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000256 ArgType = Ref->getPointeeType();
257
258 ArgType = ArgType.getUnqualifiedType();
259 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
260 const_cast<CXXRecordDecl*>(this)));
261
262 if (ClassType != Context.getCanonicalType(ArgType))
263 return;
264
265 // This is a copy assignment operator.
266 // Suppress the implicit declaration of a copy constructor.
267 UserDeclaredCopyAssignment = true;
268
Douglas Gregorf73c8512009-07-22 18:25:24 +0000269 // C++ [class.copy]p11:
270 // A copy assignment operator is trivial if it is implicitly declared.
271 // FIXME: C++0x: don't do this for "= default" copy operators.
272 HasTrivialCopyAssignment = false;
273
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000274 // C++ [class]p4:
275 // A POD-struct is an aggregate class that [...] has no user-defined copy
276 // assignment operator [...].
277 PlainOldData = false;
278}
279
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000280void CXXRecordDecl::addConversionFunction(ASTContext &Context,
281 CXXConversionDecl *ConvDecl) {
Douglas Gregor8c860df2009-08-21 23:19:43 +0000282 assert(!ConvDecl->getDescribedFunctionTemplate() &&
283 "Conversion function templates should cast to FunctionTemplateDecl.");
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000284 Conversions.addOverload(ConvDecl);
285}
286
Douglas Gregor8c860df2009-08-21 23:19:43 +0000287void CXXRecordDecl::addConversionFunction(ASTContext &Context,
288 FunctionTemplateDecl *ConvDecl) {
289 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
290 "Function template is not a conversion function template");
291 Conversions.addOverload(ConvDecl);
292}
Fariborz Jahanian3603e0c2009-06-19 19:55:27 +0000293
294CXXConstructorDecl *
295CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
296 QualType ClassType = Context.getTypeDeclType(this);
297 DeclarationName ConstructorName
298 = Context.DeclarationNames.getCXXConstructorName(
299 Context.getCanonicalType(ClassType.getUnqualifiedType()));
300
301 DeclContext::lookup_const_iterator Con, ConEnd;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000302 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanian3603e0c2009-06-19 19:55:27 +0000303 Con != ConEnd; ++Con) {
304 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
305 if (Constructor->isDefaultConstructor())
306 return Constructor;
307 }
308 return 0;
309}
310
Anders Carlssond3f6b162009-05-29 21:03:38 +0000311const CXXDestructorDecl *
312CXXRecordDecl::getDestructor(ASTContext &Context) {
313 QualType ClassType = Context.getTypeDeclType(this);
Fariborz Jahanian3603e0c2009-06-19 19:55:27 +0000314
Anders Carlssond3f6b162009-05-29 21:03:38 +0000315 DeclarationName Name
Douglas Gregorcfe6ae52009-08-05 05:36:45 +0000316 = Context.DeclarationNames.getCXXDestructorName(
317 Context.getCanonicalType(ClassType));
Anders Carlssond3f6b162009-05-29 21:03:38 +0000318
319 DeclContext::lookup_iterator I, E;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000320 llvm::tie(I, E) = lookup(Name);
Anders Carlssond3f6b162009-05-29 21:03:38 +0000321 assert(I != E && "Did not find a destructor!");
322
323 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
324 assert(++I == E && "Found more than one destructor!");
325
326 return Dtor;
327}
328
Ted Kremenek46a837c2008-09-05 17:16:31 +0000329CXXMethodDecl *
330CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor6704b312008-11-17 22:58:34 +0000331 SourceLocation L, DeclarationName N,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000332 QualType T, DeclaratorInfo *DInfo,
333 bool isStatic, bool isInline) {
334 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, DInfo,
335 isStatic, isInline);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000336}
337
Anders Carlsson76244712009-05-16 23:58:37 +0000338
339typedef llvm::DenseMap<const CXXMethodDecl*,
340 std::vector<const CXXMethodDecl *> *>
341 OverriddenMethodsMapTy;
342
Mike Stumpf07ede52009-08-21 01:45:00 +0000343// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
344// the vtable building code uses it at CG time.
Anders Carlsson76244712009-05-16 23:58:37 +0000345static OverriddenMethodsMapTy *OverriddenMethods = 0;
346
347void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
348 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
349
350 if (!OverriddenMethods)
351 OverriddenMethods = new OverriddenMethodsMapTy();
352
353 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
354 if (!Methods)
355 Methods = new std::vector<const CXXMethodDecl *>;
356
357 Methods->push_back(MD);
358}
359
360CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
361 if (!OverriddenMethods)
362 return 0;
363
364 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbarce956ab2009-08-01 23:40:20 +0000365 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson76244712009-05-16 23:58:37 +0000366 return 0;
Daniel Dunbarce956ab2009-08-01 23:40:20 +0000367
Anders Carlsson76244712009-05-16 23:58:37 +0000368 return &(*it->second)[0];
369}
370
371CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
372 if (!OverriddenMethods)
373 return 0;
374
375 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbarce956ab2009-08-01 23:40:20 +0000376 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson76244712009-05-16 23:58:37 +0000377 return 0;
378
Daniel Dunbar655658a2009-08-02 01:48:29 +0000379 return &(*it->second)[0] + it->second->size();
Anders Carlsson76244712009-05-16 23:58:37 +0000380}
381
Ted Kremenek46a837c2008-09-05 17:16:31 +0000382QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argiris Kirtzidis8e4d5d72008-10-24 22:28:18 +0000383 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
384 // If the member function is declared const, the type of this is const X*,
385 // if the member function is declared volatile, the type of this is
386 // volatile X*, and if the member function is declared const volatile,
387 // the type of this is const volatile X*.
388
Ted Kremenek46a837c2008-09-05 17:16:31 +0000389 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson9c395022009-06-13 02:59:33 +0000390
391 QualType ClassTy;
392 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
393 ClassTy = TD->getInjectedClassNameType(C);
394 else
Mike Stumpe7545622009-08-07 18:05:12 +0000395 ClassTy = C.getTagDeclType(getParent());
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000396 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
Anders Carlsson0f6762b2009-07-10 21:35:09 +0000397 return C.getPointerType(ClassTy);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000398}
399
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000400CXXBaseOrMemberInitializer::
Fariborz Jahanian89f61bd2009-06-30 00:02:17 +0000401CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
Fariborz Jahanian898f5742009-07-23 00:42:24 +0000402 CXXConstructorDecl *C,
Anders Carlsson6d68ad42009-08-29 01:31:33 +0000403 SourceLocation L, SourceLocation R)
Douglas Gregor80f00752009-09-01 21:04:42 +0000404 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000405 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
406 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
407 BaseOrMember |= 0x01;
408
409 if (NumArgs > 0) {
410 this->NumArgs = NumArgs;
Fariborz Jahanian56baceb2009-07-24 17:57:02 +0000411 // FIXME. Allocation via Context
412 this->Args = new Stmt*[NumArgs];
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000413 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
414 this->Args[Idx] = Args[Idx];
415 }
Douglas Gregor80f00752009-09-01 21:04:42 +0000416 CtorOrAnonUnion = C;
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000417}
418
419CXXBaseOrMemberInitializer::
Fariborz Jahanian89f61bd2009-06-30 00:02:17 +0000420CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
Fariborz Jahanian898f5742009-07-23 00:42:24 +0000421 CXXConstructorDecl *C,
Anders Carlsson6d68ad42009-08-29 01:31:33 +0000422 SourceLocation L, SourceLocation R)
Douglas Gregor80f00752009-09-01 21:04:42 +0000423 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000424 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
425 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
426
427 if (NumArgs > 0) {
428 this->NumArgs = NumArgs;
Fariborz Jahanian56baceb2009-07-24 17:57:02 +0000429 this->Args = new Stmt*[NumArgs];
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000430 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
431 this->Args[Idx] = Args[Idx];
432 }
Douglas Gregor80f00752009-09-01 21:04:42 +0000433 CtorOrAnonUnion = C;
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000434}
435
436CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
437 delete [] Args;
438}
439
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000440CXXConstructorDecl *
441CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000442 SourceLocation L, DeclarationName N,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000443 QualType T, DeclaratorInfo *DInfo,
444 bool isExplicit,
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000445 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000446 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
447 "Name must refer to a constructor");
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000448 return new (C) CXXConstructorDecl(RD, L, N, T, DInfo, isExplicit, isInline,
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000449 isImplicitlyDeclared);
450}
451
Douglas Gregorccabf082008-10-31 20:25:05 +0000452bool CXXConstructorDecl::isDefaultConstructor() const {
453 // C++ [class.ctor]p5:
Douglas Gregor15e04622008-11-05 16:20:31 +0000454 // A default constructor for a class X is a constructor of class
455 // X that can be called without an argument.
Douglas Gregorccabf082008-10-31 20:25:05 +0000456 return (getNumParams() == 0) ||
Anders Carlsson665e4692009-08-25 05:12:04 +0000457 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregorccabf082008-10-31 20:25:05 +0000458}
459
460bool
461CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
462 unsigned &TypeQuals) const {
463 // C++ [class.copy]p2:
Douglas Gregor15e04622008-11-05 16:20:31 +0000464 // A non-template constructor for class X is a copy constructor
465 // if its first parameter is of type X&, const X&, volatile X& or
466 // const volatile X&, and either there are no other parameters
467 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorccabf082008-10-31 20:25:05 +0000468 if ((getNumParams() < 1) ||
Anders Carlssond2e57d92009-06-06 04:14:07 +0000469 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregorccabf082008-10-31 20:25:05 +0000470 return false;
471
472 const ParmVarDecl *Param = getParamDecl(0);
473
Sebastian Redlce6fff02009-03-16 23:22:08 +0000474 // Do we have a reference type? Rvalue references don't count.
475 const LValueReferenceType *ParamRefType =
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000476 Param->getType()->getAs<LValueReferenceType>();
Douglas Gregorccabf082008-10-31 20:25:05 +0000477 if (!ParamRefType)
478 return false;
479
480 // Is it a reference to our class type?
Mike Stumpe7545622009-08-07 18:05:12 +0000481 QualType PointeeType
Douglas Gregorccabf082008-10-31 20:25:05 +0000482 = Context.getCanonicalType(ParamRefType->getPointeeType());
Mike Stumpe7545622009-08-07 18:05:12 +0000483 QualType ClassTy = Context.getTagDeclType(getParent());
Douglas Gregorccabf082008-10-31 20:25:05 +0000484 if (PointeeType.getUnqualifiedType() != ClassTy)
485 return false;
486
487 // We have a copy constructor.
488 TypeQuals = PointeeType.getCVRQualifiers();
489 return true;
490}
491
Anders Carlsson94894572009-08-28 16:57:08 +0000492bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000493 // C++ [class.conv.ctor]p1:
494 // A constructor declared without the function-specifier explicit
495 // that can be called with a single parameter specifies a
496 // conversion from the type of its first parameter to the type of
497 // its class. Such a constructor is called a converting
498 // constructor.
Anders Carlsson94894572009-08-28 16:57:08 +0000499 if (isExplicit() && !AllowExplicit)
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000500 return false;
501
502 return (getNumParams() == 0 &&
Douglas Gregor4fa58902009-02-26 23:50:07 +0000503 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000504 (getNumParams() == 1) ||
Anders Carlssond2e57d92009-06-06 04:14:07 +0000505 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000506}
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000507
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000508CXXDestructorDecl *
509CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000510 SourceLocation L, DeclarationName N,
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000511 QualType T, bool isInline,
512 bool isImplicitlyDeclared) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000513 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
514 "Name must refer to a destructor");
Steve Naroff5abb0282009-01-27 21:25:57 +0000515 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
516 isImplicitlyDeclared);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000517}
518
Fariborz Jahanian28b9c882009-07-01 21:05:43 +0000519void
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000520CXXDestructorDecl::Destroy(ASTContext& C) {
521 C.Deallocate(BaseOrMemberDestructions);
522 CXXMethodDecl::Destroy(C);
523}
524
525void
Fariborz Jahanianbb70eb32009-07-01 23:35:25 +0000526CXXConstructorDecl::Destroy(ASTContext& C) {
527 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanianb29c9b22009-07-07 16:24:08 +0000528 CXXMethodDecl::Destroy(C);
Fariborz Jahanianbb70eb32009-07-01 23:35:25 +0000529}
530
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000531CXXConversionDecl *
532CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000533 SourceLocation L, DeclarationName N,
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000534 QualType T, DeclaratorInfo *DInfo,
535 bool isInline, bool isExplicit) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000536 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
537 "Name must refer to a conversion function");
Argiris Kirtzidisb17120c2009-08-19 01:27:57 +0000538 return new (C) CXXConversionDecl(RD, L, N, T, DInfo, isInline, isExplicit);
Douglas Gregor3ef6c972008-11-07 20:08:42 +0000539}
540
Douglas Gregord2baafd2008-10-21 16:13:35 +0000541OverloadedFunctionDecl *
542OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000543 DeclarationName N) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000544 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000545}
Chris Lattner825f6ea2008-11-04 16:51:42 +0000546
Douglas Gregor050cabf2009-08-21 18:42:58 +0000547OverloadIterator::OverloadIterator(NamedDecl *ND) : D(0) {
548 if (!ND)
549 return;
550
551 if (isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND))
552 D = ND;
553 else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(ND)) {
Douglas Gregor23026c82009-09-01 16:58:52 +0000554 if (Ovl->size() != 0) {
555 D = ND;
556 Iter = Ovl->function_begin();
557 }
Douglas Gregor050cabf2009-08-21 18:42:58 +0000558 }
559}
560
Douglas Gregor993a0602009-06-27 21:05:07 +0000561void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
562 Functions.push_back(F);
563 this->setLocation(F.get()->getLocation());
Douglas Gregorb60eb752009-06-25 22:08:12 +0000564}
565
Douglas Gregor6e264102009-07-08 10:57:20 +0000566OverloadIterator::reference OverloadIterator::operator*() const {
567 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
568 return FD;
569
570 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
571 return FTD;
572
573 assert(isa<OverloadedFunctionDecl>(D));
574 return *Iter;
575}
576
577OverloadIterator &OverloadIterator::operator++() {
578 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
579 D = 0;
580 return *this;
581 }
582
583 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
584 D = 0;
585
586 return *this;
587}
588
589bool OverloadIterator::Equals(const OverloadIterator &Other) const {
590 if (!D || !Other.D)
591 return D == Other.D;
592
593 if (D != Other.D)
594 return false;
595
596 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
597}
598
John McCall7de15912009-08-28 07:59:38 +0000599FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
600 SourceLocation L,
601 FriendUnion Friend,
602 SourceLocation FriendL) {
Daniel Dunbar47040442009-08-31 19:16:38 +0000603#ifndef NDEBUG
John McCall7de15912009-08-28 07:59:38 +0000604 if (Friend.is<NamedDecl*>()) {
605 NamedDecl *D = Friend.get<NamedDecl*>();
606 assert(isa<FunctionDecl>(D) ||
607 isa<CXXRecordDecl>(D) ||
608 isa<FunctionTemplateDecl>(D) ||
609 isa<ClassTemplateDecl>(D));
610 assert(D->getFriendObjectKind());
611 }
Daniel Dunbar47040442009-08-31 19:16:38 +0000612#endif
John McCall7be34f42009-08-11 21:13:21 +0000613
John McCall7de15912009-08-28 07:59:38 +0000614 return new (C) FriendDecl(DC, L, Friend, FriendL);
John McCall7be34f42009-08-11 21:13:21 +0000615}
John McCall36493082009-08-11 06:59:38 +0000616
Chris Lattner825f6ea2008-11-04 16:51:42 +0000617LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregord8028382009-01-05 19:45:36 +0000618 DeclContext *DC,
Chris Lattner825f6ea2008-11-04 16:51:42 +0000619 SourceLocation L,
Douglas Gregord8028382009-01-05 19:45:36 +0000620 LanguageIDs Lang, bool Braces) {
Steve Naroff5abb0282009-01-27 21:25:57 +0000621 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorad17e372008-12-16 22:23:02 +0000622}
Douglas Gregor7a7be652009-02-03 19:21:40 +0000623
624UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
625 SourceLocation L,
626 SourceLocation NamespaceLoc,
Douglas Gregor1d27d692009-05-30 06:31:56 +0000627 SourceRange QualifierRange,
628 NestedNameSpecifier *Qualifier,
Douglas Gregor7a7be652009-02-03 19:21:40 +0000629 SourceLocation IdentLoc,
630 NamespaceDecl *Used,
631 DeclContext *CommonAncestor) {
Douglas Gregor1d27d692009-05-30 06:31:56 +0000632 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
633 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor7a7be652009-02-03 19:21:40 +0000634}
635
Anders Carlssonddb1d8b2009-03-28 22:58:02 +0000636NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
637 SourceLocation L,
638 SourceLocation AliasLoc,
639 IdentifierInfo *Alias,
Douglas Gregor8d8ddca2009-05-30 06:48:27 +0000640 SourceRange QualifierRange,
641 NestedNameSpecifier *Qualifier,
Anders Carlssonddb1d8b2009-03-28 22:58:02 +0000642 SourceLocation IdentLoc,
643 NamedDecl *Namespace) {
Douglas Gregor8d8ddca2009-05-30 06:48:27 +0000644 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
645 Qualifier, IdentLoc, Namespace);
Anders Carlssonddb1d8b2009-03-28 22:58:02 +0000646}
647
Douglas Gregor683a1142009-06-20 00:51:54 +0000648UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
649 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
650 SourceLocation UL, NamedDecl* Target,
651 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
652 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
653 TargetNNS, IsTypeNameArg);
654}
655
Anders Carlsson4e750062009-08-28 05:30:28 +0000656UnresolvedUsingDecl *UnresolvedUsingDecl::Create(ASTContext &C, DeclContext *DC,
657 SourceLocation UsingLoc,
658 SourceRange TargetNNR,
659 NestedNameSpecifier *TargetNNS,
660 SourceLocation TargetNameLoc,
661 DeclarationName TargetName,
662 bool IsTypeNameArg) {
663 return new (C) UnresolvedUsingDecl(DC, UsingLoc, TargetNNR, TargetNNS,
664 TargetNameLoc, TargetName, IsTypeNameArg);
665}
666
Anders Carlssoned691562009-03-14 00:25:26 +0000667StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
668 SourceLocation L, Expr *AssertExpr,
669 StringLiteral *Message) {
670 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
671}
672
673void StaticAssertDecl::Destroy(ASTContext& C) {
674 AssertExpr->Destroy(C);
675 Message->Destroy(C);
676 this->~StaticAssertDecl();
677 C.Deallocate((void *)this);
678}
679
680StaticAssertDecl::~StaticAssertDecl() {
681}
682
Anders Carlssone8dcd922009-03-26 23:46:50 +0000683static const char *getAccessName(AccessSpecifier AS) {
684 switch (AS) {
685 default:
686 case AS_none:
687 assert("Invalid access specifier!");
688 return 0;
689 case AS_public:
690 return "public";
691 case AS_private:
692 return "private";
693 case AS_protected:
694 return "protected";
695 }
696}
697
698const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
699 AccessSpecifier AS) {
700 return DB << getAccessName(AS);
701}
702
703