blob: abb55057312d6eaa50500724ae4668cc02c66b9c [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,
Mike Stump1eb44332009-09-09 15:08:12 +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),
Fariborz Jahanian62509212009-09-12 18:26:03 +000036 HasTrivialDestructor(true), ComputedVisibleConversions(false),
37 Bases(0), NumBases(0), VBases(0), NumVBases(0),
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000038 Conversions(DC, DeclarationName()),
Fariborz Jahanian53462782009-09-11 21:44:33 +000039 VisibleConversions(DC, DeclarationName()),
Douglas Gregord475b8d2009-03-25 21:17:03 +000040 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000041
Ted Kremenek4b7c9832008-09-05 17:16:31 +000042CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
43 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000044 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000045 CXXRecordDecl* PrevDecl,
46 bool DelayTypeCreation) {
Mike Stump1eb44332009-09-09 15:08:12 +000047 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000048 PrevDecl, TKL);
Mike Stump1eb44332009-09-09 15:08:12 +000049
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000050 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000051 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000052 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000053 return R;
54}
55
Douglas Gregorf8268ae2008-10-22 17:49:05 +000056CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000057}
58
59void CXXRecordDecl::Destroy(ASTContext &C) {
60 C.Deallocate(Bases);
Fariborz Jahanian71c6e712009-07-22 17:41:53 +000061 C.Deallocate(VBases);
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000062 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000063}
64
Mike Stump1eb44332009-09-09 15:08:12 +000065void
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000066CXXRecordDecl::setBases(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +000067 CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000068 unsigned NumBases) {
Mike Stump1eb44332009-09-09 15:08:12 +000069 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000070 // An aggregate is an array or a class (clause 9) with [...]
71 // no base classes [...].
72 Aggregate = false;
73
Douglas Gregor57c856b2008-10-23 18:13:27 +000074 if (this->Bases)
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000075 C.Deallocate(this->Bases);
Mike Stump1eb44332009-09-09 15:08:12 +000076
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000077 int vbaseCount = 0;
78 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
79 bool hasDirectVirtualBase = false;
Mike Stump1eb44332009-09-09 15:08:12 +000080
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000081 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor57c856b2008-10-23 18:13:27 +000082 this->NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000083 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor57c856b2008-10-23 18:13:27 +000084 this->Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000085 // Keep track of inherited vbases for this base class.
86 const CXXBaseSpecifier *Base = Bases[i];
87 QualType BaseType = Base->getType();
Mike Stump1eb44332009-09-09 15:08:12 +000088 // Skip template types.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000089 // FIXME. This means that this list must be rebuilt during template
90 // instantiation.
91 if (BaseType->isDependentType())
92 continue;
93 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +000094 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000095 if (Base->isVirtual())
96 hasDirectVirtualBase = true;
Mike Stump1eb44332009-09-09 15:08:12 +000097 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000098 BaseClassDecl->vbases_begin(),
99 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Mike Stump1eb44332009-09-09 15:08:12 +0000100 // Add this vbase to the array of vbases for current class if it is
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000101 // not already in the list.
102 // FIXME. Note that we do a linear search as number of such classes are
103 // very few.
104 int i;
105 for (i = 0; i < vbaseCount; ++i)
106 if (UniqueVbases[i]->getType() == VBase->getType())
107 break;
108 if (i == vbaseCount) {
109 UniqueVbases.push_back(VBase);
110 ++vbaseCount;
111 }
112 }
113 }
114 if (hasDirectVirtualBase) {
115 // Iterate one more time through the direct bases and add the virtual
116 // base to the list of vritual bases for current class.
117 for (unsigned i = 0; i < NumBases; ++i) {
118 const CXXBaseSpecifier *VBase = Bases[i];
119 if (!VBase->isVirtual())
120 continue;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000121 int j;
122 for (j = 0; j < vbaseCount; ++j)
123 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000124 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000125 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000126 UniqueVbases.push_back(VBase);
127 ++vbaseCount;
128 }
129 }
130 }
131 if (vbaseCount > 0) {
132 // build AST for inhireted, direct or indirect, virtual bases.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000133 this->VBases = new (C) CXXBaseSpecifier [vbaseCount];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000134 this->NumVBases = vbaseCount;
135 for (int i = 0; i < vbaseCount; i++) {
136 QualType QT = UniqueVbases[i]->getType();
137 CXXRecordDecl *VBaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000138 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000139 this->VBases[i] =
Douglas Gregor2aef06d2009-07-22 20:55:49 +0000140 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
141 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
142 UniqueVbases[i]->getAccessSpecifier(), QT);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000143 }
144 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000145}
146
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000147bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000148 return getCopyConstructor(Context, QualType::Const) != 0;
149}
150
Mike Stump1eb44332009-09-09 15:08:12 +0000151CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000152 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000153 QualType ClassType
154 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000155 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000156 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000157 Context.getCanonicalType(ClassType));
158 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000159 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000160 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000161 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000162 // C++ [class.copy]p2:
163 // A non-template constructor for class X is a copy constructor if [...]
164 if (isa<FunctionTemplateDecl>(*Con))
165 continue;
166
Mike Stump1eb44332009-09-09 15:08:12 +0000167 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000168 FoundTQs)) {
169 if (((TypeQuals & QualType::Const) == (FoundTQs & QualType::Const)) ||
170 (!(TypeQuals & QualType::Const) && (FoundTQs & QualType::Const)))
171 return cast<CXXConstructorDecl>(*Con);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000173 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000174 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000175 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000176}
177
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000178bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
179 const CXXMethodDecl *& MD) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000180 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
181 const_cast<CXXRecordDecl*>(this)));
182 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
183
184 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000185 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000186 Op != OpEnd; ++Op) {
187 // C++ [class.copy]p9:
188 // A user-declared copy assignment operator is a non-static non-template
189 // member function of class X with exactly one parameter of type X, X&,
190 // const X&, volatile X& or const volatile X&.
191 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
192 if (Method->isStatic())
193 continue;
194 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor72564e72009-02-26 23:50:07 +0000195 const FunctionProtoType *FnType =
196 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000197 assert(FnType && "Overloaded operator has no prototype.");
198 // Don't assert on this; an invalid decl might have been left in the AST.
199 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
200 continue;
201 bool AcceptsConst = true;
202 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000203 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000204 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000205 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000206 if (!ArgType.isConstQualified())
207 AcceptsConst = false;
208 }
209 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
210 continue;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +0000211 MD = Method;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000212 // We have a single argument of type cv X or cv X&, i.e. we've found the
213 // copy assignment operator. Return whether it accepts const arguments.
214 return AcceptsConst;
215 }
216 assert(isInvalidDecl() &&
217 "No copy assignment operator declared in valid code.");
218 return false;
219}
220
221void
Mike Stump1eb44332009-09-09 15:08:12 +0000222CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000223 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000224 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
225 // Note that we have a user-declared constructor.
226 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000227
Mike Stump1eb44332009-09-09 15:08:12 +0000228 // C++ [dcl.init.aggr]p1:
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000229 // An aggregate is an array or a class (clause 9) with no
230 // user-declared constructors (12.1) [...].
231 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000232
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000233 // C++ [class]p4:
234 // A POD-struct is an aggregate class [...]
235 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000236
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000237 // C++ [class.ctor]p5:
238 // A constructor is trivial if it is an implicitly-declared default
239 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000240 // FIXME: C++0x: don't do this for "= default" default constructors.
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000241 HasTrivialConstructor = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000243 // Note when we have a user-declared copy constructor, which will
244 // suppress the implicit declaration of a copy constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000245 if (ConDecl->isCopyConstructor(Context)) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000246 UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000247
248 // C++ [class.copy]p6:
249 // A copy constructor is trivial if it is implicitly declared.
250 // FIXME: C++0x: don't do this for "= default" copy constructors.
251 HasTrivialCopyConstructor = false;
252 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000253}
254
Sebastian Redl64b45f72009-01-05 20:52:13 +0000255void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
256 CXXMethodDecl *OpDecl) {
257 // We're interested specifically in copy assignment operators.
Douglas Gregor72564e72009-02-26 23:50:07 +0000258 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000259 assert(FnType && "Overloaded operator has no proto function type.");
260 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
261 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000262 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000263 ArgType = Ref->getPointeeType();
264
265 ArgType = ArgType.getUnqualifiedType();
266 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
267 const_cast<CXXRecordDecl*>(this)));
268
269 if (ClassType != Context.getCanonicalType(ArgType))
270 return;
271
272 // This is a copy assignment operator.
273 // Suppress the implicit declaration of a copy constructor.
274 UserDeclaredCopyAssignment = true;
275
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000276 // C++ [class.copy]p11:
277 // A copy assignment operator is trivial if it is implicitly declared.
278 // FIXME: C++0x: don't do this for "= default" copy operators.
279 HasTrivialCopyAssignment = false;
280
Sebastian Redl64b45f72009-01-05 20:52:13 +0000281 // C++ [class]p4:
282 // A POD-struct is an aggregate class that [...] has no user-defined copy
283 // assignment operator [...].
284 PlainOldData = false;
285}
286
Fariborz Jahanian62509212009-09-12 18:26:03 +0000287/// getNestedVisibleConversionFunctions - imports unique conversion
288/// functions from base classes into the visible conversion function
289/// list of the class 'RD'. This is a private helper method.
290void
291CXXRecordDecl::getNestedVisibleConversionFunctions(CXXRecordDecl *RD) {
292 QualType ClassType = getASTContext().getTypeDeclType(this);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000293 if (const RecordType *Record = ClassType->getAs<RecordType>()) {
294 OverloadedFunctionDecl *Conversions
295 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions();
296 for (OverloadedFunctionDecl::function_iterator
297 Func = Conversions->function_begin(),
298 FuncEnd = Conversions->function_end();
299 Func != FuncEnd; ++Func) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000300 NamedDecl *Conv = Func->get();
Fariborz Jahanian53462782009-09-11 21:44:33 +0000301 bool Candidate = true;
302 // Only those conversions not exact match of conversions in current
303 // class are candidateconversion routines.
Fariborz Jahanian62509212009-09-12 18:26:03 +0000304 // FIXME. This is a O(n^2) algorithm.
Fariborz Jahanian53462782009-09-11 21:44:33 +0000305 if (RD != this) {
306 OverloadedFunctionDecl *TopConversions = RD->getConversionFunctions();
Fariborz Jahanian62509212009-09-12 18:26:03 +0000307 QualType ConvType;
308 FunctionDecl *FD;
309 if (FunctionTemplateDecl *ConversionTemplate =
310 dyn_cast<FunctionTemplateDecl>(Conv))
311 FD = ConversionTemplate->getTemplatedDecl();
312 else
313 FD = cast<FunctionDecl>(Conv);
314 ConvType = getASTContext().getCanonicalType(FD->getType());
315
Fariborz Jahanian53462782009-09-11 21:44:33 +0000316 for (OverloadedFunctionDecl::function_iterator
317 TFunc = TopConversions->function_begin(),
318 TFuncEnd = TopConversions->function_end();
319 TFunc != TFuncEnd; ++TFunc) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000320
321 NamedDecl *TopConv = TFunc->get();
322 FunctionDecl *TFD;
323 QualType TConvType;
324 if (FunctionTemplateDecl *TConversionTemplate =
325 dyn_cast<FunctionTemplateDecl>(TopConv))
326 TFD = TConversionTemplate->getTemplatedDecl();
327 else
328 TFD = cast<FunctionDecl>(TopConv);
329 TConvType = getASTContext().getCanonicalType(TFD->getType());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000330 if (ConvType == TConvType) {
331 Candidate = false;
332 break;
333 }
334 }
335 }
336 if (Candidate) {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000337 if (FunctionTemplateDecl *ConversionTemplate =
338 dyn_cast<FunctionTemplateDecl>(Conv))
339 RD->addVisibleConversionFunction(ConversionTemplate);
340 else
341 RD->addVisibleConversionFunction(cast<CXXConversionDecl>(Conv));
Fariborz Jahanian53462782009-09-11 21:44:33 +0000342 }
343 }
344 }
345
346 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(),
347 E = vbases_end(); VBase != E; ++VBase) {
348 CXXRecordDecl *VBaseClassDecl
349 = cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian62509212009-09-12 18:26:03 +0000350 VBaseClassDecl->getNestedVisibleConversionFunctions(RD);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000351 }
352 for (CXXRecordDecl::base_class_iterator Base = bases_begin(),
353 E = bases_end(); Base != E; ++Base) {
354 if (Base->isVirtual())
355 continue;
356 CXXRecordDecl *BaseClassDecl
357 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian62509212009-09-12 18:26:03 +0000358 BaseClassDecl->getNestedVisibleConversionFunctions(RD);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000359 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000360}
361
362/// getVisibleConversionFunctions - get all conversion functions visible
363/// in current class; including conversion function templates.
364OverloadedFunctionDecl *
365CXXRecordDecl::getVisibleConversionFunctions() {
366 // If root class, all conversions are visible.
367 if (bases_begin() == bases_end())
368 return &Conversions;
369 // If visible conversion list is already evaluated, return it.
370 if (ComputedVisibleConversions)
371 return &VisibleConversions;
372 getNestedVisibleConversionFunctions(this);
373 ComputedVisibleConversions = true;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000374 return &VisibleConversions;
375}
376
Fariborz Jahanian62509212009-09-12 18:26:03 +0000377void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000378 CXXConversionDecl *ConvDecl) {
379 assert(!ConvDecl->getDescribedFunctionTemplate() &&
380 "Conversion function templates should cast to FunctionTemplateDecl.");
381 VisibleConversions.addOverload(ConvDecl);
382}
383
Fariborz Jahanian62509212009-09-12 18:26:03 +0000384void CXXRecordDecl::addVisibleConversionFunction(
Fariborz Jahanian53462782009-09-11 21:44:33 +0000385 FunctionTemplateDecl *ConvDecl) {
386 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
387 "Function template is not a conversion function template");
388 VisibleConversions.addOverload(ConvDecl);
389}
390
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000391void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000392 assert(!ConvDecl->getDescribedFunctionTemplate() &&
393 "Conversion function templates should cast to FunctionTemplateDecl.");
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000394 Conversions.addOverload(ConvDecl);
395}
396
Fariborz Jahaniandebc6292009-09-12 19:02:34 +0000397void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000398 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) &&
399 "Function template is not a conversion function template");
400 Conversions.addOverload(ConvDecl);
401}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000402
403CXXConstructorDecl *
404CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
405 QualType ClassType = Context.getTypeDeclType(this);
406 DeclarationName ConstructorName
407 = Context.DeclarationNames.getCXXConstructorName(
408 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000410 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000411 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000412 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000413 // FIXME: In C++0x, a constructor template can be a default constructor.
414 if (isa<FunctionTemplateDecl>(*Con))
415 continue;
416
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000417 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
418 if (Constructor->isDefaultConstructor())
419 return Constructor;
420 }
421 return 0;
422}
423
Anders Carlsson7267c162009-05-29 21:03:38 +0000424const CXXDestructorDecl *
425CXXRecordDecl::getDestructor(ASTContext &Context) {
426 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000427
428 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000429 = Context.DeclarationNames.getCXXDestructorName(
430 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000431
432 DeclContext::lookup_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000433 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000434 assert(I != E && "Did not find a destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Anders Carlsson7267c162009-05-29 21:03:38 +0000436 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
437 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Anders Carlsson7267c162009-05-29 21:03:38 +0000439 return Dtor;
440}
441
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000442CXXMethodDecl *
443CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000444 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000445 QualType T, DeclaratorInfo *DInfo,
446 bool isStatic, bool isInline) {
447 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, DInfo,
448 isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000449}
450
Mike Stump1eb44332009-09-09 15:08:12 +0000451typedef llvm::DenseMap<const CXXMethodDecl*,
452 std::vector<const CXXMethodDecl *> *>
Anders Carlsson05eb2442009-05-16 23:58:37 +0000453 OverriddenMethodsMapTy;
454
Mike Stumpb9871a22009-08-21 01:45:00 +0000455// FIXME: We hate static data. This doesn't survive PCH saving/loading, and
456// the vtable building code uses it at CG time.
Anders Carlsson05eb2442009-05-16 23:58:37 +0000457static OverriddenMethodsMapTy *OverriddenMethods = 0;
458
459void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
460 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Anders Carlsson05eb2442009-05-16 23:58:37 +0000462 if (!OverriddenMethods)
463 OverriddenMethods = new OverriddenMethodsMapTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Anders Carlsson05eb2442009-05-16 23:58:37 +0000465 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
466 if (!Methods)
467 Methods = new std::vector<const CXXMethodDecl *>;
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Anders Carlsson05eb2442009-05-16 23:58:37 +0000469 Methods->push_back(MD);
470}
471
472CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
473 if (!OverriddenMethods)
474 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Anders Carlsson05eb2442009-05-16 23:58:37 +0000476 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000477 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000478 return 0;
Daniel Dunbar0908c332009-08-01 23:40:20 +0000479
Anders Carlsson05eb2442009-05-16 23:58:37 +0000480 return &(*it->second)[0];
481}
482
483CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
484 if (!OverriddenMethods)
485 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Anders Carlsson05eb2442009-05-16 23:58:37 +0000487 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
Daniel Dunbar0908c332009-08-01 23:40:20 +0000488 if (it == OverriddenMethods->end() || it->second->empty())
Anders Carlsson05eb2442009-05-16 23:58:37 +0000489 return 0;
490
Daniel Dunbar637ec322009-08-02 01:48:29 +0000491 return &(*it->second)[0] + it->second->size();
Anders Carlsson05eb2442009-05-16 23:58:37 +0000492}
493
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000494QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000495 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
496 // If the member function is declared const, the type of this is const X*,
497 // if the member function is declared volatile, the type of this is
498 // volatile X*, and if the member function is declared const volatile,
499 // the type of this is const volatile X*.
500
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000501 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000502
503 QualType ClassTy;
504 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
505 ClassTy = TD->getInjectedClassNameType(C);
506 else
Mike Stumpe607ed02009-08-07 18:05:12 +0000507 ClassTy = C.getTagDeclType(getParent());
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000508 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
Anders Carlsson4e579922009-07-10 21:35:09 +0000509 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000510}
511
Douglas Gregor7ad83902008-11-05 04:29:56 +0000512CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000513CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000514 CXXConstructorDecl *C,
Mike Stump1eb44332009-09-09 15:08:12 +0000515 SourceLocation L, SourceLocation R)
Douglas Gregor72f6d672009-09-01 21:04:42 +0000516 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000517 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
518 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
519 BaseOrMember |= 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Douglas Gregor7ad83902008-11-05 04:29:56 +0000521 if (NumArgs > 0) {
522 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000523 // FIXME. Allocation via Context
524 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000525 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
526 this->Args[Idx] = Args[Idx];
527 }
Douglas Gregor72f6d672009-09-01 21:04:42 +0000528 CtorOrAnonUnion = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000529}
530
531CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000532CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000533 CXXConstructorDecl *C,
Anders Carlsson8c57a662009-08-29 01:31:33 +0000534 SourceLocation L, SourceLocation R)
Douglas Gregor72f6d672009-09-01 21:04:42 +0000535 : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000536 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
Mike Stump1eb44332009-09-09 15:08:12 +0000537 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
Douglas Gregor7ad83902008-11-05 04:29:56 +0000538
539 if (NumArgs > 0) {
540 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000541 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000542 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
543 this->Args[Idx] = Args[Idx];
544 }
Douglas Gregor72f6d672009-09-01 21:04:42 +0000545 CtorOrAnonUnion = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000546}
547
548CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
549 delete [] Args;
550}
551
Douglas Gregorb48fe382008-10-31 09:07:45 +0000552CXXConstructorDecl *
553CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000554 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000555 QualType T, DeclaratorInfo *DInfo,
556 bool isExplicit,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000557 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000558 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
559 "Name must refer to a constructor");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000560 return new (C) CXXConstructorDecl(RD, L, N, T, DInfo, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000561 isImplicitlyDeclared);
562}
563
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000564bool CXXConstructorDecl::isDefaultConstructor() const {
565 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000566 // A default constructor for a class X is a constructor of class
567 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000568 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000569 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000570}
571
Mike Stump1eb44332009-09-09 15:08:12 +0000572bool
573CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000574 unsigned &TypeQuals) const {
575 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000576 // A non-template constructor for class X is a copy constructor
577 // if its first parameter is of type X&, const X&, volatile X& or
578 // const volatile X&, and either there are no other parameters
579 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000580 if ((getNumParams() < 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000581 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000582 return false;
583
584 const ParmVarDecl *Param = getParamDecl(0);
585
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000586 // Do we have a reference type? Rvalue references don't count.
587 const LValueReferenceType *ParamRefType =
Ted Kremenek6217b802009-07-29 21:53:49 +0000588 Param->getType()->getAs<LValueReferenceType>();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000589 if (!ParamRefType)
590 return false;
591
592 // Is it a reference to our class type?
Mike Stumpe607ed02009-08-07 18:05:12 +0000593 QualType PointeeType
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000594 = Context.getCanonicalType(ParamRefType->getPointeeType());
Mike Stumpe607ed02009-08-07 18:05:12 +0000595 QualType ClassTy = Context.getTagDeclType(getParent());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000596 if (PointeeType.getUnqualifiedType() != ClassTy)
597 return false;
598
599 // We have a copy constructor.
600 TypeQuals = PointeeType.getCVRQualifiers();
601 return true;
602}
603
Anders Carlssonfaccd722009-08-28 16:57:08 +0000604bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000605 // C++ [class.conv.ctor]p1:
606 // A constructor declared without the function-specifier explicit
607 // that can be called with a single parameter specifies a
608 // conversion from the type of its first parameter to the type of
609 // its class. Such a constructor is called a converting
610 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000611 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000612 return false;
613
Mike Stump1eb44332009-09-09 15:08:12 +0000614 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000615 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000616 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000617 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000618}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000619
Douglas Gregor42a552f2008-11-05 20:51:48 +0000620CXXDestructorDecl *
621CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000622 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000623 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000624 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000625 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
626 "Name must refer to a destructor");
Mike Stump1eb44332009-09-09 15:08:12 +0000627 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff3e970492009-01-27 21:25:57 +0000628 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000629}
630
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000631void
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000632CXXDestructorDecl::Destroy(ASTContext& C) {
633 C.Deallocate(BaseOrMemberDestructions);
634 CXXMethodDecl::Destroy(C);
635}
636
637void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000638CXXConstructorDecl::Destroy(ASTContext& C) {
639 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000640 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000641}
642
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000643CXXConversionDecl *
644CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000645 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000646 QualType T, DeclaratorInfo *DInfo,
647 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000648 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
649 "Name must refer to a conversion function");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000650 return new (C) CXXConversionDecl(RD, L, N, T, DInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000651}
652
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000653OverloadedFunctionDecl *
654OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000655 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000656 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000657}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000658
Douglas Gregordec06662009-08-21 18:42:58 +0000659OverloadIterator::OverloadIterator(NamedDecl *ND) : D(0) {
660 if (!ND)
661 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Douglas Gregordec06662009-08-21 18:42:58 +0000663 if (isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND))
664 D = ND;
665 else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(ND)) {
Douglas Gregor8f1d89e2009-09-01 16:58:52 +0000666 if (Ovl->size() != 0) {
667 D = ND;
668 Iter = Ovl->function_begin();
669 }
Douglas Gregordec06662009-08-21 18:42:58 +0000670 }
671}
672
Douglas Gregor364e0212009-06-27 21:05:07 +0000673void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
674 Functions.push_back(F);
675 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000676}
677
Douglas Gregordaa439a2009-07-08 10:57:20 +0000678OverloadIterator::reference OverloadIterator::operator*() const {
679 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
680 return FD;
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Douglas Gregordaa439a2009-07-08 10:57:20 +0000682 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
683 return FTD;
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Douglas Gregordaa439a2009-07-08 10:57:20 +0000685 assert(isa<OverloadedFunctionDecl>(D));
686 return *Iter;
687}
688
689OverloadIterator &OverloadIterator::operator++() {
690 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
691 D = 0;
692 return *this;
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Douglas Gregordaa439a2009-07-08 10:57:20 +0000695 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
696 D = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Douglas Gregordaa439a2009-07-08 10:57:20 +0000698 return *this;
699}
700
701bool OverloadIterator::Equals(const OverloadIterator &Other) const {
702 if (!D || !Other.D)
703 return D == Other.D;
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Douglas Gregordaa439a2009-07-08 10:57:20 +0000705 if (D != Other.D)
706 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Douglas Gregordaa439a2009-07-08 10:57:20 +0000708 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
709}
710
John McCall02cace72009-08-28 07:59:38 +0000711FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
712 SourceLocation L,
713 FriendUnion Friend,
714 SourceLocation FriendL) {
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000715#ifndef NDEBUG
John McCall02cace72009-08-28 07:59:38 +0000716 if (Friend.is<NamedDecl*>()) {
717 NamedDecl *D = Friend.get<NamedDecl*>();
718 assert(isa<FunctionDecl>(D) ||
719 isa<CXXRecordDecl>(D) ||
720 isa<FunctionTemplateDecl>(D) ||
721 isa<ClassTemplateDecl>(D));
722 assert(D->getFriendObjectKind());
723 }
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000724#endif
John McCallc48fbdf2009-08-11 21:13:21 +0000725
John McCall02cace72009-08-28 07:59:38 +0000726 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump1eb44332009-09-09 15:08:12 +0000727}
John McCall3f9a8a62009-08-11 06:59:38 +0000728
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000729LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +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) {
Mike Stump1eb44332009-09-09 15:08:12 +0000744 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000745 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000746}
747
Mike Stump1eb44332009-09-09 15:08:12 +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,
Mike Stump1eb44332009-09-09 15:08:12 +0000754 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +0000755 NamedDecl *Namespace) {
Mike Stump1eb44332009-09-09 15:08:12 +0000756 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000757 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 Carlsson665b49c2009-08-28 05:30:28 +0000768UnresolvedUsingDecl *UnresolvedUsingDecl::Create(ASTContext &C, DeclContext *DC,
769 SourceLocation UsingLoc,
770 SourceRange TargetNNR,
771 NestedNameSpecifier *TargetNNS,
772 SourceLocation TargetNameLoc,
773 DeclarationName TargetName,
774 bool IsTypeNameArg) {
775 return new (C) UnresolvedUsingDecl(DC, UsingLoc, TargetNNR, TargetNNS,
776 TargetNameLoc, TargetName, IsTypeNameArg);
777}
778
Anders Carlssonfb311762009-03-14 00:25:26 +0000779StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
780 SourceLocation L, Expr *AssertExpr,
781 StringLiteral *Message) {
782 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
783}
784
785void StaticAssertDecl::Destroy(ASTContext& C) {
786 AssertExpr->Destroy(C);
787 Message->Destroy(C);
788 this->~StaticAssertDecl();
789 C.Deallocate((void *)this);
790}
791
792StaticAssertDecl::~StaticAssertDecl() {
793}
794
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000795static const char *getAccessName(AccessSpecifier AS) {
796 switch (AS) {
797 default:
798 case AS_none:
799 assert("Invalid access specifier!");
800 return 0;
801 case AS_public:
802 return "public";
803 case AS_private:
804 return "private";
805 case AS_protected:
806 return "protected";
807 }
808}
809
810const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
811 AccessSpecifier AS) {
812 return DB << getAccessName(AS);
813}
814
815