blob: 78ed3fcf4d75add02ef5985cac6c16d519313a28 [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"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000017#include "clang/AST/Expr.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000018#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000019#include "llvm/ADT/STLExtras.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// Decl Allocation/Deallocation Method Implementations
24//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000025
Douglas Gregor3e00bad2009-02-17 01:05:43 +000026CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Douglas Gregor7d7e6722008-11-12 23:21:09 +000027 SourceLocation L, IdentifierInfo *Id)
Douglas Gregor3e00bad2009-02-17 01:05:43 +000028 : RecordDecl(K, TK, DC, L, Id),
Douglas Gregor7d7e6722008-11-12 23:21:09 +000029 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000030 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Anders Carlsson67e4dd22009-03-22 01:52:17 +000031 Aggregate(true), PlainOldData(true), Polymorphic(false), Abstract(false),
Anders Carlsson347ba892009-04-16 00:08:20 +000032 HasTrivialConstructor(true),
Douglas Gregord475b8d2009-03-25 21:17:03 +000033 Bases(0), NumBases(0), Conversions(DC, DeclarationName()),
34 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000035
Ted Kremenek4b7c9832008-09-05 17:16:31 +000036CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
37 SourceLocation L, IdentifierInfo *Id,
38 CXXRecordDecl* PrevDecl) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +000039 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000040 C.getTypeDeclType(R, PrevDecl);
41 return R;
42}
43
Douglas Gregorf8268ae2008-10-22 17:49:05 +000044CXXRecordDecl::~CXXRecordDecl() {
Douglas Gregorf8268ae2008-10-22 17:49:05 +000045 delete [] Bases;
46}
47
Douglas Gregor57c856b2008-10-23 18:13:27 +000048void
49CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
50 unsigned NumBases) {
Douglas Gregor64bffa92008-11-05 16:20:31 +000051 // C++ [dcl.init.aggr]p1:
52 // An aggregate is an array or a class (clause 9) with [...]
53 // no base classes [...].
54 Aggregate = false;
55
Douglas Gregor57c856b2008-10-23 18:13:27 +000056 if (this->Bases)
57 delete [] this->Bases;
58
Douglas Gregor2943aed2009-03-03 04:44:36 +000059 // FIXME: allocate using the ASTContext
Douglas Gregor57c856b2008-10-23 18:13:27 +000060 this->Bases = new CXXBaseSpecifier[NumBases];
61 this->NumBases = NumBases;
62 for (unsigned i = 0; i < NumBases; ++i)
63 this->Bases[i] = *Bases[i];
64}
65
Douglas Gregor396b7cd2008-11-03 17:51:48 +000066bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +000067 QualType ClassType
68 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Douglas Gregor9e7d9de2008-12-15 21:24:18 +000069 DeclarationName ConstructorName
70 = Context.DeclarationNames.getCXXConstructorName(
71 Context.getCanonicalType(ClassType));
72 unsigned TypeQuals;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000073 DeclContext::lookup_const_iterator Con, ConEnd;
Douglas Gregor6ab35242009-04-09 21:40:53 +000074 for (llvm::tie(Con, ConEnd) = this->lookup(Context, ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000075 Con != ConEnd; ++Con) {
Douglas Gregor396b7cd2008-11-03 17:51:48 +000076 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context, TypeQuals) &&
Eli Friedmane8e32052008-12-16 20:06:41 +000077 (TypeQuals & QualType::Const) != 0)
Douglas Gregor396b7cd2008-11-03 17:51:48 +000078 return true;
79 }
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000080
Douglas Gregor396b7cd2008-11-03 17:51:48 +000081 return false;
82}
83
Sebastian Redl64b45f72009-01-05 20:52:13 +000084bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context) const {
85 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
86 const_cast<CXXRecordDecl*>(this)));
87 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
88
89 DeclContext::lookup_const_iterator Op, OpEnd;
Douglas Gregor6ab35242009-04-09 21:40:53 +000090 for (llvm::tie(Op, OpEnd) = this->lookup(Context, OpName);
Sebastian Redl64b45f72009-01-05 20:52:13 +000091 Op != OpEnd; ++Op) {
92 // C++ [class.copy]p9:
93 // A user-declared copy assignment operator is a non-static non-template
94 // member function of class X with exactly one parameter of type X, X&,
95 // const X&, volatile X& or const volatile X&.
96 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op);
97 if (Method->isStatic())
98 continue;
99 // TODO: Skip templates? Or is this implicitly done due to parameter types?
Douglas Gregor72564e72009-02-26 23:50:07 +0000100 const FunctionProtoType *FnType =
101 Method->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000102 assert(FnType && "Overloaded operator has no prototype.");
103 // Don't assert on this; an invalid decl might have been left in the AST.
104 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
105 continue;
106 bool AcceptsConst = true;
107 QualType ArgType = FnType->getArgType(0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000108 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000109 ArgType = Ref->getPointeeType();
Douglas Gregor2ff44782009-03-20 20:21:37 +0000110 // Is it a non-const lvalue reference?
Sebastian Redl64b45f72009-01-05 20:52:13 +0000111 if (!ArgType.isConstQualified())
112 AcceptsConst = false;
113 }
114 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType)
115 continue;
116
117 // We have a single argument of type cv X or cv X&, i.e. we've found the
118 // copy assignment operator. Return whether it accepts const arguments.
119 return AcceptsConst;
120 }
121 assert(isInvalidDecl() &&
122 "No copy assignment operator declared in valid code.");
123 return false;
124}
125
126void
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000127CXXRecordDecl::addedConstructor(ASTContext &Context,
128 CXXConstructorDecl *ConDecl) {
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000129 if (!ConDecl->isImplicit()) {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000130 // Note that we have a user-declared constructor.
131 UserDeclaredConstructor = true;
132
Douglas Gregor64bffa92008-11-05 16:20:31 +0000133 // C++ [dcl.init.aggr]p1:
134 // An aggregate is an array or a class (clause 9) with no
135 // user-declared constructors (12.1) [...].
136 Aggregate = false;
137
Sebastian Redl64b45f72009-01-05 20:52:13 +0000138 // C++ [class]p4:
139 // A POD-struct is an aggregate class [...]
140 PlainOldData = false;
141
Anders Carlsson347ba892009-04-16 00:08:20 +0000142 // C++ [class.ctor]p5:
143 // A constructor is trivial if it is an implicitly-declared default
144 // constructor.
145 HasTrivialConstructor = false;
146
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000147 // Note when we have a user-declared copy constructor, which will
148 // suppress the implicit declaration of a copy constructor.
149 if (ConDecl->isCopyConstructor(Context))
150 UserDeclaredCopyConstructor = true;
151 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000152}
153
Sebastian Redl64b45f72009-01-05 20:52:13 +0000154void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
155 CXXMethodDecl *OpDecl) {
156 // We're interested specifically in copy assignment operators.
157 // Unlike addedConstructor, this method is not called for implicit
158 // declarations.
Douglas Gregor72564e72009-02-26 23:50:07 +0000159 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000160 assert(FnType && "Overloaded operator has no proto function type.");
161 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
162 QualType ArgType = FnType->getArgType(0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000163 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000164 ArgType = Ref->getPointeeType();
165
166 ArgType = ArgType.getUnqualifiedType();
167 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
168 const_cast<CXXRecordDecl*>(this)));
169
170 if (ClassType != Context.getCanonicalType(ArgType))
171 return;
172
173 // This is a copy assignment operator.
174 // Suppress the implicit declaration of a copy constructor.
175 UserDeclaredCopyAssignment = true;
176
177 // C++ [class]p4:
178 // A POD-struct is an aggregate class that [...] has no user-defined copy
179 // assignment operator [...].
180 PlainOldData = false;
181}
182
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000183void CXXRecordDecl::addConversionFunction(ASTContext &Context,
184 CXXConversionDecl *ConvDecl) {
185 Conversions.addOverload(ConvDecl);
186}
187
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000188CXXMethodDecl *
189CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor10bd3682008-11-17 22:58:34 +0000190 SourceLocation L, DeclarationName N,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000191 QualType T, bool isStatic, bool isInline) {
Steve Naroff3e970492009-01-27 21:25:57 +0000192 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000193}
194
195QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000196 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
197 // If the member function is declared const, the type of this is const X*,
198 // if the member function is declared volatile, the type of this is
199 // volatile X*, and if the member function is declared const volatile,
200 // the type of this is const volatile X*.
201
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000202 assert(isInstance() && "No 'this' for static methods!");
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000203 QualType ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000204 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
205 return C.getPointerType(ClassTy).withConst();
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000206}
207
Douglas Gregor7ad83902008-11-05 04:29:56 +0000208CXXBaseOrMemberInitializer::
209CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs)
210 : Args(0), NumArgs(0) {
211 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
212 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
213 BaseOrMember |= 0x01;
214
215 if (NumArgs > 0) {
216 this->NumArgs = NumArgs;
217 this->Args = new Expr*[NumArgs];
218 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
219 this->Args[Idx] = Args[Idx];
220 }
221}
222
223CXXBaseOrMemberInitializer::
Douglas Gregor44b43212008-12-11 16:49:14 +0000224CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs)
Douglas Gregor7ad83902008-11-05 04:29:56 +0000225 : Args(0), NumArgs(0) {
226 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
227 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
228
229 if (NumArgs > 0) {
230 this->NumArgs = NumArgs;
231 this->Args = new Expr*[NumArgs];
232 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
233 this->Args[Idx] = Args[Idx];
234 }
235}
236
237CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
238 delete [] Args;
239}
240
Douglas Gregorb48fe382008-10-31 09:07:45 +0000241CXXConstructorDecl *
242CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000243 SourceLocation L, DeclarationName N,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000244 QualType T, bool isExplicit,
245 bool isInline, bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000246 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
247 "Name must refer to a constructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000248 return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline,
Douglas Gregorb48fe382008-10-31 09:07:45 +0000249 isImplicitlyDeclared);
250}
251
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000252bool CXXConstructorDecl::isDefaultConstructor() const {
253 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000254 // A default constructor for a class X is a constructor of class
255 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000256 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000257 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000258}
259
260bool
261CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
262 unsigned &TypeQuals) const {
263 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000264 // A non-template constructor for class X is a copy constructor
265 // if its first parameter is of type X&, const X&, volatile X& or
266 // const volatile X&, and either there are no other parameters
267 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000268 if ((getNumParams() < 1) ||
269 (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() == 0))
270 return false;
271
272 const ParmVarDecl *Param = getParamDecl(0);
273
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000274 // Do we have a reference type? Rvalue references don't count.
275 const LValueReferenceType *ParamRefType =
276 Param->getType()->getAsLValueReferenceType();
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000277 if (!ParamRefType)
278 return false;
279
280 // Is it a reference to our class type?
281 QualType PointeeType
282 = Context.getCanonicalType(ParamRefType->getPointeeType());
283 QualType ClassTy
284 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
285 if (PointeeType.getUnqualifiedType() != ClassTy)
286 return false;
287
288 // We have a copy constructor.
289 TypeQuals = PointeeType.getCVRQualifiers();
290 return true;
291}
292
Douglas Gregor60d62c22008-10-31 16:23:19 +0000293bool CXXConstructorDecl::isConvertingConstructor() const {
294 // C++ [class.conv.ctor]p1:
295 // A constructor declared without the function-specifier explicit
296 // that can be called with a single parameter specifies a
297 // conversion from the type of its first parameter to the type of
298 // its class. Such a constructor is called a converting
299 // constructor.
300 if (isExplicit())
301 return false;
302
303 return (getNumParams() == 0 &&
Douglas Gregor72564e72009-02-26 23:50:07 +0000304 getType()->getAsFunctionProtoType()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000305 (getNumParams() == 1) ||
306 (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() != 0);
307}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000308
Douglas Gregor42a552f2008-11-05 20:51:48 +0000309CXXDestructorDecl *
310CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000311 SourceLocation L, DeclarationName N,
Douglas Gregor42a552f2008-11-05 20:51:48 +0000312 QualType T, bool isInline,
313 bool isImplicitlyDeclared) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000314 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
315 "Name must refer to a destructor");
Steve Naroff3e970492009-01-27 21:25:57 +0000316 return new (C) CXXDestructorDecl(RD, L, N, T, isInline,
317 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000318}
319
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000320CXXConversionDecl *
321CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000322 SourceLocation L, DeclarationName N,
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000323 QualType T, bool isInline, bool isExplicit) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000324 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
325 "Name must refer to a conversion function");
Steve Naroff3e970492009-01-27 21:25:57 +0000326 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000327}
328
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000329OverloadedFunctionDecl *
330OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000331 DeclarationName N) {
Steve Naroff3e970492009-01-27 21:25:57 +0000332 return new (C) OverloadedFunctionDecl(DC, N);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000333}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000334
335LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Douglas Gregor074149e2009-01-05 19:45:36 +0000336 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000337 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +0000338 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +0000339 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +0000340}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000341
342UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
343 SourceLocation L,
344 SourceLocation NamespaceLoc,
345 SourceLocation IdentLoc,
346 NamespaceDecl *Used,
347 DeclContext *CommonAncestor) {
348 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, IdentLoc,
349 Used, CommonAncestor);
350}
351
Anders Carlsson68771c72009-03-28 22:58:02 +0000352NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
353 SourceLocation L,
354 SourceLocation AliasLoc,
355 IdentifierInfo *Alias,
356 SourceLocation IdentLoc,
357 NamedDecl *Namespace) {
358 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, IdentLoc,
359 Namespace);
360}
361
Anders Carlssonfb311762009-03-14 00:25:26 +0000362StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
363 SourceLocation L, Expr *AssertExpr,
364 StringLiteral *Message) {
365 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
366}
367
368void StaticAssertDecl::Destroy(ASTContext& C) {
369 AssertExpr->Destroy(C);
370 Message->Destroy(C);
371 this->~StaticAssertDecl();
372 C.Deallocate((void *)this);
373}
374
375StaticAssertDecl::~StaticAssertDecl() {
376}
377
Anders Carlsson05bf2c72009-03-26 23:46:50 +0000378static const char *getAccessName(AccessSpecifier AS) {
379 switch (AS) {
380 default:
381 case AS_none:
382 assert("Invalid access specifier!");
383 return 0;
384 case AS_public:
385 return "public";
386 case AS_private:
387 return "private";
388 case AS_protected:
389 return "protected";
390 }
391}
392
393const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
394 AccessSpecifier AS) {
395 return DB << getAccessName(AS);
396}
397
398