blob: af54fb07d1e6b52e761b664109b7584f1b073cf4 [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);
Fariborz Jahanian71c6e712009-07-22 17:41:53 +000054 C.Deallocate(VBases);
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000055 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000056}
57
Douglas Gregor57c856b2008-10-23 18:13:27 +000058void
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000059CXXRecordDecl::setBases(ASTContext &C,
60 CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000061 unsigned NumBases) {
Douglas Gregor64bffa92008-11-05 16:20:31 +000062 // C++ [dcl.init.aggr]p1:
63 // An aggregate is an array or a class (clause 9) with [...]
64 // no base classes [...].
65 Aggregate = false;
66
Douglas Gregor57c856b2008-10-23 18:13:27 +000067 if (this->Bases)
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000068 C.Deallocate(this->Bases);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000069
70 int vbaseCount = 0;
71 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
72 bool hasDirectVirtualBase = false;
73
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000074 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor57c856b2008-10-23 18:13:27 +000075 this->NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000076 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor57c856b2008-10-23 18:13:27 +000077 this->Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000078 // Keep track of inherited vbases for this base class.
79 const CXXBaseSpecifier *Base = Bases[i];
80 QualType BaseType = Base->getType();
81 // Skip template types.
82 // FIXME. This means that this list must be rebuilt during template
83 // instantiation.
84 if (BaseType->isDependentType())
85 continue;
86 CXXRecordDecl *BaseClassDecl
Ted Kremenek35366a62009-07-17 17:50:17 +000087 = cast<CXXRecordDecl>(BaseType->getAsRecordType()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000088 if (Base->isVirtual())
89 hasDirectVirtualBase = true;
90 for (CXXRecordDecl::base_class_iterator VBase =
91 BaseClassDecl->vbases_begin(),
92 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
93 // Add this vbase to the array of vbases for current class if it is
94 // not already in the list.
95 // FIXME. Note that we do a linear search as number of such classes are
96 // very few.
97 int i;
98 for (i = 0; i < vbaseCount; ++i)
99 if (UniqueVbases[i]->getType() == VBase->getType())
100 break;
101 if (i == vbaseCount) {
102 UniqueVbases.push_back(VBase);
103 ++vbaseCount;
104 }
105 }
106 }
107 if (hasDirectVirtualBase) {
108 // Iterate one more time through the direct bases and add the virtual
109 // base to the list of vritual bases for current class.
110 for (unsigned i = 0; i < NumBases; ++i) {
111 const CXXBaseSpecifier *VBase = Bases[i];
112 if (!VBase->isVirtual())
113 continue;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000114 int j;
115 for (j = 0; j < vbaseCount; ++j)
116 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000117 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000118 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000119 UniqueVbases.push_back(VBase);
120 ++vbaseCount;
121 }
122 }
123 }
124 if (vbaseCount > 0) {
125 // build AST for inhireted, direct or indirect, virtual bases.
126 this->VBases = new(C) CXXBaseSpecifier [vbaseCount];
127 this->NumVBases = vbaseCount;
128 for (int i = 0; i < vbaseCount; i++) {
129 QualType QT = UniqueVbases[i]->getType();
130 CXXRecordDecl *VBaseClassDecl
Ted Kremenek35366a62009-07-17 17:50:17 +0000131 = cast<CXXRecordDecl>(QT->getAsRecordType()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000132 this->VBases[i] =
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000133 *new (C) CXXBaseSpecifier(
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000134 VBaseClassDecl->getSourceRange(), true,
135 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
136 UniqueVbases[i]->getAccessSpecifier(), QT);
137 }
138 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000139}
140
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000141bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000142 return getCopyConstructor(Context, QualType::Const) != 0;
143}
144
145CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
146 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000147 QualType ClassType
148 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000149 DeclarationName ConstructorName
150 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000151 Context.getCanonicalType(ClassType));
152 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000153 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000154 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000155 Con != ConEnd; ++Con) {
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000156 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context,
157 FoundTQs)) {
158 if (((TypeQuals & QualType::Const) == (FoundTQs & QualType::Const)) ||
159 (!(TypeQuals & QualType::Const) && (FoundTQs & QualType::Const)))
160 return cast<CXXConstructorDecl>(*Con);
161
162 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000163 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000164 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000165}
166
Sebastian Redl64b45f72009-01-05 20:52:13 +0000167bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context) const {
168 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
169 const_cast<CXXRecordDecl*>(this)));
170 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
171
172 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000173 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000174 Op != OpEnd; ++Op) {
175 // C++ [class.copy]p9:
176 // A user-declared copy assignment operator is a non-static non-template
177 // member function of class X with exactly one parameter of type X, X&,
178 // const X&, volatile X& or const volatile X&.
179 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
180 if (Method->isStatic())
181 continue;
182 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor72564e72009-02-26 23:50:07 +0000183 const FunctionProtoType *FnType =
184 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000185 assert(FnType && "Overloaded operator has no prototype.");
186 // Don't assert on this; an invalid decl might have been left in the AST.
187 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
188 continue;
189 bool AcceptsConst = true;
190 QualType ArgType = FnType->getArgType(0);
Ted Kremenek35366a62009-07-17 17:50:17 +0000191 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000192 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000193 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000194 if (!ArgType.isConstQualified())
195 AcceptsConst = false;
196 }
197 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
198 continue;
199
200 // We have a single argument of type cv X or cv X&, i.e. we've found the
201 // copy assignment operator. Return whether it accepts const arguments.
202 return AcceptsConst;
203 }
204 assert(isInvalidDecl() &&
205 "No copy assignment operator declared in valid code.");
206 return false;
207}
208
209void
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000210CXXRecordDecl::addedConstructor(ASTContext &Context,
211 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000212 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
213 // Note that we have a user-declared constructor.
214 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000215
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000216 // C++ [dcl.init.aggr]p1:
217 // An aggregate is an array or a class (clause 9) with no
218 // user-declared constructors (12.1) [...].
219 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000220
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000221 // C++ [class]p4:
222 // A POD-struct is an aggregate class [...]
223 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000224
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000225 // C++ [class.ctor]p5:
226 // A constructor is trivial if it is an implicitly-declared default
227 // constructor.
228 HasTrivialConstructor = false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000229
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000230 // Note when we have a user-declared copy constructor, which will
231 // suppress the implicit declaration of a copy constructor.
232 if (ConDecl->isCopyConstructor(Context))
233 UserDeclaredCopyConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000234}
235
Sebastian Redl64b45f72009-01-05 20:52:13 +0000236void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
237 CXXMethodDecl *OpDecl) {
238 // We're interested specifically in copy assignment operators.
Douglas Gregor72564e72009-02-26 23:50:07 +0000239 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000240 assert(FnType && "Overloaded operator has no proto function type.");
241 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
242 QualType ArgType = FnType->getArgType(0);
Ted Kremenek35366a62009-07-17 17:50:17 +0000243 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000244 ArgType = Ref->getPointeeType();
245
246 ArgType = ArgType.getUnqualifiedType();
247 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
248 const_cast<CXXRecordDecl*>(this)));
249
250 if (ClassType != Context.getCanonicalType(ArgType))
251 return;
252
253 // This is a copy assignment operator.
254 // Suppress the implicit declaration of a copy constructor.
255 UserDeclaredCopyAssignment = true;
256
257 // C++ [class]p4:
258 // A POD-struct is an aggregate class that [...] has no user-defined copy
259 // assignment operator [...].
260 PlainOldData = false;
261}
262
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000263void CXXRecordDecl::addConversionFunction(ASTContext &Context,
264 CXXConversionDecl *ConvDecl) {
265 Conversions.addOverload(ConvDecl);
266}
267
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000268
269CXXConstructorDecl *
270CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
271 QualType ClassType = Context.getTypeDeclType(this);
272 DeclarationName ConstructorName
273 = Context.DeclarationNames.getCXXConstructorName(
274 Context.getCanonicalType(ClassType.getUnqualifiedType()));
275
276 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000277 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000278 Con != ConEnd; ++Con) {
279 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
280 if (Constructor->isDefaultConstructor())
281 return Constructor;
282 }
283 return 0;
284}
285
Anders Carlsson7267c162009-05-29 21:03:38 +0000286const CXXDestructorDecl *
287CXXRecordDecl::getDestructor(ASTContext &Context) {
288 QualType ClassType = Context.getTypeDeclType(this);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000289
Anders Carlsson7267c162009-05-29 21:03:38 +0000290 DeclarationName Name
291 = Context.DeclarationNames.getCXXDestructorName(ClassType);
292
293 DeclContext::lookup_iterator I, E;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000294 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000295 assert(I != E && "Did not find a destructor!");
296
297 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
298 assert(++I == E && "Found more than one destructor!");
299
300 return Dtor;
301}
302
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000303CXXMethodDecl *
304CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000305 SourceLocation L, DeclarationName N,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000306 QualType T, bool isStatic, bool isInline) {
Steve Naroff3e970492009-01-27 21:25:57 +0000307 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000308}
309
Anders Carlsson05eb2442009-05-16 23:58:37 +0000310
311typedef llvm::DenseMap<const CXXMethodDecl*,
312 std::vector<const CXXMethodDecl *> *>
313 OverriddenMethodsMapTy;
314
315static OverriddenMethodsMapTy *OverriddenMethods = 0;
316
317void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
318 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
319
320 if (!OverriddenMethods)
321 OverriddenMethods = new OverriddenMethodsMapTy();
322
323 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
324 if (!Methods)
325 Methods = new std::vector<const CXXMethodDecl *>;
326
327 Methods->push_back(MD);
328}
329
330CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
331 if (!OverriddenMethods)
332 return 0;
333
334 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
335 if (it == OverriddenMethods->end())
336 return 0;
337 return &(*it->second)[0];
338}
339
340CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
341 if (!OverriddenMethods)
342 return 0;
343
344 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
345 if (it == OverriddenMethods->end())
346 return 0;
347
348 return &(*it->second)[it->second->size()];
349}
350
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000351QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000352 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
353 // If the member function is declared const, the type of this is const X*,
354 // if the member function is declared volatile, the type of this is
355 // volatile X*, and if the member function is declared const volatile,
356 // the type of this is const volatile X*.
357
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000358 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000359
360 QualType ClassTy;
361 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
362 ClassTy = TD->getInjectedClassNameType(C);
363 else
364 ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000365 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
Anders Carlsson4e579922009-07-10 21:35:09 +0000366 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000367}
368
Douglas Gregor7ad83902008-11-05 04:29:56 +0000369CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000370CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
371 SourceLocation L)
372 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000373 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
374 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
375 BaseOrMember |= 0x01;
376
377 if (NumArgs > 0) {
378 this->NumArgs = NumArgs;
379 this->Args = new Expr*[NumArgs];
380 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
381 this->Args[Idx] = Args[Idx];
382 }
383}
384
385CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000386CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
387 SourceLocation L)
388 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000389 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
390 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
391
392 if (NumArgs > 0) {
393 this->NumArgs = NumArgs;
394 this->Args = new Expr*[NumArgs];
395 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
396 this->Args[Idx] = Args[Idx];
397 }
398}
399
400CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
401 delete [] Args;
402}
403
Douglas Gregorb48fe382008-10-31 09:07:45 +0000404CXXConstructorDecl *
405CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000406 SourceLocation L, DeclarationName N,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000407 QualType T, bool isExplicit,
408 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000409 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
410 "Name must refer to a constructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000411 return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000412 isImplicitlyDeclared);
413}
414
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000415bool CXXConstructorDecl::isDefaultConstructor() const {
416 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000417 // A default constructor for a class X is a constructor of class
418 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000419 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000420 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000421}
422
423bool
424CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
425 unsigned &TypeQuals) const {
426 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000427 // A non-template constructor for class X is a copy constructor
428 // if its first parameter is of type X&, const X&, volatile X& or
429 // const volatile X&, and either there are no other parameters
430 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000431 if ((getNumParams() < 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000432 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000433 return false;
434
435 const ParmVarDecl *Param = getParamDecl(0);
436
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000437 // Do we have a reference type? Rvalue references don't count.
438 const LValueReferenceType *ParamRefType =
Ted Kremenek35366a62009-07-17 17:50:17 +0000439 Param->getType()->getAsLValueReferenceType();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000440 if (!ParamRefType)
441 return false;
442
443 // Is it a reference to our class type?
444 QualType PointeeType
445 = Context.getCanonicalType(ParamRefType->getPointeeType());
446 QualType ClassTy
447 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
448 if (PointeeType.getUnqualifiedType() != ClassTy)
449 return false;
450
451 // We have a copy constructor.
452 TypeQuals = PointeeType.getCVRQualifiers();
453 return true;
454}
455
Douglas Gregor60d62c22008-10-31 16:23:19 +0000456bool CXXConstructorDecl::isConvertingConstructor() const {
457 // C++ [class.conv.ctor]p1:
458 // A constructor declared without the function-specifier explicit
459 // that can be called with a single parameter specifies a
460 // conversion from the type of its first parameter to the type of
461 // its class. Such a constructor is called a converting
462 // constructor.
463 if (isExplicit())
464 return false;
465
466 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000467 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000468 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000469 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000470}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000471
Douglas Gregor42a552f2008-11-05 20:51:48 +0000472CXXDestructorDecl *
473CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000474 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000475 QualType T, bool isInline,
476 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000477 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
478 "Name must refer to a destructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000479 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
480 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000481}
482
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000483void
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000484CXXDestructorDecl::Destroy(ASTContext& C) {
485 C.Deallocate(BaseOrMemberDestructions);
486 CXXMethodDecl::Destroy(C);
487}
488
489void
490CXXDestructorDecl::computeBaseOrMembersToDestroy(ASTContext &C) {
Fariborz Jahanian560de452009-07-15 22:34:08 +0000491 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000492 llvm::SmallVector<uintptr_t, 32> AllToDestruct;
493
Fariborz Jahanian560de452009-07-15 22:34:08 +0000494 for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(),
495 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000496 // Skip over virtual bases which have trivial destructors.
497 CXXRecordDecl *BaseClassDecl
498 = cast<CXXRecordDecl>(VBase->getType()->getAsRecordType()->getDecl());
499 if (BaseClassDecl->hasTrivialDestructor())
500 continue;
501 uintptr_t Member =
Fariborz Jahaniancf183122009-07-22 00:42:46 +0000502 reinterpret_cast<uintptr_t>(VBase->getType().getTypePtr()) | VBASE;
Fariborz Jahanian560de452009-07-15 22:34:08 +0000503 AllToDestruct.push_back(Member);
504 }
505 for (CXXRecordDecl::base_class_iterator Base =
506 ClassDecl->bases_begin(),
507 E = ClassDecl->bases_end(); Base != E; ++Base) {
508 if (Base->isVirtual())
509 continue;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000510 // Skip over virtual bases which have trivial destructors.
511 CXXRecordDecl *BaseClassDecl
512 = cast<CXXRecordDecl>(Base->getType()->getAsRecordType()->getDecl());
513 if (BaseClassDecl->hasTrivialDestructor())
514 continue;
515
516 uintptr_t Member =
Fariborz Jahaniancf183122009-07-22 00:42:46 +0000517 reinterpret_cast<uintptr_t>(Base->getType().getTypePtr()) | DRCTNONVBASE;
Fariborz Jahanian560de452009-07-15 22:34:08 +0000518 AllToDestruct.push_back(Member);
519 }
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000520
Fariborz Jahanian560de452009-07-15 22:34:08 +0000521 // non-static data members.
522 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
523 E = ClassDecl->field_end(); Field != E; ++Field) {
524 QualType FieldType = C.getCanonicalType((*Field)->getType());
525 while (const ArrayType *AT = C.getAsArrayType(FieldType))
526 FieldType = AT->getElementType();
527
Ted Kremenek35366a62009-07-17 17:50:17 +0000528 if (FieldType->getAsRecordType()) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000529 // Skip over virtual bases which have trivial destructors.
530 CXXRecordDecl *BaseClassDecl
531 = cast<CXXRecordDecl>(FieldType->getAsRecordType()->getDecl());
532 if (BaseClassDecl->hasTrivialDestructor())
533 continue;
534 uintptr_t Member = reinterpret_cast<uintptr_t>(*Field);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000535 AllToDestruct.push_back(Member);
536 }
537 }
538
539 unsigned NumDestructions = AllToDestruct.size();
540 if (NumDestructions > 0) {
541 NumBaseOrMemberDestructions = NumDestructions;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000542 BaseOrMemberDestructions = new (C) uintptr_t [NumDestructions];
Fariborz Jahanian560de452009-07-15 22:34:08 +0000543 // Insert in reverse order.
544 for (int Idx = NumDestructions-1, i=0 ; Idx >= 0; --Idx)
545 BaseOrMemberDestructions[i++] = AllToDestruct[Idx];
546 }
547}
548
549void
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000550CXXConstructorDecl::setBaseOrMemberInitializers(
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000551 ASTContext &C,
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000552 CXXBaseOrMemberInitializer **Initializers,
553 unsigned NumInitializers) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000554 // We need to build the initializer AST according to order of construction
555 // and not what user specified in the Initializers list.
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000556 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
Fariborz Jahanian639dead2009-07-14 18:29:14 +0000557 // FIXME. We probably don't need to use AllToInit. But it is cleaner.
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000558 llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit;
559 // Push virtual bases before others.
560 for (CXXRecordDecl::base_class_iterator VBase =
561 ClassDecl->vbases_begin(),
562 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Ted Kremenek35366a62009-07-17 17:50:17 +0000563 const Type * T = VBase->getType()->getAsRecordType();
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000564 unsigned int i = 0;
565 for (i = 0; i < NumInitializers; i++) {
566 CXXBaseOrMemberInitializer *Member = Initializers[i];
567 if (Member->isBaseInitializer() &&
Ted Kremenek35366a62009-07-17 17:50:17 +0000568 Member->getBaseClass()->getAsRecordType() == T) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000569 AllToInit.push_back(Member);
570 break;
571 }
572 }
573 if (i == NumInitializers) {
574 CXXBaseOrMemberInitializer *Member =
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000575 new (C) CXXBaseOrMemberInitializer(VBase->getType(), 0, 0,
576 SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000577 AllToInit.push_back(Member);
578 }
579 }
580 for (CXXRecordDecl::base_class_iterator Base =
581 ClassDecl->bases_begin(),
582 E = ClassDecl->bases_end(); Base != E; ++Base) {
583 // Virtuals are in the virtual base list and already constructed.
584 if (Base->isVirtual())
585 continue;
Ted Kremenek35366a62009-07-17 17:50:17 +0000586 const Type * T = Base->getType()->getAsRecordType();
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000587 unsigned int i = 0;
588 for (i = 0; i < NumInitializers; i++) {
589 CXXBaseOrMemberInitializer *Member = Initializers[i];
Fariborz Jahanian42a52172009-07-14 22:40:50 +0000590 if (Member->isBaseInitializer() &&
Ted Kremenek35366a62009-07-17 17:50:17 +0000591 Member->getBaseClass()->getAsRecordType() == T) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000592 AllToInit.push_back(Member);
593 break;
594 }
595 }
596 if (i == NumInitializers) {
597 CXXBaseOrMemberInitializer *Member =
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000598 new (C) CXXBaseOrMemberInitializer(Base->getType(), 0, 0,
599 SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000600 AllToInit.push_back(Member);
601 }
602 }
603 // non-static data members.
604 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
605 E = ClassDecl->field_end(); Field != E; ++Field) {
606 unsigned int i = 0;
607 for (i = 0; i < NumInitializers; i++) {
608 CXXBaseOrMemberInitializer *Member = Initializers[i];
609 if (Member->isMemberInitializer() && Member->getMember() == (*Field)) {
610 AllToInit.push_back(Member);
611 break;
612 }
613 }
614 if (i == NumInitializers) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000615 QualType FieldType = C.getCanonicalType((*Field)->getType());
Fariborz Jahanian9b9f4242009-07-14 18:56:31 +0000616 while (const ArrayType *AT = C.getAsArrayType(FieldType))
617 FieldType = AT->getElementType();
618
Ted Kremenek35366a62009-07-17 17:50:17 +0000619 if (FieldType->getAsRecordType()) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000620 CXXBaseOrMemberInitializer *Member =
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000621 new (C) CXXBaseOrMemberInitializer((*Field), 0, 0, SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000622 AllToInit.push_back(Member);
623 }
624 }
625 }
626
627 NumInitializers = AllToInit.size();
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000628 if (NumInitializers > 0) {
629 NumBaseOrMemberInitializers = NumInitializers;
630 BaseOrMemberInitializers =
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000631 new (C) CXXBaseOrMemberInitializer*[NumInitializers];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000632 for (unsigned Idx = 0; Idx < NumInitializers; ++Idx)
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000633 BaseOrMemberInitializers[Idx] = AllToInit[Idx];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000634 }
635}
636
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000637void
638CXXConstructorDecl::Destroy(ASTContext& C) {
639 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000640 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000641}
642
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000643CXXConversionDecl *
644CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000645 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000646 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000647 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
648 "Name must refer to a conversion function");
Steve Naroff3e970492009-01-27 21:25:57 +0000649 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000650}
651
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000652OverloadedFunctionDecl *
653OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000654 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000655 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000656}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000657
Douglas Gregor364e0212009-06-27 21:05:07 +0000658void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
659 Functions.push_back(F);
660 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000661}
662
Douglas Gregordaa439a2009-07-08 10:57:20 +0000663OverloadIterator::reference OverloadIterator::operator*() const {
664 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
665 return FD;
666
667 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
668 return FTD;
669
670 assert(isa<OverloadedFunctionDecl>(D));
671 return *Iter;
672}
673
674OverloadIterator &OverloadIterator::operator++() {
675 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
676 D = 0;
677 return *this;
678 }
679
680 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
681 D = 0;
682
683 return *this;
684}
685
686bool OverloadIterator::Equals(const OverloadIterator &Other) const {
687 if (!D || !Other.D)
688 return D == Other.D;
689
690 if (D != Other.D)
691 return false;
692
693 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
694}
695
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000696LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000697 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000698 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000699 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000700 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000701}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000702
703UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
704 SourceLocation L,
705 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000706 SourceRange QualifierRange,
707 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000708 SourceLocation IdentLoc,
709 NamespaceDecl *Used,
710 DeclContext *CommonAncestor) {
Douglas Gregor8419fa32009-05-30 06:31:56 +0000711 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
712 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000713}
714
Anders Carlsson68771c72009-03-28 22:58:02 +0000715NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
716 SourceLocation L,
717 SourceLocation AliasLoc,
718 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000719 SourceRange QualifierRange,
720 NestedNameSpecifier *Qualifier,
Anders Carlsson68771c72009-03-28 22:58:02 +0000721 SourceLocation IdentLoc,
722 NamedDecl *Namespace) {
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000723 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
724 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000725}
726
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000727UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
728 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
729 SourceLocation UL, NamedDecl* Target,
730 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
731 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
732 TargetNNS, IsTypeNameArg);
733}
734
Anders Carlssonfb311762009-03-14 00:25:26 +0000735StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
736 SourceLocation L, Expr *AssertExpr,
737 StringLiteral *Message) {
738 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
739}
740
741void StaticAssertDecl::Destroy(ASTContext& C) {
742 AssertExpr->Destroy(C);
743 Message->Destroy(C);
744 this->~StaticAssertDecl();
745 C.Deallocate((void *)this);
746}
747
748StaticAssertDecl::~StaticAssertDecl() {
749}
750
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000751static const char *getAccessName(AccessSpecifier AS) {
752 switch (AS) {
753 default:
754 case AS_none:
755 assert("Invalid access specifier!");
756 return 0;
757 case AS_public:
758 return "public";
759 case AS_private:
760 return "private";
761 case AS_protected:
762 return "protected";
763 }
764}
765
766const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
767 AccessSpecifier AS) {
768 return DB << getAccessName(AS);
769}
770
771