blob: 1de640743d9fe7c6fd95aad4d3eb7ddaf3aa76d2 [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"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000017using namespace clang;
18
19//===----------------------------------------------------------------------===//
20// Decl Allocation/Deallocation Method Implementations
21//===----------------------------------------------------------------------===//
22
23CXXFieldDecl *CXXFieldDecl::Create(ASTContext &C, CXXRecordDecl *RD,
24 SourceLocation L, IdentifierInfo *Id,
25 QualType T, Expr *BW) {
26 void *Mem = C.getAllocator().Allocate<CXXFieldDecl>();
27 return new (Mem) CXXFieldDecl(RD, L, Id, T, BW);
28}
29
Douglas Gregor7d7e6722008-11-12 23:21:09 +000030CXXRecordDecl::CXXRecordDecl(ASTContext &C, TagKind TK, DeclContext *DC,
31 SourceLocation L, IdentifierInfo *Id)
32 : RecordDecl(CXXRecord, TK, DC, L, Id), DeclContext(CXXRecord),
33 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
34 Aggregate(true), Polymorphic(false), Bases(0), NumBases(0),
35 Constructors(DC, &C.Idents.getConstructorId()),
36 Destructor(0),
37 Conversions(DC, &C.Idents.getConversionFunctionId()) { }
38
Ted Kremenek4b7c9832008-09-05 17:16:31 +000039CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
40 SourceLocation L, IdentifierInfo *Id,
41 CXXRecordDecl* PrevDecl) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +000042 void *Mem = C.getAllocator().Allocate<CXXRecordDecl>();
Douglas Gregor7d7e6722008-11-12 23:21:09 +000043 CXXRecordDecl* R = new (Mem) CXXRecordDecl(C, TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000044 C.getTypeDeclType(R, PrevDecl);
45 return R;
46}
47
Douglas Gregorf8268ae2008-10-22 17:49:05 +000048CXXRecordDecl::~CXXRecordDecl() {
Douglas Gregorf8268ae2008-10-22 17:49:05 +000049 delete [] Bases;
50}
51
Douglas Gregorb48fe382008-10-31 09:07:45 +000052void CXXRecordDecl::Destroy(ASTContext &C) {
53 for (OverloadedFunctionDecl::function_iterator func
54 = Constructors.function_begin();
55 func != Constructors.function_end(); ++func)
56 (*func)->Destroy(C);
Douglas Gregor42a552f2008-11-05 20:51:48 +000057
58 if (isDefinition())
59 Destructor->Destroy(C);
60
Douglas Gregor2f1bc522008-11-07 20:08:42 +000061 for (OverloadedFunctionDecl::function_iterator func
62 = Conversions.function_begin();
63 func != Conversions.function_end(); ++func)
64 (*func)->Destroy(C);
65
Douglas Gregorb48fe382008-10-31 09:07:45 +000066 RecordDecl::Destroy(C);
67}
68
Douglas Gregor57c856b2008-10-23 18:13:27 +000069void
70CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
71 unsigned NumBases) {
Douglas Gregor64bffa92008-11-05 16:20:31 +000072 // C++ [dcl.init.aggr]p1:
73 // An aggregate is an array or a class (clause 9) with [...]
74 // no base classes [...].
75 Aggregate = false;
76
Douglas Gregor57c856b2008-10-23 18:13:27 +000077 if (this->Bases)
78 delete [] this->Bases;
79
80 this->Bases = new CXXBaseSpecifier[NumBases];
81 this->NumBases = NumBases;
82 for (unsigned i = 0; i < NumBases; ++i)
83 this->Bases[i] = *Bases[i];
84}
85
Douglas Gregor396b7cd2008-11-03 17:51:48 +000086bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
87 for (OverloadedFunctionDecl::function_const_iterator Con
88 = Constructors.function_begin();
89 Con != Constructors.function_end(); ++Con) {
90 unsigned TypeQuals;
91 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context, TypeQuals) &&
92 (TypeQuals & QualType::Const != 0))
93 return true;
94 }
95 return false;
96}
97
Douglas Gregor030ff0c2008-10-31 20:25:05 +000098void
99CXXRecordDecl::addConstructor(ASTContext &Context,
100 CXXConstructorDecl *ConDecl) {
101 if (!ConDecl->isImplicitlyDeclared()) {
102 // Note that we have a user-declared constructor.
103 UserDeclaredConstructor = true;
104
Douglas Gregor64bffa92008-11-05 16:20:31 +0000105 // C++ [dcl.init.aggr]p1:
106 // An aggregate is an array or a class (clause 9) with no
107 // user-declared constructors (12.1) [...].
108 Aggregate = false;
109
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000110 // Note when we have a user-declared copy constructor, which will
111 // suppress the implicit declaration of a copy constructor.
112 if (ConDecl->isCopyConstructor(Context))
113 UserDeclaredCopyConstructor = true;
114 }
115
116 Constructors.addOverload(ConDecl);
117}
118
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000119void CXXRecordDecl::addConversionFunction(ASTContext &Context,
120 CXXConversionDecl *ConvDecl) {
121 Conversions.addOverload(ConvDecl);
122}
123
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000124CXXMethodDecl *
125CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
126 SourceLocation L, IdentifierInfo *Id,
127 QualType T, bool isStatic, bool isInline,
128 ScopedDecl *PrevDecl) {
129 void *Mem = C.getAllocator().Allocate<CXXMethodDecl>();
Argyrios Kyrtzidis5540a722008-11-08 11:24:06 +0000130 return new (Mem) CXXMethodDecl(CXXMethod, RD, L, Id, T, isStatic, isInline, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000131}
132
133QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000134 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
135 // If the member function is declared const, the type of this is const X*,
136 // if the member function is declared volatile, the type of this is
137 // volatile X*, and if the member function is declared const volatile,
138 // the type of this is const volatile X*.
139
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000140 assert(isInstance() && "No 'this' for static methods!");
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000141 QualType ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000142 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
143 return C.getPointerType(ClassTy).withConst();
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000144}
145
Douglas Gregor7ad83902008-11-05 04:29:56 +0000146CXXBaseOrMemberInitializer::
147CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs)
148 : Args(0), NumArgs(0) {
149 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
150 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
151 BaseOrMember |= 0x01;
152
153 if (NumArgs > 0) {
154 this->NumArgs = NumArgs;
155 this->Args = new Expr*[NumArgs];
156 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
157 this->Args[Idx] = Args[Idx];
158 }
159}
160
161CXXBaseOrMemberInitializer::
162CXXBaseOrMemberInitializer(CXXFieldDecl *Member, Expr **Args, unsigned NumArgs)
163 : Args(0), NumArgs(0) {
164 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
165 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
166
167 if (NumArgs > 0) {
168 this->NumArgs = NumArgs;
169 this->Args = new Expr*[NumArgs];
170 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
171 this->Args[Idx] = Args[Idx];
172 }
173}
174
175CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
176 delete [] Args;
177}
178
Douglas Gregorb48fe382008-10-31 09:07:45 +0000179CXXConstructorDecl *
180CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
181 SourceLocation L, IdentifierInfo *Id,
182 QualType T, bool isExplicit,
183 bool isInline, bool isImplicitlyDeclared) {
184 void *Mem = C.getAllocator().Allocate<CXXConstructorDecl>();
185 return new (Mem) CXXConstructorDecl(RD, L, Id, T, isExplicit, isInline,
186 isImplicitlyDeclared);
187}
188
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000189bool CXXConstructorDecl::isDefaultConstructor() const {
190 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000191 // A default constructor for a class X is a constructor of class
192 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000193 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000194 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000195}
196
197bool
198CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
199 unsigned &TypeQuals) const {
200 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000201 // A non-template constructor for class X is a copy constructor
202 // if its first parameter is of type X&, const X&, volatile X& or
203 // const volatile X&, and either there are no other parameters
204 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000205 if ((getNumParams() < 1) ||
206 (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() == 0))
207 return false;
208
209 const ParmVarDecl *Param = getParamDecl(0);
210
211 // Do we have a reference type?
212 const ReferenceType *ParamRefType = Param->getType()->getAsReferenceType();
213 if (!ParamRefType)
214 return false;
215
216 // Is it a reference to our class type?
217 QualType PointeeType
218 = Context.getCanonicalType(ParamRefType->getPointeeType());
219 QualType ClassTy
220 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
221 if (PointeeType.getUnqualifiedType() != ClassTy)
222 return false;
223
224 // We have a copy constructor.
225 TypeQuals = PointeeType.getCVRQualifiers();
226 return true;
227}
228
Douglas Gregor60d62c22008-10-31 16:23:19 +0000229bool CXXConstructorDecl::isConvertingConstructor() const {
230 // C++ [class.conv.ctor]p1:
231 // A constructor declared without the function-specifier explicit
232 // that can be called with a single parameter specifies a
233 // conversion from the type of its first parameter to the type of
234 // its class. Such a constructor is called a converting
235 // constructor.
236 if (isExplicit())
237 return false;
238
239 return (getNumParams() == 0 &&
240 getType()->getAsFunctionTypeProto()->isVariadic()) ||
241 (getNumParams() == 1) ||
242 (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() != 0);
243}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000244
Douglas Gregor7d7e6722008-11-12 23:21:09 +0000245const char *CXXConstructorDecl::getName() const {
246 return getParent()->getName();
247}
248
Douglas Gregor42a552f2008-11-05 20:51:48 +0000249CXXDestructorDecl *
250CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
251 SourceLocation L, IdentifierInfo *Id,
252 QualType T, bool isInline,
253 bool isImplicitlyDeclared) {
254 void *Mem = C.getAllocator().Allocate<CXXDestructorDecl>();
255 return new (Mem) CXXDestructorDecl(RD, L, Id, T, isInline,
256 isImplicitlyDeclared);
257}
258
Douglas Gregor7d7e6722008-11-12 23:21:09 +0000259CXXDestructorDecl::~CXXDestructorDecl() {
260 delete [] Name;
261}
262
263const char *CXXDestructorDecl::getName() const {
264 if (!Name) {
265 std::string Builder = "~";
266 Builder += getParent()->getName();
267 Name = new char[Builder.size()+1];
268 strcpy(Name, Builder.c_str());
269 }
270 return Name;
271}
272
273CXXConversionDecl::~CXXConversionDecl() {
274 delete [] Name;
275}
276
277const char *CXXConversionDecl::getName() const {
278 if (!Name) {
279 std::string Builder = "operator ";
280 Builder += getConversionType().getAsString();
281 Name = new char[Builder.size()+1];
282 strcpy(Name, Builder.c_str());
283 }
284 return Name;
285}
286
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000287CXXConversionDecl *
288CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
289 SourceLocation L, IdentifierInfo *Id,
290 QualType T, bool isInline, bool isExplicit) {
291 void *Mem = C.getAllocator().Allocate<CXXConversionDecl>();
292 return new (Mem) CXXConversionDecl(RD, L, Id, T, isInline, isExplicit);
293}
294
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000295CXXClassVarDecl *CXXClassVarDecl::Create(ASTContext &C, CXXRecordDecl *RD,
296 SourceLocation L, IdentifierInfo *Id,
297 QualType T, ScopedDecl *PrevDecl) {
298 void *Mem = C.getAllocator().Allocate<CXXClassVarDecl>();
299 return new (Mem) CXXClassVarDecl(RD, L, Id, T, PrevDecl);
300}
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000301
302OverloadedFunctionDecl *
303OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
304 IdentifierInfo *Id) {
305 void *Mem = C.getAllocator().Allocate<OverloadedFunctionDecl>();
306 return new (Mem) OverloadedFunctionDecl(DC, Id);
307}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000308
309LinkageSpecDecl *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