blob: 8b596e1e7c22113478eff7c35ce3010b60b21fb1 [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() {
Douglas Gregorf8268ae2008-10-22 17:49:05 +000047 delete [] Bases;
48}
49
Douglas Gregor57c856b2008-10-23 18:13:27 +000050void
51CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
52 unsigned NumBases) {
Douglas Gregor64bffa92008-11-05 16:20:31 +000053 // C++ [dcl.init.aggr]p1:
54 // An aggregate is an array or a class (clause 9) with [...]
55 // no base classes [...].
56 Aggregate = false;
57
Douglas Gregor57c856b2008-10-23 18:13:27 +000058 if (this->Bases)
59 delete [] this->Bases;
60
Douglas Gregor2943aed2009-03-03 04:44:36 +000061 // FIXME: allocate using the ASTContext
Douglas Gregor57c856b2008-10-23 18:13:27 +000062 this->Bases = new CXXBaseSpecifier[NumBases];
63 this->NumBases = NumBases;
64 for (unsigned i = 0; i < NumBases; ++i)
65 this->Bases[i] = *Bases[i];
66}
67
Douglas Gregor396b7cd2008-11-03 17:51:48 +000068bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Fariborz Jahanian485f0872009-06-22 23:34:40 +000069 return getCopyConstructor(Context, QualType::Const) != 0;
70}
71
72CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
73 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +000074 QualType ClassType
75 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Douglas Gregor9e7d9de2008-12-15 21:24:18 +000076 DeclarationName ConstructorName
77 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +000078 Context.getCanonicalType(ClassType));
79 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000080 DeclContext::lookup_const_iterator Con, ConEnd;
Douglas Gregor6ab35242009-04-09 21:40:53 +000081 for (llvm::tie(Con, ConEnd) = this->lookup(Context, ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000082 Con != ConEnd; ++Con) {
Fariborz Jahanian485f0872009-06-22 23:34:40 +000083 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context,
84 FoundTQs)) {
85 if (((TypeQuals & QualType::Const) == (FoundTQs & QualType::Const)) ||
86 (!(TypeQuals & QualType::Const) && (FoundTQs & QualType::Const)))
87 return cast<CXXConstructorDecl>(*Con);
88
89 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +000090 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +000091 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +000092}
93
Sebastian Redl64b45f72009-01-05 20:52:13 +000094bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context) const {
95 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
96 const_cast<CXXRecordDecl*>(this)));
97 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
98
99 DeclContext::lookup_const_iterator Op, OpEnd;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000100 for (llvm::tie(Op, OpEnd) = this->lookup(Context, OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000101 Op != OpEnd; ++Op) {
102 // C++ [class.copy]p9:
103 // A user-declared copy assignment operator is a non-static non-template
104 // member function of class X with exactly one parameter of type X, X&,
105 // const X&, volatile X& or const volatile X&.
106 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
107 if (Method->isStatic())
108 continue;
109 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor72564e72009-02-26 23:50:07 +0000110 const FunctionProtoType *FnType =
111 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000112 assert(FnType && "Overloaded operator has no prototype.");
113 // Don't assert on this; an invalid decl might have been left in the AST.
114 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
115 continue;
116 bool AcceptsConst = true;
117 QualType ArgType = FnType->getArgType(0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000118 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000119 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000120 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000121 if (!ArgType.isConstQualified())
122 AcceptsConst = false;
123 }
124 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
125 continue;
126
127 // We have a single argument of type cv X or cv X&, i.e. we've found the
128 // copy assignment operator. Return whether it accepts const arguments.
129 return AcceptsConst;
130 }
131 assert(isInvalidDecl() &&
132 "No copy assignment operator declared in valid code.");
133 return false;
134}
135
136void
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000137CXXRecordDecl::addedConstructor(ASTContext &Context,
138 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000139 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
140 // Note that we have a user-declared constructor.
141 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000142
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000143 // C++ [dcl.init.aggr]p1:
144 // An aggregate is an array or a class (clause 9) with no
145 // user-declared constructors (12.1) [...].
146 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000147
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000148 // C++ [class]p4:
149 // A POD-struct is an aggregate class [...]
150 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000151
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000152 // C++ [class.ctor]p5:
153 // A constructor is trivial if it is an implicitly-declared default
154 // constructor.
155 HasTrivialConstructor = false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000156
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000157 // Note when we have a user-declared copy constructor, which will
158 // suppress the implicit declaration of a copy constructor.
159 if (ConDecl->isCopyConstructor(Context))
160 UserDeclaredCopyConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000161}
162
Sebastian Redl64b45f72009-01-05 20:52:13 +0000163void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
164 CXXMethodDecl *OpDecl) {
165 // We're interested specifically in copy assignment operators.
Douglas Gregor72564e72009-02-26 23:50:07 +0000166 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000167 assert(FnType && "Overloaded operator has no proto function type.");
168 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
169 QualType ArgType = FnType->getArgType(0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000170 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000171 ArgType = Ref->getPointeeType();
172
173 ArgType = ArgType.getUnqualifiedType();
174 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
175 const_cast<CXXRecordDecl*>(this)));
176
177 if (ClassType != Context.getCanonicalType(ArgType))
178 return;
179
180 // This is a copy assignment operator.
181 // Suppress the implicit declaration of a copy constructor.
182 UserDeclaredCopyAssignment = true;
183
184 // C++ [class]p4:
185 // A POD-struct is an aggregate class that [...] has no user-defined copy
186 // assignment operator [...].
187 PlainOldData = false;
188}
189
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000190void CXXRecordDecl::addConversionFunction(ASTContext &Context,
191 CXXConversionDecl *ConvDecl) {
192 Conversions.addOverload(ConvDecl);
193}
194
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000195
196CXXConstructorDecl *
197CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
198 QualType ClassType = Context.getTypeDeclType(this);
199 DeclarationName ConstructorName
200 = Context.DeclarationNames.getCXXConstructorName(
201 Context.getCanonicalType(ClassType.getUnqualifiedType()));
202
203 DeclContext::lookup_const_iterator Con, ConEnd;
204 for (llvm::tie(Con, ConEnd) = lookup(Context, ConstructorName);
205 Con != ConEnd; ++Con) {
206 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
207 if (Constructor->isDefaultConstructor())
208 return Constructor;
209 }
210 return 0;
211}
212
Anders Carlsson7267c162009-05-29 21:03:38 +0000213const CXXDestructorDecl *
214CXXRecordDecl::getDestructor(ASTContext &Context) {
215 QualType ClassType = Context.getTypeDeclType(this);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000216
Anders Carlsson7267c162009-05-29 21:03:38 +0000217 DeclarationName Name
218 = Context.DeclarationNames.getCXXDestructorName(ClassType);
219
220 DeclContext::lookup_iterator I, E;
221 llvm::tie(I, E) = lookup(Context, Name);
222 assert(I != E && "Did not find a destructor!");
223
224 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
225 assert(++I == E && "Found more than one destructor!");
226
227 return Dtor;
228}
229
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000230CXXMethodDecl *
231CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000232 SourceLocation L, DeclarationName N,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000233 QualType T, bool isStatic, bool isInline) {
Steve Naroff3e970492009-01-27 21:25:57 +0000234 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000235}
236
Anders Carlsson05eb2442009-05-16 23:58:37 +0000237
238typedef llvm::DenseMap<const CXXMethodDecl*,
239 std::vector<const CXXMethodDecl *> *>
240 OverriddenMethodsMapTy;
241
242static OverriddenMethodsMapTy *OverriddenMethods = 0;
243
244void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
245 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
246
247 if (!OverriddenMethods)
248 OverriddenMethods = new OverriddenMethodsMapTy();
249
250 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
251 if (!Methods)
252 Methods = new std::vector<const CXXMethodDecl *>;
253
254 Methods->push_back(MD);
255}
256
257CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
258 if (!OverriddenMethods)
259 return 0;
260
261 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
262 if (it == OverriddenMethods->end())
263 return 0;
264 return &(*it->second)[0];
265}
266
267CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
268 if (!OverriddenMethods)
269 return 0;
270
271 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
272 if (it == OverriddenMethods->end())
273 return 0;
274
275 return &(*it->second)[it->second->size()];
276}
277
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000278QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000279 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
280 // If the member function is declared const, the type of this is const X*,
281 // if the member function is declared volatile, the type of this is
282 // volatile X*, and if the member function is declared const volatile,
283 // the type of this is const volatile X*.
284
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000285 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000286
287 QualType ClassTy;
288 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
289 ClassTy = TD->getInjectedClassNameType(C);
290 else
291 ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000292 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
293 return C.getPointerType(ClassTy).withConst();
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000294}
295
Douglas Gregor7ad83902008-11-05 04:29:56 +0000296CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000297CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
298 SourceLocation L)
299 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000300 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
301 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
302 BaseOrMember |= 0x01;
303
304 if (NumArgs > 0) {
305 this->NumArgs = NumArgs;
306 this->Args = new Expr*[NumArgs];
307 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
308 this->Args[Idx] = Args[Idx];
309 }
310}
311
312CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000313CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
314 SourceLocation L)
315 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000316 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
317 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
318
319 if (NumArgs > 0) {
320 this->NumArgs = NumArgs;
321 this->Args = new Expr*[NumArgs];
322 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
323 this->Args[Idx] = Args[Idx];
324 }
325}
326
327CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
328 delete [] Args;
329}
330
Douglas Gregorb48fe382008-10-31 09:07:45 +0000331CXXConstructorDecl *
332CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000333 SourceLocation L, DeclarationName N,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000334 QualType T, bool isExplicit,
335 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000336 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
337 "Name must refer to a constructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000338 return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000339 isImplicitlyDeclared);
340}
341
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000342bool CXXConstructorDecl::isDefaultConstructor() const {
343 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000344 // A default constructor for a class X is a constructor of class
345 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000346 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000347 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000348}
349
350bool
351CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
352 unsigned &TypeQuals) const {
353 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000354 // A non-template constructor for class X is a copy constructor
355 // if its first parameter is of type X&, const X&, volatile X& or
356 // const volatile X&, and either there are no other parameters
357 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000358 if ((getNumParams() < 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000359 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000360 return false;
361
362 const ParmVarDecl *Param = getParamDecl(0);
363
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000364 // Do we have a reference type? Rvalue references don't count.
365 const LValueReferenceType *ParamRefType =
366 Param->getType()->getAsLValueReferenceType();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000367 if (!ParamRefType)
368 return false;
369
370 // Is it a reference to our class type?
371 QualType PointeeType
372 = Context.getCanonicalType(ParamRefType->getPointeeType());
373 QualType ClassTy
374 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
375 if (PointeeType.getUnqualifiedType() != ClassTy)
376 return false;
377
378 // We have a copy constructor.
379 TypeQuals = PointeeType.getCVRQualifiers();
380 return true;
381}
382
Douglas Gregor60d62c22008-10-31 16:23:19 +0000383bool CXXConstructorDecl::isConvertingConstructor() const {
384 // C++ [class.conv.ctor]p1:
385 // A constructor declared without the function-specifier explicit
386 // that can be called with a single parameter specifies a
387 // conversion from the type of its first parameter to the type of
388 // its class. Such a constructor is called a converting
389 // constructor.
390 if (isExplicit())
391 return false;
392
393 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000394 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000395 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000396 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000397}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000398
Douglas Gregor42a552f2008-11-05 20:51:48 +0000399CXXDestructorDecl *
400CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000401 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000402 QualType T, bool isInline,
403 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000404 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
405 "Name must refer to a destructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000406 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
407 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000408}
409
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000410CXXConversionDecl *
411CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000412 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000413 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000414 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
415 "Name must refer to a conversion function");
Steve Naroff3e970492009-01-27 21:25:57 +0000416 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000417}
418
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000419OverloadedFunctionDecl *
420OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000421 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000422 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000423}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000424
Douglas Gregor364e0212009-06-27 21:05:07 +0000425void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
426 Functions.push_back(F);
427 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000428}
429
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000430LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000431 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000432 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000433 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000434 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000435}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000436
437UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
438 SourceLocation L,
439 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000440 SourceRange QualifierRange,
441 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000442 SourceLocation IdentLoc,
443 NamespaceDecl *Used,
444 DeclContext *CommonAncestor) {
Douglas Gregor8419fa32009-05-30 06:31:56 +0000445 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
446 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000447}
448
Anders Carlsson68771c72009-03-28 22:58:02 +0000449NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
450 SourceLocation L,
451 SourceLocation AliasLoc,
452 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000453 SourceRange QualifierRange,
454 NestedNameSpecifier *Qualifier,
Anders Carlsson68771c72009-03-28 22:58:02 +0000455 SourceLocation IdentLoc,
456 NamedDecl *Namespace) {
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000457 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
458 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000459}
460
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000461UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
462 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
463 SourceLocation UL, NamedDecl* Target,
464 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
465 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
466 TargetNNS, IsTypeNameArg);
467}
468
Anders Carlssonfb311762009-03-14 00:25:26 +0000469StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
470 SourceLocation L, Expr *AssertExpr,
471 StringLiteral *Message) {
472 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
473}
474
475void StaticAssertDecl::Destroy(ASTContext& C) {
476 AssertExpr->Destroy(C);
477 Message->Destroy(C);
478 this->~StaticAssertDecl();
479 C.Deallocate((void *)this);
480}
481
482StaticAssertDecl::~StaticAssertDecl() {
483}
484
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000485static const char *getAccessName(AccessSpecifier AS) {
486 switch (AS) {
487 default:
488 case AS_none:
489 assert("Invalid access specifier!");
490 return 0;
491 case AS_public:
492 return "public";
493 case AS_private:
494 return "private";
495 case AS_protected:
496 return "protected";
497 }
498}
499
500const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
501 AccessSpecifier AS) {
502 return DB << getAccessName(AS);
503}
504
505