blob: 52619e9920780ba53e833632b52c2280045ef13a [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),
Anders Carlsson67e4dd22009-03-22 01:52:17 +000033 Aggregate(true), PlainOldData(true), Polymorphic(false), Abstract(false),
Douglas Gregor1f2023a2009-07-22 18:25:24 +000034 HasTrivialConstructor(true), HasTrivialCopyConstructor(true),
35 HasTrivialCopyAssignment(true), HasTrivialDestructor(true),
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000036 Bases(0), NumBases(0), VBases(0), NumVBases(0),
37 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
Sebastian Redl64b45f72009-01-05 20:52:13 +0000171bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context) const {
172 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
173 const_cast<CXXRecordDecl*>(this)));
174 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
175
176 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000177 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000178 Op != OpEnd; ++Op) {
179 // C++ [class.copy]p9:
180 // A user-declared copy assignment operator is a non-static non-template
181 // member function of class X with exactly one parameter of type X, X&,
182 // const X&, volatile X& or const volatile X&.
183 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
184 if (Method->isStatic())
185 continue;
186 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor72564e72009-02-26 23:50:07 +0000187 const FunctionProtoType *FnType =
188 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000189 assert(FnType && "Overloaded operator has no prototype.");
190 // Don't assert on this; an invalid decl might have been left in the AST.
191 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
192 continue;
193 bool AcceptsConst = true;
194 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000195 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000196 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000197 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000198 if (!ArgType.isConstQualified())
199 AcceptsConst = false;
200 }
201 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
202 continue;
203
204 // We have a single argument of type cv X or cv X&, i.e. we've found the
205 // copy assignment operator. Return whether it accepts const arguments.
206 return AcceptsConst;
207 }
208 assert(isInvalidDecl() &&
209 "No copy assignment operator declared in valid code.");
210 return false;
211}
212
213void
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000214CXXRecordDecl::addedConstructor(ASTContext &Context,
215 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000216 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
217 // Note that we have a user-declared constructor.
218 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000219
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000220 // C++ [dcl.init.aggr]p1:
221 // An aggregate is an array or a class (clause 9) with no
222 // user-declared constructors (12.1) [...].
223 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000224
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000225 // C++ [class]p4:
226 // A POD-struct is an aggregate class [...]
227 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000228
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000229 // C++ [class.ctor]p5:
230 // A constructor is trivial if it is an implicitly-declared default
231 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000232 // FIXME: C++0x: don't do this for "= default" default constructors.
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000233 HasTrivialConstructor = false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000234
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000235 // Note when we have a user-declared copy constructor, which will
236 // suppress the implicit declaration of a copy constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000237 if (ConDecl->isCopyConstructor(Context)) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000238 UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000239
240 // C++ [class.copy]p6:
241 // A copy constructor is trivial if it is implicitly declared.
242 // FIXME: C++0x: don't do this for "= default" copy constructors.
243 HasTrivialCopyConstructor = false;
244 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000245}
246
Sebastian Redl64b45f72009-01-05 20:52:13 +0000247void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
248 CXXMethodDecl *OpDecl) {
249 // We're interested specifically in copy assignment operators.
Douglas Gregor72564e72009-02-26 23:50:07 +0000250 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000251 assert(FnType && "Overloaded operator has no proto function type.");
252 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
253 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000254 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000255 ArgType = Ref->getPointeeType();
256
257 ArgType = ArgType.getUnqualifiedType();
258 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
259 const_cast<CXXRecordDecl*>(this)));
260
261 if (ClassType != Context.getCanonicalType(ArgType))
262 return;
263
264 // This is a copy assignment operator.
265 // Suppress the implicit declaration of a copy constructor.
266 UserDeclaredCopyAssignment = true;
267
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000268 // C++ [class.copy]p11:
269 // A copy assignment operator is trivial if it is implicitly declared.
270 // FIXME: C++0x: don't do this for "= default" copy operators.
271 HasTrivialCopyAssignment = false;
272
Sebastian Redl64b45f72009-01-05 20:52:13 +0000273 // C++ [class]p4:
274 // A POD-struct is an aggregate class that [...] has no user-defined copy
275 // assignment operator [...].
276 PlainOldData = false;
277}
278
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000279void CXXRecordDecl::addConversionFunction(ASTContext &Context,
280 CXXConversionDecl *ConvDecl) {
281 Conversions.addOverload(ConvDecl);
282}
283
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000284
285CXXConstructorDecl *
286CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
287 QualType ClassType = Context.getTypeDeclType(this);
288 DeclarationName ConstructorName
289 = Context.DeclarationNames.getCXXConstructorName(
290 Context.getCanonicalType(ClassType.getUnqualifiedType()));
291
292 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000293 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000294 Con != ConEnd; ++Con) {
295 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
296 if (Constructor->isDefaultConstructor())
297 return Constructor;
298 }
299 return 0;
300}
301
Anders Carlsson7267c162009-05-29 21:03:38 +0000302const CXXDestructorDecl *
303CXXRecordDecl::getDestructor(ASTContext &Context) {
304 QualType ClassType = Context.getTypeDeclType(this);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000305
Anders Carlsson7267c162009-05-29 21:03:38 +0000306 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000307 = Context.DeclarationNames.getCXXDestructorName(
308 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000309
310 DeclContext::lookup_iterator I, E;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000311 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000312 assert(I != E && "Did not find a destructor!");
313
314 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
315 assert(++I == E && "Found more than one destructor!");
316
317 return Dtor;
318}
319
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000320CXXMethodDecl *
321CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000322 SourceLocation L, DeclarationName N,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000323 QualType T, bool isStatic, bool isInline) {
Steve Naroff3e970492009-01-27 21:25:57 +0000324 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000325}
326
Anders Carlsson05eb2442009-05-16 23:58:37 +0000327
328typedef llvm::DenseMap<const CXXMethodDecl*,
329 std::vector<const CXXMethodDecl *> *>
330 OverriddenMethodsMapTy;
331
332static OverriddenMethodsMapTy *OverriddenMethods = 0;
333
334void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
335 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
336
337 if (!OverriddenMethods)
338 OverriddenMethods = new OverriddenMethodsMapTy();
339
340 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
341 if (!Methods)
342 Methods = new std::vector<const CXXMethodDecl *>;
343
344 Methods->push_back(MD);
345}
346
347CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
348 if (!OverriddenMethods)
349 return 0;
350
351 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000352 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000353 return 0;
Daniel Dunbar0908c332009-08-01 23:40:20 +0000354
Anders Carlsson05eb2442009-05-16 23:58:37 +0000355 return &(*it->second)[0];
356}
357
358CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
359 if (!OverriddenMethods)
360 return 0;
361
362 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000363 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000364 return 0;
365
Daniel Dunbar637ec322009-08-02 01:48:29 +0000366 return &(*it->second)[0] + it->second->size();
Anders Carlsson05eb2442009-05-16 23:58:37 +0000367}
368
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000369QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000370 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
371 // If the member function is declared const, the type of this is const X*,
372 // if the member function is declared volatile, the type of this is
373 // volatile X*, and if the member function is declared const volatile,
374 // the type of this is const volatile X*.
375
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000376 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000377
378 QualType ClassTy;
379 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
380 ClassTy = TD->getInjectedClassNameType(C);
381 else
Mike Stumpf1216772009-07-31 18:25:34 +0000382 // FIXME: What is the design on getTagDeclType when it requires casting
383 // away const? mutable?
Anders Carlsson31a08752009-06-13 02:59:33 +0000384 ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000385 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
Anders Carlsson4e579922009-07-10 21:35:09 +0000386 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000387}
388
Douglas Gregor7ad83902008-11-05 04:29:56 +0000389CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000390CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000391 CXXConstructorDecl *C,
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000392 SourceLocation L)
393 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000394 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
395 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
396 BaseOrMember |= 0x01;
397
398 if (NumArgs > 0) {
399 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000400 // FIXME. Allocation via Context
401 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000402 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
403 this->Args[Idx] = Args[Idx];
404 }
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000405 CtorToCall = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000406}
407
408CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000409CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000410 CXXConstructorDecl *C,
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000411 SourceLocation L)
412 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000413 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
414 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
415
416 if (NumArgs > 0) {
417 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000418 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000419 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
420 this->Args[Idx] = Args[Idx];
421 }
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000422 CtorToCall = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000423}
424
425CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
426 delete [] Args;
427}
428
Douglas Gregorb48fe382008-10-31 09:07:45 +0000429CXXConstructorDecl *
430CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000431 SourceLocation L, DeclarationName N,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000432 QualType T, bool isExplicit,
433 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000434 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
435 "Name must refer to a constructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000436 return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000437 isImplicitlyDeclared);
438}
439
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000440bool CXXConstructorDecl::isDefaultConstructor() const {
441 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000442 // A default constructor for a class X is a constructor of class
443 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000444 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000445 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000446}
447
448bool
449CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
450 unsigned &TypeQuals) const {
451 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000452 // A non-template constructor for class X is a copy constructor
453 // if its first parameter is of type X&, const X&, volatile X& or
454 // const volatile X&, and either there are no other parameters
455 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000456 if ((getNumParams() < 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000457 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000458 return false;
459
460 const ParmVarDecl *Param = getParamDecl(0);
461
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000462 // Do we have a reference type? Rvalue references don't count.
463 const LValueReferenceType *ParamRefType =
Ted Kremenek6217b802009-07-29 21:53:49 +0000464 Param->getType()->getAs<LValueReferenceType>();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000465 if (!ParamRefType)
466 return false;
467
468 // Is it a reference to our class type?
469 QualType PointeeType
470 = Context.getCanonicalType(ParamRefType->getPointeeType());
471 QualType ClassTy
472 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
473 if (PointeeType.getUnqualifiedType() != ClassTy)
474 return false;
475
476 // We have a copy constructor.
477 TypeQuals = PointeeType.getCVRQualifiers();
478 return true;
479}
480
Douglas Gregor60d62c22008-10-31 16:23:19 +0000481bool CXXConstructorDecl::isConvertingConstructor() const {
482 // C++ [class.conv.ctor]p1:
483 // A constructor declared without the function-specifier explicit
484 // that can be called with a single parameter specifies a
485 // conversion from the type of its first parameter to the type of
486 // its class. Such a constructor is called a converting
487 // constructor.
488 if (isExplicit())
489 return false;
490
491 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000492 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000493 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000494 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000495}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000496
Douglas Gregor42a552f2008-11-05 20:51:48 +0000497CXXDestructorDecl *
498CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000499 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000500 QualType T, bool isInline,
501 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000502 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
503 "Name must refer to a destructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000504 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
505 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000506}
507
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000508void
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000509CXXDestructorDecl::Destroy(ASTContext& C) {
510 C.Deallocate(BaseOrMemberDestructions);
511 CXXMethodDecl::Destroy(C);
512}
513
514void
515CXXDestructorDecl::computeBaseOrMembersToDestroy(ASTContext &C) {
Fariborz Jahanian560de452009-07-15 22:34:08 +0000516 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000517 llvm::SmallVector<uintptr_t, 32> AllToDestruct;
518
Fariborz Jahanian560de452009-07-15 22:34:08 +0000519 for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(),
520 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000521 // Skip over virtual bases which have trivial destructors.
522 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000523 = cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000524 if (BaseClassDecl->hasTrivialDestructor())
525 continue;
526 uintptr_t Member =
Fariborz Jahaniancf183122009-07-22 00:42:46 +0000527 reinterpret_cast<uintptr_t>(VBase->getType().getTypePtr()) | VBASE;
Fariborz Jahanian560de452009-07-15 22:34:08 +0000528 AllToDestruct.push_back(Member);
529 }
530 for (CXXRecordDecl::base_class_iterator Base =
531 ClassDecl->bases_begin(),
532 E = ClassDecl->bases_end(); Base != E; ++Base) {
533 if (Base->isVirtual())
534 continue;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000535 // Skip over virtual bases which have trivial destructors.
536 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000537 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000538 if (BaseClassDecl->hasTrivialDestructor())
539 continue;
540
541 uintptr_t Member =
Fariborz Jahaniancf183122009-07-22 00:42:46 +0000542 reinterpret_cast<uintptr_t>(Base->getType().getTypePtr()) | DRCTNONVBASE;
Fariborz Jahanian560de452009-07-15 22:34:08 +0000543 AllToDestruct.push_back(Member);
544 }
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000545
Fariborz Jahanian560de452009-07-15 22:34:08 +0000546 // non-static data members.
547 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
548 E = ClassDecl->field_end(); Field != E; ++Field) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000549 QualType FieldType = C.getBaseElementType((*Field)->getType());
Fariborz Jahanian560de452009-07-15 22:34:08 +0000550
Ted Kremenek6217b802009-07-29 21:53:49 +0000551 if (const RecordType* RT = FieldType->getAs<RecordType>()) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000552 // Skip over virtual bases which have trivial destructors.
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000553 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000554 if (BaseClassDecl->hasTrivialDestructor())
555 continue;
556 uintptr_t Member = reinterpret_cast<uintptr_t>(*Field);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000557 AllToDestruct.push_back(Member);
558 }
559 }
560
561 unsigned NumDestructions = AllToDestruct.size();
562 if (NumDestructions > 0) {
563 NumBaseOrMemberDestructions = NumDestructions;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000564 BaseOrMemberDestructions = new (C) uintptr_t [NumDestructions];
Fariborz Jahanian560de452009-07-15 22:34:08 +0000565 // Insert in reverse order.
566 for (int Idx = NumDestructions-1, i=0 ; Idx >= 0; --Idx)
567 BaseOrMemberDestructions[i++] = AllToDestruct[Idx];
568 }
569}
570
571void
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000572CXXConstructorDecl::setBaseOrMemberInitializers(
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000573 ASTContext &C,
574 CXXBaseOrMemberInitializer **Initializers,
575 unsigned NumInitializers,
576 llvm::SmallVectorImpl<CXXBaseSpecifier *>& Bases,
577 llvm::SmallVectorImpl<FieldDecl *>&Fields) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000578 // We need to build the initializer AST according to order of construction
579 // and not what user specified in the Initializers list.
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000580 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
581 llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit;
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000582 llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields;
583
584 for (unsigned i = 0; i < NumInitializers; i++) {
585 CXXBaseOrMemberInitializer *Member = Initializers[i];
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000586 if (Member->isBaseInitializer())
Ted Kremenek6217b802009-07-29 21:53:49 +0000587 AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member;
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000588 else
589 AllBaseFields[Member->getMember()] = Member;
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000590 }
591
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000592 // Push virtual bases before others.
593 for (CXXRecordDecl::base_class_iterator VBase =
594 ClassDecl->vbases_begin(),
595 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000596 if (CXXBaseOrMemberInitializer *Value =
Ted Kremenek6217b802009-07-29 21:53:49 +0000597 AllBaseFields.lookup(VBase->getType()->getAs<RecordType>()))
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000598 AllToInit.push_back(Value);
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000599 else {
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000600 CXXRecordDecl *VBaseDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +0000601 cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000602 assert(VBaseDecl && "setBaseOrMemberInitializers - VBaseDecl null");
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000603 if (!VBaseDecl->getDefaultConstructor(C) &&
604 !VBase->getType()->isDependentType())
605 Bases.push_back(VBase);
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000606 CXXBaseOrMemberInitializer *Member =
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000607 new (C) CXXBaseOrMemberInitializer(VBase->getType(), 0, 0,
608 VBaseDecl->getDefaultConstructor(C),
609 SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000610 AllToInit.push_back(Member);
611 }
612 }
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000613
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000614 for (CXXRecordDecl::base_class_iterator Base =
615 ClassDecl->bases_begin(),
616 E = ClassDecl->bases_end(); Base != E; ++Base) {
617 // Virtuals are in the virtual base list and already constructed.
618 if (Base->isVirtual())
619 continue;
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000620 if (CXXBaseOrMemberInitializer *Value =
Ted Kremenek6217b802009-07-29 21:53:49 +0000621 AllBaseFields.lookup(Base->getType()->getAs<RecordType>()))
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000622 AllToInit.push_back(Value);
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000623 else {
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000624 CXXRecordDecl *BaseDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +0000625 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000626 assert(BaseDecl && "setBaseOrMemberInitializers - BaseDecl null");
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000627 if (!BaseDecl->getDefaultConstructor(C) &&
628 !Base->getType()->isDependentType())
629 Bases.push_back(Base);
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000630 CXXBaseOrMemberInitializer *Member =
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000631 new (C) CXXBaseOrMemberInitializer(Base->getType(), 0, 0,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000632 BaseDecl->getDefaultConstructor(C),
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000633 SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000634 AllToInit.push_back(Member);
635 }
636 }
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000637
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000638 // non-static data members.
639 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
640 E = ClassDecl->field_end(); Field != E; ++Field) {
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000641 if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*Field)) {
642 AllToInit.push_back(Value);
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000643 continue;
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000644 }
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000645
646 QualType FT = C.getBaseElementType((*Field)->getType());
Ted Kremenek6217b802009-07-29 21:53:49 +0000647 if (const RecordType* RT = FT->getAs<RecordType>()) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000648 CXXConstructorDecl *Ctor =
649 cast<CXXRecordDecl>(RT->getDecl())->getDefaultConstructor(C);
650 if (!Ctor && !FT->isDependentType())
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000651 Fields.push_back(*Field);
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000652 CXXBaseOrMemberInitializer *Member =
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000653 new (C) CXXBaseOrMemberInitializer((*Field), 0, 0,
654 Ctor,
655 SourceLocation());
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000656 AllToInit.push_back(Member);
657 }
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000658 }
Mike Stumpf1216772009-07-31 18:25:34 +0000659
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000660 NumInitializers = AllToInit.size();
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000661 if (NumInitializers > 0) {
662 NumBaseOrMemberInitializers = NumInitializers;
663 BaseOrMemberInitializers =
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000664 new (C) CXXBaseOrMemberInitializer*[NumInitializers];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000665 for (unsigned Idx = 0; Idx < NumInitializers; ++Idx)
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000666 BaseOrMemberInitializers[Idx] = AllToInit[Idx];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000667 }
668}
669
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000670void
671CXXConstructorDecl::Destroy(ASTContext& C) {
672 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000673 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000674}
675
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000676CXXConversionDecl *
677CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000678 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000679 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000680 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
681 "Name must refer to a conversion function");
Steve Naroff3e970492009-01-27 21:25:57 +0000682 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000683}
684
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000685OverloadedFunctionDecl *
686OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000687 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000688 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000689}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000690
Douglas Gregor364e0212009-06-27 21:05:07 +0000691void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
692 Functions.push_back(F);
693 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000694}
695
Douglas Gregordaa439a2009-07-08 10:57:20 +0000696OverloadIterator::reference OverloadIterator::operator*() const {
697 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
698 return FD;
699
700 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
701 return FTD;
702
703 assert(isa<OverloadedFunctionDecl>(D));
704 return *Iter;
705}
706
707OverloadIterator &OverloadIterator::operator++() {
708 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
709 D = 0;
710 return *this;
711 }
712
713 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
714 D = 0;
715
716 return *this;
717}
718
719bool OverloadIterator::Equals(const OverloadIterator &Other) const {
720 if (!D || !Other.D)
721 return D == Other.D;
722
723 if (D != Other.D)
724 return false;
725
726 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
727}
728
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000729LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000730 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000731 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000732 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000733 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000734}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000735
736UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
737 SourceLocation L,
738 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000739 SourceRange QualifierRange,
740 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000741 SourceLocation IdentLoc,
742 NamespaceDecl *Used,
743 DeclContext *CommonAncestor) {
Douglas Gregor8419fa32009-05-30 06:31:56 +0000744 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
745 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000746}
747
Anders Carlsson68771c72009-03-28 22:58:02 +0000748NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
749 SourceLocation L,
750 SourceLocation AliasLoc,
751 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000752 SourceRange QualifierRange,
753 NestedNameSpecifier *Qualifier,
Anders Carlsson68771c72009-03-28 22:58:02 +0000754 SourceLocation IdentLoc,
755 NamedDecl *Namespace) {
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000756 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
757 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000758}
759
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000760UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
761 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
762 SourceLocation UL, NamedDecl* Target,
763 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
764 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
765 TargetNNS, IsTypeNameArg);
766}
767
Anders Carlssonfb311762009-03-14 00:25:26 +0000768StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
769 SourceLocation L, Expr *AssertExpr,
770 StringLiteral *Message) {
771 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
772}
773
774void StaticAssertDecl::Destroy(ASTContext& C) {
775 AssertExpr->Destroy(C);
776 Message->Destroy(C);
777 this->~StaticAssertDecl();
778 C.Deallocate((void *)this);
779}
780
781StaticAssertDecl::~StaticAssertDecl() {
782}
783
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000784static const char *getAccessName(AccessSpecifier AS) {
785 switch (AS) {
786 default:
787 case AS_none:
788 assert("Invalid access specifier!");
789 return 0;
790 case AS_public:
791 return "public";
792 case AS_private:
793 return "private";
794 case AS_protected:
795 return "protected";
796 }
797}
798
799const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
800 AccessSpecifier AS) {
801 return DB << getAccessName(AS);
802}
803
804