blob: 8855e99a26f2147be2041063df7613afcff8a2db [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 Gregor2e1cd422008-11-17 14:58:09 +000030CXXRecordDecl::CXXRecordDecl(TagKind TK, DeclContext *DC,
Douglas Gregor7d7e6722008-11-12 23:21:09 +000031 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),
Douglas Gregor2e1cd422008-11-17 14:58:09 +000035 Constructors(DC, DeclarationName()),
Douglas Gregor7d7e6722008-11-12 23:21:09 +000036 Destructor(0),
Douglas Gregor2e1cd422008-11-17 14:58:09 +000037 Conversions(DC, DeclarationName()) { }
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,
41 CXXRecordDecl* PrevDecl) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +000042 void *Mem = C.getAllocator().Allocate<CXXRecordDecl>();
Douglas Gregor2e1cd422008-11-17 14:58:09 +000043 CXXRecordDecl* R = new (Mem) CXXRecordDecl(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,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000181 SourceLocation L, DeclarationName N,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000182 QualType T, bool isExplicit,
183 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000184 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
185 "Name must refer to a constructor");
Douglas Gregorb48fe382008-10-31 09:07:45 +0000186 void *Mem = C.getAllocator().Allocate<CXXConstructorDecl>();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000187 return new (Mem) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000188 isImplicitlyDeclared);
189}
190
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000191bool CXXConstructorDecl::isDefaultConstructor() const {
192 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000193 // A default constructor for a class X is a constructor of class
194 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000195 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000196 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000197}
198
199bool
200CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
201 unsigned &TypeQuals) const {
202 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000203 // A non-template constructor for class X is a copy constructor
204 // if its first parameter is of type X&, const X&, volatile X& or
205 // const volatile X&, and either there are no other parameters
206 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000207 if ((getNumParams() < 1) ||
208 (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() == 0))
209 return false;
210
211 const ParmVarDecl *Param = getParamDecl(0);
212
213 // Do we have a reference type?
214 const ReferenceType *ParamRefType = Param->getType()->getAsReferenceType();
215 if (!ParamRefType)
216 return false;
217
218 // Is it a reference to our class type?
219 QualType PointeeType
220 = Context.getCanonicalType(ParamRefType->getPointeeType());
221 QualType ClassTy
222 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
223 if (PointeeType.getUnqualifiedType() != ClassTy)
224 return false;
225
226 // We have a copy constructor.
227 TypeQuals = PointeeType.getCVRQualifiers();
228 return true;
229}
230
Douglas Gregor60d62c22008-10-31 16:23:19 +0000231bool CXXConstructorDecl::isConvertingConstructor() const {
232 // C++ [class.conv.ctor]p1:
233 // A constructor declared without the function-specifier explicit
234 // that can be called with a single parameter specifies a
235 // conversion from the type of its first parameter to the type of
236 // its class. Such a constructor is called a converting
237 // constructor.
238 if (isExplicit())
239 return false;
240
241 return (getNumParams() == 0 &&
242 getType()->getAsFunctionTypeProto()->isVariadic()) ||
243 (getNumParams() == 1) ||
244 (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() != 0);
245}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000246
Douglas Gregor42a552f2008-11-05 20:51:48 +0000247CXXDestructorDecl *
248CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000249 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000250 QualType T, bool isInline,
251 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000252 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
253 "Name must refer to a destructor");
Douglas Gregor42a552f2008-11-05 20:51:48 +0000254 void *Mem = C.getAllocator().Allocate<CXXDestructorDecl>();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000255 return new (Mem) CXXDestructorDecl(RD, L, N, T, isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000256 isImplicitlyDeclared);
257}
258
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000259CXXConversionDecl *
260CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000261 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000262 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000263 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
264 "Name must refer to a conversion function");
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000265 void *Mem = C.getAllocator().Allocate<CXXConversionDecl>();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000266 return new (Mem) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000267}
268
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000269CXXClassVarDecl *CXXClassVarDecl::Create(ASTContext &C, CXXRecordDecl *RD,
270 SourceLocation L, IdentifierInfo *Id,
271 QualType T, ScopedDecl *PrevDecl) {
272 void *Mem = C.getAllocator().Allocate<CXXClassVarDecl>();
273 return new (Mem) CXXClassVarDecl(RD, L, Id, T, PrevDecl);
274}
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000275
276OverloadedFunctionDecl *
277OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000278 DeclarationName N) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000279 void *Mem = C.getAllocator().Allocate<OverloadedFunctionDecl>();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000280 return new (Mem) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000281}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000282
283LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
284 SourceLocation L,
285 LanguageIDs Lang, Decl *D) {
286 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
287 return new (Mem) LinkageSpecDecl(L, Lang, D);
288}
289