blob: 78c351035ae07df893afc1ec6537e8e01584b9c8 [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),
Anders Carlsson072abef2009-04-17 02:34:54 +000033 HasTrivialConstructor(true), HasTrivialDestructor(true),
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000034 Bases(0), NumBases(0), VBases(0), NumVBases(0),
35 Conversions(DC, DeclarationName()),
Douglas Gregord475b8d2009-03-25 21:17:03 +000036 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000037
Ted Kremenek4b7c9832008-09-05 17:16:31 +000038CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
39 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000040 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000041 CXXRecordDecl* PrevDecl,
42 bool DelayTypeCreation) {
Douglas Gregor741dd9a2009-07-21 14:46:17 +000043 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id, TKL);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000044 if (!DelayTypeCreation)
45 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000046 return R;
47}
48
Douglas Gregorf8268ae2008-10-22 17:49:05 +000049CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000050}
51
52void CXXRecordDecl::Destroy(ASTContext &C) {
53 C.Deallocate(Bases);
54 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000055}
56
Douglas Gregor57c856b2008-10-23 18:13:27 +000057void
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000058CXXRecordDecl::setBases(ASTContext &C,
59 CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000060 unsigned NumBases) {
Douglas Gregor64bffa92008-11-05 16:20:31 +000061 // C++ [dcl.init.aggr]p1:
62 // An aggregate is an array or a class (clause 9) with [...]
63 // no base classes [...].
64 Aggregate = false;
65
Douglas Gregor57c856b2008-10-23 18:13:27 +000066 if (this->Bases)
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000067 C.Deallocate(this->Bases);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000068
69 int vbaseCount = 0;
70 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
71 bool hasDirectVirtualBase = false;
72
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000073 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor57c856b2008-10-23 18:13:27 +000074 this->NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000075 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor57c856b2008-10-23 18:13:27 +000076 this->Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000077 // Keep track of inherited vbases for this base class.
78 const CXXBaseSpecifier *Base = Bases[i];
79 QualType BaseType = Base->getType();
80 // Skip template types.
81 // FIXME. This means that this list must be rebuilt during template
82 // instantiation.
83 if (BaseType->isDependentType())
84 continue;
85 CXXRecordDecl *BaseClassDecl
Ted Kremenek35366a62009-07-17 17:50:17 +000086 = cast<CXXRecordDecl>(BaseType->getAsRecordType()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000087 if (Base->isVirtual())
88 hasDirectVirtualBase = true;
89 for (CXXRecordDecl::base_class_iterator VBase =
90 BaseClassDecl->vbases_begin(),
91 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
92 // Add this vbase to the array of vbases for current class if it is
93 // not already in the list.
94 // FIXME. Note that we do a linear search as number of such classes are
95 // very few.
96 int i;
97 for (i = 0; i < vbaseCount; ++i)
98 if (UniqueVbases[i]->getType() == VBase->getType())
99 break;
100 if (i == vbaseCount) {
101 UniqueVbases.push_back(VBase);
102 ++vbaseCount;
103 }
104 }
105 }
106 if (hasDirectVirtualBase) {
107 // Iterate one more time through the direct bases and add the virtual
108 // base to the list of vritual bases for current class.
109 for (unsigned i = 0; i < NumBases; ++i) {
110 const CXXBaseSpecifier *VBase = Bases[i];
111 if (!VBase->isVirtual())
112 continue;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000113 int j;
114 for (j = 0; j < vbaseCount; ++j)
115 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000116 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000117 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000118 UniqueVbases.push_back(VBase);
119 ++vbaseCount;
120 }
121 }
122 }
123 if (vbaseCount > 0) {
124 // build AST for inhireted, direct or indirect, virtual bases.
125 this->VBases = new(C) CXXBaseSpecifier [vbaseCount];
126 this->NumVBases = vbaseCount;
127 for (int i = 0; i < vbaseCount; i++) {
128 QualType QT = UniqueVbases[i]->getType();
129 CXXRecordDecl *VBaseClassDecl
Ted Kremenek35366a62009-07-17 17:50:17 +0000130 = cast<CXXRecordDecl>(QT->getAsRecordType()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000131 this->VBases[i] =
132 *new CXXBaseSpecifier(
133 VBaseClassDecl->getSourceRange(), true,
134 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
135 UniqueVbases[i]->getAccessSpecifier(), QT);
136 }
137 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000138}
139
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000140bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000141 return getCopyConstructor(Context, QualType::Const) != 0;
142}
143
144CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
145 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000146 QualType ClassType
147 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000148 DeclarationName ConstructorName
149 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000150 Context.getCanonicalType(ClassType));
151 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000152 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000153 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000154 Con != ConEnd; ++Con) {
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000155 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context,
156 FoundTQs)) {
157 if (((TypeQuals & QualType::Const) == (FoundTQs & QualType::Const)) ||
158 (!(TypeQuals & QualType::Const) && (FoundTQs & QualType::Const)))
159 return cast<CXXConstructorDecl>(*Con);
160
161 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000162 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000163 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000164}
165
Sebastian Redl64b45f72009-01-05 20:52:13 +0000166bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context) const {
167 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
168 const_cast<CXXRecordDecl*>(this)));
169 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
170
171 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000172 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000173 Op != OpEnd; ++Op) {
174 // C++ [class.copy]p9:
175 // A user-declared copy assignment operator is a non-static non-template
176 // member function of class X with exactly one parameter of type X, X&,
177 // const X&, volatile X& or const volatile X&.
178 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
179 if (Method->isStatic())
180 continue;
181 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor72564e72009-02-26 23:50:07 +0000182 const FunctionProtoType *FnType =
183 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000184 assert(FnType && "Overloaded operator has no prototype.");
185 // Don't assert on this; an invalid decl might have been left in the AST.
186 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
187 continue;
188 bool AcceptsConst = true;
189 QualType ArgType = FnType->getArgType(0);
Ted Kremenek35366a62009-07-17 17:50:17 +0000190 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000191 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000192 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000193 if (!ArgType.isConstQualified())
194 AcceptsConst = false;
195 }
196 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
197 continue;
198
199 // We have a single argument of type cv X or cv X&, i.e. we've found the
200 // copy assignment operator. Return whether it accepts const arguments.
201 return AcceptsConst;
202 }
203 assert(isInvalidDecl() &&
204 "No copy assignment operator declared in valid code.");
205 return false;
206}
207
208void
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000209CXXRecordDecl::addedConstructor(ASTContext &Context,
210 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000211 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
212 // Note that we have a user-declared constructor.
213 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000214
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000215 // C++ [dcl.init.aggr]p1:
216 // An aggregate is an array or a class (clause 9) with no
217 // user-declared constructors (12.1) [...].
218 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000219
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000220 // C++ [class]p4:
221 // A POD-struct is an aggregate class [...]
222 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000223
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000224 // C++ [class.ctor]p5:
225 // A constructor is trivial if it is an implicitly-declared default
226 // constructor.
227 HasTrivialConstructor = false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000228
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000229 // Note when we have a user-declared copy constructor, which will
230 // suppress the implicit declaration of a copy constructor.
231 if (ConDecl->isCopyConstructor(Context))
232 UserDeclaredCopyConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000233}
234
Sebastian Redl64b45f72009-01-05 20:52:13 +0000235void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
236 CXXMethodDecl *OpDecl) {
237 // We're interested specifically in copy assignment operators.
Douglas Gregor72564e72009-02-26 23:50:07 +0000238 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000239 assert(FnType && "Overloaded operator has no proto function type.");
240 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
241 QualType ArgType = FnType->getArgType(0);
Ted Kremenek35366a62009-07-17 17:50:17 +0000242 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000243 ArgType = Ref->getPointeeType();
244
245 ArgType = ArgType.getUnqualifiedType();
246 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
247 const_cast<CXXRecordDecl*>(this)));
248
249 if (ClassType != Context.getCanonicalType(ArgType))
250 return;
251
252 // This is a copy assignment operator.
253 // Suppress the implicit declaration of a copy constructor.
254 UserDeclaredCopyAssignment = true;
255
256 // C++ [class]p4:
257 // A POD-struct is an aggregate class that [...] has no user-defined copy
258 // assignment operator [...].
259 PlainOldData = false;
260}
261
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000262void CXXRecordDecl::addConversionFunction(ASTContext &Context,
263 CXXConversionDecl *ConvDecl) {
264 Conversions.addOverload(ConvDecl);
265}
266
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000267
268CXXConstructorDecl *
269CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
270 QualType ClassType = Context.getTypeDeclType(this);
271 DeclarationName ConstructorName
272 = Context.DeclarationNames.getCXXConstructorName(
273 Context.getCanonicalType(ClassType.getUnqualifiedType()));
274
275 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000276 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000277 Con != ConEnd; ++Con) {
278 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
279 if (Constructor->isDefaultConstructor())
280 return Constructor;
281 }
282 return 0;
283}
284
Anders Carlsson7267c162009-05-29 21:03:38 +0000285const CXXDestructorDecl *
286CXXRecordDecl::getDestructor(ASTContext &Context) {
287 QualType ClassType = Context.getTypeDeclType(this);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000288
Anders Carlsson7267c162009-05-29 21:03:38 +0000289 DeclarationName Name
290 = Context.DeclarationNames.getCXXDestructorName(ClassType);
291
292 DeclContext::lookup_iterator I, E;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000293 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000294 assert(I != E && "Did not find a destructor!");
295
296 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
297 assert(++I == E && "Found more than one destructor!");
298
299 return Dtor;
300}
301
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000302CXXMethodDecl *
303CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000304 SourceLocation L, DeclarationName N,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000305 QualType T, bool isStatic, bool isInline) {
Steve Naroff3e970492009-01-27 21:25:57 +0000306 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000307}
308
Anders Carlsson05eb2442009-05-16 23:58:37 +0000309
310typedef llvm::DenseMap<const CXXMethodDecl*,
311 std::vector<const CXXMethodDecl *> *>
312 OverriddenMethodsMapTy;
313
314static OverriddenMethodsMapTy *OverriddenMethods = 0;
315
316void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
317 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
318
319 if (!OverriddenMethods)
320 OverriddenMethods = new OverriddenMethodsMapTy();
321
322 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
323 if (!Methods)
324 Methods = new std::vector<const CXXMethodDecl *>;
325
326 Methods->push_back(MD);
327}
328
329CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
330 if (!OverriddenMethods)
331 return 0;
332
333 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
334 if (it == OverriddenMethods->end())
335 return 0;
336 return &(*it->second)[0];
337}
338
339CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
340 if (!OverriddenMethods)
341 return 0;
342
343 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
344 if (it == OverriddenMethods->end())
345 return 0;
346
347 return &(*it->second)[it->second->size()];
348}
349
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000350QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000351 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
352 // If the member function is declared const, the type of this is const X*,
353 // if the member function is declared volatile, the type of this is
354 // volatile X*, and if the member function is declared const volatile,
355 // the type of this is const volatile X*.
356
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000357 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000358
359 QualType ClassTy;
360 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
361 ClassTy = TD->getInjectedClassNameType(C);
362 else
363 ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000364 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
Anders Carlsson4e579922009-07-10 21:35:09 +0000365 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000366}
367
Douglas Gregor7ad83902008-11-05 04:29:56 +0000368CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000369CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
370 SourceLocation L)
371 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000372 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
373 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
374 BaseOrMember |= 0x01;
375
376 if (NumArgs > 0) {
377 this->NumArgs = NumArgs;
378 this->Args = new Expr*[NumArgs];
379 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
380 this->Args[Idx] = Args[Idx];
381 }
382}
383
384CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000385CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
386 SourceLocation L)
387 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000388 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
389 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
390
391 if (NumArgs > 0) {
392 this->NumArgs = NumArgs;
393 this->Args = new Expr*[NumArgs];
394 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
395 this->Args[Idx] = Args[Idx];
396 }
397}
398
399CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
400 delete [] Args;
401}
402
Douglas Gregorb48fe382008-10-31 09:07:45 +0000403CXXConstructorDecl *
404CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000405 SourceLocation L, DeclarationName N,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000406 QualType T, bool isExplicit,
407 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000408 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
409 "Name must refer to a constructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000410 return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000411 isImplicitlyDeclared);
412}
413
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000414bool CXXConstructorDecl::isDefaultConstructor() const {
415 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000416 // A default constructor for a class X is a constructor of class
417 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000418 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000419 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000420}
421
422bool
423CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
424 unsigned &TypeQuals) const {
425 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000426 // A non-template constructor for class X is a copy constructor
427 // if its first parameter is of type X&, const X&, volatile X& or
428 // const volatile X&, and either there are no other parameters
429 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000430 if ((getNumParams() < 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000431 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000432 return false;
433
434 const ParmVarDecl *Param = getParamDecl(0);
435
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000436 // Do we have a reference type? Rvalue references don't count.
437 const LValueReferenceType *ParamRefType =
Ted Kremenek35366a62009-07-17 17:50:17 +0000438 Param->getType()->getAsLValueReferenceType();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000439 if (!ParamRefType)
440 return false;
441
442 // Is it a reference to our class type?
443 QualType PointeeType
444 = Context.getCanonicalType(ParamRefType->getPointeeType());
445 QualType ClassTy
446 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
447 if (PointeeType.getUnqualifiedType() != ClassTy)
448 return false;
449
450 // We have a copy constructor.
451 TypeQuals = PointeeType.getCVRQualifiers();
452 return true;
453}
454
Douglas Gregor60d62c22008-10-31 16:23:19 +0000455bool CXXConstructorDecl::isConvertingConstructor() const {
456 // C++ [class.conv.ctor]p1:
457 // A constructor declared without the function-specifier explicit
458 // that can be called with a single parameter specifies a
459 // conversion from the type of its first parameter to the type of
460 // its class. Such a constructor is called a converting
461 // constructor.
462 if (isExplicit())
463 return false;
464
465 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000466 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000467 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000468 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000469}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000470
Douglas Gregor42a552f2008-11-05 20:51:48 +0000471CXXDestructorDecl *
472CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000473 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000474 QualType T, bool isInline,
475 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000476 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
477 "Name must refer to a destructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000478 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
479 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000480}
481
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000482void
Fariborz Jahanian560de452009-07-15 22:34:08 +0000483CXXDestructorDecl::setBaseOrMemberDestructions(ASTContext &C) {
484 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
485 llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToDestruct;
486 for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(),
487 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
488 CXXBaseOrMemberInitializer *Member =
489 new CXXBaseOrMemberInitializer(VBase->getType(), 0, 0,SourceLocation());
490 AllToDestruct.push_back(Member);
491 }
492 for (CXXRecordDecl::base_class_iterator Base =
493 ClassDecl->bases_begin(),
494 E = ClassDecl->bases_end(); Base != E; ++Base) {
495 if (Base->isVirtual())
496 continue;
497 CXXBaseOrMemberInitializer *Member =
498 new CXXBaseOrMemberInitializer(Base->getType(), 0, 0, SourceLocation());
499 AllToDestruct.push_back(Member);
500 }
501 // non-static data members.
502 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
503 E = ClassDecl->field_end(); Field != E; ++Field) {
504 QualType FieldType = C.getCanonicalType((*Field)->getType());
505 while (const ArrayType *AT = C.getAsArrayType(FieldType))
506 FieldType = AT->getElementType();
507
Ted Kremenek35366a62009-07-17 17:50:17 +0000508 if (FieldType->getAsRecordType()) {
Fariborz Jahanian560de452009-07-15 22:34:08 +0000509 CXXBaseOrMemberInitializer *Member =
510 new CXXBaseOrMemberInitializer((*Field), 0, 0, SourceLocation());
511 AllToDestruct.push_back(Member);
512 }
513 }
514
515 unsigned NumDestructions = AllToDestruct.size();
516 if (NumDestructions > 0) {
517 NumBaseOrMemberDestructions = NumDestructions;
518 BaseOrMemberDestructions =
519 new (C) CXXBaseOrMemberInitializer*[NumDestructions];
520 // Insert in reverse order.
521 for (int Idx = NumDestructions-1, i=0 ; Idx >= 0; --Idx)
522 BaseOrMemberDestructions[i++] = AllToDestruct[Idx];
523 }
524}
525
526void
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000527CXXConstructorDecl::setBaseOrMemberInitializers(
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000528 ASTContext &C,
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000529 CXXBaseOrMemberInitializer **Initializers,
530 unsigned NumInitializers) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000531 // We need to build the initializer AST according to order of construction
532 // and not what user specified in the Initializers list.
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000533 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
Fariborz Jahanian639dead2009-07-14 18:29:14 +0000534 // FIXME. We probably don't need to use AllToInit. But it is cleaner.
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000535 llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit;
536 // Push virtual bases before others.
537 for (CXXRecordDecl::base_class_iterator VBase =
538 ClassDecl->vbases_begin(),
539 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Ted Kremenek35366a62009-07-17 17:50:17 +0000540 const Type * T = VBase->getType()->getAsRecordType();
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000541 unsigned int i = 0;
542 for (i = 0; i < NumInitializers; i++) {
543 CXXBaseOrMemberInitializer *Member = Initializers[i];
544 if (Member->isBaseInitializer() &&
Ted Kremenek35366a62009-07-17 17:50:17 +0000545 Member->getBaseClass()->getAsRecordType() == T) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000546 AllToInit.push_back(Member);
547 break;
548 }
549 }
550 if (i == NumInitializers) {
551 CXXBaseOrMemberInitializer *Member =
552 new CXXBaseOrMemberInitializer(VBase->getType(), 0, 0,SourceLocation());
553 AllToInit.push_back(Member);
554 }
555 }
556 for (CXXRecordDecl::base_class_iterator Base =
557 ClassDecl->bases_begin(),
558 E = ClassDecl->bases_end(); Base != E; ++Base) {
559 // Virtuals are in the virtual base list and already constructed.
560 if (Base->isVirtual())
561 continue;
Ted Kremenek35366a62009-07-17 17:50:17 +0000562 const Type * T = Base->getType()->getAsRecordType();
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000563 unsigned int i = 0;
564 for (i = 0; i < NumInitializers; i++) {
565 CXXBaseOrMemberInitializer *Member = Initializers[i];
Fariborz Jahanian42a52172009-07-14 22:40:50 +0000566 if (Member->isBaseInitializer() &&
Ted Kremenek35366a62009-07-17 17:50:17 +0000567 Member->getBaseClass()->getAsRecordType() == T) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000568 AllToInit.push_back(Member);
569 break;
570 }
571 }
572 if (i == NumInitializers) {
573 CXXBaseOrMemberInitializer *Member =
574 new CXXBaseOrMemberInitializer(Base->getType(), 0, 0, SourceLocation());
575 AllToInit.push_back(Member);
576 }
577 }
578 // non-static data members.
579 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
580 E = ClassDecl->field_end(); Field != E; ++Field) {
581 unsigned int i = 0;
582 for (i = 0; i < NumInitializers; i++) {
583 CXXBaseOrMemberInitializer *Member = Initializers[i];
584 if (Member->isMemberInitializer() && Member->getMember() == (*Field)) {
585 AllToInit.push_back(Member);
586 break;
587 }
588 }
589 if (i == NumInitializers) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000590 QualType FieldType = C.getCanonicalType((*Field)->getType());
Fariborz Jahanian9b9f4242009-07-14 18:56:31 +0000591 while (const ArrayType *AT = C.getAsArrayType(FieldType))
592 FieldType = AT->getElementType();
593
Ted Kremenek35366a62009-07-17 17:50:17 +0000594 if (FieldType->getAsRecordType()) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000595 CXXBaseOrMemberInitializer *Member =
596 new CXXBaseOrMemberInitializer((*Field), 0, 0, SourceLocation());
597 AllToInit.push_back(Member);
598 }
599 }
600 }
601
602 NumInitializers = AllToInit.size();
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000603 if (NumInitializers > 0) {
604 NumBaseOrMemberInitializers = NumInitializers;
605 BaseOrMemberInitializers =
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000606 new (C) CXXBaseOrMemberInitializer*[NumInitializers];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000607 for (unsigned Idx = 0; Idx < NumInitializers; ++Idx)
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000608 BaseOrMemberInitializers[Idx] = AllToInit[Idx];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000609 }
610}
611
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000612void
613CXXConstructorDecl::Destroy(ASTContext& C) {
614 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000615 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000616}
617
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000618CXXConversionDecl *
619CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000620 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000621 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000622 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
623 "Name must refer to a conversion function");
Steve Naroff3e970492009-01-27 21:25:57 +0000624 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000625}
626
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000627OverloadedFunctionDecl *
628OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000629 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000630 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000631}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000632
Douglas Gregor364e0212009-06-27 21:05:07 +0000633void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
634 Functions.push_back(F);
635 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000636}
637
Douglas Gregordaa439a2009-07-08 10:57:20 +0000638OverloadIterator::reference OverloadIterator::operator*() const {
639 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
640 return FD;
641
642 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
643 return FTD;
644
645 assert(isa<OverloadedFunctionDecl>(D));
646 return *Iter;
647}
648
649OverloadIterator &OverloadIterator::operator++() {
650 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
651 D = 0;
652 return *this;
653 }
654
655 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
656 D = 0;
657
658 return *this;
659}
660
661bool OverloadIterator::Equals(const OverloadIterator &Other) const {
662 if (!D || !Other.D)
663 return D == Other.D;
664
665 if (D != Other.D)
666 return false;
667
668 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
669}
670
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000671LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000672 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000673 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000674 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000675 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000676}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000677
678UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
679 SourceLocation L,
680 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000681 SourceRange QualifierRange,
682 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000683 SourceLocation IdentLoc,
684 NamespaceDecl *Used,
685 DeclContext *CommonAncestor) {
Douglas Gregor8419fa32009-05-30 06:31:56 +0000686 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
687 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000688}
689
Anders Carlsson68771c72009-03-28 22:58:02 +0000690NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
691 SourceLocation L,
692 SourceLocation AliasLoc,
693 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000694 SourceRange QualifierRange,
695 NestedNameSpecifier *Qualifier,
Anders Carlsson68771c72009-03-28 22:58:02 +0000696 SourceLocation IdentLoc,
697 NamedDecl *Namespace) {
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000698 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
699 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000700}
701
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000702UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
703 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
704 SourceLocation UL, NamedDecl* Target,
705 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
706 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
707 TargetNNS, IsTypeNameArg);
708}
709
Anders Carlssonfb311762009-03-14 00:25:26 +0000710StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
711 SourceLocation L, Expr *AssertExpr,
712 StringLiteral *Message) {
713 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
714}
715
716void StaticAssertDecl::Destroy(ASTContext& C) {
717 AssertExpr->Destroy(C);
718 Message->Destroy(C);
719 this->~StaticAssertDecl();
720 C.Deallocate((void *)this);
721}
722
723StaticAssertDecl::~StaticAssertDecl() {
724}
725
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000726static const char *getAccessName(AccessSpecifier AS) {
727 switch (AS) {
728 default:
729 case AS_none:
730 assert("Invalid access specifier!");
731 return 0;
732 case AS_public:
733 return "public";
734 case AS_private:
735 return "private";
736 case AS_protected:
737 return "protected";
738 }
739}
740
741const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
742 AccessSpecifier AS) {
743 return DB << getAccessName(AS);
744}
745
746