blob: 3f5320b8f132d7d109e9bdac1d81b2ef10993d52 [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000017#include "clang/AST/Expr.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000018#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000019#include "llvm/ADT/STLExtras.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// Decl Allocation/Deallocation Method Implementations
24//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000025
Douglas Gregor3e00bad2009-02-17 01:05:43 +000026CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000027 SourceLocation L, IdentifierInfo *Id,
28 SourceLocation TKL)
29 : RecordDecl(K, TK, DC, L, Id, TKL),
Douglas Gregor7d7e6722008-11-12 23:21:09 +000030 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000031 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Anders Carlsson67e4dd22009-03-22 01:52:17 +000032 Aggregate(true), PlainOldData(true), Polymorphic(false), Abstract(false),
Douglas Gregor1f2023a2009-07-22 18:25:24 +000033 HasTrivialConstructor(true), HasTrivialCopyConstructor(true),
34 HasTrivialCopyAssignment(true), HasTrivialDestructor(true),
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000035 Bases(0), NumBases(0), VBases(0), NumVBases(0),
36 Conversions(DC, DeclarationName()),
Douglas Gregord475b8d2009-03-25 21:17:03 +000037 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000038
Ted Kremenek4b7c9832008-09-05 17:16:31 +000039CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
40 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000041 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000042 CXXRecordDecl* PrevDecl,
43 bool DelayTypeCreation) {
Douglas Gregor741dd9a2009-07-21 14:46:17 +000044 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id, TKL);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000045 if (!DelayTypeCreation)
46 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000047 return R;
48}
49
Douglas Gregorf8268ae2008-10-22 17:49:05 +000050CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000051}
52
53void CXXRecordDecl::Destroy(ASTContext &C) {
54 C.Deallocate(Bases);
Fariborz Jahanian71c6e712009-07-22 17:41:53 +000055 C.Deallocate(VBases);
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000056 this->RecordDecl::Destroy(C);
Douglas Gregorf8268ae2008-10-22 17:49:05 +000057}
58
Douglas Gregor57c856b2008-10-23 18:13:27 +000059void
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000060CXXRecordDecl::setBases(ASTContext &C,
61 CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000062 unsigned NumBases) {
Douglas Gregor64bffa92008-11-05 16:20:31 +000063 // C++ [dcl.init.aggr]p1:
64 // An aggregate is an array or a class (clause 9) with [...]
65 // no base classes [...].
66 Aggregate = false;
67
Douglas Gregor57c856b2008-10-23 18:13:27 +000068 if (this->Bases)
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000069 C.Deallocate(this->Bases);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000070
71 int vbaseCount = 0;
72 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases;
73 bool hasDirectVirtualBase = false;
74
Fariborz Jahanian5ffcd7b2009-07-02 18:26:15 +000075 this->Bases = new(C) CXXBaseSpecifier [NumBases];
Douglas Gregor57c856b2008-10-23 18:13:27 +000076 this->NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000077 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor57c856b2008-10-23 18:13:27 +000078 this->Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000079 // Keep track of inherited vbases for this base class.
80 const CXXBaseSpecifier *Base = Bases[i];
81 QualType BaseType = Base->getType();
82 // Skip template types.
83 // FIXME. This means that this list must be rebuilt during template
84 // instantiation.
85 if (BaseType->isDependentType())
86 continue;
87 CXXRecordDecl *BaseClassDecl
Ted Kremenek35366a62009-07-17 17:50:17 +000088 = cast<CXXRecordDecl>(BaseType->getAsRecordType()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000089 if (Base->isVirtual())
90 hasDirectVirtualBase = true;
91 for (CXXRecordDecl::base_class_iterator VBase =
92 BaseClassDecl->vbases_begin(),
93 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
94 // Add this vbase to the array of vbases for current class if it is
95 // not already in the list.
96 // FIXME. Note that we do a linear search as number of such classes are
97 // very few.
98 int i;
99 for (i = 0; i < vbaseCount; ++i)
100 if (UniqueVbases[i]->getType() == VBase->getType())
101 break;
102 if (i == vbaseCount) {
103 UniqueVbases.push_back(VBase);
104 ++vbaseCount;
105 }
106 }
107 }
108 if (hasDirectVirtualBase) {
109 // Iterate one more time through the direct bases and add the virtual
110 // base to the list of vritual bases for current class.
111 for (unsigned i = 0; i < NumBases; ++i) {
112 const CXXBaseSpecifier *VBase = Bases[i];
113 if (!VBase->isVirtual())
114 continue;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000115 int j;
116 for (j = 0; j < vbaseCount; ++j)
117 if (UniqueVbases[j]->getType() == VBase->getType())
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000118 break;
Alisdair Meredith002b91f2009-07-11 14:32:10 +0000119 if (j == vbaseCount) {
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000120 UniqueVbases.push_back(VBase);
121 ++vbaseCount;
122 }
123 }
124 }
125 if (vbaseCount > 0) {
126 // build AST for inhireted, direct or indirect, virtual bases.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000127 this->VBases = new (C) CXXBaseSpecifier [vbaseCount];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000128 this->NumVBases = vbaseCount;
129 for (int i = 0; i < vbaseCount; i++) {
130 QualType QT = UniqueVbases[i]->getType();
131 CXXRecordDecl *VBaseClassDecl
Ted Kremenek35366a62009-07-17 17:50:17 +0000132 = cast<CXXRecordDecl>(QT->getAsRecordType()->getDecl());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000133 this->VBases[i] =
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000134 *new (C) CXXBaseSpecifier(
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000135 VBaseClassDecl->getSourceRange(), true,
136 VBaseClassDecl->getTagKind() == RecordDecl::TK_class,
137 UniqueVbases[i]->getAccessSpecifier(), QT);
138 }
139 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000140}
141
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000142bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000143 return getCopyConstructor(Context, QualType::Const) != 0;
144}
145
146CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
147 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000148 QualType ClassType
149 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000150 DeclarationName ConstructorName
151 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000152 Context.getCanonicalType(ClassType));
153 unsigned FoundTQs;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000154 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000155 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000156 Con != ConEnd; ++Con) {
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000157 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context,
158 FoundTQs)) {
159 if (((TypeQuals & QualType::Const) == (FoundTQs & QualType::Const)) ||
160 (!(TypeQuals & QualType::Const) && (FoundTQs & QualType::Const)))
161 return cast<CXXConstructorDecl>(*Con);
162
163 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000164 }
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000165 return 0;
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000166}
167
Sebastian Redl64b45f72009-01-05 20:52:13 +0000168bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context) const {
169 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
170 const_cast<CXXRecordDecl*>(this)));
171 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
172
173 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000174 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000175 Op != OpEnd; ++Op) {
176 // C++ [class.copy]p9:
177 // A user-declared copy assignment operator is a non-static non-template
178 // member function of class X with exactly one parameter of type X, X&,
179 // const X&, volatile X& or const volatile X&.
180 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
181 if (Method->isStatic())
182 continue;
183 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor72564e72009-02-26 23:50:07 +0000184 const FunctionProtoType *FnType =
185 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000186 assert(FnType && "Overloaded operator has no prototype.");
187 // Don't assert on this; an invalid decl might have been left in the AST.
188 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
189 continue;
190 bool AcceptsConst = true;
191 QualType ArgType = FnType->getArgType(0);
Ted Kremenek35366a62009-07-17 17:50:17 +0000192 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000193 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000194 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000195 if (!ArgType.isConstQualified())
196 AcceptsConst = false;
197 }
198 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
199 continue;
200
201 // We have a single argument of type cv X or cv X&, i.e. we've found the
202 // copy assignment operator. Return whether it accepts const arguments.
203 return AcceptsConst;
204 }
205 assert(isInvalidDecl() &&
206 "No copy assignment operator declared in valid code.");
207 return false;
208}
209
210void
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000211CXXRecordDecl::addedConstructor(ASTContext &Context,
212 CXXConstructorDecl *ConDecl) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000213 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
214 // Note that we have a user-declared constructor.
215 UserDeclaredConstructor = true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000216
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000217 // C++ [dcl.init.aggr]p1:
218 // An aggregate is an array or a class (clause 9) with no
219 // user-declared constructors (12.1) [...].
220 Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +0000221
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000222 // C++ [class]p4:
223 // A POD-struct is an aggregate class [...]
224 PlainOldData = false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000225
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000226 // C++ [class.ctor]p5:
227 // A constructor is trivial if it is an implicitly-declared default
228 // constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000229 // FIXME: C++0x: don't do this for "= default" default constructors.
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000230 HasTrivialConstructor = false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000231
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000232 // Note when we have a user-declared copy constructor, which will
233 // suppress the implicit declaration of a copy constructor.
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000234 if (ConDecl->isCopyConstructor(Context)) {
Fariborz Jahanian8bc3fa42009-06-17 22:44:31 +0000235 UserDeclaredCopyConstructor = true;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000236
237 // C++ [class.copy]p6:
238 // A copy constructor is trivial if it is implicitly declared.
239 // FIXME: C++0x: don't do this for "= default" copy constructors.
240 HasTrivialCopyConstructor = false;
241 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000242}
243
Sebastian Redl64b45f72009-01-05 20:52:13 +0000244void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
245 CXXMethodDecl *OpDecl) {
246 // We're interested specifically in copy assignment operators.
Douglas Gregor72564e72009-02-26 23:50:07 +0000247 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000248 assert(FnType && "Overloaded operator has no proto function type.");
249 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
250 QualType ArgType = FnType->getArgType(0);
Ted Kremenek35366a62009-07-17 17:50:17 +0000251 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000252 ArgType = Ref->getPointeeType();
253
254 ArgType = ArgType.getUnqualifiedType();
255 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
256 const_cast<CXXRecordDecl*>(this)));
257
258 if (ClassType != Context.getCanonicalType(ArgType))
259 return;
260
261 // This is a copy assignment operator.
262 // Suppress the implicit declaration of a copy constructor.
263 UserDeclaredCopyAssignment = true;
264
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000265 // C++ [class.copy]p11:
266 // A copy assignment operator is trivial if it is implicitly declared.
267 // FIXME: C++0x: don't do this for "= default" copy operators.
268 HasTrivialCopyAssignment = false;
269
Sebastian Redl64b45f72009-01-05 20:52:13 +0000270 // C++ [class]p4:
271 // A POD-struct is an aggregate class that [...] has no user-defined copy
272 // assignment operator [...].
273 PlainOldData = false;
274}
275
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000276void CXXRecordDecl::addConversionFunction(ASTContext &Context,
277 CXXConversionDecl *ConvDecl) {
278 Conversions.addOverload(ConvDecl);
279}
280
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000281
282CXXConstructorDecl *
283CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
284 QualType ClassType = Context.getTypeDeclType(this);
285 DeclarationName ConstructorName
286 = Context.DeclarationNames.getCXXConstructorName(
287 Context.getCanonicalType(ClassType.getUnqualifiedType()));
288
289 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000290 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000291 Con != ConEnd; ++Con) {
292 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
293 if (Constructor->isDefaultConstructor())
294 return Constructor;
295 }
296 return 0;
297}
298
Anders Carlsson7267c162009-05-29 21:03:38 +0000299const CXXDestructorDecl *
300CXXRecordDecl::getDestructor(ASTContext &Context) {
301 QualType ClassType = Context.getTypeDeclType(this);
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000302
Anders Carlsson7267c162009-05-29 21:03:38 +0000303 DeclarationName Name
304 = Context.DeclarationNames.getCXXDestructorName(ClassType);
305
306 DeclContext::lookup_iterator I, E;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000307 llvm::tie(I, E) = lookup(Name);
Anders Carlsson7267c162009-05-29 21:03:38 +0000308 assert(I != E && "Did not find a destructor!");
309
310 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
311 assert(++I == E && "Found more than one destructor!");
312
313 return Dtor;
314}
315
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000316CXXMethodDecl *
317CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000318 SourceLocation L, DeclarationName N,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000319 QualType T, bool isStatic, bool isInline) {
Steve Naroff3e970492009-01-27 21:25:57 +0000320 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000321}
322
Anders Carlsson05eb2442009-05-16 23:58:37 +0000323
324typedef llvm::DenseMap<const CXXMethodDecl*,
325 std::vector<const CXXMethodDecl *> *>
326 OverriddenMethodsMapTy;
327
328static OverriddenMethodsMapTy *OverriddenMethods = 0;
329
330void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
331 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry.
332
333 if (!OverriddenMethods)
334 OverriddenMethods = new OverriddenMethodsMapTy();
335
336 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this];
337 if (!Methods)
338 Methods = new std::vector<const CXXMethodDecl *>;
339
340 Methods->push_back(MD);
341}
342
343CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
344 if (!OverriddenMethods)
345 return 0;
346
347 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
348 if (it == OverriddenMethods->end())
349 return 0;
350 return &(*it->second)[0];
351}
352
353CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
354 if (!OverriddenMethods)
355 return 0;
356
357 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this);
358 if (it == OverriddenMethods->end())
359 return 0;
360
361 return &(*it->second)[it->second->size()];
362}
363
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000364QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000365 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
366 // If the member function is declared const, the type of this is const X*,
367 // if the member function is declared volatile, the type of this is
368 // volatile X*, and if the member function is declared const volatile,
369 // the type of this is const volatile X*.
370
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000371 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000372
373 QualType ClassTy;
374 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate())
375 ClassTy = TD->getInjectedClassNameType(C);
376 else
377 ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000378 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
Anders Carlsson4e579922009-07-10 21:35:09 +0000379 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000380}
381
Douglas Gregor7ad83902008-11-05 04:29:56 +0000382CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000383CXXBaseOrMemberInitializer(QualType BaseType, 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>(BaseType.getTypePtr());
387 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
388 BaseOrMember |= 0x01;
389
390 if (NumArgs > 0) {
391 this->NumArgs = NumArgs;
392 this->Args = new Expr*[NumArgs];
393 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
394 this->Args[Idx] = Args[Idx];
395 }
396}
397
398CXXBaseOrMemberInitializer::
Fariborz Jahanian47deacf2009-06-30 00:02:17 +0000399CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
400 SourceLocation L)
401 : Args(0), NumArgs(0), IdLoc(L) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000402 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
403 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
404
405 if (NumArgs > 0) {
406 this->NumArgs = NumArgs;
407 this->Args = new Expr*[NumArgs];
408 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
409 this->Args[Idx] = Args[Idx];
410 }
411}
412
413CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
414 delete [] Args;
415}
416
Douglas Gregorb48fe382008-10-31 09:07:45 +0000417CXXConstructorDecl *
418CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000419 SourceLocation L, DeclarationName N,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000420 QualType T, bool isExplicit,
421 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000422 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
423 "Name must refer to a constructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000424 return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000425 isImplicitlyDeclared);
426}
427
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000428bool CXXConstructorDecl::isDefaultConstructor() const {
429 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000430 // A default constructor for a class X is a constructor of class
431 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000432 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000433 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000434}
435
436bool
437CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
438 unsigned &TypeQuals) const {
439 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000440 // A non-template constructor for class X is a copy constructor
441 // if its first parameter is of type X&, const X&, volatile X& or
442 // const volatile X&, and either there are no other parameters
443 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000444 if ((getNumParams() < 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000445 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000446 return false;
447
448 const ParmVarDecl *Param = getParamDecl(0);
449
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000450 // Do we have a reference type? Rvalue references don't count.
451 const LValueReferenceType *ParamRefType =
Ted Kremenek35366a62009-07-17 17:50:17 +0000452 Param->getType()->getAsLValueReferenceType();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000453 if (!ParamRefType)
454 return false;
455
456 // Is it a reference to our class type?
457 QualType PointeeType
458 = Context.getCanonicalType(ParamRefType->getPointeeType());
459 QualType ClassTy
460 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
461 if (PointeeType.getUnqualifiedType() != ClassTy)
462 return false;
463
464 // We have a copy constructor.
465 TypeQuals = PointeeType.getCVRQualifiers();
466 return true;
467}
468
Douglas Gregor60d62c22008-10-31 16:23:19 +0000469bool CXXConstructorDecl::isConvertingConstructor() const {
470 // C++ [class.conv.ctor]p1:
471 // A constructor declared without the function-specifier explicit
472 // that can be called with a single parameter specifies a
473 // conversion from the type of its first parameter to the type of
474 // its class. Such a constructor is called a converting
475 // constructor.
476 if (isExplicit())
477 return false;
478
479 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000480 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000481 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000482 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000483}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000484
Douglas Gregor42a552f2008-11-05 20:51:48 +0000485CXXDestructorDecl *
486CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000487 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000488 QualType T, bool isInline,
489 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000490 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
491 "Name must refer to a destructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000492 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
493 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000494}
495
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000496void
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000497CXXDestructorDecl::Destroy(ASTContext& C) {
498 C.Deallocate(BaseOrMemberDestructions);
499 CXXMethodDecl::Destroy(C);
500}
501
502void
503CXXDestructorDecl::computeBaseOrMembersToDestroy(ASTContext &C) {
Fariborz Jahanian560de452009-07-15 22:34:08 +0000504 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000505 llvm::SmallVector<uintptr_t, 32> AllToDestruct;
506
Fariborz Jahanian560de452009-07-15 22:34:08 +0000507 for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(),
508 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000509 // Skip over virtual bases which have trivial destructors.
510 CXXRecordDecl *BaseClassDecl
511 = cast<CXXRecordDecl>(VBase->getType()->getAsRecordType()->getDecl());
512 if (BaseClassDecl->hasTrivialDestructor())
513 continue;
514 uintptr_t Member =
Fariborz Jahaniancf183122009-07-22 00:42:46 +0000515 reinterpret_cast<uintptr_t>(VBase->getType().getTypePtr()) | VBASE;
Fariborz Jahanian560de452009-07-15 22:34:08 +0000516 AllToDestruct.push_back(Member);
517 }
518 for (CXXRecordDecl::base_class_iterator Base =
519 ClassDecl->bases_begin(),
520 E = ClassDecl->bases_end(); Base != E; ++Base) {
521 if (Base->isVirtual())
522 continue;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000523 // Skip over virtual bases which have trivial destructors.
524 CXXRecordDecl *BaseClassDecl
525 = cast<CXXRecordDecl>(Base->getType()->getAsRecordType()->getDecl());
526 if (BaseClassDecl->hasTrivialDestructor())
527 continue;
528
529 uintptr_t Member =
Fariborz Jahaniancf183122009-07-22 00:42:46 +0000530 reinterpret_cast<uintptr_t>(Base->getType().getTypePtr()) | DRCTNONVBASE;
Fariborz Jahanian560de452009-07-15 22:34:08 +0000531 AllToDestruct.push_back(Member);
532 }
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000533
Fariborz Jahanian560de452009-07-15 22:34:08 +0000534 // non-static data members.
535 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
536 E = ClassDecl->field_end(); Field != E; ++Field) {
537 QualType FieldType = C.getCanonicalType((*Field)->getType());
538 while (const ArrayType *AT = C.getAsArrayType(FieldType))
539 FieldType = AT->getElementType();
540
Ted Kremenek35366a62009-07-17 17:50:17 +0000541 if (FieldType->getAsRecordType()) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000542 // Skip over virtual bases which have trivial destructors.
543 CXXRecordDecl *BaseClassDecl
544 = cast<CXXRecordDecl>(FieldType->getAsRecordType()->getDecl());
545 if (BaseClassDecl->hasTrivialDestructor())
546 continue;
547 uintptr_t Member = reinterpret_cast<uintptr_t>(*Field);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000548 AllToDestruct.push_back(Member);
549 }
550 }
551
552 unsigned NumDestructions = AllToDestruct.size();
553 if (NumDestructions > 0) {
554 NumBaseOrMemberDestructions = NumDestructions;
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000555 BaseOrMemberDestructions = new (C) uintptr_t [NumDestructions];
Fariborz Jahanian560de452009-07-15 22:34:08 +0000556 // Insert in reverse order.
557 for (int Idx = NumDestructions-1, i=0 ; Idx >= 0; --Idx)
558 BaseOrMemberDestructions[i++] = AllToDestruct[Idx];
559 }
560}
561
562void
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000563CXXConstructorDecl::setBaseOrMemberInitializers(
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000564 ASTContext &C,
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000565 CXXBaseOrMemberInitializer **Initializers,
566 unsigned NumInitializers) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000567 // We need to build the initializer AST according to order of construction
568 // and not what user specified in the Initializers list.
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000569 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext());
Fariborz Jahanian639dead2009-07-14 18:29:14 +0000570 // FIXME. We probably don't need to use AllToInit. But it is cleaner.
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000571 llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit;
572 // Push virtual bases before others.
573 for (CXXRecordDecl::base_class_iterator VBase =
574 ClassDecl->vbases_begin(),
575 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Ted Kremenek35366a62009-07-17 17:50:17 +0000576 const Type * T = VBase->getType()->getAsRecordType();
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000577 unsigned int i = 0;
578 for (i = 0; i < NumInitializers; i++) {
579 CXXBaseOrMemberInitializer *Member = Initializers[i];
580 if (Member->isBaseInitializer() &&
Ted Kremenek35366a62009-07-17 17:50:17 +0000581 Member->getBaseClass()->getAsRecordType() == T) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000582 AllToInit.push_back(Member);
583 break;
584 }
585 }
586 if (i == NumInitializers) {
587 CXXBaseOrMemberInitializer *Member =
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000588 new (C) CXXBaseOrMemberInitializer(VBase->getType(), 0, 0,
589 SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000590 AllToInit.push_back(Member);
591 }
592 }
593 for (CXXRecordDecl::base_class_iterator Base =
594 ClassDecl->bases_begin(),
595 E = ClassDecl->bases_end(); Base != E; ++Base) {
596 // Virtuals are in the virtual base list and already constructed.
597 if (Base->isVirtual())
598 continue;
Ted Kremenek35366a62009-07-17 17:50:17 +0000599 const Type * T = Base->getType()->getAsRecordType();
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000600 unsigned int i = 0;
601 for (i = 0; i < NumInitializers; i++) {
602 CXXBaseOrMemberInitializer *Member = Initializers[i];
Fariborz Jahanian42a52172009-07-14 22:40:50 +0000603 if (Member->isBaseInitializer() &&
Ted Kremenek35366a62009-07-17 17:50:17 +0000604 Member->getBaseClass()->getAsRecordType() == T) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000605 AllToInit.push_back(Member);
606 break;
607 }
608 }
609 if (i == NumInitializers) {
610 CXXBaseOrMemberInitializer *Member =
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000611 new (C) CXXBaseOrMemberInitializer(Base->getType(), 0, 0,
612 SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000613 AllToInit.push_back(Member);
614 }
615 }
616 // non-static data members.
617 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
618 E = ClassDecl->field_end(); Field != E; ++Field) {
619 unsigned int i = 0;
620 for (i = 0; i < NumInitializers; i++) {
621 CXXBaseOrMemberInitializer *Member = Initializers[i];
622 if (Member->isMemberInitializer() && Member->getMember() == (*Field)) {
623 AllToInit.push_back(Member);
624 break;
625 }
626 }
627 if (i == NumInitializers) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000628 QualType FieldType = C.getCanonicalType((*Field)->getType());
Fariborz Jahanian9b9f4242009-07-14 18:56:31 +0000629 while (const ArrayType *AT = C.getAsArrayType(FieldType))
630 FieldType = AT->getElementType();
631
Ted Kremenek35366a62009-07-17 17:50:17 +0000632 if (FieldType->getAsRecordType()) {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000633 CXXBaseOrMemberInitializer *Member =
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000634 new (C) CXXBaseOrMemberInitializer((*Field), 0, 0, SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000635 AllToInit.push_back(Member);
636 }
637 }
638 }
639
640 NumInitializers = AllToInit.size();
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000641 if (NumInitializers > 0) {
642 NumBaseOrMemberInitializers = NumInitializers;
643 BaseOrMemberInitializers =
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000644 new (C) CXXBaseOrMemberInitializer*[NumInitializers];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000645 for (unsigned Idx = 0; Idx < NumInitializers; ++Idx)
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000646 BaseOrMemberInitializers[Idx] = AllToInit[Idx];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000647 }
648}
649
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000650void
651CXXConstructorDecl::Destroy(ASTContext& C) {
652 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000653 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000654}
655
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000656CXXConversionDecl *
657CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000658 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000659 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000660 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
661 "Name must refer to a conversion function");
Steve Naroff3e970492009-01-27 21:25:57 +0000662 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000663}
664
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000665OverloadedFunctionDecl *
666OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000667 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000668 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000669}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000670
Douglas Gregor364e0212009-06-27 21:05:07 +0000671void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
672 Functions.push_back(F);
673 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000674}
675
Douglas Gregordaa439a2009-07-08 10:57:20 +0000676OverloadIterator::reference OverloadIterator::operator*() const {
677 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
678 return FD;
679
680 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
681 return FTD;
682
683 assert(isa<OverloadedFunctionDecl>(D));
684 return *Iter;
685}
686
687OverloadIterator &OverloadIterator::operator++() {
688 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
689 D = 0;
690 return *this;
691 }
692
693 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
694 D = 0;
695
696 return *this;
697}
698
699bool OverloadIterator::Equals(const OverloadIterator &Other) const {
700 if (!D || !Other.D)
701 return D == Other.D;
702
703 if (D != Other.D)
704 return false;
705
706 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
707}
708
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000709LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000710 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000711 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000712 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000713 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000714}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000715
716UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
717 SourceLocation L,
718 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000719 SourceRange QualifierRange,
720 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000721 SourceLocation IdentLoc,
722 NamespaceDecl *Used,
723 DeclContext *CommonAncestor) {
Douglas Gregor8419fa32009-05-30 06:31:56 +0000724 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
725 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000726}
727
Anders Carlsson68771c72009-03-28 22:58:02 +0000728NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
729 SourceLocation L,
730 SourceLocation AliasLoc,
731 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000732 SourceRange QualifierRange,
733 NestedNameSpecifier *Qualifier,
Anders Carlsson68771c72009-03-28 22:58:02 +0000734 SourceLocation IdentLoc,
735 NamedDecl *Namespace) {
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000736 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
737 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000738}
739
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000740UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
741 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
742 SourceLocation UL, NamedDecl* Target,
743 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
744 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
745 TargetNNS, IsTypeNameArg);
746}
747
Anders Carlssonfb311762009-03-14 00:25:26 +0000748StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
749 SourceLocation L, Expr *AssertExpr,
750 StringLiteral *Message) {
751 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
752}
753
754void StaticAssertDecl::Destroy(ASTContext& C) {
755 AssertExpr->Destroy(C);
756 Message->Destroy(C);
757 this->~StaticAssertDecl();
758 C.Deallocate((void *)this);
759}
760
761StaticAssertDecl::~StaticAssertDecl() {
762}
763
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000764static const char *getAccessName(AccessSpecifier AS) {
765 switch (AS) {
766 default:
767 case AS_none:
768 assert("Invalid access specifier!");
769 return 0;
770 case AS_public:
771 return "public";
772 case AS_private:
773 return "private";
774 case AS_protected:
775 return "protected";
776 }
777}
778
779const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
780 AccessSpecifier AS) {
781 return DB << getAccessName(AS);
782}
783
784