blob: 7b5a29028c4c7b5d3eaaf722c2f5b48a3e463bae [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 Kremenek35366a62009-07-17 17:50:17 +000088 = cast<CXXRecordDecl>(BaseType->getAsRecordType()->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 Kremenek35366a62009-07-17 17:50:17 +0000132 = cast<CXXRecordDecl>(QT->getAsRecordType()->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 Kremenek35366a62009-07-17 17:50:17 +0000191 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType()) {
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 Kremenek35366a62009-07-17 17:50:17 +0000250 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType())
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;
392 this->Args = new Expr*[NumArgs];
393 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
394 this->Args[Idx] = Args[Idx];
395 }
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000396 CtorToCall = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000397}
398
399CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000400CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000401 CXXConstructorDecl *C,
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000402 SourceLocation L)
403 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000404 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
405 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
406
407 if (NumArgs > 0) {
408 this->NumArgs = NumArgs;
409 this->Args = new Expr*[NumArgs];
410 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
411 this->Args[Idx] = Args[Idx];
412 }
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000413 CtorToCall = C;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000414}
415
416CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
417 delete [] Args;
418}
419
Douglas Gregorb48fe382008-10-31 09:07:45 +0000420CXXConstructorDecl *
421CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000422 SourceLocation L, DeclarationName N,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000423 QualType T, bool isExplicit,
424 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000425 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
426 "Name must refer to a constructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000427 return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000428 isImplicitlyDeclared);
429}
430
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000431bool CXXConstructorDecl::isDefaultConstructor() const {
432 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000433 // A default constructor for a class X is a constructor of class
434 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000435 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000436 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000437}
438
439bool
440CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
441 unsigned &TypeQuals) const {
442 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000443 // A non-template constructor for class X is a copy constructor
444 // if its first parameter is of type X&, const X&, volatile X& or
445 // const volatile X&, and either there are no other parameters
446 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000447 if ((getNumParams() < 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000448 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000449 return false;
450
451 const ParmVarDecl *Param = getParamDecl(0);
452
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000453 // Do we have a reference type? Rvalue references don't count.
454 const LValueReferenceType *ParamRefType =
Ted Kremenek35366a62009-07-17 17:50:17 +0000455 Param->getType()->getAsLValueReferenceType();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000456 if (!ParamRefType)
457 return false;
458
459 // Is it a reference to our class type?
460 QualType PointeeType
461 = Context.getCanonicalType(ParamRefType->getPointeeType());
462 QualType ClassTy
463 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
464 if (PointeeType.getUnqualifiedType() != ClassTy)
465 return false;
466
467 // We have a copy constructor.
468 TypeQuals = PointeeType.getCVRQualifiers();
469 return true;
470}
471
Douglas Gregor60d62c22008-10-31 16:23:19 +0000472bool CXXConstructorDecl::isConvertingConstructor() const {
473 // C++ [class.conv.ctor]p1:
474 // A constructor declared without the function-specifier explicit
475 // that can be called with a single parameter specifies a
476 // conversion from the type of its first parameter to the type of
477 // its class. Such a constructor is called a converting
478 // constructor.
479 if (isExplicit())
480 return false;
481
482 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000483 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000484 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000485 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000486}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000487
Douglas Gregor42a552f2008-11-05 20:51:48 +0000488CXXDestructorDecl *
489CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000490 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000491 QualType T, bool isInline,
492 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000493 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
494 "Name must refer to a destructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000495 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
496 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000497}
498
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000499void
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000500CXXDestructorDecl::Destroy(ASTContext& C) {
501 C.Deallocate(BaseOrMemberDestructions);
502 CXXMethodDecl::Destroy(C);
503}
504
505void
506CXXDestructorDecl::computeBaseOrMembersToDestroy(ASTContext &C) {
Fariborz Jahanian560de452009-07-15 22:34:08 +0000507 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000508 llvm::SmallVector<uintptr_t, 32> AllToDestruct;
509
Fariborz Jahanian560de452009-07-15 22:34:08 +0000510 for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(),
511 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000512 // Skip over virtual bases which have trivial destructors.
513 CXXRecordDecl *BaseClassDecl
514 = cast<CXXRecordDecl>(VBase->getType()->getAsRecordType()->getDecl());
515 if (BaseClassDecl->hasTrivialDestructor())
516 continue;
517 uintptr_t Member =
Fariborz Jahaniancf183122009-07-22 00:42:46 +0000518 reinterpret_cast<uintptr_t>(VBase->getType().getTypePtr()) | VBASE;
Fariborz Jahanian560de452009-07-15 22:34:08 +0000519 AllToDestruct.push_back(Member);
520 }
521 for (CXXRecordDecl::base_class_iterator Base =
522 ClassDecl->bases_begin(),
523 E = ClassDecl->bases_end(); Base != E; ++Base) {
524 if (Base->isVirtual())
525 continue;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000526 // Skip over virtual bases which have trivial destructors.
527 CXXRecordDecl *BaseClassDecl
528 = cast<CXXRecordDecl>(Base->getType()->getAsRecordType()->getDecl());
529 if (BaseClassDecl->hasTrivialDestructor())
530 continue;
531
532 uintptr_t Member =
Fariborz Jahaniancf183122009-07-22 00:42:46 +0000533 reinterpret_cast<uintptr_t>(Base->getType().getTypePtr()) | DRCTNONVBASE;
Fariborz Jahanian560de452009-07-15 22:34:08 +0000534 AllToDestruct.push_back(Member);
535 }
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000536
Fariborz Jahanian560de452009-07-15 22:34:08 +0000537 // non-static data members.
538 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
539 E = ClassDecl->field_end(); Field != E; ++Field) {
540 QualType FieldType = C.getCanonicalType((*Field)->getType());
541 while (const ArrayType *AT = C.getAsArrayType(FieldType))
542 FieldType = AT->getElementType();
543
Ted Kremenek35366a62009-07-17 17:50:17 +0000544 if (FieldType->getAsRecordType()) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000545 // Skip over virtual bases which have trivial destructors.
546 CXXRecordDecl *BaseClassDecl
547 = cast<CXXRecordDecl>(FieldType->getAsRecordType()->getDecl());
548 if (BaseClassDecl->hasTrivialDestructor())
549 continue;
550 uintptr_t Member = reinterpret_cast<uintptr_t>(*Field);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000551 AllToDestruct.push_back(Member);
552 }
553 }
554
555 unsigned NumDestructions = AllToDestruct.size();
556 if (NumDestructions > 0) {
557 NumBaseOrMemberDestructions = NumDestructions;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000558 BaseOrMemberDestructions = new (C) uintptr_t [NumDestructions];
Fariborz Jahanian560de452009-07-15 22:34:08 +0000559 // Insert in reverse order.
560 for (int Idx = NumDestructions-1, i=0 ; Idx >= 0; --Idx)
561 BaseOrMemberDestructions[i++] = AllToDestruct[Idx];
562 }
563}
564
565void
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000566CXXConstructorDecl::setBaseOrMemberInitializers(
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000567 ASTContext &C,
568 CXXBaseOrMemberInitializer **Initializers,
569 unsigned NumInitializers,
570 llvm::SmallVectorImpl<CXXBaseSpecifier *>& Bases,
571 llvm::SmallVectorImpl<FieldDecl *>&Fields) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000572 // We need to build the initializer AST according to order of construction
573 // and not what user specified in the Initializers list.
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000574 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
575 llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit;
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000576 llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields;
577
578 for (unsigned i = 0; i < NumInitializers; i++) {
579 CXXBaseOrMemberInitializer *Member = Initializers[i];
580 const void * Key = Member->isBaseInitializer() ?
581 reinterpret_cast<const void *>(
582 Member->getBaseClass()->getAsRecordType()) :
583 reinterpret_cast<const void *>(Member->getMember());
584 AllBaseFields[Key] = Member;
585 }
586
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000587 // Push virtual bases before others.
588 for (CXXRecordDecl::base_class_iterator VBase =
589 ClassDecl->vbases_begin(),
590 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000591 const void *Key = reinterpret_cast<const void *>(
592 VBase->getType()->getAsRecordType());
593 if (AllBaseFields[Key])
594 AllToInit.push_back(AllBaseFields[Key]);
595 else {
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000596 CXXRecordDecl *VBaseDecl =
597 cast<CXXRecordDecl>(VBase->getType()->getAsRecordType()->getDecl());
598 assert(VBaseDecl && "setBaseOrMemberInitializers - VBaseDecl null");
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000599 if (!VBaseDecl->getDefaultConstructor(C) &&
600 !VBase->getType()->isDependentType())
601 Bases.push_back(VBase);
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000602 CXXBaseOrMemberInitializer *Member =
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000603 new (C) CXXBaseOrMemberInitializer(VBase->getType(), 0, 0,
604 VBaseDecl->getDefaultConstructor(C),
605 SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000606 AllToInit.push_back(Member);
607 }
608 }
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000609
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000610 for (CXXRecordDecl::base_class_iterator Base =
611 ClassDecl->bases_begin(),
612 E = ClassDecl->bases_end(); Base != E; ++Base) {
613 // Virtuals are in the virtual base list and already constructed.
614 if (Base->isVirtual())
615 continue;
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000616 const void *Key = reinterpret_cast<const void *>(
617 Base->getType()->getAsRecordType());
618 if (AllBaseFields[Key])
619 AllToInit.push_back(AllBaseFields[Key]);
620 else {
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000621 CXXRecordDecl *BaseDecl =
622 cast<CXXRecordDecl>(Base->getType()->getAsRecordType()->getDecl());
623 assert(BaseDecl && "setBaseOrMemberInitializers - BaseDecl null");
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000624 if (!BaseDecl->getDefaultConstructor(C) &&
625 !Base->getType()->isDependentType())
626 Bases.push_back(Base);
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000627 CXXBaseOrMemberInitializer *Member =
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000628 new (C) CXXBaseOrMemberInitializer(Base->getType(), 0, 0,
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000629 BaseDecl->getDefaultConstructor(C),
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000630 SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000631 AllToInit.push_back(Member);
632 }
633 }
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000634
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000635 // non-static data members.
636 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
637 E = ClassDecl->field_end(); Field != E; ++Field) {
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000638 const void * Key = reinterpret_cast<const void *>(*Field);
639 if (AllBaseFields[Key]) {
640 AllToInit.push_back(AllBaseFields[Key]);
641 continue;
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000642 }
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000643 QualType FieldType = C.getCanonicalType((*Field)->getType());
644 while (const ArrayType *AT = C.getAsArrayType(FieldType))
645 FieldType = AT->getElementType();
Fariborz Jahanian9b9f4242009-07-14 18:56:31 +0000646
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000647 if (FieldType->getAsRecordType()) {
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000648 CXXConstructorDecl *Ctor = 0;
649 if (CXXRecordDecl *FieldClassDecl =
650 dyn_cast<CXXRecordDecl>(FieldType->getAsRecordType()->getDecl()))
651 Ctor = FieldClassDecl->getDefaultConstructor(C);
Fariborz Jahanian87595e42009-07-23 23:32:59 +0000652 if (!Ctor && !FieldType->isDependentType())
653 Fields.push_back(*Field);
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000654 CXXBaseOrMemberInitializer *Member =
Fariborz Jahaniand7b27e12009-07-23 00:42:24 +0000655 new (C) CXXBaseOrMemberInitializer((*Field), 0, 0,
656 Ctor,
657 SourceLocation());
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000658 AllToInit.push_back(Member);
659 }
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000660 }
661
662 NumInitializers = AllToInit.size();
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000663 if (NumInitializers > 0) {
664 NumBaseOrMemberInitializers = NumInitializers;
665 BaseOrMemberInitializers =
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000666 new (C) CXXBaseOrMemberInitializer*[NumInitializers];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000667 for (unsigned Idx = 0; Idx < NumInitializers; ++Idx)
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000668 BaseOrMemberInitializers[Idx] = AllToInit[Idx];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000669 }
670}
671
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000672void
673CXXConstructorDecl::Destroy(ASTContext& C) {
674 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000675 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000676}
677
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000678CXXConversionDecl *
679CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000680 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000681 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000682 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
683 "Name must refer to a conversion function");
Steve Naroff3e970492009-01-27 21:25:57 +0000684 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000685}
686
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000687OverloadedFunctionDecl *
688OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000689 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000690 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000691}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000692
Douglas Gregor364e0212009-06-27 21:05:07 +0000693void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
694 Functions.push_back(F);
695 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000696}
697
Douglas Gregordaa439a2009-07-08 10:57:20 +0000698OverloadIterator::reference OverloadIterator::operator*() const {
699 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
700 return FD;
701
702 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
703 return FTD;
704
705 assert(isa<OverloadedFunctionDecl>(D));
706 return *Iter;
707}
708
709OverloadIterator &OverloadIterator::operator++() {
710 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
711 D = 0;
712 return *this;
713 }
714
715 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
716 D = 0;
717
718 return *this;
719}
720
721bool OverloadIterator::Equals(const OverloadIterator &Other) const {
722 if (!D || !Other.D)
723 return D == Other.D;
724
725 if (D != Other.D)
726 return false;
727
728 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
729}
730
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000731LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000732 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000733 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000734 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000735 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000736}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000737
738UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
739 SourceLocation L,
740 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000741 SourceRange QualifierRange,
742 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000743 SourceLocation IdentLoc,
744 NamespaceDecl *Used,
745 DeclContext *CommonAncestor) {
Douglas Gregor8419fa32009-05-30 06:31:56 +0000746 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
747 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000748}
749
Anders Carlsson68771c72009-03-28 22:58:02 +0000750NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
751 SourceLocation L,
752 SourceLocation AliasLoc,
753 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000754 SourceRange QualifierRange,
755 NestedNameSpecifier *Qualifier,
Anders Carlsson68771c72009-03-28 22:58:02 +0000756 SourceLocation IdentLoc,
757 NamedDecl *Namespace) {
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000758 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
759 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000760}
761
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000762UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
763 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
764 SourceLocation UL, NamedDecl* Target,
765 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
766 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
767 TargetNNS, IsTypeNameArg);
768}
769
Anders Carlssonfb311762009-03-14 00:25:26 +0000770StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
771 SourceLocation L, Expr *AssertExpr,
772 StringLiteral *Message) {
773 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
774}
775
776void StaticAssertDecl::Destroy(ASTContext& C) {
777 AssertExpr->Destroy(C);
778 Message->Destroy(C);
779 this->~StaticAssertDecl();
780 C.Deallocate((void *)this);
781}
782
783StaticAssertDecl::~StaticAssertDecl() {
784}
785
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000786static const char *getAccessName(AccessSpecifier AS) {
787 switch (AS) {
788 default:
789 case AS_none:
790 assert("Invalid access specifier!");
791 return 0;
792 case AS_public:
793 return "public";
794 case AS_private:
795 return "private";
796 case AS_protected:
797 return "protected";
798 }
799}
800
801const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
802 AccessSpecifier AS) {
803 return DB << getAccessName(AS);
804}
805
806