blob: fa9beb0c3881e9878bdf67d3941a399c2d4a0135 [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());
570 llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit;
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000571 llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields;
572
573 for (unsigned i = 0; i < NumInitializers; i++) {
574 CXXBaseOrMemberInitializer *Member = Initializers[i];
575 const void * Key = Member->isBaseInitializer() ?
576 reinterpret_cast<const void *>(
577 Member->getBaseClass()->getAsRecordType()) :
578 reinterpret_cast<const void *>(Member->getMember());
579 AllBaseFields[Key] = Member;
580 }
581
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000582 // Push virtual bases before others.
583 for (CXXRecordDecl::base_class_iterator VBase =
584 ClassDecl->vbases_begin(),
585 E = ClassDecl->vbases_end(); VBase != E; ++VBase) {
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000586 const void *Key = reinterpret_cast<const void *>(
587 VBase->getType()->getAsRecordType());
588 if (AllBaseFields[Key])
589 AllToInit.push_back(AllBaseFields[Key]);
590 else {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000591 CXXBaseOrMemberInitializer *Member =
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000592 new (C) CXXBaseOrMemberInitializer(VBase->getType(), 0, 0,
593 SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000594 AllToInit.push_back(Member);
595 }
596 }
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000597
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000598 for (CXXRecordDecl::base_class_iterator Base =
599 ClassDecl->bases_begin(),
600 E = ClassDecl->bases_end(); Base != E; ++Base) {
601 // Virtuals are in the virtual base list and already constructed.
602 if (Base->isVirtual())
603 continue;
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000604 const void *Key = reinterpret_cast<const void *>(
605 Base->getType()->getAsRecordType());
606 if (AllBaseFields[Key])
607 AllToInit.push_back(AllBaseFields[Key]);
608 else {
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000609 CXXBaseOrMemberInitializer *Member =
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000610 new (C) CXXBaseOrMemberInitializer(Base->getType(), 0, 0,
611 SourceLocation());
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000612 AllToInit.push_back(Member);
613 }
614 }
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000615
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000616 // non-static data members.
617 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
618 E = ClassDecl->field_end(); Field != E; ++Field) {
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000619 const void * Key = reinterpret_cast<const void *>(*Field);
620 if (AllBaseFields[Key]) {
621 AllToInit.push_back(AllBaseFields[Key]);
622 continue;
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000623 }
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000624 QualType FieldType = C.getCanonicalType((*Field)->getType());
625 while (const ArrayType *AT = C.getAsArrayType(FieldType))
626 FieldType = AT->getElementType();
Fariborz Jahanian9b9f4242009-07-14 18:56:31 +0000627
Fariborz Jahanianaa266502009-07-22 20:25:00 +0000628 if (FieldType->getAsRecordType()) {
629 CXXBaseOrMemberInitializer *Member =
630 new (C) CXXBaseOrMemberInitializer((*Field), 0, 0, SourceLocation());
631 AllToInit.push_back(Member);
632 }
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000633 }
634
635 NumInitializers = AllToInit.size();
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000636 if (NumInitializers > 0) {
637 NumBaseOrMemberInitializers = NumInitializers;
638 BaseOrMemberInitializers =
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000639 new (C) CXXBaseOrMemberInitializer*[NumInitializers];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000640 for (unsigned Idx = 0; Idx < NumInitializers; ++Idx)
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000641 BaseOrMemberInitializers[Idx] = AllToInit[Idx];
Fariborz Jahaniand45c3632009-07-01 21:05:43 +0000642 }
643}
644
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000645void
646CXXConstructorDecl::Destroy(ASTContext& C) {
647 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian0d3c26c2009-07-07 16:24:08 +0000648 CXXMethodDecl::Destroy(C);
Fariborz Jahanian73b85f32009-07-01 23:35:25 +0000649}
650
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000651CXXConversionDecl *
652CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000653 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000654 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000655 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
656 "Name must refer to a conversion function");
Steve Naroff3e970492009-01-27 21:25:57 +0000657 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000658}
659
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000660OverloadedFunctionDecl *
661OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000662 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000663 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000664}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000665
Douglas Gregor364e0212009-06-27 21:05:07 +0000666void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) {
667 Functions.push_back(F);
668 this->setLocation(F.get()->getLocation());
Douglas Gregore53060f2009-06-25 22:08:12 +0000669}
670
Douglas Gregordaa439a2009-07-08 10:57:20 +0000671OverloadIterator::reference OverloadIterator::operator*() const {
672 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
673 return FD;
674
675 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
676 return FTD;
677
678 assert(isa<OverloadedFunctionDecl>(D));
679 return *Iter;
680}
681
682OverloadIterator &OverloadIterator::operator++() {
683 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
684 D = 0;
685 return *this;
686 }
687
688 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end())
689 D = 0;
690
691 return *this;
692}
693
694bool OverloadIterator::Equals(const OverloadIterator &Other) const {
695 if (!D || !Other.D)
696 return D == Other.D;
697
698 if (D != Other.D)
699 return false;
700
701 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter;
702}
703
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000704LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000705 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000706 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000707 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000708 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000709}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000710
711UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
712 SourceLocation L,
713 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +0000714 SourceRange QualifierRange,
715 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000716 SourceLocation IdentLoc,
717 NamespaceDecl *Used,
718 DeclContext *CommonAncestor) {
Douglas Gregor8419fa32009-05-30 06:31:56 +0000719 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
720 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000721}
722
Anders Carlsson68771c72009-03-28 22:58:02 +0000723NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
724 SourceLocation L,
725 SourceLocation AliasLoc,
726 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000727 SourceRange QualifierRange,
728 NestedNameSpecifier *Qualifier,
Anders Carlsson68771c72009-03-28 22:58:02 +0000729 SourceLocation IdentLoc,
730 NamedDecl *Namespace) {
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000731 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
732 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +0000733}
734
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000735UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
736 SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
737 SourceLocation UL, NamedDecl* Target,
738 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) {
739 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target,
740 TargetNNS, IsTypeNameArg);
741}
742
Anders Carlssonfb311762009-03-14 00:25:26 +0000743StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
744 SourceLocation L, Expr *AssertExpr,
745 StringLiteral *Message) {
746 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
747}
748
749void StaticAssertDecl::Destroy(ASTContext& C) {
750 AssertExpr->Destroy(C);
751 Message->Destroy(C);
752 this->~StaticAssertDecl();
753 C.Deallocate((void *)this);
754}
755
756StaticAssertDecl::~StaticAssertDecl() {
757}
758
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000759static const char *getAccessName(AccessSpecifier AS) {
760 switch (AS) {
761 default:
762 case AS_none:
763 assert("Invalid access specifier!");
764 return 0;
765 case AS_public:
766 return "public";
767 case AS_private:
768 return "private";
769 case AS_protected:
770 return "protected";
771 }
772}
773
774const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
775 AccessSpecifier AS) {
776 return DB << getAccessName(AS);
777}
778
779