blob: 57897b04608648f7b108103bda0732f7514c487d [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,
28 SourceLocation TKL)
29 : RecordDecl(K, TK, DC, L, Id, TKL),
Douglas Gregor7d7e6722008-11-12 23:21:09 +000030 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000031 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Anders Carlsson67e4dd22009-03-22 01:52:17 +000032 Aggregate(true), PlainOldData(true), Polymorphic(false), Abstract(false),
Douglas Gregor1f2023a2009-07-22 18:25:24 +000033 HasTrivialConstructor(true), HasTrivialCopyConstructor(true),
34 HasTrivialCopyAssignment(true), HasTrivialDestructor(true),
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000035 Bases(0), NumBases(0), VBases(0), NumVBases(0),
36 Conversions(DC, DeclarationName()),
Douglas Gregord475b8d2009-03-25 21:17:03 +000037 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000038
Ted Kremenek4b7c9832008-09-05 17:16:31 +000039CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
40 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000041 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000042 CXXRecordDecl* PrevDecl,
43 bool DelayTypeCreation) {
Douglas Gregor741dd9a2009-07-21 14:46:17 +000044 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id, TKL);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000045 if (!DelayTypeCreation)
46 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000047 return R;
48}
49
Douglas Gregorf8268ae2008-10-22 17:49:05 +000050CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000051}
52
53void CXXRecordDecl::Destroy(ASTContext &C) {
54 C.Deallocate(Bases);
Fariborz Jahanian71c6e712009-07-22 17:41:53 +000055 C.Deallocate(VBases);
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000056 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000057}
58
Douglas Gregor57c856b2008-10-23 18:13:27 +000059void
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000060CXXRecordDecl::setBases(ASTContext &C,
61 CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000062 unsigned NumBases) {
Douglas Gregor64bffa92008-11-05 16:20:31 +000063 // C++ [dcl.init.aggr]p1:
64 // An aggregate is an array or a class (clause 9) with [...]
65 // no base classes [...].
66 Aggregate = false;
67
Douglas Gregor57c856b2008-10-23 18:13:27 +000068 if (this->Bases)
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000069 C.Deallocate(this->Bases);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000070
71 int vbaseCount = 0;
72 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
73 bool hasDirectVirtualBase = false;
74
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000075 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor57c856b2008-10-23 18:13:27 +000076 this->NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000077 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor57c856b2008-10-23 18:13:27 +000078 this->Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000079 // Keep track of inherited vbases for this base class.
80 const CXXBaseSpecifier *Base = Bases[i];
81 QualType BaseType = Base->getType();
82 // Skip template types.
83 // FIXME. This means that this list must be rebuilt during template
84 // instantiation.
85 if (BaseType->isDependentType())
86 continue;
87 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +000088 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000089 if (Base->isVirtual())
90 hasDirectVirtualBase = true;
91 for (CXXRecordDecl::base_class_iterator VBase =
92 BaseClassDecl->vbases_begin(),
93 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
94 // Add this vbase to the array of vbases for current class if it is
95 // not already in the list.
96 // FIXME. Note that we do a linear search as number of such classes are
97 // very few.
98 int i;
99 for (i = 0; i < vbaseCount; ++i)
100 if (UniqueVbases[i]->getType() == VBase->getType())
101 break;
102 if (i == vbaseCount) {
103 UniqueVbases.push_back(VBase);
104 ++vbaseCount;
105 }
106 }
107 }
108 if (hasDirectVirtualBase) {
109 // Iterate one more time through the direct bases and add the virtual
110 // base to the list of vritual bases for current class.
111 for (unsigned i = 0; i < NumBases; ++i) {
112 const CXXBaseSpecifier *VBase = Bases[i];
113 if (!VBase->isVirtual())
114 continue;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000115 int j;
116 for (j = 0; j < vbaseCount; ++j)
117 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000118 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000119 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000120 UniqueVbases.push_back(VBase);
121 ++vbaseCount;
122 }
123 }
124 }
125 if (vbaseCount > 0) {
126 // build AST for inhireted, direct or indirect, virtual bases.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000127 this->VBases = new (C) CXXBaseSpecifier [vbaseCount];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000128 this->NumVBases = vbaseCount;
129 for (int i = 0; i < vbaseCount; i++) {
130 QualType QT = UniqueVbases[i]->getType();
131 CXXRecordDecl *VBaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000132 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000133 this->VBases[i] =
Douglas Gregor2aef06d2009-07-22 20:55:49 +0000134 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
135 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
136 UniqueVbases[i]->getAccessSpecifier(), QT);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000137 }
138 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000139}
140
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000141bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000142 return getCopyConstructor(Context, QualType::Const) != 0;
143}
144
145CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
146 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000147 QualType ClassType
148 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000149 DeclarationName ConstructorName
150 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000151 Context.getCanonicalType(ClassType));
152 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000153 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000154 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000155 Con != ConEnd; ++Con) {
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000156 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context,
157 FoundTQs)) {
158 if (((TypeQuals & QualType::Const) == (FoundTQs & QualType::Const)) ||
159 (!(TypeQuals & QualType::Const) && (FoundTQs & QualType::Const)))
160 return cast<CXXConstructorDecl>(*Con);
161
162 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000163 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000164 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000165}
166
Sebastian Redl64b45f72009-01-05 20:52:13 +0000167bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context) const {
168 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
169 const_cast<CXXRecordDecl*>(this)));
170 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
171
172 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000173 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000174 Op != OpEnd; ++Op) {
175 // C++ [class.copy]p9:
176 // A user-declared copy assignment operator is a non-static non-template
177 // member function of class X with exactly one parameter of type X, X&,
178 // const X&, volatile X& or const volatile X&.
179 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
180 if (Method->isStatic())
181 continue;
182 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor72564e72009-02-26 23:50:07 +0000183 const FunctionProtoType *FnType =
184 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000185 assert(FnType && "Overloaded operator has no prototype.");
186 // Don't assert on this; an invalid decl might have been left in the AST.
187 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
188 continue;
189 bool AcceptsConst = true;
190 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000191 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000192 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000193 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000194 if (!ArgType.isConstQualified())
195 AcceptsConst = false;
196 }
197 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
198 continue;
199
200 // We have a single argument of type cv X or cv X&, i.e. we've found the
201 // copy assignment operator. Return whether it accepts const arguments.
202 return AcceptsConst;
203 }
204 assert(isInvalidDecl() &&
205 "No copy assignment operator declared in valid code.");
206 return false;
207}
208
209void
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000210CXXRecordDecl::addedConstructor(ASTContext &Context,
211 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000212 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
213 // Note that we have a user-declared constructor.
214 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000215
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000216 // C++ [dcl.init.aggr]p1:
217 // An aggregate is an array or a class (clause 9) with no
218 // user-declared constructors (12.1) [...].
219 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000220
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000221 // C++ [class]p4:
222 // A POD-struct is an aggregate class [...]
223 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000224
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000225 // C++ [class.ctor]p5:
226 // A constructor is trivial if it is an implicitly-declared default
227 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000228 // FIXME: C++0x: don't do this for "= default" default constructors.
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000229 HasTrivialConstructor = false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000230
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000231 // Note when we have a user-declared copy constructor, which will
232 // suppress the implicit declaration of a copy constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000233 if (ConDecl->isCopyConstructor(Context)) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000234 UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000235
236 // C++ [class.copy]p6:
237 // A copy constructor is trivial if it is implicitly declared.
238 // FIXME: C++0x: don't do this for "= default" copy constructors.
239 HasTrivialCopyConstructor = false;
240 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000241}
242
Sebastian Redl64b45f72009-01-05 20:52:13 +0000243void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
244 CXXMethodDecl *OpDecl) {
245 // We're interested specifically in copy assignment operators.
Douglas Gregor72564e72009-02-26 23:50:07 +0000246 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000247 assert(FnType && "Overloaded operator has no proto function type.");
248 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
249 QualType ArgType = FnType->getArgType(0);
Ted Kremenek6217b802009-07-29 21:53:49 +0000250 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000251 ArgType = Ref->getPointeeType();
252
253 ArgType = ArgType.getUnqualifiedType();
254 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
255 const_cast<CXXRecordDecl*>(this)));
256
257 if (ClassType != Context.getCanonicalType(ArgType))
258 return;
259
260 // This is a copy assignment operator.
261 // Suppress the implicit declaration of a copy constructor.
262 UserDeclaredCopyAssignment = true;
263
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000264 // C++ [class.copy]p11:
265 // A copy assignment operator is trivial if it is implicitly declared.
266 // FIXME: C++0x: don't do this for "= default" copy operators.
267 HasTrivialCopyAssignment = false;
268
Sebastian Redl64b45f72009-01-05 20:52:13 +0000269 // C++ [class]p4:
270 // A POD-struct is an aggregate class that [...] has no user-defined copy
271 // assignment operator [...].
272 PlainOldData = false;
273}
274
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000275void CXXRecordDecl::addConversionFunction(ASTContext &Context,
276 CXXConversionDecl *ConvDecl) {
277 Conversions.addOverload(ConvDecl);
278}
279
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000280
281CXXConstructorDecl *
282CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
283 QualType ClassType = Context.getTypeDeclType(this);
284 DeclarationName ConstructorName
285 = Context.DeclarationNames.getCXXConstructorName(
286 Context.getCanonicalType(ClassType.getUnqualifiedType()));
287
288 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000289 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000290 Con != ConEnd; ++Con) {
291 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
292 if (Constructor->isDefaultConstructor())
293 return Constructor;
294 }
295 return 0;
296}
297
Anders Carlsson7267c162009-05-29 21:03:38 +0000298const CXXDestructorDecl *
299CXXRecordDecl::getDestructor(ASTContext &Context) {
300 QualType ClassType = Context.getTypeDeclType(this);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000301
Anders Carlsson7267c162009-05-29 21:03:38 +0000302 DeclarationName Name
303 = Context.DeclarationNames.getCXXDestructorName(ClassType);
304
305 DeclContext::lookup_iterator I, E;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000306 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000307 assert(I != E && "Did not find a destructor!");
308
309 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
310 assert(++I == E && "Found more than one destructor!");
311
312 return Dtor;
313}
314
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000315CXXMethodDecl *
316CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000317 SourceLocation L, DeclarationName N,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000318 QualType T, bool isStatic, bool isInline) {
Steve Naroff3e970492009-01-27 21:25:57 +0000319 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000320}
321
Anders Carlsson05eb2442009-05-16 23:58:37 +0000322
323typedef llvm::DenseMap<const CXXMethodDecl*,
324 std::vector<const CXXMethodDecl *> *>
325 OverriddenMethodsMapTy;
326
327static OverriddenMethodsMapTy *OverriddenMethods = 0;
328
329void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
330 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
331
332 if (!OverriddenMethods)
333 OverriddenMethods = new OverriddenMethodsMapTy();
334
335 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
336 if (!Methods)
337 Methods = new std::vector<const CXXMethodDecl *>;
338
339 Methods->push_back(MD);
340}
341
342CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
343 if (!OverriddenMethods)
344 return 0;
345
346 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
347 if (it == OverriddenMethods->end())
348 return 0;
349 return &(*it->second)[0];
350}
351
352CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
353 if (!OverriddenMethods)
354 return 0;
355
356 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
357 if (it == OverriddenMethods->end())
358 return 0;
359
360 return &(*it->second)[it->second->size()];
361}
362
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000363QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000364 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
365 // If the member function is declared const, the type of this is const X*,
366 // if the member function is declared volatile, the type of this is
367 // volatile X*, and if the member function is declared const volatile,
368 // the type of this is const volatile X*.
369
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000370 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000371
372 QualType ClassTy;
373 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
374 ClassTy = TD->getInjectedClassNameType(C);
375 else
376 ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000377 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
Anders Carlsson4e579922009-07-10 21:35:09 +0000378 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000379}
380
Douglas Gregor7ad83902008-11-05 04:29:56 +0000381CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000382CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000383 CXXConstructorDecl *C,
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000384 SourceLocation L)
385 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000386 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
387 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
388 BaseOrMember |= 0x01;
389
390 if (NumArgs > 0) {
391 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000392 // FIXME. Allocation via Context
393 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000394 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
395 this->Args[Idx] = Args[Idx];
396 }
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000397 CtorToCall = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000398}
399
400CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000401CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000402 CXXConstructorDecl *C,
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000403 SourceLocation L)
404 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000405 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
406 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
407
408 if (NumArgs > 0) {
409 this->NumArgs = NumArgs;
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +0000410 this->Args = new Stmt*[NumArgs];
Douglas Gregor7ad83902008-11-05 04:29:56 +0000411 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
412 this->Args[Idx] = Args[Idx];
413 }
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000414 CtorToCall = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000415}
416
417CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
418 delete [] Args;
419}
420
Douglas Gregorb48fe382008-10-31 09:07:45 +0000421CXXConstructorDecl *
422CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000423 SourceLocation L, DeclarationName N,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000424 QualType T, bool isExplicit,
425 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000426 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
427 "Name must refer to a constructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000428 return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000429 isImplicitlyDeclared);
430}
431
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000432bool CXXConstructorDecl::isDefaultConstructor() const {
433 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000434 // A default constructor for a class X is a constructor of class
435 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000436 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000437 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000438}
439
440bool
441CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
442 unsigned &TypeQuals) const {
443 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000444 // A non-template constructor for class X is a copy constructor
445 // if its first parameter is of type X&, const X&, volatile X& or
446 // const volatile X&, and either there are no other parameters
447 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000448 if ((getNumParams() < 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000449 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000450 return false;
451
452 const ParmVarDecl *Param = getParamDecl(0);
453
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000454 // Do we have a reference type? Rvalue references don't count.
455 const LValueReferenceType *ParamRefType =
Ted Kremenek6217b802009-07-29 21:53:49 +0000456 Param->getType()->getAs<LValueReferenceType>();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000457 if (!ParamRefType)
458 return false;
459
460 // Is it a reference to our class type?
461 QualType PointeeType
462 = Context.getCanonicalType(ParamRefType->getPointeeType());
463 QualType ClassTy
464 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
465 if (PointeeType.getUnqualifiedType() != ClassTy)
466 return false;
467
468 // We have a copy constructor.
469 TypeQuals = PointeeType.getCVRQualifiers();
470 return true;
471}
472
Douglas Gregor60d62c22008-10-31 16:23:19 +0000473bool CXXConstructorDecl::isConvertingConstructor() const {
474 // C++ [class.conv.ctor]p1:
475 // A constructor declared without the function-specifier explicit
476 // that can be called with a single parameter specifies a
477 // conversion from the type of its first parameter to the type of
478 // its class. Such a constructor is called a converting
479 // constructor.
480 if (isExplicit())
481 return false;
482
483 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000484 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000485 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000486 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000487}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000488
Douglas Gregor42a552f2008-11-05 20:51:48 +0000489CXXDestructorDecl *
490CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000491 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000492 QualType T, bool isInline,
493 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000494 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
495 "Name must refer to a destructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000496 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
497 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000498}
499
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000500void
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000501CXXDestructorDecl::Destroy(ASTContext& C) {
502 C.Deallocate(BaseOrMemberDestructions);
503 CXXMethodDecl::Destroy(C);
504}
505
506void
507CXXDestructorDecl::computeBaseOrMembersToDestroy(ASTContext &C) {
Fariborz Jahanian560de452009-07-15 22:34:08 +0000508 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000509 llvm::SmallVector<uintptr_t, 32> AllToDestruct;
510
Fariborz Jahanian560de452009-07-15 22:34:08 +0000511 for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(),
512 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000513 // Skip over virtual bases which have trivial destructors.
514 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000515 = cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000516 if (BaseClassDecl->hasTrivialDestructor())
517 continue;
518 uintptr_t Member =
Fariborz Jahaniancf183122009-07-22 00:42:46 +0000519 reinterpret_cast<uintptr_t>(VBase->getType().getTypePtr()) | VBASE;
Fariborz Jahanian560de452009-07-15 22:34:08 +0000520 AllToDestruct.push_back(Member);
521 }
522 for (CXXRecordDecl::base_class_iterator Base =
523 ClassDecl->bases_begin(),
524 E = ClassDecl->bases_end(); Base != E; ++Base) {
525 if (Base->isVirtual())
526 continue;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000527 // Skip over virtual bases which have trivial destructors.
528 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000529 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000530 if (BaseClassDecl->hasTrivialDestructor())
531 continue;
532
533 uintptr_t Member =
Fariborz Jahaniancf183122009-07-22 00:42:46 +0000534 reinterpret_cast<uintptr_t>(Base->getType().getTypePtr()) | DRCTNONVBASE;
Fariborz Jahanian560de452009-07-15 22:34:08 +0000535 AllToDestruct.push_back(Member);
536 }
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000537
Fariborz Jahanian560de452009-07-15 22:34:08 +0000538 // non-static data members.
539 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
540 E = ClassDecl->field_end(); Field != E; ++Field) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000541 QualType FieldType = C.getBaseElementType((*Field)->getType());
Fariborz Jahanian560de452009-07-15 22:34:08 +0000542
Ted Kremenek6217b802009-07-29 21:53:49 +0000543 if (const RecordType* RT = FieldType->getAs<RecordType>()) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000544 // Skip over virtual bases which have trivial destructors.
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000545 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000546 if (BaseClassDecl->hasTrivialDestructor())
547 continue;
548 uintptr_t Member = reinterpret_cast<uintptr_t>(*Field);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000549 AllToDestruct.push_back(Member);
550 }
551 }
552
553 unsigned NumDestructions = AllToDestruct.size();
554 if (NumDestructions > 0) {
555 NumBaseOrMemberDestructions = NumDestructions;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000556 BaseOrMemberDestructions = new (C) uintptr_t [NumDestructions];
Fariborz Jahanian560de452009-07-15 22:34:08 +0000557 // Insert in reverse order.
558 for (int Idx = NumDestructions-1, i=0 ; Idx >= 0; --Idx)
559 BaseOrMemberDestructions[i++] = AllToDestruct[Idx];
560 }
561}
562
563void
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000564CXXConstructorDecl::setBaseOrMemberInitializers(
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000565 ASTContext &C,
566 CXXBaseOrMemberInitializer **Initializers,
567 unsigned NumInitializers,
568 llvm::SmallVectorImpl<CXXBaseSpecifier *>& Bases,
569 llvm::SmallVectorImpl<FieldDecl *>&Fields) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000570 // We need to build the initializer AST according to order of construction
571 // and not what user specified in the Initializers list.
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000572 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
573 llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit;
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000574 llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields;
575
576 for (unsigned i = 0; i < NumInitializers; i++) {
577 CXXBaseOrMemberInitializer *Member = Initializers[i];
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000578 if (Member->isBaseInitializer())
Ted Kremenek6217b802009-07-29 21:53:49 +0000579 AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member;
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000580 else
581 AllBaseFields[Member->getMember()] = Member;
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000582 }
583
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000584 // Push virtual bases before others.
585 for (CXXRecordDecl::base_class_iterator VBase =
586 ClassDecl->vbases_begin(),
587 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000588 if (CXXBaseOrMemberInitializer *Value =
Ted Kremenek6217b802009-07-29 21:53:49 +0000589 AllBaseFields.lookup(VBase->getType()->getAs<RecordType>()))
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000590 AllToInit.push_back(Value);
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000591 else {
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000592 CXXRecordDecl *VBaseDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +0000593 cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000594 assert(VBaseDecl && "setBaseOrMemberInitializers - VBaseDecl null");
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000595 if (!VBaseDecl->getDefaultConstructor(C) &&
596 !VBase->getType()->isDependentType())
597 Bases.push_back(VBase);
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000598 CXXBaseOrMemberInitializer *Member =
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000599 new (C) CXXBaseOrMemberInitializer(VBase->getType(), 0, 0,
600 VBaseDecl->getDefaultConstructor(C),
601 SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000602 AllToInit.push_back(Member);
603 }
604 }
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000605
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000606 for (CXXRecordDecl::base_class_iterator Base =
607 ClassDecl->bases_begin(),
608 E = ClassDecl->bases_end(); Base != E; ++Base) {
609 // Virtuals are in the virtual base list and already constructed.
610 if (Base->isVirtual())
611 continue;
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000612 if (CXXBaseOrMemberInitializer *Value =
Ted Kremenek6217b802009-07-29 21:53:49 +0000613 AllBaseFields.lookup(Base->getType()->getAs<RecordType>()))
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000614 AllToInit.push_back(Value);
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000615 else {
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000616 CXXRecordDecl *BaseDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +0000617 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000618 assert(BaseDecl && "setBaseOrMemberInitializers - BaseDecl null");
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000619 if (!BaseDecl->getDefaultConstructor(C) &&
620 !Base->getType()->isDependentType())
621 Bases.push_back(Base);
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000622 CXXBaseOrMemberInitializer *Member =
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000623 new (C) CXXBaseOrMemberInitializer(Base->getType(), 0, 0,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000624 BaseDecl->getDefaultConstructor(C),
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000625 SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000626 AllToInit.push_back(Member);
627 }
628 }
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000629
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000630 // non-static data members.
631 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
632 E = ClassDecl->field_end(); Field != E; ++Field) {
Fariborz Jahanian08c63572009-07-25 01:08:28 +0000633 if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*Field)) {
634 AllToInit.push_back(Value);
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000635 continue;
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000636 }
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000637
638 QualType FT = C.getBaseElementType((*Field)->getType());
Ted Kremenek6217b802009-07-29 21:53:49 +0000639 if (const RecordType* RT = FT->getAs<RecordType>()) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000640 CXXConstructorDecl *Ctor =
641 cast<CXXRecordDecl>(RT->getDecl())->getDefaultConstructor(C);
642 if (!Ctor && !FT->isDependentType())
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000643 Fields.push_back(*Field);
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000644 CXXBaseOrMemberInitializer *Member =
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000645 new (C) CXXBaseOrMemberInitializer((*Field), 0, 0,
646 Ctor,
647 SourceLocation());
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000648 AllToInit.push_back(Member);
649 }
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000650 }
651
652 NumInitializers = AllToInit.size();
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000653 if (NumInitializers > 0) {
654 NumBaseOrMemberInitializers = NumInitializers;
655 BaseOrMemberInitializers =
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000656 new (C) CXXBaseOrMemberInitializer*[NumInitializers];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000657 for (unsigned Idx = 0; Idx < NumInitializers; ++Idx)
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000658 BaseOrMemberInitializers[Idx] = AllToInit[Idx];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000659 }
660}
661
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000662void
663CXXConstructorDecl::Destroy(ASTContext& C) {
664 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000665 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000666}
667
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000668CXXConversionDecl *
669CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000670 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000671 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000672 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
673 "Name must refer to a conversion function");
Steve Naroff3e970492009-01-27 21:25:57 +0000674 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000675}
676
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000677OverloadedFunctionDecl *
678OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000679 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000680 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000681}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000682
Douglas Gregor364e0212009-06-27 21:05:07 +0000683void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
684 Functions.push_back(F);
685 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000686}
687
Douglas Gregordaa439a2009-07-08 10:57:20 +0000688OverloadIterator::reference OverloadIterator::operator*() const {
689 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
690 return FD;
691
692 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
693 return FTD;
694
695 assert(isa<OverloadedFunctionDecl>(D));
696 return *Iter;
697}
698
699OverloadIterator &OverloadIterator::operator++() {
700 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
701 D = 0;
702 return *this;
703 }
704
705 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
706 D = 0;
707
708 return *this;
709}
710
711bool OverloadIterator::Equals(const OverloadIterator &Other) const {
712 if (!D || !Other.D)
713 return D == Other.D;
714
715 if (D != Other.D)
716 return false;
717
718 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
719}
720
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000721LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000722 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000723 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000724 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000725 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000726}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000727
728UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
729 SourceLocation L,
730 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000731 SourceRange QualifierRange,
732 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000733 SourceLocation IdentLoc,
734 NamespaceDecl *Used,
735 DeclContext *CommonAncestor) {
Douglas Gregor8419fa32009-05-30 06:31:56 +0000736 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
737 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000738}
739
Anders Carlsson68771c72009-03-28 22:58:02 +0000740NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
741 SourceLocation L,
742 SourceLocation AliasLoc,
743 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000744 SourceRange QualifierRange,
745 NestedNameSpecifier *Qualifier,
Anders Carlsson68771c72009-03-28 22:58:02 +0000746 SourceLocation IdentLoc,
747 NamedDecl *Namespace) {
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000748 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
749 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000750}
751
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000752UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
753 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
754 SourceLocation UL, NamedDecl* Target,
755 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
756 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
757 TargetNNS, IsTypeNameArg);
758}
759
Anders Carlssonfb311762009-03-14 00:25:26 +0000760StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
761 SourceLocation L, Expr *AssertExpr,
762 StringLiteral *Message) {
763 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
764}
765
766void StaticAssertDecl::Destroy(ASTContext& C) {
767 AssertExpr->Destroy(C);
768 Message->Destroy(C);
769 this->~StaticAssertDecl();
770 C.Deallocate((void *)this);
771}
772
773StaticAssertDecl::~StaticAssertDecl() {
774}
775
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000776static const char *getAccessName(AccessSpecifier AS) {
777 switch (AS) {
778 default:
779 case AS_none:
780 assert("Invalid access specifier!");
781 return 0;
782 case AS_public:
783 return "public";
784 case AS_private:
785 return "private";
786 case AS_protected:
787 return "protected";
788 }
789}
790
791const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
792 AccessSpecifier AS) {
793 return DB << getAccessName(AS);
794}
795
796