blob: 45a3372ada682ae5a9e32d0633a81d3cf08a7046 [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
Douglas Gregor3e00bad2009-02-17 01:05:43 +000024CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Douglas Gregor7d7e6722008-11-12 23:21:09 +000025 SourceLocation L, IdentifierInfo *Id)
Douglas Gregor3e00bad2009-02-17 01:05:43 +000026 : RecordDecl(K, TK, DC, L, Id),
Douglas Gregor7d7e6722008-11-12 23:21:09 +000027 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000028 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
29 Aggregate(true), PlainOldData(true), Polymorphic(false), Bases(0),
30 NumBases(0), Conversions(DC, DeclarationName()) { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000031
Ted Kremenek4b7c9832008-09-05 17:16:31 +000032CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
33 SourceLocation L, IdentifierInfo *Id,
34 CXXRecordDecl* PrevDecl) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +000035 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000036 C.getTypeDeclType(R, PrevDecl);
37 return R;
38}
39
Douglas Gregorf8268ae2008-10-22 17:49:05 +000040CXXRecordDecl::~CXXRecordDecl() {
Douglas Gregorf8268ae2008-10-22 17:49:05 +000041 delete [] Bases;
42}
43
Douglas Gregor57c856b2008-10-23 18:13:27 +000044void
45CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
46 unsigned NumBases) {
Douglas Gregor64bffa92008-11-05 16:20:31 +000047 // C++ [dcl.init.aggr]p1:
48 // An aggregate is an array or a class (clause 9) with [...]
49 // no base classes [...].
50 Aggregate = false;
51
Douglas Gregor57c856b2008-10-23 18:13:27 +000052 if (this->Bases)
53 delete [] this->Bases;
54
Douglas Gregor2943aed2009-03-03 04:44:36 +000055 // FIXME: allocate using the ASTContext
Douglas Gregor57c856b2008-10-23 18:13:27 +000056 this->Bases = new CXXBaseSpecifier[NumBases];
57 this->NumBases = NumBases;
58 for (unsigned i = 0; i < NumBases; ++i)
59 this->Bases[i] = *Bases[i];
60}
61
Douglas Gregor396b7cd2008-11-03 17:51:48 +000062bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +000063 QualType ClassType
64 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Douglas Gregor9e7d9de2008-12-15 21:24:18 +000065 DeclarationName ConstructorName
66 = Context.DeclarationNames.getCXXConstructorName(
67 Context.getCanonicalType(ClassType));
68 unsigned TypeQuals;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000069 DeclContext::lookup_const_iterator Con, ConEnd;
Steve Naroff0701bbb2009-01-08 17:28:14 +000070 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000071 Con != ConEnd; ++Con) {
Douglas Gregor396b7cd2008-11-03 17:51:48 +000072 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context, TypeQuals) &&
Eli Friedmane8e32052008-12-16 20:06:41 +000073 (TypeQuals & QualType::Const) != 0)
Douglas Gregor396b7cd2008-11-03 17:51:48 +000074 return true;
75 }
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000076
Douglas Gregor396b7cd2008-11-03 17:51:48 +000077 return false;
78}
79
Sebastian Redl64b45f72009-01-05 20:52:13 +000080bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context) const {
81 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
82 const_cast<CXXRecordDecl*>(this)));
83 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
84
85 DeclContext::lookup_const_iterator Op, OpEnd;
Steve Naroff0701bbb2009-01-08 17:28:14 +000086 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +000087 Op != OpEnd; ++Op) {
88 // C++ [class.copy]p9:
89 // A user-declared copy assignment operator is a non-static non-template
90 // member function of class X with exactly one parameter of type X, X&,
91 // const X&, volatile X& or const volatile X&.
92 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
93 if (Method->isStatic())
94 continue;
95 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor72564e72009-02-26 23:50:07 +000096 const FunctionProtoType *FnType =
97 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +000098 assert(FnType && "Overloaded operator has no prototype.");
99 // Don't assert on this; an invalid decl might have been left in the AST.
100 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
101 continue;
102 bool AcceptsConst = true;
103 QualType ArgType = FnType->getArgType(0);
104 if (const ReferenceType *Ref = ArgType->getAsReferenceType()) {
105 ArgType = Ref->getPointeeType();
106 // Is it a non-const reference?
107 if (!ArgType.isConstQualified())
108 AcceptsConst = false;
109 }
110 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
111 continue;
112
113 // We have a single argument of type cv X or cv X&, i.e. we've found the
114 // copy assignment operator. Return whether it accepts const arguments.
115 return AcceptsConst;
116 }
117 assert(isInvalidDecl() &&
118 "No copy assignment operator declared in valid code.");
119 return false;
120}
121
122void
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000123CXXRecordDecl::addedConstructor(ASTContext &Context,
124 CXXConstructorDecl *ConDecl) {
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000125 if (!ConDecl->isImplicit()) {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000126 // Note that we have a user-declared constructor.
127 UserDeclaredConstructor = true;
128
Douglas Gregor64bffa92008-11-05 16:20:31 +0000129 // C++ [dcl.init.aggr]p1:
130 // An aggregate is an array or a class (clause 9) with no
131 // user-declared constructors (12.1) [...].
132 Aggregate = false;
133
Sebastian Redl64b45f72009-01-05 20:52:13 +0000134 // C++ [class]p4:
135 // A POD-struct is an aggregate class [...]
136 PlainOldData = false;
137
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000138 // Note when we have a user-declared copy constructor, which will
139 // suppress the implicit declaration of a copy constructor.
140 if (ConDecl->isCopyConstructor(Context))
141 UserDeclaredCopyConstructor = true;
142 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000143}
144
Sebastian Redl64b45f72009-01-05 20:52:13 +0000145void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
146 CXXMethodDecl *OpDecl) {
147 // We're interested specifically in copy assignment operators.
148 // Unlike addedConstructor, this method is not called for implicit
149 // declarations.
Douglas Gregor72564e72009-02-26 23:50:07 +0000150 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000151 assert(FnType && "Overloaded operator has no proto function type.");
152 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
153 QualType ArgType = FnType->getArgType(0);
154 if (const ReferenceType *Ref = ArgType->getAsReferenceType())
155 ArgType = Ref->getPointeeType();
156
157 ArgType = ArgType.getUnqualifiedType();
158 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
159 const_cast<CXXRecordDecl*>(this)));
160
161 if (ClassType != Context.getCanonicalType(ArgType))
162 return;
163
164 // This is a copy assignment operator.
165 // Suppress the implicit declaration of a copy constructor.
166 UserDeclaredCopyAssignment = true;
167
168 // C++ [class]p4:
169 // A POD-struct is an aggregate class that [...] has no user-defined copy
170 // assignment operator [...].
171 PlainOldData = false;
172}
173
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000174void CXXRecordDecl::addConversionFunction(ASTContext &Context,
175 CXXConversionDecl *ConvDecl) {
176 Conversions.addOverload(ConvDecl);
177}
178
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000179CXXMethodDecl *
180CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000181 SourceLocation L, DeclarationName N,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000182 QualType T, bool isStatic, bool isInline) {
Steve Naroff3e970492009-01-27 21:25:57 +0000183 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000184}
185
186QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000187 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
188 // If the member function is declared const, the type of this is const X*,
189 // if the member function is declared volatile, the type of this is
190 // volatile X*, and if the member function is declared const volatile,
191 // the type of this is const volatile X*.
192
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000193 assert(isInstance() && "No 'this' for static methods!");
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000194 QualType ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000195 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
196 return C.getPointerType(ClassTy).withConst();
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000197}
198
Douglas Gregor7ad83902008-11-05 04:29:56 +0000199CXXBaseOrMemberInitializer::
200CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs)
201 : Args(0), NumArgs(0) {
202 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
203 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
204 BaseOrMember |= 0x01;
205
206 if (NumArgs > 0) {
207 this->NumArgs = NumArgs;
208 this->Args = new Expr*[NumArgs];
209 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
210 this->Args[Idx] = Args[Idx];
211 }
212}
213
214CXXBaseOrMemberInitializer::
Douglas Gregor44b43212008-12-11 16:49:14 +0000215CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs)
Douglas Gregor7ad83902008-11-05 04:29:56 +0000216 : Args(0), NumArgs(0) {
217 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
218 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
219
220 if (NumArgs > 0) {
221 this->NumArgs = NumArgs;
222 this->Args = new Expr*[NumArgs];
223 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
224 this->Args[Idx] = Args[Idx];
225 }
226}
227
228CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
229 delete [] Args;
230}
231
Douglas Gregorb48fe382008-10-31 09:07:45 +0000232CXXConstructorDecl *
233CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000234 SourceLocation L, DeclarationName N,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000235 QualType T, bool isExplicit,
236 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000237 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
238 "Name must refer to a constructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000239 return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000240 isImplicitlyDeclared);
241}
242
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000243bool CXXConstructorDecl::isDefaultConstructor() const {
244 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000245 // A default constructor for a class X is a constructor of class
246 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000247 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000248 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000249}
250
251bool
252CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
253 unsigned &TypeQuals) const {
254 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000255 // A non-template constructor for class X is a copy constructor
256 // if its first parameter is of type X&, const X&, volatile X& or
257 // const volatile X&, and either there are no other parameters
258 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000259 if ((getNumParams() < 1) ||
260 (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() == 0))
261 return false;
262
263 const ParmVarDecl *Param = getParamDecl(0);
264
265 // Do we have a reference type?
266 const ReferenceType *ParamRefType = Param->getType()->getAsReferenceType();
267 if (!ParamRefType)
268 return false;
269
270 // Is it a reference to our class type?
271 QualType PointeeType
272 = Context.getCanonicalType(ParamRefType->getPointeeType());
273 QualType ClassTy
274 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
275 if (PointeeType.getUnqualifiedType() != ClassTy)
276 return false;
277
278 // We have a copy constructor.
279 TypeQuals = PointeeType.getCVRQualifiers();
280 return true;
281}
282
Douglas Gregor60d62c22008-10-31 16:23:19 +0000283bool CXXConstructorDecl::isConvertingConstructor() const {
284 // C++ [class.conv.ctor]p1:
285 // A constructor declared without the function-specifier explicit
286 // that can be called with a single parameter specifies a
287 // conversion from the type of its first parameter to the type of
288 // its class. Such a constructor is called a converting
289 // constructor.
290 if (isExplicit())
291 return false;
292
293 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000294 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000295 (getNumParams() == 1) ||
296 (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() != 0);
297}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000298
Douglas Gregor42a552f2008-11-05 20:51:48 +0000299CXXDestructorDecl *
300CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000301 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000302 QualType T, bool isInline,
303 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000304 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
305 "Name must refer to a destructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000306 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
307 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000308}
309
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000310CXXConversionDecl *
311CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000312 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000313 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000314 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
315 "Name must refer to a conversion function");
Steve Naroff3e970492009-01-27 21:25:57 +0000316 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000317}
318
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000319CXXClassVarDecl *CXXClassVarDecl::Create(ASTContext &C, CXXRecordDecl *RD,
320 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000321 QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +0000322 return new (C) CXXClassVarDecl(RD, L, Id, T);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000323}
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000324
325OverloadedFunctionDecl *
326OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000327 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000328 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000329}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000330
331LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000332 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000333 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000334 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000335 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000336}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000337
338UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
339 SourceLocation L,
340 SourceLocation NamespaceLoc,
341 SourceLocation IdentLoc,
342 NamespaceDecl *Used,
343 DeclContext *CommonAncestor) {
344 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, IdentLoc,
345 Used, CommonAncestor);
346}
347