blob: e3a0f486d593c3fb99eedcfaad71dd6bccf87f96 [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000017#include "clang/AST/Expr.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000018#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000019#include "llvm/ADT/STLExtras.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// Decl Allocation/Deallocation Method Implementations
24//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000025
Douglas Gregor3e00bad2009-02-17 01:05:43 +000026CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000027 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000028 CXXRecordDecl *PrevDecl,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000029 SourceLocation TKL)
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000030 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
Douglas Gregor7d7e6722008-11-12 23:21:09 +000031 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000032 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-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 Jahanian40c072f2009-07-10 20:13:23 +000037 Conversions(DC, DeclarationName()),
Douglas Gregord475b8d2009-03-25 21:17:03 +000038 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000039
Ted Kremenek4b7c9832008-09-05 17:16:31 +000040CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
41 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000042 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000043 CXXRecordDecl* PrevDecl,
44 bool DelayTypeCreation) {
Douglas Gregor8e9e9ef2009-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 Gregoraafc0cc2009-05-15 19:11:46 +000049 if (!DelayTypeCreation)
50 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000051 return R;
52}
53
Douglas Gregorf8268ae2008-10-22 17:49:05 +000054CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000055}
56
57void CXXRecordDecl::Destroy(ASTContext &C) {
58 C.Deallocate(Bases);
Fariborz Jahanian71c6e712009-07-22 17:41:53 +000059 C.Deallocate(VBases);
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000060 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000061}
62
Douglas Gregor57c856b2008-10-23 18:13:27 +000063void
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000064CXXRecordDecl::setBases(ASTContext &C,
65 CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000066 unsigned NumBases) {
Douglas Gregor64bffa92008-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 Gregor57c856b2008-10-23 18:13:27 +000072 if (this->Bases)
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000073 C.Deallocate(this->Bases);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000074
75 int vbaseCount = 0;
76 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
77 bool hasDirectVirtualBase = false;
78
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000079 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor57c856b2008-10-23 18:13:27 +000080 this->NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000081 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor57c856b2008-10-23 18:13:27 +000082 this->Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-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 Kremenek6217b802009-07-29 21:53:49 +000092 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Fariborz Jahanian40c072f2009-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 Meredith002b91f2009-07-11 14:32:10 +0000119 int j;
120 for (j = 0; j < vbaseCount; ++j)
121 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000122 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000123 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-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 Gregor1f2023a2009-07-22 18:25:24 +0000131 this->VBases = new (C) CXXBaseSpecifier [vbaseCount];
Fariborz Jahanian40c072f2009-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 Kremenek6217b802009-07-29 21:53:49 +0000136 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000137 this->VBases[i] =
Douglas Gregor2aef06d2009-07-22 20:55:49 +0000138 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
139 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
140 UniqueVbases[i]->getAccessSpecifier(), QT);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000141 }
142 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000143}
144
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000145bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000146 return getCopyConstructor(Context, QualType::Const) != 0;
147}
148
149CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
150 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000151 QualType ClassType
152 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000153 DeclarationName ConstructorName
154 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000155 Context.getCanonicalType(ClassType));
156 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000157 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000158 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000159 Con != ConEnd; ++Con) {
Fariborz Jahanian485f0872009-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 Gregor396b7cd2008-11-03 17:51:48 +0000167 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000168 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000169}
170
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000171bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
172 const CXXMethodDecl *& MD) const {
Sebastian Redl64b45f72009-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;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000178 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-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 Gregor72564e72009-02-26 23:50:07 +0000188 const FunctionProtoType *FnType =
189 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-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 Kremenek6217b802009-07-29 21:53:49 +0000196 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000197 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000198 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000199 if (!ArgType.isConstQualified())
200 AcceptsConst = false;
201 }
202 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
203 continue;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000204 MD = Method;
Sebastian Redl64b45f72009-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 Gregor9e7d9de2008-12-15 21:24:18 +0000215CXXRecordDecl::addedConstructor(ASTContext &Context,
216 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-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 Gregor030ff0c2008-10-31 20:25:05 +0000220
Fariborz Jahanian8bc3fa42009-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 Gregor64bffa92008-11-05 16:20:31 +0000225
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000226 // C++ [class]p4:
227 // A POD-struct is an aggregate class [...]
228 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000229
Fariborz Jahanian8bc3fa42009-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 Gregor1f2023a2009-07-22 18:25:24 +0000233 // FIXME: C++0x: don't do this for "= default" default constructors.
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000234 HasTrivialConstructor = false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000235
Fariborz Jahanian8bc3fa42009-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 Gregor1f2023a2009-07-22 18:25:24 +0000238 if (ConDecl->isCopyConstructor(Context)) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000239 UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-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 Gregor030ff0c2008-10-31 20:25:05 +0000246}
247
Sebastian Redl64b45f72009-01-05 20:52:13 +0000248void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
249 CXXMethodDecl *OpDecl) {
250 // We're interested specifically in copy assignment operators.
Douglas Gregor72564e72009-02-26 23:50:07 +0000251 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-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 Kremenek6217b802009-07-29 21:53:49 +0000255 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-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 Gregor1f2023a2009-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 Redl64b45f72009-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 Gregor2f1bc522008-11-07 20:08:42 +0000280void CXXRecordDecl::addConversionFunction(ASTContext &Context,
281 CXXConversionDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000282 assert(!ConvDecl->getDescribedFunctionTemplate() &&
283 "Conversion function templates should cast to FunctionTemplateDecl.");
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000284 Conversions.addOverload(ConvDecl);
285}
286
Douglas Gregor65ec1fd2009-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 Jahanianf8dcb862009-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;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000302 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-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 Carlsson7267c162009-05-29 21:03:38 +0000311const CXXDestructorDecl *
312CXXRecordDecl::getDestructor(ASTContext &Context) {
313 QualType ClassType = Context.getTypeDeclType(this);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000314
Anders Carlsson7267c162009-05-29 21:03:38 +0000315 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000316 = Context.DeclarationNames.getCXXDestructorName(
317 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000318
319 DeclContext::lookup_iterator I, E;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000320 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-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 Kremenek4b7c9832008-09-05 17:16:31 +0000329CXXMethodDecl *
330CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000331 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-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 Kremenek4b7c9832008-09-05 17:16:31 +0000336}
337
Anders Carlsson05eb2442009-05-16 23:58:37 +0000338
339typedef llvm::DenseMap<const CXXMethodDecl*,
340 std::vector<const CXXMethodDecl *> *>
341 OverriddenMethodsMapTy;
342
Mike Stumpb9871a22009-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 Carlsson05eb2442009-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 Dunbar0908c332009-08-01 23:40:20 +0000365 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000366 return 0;
Daniel Dunbar0908c332009-08-01 23:40:20 +0000367
Anders Carlsson05eb2442009-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 Dunbar0908c332009-08-01 23:40:20 +0000376 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000377 return 0;
378
Daniel Dunbar637ec322009-08-02 01:48:29 +0000379 return &(*it->second)[0] + it->second->size();
Anders Carlsson05eb2442009-05-16 23:58:37 +0000380}
381
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000382QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-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 Kremenek4b7c9832008-09-05 17:16:31 +0000389 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000390
391 QualType ClassTy;
392 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
393 ClassTy = TD->getInjectedClassNameType(C);
394 else
Mike Stumpe607ed02009-08-07 18:05:12 +0000395 ClassTy = C.getTagDeclType(getParent());
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000396 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
Anders Carlsson4e579922009-07-10 21:35:09 +0000397 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000398}
399
Douglas Gregor7ad83902008-11-05 04:29:56 +0000400CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000401CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000402 CXXConstructorDecl *C,
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000403 SourceLocation L)
404 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-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 Jahanian50b8eea2009-07-24 17:57:02 +0000411 // FIXME. Allocation via Context
412 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000413 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
414 this->Args[Idx] = Args[Idx];
415 }
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000416 CtorToCall = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000417}
418
419CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000420CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000421 CXXConstructorDecl *C,
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000422 SourceLocation L)
423 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-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 Jahanian50b8eea2009-07-24 17:57:02 +0000429 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000430 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
431 this->Args[Idx] = Args[Idx];
432 }
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000433 CtorToCall = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000434}
435
436CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
437 delete [] Args;
438}
439
Douglas Gregorb48fe382008-10-31 09:07:45 +0000440CXXConstructorDecl *
441CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000442 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000443 QualType T, DeclaratorInfo *DInfo,
444 bool isExplicit,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000445 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000446 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
447 "Name must refer to a constructor");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000448 return new (C) CXXConstructorDecl(RD, L, N, T, DInfo, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000449 isImplicitlyDeclared);
450}
451
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000452bool CXXConstructorDecl::isDefaultConstructor() const {
453 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-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 Gregor030ff0c2008-10-31 20:25:05 +0000456 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000457 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000458}
459
460bool
461CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
462 unsigned &TypeQuals) const {
463 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-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 Gregor030ff0c2008-10-31 20:25:05 +0000468 if ((getNumParams() < 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000469 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000470 return false;
471
472 const ParmVarDecl *Param = getParamDecl(0);
473
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000474 // Do we have a reference type? Rvalue references don't count.
475 const LValueReferenceType *ParamRefType =
Ted Kremenek6217b802009-07-29 21:53:49 +0000476 Param->getType()->getAs<LValueReferenceType>();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000477 if (!ParamRefType)
478 return false;
479
480 // Is it a reference to our class type?
Mike Stumpe607ed02009-08-07 18:05:12 +0000481 QualType PointeeType
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000482 = Context.getCanonicalType(ParamRefType->getPointeeType());
Mike Stumpe607ed02009-08-07 18:05:12 +0000483 QualType ClassTy = Context.getTagDeclType(getParent());
Douglas Gregor030ff0c2008-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
Douglas Gregor60d62c22008-10-31 16:23:19 +0000492bool CXXConstructorDecl::isConvertingConstructor() const {
493 // 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.
499 if (isExplicit())
500 return false;
501
502 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000503 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000504 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000505 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000506}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000507
Douglas Gregor42a552f2008-11-05 20:51:48 +0000508CXXDestructorDecl *
509CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000510 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000511 QualType T, bool isInline,
512 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000513 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
514 "Name must refer to a destructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000515 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
516 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000517}
518
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000519void
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000520CXXDestructorDecl::Destroy(ASTContext& C) {
521 C.Deallocate(BaseOrMemberDestructions);
522 CXXMethodDecl::Destroy(C);
523}
524
525void
526CXXDestructorDecl::computeBaseOrMembersToDestroy(ASTContext &C) {
Fariborz Jahanian560de452009-07-15 22:34:08 +0000527 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000528 llvm::SmallVector<uintptr_t, 32> AllToDestruct;
529
Fariborz Jahanian560de452009-07-15 22:34:08 +0000530 for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(),
531 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Fariborz Jahanianc87efbd2009-08-25 16:37:49 +0000532 if (VBase->getType()->isDependentType())
533 continue;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000534 // Skip over virtual bases which have trivial destructors.
535 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000536 = cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000537 if (BaseClassDecl->hasTrivialDestructor())
538 continue;
539 uintptr_t Member =
Fariborz Jahaniancf183122009-07-22 00:42:46 +0000540 reinterpret_cast<uintptr_t>(VBase->getType().getTypePtr()) | VBASE;
Fariborz Jahanian560de452009-07-15 22:34:08 +0000541 AllToDestruct.push_back(Member);
542 }
543 for (CXXRecordDecl::base_class_iterator Base =
544 ClassDecl->bases_begin(),
545 E = ClassDecl->bases_end(); Base != E; ++Base) {
546 if (Base->isVirtual())
547 continue;
Fariborz Jahanianc87efbd2009-08-25 16:37:49 +0000548 if (Base->getType()->isDependentType())
549 continue;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000550 // Skip over virtual bases which have trivial destructors.
551 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000552 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000553 if (BaseClassDecl->hasTrivialDestructor())
554 continue;
555
556 uintptr_t Member =
Fariborz Jahaniancf183122009-07-22 00:42:46 +0000557 reinterpret_cast<uintptr_t>(Base->getType().getTypePtr()) | DRCTNONVBASE;
Fariborz Jahanian560de452009-07-15 22:34:08 +0000558 AllToDestruct.push_back(Member);
559 }
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000560
Fariborz Jahanian560de452009-07-15 22:34:08 +0000561 // non-static data members.
562 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
563 E = ClassDecl->field_end(); Field != E; ++Field) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000564 QualType FieldType = C.getBaseElementType((*Field)->getType());
Fariborz Jahanian560de452009-07-15 22:34:08 +0000565
Ted Kremenek6217b802009-07-29 21:53:49 +0000566 if (const RecordType* RT = FieldType->getAs<RecordType>()) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000567 // Skip over virtual bases which have trivial destructors.
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000568 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000569 if (BaseClassDecl->hasTrivialDestructor())
570 continue;
571 uintptr_t Member = reinterpret_cast<uintptr_t>(*Field);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000572 AllToDestruct.push_back(Member);
573 }
574 }
575
576 unsigned NumDestructions = AllToDestruct.size();
577 if (NumDestructions > 0) {
578 NumBaseOrMemberDestructions = NumDestructions;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000579 BaseOrMemberDestructions = new (C) uintptr_t [NumDestructions];
Fariborz Jahanian560de452009-07-15 22:34:08 +0000580 // Insert in reverse order.
581 for (int Idx = NumDestructions-1, i=0 ; Idx >= 0; --Idx)
582 BaseOrMemberDestructions[i++] = AllToDestruct[Idx];
583 }
584}
585
586void
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000587CXXConstructorDecl::setBaseOrMemberInitializers(
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000588 ASTContext &C,
589 CXXBaseOrMemberInitializer **Initializers,
590 unsigned NumInitializers,
591 llvm::SmallVectorImpl<CXXBaseSpecifier *>& Bases,
592 llvm::SmallVectorImpl<FieldDecl *>&Fields) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000593 // We need to build the initializer AST according to order of construction
594 // and not what user specified in the Initializers list.
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000595 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
596 llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit;
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000597 llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields;
598
599 for (unsigned i = 0; i < NumInitializers; i++) {
600 CXXBaseOrMemberInitializer *Member = Initializers[i];
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000601 if (Member->isBaseInitializer())
Ted Kremenek6217b802009-07-29 21:53:49 +0000602 AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member;
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000603 else
Fariborz Jahanian8c64e002009-08-10 23:56:17 +0000604 AllBaseFields[Member->getMember()] = Member;
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000605 }
606
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000607 // Push virtual bases before others.
608 for (CXXRecordDecl::base_class_iterator VBase =
609 ClassDecl->vbases_begin(),
610 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Fariborz Jahanian77a2b4f2009-08-24 17:19:23 +0000611 if (VBase->getType()->isDependentType())
612 continue;
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000613 if (CXXBaseOrMemberInitializer *Value =
Ted Kremenek6217b802009-07-29 21:53:49 +0000614 AllBaseFields.lookup(VBase->getType()->getAs<RecordType>()))
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000615 AllToInit.push_back(Value);
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000616 else {
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000617 CXXRecordDecl *VBaseDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +0000618 cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000619 assert(VBaseDecl && "setBaseOrMemberInitializers - VBaseDecl null");
Fariborz Jahanian77a2b4f2009-08-24 17:19:23 +0000620 if (!VBaseDecl->getDefaultConstructor(C))
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000621 Bases.push_back(VBase);
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000622 CXXBaseOrMemberInitializer *Member =
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000623 new (C) CXXBaseOrMemberInitializer(VBase->getType(), 0, 0,
624 VBaseDecl->getDefaultConstructor(C),
625 SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000626 AllToInit.push_back(Member);
627 }
628 }
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000629
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000630 for (CXXRecordDecl::base_class_iterator Base =
631 ClassDecl->bases_begin(),
632 E = ClassDecl->bases_end(); Base != E; ++Base) {
633 // Virtuals are in the virtual base list and already constructed.
634 if (Base->isVirtual())
635 continue;
Fariborz Jahanian77a2b4f2009-08-24 17:19:23 +0000636 // Skip dependent types.
637 if (Base->getType()->isDependentType())
638 continue;
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000639 if (CXXBaseOrMemberInitializer *Value =
Ted Kremenek6217b802009-07-29 21:53:49 +0000640 AllBaseFields.lookup(Base->getType()->getAs<RecordType>()))
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000641 AllToInit.push_back(Value);
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000642 else {
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000643 CXXRecordDecl *BaseDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +0000644 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000645 assert(BaseDecl && "setBaseOrMemberInitializers - BaseDecl null");
Fariborz Jahanian77a2b4f2009-08-24 17:19:23 +0000646 if (!BaseDecl->getDefaultConstructor(C))
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000647 Bases.push_back(Base);
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000648 CXXBaseOrMemberInitializer *Member =
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000649 new (C) CXXBaseOrMemberInitializer(Base->getType(), 0, 0,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000650 BaseDecl->getDefaultConstructor(C),
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000651 SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000652 AllToInit.push_back(Member);
653 }
654 }
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000655
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000656 // non-static data members.
657 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
658 E = ClassDecl->field_end(); Field != E; ++Field) {
Fariborz Jahanian8c64e002009-08-10 23:56:17 +0000659 if ((*Field)->isAnonymousStructOrUnion()) {
660 if (const RecordType *FieldClassType =
661 Field->getType()->getAs<RecordType>()) {
662 CXXRecordDecl *FieldClassDecl
663 = cast<CXXRecordDecl>(FieldClassType->getDecl());
664 for(RecordDecl::field_iterator FA = FieldClassDecl->field_begin(),
665 EA = FieldClassDecl->field_end(); FA != EA; FA++) {
666 if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*FA)) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +0000667 // 'Member' is the anonymous union field and 'AnonUnionMember' is
668 // set to the anonymous union data member used in the initializer
669 // list.
670 Value->setMember(*Field);
671 Value->setAnonUnionMember(*FA);
Fariborz Jahanian8c64e002009-08-10 23:56:17 +0000672 AllToInit.push_back(Value);
673 break;
674 }
675 }
676 }
677 continue;
678 }
Fariborz Jahanian89350be2009-08-10 23:59:59 +0000679 if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*Field)) {
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000680 AllToInit.push_back(Value);
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000681 continue;
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000682 }
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000683
684 QualType FT = C.getBaseElementType((*Field)->getType());
Ted Kremenek6217b802009-07-29 21:53:49 +0000685 if (const RecordType* RT = FT->getAs<RecordType>()) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000686 CXXConstructorDecl *Ctor =
687 cast<CXXRecordDecl>(RT->getDecl())->getDefaultConstructor(C);
688 if (!Ctor && !FT->isDependentType())
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000689 Fields.push_back(*Field);
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000690 CXXBaseOrMemberInitializer *Member =
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000691 new (C) CXXBaseOrMemberInitializer((*Field), 0, 0,
692 Ctor,
693 SourceLocation());
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000694 AllToInit.push_back(Member);
695 }
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000696 }
Mike Stumpf1216772009-07-31 18:25:34 +0000697
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000698 NumInitializers = AllToInit.size();
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000699 if (NumInitializers > 0) {
700 NumBaseOrMemberInitializers = NumInitializers;
701 BaseOrMemberInitializers =
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000702 new (C) CXXBaseOrMemberInitializer*[NumInitializers];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000703 for (unsigned Idx = 0; Idx < NumInitializers; ++Idx)
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000704 BaseOrMemberInitializers[Idx] = AllToInit[Idx];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000705 }
706}
707
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000708void
709CXXConstructorDecl::Destroy(ASTContext& C) {
710 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000711 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000712}
713
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000714CXXConversionDecl *
715CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000716 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000717 QualType T, DeclaratorInfo *DInfo,
718 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000719 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
720 "Name must refer to a conversion function");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000721 return new (C) CXXConversionDecl(RD, L, N, T, DInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000722}
723
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000724OverloadedFunctionDecl *
725OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000726 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000727 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000728}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000729
Douglas Gregordec06662009-08-21 18:42:58 +0000730OverloadIterator::OverloadIterator(NamedDecl *ND) : D(0) {
731 if (!ND)
732 return;
733
734 if (isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND))
735 D = ND;
736 else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(ND)) {
737 D = ND;
738 Iter = Ovl->function_begin();
739 }
740}
741
Douglas Gregor364e0212009-06-27 21:05:07 +0000742void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
743 Functions.push_back(F);
744 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000745}
746
Douglas Gregordaa439a2009-07-08 10:57:20 +0000747OverloadIterator::reference OverloadIterator::operator*() const {
748 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
749 return FD;
750
751 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
752 return FTD;
753
754 assert(isa<OverloadedFunctionDecl>(D));
755 return *Iter;
756}
757
758OverloadIterator &OverloadIterator::operator++() {
759 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
760 D = 0;
761 return *this;
762 }
763
764 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
765 D = 0;
766
767 return *this;
768}
769
770bool OverloadIterator::Equals(const OverloadIterator &Other) const {
771 if (!D || !Other.D)
772 return D == Other.D;
773
774 if (D != Other.D)
775 return false;
776
777 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
778}
779
John McCallc48fbdf2009-08-11 21:13:21 +0000780FriendFunctionDecl *FriendFunctionDecl::Create(ASTContext &C,
781 DeclContext *DC,
John McCall3f9a8a62009-08-11 06:59:38 +0000782 SourceLocation L,
783 DeclarationName N, QualType T,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000784 DeclaratorInfo *DInfo,
John McCall3f9a8a62009-08-11 06:59:38 +0000785 bool isInline,
786 SourceLocation FriendL) {
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000787 return new (C) FriendFunctionDecl(DC, L, N, T, DInfo, isInline, FriendL);
John McCall3f9a8a62009-08-11 06:59:38 +0000788}
John McCallc48fbdf2009-08-11 21:13:21 +0000789
790FriendClassDecl *FriendClassDecl::Create(ASTContext &C, DeclContext *DC,
791 SourceLocation L, QualType T,
792 SourceLocation FriendL) {
793 return new (C) FriendClassDecl(DC, L, T, FriendL);
794}
John McCall3f9a8a62009-08-11 06:59:38 +0000795
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000796LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000797 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000798 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000799 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000800 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000801}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000802
803UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
804 SourceLocation L,
805 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000806 SourceRange QualifierRange,
807 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000808 SourceLocation IdentLoc,
809 NamespaceDecl *Used,
810 DeclContext *CommonAncestor) {
Douglas Gregor8419fa32009-05-30 06:31:56 +0000811 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
812 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000813}
814
Anders Carlsson68771c72009-03-28 22:58:02 +0000815NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
816 SourceLocation L,
817 SourceLocation AliasLoc,
818 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000819 SourceRange QualifierRange,
820 NestedNameSpecifier *Qualifier,
Anders Carlsson68771c72009-03-28 22:58:02 +0000821 SourceLocation IdentLoc,
822 NamedDecl *Namespace) {
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000823 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
824 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000825}
826
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000827UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
828 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
829 SourceLocation UL, NamedDecl* Target,
830 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
831 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
832 TargetNNS, IsTypeNameArg);
833}
834
Anders Carlssonfb311762009-03-14 00:25:26 +0000835StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
836 SourceLocation L, Expr *AssertExpr,
837 StringLiteral *Message) {
838 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
839}
840
841void StaticAssertDecl::Destroy(ASTContext& C) {
842 AssertExpr->Destroy(C);
843 Message->Destroy(C);
844 this->~StaticAssertDecl();
845 C.Deallocate((void *)this);
846}
847
848StaticAssertDecl::~StaticAssertDecl() {
849}
850
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000851static const char *getAccessName(AccessSpecifier AS) {
852 switch (AS) {
853 default:
854 case AS_none:
855 assert("Invalid access specifier!");
856 return 0;
857 case AS_public:
858 return "public";
859 case AS_private:
860 return "private";
861 case AS_protected:
862 return "protected";
863 }
864}
865
866const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
867 AccessSpecifier AS) {
868 return DB << getAccessName(AS);
869}
870
871