blob: 27065a75bae3ba2790d2940e8bbd44d8cae60685 [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"
15#include "clang/AST/ASTContext.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000016#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000017#include "llvm/ADT/STLExtras.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000018using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// Decl Allocation/Deallocation Method Implementations
22//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000023
24TemplateTypeParmDecl *
25TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC,
Nate Begemanfea86852008-12-16 19:57:09 +000026 SourceLocation L, IdentifierInfo *Id,
27 bool Typename) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000028 void *Mem = C.getAllocator().Allocate<TemplateTypeParmDecl>();
29 return new (Mem) TemplateTypeParmDecl(DC, L, Id, Typename);
30}
31
32NonTypeTemplateParmDecl *
33NonTypeTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
Nate Begemanfea86852008-12-16 19:57:09 +000034 SourceLocation L, IdentifierInfo *Id,
35 QualType T, SourceLocation TypeSpecStartLoc) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000036 void *Mem = C.getAllocator().Allocate<NonTypeTemplateParmDecl>();
37 return new (Mem) NonTypeTemplateParmDecl(DC, L, Id, T, TypeSpecStartLoc);
38}
39
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +000040TemplateParameterList::TemplateParameterList(Decl **Params, unsigned NumParams)
41 : NumParams(NumParams) {
42 for (unsigned Idx = 0; Idx < NumParams; ++Idx)
43 begin()[Idx] = Params[Idx];
44}
45
46TemplateParameterList *
47TemplateParameterList::Create(ASTContext &C, Decl **Params,
48 unsigned NumParams) {
49 unsigned Size = sizeof(TemplateParameterList) + sizeof(Decl *) * NumParams;
50 unsigned Align = llvm::AlignOf<TemplateParameterList>::Alignment;
51 void *Mem = C.getAllocator().Allocate(Size, Align);
52 return new (Mem) TemplateParameterList(Params, NumParams);
53}
54
Douglas Gregor2e1cd422008-11-17 14:58:09 +000055CXXRecordDecl::CXXRecordDecl(TagKind TK, DeclContext *DC,
Douglas Gregor7d7e6722008-11-12 23:21:09 +000056 SourceLocation L, IdentifierInfo *Id)
Douglas Gregor44b43212008-12-11 16:49:14 +000057 : RecordDecl(CXXRecord, TK, DC, L, Id),
Douglas Gregor7d7e6722008-11-12 23:21:09 +000058 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Douglas Gregor9e7d9de2008-12-15 21:24:18 +000059 UserDeclaredDestructor(false), Aggregate(true), Polymorphic(false),
60 Bases(0), NumBases(0),
Douglas Gregor2e1cd422008-11-17 14:58:09 +000061 Conversions(DC, DeclarationName()) { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000062
Ted Kremenek4b7c9832008-09-05 17:16:31 +000063CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
64 SourceLocation L, IdentifierInfo *Id,
65 CXXRecordDecl* PrevDecl) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +000066 void *Mem = C.getAllocator().Allocate<CXXRecordDecl>();
Douglas Gregor2e1cd422008-11-17 14:58:09 +000067 CXXRecordDecl* R = new (Mem) CXXRecordDecl(TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000068 C.getTypeDeclType(R, PrevDecl);
69 return R;
70}
71
Douglas Gregorf8268ae2008-10-22 17:49:05 +000072CXXRecordDecl::~CXXRecordDecl() {
Douglas Gregorf8268ae2008-10-22 17:49:05 +000073 delete [] Bases;
74}
75
Douglas Gregor57c856b2008-10-23 18:13:27 +000076void
77CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
78 unsigned NumBases) {
Douglas Gregor64bffa92008-11-05 16:20:31 +000079 // C++ [dcl.init.aggr]p1:
80 // An aggregate is an array or a class (clause 9) with [...]
81 // no base classes [...].
82 Aggregate = false;
83
Douglas Gregor57c856b2008-10-23 18:13:27 +000084 if (this->Bases)
85 delete [] this->Bases;
86
87 this->Bases = new CXXBaseSpecifier[NumBases];
88 this->NumBases = NumBases;
89 for (unsigned i = 0; i < NumBases; ++i)
90 this->Bases[i] = *Bases[i];
91}
92
Douglas Gregor396b7cd2008-11-03 17:51:48 +000093bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Douglas Gregor9e7d9de2008-12-15 21:24:18 +000094 QualType ClassType = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
95 DeclarationName ConstructorName
96 = Context.DeclarationNames.getCXXConstructorName(
97 Context.getCanonicalType(ClassType));
98 unsigned TypeQuals;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000099 DeclContext::lookup_const_iterator Con, ConEnd;
100 for (llvm::tie(Con, ConEnd) = this->lookup(Context, ConstructorName);
101 Con != ConEnd; ++Con) {
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000102 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context, TypeQuals) &&
Eli Friedmane8e32052008-12-16 20:06:41 +0000103 (TypeQuals & QualType::Const) != 0)
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000104 return true;
105 }
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000106
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000107 return false;
108}
109
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000110void
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000111CXXRecordDecl::addedConstructor(ASTContext &Context,
112 CXXConstructorDecl *ConDecl) {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000113 if (!ConDecl->isImplicitlyDeclared()) {
114 // Note that we have a user-declared constructor.
115 UserDeclaredConstructor = true;
116
Douglas Gregor64bffa92008-11-05 16:20:31 +0000117 // C++ [dcl.init.aggr]p1:
118 // An aggregate is an array or a class (clause 9) with no
119 // user-declared constructors (12.1) [...].
120 Aggregate = false;
121
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000122 // Note when we have a user-declared copy constructor, which will
123 // suppress the implicit declaration of a copy constructor.
124 if (ConDecl->isCopyConstructor(Context))
125 UserDeclaredCopyConstructor = true;
126 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000127}
128
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000129void CXXRecordDecl::addConversionFunction(ASTContext &Context,
130 CXXConversionDecl *ConvDecl) {
131 Conversions.addOverload(ConvDecl);
132}
133
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000134CXXMethodDecl *
135CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000136 SourceLocation L, DeclarationName N,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000137 QualType T, bool isStatic, bool isInline,
138 ScopedDecl *PrevDecl) {
139 void *Mem = C.getAllocator().Allocate<CXXMethodDecl>();
Douglas Gregor10bd3682008-11-17 22:58:34 +0000140 return new (Mem) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline,
141 PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000142}
143
144QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000145 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
146 // If the member function is declared const, the type of this is const X*,
147 // if the member function is declared volatile, the type of this is
148 // volatile X*, and if the member function is declared const volatile,
149 // the type of this is const volatile X*.
150
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000151 assert(isInstance() && "No 'this' for static methods!");
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000152 QualType ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000153 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
154 return C.getPointerType(ClassTy).withConst();
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000155}
156
Douglas Gregor7ad83902008-11-05 04:29:56 +0000157CXXBaseOrMemberInitializer::
158CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs)
159 : Args(0), NumArgs(0) {
160 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
161 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
162 BaseOrMember |= 0x01;
163
164 if (NumArgs > 0) {
165 this->NumArgs = NumArgs;
166 this->Args = new Expr*[NumArgs];
167 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
168 this->Args[Idx] = Args[Idx];
169 }
170}
171
172CXXBaseOrMemberInitializer::
Douglas Gregor44b43212008-12-11 16:49:14 +0000173CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs)
Douglas Gregor7ad83902008-11-05 04:29:56 +0000174 : Args(0), NumArgs(0) {
175 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
176 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
177
178 if (NumArgs > 0) {
179 this->NumArgs = NumArgs;
180 this->Args = new Expr*[NumArgs];
181 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
182 this->Args[Idx] = Args[Idx];
183 }
184}
185
186CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
187 delete [] Args;
188}
189
Douglas Gregorb48fe382008-10-31 09:07:45 +0000190CXXConstructorDecl *
191CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000192 SourceLocation L, DeclarationName N,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000193 QualType T, bool isExplicit,
194 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000195 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
196 "Name must refer to a constructor");
Douglas Gregorb48fe382008-10-31 09:07:45 +0000197 void *Mem = C.getAllocator().Allocate<CXXConstructorDecl>();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000198 return new (Mem) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000199 isImplicitlyDeclared);
200}
201
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000202bool CXXConstructorDecl::isDefaultConstructor() const {
203 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000204 // A default constructor for a class X is a constructor of class
205 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000206 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000207 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000208}
209
210bool
211CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
212 unsigned &TypeQuals) const {
213 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000214 // A non-template constructor for class X is a copy constructor
215 // if its first parameter is of type X&, const X&, volatile X& or
216 // const volatile X&, and either there are no other parameters
217 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000218 if ((getNumParams() < 1) ||
219 (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() == 0))
220 return false;
221
222 const ParmVarDecl *Param = getParamDecl(0);
223
224 // Do we have a reference type?
225 const ReferenceType *ParamRefType = Param->getType()->getAsReferenceType();
226 if (!ParamRefType)
227 return false;
228
229 // Is it a reference to our class type?
230 QualType PointeeType
231 = Context.getCanonicalType(ParamRefType->getPointeeType());
232 QualType ClassTy
233 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
234 if (PointeeType.getUnqualifiedType() != ClassTy)
235 return false;
236
237 // We have a copy constructor.
238 TypeQuals = PointeeType.getCVRQualifiers();
239 return true;
240}
241
Douglas Gregor60d62c22008-10-31 16:23:19 +0000242bool CXXConstructorDecl::isConvertingConstructor() const {
243 // C++ [class.conv.ctor]p1:
244 // A constructor declared without the function-specifier explicit
245 // that can be called with a single parameter specifies a
246 // conversion from the type of its first parameter to the type of
247 // its class. Such a constructor is called a converting
248 // constructor.
249 if (isExplicit())
250 return false;
251
252 return (getNumParams() == 0 &&
253 getType()->getAsFunctionTypeProto()->isVariadic()) ||
254 (getNumParams() == 1) ||
255 (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() != 0);
256}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000257
Douglas Gregor42a552f2008-11-05 20:51:48 +0000258CXXDestructorDecl *
259CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000260 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000261 QualType T, bool isInline,
262 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000263 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
264 "Name must refer to a destructor");
Douglas Gregor42a552f2008-11-05 20:51:48 +0000265 void *Mem = C.getAllocator().Allocate<CXXDestructorDecl>();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000266 return new (Mem) CXXDestructorDecl(RD, L, N, T, isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000267 isImplicitlyDeclared);
268}
269
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000270CXXConversionDecl *
271CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000272 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000273 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000274 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
275 "Name must refer to a conversion function");
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000276 void *Mem = C.getAllocator().Allocate<CXXConversionDecl>();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000277 return new (Mem) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000278}
279
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000280CXXClassVarDecl *CXXClassVarDecl::Create(ASTContext &C, CXXRecordDecl *RD,
281 SourceLocation L, IdentifierInfo *Id,
282 QualType T, ScopedDecl *PrevDecl) {
283 void *Mem = C.getAllocator().Allocate<CXXClassVarDecl>();
284 return new (Mem) CXXClassVarDecl(RD, L, Id, T, PrevDecl);
285}
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000286
287OverloadedFunctionDecl *
288OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000289 DeclarationName N) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000290 void *Mem = C.getAllocator().Allocate<OverloadedFunctionDecl>();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000291 return new (Mem) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000292}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000293
Douglas Gregorf44515a2008-12-16 22:23:02 +0000294LinkageSpecDecl::LinkageSpecDecl(SourceLocation L, LanguageIDs lang,
295 Decl **InDecls, unsigned InNumDecls)
296 : Decl(LinkageSpec, L), Language(lang), HadBraces(true),
297 Decls(0), NumDecls(InNumDecls) {
298 Decl **NewDecls = new Decl*[NumDecls];
299 for (unsigned I = 0; I < NumDecls; ++I)
300 NewDecls[I] = InDecls[I];
301 Decls = NewDecls;
302}
303
304LinkageSpecDecl::~LinkageSpecDecl() {
305 if (HadBraces)
306 delete [] (Decl**)Decls;
307}
308
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000309LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
310 SourceLocation L,
311 LanguageIDs Lang, Decl *D) {
312 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
313 return new (Mem) LinkageSpecDecl(L, Lang, D);
314}
315
Douglas Gregorf44515a2008-12-16 22:23:02 +0000316LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
317 SourceLocation L,
318 LanguageIDs Lang,
319 Decl **Decls, unsigned NumDecls) {
320 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
321 return new (Mem) LinkageSpecDecl(L, Lang, Decls, NumDecls);
322}
323
324LinkageSpecDecl::decl_const_iterator LinkageSpecDecl::decls_begin() const {
325 if (hasBraces()) return (Decl**)Decls;
326 else return (Decl**)&Decls;
327}
328
329LinkageSpecDecl::decl_iterator LinkageSpecDecl::decls_end() const {
330 if (hasBraces()) return (Decl**)Decls + NumDecls;
331 else return (Decl**)&Decls + 1;
332}