blob: a9eca9b23a23d288b3205b7c59613d0d9671246d [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
Mike Stump1eb44332009-09-09 15:08:12 +0000391void CXXRecordDecl::addConversionFunction(ASTContext &Context,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000392 CXXConversionDecl *ConvDecl) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000393 assert(!ConvDecl->getDescribedFunctionTemplate() &&
394 "Conversion function templates should cast to FunctionTemplateDecl.");
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000395 Conversions.addOverload(ConvDecl);
396}
397
Mike Stump1eb44332009-09-09 15:08:12 +0000398void CXXRecordDecl::addConversionFunction(ASTContext &Context,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000399 FunctionTemplateDecl *ConvDecl) {
400 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?
Mike Stumpe607ed02009-08-07 18:05:12 +0000595 QualType PointeeType
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000596 = Context.getCanonicalType(ParamRefType->getPointeeType());
Mike Stumpe607ed02009-08-07 18:05:12 +0000597 QualType ClassTy = Context.getTagDeclType(getParent());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000598 if (PointeeType.getUnqualifiedType() != ClassTy)
599 return false;
600
601 // We have a copy constructor.
602 TypeQuals = PointeeType.getCVRQualifiers();
603 return true;
604}
605
Anders Carlssonfaccd722009-08-28 16:57:08 +0000606bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000607 // C++ [class.conv.ctor]p1:
608 // A constructor declared without the function-specifier explicit
609 // that can be called with a single parameter specifies a
610 // conversion from the type of its first parameter to the type of
611 // its class. Such a constructor is called a converting
612 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000613 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000614 return false;
615
Mike Stump1eb44332009-09-09 15:08:12 +0000616 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000617 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000618 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000619 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000620}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000621
Douglas Gregor42a552f2008-11-05 20:51:48 +0000622CXXDestructorDecl *
623CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000624 SourceLocation L, DeclarationName N,
Mike Stump1eb44332009-09-09 15:08:12 +0000625 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000626 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000627 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
628 "Name must refer to a destructor");
Mike Stump1eb44332009-09-09 15:08:12 +0000629 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
Steve Naroff3e970492009-01-27 21:25:57 +0000630 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000631}
632
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000633void
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000634CXXDestructorDecl::Destroy(ASTContext& C) {
635 C.Deallocate(BaseOrMemberDestructions);
636 CXXMethodDecl::Destroy(C);
637}
638
639void
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000640CXXConstructorDecl::Destroy(ASTContext& C) {
641 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000642 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000643}
644
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000645CXXConversionDecl *
646CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000647 SourceLocation L, DeclarationName N,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000648 QualType T, DeclaratorInfo *DInfo,
649 bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000650 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
651 "Name must refer to a conversion function");
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000652 return new (C) CXXConversionDecl(RD, L, N, T, DInfo, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000653}
654
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000655OverloadedFunctionDecl *
656OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000657 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000658 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000659}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000660
Douglas Gregordec06662009-08-21 18:42:58 +0000661OverloadIterator::OverloadIterator(NamedDecl *ND) : D(0) {
662 if (!ND)
663 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Douglas Gregordec06662009-08-21 18:42:58 +0000665 if (isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND))
666 D = ND;
667 else if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(ND)) {
Douglas Gregor8f1d89e2009-09-01 16:58:52 +0000668 if (Ovl->size() != 0) {
669 D = ND;
670 Iter = Ovl->function_begin();
671 }
Douglas Gregordec06662009-08-21 18:42:58 +0000672 }
673}
674
Douglas Gregor364e0212009-06-27 21:05:07 +0000675void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
676 Functions.push_back(F);
677 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000678}
679
Douglas Gregordaa439a2009-07-08 10:57:20 +0000680OverloadIterator::reference OverloadIterator::operator*() const {
681 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
682 return FD;
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Douglas Gregordaa439a2009-07-08 10:57:20 +0000684 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
685 return FTD;
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Douglas Gregordaa439a2009-07-08 10:57:20 +0000687 assert(isa<OverloadedFunctionDecl>(D));
688 return *Iter;
689}
690
691OverloadIterator &OverloadIterator::operator++() {
692 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
693 D = 0;
694 return *this;
695 }
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Douglas Gregordaa439a2009-07-08 10:57:20 +0000697 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
698 D = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Douglas Gregordaa439a2009-07-08 10:57:20 +0000700 return *this;
701}
702
703bool OverloadIterator::Equals(const OverloadIterator &Other) const {
704 if (!D || !Other.D)
705 return D == Other.D;
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Douglas Gregordaa439a2009-07-08 10:57:20 +0000707 if (D != Other.D)
708 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Douglas Gregordaa439a2009-07-08 10:57:20 +0000710 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
711}
712
John McCall02cace72009-08-28 07:59:38 +0000713FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
714 SourceLocation L,
715 FriendUnion Friend,
716 SourceLocation FriendL) {
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000717#ifndef NDEBUG
John McCall02cace72009-08-28 07:59:38 +0000718 if (Friend.is<NamedDecl*>()) {
719 NamedDecl *D = Friend.get<NamedDecl*>();
720 assert(isa<FunctionDecl>(D) ||
721 isa<CXXRecordDecl>(D) ||
722 isa<FunctionTemplateDecl>(D) ||
723 isa<ClassTemplateDecl>(D));
724 assert(D->getFriendObjectKind());
725 }
Daniel Dunbare32ecce2009-08-31 19:16:38 +0000726#endif
John McCallc48fbdf2009-08-11 21:13:21 +0000727
John McCall02cace72009-08-28 07:59:38 +0000728 return new (C) FriendDecl(DC, L, Friend, FriendL);
Mike Stump1eb44332009-09-09 15:08:12 +0000729}
John McCall3f9a8a62009-08-11 06:59:38 +0000730
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000731LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +0000732 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000733 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000734 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000735 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000736}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000737
738UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
739 SourceLocation L,
740 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000741 SourceRange QualifierRange,
742 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000743 SourceLocation IdentLoc,
744 NamespaceDecl *Used,
745 DeclContext *CommonAncestor) {
Mike Stump1eb44332009-09-09 15:08:12 +0000746 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000747 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000748}
749
Mike Stump1eb44332009-09-09 15:08:12 +0000750NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
751 SourceLocation L,
752 SourceLocation AliasLoc,
753 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000754 SourceRange QualifierRange,
755 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +0000756 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +0000757 NamedDecl *Namespace) {
Mike Stump1eb44332009-09-09 15:08:12 +0000758 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000759 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000760}
761
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000762UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
763 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
764 SourceLocation UL, NamedDecl* Target,
765 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
766 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
767 TargetNNS, IsTypeNameArg);
768}
769
Anders Carlsson665b49c2009-08-28 05:30:28 +0000770UnresolvedUsingDecl *UnresolvedUsingDecl::Create(ASTContext &C, DeclContext *DC,
771 SourceLocation UsingLoc,
772 SourceRange TargetNNR,
773 NestedNameSpecifier *TargetNNS,
774 SourceLocation TargetNameLoc,
775 DeclarationName TargetName,
776 bool IsTypeNameArg) {
777 return new (C) UnresolvedUsingDecl(DC, UsingLoc, TargetNNR, TargetNNS,
778 TargetNameLoc, TargetName, IsTypeNameArg);
779}
780
Anders Carlssonfb311762009-03-14 00:25:26 +0000781StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
782 SourceLocation L, Expr *AssertExpr,
783 StringLiteral *Message) {
784 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
785}
786
787void StaticAssertDecl::Destroy(ASTContext& C) {
788 AssertExpr->Destroy(C);
789 Message->Destroy(C);
790 this->~StaticAssertDecl();
791 C.Deallocate((void *)this);
792}
793
794StaticAssertDecl::~StaticAssertDecl() {
795}
796
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000797static const char *getAccessName(AccessSpecifier AS) {
798 switch (AS) {
799 default:
800 case AS_none:
801 assert("Invalid access specifier!");
802 return 0;
803 case AS_public:
804 return "public";
805 case AS_private:
806 return "private";
807 case AS_protected:
808 return "protected";
809 }
810}
811
812const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
813 AccessSpecifier AS) {
814 return DB << getAccessName(AS);
815}
816
817