blob: 2f8a1ebd7e3520f73d3e710ad3c654afcb174615 [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 Jahanian393612e2009-07-21 22:36:06 +0000483CXXDestructorDecl::Destroy(ASTContext& C) {
484 C.Deallocate(BaseOrMemberDestructions);
485 CXXMethodDecl::Destroy(C);
486}
487
488void
489CXXDestructorDecl::computeBaseOrMembersToDestroy(ASTContext &C) {
Fariborz Jahanian560de452009-07-15 22:34:08 +0000490 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000491 llvm::SmallVector<uintptr_t, 32> AllToDestruct;
492
Fariborz Jahanian560de452009-07-15 22:34:08 +0000493 for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(),
494 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000495 // Skip over virtual bases which have trivial destructors.
496 CXXRecordDecl *BaseClassDecl
497 = cast<CXXRecordDecl>(VBase->getType()->getAsRecordType()->getDecl());
498 if (BaseClassDecl->hasTrivialDestructor())
499 continue;
500 uintptr_t Member =
501 reinterpret_cast<uintptr_t>(VBase->getType().getTypePtr()) | 0x1;
Fariborz Jahanian560de452009-07-15 22:34:08 +0000502 AllToDestruct.push_back(Member);
503 }
504 for (CXXRecordDecl::base_class_iterator Base =
505 ClassDecl->bases_begin(),
506 E = ClassDecl->bases_end(); Base != E; ++Base) {
507 if (Base->isVirtual())
508 continue;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000509 // Skip over virtual bases which have trivial destructors.
510 CXXRecordDecl *BaseClassDecl
511 = cast<CXXRecordDecl>(Base->getType()->getAsRecordType()->getDecl());
512 if (BaseClassDecl->hasTrivialDestructor())
513 continue;
514
515 uintptr_t Member =
516 reinterpret_cast<uintptr_t>(Base->getType().getTypePtr()) | 0x2;
Fariborz Jahanian560de452009-07-15 22:34:08 +0000517 AllToDestruct.push_back(Member);
518 }
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000519
Fariborz Jahanian560de452009-07-15 22:34:08 +0000520 // non-static data members.
521 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
522 E = ClassDecl->field_end(); Field != E; ++Field) {
523 QualType FieldType = C.getCanonicalType((*Field)->getType());
524 while (const ArrayType *AT = C.getAsArrayType(FieldType))
525 FieldType = AT->getElementType();
526
Ted Kremenek35366a62009-07-17 17:50:17 +0000527 if (FieldType->getAsRecordType()) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000528 // Skip over virtual bases which have trivial destructors.
529 CXXRecordDecl *BaseClassDecl
530 = cast<CXXRecordDecl>(FieldType->getAsRecordType()->getDecl());
531 if (BaseClassDecl->hasTrivialDestructor())
532 continue;
533 uintptr_t Member = reinterpret_cast<uintptr_t>(*Field);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000534 AllToDestruct.push_back(Member);
535 }
536 }
537
538 unsigned NumDestructions = AllToDestruct.size();
539 if (NumDestructions > 0) {
540 NumBaseOrMemberDestructions = NumDestructions;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000541 BaseOrMemberDestructions = new (C) uintptr_t [NumDestructions];
Fariborz Jahanian560de452009-07-15 22:34:08 +0000542 // Insert in reverse order.
543 for (int Idx = NumDestructions-1, i=0 ; Idx >= 0; --Idx)
544 BaseOrMemberDestructions[i++] = AllToDestruct[Idx];
545 }
546}
547
548void
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000549CXXConstructorDecl::setBaseOrMemberInitializers(
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000550 ASTContext &C,
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000551 CXXBaseOrMemberInitializer **Initializers,
552 unsigned NumInitializers) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000553 // We need to build the initializer AST according to order of construction
554 // and not what user specified in the Initializers list.
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000555 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
Fariborz Jahanian639dead2009-07-14 18:29:14 +0000556 // FIXME. We probably don't need to use AllToInit. But it is cleaner.
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000557 llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit;
558 // Push virtual bases before others.
559 for (CXXRecordDecl::base_class_iterator VBase =
560 ClassDecl->vbases_begin(),
561 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Ted Kremenek35366a62009-07-17 17:50:17 +0000562 const Type * T = VBase->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];
566 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(VBase->getType(), 0, 0,SourceLocation());
575 AllToInit.push_back(Member);
576 }
577 }
578 for (CXXRecordDecl::base_class_iterator Base =
579 ClassDecl->bases_begin(),
580 E = ClassDecl->bases_end(); Base != E; ++Base) {
581 // Virtuals are in the virtual base list and already constructed.
582 if (Base->isVirtual())
583 continue;
Ted Kremenek35366a62009-07-17 17:50:17 +0000584 const Type * T = Base->getType()->getAsRecordType();
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000585 unsigned int i = 0;
586 for (i = 0; i < NumInitializers; i++) {
587 CXXBaseOrMemberInitializer *Member = Initializers[i];
Fariborz Jahanian42a52172009-07-14 22:40:50 +0000588 if (Member->isBaseInitializer() &&
Ted Kremenek35366a62009-07-17 17:50:17 +0000589 Member->getBaseClass()->getAsRecordType() == T) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000590 AllToInit.push_back(Member);
591 break;
592 }
593 }
594 if (i == NumInitializers) {
595 CXXBaseOrMemberInitializer *Member =
596 new CXXBaseOrMemberInitializer(Base->getType(), 0, 0, SourceLocation());
597 AllToInit.push_back(Member);
598 }
599 }
600 // non-static data members.
601 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
602 E = ClassDecl->field_end(); Field != E; ++Field) {
603 unsigned int i = 0;
604 for (i = 0; i < NumInitializers; i++) {
605 CXXBaseOrMemberInitializer *Member = Initializers[i];
606 if (Member->isMemberInitializer() && Member->getMember() == (*Field)) {
607 AllToInit.push_back(Member);
608 break;
609 }
610 }
611 if (i == NumInitializers) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000612 QualType FieldType = C.getCanonicalType((*Field)->getType());
Fariborz Jahanian9b9f4242009-07-14 18:56:31 +0000613 while (const ArrayType *AT = C.getAsArrayType(FieldType))
614 FieldType = AT->getElementType();
615
Ted Kremenek35366a62009-07-17 17:50:17 +0000616 if (FieldType->getAsRecordType()) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000617 CXXBaseOrMemberInitializer *Member =
618 new CXXBaseOrMemberInitializer((*Field), 0, 0, SourceLocation());
619 AllToInit.push_back(Member);
620 }
621 }
622 }
623
624 NumInitializers = AllToInit.size();
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000625 if (NumInitializers > 0) {
626 NumBaseOrMemberInitializers = NumInitializers;
627 BaseOrMemberInitializers =
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000628 new (C) CXXBaseOrMemberInitializer*[NumInitializers];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000629 for (unsigned Idx = 0; Idx < NumInitializers; ++Idx)
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000630 BaseOrMemberInitializers[Idx] = AllToInit[Idx];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000631 }
632}
633
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000634void
635CXXConstructorDecl::Destroy(ASTContext& C) {
636 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000637 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000638}
639
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000640CXXConversionDecl *
641CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000642 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000643 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000644 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
645 "Name must refer to a conversion function");
Steve Naroff3e970492009-01-27 21:25:57 +0000646 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000647}
648
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000649OverloadedFunctionDecl *
650OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000651 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000652 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000653}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000654
Douglas Gregor364e0212009-06-27 21:05:07 +0000655void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
656 Functions.push_back(F);
657 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000658}
659
Douglas Gregordaa439a2009-07-08 10:57:20 +0000660OverloadIterator::reference OverloadIterator::operator*() const {
661 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
662 return FD;
663
664 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
665 return FTD;
666
667 assert(isa<OverloadedFunctionDecl>(D));
668 return *Iter;
669}
670
671OverloadIterator &OverloadIterator::operator++() {
672 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
673 D = 0;
674 return *this;
675 }
676
677 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
678 D = 0;
679
680 return *this;
681}
682
683bool OverloadIterator::Equals(const OverloadIterator &Other) const {
684 if (!D || !Other.D)
685 return D == Other.D;
686
687 if (D != Other.D)
688 return false;
689
690 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
691}
692
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000693LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000694 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000695 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000696 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000697 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000698}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000699
700UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
701 SourceLocation L,
702 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000703 SourceRange QualifierRange,
704 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000705 SourceLocation IdentLoc,
706 NamespaceDecl *Used,
707 DeclContext *CommonAncestor) {
Douglas Gregor8419fa32009-05-30 06:31:56 +0000708 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
709 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000710}
711
Anders Carlsson68771c72009-03-28 22:58:02 +0000712NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
713 SourceLocation L,
714 SourceLocation AliasLoc,
715 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000716 SourceRange QualifierRange,
717 NestedNameSpecifier *Qualifier,
Anders Carlsson68771c72009-03-28 22:58:02 +0000718 SourceLocation IdentLoc,
719 NamedDecl *Namespace) {
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000720 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
721 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000722}
723
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000724UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
725 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
726 SourceLocation UL, NamedDecl* Target,
727 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
728 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
729 TargetNNS, IsTypeNameArg);
730}
731
Anders Carlssonfb311762009-03-14 00:25:26 +0000732StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
733 SourceLocation L, Expr *AssertExpr,
734 StringLiteral *Message) {
735 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
736}
737
738void StaticAssertDecl::Destroy(ASTContext& C) {
739 AssertExpr->Destroy(C);
740 Message->Destroy(C);
741 this->~StaticAssertDecl();
742 C.Deallocate((void *)this);
743}
744
745StaticAssertDecl::~StaticAssertDecl() {
746}
747
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000748static const char *getAccessName(AccessSpecifier AS) {
749 switch (AS) {
750 default:
751 case AS_none:
752 assert("Invalid access specifier!");
753 return 0;
754 case AS_public:
755 return "public";
756 case AS_private:
757 return "private";
758 case AS_protected:
759 return "protected";
760 }
761}
762
763const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
764 AccessSpecifier AS) {
765 return DB << getAccessName(AS);
766}
767
768