blob: fd900834caf468d2f57d3a2d01ce7c9cb5e799fc [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"
Anders Carlssonfb311762009-03-14 00:25:26 +000016#include "clang/AST/Expr.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000017#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000018#include "llvm/ADT/STLExtras.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000019using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// Decl Allocation/Deallocation Method Implementations
23//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000024
Douglas Gregor3e00bad2009-02-17 01:05:43 +000025CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Douglas Gregor7d7e6722008-11-12 23:21:09 +000026 SourceLocation L, IdentifierInfo *Id)
Douglas Gregor3e00bad2009-02-17 01:05:43 +000027 : RecordDecl(K, TK, DC, L, Id),
Douglas Gregor7d7e6722008-11-12 23:21:09 +000028 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000029 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
30 Aggregate(true), PlainOldData(true), Polymorphic(false), Bases(0),
31 NumBases(0), Conversions(DC, DeclarationName()) { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000032
Ted Kremenek4b7c9832008-09-05 17:16:31 +000033CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
34 SourceLocation L, IdentifierInfo *Id,
35 CXXRecordDecl* PrevDecl) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +000036 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000037 C.getTypeDeclType(R, PrevDecl);
38 return R;
39}
40
Douglas Gregorf8268ae2008-10-22 17:49:05 +000041CXXRecordDecl::~CXXRecordDecl() {
Douglas Gregorf8268ae2008-10-22 17:49:05 +000042 delete [] Bases;
43}
44
Douglas Gregor57c856b2008-10-23 18:13:27 +000045void
46CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
47 unsigned NumBases) {
Douglas Gregor64bffa92008-11-05 16:20:31 +000048 // C++ [dcl.init.aggr]p1:
49 // An aggregate is an array or a class (clause 9) with [...]
50 // no base classes [...].
51 Aggregate = false;
52
Douglas Gregor57c856b2008-10-23 18:13:27 +000053 if (this->Bases)
54 delete [] this->Bases;
55
Douglas Gregor2943aed2009-03-03 04:44:36 +000056 // FIXME: allocate using the ASTContext
Douglas Gregor57c856b2008-10-23 18:13:27 +000057 this->Bases = new CXXBaseSpecifier[NumBases];
58 this->NumBases = NumBases;
59 for (unsigned i = 0; i < NumBases; ++i)
60 this->Bases[i] = *Bases[i];
61}
62
Douglas Gregor396b7cd2008-11-03 17:51:48 +000063bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +000064 QualType ClassType
65 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Douglas Gregor9e7d9de2008-12-15 21:24:18 +000066 DeclarationName ConstructorName
67 = Context.DeclarationNames.getCXXConstructorName(
68 Context.getCanonicalType(ClassType));
69 unsigned TypeQuals;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000070 DeclContext::lookup_const_iterator Con, ConEnd;
Steve Naroff0701bbb2009-01-08 17:28:14 +000071 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000072 Con != ConEnd; ++Con) {
Douglas Gregor396b7cd2008-11-03 17:51:48 +000073 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context, TypeQuals) &&
Eli Friedmane8e32052008-12-16 20:06:41 +000074 (TypeQuals & QualType::Const) != 0)
Douglas Gregor396b7cd2008-11-03 17:51:48 +000075 return true;
76 }
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000077
Douglas Gregor396b7cd2008-11-03 17:51:48 +000078 return false;
79}
80
Sebastian Redl64b45f72009-01-05 20:52:13 +000081bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context) const {
82 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
83 const_cast<CXXRecordDecl*>(this)));
84 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
85
86 DeclContext::lookup_const_iterator Op, OpEnd;
Steve Naroff0701bbb2009-01-08 17:28:14 +000087 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +000088 Op != OpEnd; ++Op) {
89 // C++ [class.copy]p9:
90 // A user-declared copy assignment operator is a non-static non-template
91 // member function of class X with exactly one parameter of type X, X&,
92 // const X&, volatile X& or const volatile X&.
93 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
94 if (Method->isStatic())
95 continue;
96 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor72564e72009-02-26 23:50:07 +000097 const FunctionProtoType *FnType =
98 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +000099 assert(FnType && "Overloaded operator has no prototype.");
100 // Don't assert on this; an invalid decl might have been left in the AST.
101 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
102 continue;
103 bool AcceptsConst = true;
104 QualType ArgType = FnType->getArgType(0);
105 if (const ReferenceType *Ref = ArgType->getAsReferenceType()) {
106 ArgType = Ref->getPointeeType();
107 // Is it a non-const reference?
108 if (!ArgType.isConstQualified())
109 AcceptsConst = false;
110 }
111 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
112 continue;
113
114 // We have a single argument of type cv X or cv X&, i.e. we've found the
115 // copy assignment operator. Return whether it accepts const arguments.
116 return AcceptsConst;
117 }
118 assert(isInvalidDecl() &&
119 "No copy assignment operator declared in valid code.");
120 return false;
121}
122
123void
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000124CXXRecordDecl::addedConstructor(ASTContext &Context,
125 CXXConstructorDecl *ConDecl) {
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000126 if (!ConDecl->isImplicit()) {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000127 // Note that we have a user-declared constructor.
128 UserDeclaredConstructor = true;
129
Douglas Gregor64bffa92008-11-05 16:20:31 +0000130 // C++ [dcl.init.aggr]p1:
131 // An aggregate is an array or a class (clause 9) with no
132 // user-declared constructors (12.1) [...].
133 Aggregate = false;
134
Sebastian Redl64b45f72009-01-05 20:52:13 +0000135 // C++ [class]p4:
136 // A POD-struct is an aggregate class [...]
137 PlainOldData = false;
138
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000139 // Note when we have a user-declared copy constructor, which will
140 // suppress the implicit declaration of a copy constructor.
141 if (ConDecl->isCopyConstructor(Context))
142 UserDeclaredCopyConstructor = true;
143 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000144}
145
Sebastian Redl64b45f72009-01-05 20:52:13 +0000146void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
147 CXXMethodDecl *OpDecl) {
148 // We're interested specifically in copy assignment operators.
149 // Unlike addedConstructor, this method is not called for implicit
150 // declarations.
Douglas Gregor72564e72009-02-26 23:50:07 +0000151 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000152 assert(FnType && "Overloaded operator has no proto function type.");
153 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
154 QualType ArgType = FnType->getArgType(0);
155 if (const ReferenceType *Ref = ArgType->getAsReferenceType())
156 ArgType = Ref->getPointeeType();
157
158 ArgType = ArgType.getUnqualifiedType();
159 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
160 const_cast<CXXRecordDecl*>(this)));
161
162 if (ClassType != Context.getCanonicalType(ArgType))
163 return;
164
165 // This is a copy assignment operator.
166 // Suppress the implicit declaration of a copy constructor.
167 UserDeclaredCopyAssignment = true;
168
169 // C++ [class]p4:
170 // A POD-struct is an aggregate class that [...] has no user-defined copy
171 // assignment operator [...].
172 PlainOldData = false;
173}
174
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000175void CXXRecordDecl::addConversionFunction(ASTContext &Context,
176 CXXConversionDecl *ConvDecl) {
177 Conversions.addOverload(ConvDecl);
178}
179
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000180CXXMethodDecl *
181CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000182 SourceLocation L, DeclarationName N,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000183 QualType T, bool isStatic, bool isInline) {
Steve Naroff3e970492009-01-27 21:25:57 +0000184 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000185}
186
187QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000188 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
189 // If the member function is declared const, the type of this is const X*,
190 // if the member function is declared volatile, the type of this is
191 // volatile X*, and if the member function is declared const volatile,
192 // the type of this is const volatile X*.
193
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000194 assert(isInstance() && "No 'this' for static methods!");
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000195 QualType ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000196 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
197 return C.getPointerType(ClassTy).withConst();
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000198}
199
Douglas Gregor7ad83902008-11-05 04:29:56 +0000200CXXBaseOrMemberInitializer::
201CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs)
202 : Args(0), NumArgs(0) {
203 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
204 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
205 BaseOrMember |= 0x01;
206
207 if (NumArgs > 0) {
208 this->NumArgs = NumArgs;
209 this->Args = new Expr*[NumArgs];
210 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
211 this->Args[Idx] = Args[Idx];
212 }
213}
214
215CXXBaseOrMemberInitializer::
Douglas Gregor44b43212008-12-11 16:49:14 +0000216CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs)
Douglas Gregor7ad83902008-11-05 04:29:56 +0000217 : Args(0), NumArgs(0) {
218 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
219 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
220
221 if (NumArgs > 0) {
222 this->NumArgs = NumArgs;
223 this->Args = new Expr*[NumArgs];
224 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
225 this->Args[Idx] = Args[Idx];
226 }
227}
228
229CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
230 delete [] Args;
231}
232
Douglas Gregorb48fe382008-10-31 09:07:45 +0000233CXXConstructorDecl *
234CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000235 SourceLocation L, DeclarationName N,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000236 QualType T, bool isExplicit,
237 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000238 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
239 "Name must refer to a constructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000240 return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000241 isImplicitlyDeclared);
242}
243
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000244bool CXXConstructorDecl::isDefaultConstructor() const {
245 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000246 // A default constructor for a class X is a constructor of class
247 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000248 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000249 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000250}
251
252bool
253CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
254 unsigned &TypeQuals) const {
255 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000256 // A non-template constructor for class X is a copy constructor
257 // if its first parameter is of type X&, const X&, volatile X& or
258 // const volatile X&, and either there are no other parameters
259 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000260 if ((getNumParams() < 1) ||
261 (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() == 0))
262 return false;
263
264 const ParmVarDecl *Param = getParamDecl(0);
265
266 // Do we have a reference type?
267 const ReferenceType *ParamRefType = Param->getType()->getAsReferenceType();
268 if (!ParamRefType)
269 return false;
270
271 // Is it a reference to our class type?
272 QualType PointeeType
273 = Context.getCanonicalType(ParamRefType->getPointeeType());
274 QualType ClassTy
275 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
276 if (PointeeType.getUnqualifiedType() != ClassTy)
277 return false;
278
279 // We have a copy constructor.
280 TypeQuals = PointeeType.getCVRQualifiers();
281 return true;
282}
283
Douglas Gregor60d62c22008-10-31 16:23:19 +0000284bool CXXConstructorDecl::isConvertingConstructor() const {
285 // C++ [class.conv.ctor]p1:
286 // A constructor declared without the function-specifier explicit
287 // that can be called with a single parameter specifies a
288 // conversion from the type of its first parameter to the type of
289 // its class. Such a constructor is called a converting
290 // constructor.
291 if (isExplicit())
292 return false;
293
294 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000295 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000296 (getNumParams() == 1) ||
297 (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() != 0);
298}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000299
Douglas Gregor42a552f2008-11-05 20:51:48 +0000300CXXDestructorDecl *
301CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000302 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000303 QualType T, bool isInline,
304 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000305 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
306 "Name must refer to a destructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000307 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
308 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000309}
310
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000311CXXConversionDecl *
312CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000313 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000314 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000315 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
316 "Name must refer to a conversion function");
Steve Naroff3e970492009-01-27 21:25:57 +0000317 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000318}
319
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000320OverloadedFunctionDecl *
321OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000322 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000323 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000324}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000325
326LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000327 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000328 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000329 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000330 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000331}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000332
333UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
334 SourceLocation L,
335 SourceLocation NamespaceLoc,
336 SourceLocation IdentLoc,
337 NamespaceDecl *Used,
338 DeclContext *CommonAncestor) {
339 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, IdentLoc,
340 Used, CommonAncestor);
341}
342
Anders Carlssonfb311762009-03-14 00:25:26 +0000343StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
344 SourceLocation L, Expr *AssertExpr,
345 StringLiteral *Message) {
346 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
347}
348
349void StaticAssertDecl::Destroy(ASTContext& C) {
350 AssertExpr->Destroy(C);
351 Message->Destroy(C);
352 this->~StaticAssertDecl();
353 C.Deallocate((void *)this);
354}
355
356StaticAssertDecl::~StaticAssertDecl() {
357}
358