blob: 87550671379cc843222bf681b28faa0fdb2f49ea [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"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000020#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// Decl Allocation/Deallocation Method Implementations
25//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000026
Douglas Gregor3e00bad2009-02-17 01:05:43 +000027CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000028 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000029 CXXRecordDecl *PrevDecl,
Mike Stump1eb44332009-09-09 15:08:12 +000030 SourceLocation TKL)
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000031 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
Douglas Gregor7d7e6722008-11-12 23:21:09 +000032 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000033 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000034 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
35 Abstract(false), HasTrivialConstructor(true),
36 HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true),
Fariborz Jahanian62509212009-09-12 18:26:03 +000037 HasTrivialDestructor(true), ComputedVisibleConversions(false),
38 Bases(0), NumBases(0), VBases(0), NumVBases(0),
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000039 Conversions(DC, DeclarationName()),
Fariborz Jahanian53462782009-09-11 21:44:33 +000040 VisibleConversions(DC, DeclarationName()),
Douglas Gregord475b8d2009-03-25 21:17:03 +000041 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000042
Ted Kremenek4b7c9832008-09-05 17:16:31 +000043CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
44 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000045 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000046 CXXRecordDecl* PrevDecl,
47 bool DelayTypeCreation) {
Mike Stump1eb44332009-09-09 15:08:12 +000048 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000049 PrevDecl, TKL);
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000051 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000052 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000053 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000054 return R;
55}
56
Douglas Gregorf8268ae2008-10-22 17:49:05 +000057CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000058}
59
60void CXXRecordDecl::Destroy(ASTContext &C) {
61 C.Deallocate(Bases);
Fariborz Jahanian71c6e712009-07-22 17:41:53 +000062 C.Deallocate(VBases);
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000063 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000064}
65
Mike Stump1eb44332009-09-09 15:08:12 +000066void
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000067CXXRecordDecl::setBases(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +000068 CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000069 unsigned NumBases) {
Mike Stump1eb44332009-09-09 15:08:12 +000070 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000071 // An aggregate is an array or a class (clause 9) with [...]
72 // no base classes [...].
73 Aggregate = false;
74
Douglas Gregor57c856b2008-10-23 18:13:27 +000075 if (this->Bases)
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000076 C.Deallocate(this->Bases);
Mike Stump1eb44332009-09-09 15:08:12 +000077
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000078 int vbaseCount = 0;
79 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
80 bool hasDirectVirtualBase = false;
Mike Stump1eb44332009-09-09 15:08:12 +000081
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000082 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor57c856b2008-10-23 18:13:27 +000083 this->NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000084 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor57c856b2008-10-23 18:13:27 +000085 this->Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000086 // Keep track of inherited vbases for this base class.
87 const CXXBaseSpecifier *Base = Bases[i];
88 QualType BaseType = Base->getType();
Mike Stump1eb44332009-09-09 15:08:12 +000089 // Skip template types.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000090 // FIXME. This means that this list must be rebuilt during template
91 // instantiation.
92 if (BaseType->isDependentType())
93 continue;
94 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +000095 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000096 if (Base->isVirtual())
97 hasDirectVirtualBase = true;
Mike Stump1eb44332009-09-09 15:08:12 +000098 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000099 BaseClassDecl->vbases_begin(),
100 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Mike Stump1eb44332009-09-09 15:08:12 +0000101 // Add this vbase to the array of vbases for current class if it is
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000102 // not already in the list.
103 // FIXME. Note that we do a linear search as number of such classes are
104 // very few.
105 int i;
106 for (i = 0; i < vbaseCount; ++i)
107 if (UniqueVbases[i]->getType() == VBase->getType())
108 break;
109 if (i == vbaseCount) {
110 UniqueVbases.push_back(VBase);
111 ++vbaseCount;
112 }
113 }
114 }
115 if (hasDirectVirtualBase) {
116 // Iterate one more time through the direct bases and add the virtual
117 // base to the list of vritual bases for current class.
118 for (unsigned i = 0; i < NumBases; ++i) {
119 const CXXBaseSpecifier *VBase = Bases[i];
120 if (!VBase->isVirtual())
121 continue;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000122 int j;
123 for (j = 0; j < vbaseCount; ++j)
124 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000125 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000126 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000127 UniqueVbases.push_back(VBase);
128 ++vbaseCount;
129 }
130 }
131 }
132 if (vbaseCount > 0) {
133 // build AST for inhireted, direct or indirect, virtual bases.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000134 this->VBases = new (C) CXXBaseSpecifier [vbaseCount];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000135 this->NumVBases = vbaseCount;
136 for (int i = 0; i < vbaseCount; i++) {
137 QualType QT = UniqueVbases[i]->getType();
138 CXXRecordDecl *VBaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000139 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000140 this->VBases[i] =
Douglas Gregor2aef06d2009-07-22 20:55:49 +0000141 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
142 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
143 UniqueVbases[i]->getAccessSpecifier(), QT);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000144 }
145 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000146}
147
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000148bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000149 return getCopyConstructor(Context, QualType::Const) != 0;
150}
151
Mike Stump1eb44332009-09-09 15:08:12 +0000152CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000153 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000154 QualType ClassType
155 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000156 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000157 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000158 Context.getCanonicalType(ClassType));
159 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000160 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000161 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000162 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000163 // C++ [class.copy]p2:
164 // A non-template constructor for class X is a copy constructor if [...]
165 if (isa<FunctionTemplateDecl>(*Con))
166 continue;
167
Mike Stump1eb44332009-09-09 15:08:12 +0000168 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000169 FoundTQs)) {
170 if (((TypeQuals & QualType::Const) == (FoundTQs & QualType::Const)) ||
171 (!(TypeQuals & QualType::Const) && (FoundTQs & QualType::Const)))
172 return cast<CXXConstructorDecl>(*Con);
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000174 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000175 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000176 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000177}
178
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000179bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
180 const CXXMethodDecl *& MD) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000181 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
182 const_cast<CXXRecordDecl*>(this)));
183 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
184
185 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000186 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000187 Op != OpEnd; ++Op) {
188 // C++ [class.copy]p9:
189 // A user-declared copy assignment operator is a non-static non-template
190 // member function of class X with exactly one parameter of type X, X&,
191 // const X&, volatile X& or const volatile X&.
192 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
193 if (Method->isStatic())
194 continue;
195 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor72564e72009-02-26 23:50:07 +0000196 const FunctionProtoType *FnType =
197 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000198 assert(FnType && "Overloaded operator has no prototype.");
199 // Don't assert on this; an invalid decl might have been left in the AST.
200 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
201 continue;
202 bool AcceptsConst = true;
203 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000204 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000205 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000206 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000207 if (!ArgType.isConstQualified())
208 AcceptsConst = false;
209 }
210 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
211 continue;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000212 MD = Method;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000213 // We have a single argument of type cv X or cv X&, i.e. we've found the
214 // copy assignment operator. Return whether it accepts const arguments.
215 return AcceptsConst;
216 }
217 assert(isInvalidDecl() &&
218 "No copy assignment operator declared in valid code.");
219 return false;
220}
221
222void
Mike Stump1eb44332009-09-09 15:08:12 +0000223CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000224 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000225 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
226 // Note that we have a user-declared constructor.
227 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000228
Mike Stump1eb44332009-09-09 15:08:12 +0000229 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000230 // An aggregate is an array or a class (clause 9) with no
231 // user-declared constructors (12.1) [...].
232 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000233
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000234 // C++ [class]p4:
235 // A POD-struct is an aggregate class [...]
236 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000237
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000238 // C++ [class.ctor]p5:
239 // A constructor is trivial if it is an implicitly-declared default
240 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000241 // FIXME: C++0x: don't do this for "= default" default constructors.
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000242 HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000244 // Note when we have a user-declared copy constructor, which will
245 // suppress the implicit declaration of a copy constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000246 if (ConDecl->isCopyConstructor(Context)) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000247 UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000248
249 // C++ [class.copy]p6:
250 // A copy constructor is trivial if it is implicitly declared.
251 // FIXME: C++0x: don't do this for "= default" copy constructors.
252 HasTrivialCopyConstructor = false;
253 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000254}
255
Sebastian Redl64b45f72009-01-05 20:52:13 +0000256void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
257 CXXMethodDecl *OpDecl) {
258 // We're interested specifically in copy assignment operators.
Douglas Gregor72564e72009-02-26 23:50:07 +0000259 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000260 assert(FnType && "Overloaded operator has no proto function type.");
261 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
262 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000263 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000264 ArgType = Ref->getPointeeType();
265
266 ArgType = ArgType.getUnqualifiedType();
267 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
268 const_cast<CXXRecordDecl*>(this)));
269
270 if (ClassType != Context.getCanonicalType(ArgType))
271 return;
272
273 // This is a copy assignment operator.
274 // Suppress the implicit declaration of a copy constructor.
275 UserDeclaredCopyAssignment = true;
276
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000277 // C++ [class.copy]p11:
278 // A copy assignment operator is trivial if it is implicitly declared.
279 // FIXME: C++0x: don't do this for "= default" copy operators.
280 HasTrivialCopyAssignment = false;
281
Sebastian Redl64b45f72009-01-05 20:52:13 +0000282 // C++ [class]p4:
283 // A POD-struct is an aggregate class that [...] has no user-defined copy
284 // assignment operator [...].
285 PlainOldData = false;
286}
287
Fariborz Jahanian62509212009-09-12 18:26:03 +0000288/// getNestedVisibleConversionFunctions - imports unique conversion
289/// functions from base classes into the visible conversion function
290/// list of the class 'RD'. This is a private helper method.
291void
292CXXRecordDecl::getNestedVisibleConversionFunctions(CXXRecordDecl *RD) {
293 QualType ClassType = getASTContext().getTypeDeclType(this);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000294 if (const RecordType *Record = ClassType->getAs<RecordType>()) {
295 OverloadedFunctionDecl *Conversions
296 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000297 llvm::SmallPtrSet<QualType, 8> TopConversionsTypeSet;
298 bool inTopClass = (RD == this);
299 if (!inTopClass &&
300 (Conversions->function_begin() != Conversions->function_end())) {
301 // populate the TypeSet with the type of current class's conversions.
302 OverloadedFunctionDecl *TopConversions = RD->getConversionFunctions();
303 for (OverloadedFunctionDecl::function_iterator
304 TFunc = TopConversions->function_begin(),
305 TFuncEnd = TopConversions->function_end();
306 TFunc != TFuncEnd; ++TFunc) {
307 NamedDecl *TopConv = TFunc->get();
308 QualType TConvType;
309 if (FunctionTemplateDecl *TConversionTemplate =
310 dyn_cast<FunctionTemplateDecl>(TopConv))
311 TConvType =
312 getASTContext().getCanonicalType(
313 TConversionTemplate->getTemplatedDecl()->getType());
314 else
315 TConvType =
316 getASTContext().getCanonicalType(
317 cast<FunctionDecl>(TopConv)->getType());
318 TopConversionsTypeSet.insert(TConvType);
319 }
320 }
321
Fariborz Jahanian53462782009-09-11 21:44:33 +0000322 for (OverloadedFunctionDecl::function_iterator
323 Func = Conversions->function_begin(),
324 FuncEnd = Conversions->function_end();
325 Func != FuncEnd; ++Func) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000326 NamedDecl *Conv = Func->get();
Fariborz Jahanian53462782009-09-11 21:44:33 +0000327 // Only those conversions not exact match of conversions in current
328 // class are candidateconversion routines.
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +0000329 QualType ConvType;
330 if (FunctionTemplateDecl *ConversionTemplate =
331 dyn_cast<FunctionTemplateDecl>(Conv))
332 ConvType =
333 getASTContext().getCanonicalType(
334 ConversionTemplate->getTemplatedDecl()->getType());
335 else
336 ConvType =
337 getASTContext().getCanonicalType(cast<FunctionDecl>(Conv)->getType());
338 if (inTopClass || !TopConversionsTypeSet.count(ConvType)) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000339 if (FunctionTemplateDecl *ConversionTemplate =
340 dyn_cast<FunctionTemplateDecl>(Conv))
341 RD->addVisibleConversionFunction(ConversionTemplate);
342 else
343 RD->addVisibleConversionFunction(cast<CXXConversionDecl>(Conv));
Fariborz Jahanian53462782009-09-11 21:44:33 +0000344 }
345 }
346 }
347
348 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(),
349 E = vbases_end(); VBase != E; ++VBase) {
350 CXXRecordDecl *VBaseClassDecl
351 = cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian62509212009-09-12 18:26:03 +0000352 VBaseClassDecl->getNestedVisibleConversionFunctions(RD);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000353 }
354 for (CXXRecordDecl::base_class_iterator Base = bases_begin(),
355 E = bases_end(); Base != E; ++Base) {
356 if (Base->isVirtual())
357 continue;
358 CXXRecordDecl *BaseClassDecl
359 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian62509212009-09-12 18:26:03 +0000360 BaseClassDecl->getNestedVisibleConversionFunctions(RD);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000361 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000362}
363
364/// getVisibleConversionFunctions - get all conversion functions visible
365/// in current class; including conversion function templates.
366OverloadedFunctionDecl *
367CXXRecordDecl::getVisibleConversionFunctions() {
368 // If root class, all conversions are visible.
369 if (bases_begin() == bases_end())
370 return &Conversions;
371 // If visible conversion list is already evaluated, return it.
372 if (ComputedVisibleConversions)
373 return &VisibleConversions;
374 getNestedVisibleConversionFunctions(this);
375 ComputedVisibleConversions = true;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000376 return &VisibleConversions;
377}
378
Fariborz Jahanian62509212009-09-12 18:26:03 +0000379void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000380 CXXConversionDecl *ConvDecl) {
381 assert(!ConvDecl->getDescribedFunctionTemplate() &&
382 "Conversion function templates should cast to FunctionTemplateDecl.");
383 VisibleConversions.addOverload(ConvDecl);
384}
385
Fariborz Jahanian62509212009-09-12 18:26:03 +0000386void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000387 FunctionTemplateDecl *ConvDecl) {
388 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
389 "Function template is not a conversion function template");
390 VisibleConversions.addOverload(ConvDecl);
391}
392
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000393void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000394 assert(!ConvDecl->getDescribedFunctionTemplate() &&
395 "Conversion function templates should cast to FunctionTemplateDecl.");
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000396 Conversions.addOverload(ConvDecl);
397}
398
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000399void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000400 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
401 "Function template is not a conversion function template");
402 Conversions.addOverload(ConvDecl);
403}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000404
405CXXConstructorDecl *
406CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
407 QualType ClassType = Context.getTypeDeclType(this);
408 DeclarationName ConstructorName
409 = Context.DeclarationNames.getCXXConstructorName(
410 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000412 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000413 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000414 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000415 // FIXME: In C++0x, a constructor template can be a default constructor.
416 if (isa<FunctionTemplateDecl>(*Con))
417 continue;
418
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000419 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
420 if (Constructor->isDefaultConstructor())
421 return Constructor;
422 }
423 return 0;
424}
425
Anders Carlsson7267c162009-05-29 21:03:38 +0000426const CXXDestructorDecl *
427CXXRecordDecl::getDestructor(ASTContext &Context) {
428 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000429
430 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000431 = Context.DeclarationNames.getCXXDestructorName(
432 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000433
434 DeclContext::lookup_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000435 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000436 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Anders Carlsson7267c162009-05-29 21:03:38 +0000438 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
439 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Anders Carlsson7267c162009-05-29 21:03:38 +0000441 return Dtor;
442}
443
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000444CXXMethodDecl *
445CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000446 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000447 QualType T, DeclaratorInfo *DInfo,
448 bool isStatic, bool isInline) {
449 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, DInfo,
450 isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000451}
452
Mike Stump1eb44332009-09-09 15:08:12 +0000453typedef llvm::DenseMap<const CXXMethodDecl*,
454 std::vector<const CXXMethodDecl *> *>
Anders Carlsson05eb2442009-05-16 23:58:37 +0000455 OverriddenMethodsMapTy;
456
Mike Stumpb9871a22009-08-21 01:45:00 +0000457// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
458// the vtable building code uses it at CG time.
Anders Carlsson05eb2442009-05-16 23:58:37 +0000459static OverriddenMethodsMapTy *OverriddenMethods = 0;
460
461void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
462 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Anders Carlsson05eb2442009-05-16 23:58:37 +0000464 if (!OverriddenMethods)
465 OverriddenMethods = new OverriddenMethodsMapTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Anders Carlsson05eb2442009-05-16 23:58:37 +0000467 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
468 if (!Methods)
469 Methods = new std::vector<const CXXMethodDecl *>;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Anders Carlsson05eb2442009-05-16 23:58:37 +0000471 Methods->push_back(MD);
472}
473
474CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
475 if (!OverriddenMethods)
476 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Anders Carlsson05eb2442009-05-16 23:58:37 +0000478 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000479 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000480 return 0;
Daniel Dunbar0908c332009-08-01 23:40:20 +0000481
Anders Carlsson05eb2442009-05-16 23:58:37 +0000482 return &(*it->second)[0];
483}
484
485CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
486 if (!OverriddenMethods)
487 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Anders Carlsson05eb2442009-05-16 23:58:37 +0000489 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000490 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000491 return 0;
492
Daniel Dunbar637ec322009-08-02 01:48:29 +0000493 return &(*it->second)[0] + it->second->size();
Anders Carlsson05eb2442009-05-16 23:58:37 +0000494}
495
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000496QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000497 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
498 // If the member function is declared const, the type of this is const X*,
499 // if the member function is declared volatile, the type of this is
500 // volatile X*, and if the member function is declared const volatile,
501 // the type of this is const volatile X*.
502
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000503 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000504
505 QualType ClassTy;
506 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
507 ClassTy = TD->getInjectedClassNameType(C);
508 else
Mike Stumpe607ed02009-08-07 18:05:12 +0000509 ClassTy = C.getTagDeclType(getParent());
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000510 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
Anders Carlsson4e579922009-07-10 21:35:09 +0000511 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000512}
513
Douglas Gregor7ad83902008-11-05 04:29:56 +0000514CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000515CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000516 CXXConstructorDecl *C,
Mike Stump1eb44332009-09-09 15:08:12 +0000517 SourceLocation L, SourceLocation R)
Douglas Gregor72f6d672009-09-01 21:04:42 +0000518 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000519 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
520 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
521 BaseOrMember |= 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Douglas Gregor7ad83902008-11-05 04:29:56 +0000523 if (NumArgs > 0) {
524 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000525 // FIXME. Allocation via Context
526 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000527 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
528 this->Args[Idx] = Args[Idx];
529 }
Douglas Gregor72f6d672009-09-01 21:04:42 +0000530 CtorOrAnonUnion = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000531}
532
533CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000534CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000535 CXXConstructorDecl *C,
Anders Carlsson8c57a662009-08-29 01:31:33 +0000536 SourceLocation L, SourceLocation R)
Douglas Gregor72f6d672009-09-01 21:04:42 +0000537 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000538 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
Mike Stump1eb44332009-09-09 15:08:12 +0000539 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
Douglas Gregor7ad83902008-11-05 04:29:56 +0000540
541 if (NumArgs > 0) {
542 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000543 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000544 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
545 this->Args[Idx] = Args[Idx];
546 }
Douglas Gregor72f6d672009-09-01 21:04:42 +0000547 CtorOrAnonUnion = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000548}
549
550CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
551 delete [] Args;
552}
553
Douglas Gregorb48fe382008-10-31 09:07:45 +0000554CXXConstructorDecl *
555CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000556 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000557 QualType T, DeclaratorInfo *DInfo,
558 bool isExplicit,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000559 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000560 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
561 "Name must refer to a constructor");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000562 return new (C) CXXConstructorDecl(RD, L, N, T, DInfo, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000563 isImplicitlyDeclared);
564}
565
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000566bool CXXConstructorDecl::isDefaultConstructor() const {
567 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000568 // A default constructor for a class X is a constructor of class
569 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000570 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000571 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000572}
573
Mike Stump1eb44332009-09-09 15:08:12 +0000574bool
575CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000576 unsigned &TypeQuals) const {
577 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000578 // A non-template constructor for class X is a copy constructor
579 // if its first parameter is of type X&, const X&, volatile X& or
580 // const volatile X&, and either there are no other parameters
581 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000582 if ((getNumParams() < 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000583 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000584 return false;
585
586 const ParmVarDecl *Param = getParamDecl(0);
587
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000588 // Do we have a reference type? Rvalue references don't count.
589 const LValueReferenceType *ParamRefType =
Ted Kremenek6217b802009-07-29 21:53:49 +0000590 Param->getType()->getAs<LValueReferenceType>();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000591 if (!ParamRefType)
592 return false;
593
594 // Is it a reference to our class type?
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000595 CanQualType PointeeType
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000596 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000597 CanQualType ClassTy
598 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000599 if (PointeeType.getUnqualifiedType() != ClassTy)
600 return false;
601
602 // We have a copy constructor.
603 TypeQuals = PointeeType.getCVRQualifiers();
604 return true;
605}
606
Anders Carlssonfaccd722009-08-28 16:57:08 +0000607bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000608 // C++ [class.conv.ctor]p1:
609 // A constructor declared without the function-specifier explicit
610 // that can be called with a single parameter specifies a
611 // conversion from the type of its first parameter to the type of
612 // its class. Such a constructor is called a converting
613 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000614 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000615 return false;
616
Mike Stump1eb44332009-09-09 15:08:12 +0000617 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000618 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000619 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000620 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000621}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000622
Douglas Gregor42a552f2008-11-05 20:51:48 +0000623CXXDestructorDecl *
624CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000625 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000626 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000627 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000628 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
629 "Name must refer to a destructor");
Mike Stump1eb44332009-09-09 15:08:12 +0000630 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff3e970492009-01-27 21:25:57 +0000631 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000632}
633
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000634void
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000635CXXDestructorDecl::Destroy(ASTContext& C) {
636 C.Deallocate(BaseOrMemberDestructions);
637 CXXMethodDecl::Destroy(C);
638}
639
640void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000641CXXConstructorDecl::Destroy(ASTContext& C) {
642 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000643 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000644}
645
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000646CXXConversionDecl *
647CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000648 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000649 QualType T, DeclaratorInfo *DInfo,
650 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000651 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
652 "Name must refer to a conversion function");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000653 return new (C) CXXConversionDecl(RD, L, N, T, DInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000654}
655
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000656OverloadedFunctionDecl *
657OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000658 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000659 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000660}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000661
Douglas Gregordec06662009-08-21 18:42:58 +0000662OverloadIterator::OverloadIterator(NamedDecl *ND) : D(0) {
663 if (!ND)
664 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Douglas Gregordec06662009-08-21 18:42:58 +0000666 if (isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND))
667 D = ND;
668 else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(ND)) {
Douglas Gregor8f1d89e2009-09-01 16:58:52 +0000669 if (Ovl->size() != 0) {
670 D = ND;
671 Iter = Ovl->function_begin();
672 }
Douglas Gregordec06662009-08-21 18:42:58 +0000673 }
674}
675
Douglas Gregor364e0212009-06-27 21:05:07 +0000676void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
677 Functions.push_back(F);
678 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000679}
680
Douglas Gregordaa439a2009-07-08 10:57:20 +0000681OverloadIterator::reference OverloadIterator::operator*() const {
682 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
683 return FD;
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Douglas Gregordaa439a2009-07-08 10:57:20 +0000685 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
686 return FTD;
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Douglas Gregordaa439a2009-07-08 10:57:20 +0000688 assert(isa<OverloadedFunctionDecl>(D));
689 return *Iter;
690}
691
692OverloadIterator &OverloadIterator::operator++() {
693 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
694 D = 0;
695 return *this;
696 }
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Douglas Gregordaa439a2009-07-08 10:57:20 +0000698 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
699 D = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Douglas Gregordaa439a2009-07-08 10:57:20 +0000701 return *this;
702}
703
704bool OverloadIterator::Equals(const OverloadIterator &Other) const {
705 if (!D || !Other.D)
706 return D == Other.D;
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Douglas Gregordaa439a2009-07-08 10:57:20 +0000708 if (D != Other.D)
709 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Douglas Gregordaa439a2009-07-08 10:57:20 +0000711 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
712}
713
John McCall02cace72009-08-28 07:59:38 +0000714FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
715 SourceLocation L,
716 FriendUnion Friend,
717 SourceLocation FriendL) {
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000718#ifndef NDEBUG
John McCall02cace72009-08-28 07:59:38 +0000719 if (Friend.is<NamedDecl*>()) {
720 NamedDecl *D = Friend.get<NamedDecl*>();
721 assert(isa<FunctionDecl>(D) ||
722 isa<CXXRecordDecl>(D) ||
723 isa<FunctionTemplateDecl>(D) ||
724 isa<ClassTemplateDecl>(D));
725 assert(D->getFriendObjectKind());
726 }
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000727#endif
John McCallc48fbdf2009-08-11 21:13:21 +0000728
John McCall02cace72009-08-28 07:59:38 +0000729 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump1eb44332009-09-09 15:08:12 +0000730}
John McCall3f9a8a62009-08-11 06:59:38 +0000731
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000732LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000733 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000734 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000735 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000736 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000737}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000738
739UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
740 SourceLocation L,
741 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000742 SourceRange QualifierRange,
743 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000744 SourceLocation IdentLoc,
745 NamespaceDecl *Used,
746 DeclContext *CommonAncestor) {
Mike Stump1eb44332009-09-09 15:08:12 +0000747 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000748 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000749}
750
Mike Stump1eb44332009-09-09 15:08:12 +0000751NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
752 SourceLocation L,
753 SourceLocation AliasLoc,
754 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000755 SourceRange QualifierRange,
756 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +0000757 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +0000758 NamedDecl *Namespace) {
Mike Stump1eb44332009-09-09 15:08:12 +0000759 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000760 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000761}
762
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000763UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
764 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
765 SourceLocation UL, NamedDecl* Target,
766 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
767 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
768 TargetNNS, IsTypeNameArg);
769}
770
Anders Carlsson665b49c2009-08-28 05:30:28 +0000771UnresolvedUsingDecl *UnresolvedUsingDecl::Create(ASTContext &C, DeclContext *DC,
772 SourceLocation UsingLoc,
773 SourceRange TargetNNR,
774 NestedNameSpecifier *TargetNNS,
775 SourceLocation TargetNameLoc,
776 DeclarationName TargetName,
777 bool IsTypeNameArg) {
778 return new (C) UnresolvedUsingDecl(DC, UsingLoc, TargetNNR, TargetNNS,
779 TargetNameLoc, TargetName, IsTypeNameArg);
780}
781
Anders Carlssonfb311762009-03-14 00:25:26 +0000782StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
783 SourceLocation L, Expr *AssertExpr,
784 StringLiteral *Message) {
785 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
786}
787
788void StaticAssertDecl::Destroy(ASTContext& C) {
789 AssertExpr->Destroy(C);
790 Message->Destroy(C);
791 this->~StaticAssertDecl();
792 C.Deallocate((void *)this);
793}
794
795StaticAssertDecl::~StaticAssertDecl() {
796}
797
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000798static const char *getAccessName(AccessSpecifier AS) {
799 switch (AS) {
800 default:
801 case AS_none:
802 assert("Invalid access specifier!");
803 return 0;
804 case AS_public:
805 return "public";
806 case AS_private:
807 return "private";
808 case AS_protected:
809 return "protected";
810 }
811}
812
813const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
814 AccessSpecifier AS) {
815 return DB << getAccessName(AS);
816}
817
818