blob: f525667ad0a644f0cc799bddaa48e4bf42475311 [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 Gregor7d7e6722008-11-12 23:21:09 +000027 SourceLocation L, IdentifierInfo *Id)
Douglas Gregor3e00bad2009-02-17 01:05:43 +000028 : RecordDecl(K, TK, DC, L, Id),
Douglas Gregor7d7e6722008-11-12 23:21:09 +000029 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000030 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Anders Carlsson67e4dd22009-03-22 01:52:17 +000031 Aggregate(true), PlainOldData(true), Polymorphic(false), Abstract(false),
Anders Carlsson072abef2009-04-17 02:34:54 +000032 HasTrivialConstructor(true), HasTrivialDestructor(true),
Douglas Gregord475b8d2009-03-25 21:17:03 +000033 Bases(0), NumBases(0), Conversions(DC, DeclarationName()),
34 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000035
Ted Kremenek4b7c9832008-09-05 17:16:31 +000036CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
37 SourceLocation L, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000038 CXXRecordDecl* PrevDecl,
39 bool DelayTypeCreation) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +000040 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000041 if (!DelayTypeCreation)
42 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000043 return R;
44}
45
Douglas Gregorf8268ae2008-10-22 17:49:05 +000046CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000047}
48
49void CXXRecordDecl::Destroy(ASTContext &C) {
50 C.Deallocate(Bases);
51 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000052}
53
Douglas Gregor57c856b2008-10-23 18:13:27 +000054void
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000055CXXRecordDecl::setBases(ASTContext &C,
56 CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000057 unsigned NumBases) {
Douglas Gregor64bffa92008-11-05 16:20:31 +000058 // C++ [dcl.init.aggr]p1:
59 // An aggregate is an array or a class (clause 9) with [...]
60 // no base classes [...].
61 Aggregate = false;
62
Douglas Gregor57c856b2008-10-23 18:13:27 +000063 if (this->Bases)
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000064 C.Deallocate(this->Bases);
Douglas Gregor57c856b2008-10-23 18:13:27 +000065
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000066 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor57c856b2008-10-23 18:13:27 +000067 this->NumBases = NumBases;
68 for (unsigned i = 0; i < NumBases; ++i)
69 this->Bases[i] = *Bases[i];
70}
71
Douglas Gregor396b7cd2008-11-03 17:51:48 +000072bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Fariborz Jahanian485f0872009-06-22 23:34:40 +000073 return getCopyConstructor(Context, QualType::Const) != 0;
74}
75
76CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
77 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +000078 QualType ClassType
79 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Douglas Gregor9e7d9de2008-12-15 21:24:18 +000080 DeclarationName ConstructorName
81 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +000082 Context.getCanonicalType(ClassType));
83 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000084 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000085 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000086 Con != ConEnd; ++Con) {
Fariborz Jahanian485f0872009-06-22 23:34:40 +000087 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context,
88 FoundTQs)) {
89 if (((TypeQuals & QualType::Const) == (FoundTQs & QualType::Const)) ||
90 (!(TypeQuals & QualType::Const) && (FoundTQs & QualType::Const)))
91 return cast<CXXConstructorDecl>(*Con);
92
93 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +000094 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +000095 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +000096}
97
Sebastian Redl64b45f72009-01-05 20:52:13 +000098bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context) const {
99 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
100 const_cast<CXXRecordDecl*>(this)));
101 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
102
103 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000104 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000105 Op != OpEnd; ++Op) {
106 // C++ [class.copy]p9:
107 // A user-declared copy assignment operator is a non-static non-template
108 // member function of class X with exactly one parameter of type X, X&,
109 // const X&, volatile X& or const volatile X&.
110 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
111 if (Method->isStatic())
112 continue;
113 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor72564e72009-02-26 23:50:07 +0000114 const FunctionProtoType *FnType =
115 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000116 assert(FnType && "Overloaded operator has no prototype.");
117 // Don't assert on this; an invalid decl might have been left in the AST.
118 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
119 continue;
120 bool AcceptsConst = true;
121 QualType ArgType = FnType->getArgType(0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000122 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000123 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000124 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000125 if (!ArgType.isConstQualified())
126 AcceptsConst = false;
127 }
128 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
129 continue;
130
131 // We have a single argument of type cv X or cv X&, i.e. we've found the
132 // copy assignment operator. Return whether it accepts const arguments.
133 return AcceptsConst;
134 }
135 assert(isInvalidDecl() &&
136 "No copy assignment operator declared in valid code.");
137 return false;
138}
139
140void
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000141CXXRecordDecl::addedConstructor(ASTContext &Context,
142 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000143 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
144 // Note that we have a user-declared constructor.
145 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000146
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000147 // C++ [dcl.init.aggr]p1:
148 // An aggregate is an array or a class (clause 9) with no
149 // user-declared constructors (12.1) [...].
150 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000151
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000152 // C++ [class]p4:
153 // A POD-struct is an aggregate class [...]
154 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000155
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000156 // C++ [class.ctor]p5:
157 // A constructor is trivial if it is an implicitly-declared default
158 // constructor.
159 HasTrivialConstructor = false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000160
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000161 // Note when we have a user-declared copy constructor, which will
162 // suppress the implicit declaration of a copy constructor.
163 if (ConDecl->isCopyConstructor(Context))
164 UserDeclaredCopyConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000165}
166
Sebastian Redl64b45f72009-01-05 20:52:13 +0000167void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
168 CXXMethodDecl *OpDecl) {
169 // We're interested specifically in copy assignment operators.
Douglas Gregor72564e72009-02-26 23:50:07 +0000170 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000171 assert(FnType && "Overloaded operator has no proto function type.");
172 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
173 QualType ArgType = FnType->getArgType(0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000174 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000175 ArgType = Ref->getPointeeType();
176
177 ArgType = ArgType.getUnqualifiedType();
178 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
179 const_cast<CXXRecordDecl*>(this)));
180
181 if (ClassType != Context.getCanonicalType(ArgType))
182 return;
183
184 // This is a copy assignment operator.
185 // Suppress the implicit declaration of a copy constructor.
186 UserDeclaredCopyAssignment = true;
187
188 // C++ [class]p4:
189 // A POD-struct is an aggregate class that [...] has no user-defined copy
190 // assignment operator [...].
191 PlainOldData = false;
192}
193
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000194void CXXRecordDecl::addConversionFunction(ASTContext &Context,
195 CXXConversionDecl *ConvDecl) {
196 Conversions.addOverload(ConvDecl);
197}
198
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000199
200CXXConstructorDecl *
201CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
202 QualType ClassType = Context.getTypeDeclType(this);
203 DeclarationName ConstructorName
204 = Context.DeclarationNames.getCXXConstructorName(
205 Context.getCanonicalType(ClassType.getUnqualifiedType()));
206
207 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000208 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000209 Con != ConEnd; ++Con) {
210 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
211 if (Constructor->isDefaultConstructor())
212 return Constructor;
213 }
214 return 0;
215}
216
Anders Carlsson7267c162009-05-29 21:03:38 +0000217const CXXDestructorDecl *
218CXXRecordDecl::getDestructor(ASTContext &Context) {
219 QualType ClassType = Context.getTypeDeclType(this);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000220
Anders Carlsson7267c162009-05-29 21:03:38 +0000221 DeclarationName Name
222 = Context.DeclarationNames.getCXXDestructorName(ClassType);
223
224 DeclContext::lookup_iterator I, E;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000225 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000226 assert(I != E && "Did not find a destructor!");
227
228 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
229 assert(++I == E && "Found more than one destructor!");
230
231 return Dtor;
232}
233
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000234CXXMethodDecl *
235CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000236 SourceLocation L, DeclarationName N,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000237 QualType T, bool isStatic, bool isInline) {
Steve Naroff3e970492009-01-27 21:25:57 +0000238 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000239}
240
Anders Carlsson05eb2442009-05-16 23:58:37 +0000241
242typedef llvm::DenseMap<const CXXMethodDecl*,
243 std::vector<const CXXMethodDecl *> *>
244 OverriddenMethodsMapTy;
245
246static OverriddenMethodsMapTy *OverriddenMethods = 0;
247
248void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
249 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
250
251 if (!OverriddenMethods)
252 OverriddenMethods = new OverriddenMethodsMapTy();
253
254 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
255 if (!Methods)
256 Methods = new std::vector<const CXXMethodDecl *>;
257
258 Methods->push_back(MD);
259}
260
261CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
262 if (!OverriddenMethods)
263 return 0;
264
265 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
266 if (it == OverriddenMethods->end())
267 return 0;
268 return &(*it->second)[0];
269}
270
271CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
272 if (!OverriddenMethods)
273 return 0;
274
275 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
276 if (it == OverriddenMethods->end())
277 return 0;
278
279 return &(*it->second)[it->second->size()];
280}
281
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000282QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000283 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
284 // If the member function is declared const, the type of this is const X*,
285 // if the member function is declared volatile, the type of this is
286 // volatile X*, and if the member function is declared const volatile,
287 // the type of this is const volatile X*.
288
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000289 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000290
291 QualType ClassTy;
292 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
293 ClassTy = TD->getInjectedClassNameType(C);
294 else
295 ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000296 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
297 return C.getPointerType(ClassTy).withConst();
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000298}
299
Douglas Gregor7ad83902008-11-05 04:29:56 +0000300CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000301CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
302 SourceLocation L)
303 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000304 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
305 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
306 BaseOrMember |= 0x01;
307
308 if (NumArgs > 0) {
309 this->NumArgs = NumArgs;
310 this->Args = new Expr*[NumArgs];
311 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
312 this->Args[Idx] = Args[Idx];
313 }
314}
315
316CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000317CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
318 SourceLocation L)
319 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000320 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
321 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
322
323 if (NumArgs > 0) {
324 this->NumArgs = NumArgs;
325 this->Args = new Expr*[NumArgs];
326 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
327 this->Args[Idx] = Args[Idx];
328 }
329}
330
331CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
332 delete [] Args;
333}
334
Douglas Gregorb48fe382008-10-31 09:07:45 +0000335CXXConstructorDecl *
336CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000337 SourceLocation L, DeclarationName N,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000338 QualType T, bool isExplicit,
339 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000340 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
341 "Name must refer to a constructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000342 return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000343 isImplicitlyDeclared);
344}
345
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000346bool CXXConstructorDecl::isDefaultConstructor() const {
347 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000348 // A default constructor for a class X is a constructor of class
349 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000350 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000351 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000352}
353
354bool
355CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
356 unsigned &TypeQuals) const {
357 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000358 // A non-template constructor for class X is a copy constructor
359 // if its first parameter is of type X&, const X&, volatile X& or
360 // const volatile X&, and either there are no other parameters
361 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000362 if ((getNumParams() < 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000363 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000364 return false;
365
366 const ParmVarDecl *Param = getParamDecl(0);
367
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000368 // Do we have a reference type? Rvalue references don't count.
369 const LValueReferenceType *ParamRefType =
370 Param->getType()->getAsLValueReferenceType();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000371 if (!ParamRefType)
372 return false;
373
374 // Is it a reference to our class type?
375 QualType PointeeType
376 = Context.getCanonicalType(ParamRefType->getPointeeType());
377 QualType ClassTy
378 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
379 if (PointeeType.getUnqualifiedType() != ClassTy)
380 return false;
381
382 // We have a copy constructor.
383 TypeQuals = PointeeType.getCVRQualifiers();
384 return true;
385}
386
Douglas Gregor60d62c22008-10-31 16:23:19 +0000387bool CXXConstructorDecl::isConvertingConstructor() const {
388 // C++ [class.conv.ctor]p1:
389 // A constructor declared without the function-specifier explicit
390 // that can be called with a single parameter specifies a
391 // conversion from the type of its first parameter to the type of
392 // its class. Such a constructor is called a converting
393 // constructor.
394 if (isExplicit())
395 return false;
396
397 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000398 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000399 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000400 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000401}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000402
Douglas Gregor42a552f2008-11-05 20:51:48 +0000403CXXDestructorDecl *
404CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000405 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000406 QualType T, bool isInline,
407 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000408 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
409 "Name must refer to a destructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000410 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
411 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000412}
413
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000414void
415CXXConstructorDecl::setBaseOrMemberInitializers(
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000416 ASTContext &C,
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000417 CXXBaseOrMemberInitializer **Initializers,
418 unsigned NumInitializers) {
419 if (NumInitializers > 0) {
420 NumBaseOrMemberInitializers = NumInitializers;
421 BaseOrMemberInitializers =
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000422 new (C) CXXBaseOrMemberInitializer*[NumInitializers];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000423 for (unsigned Idx = 0; Idx < NumInitializers; ++Idx)
424 BaseOrMemberInitializers[Idx] = Initializers[Idx];
425 }
426}
427
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000428void
429CXXConstructorDecl::Destroy(ASTContext& C) {
430 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000431 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000432}
433
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000434CXXConversionDecl *
435CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000436 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000437 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000438 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
439 "Name must refer to a conversion function");
Steve Naroff3e970492009-01-27 21:25:57 +0000440 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000441}
442
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000443OverloadedFunctionDecl *
444OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000445 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000446 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000447}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000448
Douglas Gregor364e0212009-06-27 21:05:07 +0000449void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
450 Functions.push_back(F);
451 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000452}
453
Douglas Gregordaa439a2009-07-08 10:57:20 +0000454OverloadIterator::reference OverloadIterator::operator*() const {
455 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
456 return FD;
457
458 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
459 return FTD;
460
461 assert(isa<OverloadedFunctionDecl>(D));
462 return *Iter;
463}
464
465OverloadIterator &OverloadIterator::operator++() {
466 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
467 D = 0;
468 return *this;
469 }
470
471 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
472 D = 0;
473
474 return *this;
475}
476
477bool OverloadIterator::Equals(const OverloadIterator &Other) const {
478 if (!D || !Other.D)
479 return D == Other.D;
480
481 if (D != Other.D)
482 return false;
483
484 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
485}
486
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000487LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000488 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000489 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000490 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000491 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000492}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000493
494UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
495 SourceLocation L,
496 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000497 SourceRange QualifierRange,
498 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000499 SourceLocation IdentLoc,
500 NamespaceDecl *Used,
501 DeclContext *CommonAncestor) {
Douglas Gregor8419fa32009-05-30 06:31:56 +0000502 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
503 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000504}
505
Anders Carlsson68771c72009-03-28 22:58:02 +0000506NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
507 SourceLocation L,
508 SourceLocation AliasLoc,
509 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000510 SourceRange QualifierRange,
511 NestedNameSpecifier *Qualifier,
Anders Carlsson68771c72009-03-28 22:58:02 +0000512 SourceLocation IdentLoc,
513 NamedDecl *Namespace) {
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000514 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
515 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000516}
517
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000518UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
519 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
520 SourceLocation UL, NamedDecl* Target,
521 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
522 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
523 TargetNNS, IsTypeNameArg);
524}
525
Anders Carlssonfb311762009-03-14 00:25:26 +0000526StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
527 SourceLocation L, Expr *AssertExpr,
528 StringLiteral *Message) {
529 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
530}
531
532void StaticAssertDecl::Destroy(ASTContext& C) {
533 AssertExpr->Destroy(C);
534 Message->Destroy(C);
535 this->~StaticAssertDecl();
536 C.Deallocate((void *)this);
537}
538
539StaticAssertDecl::~StaticAssertDecl() {
540}
541
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000542static const char *getAccessName(AccessSpecifier AS) {
543 switch (AS) {
544 default:
545 case AS_none:
546 assert("Invalid access specifier!");
547 return 0;
548 case AS_public:
549 return "public";
550 case AS_private:
551 return "private";
552 case AS_protected:
553 return "protected";
554 }
555}
556
557const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
558 AccessSpecifier AS) {
559 return DB << getAccessName(AS);
560}
561
562