blob: 70c7a1bd5053e50f16d6d65ce7746f765fadf4f0 [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000017#include "clang/AST/Expr.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000018#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000019#include "llvm/ADT/STLExtras.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// Decl Allocation/Deallocation Method Implementations
24//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000025
Douglas Gregor3e00bad2009-02-17 01:05:43 +000026CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Douglas Gregor7d7e6722008-11-12 23:21:09 +000027 SourceLocation L, IdentifierInfo *Id)
Douglas Gregor3e00bad2009-02-17 01:05:43 +000028 : RecordDecl(K, TK, DC, L, Id),
Douglas Gregor7d7e6722008-11-12 23:21:09 +000029 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000030 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Anders Carlsson67e4dd22009-03-22 01:52:17 +000031 Aggregate(true), PlainOldData(true), Polymorphic(false), Abstract(false),
Anders Carlsson072abef2009-04-17 02:34:54 +000032 HasTrivialConstructor(true), HasTrivialDestructor(true),
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000033 Bases(0), NumBases(0), VBases(0), NumVBases(0),
34 Conversions(DC, DeclarationName()),
Douglas Gregord475b8d2009-03-25 21:17:03 +000035 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000036
Ted Kremenek4b7c9832008-09-05 17:16:31 +000037CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
38 SourceLocation L, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000039 CXXRecordDecl* PrevDecl,
40 bool DelayTypeCreation) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +000041 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000042 if (!DelayTypeCreation)
43 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000044 return R;
45}
46
Douglas Gregorf8268ae2008-10-22 17:49:05 +000047CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000048}
49
50void CXXRecordDecl::Destroy(ASTContext &C) {
51 C.Deallocate(Bases);
52 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000053}
54
Douglas Gregor57c856b2008-10-23 18:13:27 +000055void
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000056CXXRecordDecl::setBases(ASTContext &C,
57 CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000058 unsigned NumBases) {
Douglas Gregor64bffa92008-11-05 16:20:31 +000059 // C++ [dcl.init.aggr]p1:
60 // An aggregate is an array or a class (clause 9) with [...]
61 // no base classes [...].
62 Aggregate = false;
63
Douglas Gregor57c856b2008-10-23 18:13:27 +000064 if (this->Bases)
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000065 C.Deallocate(this->Bases);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000066
67 int vbaseCount = 0;
68 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
69 bool hasDirectVirtualBase = false;
70
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000071 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor57c856b2008-10-23 18:13:27 +000072 this->NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000073 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor57c856b2008-10-23 18:13:27 +000074 this->Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000075 // Keep track of inherited vbases for this base class.
76 const CXXBaseSpecifier *Base = Bases[i];
77 QualType BaseType = Base->getType();
78 // Skip template types.
79 // FIXME. This means that this list must be rebuilt during template
80 // instantiation.
81 if (BaseType->isDependentType())
82 continue;
83 CXXRecordDecl *BaseClassDecl
84 = cast<CXXRecordDecl>(BaseType->getAsRecordType()->getDecl());
85 if (Base->isVirtual())
86 hasDirectVirtualBase = true;
87 for (CXXRecordDecl::base_class_iterator VBase =
88 BaseClassDecl->vbases_begin(),
89 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
90 // Add this vbase to the array of vbases for current class if it is
91 // not already in the list.
92 // FIXME. Note that we do a linear search as number of such classes are
93 // very few.
94 int i;
95 for (i = 0; i < vbaseCount; ++i)
96 if (UniqueVbases[i]->getType() == VBase->getType())
97 break;
98 if (i == vbaseCount) {
99 UniqueVbases.push_back(VBase);
100 ++vbaseCount;
101 }
102 }
103 }
104 if (hasDirectVirtualBase) {
105 // Iterate one more time through the direct bases and add the virtual
106 // base to the list of vritual bases for current class.
107 for (unsigned i = 0; i < NumBases; ++i) {
108 const CXXBaseSpecifier *VBase = Bases[i];
109 if (!VBase->isVirtual())
110 continue;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000111 int j;
112 for (j = 0; j < vbaseCount; ++j)
113 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000114 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000115 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000116 UniqueVbases.push_back(VBase);
117 ++vbaseCount;
118 }
119 }
120 }
121 if (vbaseCount > 0) {
122 // build AST for inhireted, direct or indirect, virtual bases.
123 this->VBases = new(C) CXXBaseSpecifier [vbaseCount];
124 this->NumVBases = vbaseCount;
125 for (int i = 0; i < vbaseCount; i++) {
126 QualType QT = UniqueVbases[i]->getType();
127 CXXRecordDecl *VBaseClassDecl
128 = cast<CXXRecordDecl>(QT->getAsRecordType()->getDecl());
129 this->VBases[i] =
130 *new CXXBaseSpecifier(
131 VBaseClassDecl->getSourceRange(), true,
132 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
133 UniqueVbases[i]->getAccessSpecifier(), QT);
134 }
135 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000136}
137
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000138bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000139 return getCopyConstructor(Context, QualType::Const) != 0;
140}
141
142CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
143 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000144 QualType ClassType
145 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000146 DeclarationName ConstructorName
147 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000148 Context.getCanonicalType(ClassType));
149 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000150 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000151 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000152 Con != ConEnd; ++Con) {
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000153 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context,
154 FoundTQs)) {
155 if (((TypeQuals & QualType::Const) == (FoundTQs & QualType::Const)) ||
156 (!(TypeQuals & QualType::Const) && (FoundTQs & QualType::Const)))
157 return cast<CXXConstructorDecl>(*Con);
158
159 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000160 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000161 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000162}
163
Sebastian Redl64b45f72009-01-05 20:52:13 +0000164bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context) const {
165 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
166 const_cast<CXXRecordDecl*>(this)));
167 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
168
169 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000170 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000171 Op != OpEnd; ++Op) {
172 // C++ [class.copy]p9:
173 // A user-declared copy assignment operator is a non-static non-template
174 // member function of class X with exactly one parameter of type X, X&,
175 // const X&, volatile X& or const volatile X&.
176 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
177 if (Method->isStatic())
178 continue;
179 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor72564e72009-02-26 23:50:07 +0000180 const FunctionProtoType *FnType =
181 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000182 assert(FnType && "Overloaded operator has no prototype.");
183 // Don't assert on this; an invalid decl might have been left in the AST.
184 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
185 continue;
186 bool AcceptsConst = true;
187 QualType ArgType = FnType->getArgType(0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000188 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000189 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000190 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000191 if (!ArgType.isConstQualified())
192 AcceptsConst = false;
193 }
194 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
195 continue;
196
197 // We have a single argument of type cv X or cv X&, i.e. we've found the
198 // copy assignment operator. Return whether it accepts const arguments.
199 return AcceptsConst;
200 }
201 assert(isInvalidDecl() &&
202 "No copy assignment operator declared in valid code.");
203 return false;
204}
205
206void
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000207CXXRecordDecl::addedConstructor(ASTContext &Context,
208 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000209 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
210 // Note that we have a user-declared constructor.
211 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000212
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000213 // C++ [dcl.init.aggr]p1:
214 // An aggregate is an array or a class (clause 9) with no
215 // user-declared constructors (12.1) [...].
216 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000217
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000218 // C++ [class]p4:
219 // A POD-struct is an aggregate class [...]
220 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000221
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000222 // C++ [class.ctor]p5:
223 // A constructor is trivial if it is an implicitly-declared default
224 // constructor.
225 HasTrivialConstructor = false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000226
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000227 // Note when we have a user-declared copy constructor, which will
228 // suppress the implicit declaration of a copy constructor.
229 if (ConDecl->isCopyConstructor(Context))
230 UserDeclaredCopyConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000231}
232
Sebastian Redl64b45f72009-01-05 20:52:13 +0000233void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
234 CXXMethodDecl *OpDecl) {
235 // We're interested specifically in copy assignment operators.
Douglas Gregor72564e72009-02-26 23:50:07 +0000236 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000237 assert(FnType && "Overloaded operator has no proto function type.");
238 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
239 QualType ArgType = FnType->getArgType(0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000240 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000241 ArgType = Ref->getPointeeType();
242
243 ArgType = ArgType.getUnqualifiedType();
244 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
245 const_cast<CXXRecordDecl*>(this)));
246
247 if (ClassType != Context.getCanonicalType(ArgType))
248 return;
249
250 // This is a copy assignment operator.
251 // Suppress the implicit declaration of a copy constructor.
252 UserDeclaredCopyAssignment = true;
253
254 // C++ [class]p4:
255 // A POD-struct is an aggregate class that [...] has no user-defined copy
256 // assignment operator [...].
257 PlainOldData = false;
258}
259
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000260void CXXRecordDecl::addConversionFunction(ASTContext &Context,
261 CXXConversionDecl *ConvDecl) {
262 Conversions.addOverload(ConvDecl);
263}
264
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000265
266CXXConstructorDecl *
267CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
268 QualType ClassType = Context.getTypeDeclType(this);
269 DeclarationName ConstructorName
270 = Context.DeclarationNames.getCXXConstructorName(
271 Context.getCanonicalType(ClassType.getUnqualifiedType()));
272
273 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000274 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000275 Con != ConEnd; ++Con) {
276 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
277 if (Constructor->isDefaultConstructor())
278 return Constructor;
279 }
280 return 0;
281}
282
Anders Carlsson7267c162009-05-29 21:03:38 +0000283const CXXDestructorDecl *
284CXXRecordDecl::getDestructor(ASTContext &Context) {
285 QualType ClassType = Context.getTypeDeclType(this);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000286
Anders Carlsson7267c162009-05-29 21:03:38 +0000287 DeclarationName Name
288 = Context.DeclarationNames.getCXXDestructorName(ClassType);
289
290 DeclContext::lookup_iterator I, E;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000291 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000292 assert(I != E && "Did not find a destructor!");
293
294 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
295 assert(++I == E && "Found more than one destructor!");
296
297 return Dtor;
298}
299
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000300CXXMethodDecl *
301CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000302 SourceLocation L, DeclarationName N,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000303 QualType T, bool isStatic, bool isInline) {
Steve Naroff3e970492009-01-27 21:25:57 +0000304 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000305}
306
Anders Carlsson05eb2442009-05-16 23:58:37 +0000307
308typedef llvm::DenseMap<const CXXMethodDecl*,
309 std::vector<const CXXMethodDecl *> *>
310 OverriddenMethodsMapTy;
311
312static OverriddenMethodsMapTy *OverriddenMethods = 0;
313
314void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
315 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
316
317 if (!OverriddenMethods)
318 OverriddenMethods = new OverriddenMethodsMapTy();
319
320 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
321 if (!Methods)
322 Methods = new std::vector<const CXXMethodDecl *>;
323
324 Methods->push_back(MD);
325}
326
327CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
328 if (!OverriddenMethods)
329 return 0;
330
331 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
332 if (it == OverriddenMethods->end())
333 return 0;
334 return &(*it->second)[0];
335}
336
337CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
338 if (!OverriddenMethods)
339 return 0;
340
341 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
342 if (it == OverriddenMethods->end())
343 return 0;
344
345 return &(*it->second)[it->second->size()];
346}
347
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000348QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000349 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
350 // If the member function is declared const, the type of this is const X*,
351 // if the member function is declared volatile, the type of this is
352 // volatile X*, and if the member function is declared const volatile,
353 // the type of this is const volatile X*.
354
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000355 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000356
357 QualType ClassTy;
358 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
359 ClassTy = TD->getInjectedClassNameType(C);
360 else
361 ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000362 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
Anders Carlsson4e579922009-07-10 21:35:09 +0000363 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000364}
365
Douglas Gregor7ad83902008-11-05 04:29:56 +0000366CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000367CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
368 SourceLocation L)
369 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000370 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
371 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
372 BaseOrMember |= 0x01;
373
374 if (NumArgs > 0) {
375 this->NumArgs = NumArgs;
376 this->Args = new Expr*[NumArgs];
377 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
378 this->Args[Idx] = Args[Idx];
379 }
380}
381
382CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000383CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
384 SourceLocation L)
385 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000386 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
387 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
388
389 if (NumArgs > 0) {
390 this->NumArgs = NumArgs;
391 this->Args = new Expr*[NumArgs];
392 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
393 this->Args[Idx] = Args[Idx];
394 }
395}
396
397CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
398 delete [] Args;
399}
400
Douglas Gregorb48fe382008-10-31 09:07:45 +0000401CXXConstructorDecl *
402CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000403 SourceLocation L, DeclarationName N,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000404 QualType T, bool isExplicit,
405 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000406 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
407 "Name must refer to a constructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000408 return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000409 isImplicitlyDeclared);
410}
411
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000412bool CXXConstructorDecl::isDefaultConstructor() const {
413 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000414 // A default constructor for a class X is a constructor of class
415 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000416 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000417 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000418}
419
420bool
421CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
422 unsigned &TypeQuals) const {
423 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000424 // A non-template constructor for class X is a copy constructor
425 // if its first parameter is of type X&, const X&, volatile X& or
426 // const volatile X&, and either there are no other parameters
427 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000428 if ((getNumParams() < 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000429 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000430 return false;
431
432 const ParmVarDecl *Param = getParamDecl(0);
433
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000434 // Do we have a reference type? Rvalue references don't count.
435 const LValueReferenceType *ParamRefType =
436 Param->getType()->getAsLValueReferenceType();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000437 if (!ParamRefType)
438 return false;
439
440 // Is it a reference to our class type?
441 QualType PointeeType
442 = Context.getCanonicalType(ParamRefType->getPointeeType());
443 QualType ClassTy
444 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
445 if (PointeeType.getUnqualifiedType() != ClassTy)
446 return false;
447
448 // We have a copy constructor.
449 TypeQuals = PointeeType.getCVRQualifiers();
450 return true;
451}
452
Douglas Gregor60d62c22008-10-31 16:23:19 +0000453bool CXXConstructorDecl::isConvertingConstructor() const {
454 // C++ [class.conv.ctor]p1:
455 // A constructor declared without the function-specifier explicit
456 // that can be called with a single parameter specifies a
457 // conversion from the type of its first parameter to the type of
458 // its class. Such a constructor is called a converting
459 // constructor.
460 if (isExplicit())
461 return false;
462
463 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000464 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000465 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000466 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000467}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000468
Douglas Gregor42a552f2008-11-05 20:51:48 +0000469CXXDestructorDecl *
470CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000471 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000472 QualType T, bool isInline,
473 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000474 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
475 "Name must refer to a destructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000476 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
477 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000478}
479
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000480void
481CXXConstructorDecl::setBaseOrMemberInitializers(
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000482 ASTContext &C,
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000483 CXXBaseOrMemberInitializer **Initializers,
484 unsigned NumInitializers) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000485 // We need to build the initializer AST according to order of construction
486 // and not what user specified in the Initializers list.
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000487 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
Fariborz Jahanian639dead2009-07-14 18:29:14 +0000488 // FIXME. We probably don't need to use AllToInit. But it is cleaner.
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000489 llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit;
490 // Push virtual bases before others.
491 for (CXXRecordDecl::base_class_iterator VBase =
492 ClassDecl->vbases_begin(),
493 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
494 const Type * T = VBase->getType()->getAsRecordType();
495 unsigned int i = 0;
496 for (i = 0; i < NumInitializers; i++) {
497 CXXBaseOrMemberInitializer *Member = Initializers[i];
498 if (Member->isBaseInitializer() &&
499 Member->getBaseClass() == T) {
500 AllToInit.push_back(Member);
501 break;
502 }
503 }
504 if (i == NumInitializers) {
505 CXXBaseOrMemberInitializer *Member =
506 new CXXBaseOrMemberInitializer(VBase->getType(), 0, 0,SourceLocation());
507 AllToInit.push_back(Member);
508 }
509 }
510 for (CXXRecordDecl::base_class_iterator Base =
511 ClassDecl->bases_begin(),
512 E = ClassDecl->bases_end(); Base != E; ++Base) {
513 // Virtuals are in the virtual base list and already constructed.
514 if (Base->isVirtual())
515 continue;
516 const Type * T = Base->getType()->getAsRecordType();
517 unsigned int i = 0;
518 for (i = 0; i < NumInitializers; i++) {
519 CXXBaseOrMemberInitializer *Member = Initializers[i];
520 if (Member->isBaseInitializer() && Member->getBaseClass() == T) {
521 AllToInit.push_back(Member);
522 break;
523 }
524 }
525 if (i == NumInitializers) {
526 CXXBaseOrMemberInitializer *Member =
527 new CXXBaseOrMemberInitializer(Base->getType(), 0, 0, SourceLocation());
528 AllToInit.push_back(Member);
529 }
530 }
531 // non-static data members.
532 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
533 E = ClassDecl->field_end(); Field != E; ++Field) {
534 unsigned int i = 0;
535 for (i = 0; i < NumInitializers; i++) {
536 CXXBaseOrMemberInitializer *Member = Initializers[i];
537 if (Member->isMemberInitializer() && Member->getMember() == (*Field)) {
538 AllToInit.push_back(Member);
539 break;
540 }
541 }
542 if (i == NumInitializers) {
543 // FIXME. What do we do with arrays?
544 QualType FieldType = C.getCanonicalType((*Field)->getType());
545 if (FieldType->getAsRecordType()) {
546 CXXBaseOrMemberInitializer *Member =
547 new CXXBaseOrMemberInitializer((*Field), 0, 0, SourceLocation());
548 AllToInit.push_back(Member);
549 }
550 }
551 }
552
553 NumInitializers = AllToInit.size();
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000554 if (NumInitializers > 0) {
555 NumBaseOrMemberInitializers = NumInitializers;
556 BaseOrMemberInitializers =
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000557 new (C) CXXBaseOrMemberInitializer*[NumInitializers];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000558 for (unsigned Idx = 0; Idx < NumInitializers; ++Idx)
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000559 BaseOrMemberInitializers[Idx] = AllToInit[Idx];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000560 }
561}
562
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000563void
564CXXConstructorDecl::Destroy(ASTContext& C) {
565 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000566 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000567}
568
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000569CXXConversionDecl *
570CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000571 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000572 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000573 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
574 "Name must refer to a conversion function");
Steve Naroff3e970492009-01-27 21:25:57 +0000575 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000576}
577
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000578OverloadedFunctionDecl *
579OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000580 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000581 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000582}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000583
Douglas Gregor364e0212009-06-27 21:05:07 +0000584void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
585 Functions.push_back(F);
586 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000587}
588
Douglas Gregordaa439a2009-07-08 10:57:20 +0000589OverloadIterator::reference OverloadIterator::operator*() const {
590 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
591 return FD;
592
593 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
594 return FTD;
595
596 assert(isa<OverloadedFunctionDecl>(D));
597 return *Iter;
598}
599
600OverloadIterator &OverloadIterator::operator++() {
601 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
602 D = 0;
603 return *this;
604 }
605
606 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
607 D = 0;
608
609 return *this;
610}
611
612bool OverloadIterator::Equals(const OverloadIterator &Other) const {
613 if (!D || !Other.D)
614 return D == Other.D;
615
616 if (D != Other.D)
617 return false;
618
619 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
620}
621
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000622LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000623 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000624 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000625 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000626 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000627}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000628
629UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
630 SourceLocation L,
631 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000632 SourceRange QualifierRange,
633 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000634 SourceLocation IdentLoc,
635 NamespaceDecl *Used,
636 DeclContext *CommonAncestor) {
Douglas Gregor8419fa32009-05-30 06:31:56 +0000637 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
638 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000639}
640
Anders Carlsson68771c72009-03-28 22:58:02 +0000641NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
642 SourceLocation L,
643 SourceLocation AliasLoc,
644 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000645 SourceRange QualifierRange,
646 NestedNameSpecifier *Qualifier,
Anders Carlsson68771c72009-03-28 22:58:02 +0000647 SourceLocation IdentLoc,
648 NamedDecl *Namespace) {
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000649 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
650 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000651}
652
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000653UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
654 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
655 SourceLocation UL, NamedDecl* Target,
656 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
657 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
658 TargetNNS, IsTypeNameArg);
659}
660
Anders Carlssonfb311762009-03-14 00:25:26 +0000661StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
662 SourceLocation L, Expr *AssertExpr,
663 StringLiteral *Message) {
664 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
665}
666
667void StaticAssertDecl::Destroy(ASTContext& C) {
668 AssertExpr->Destroy(C);
669 Message->Destroy(C);
670 this->~StaticAssertDecl();
671 C.Deallocate((void *)this);
672}
673
674StaticAssertDecl::~StaticAssertDecl() {
675}
676
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000677static const char *getAccessName(AccessSpecifier AS) {
678 switch (AS) {
679 default:
680 case AS_none:
681 assert("Invalid access specifier!");
682 return 0;
683 case AS_public:
684 return "public";
685 case AS_private:
686 return "private";
687 case AS_protected:
688 return "protected";
689 }
690}
691
692const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
693 AccessSpecifier AS) {
694 return DB << getAccessName(AS);
695}
696
697