blob: cf59d1af3608d90dba2954bc1eabc6639cf60f56 [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"
16using namespace clang;
17
18//===----------------------------------------------------------------------===//
19// Decl Allocation/Deallocation Method Implementations
20//===----------------------------------------------------------------------===//
21
22CXXFieldDecl *CXXFieldDecl::Create(ASTContext &C, CXXRecordDecl *RD,
23 SourceLocation L, IdentifierInfo *Id,
24 QualType T, Expr *BW) {
25 void *Mem = C.getAllocator().Allocate<CXXFieldDecl>();
26 return new (Mem) CXXFieldDecl(RD, L, Id, T, BW);
27}
28
29CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
30 SourceLocation L, IdentifierInfo *Id,
31 CXXRecordDecl* PrevDecl) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +000032 void *Mem = C.getAllocator().Allocate<CXXRecordDecl>();
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +000033 CXXRecordDecl* R = new (Mem) CXXRecordDecl(TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000034 C.getTypeDeclType(R, PrevDecl);
35 return R;
36}
37
Douglas Gregorf8268ae2008-10-22 17:49:05 +000038CXXRecordDecl::~CXXRecordDecl() {
Douglas Gregorf8268ae2008-10-22 17:49:05 +000039 delete [] Bases;
40}
41
Douglas Gregorb48fe382008-10-31 09:07:45 +000042void CXXRecordDecl::Destroy(ASTContext &C) {
43 for (OverloadedFunctionDecl::function_iterator func
44 = Constructors.function_begin();
45 func != Constructors.function_end(); ++func)
46 (*func)->Destroy(C);
47 RecordDecl::Destroy(C);
48}
49
Douglas Gregor57c856b2008-10-23 18:13:27 +000050void
51CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
52 unsigned NumBases) {
Douglas Gregor64bffa92008-11-05 16:20:31 +000053 // C++ [dcl.init.aggr]p1:
54 // An aggregate is an array or a class (clause 9) with [...]
55 // no base classes [...].
56 Aggregate = false;
57
Douglas Gregor57c856b2008-10-23 18:13:27 +000058 if (this->Bases)
59 delete [] this->Bases;
60
61 this->Bases = new CXXBaseSpecifier[NumBases];
62 this->NumBases = NumBases;
63 for (unsigned i = 0; i < NumBases; ++i)
64 this->Bases[i] = *Bases[i];
65}
66
Douglas Gregor396b7cd2008-11-03 17:51:48 +000067bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
68 for (OverloadedFunctionDecl::function_const_iterator Con
69 = Constructors.function_begin();
70 Con != Constructors.function_end(); ++Con) {
71 unsigned TypeQuals;
72 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context, TypeQuals) &&
73 (TypeQuals & QualType::Const != 0))
74 return true;
75 }
76 return false;
77}
78
Douglas Gregor030ff0c2008-10-31 20:25:05 +000079void
80CXXRecordDecl::addConstructor(ASTContext &Context,
81 CXXConstructorDecl *ConDecl) {
82 if (!ConDecl->isImplicitlyDeclared()) {
83 // Note that we have a user-declared constructor.
84 UserDeclaredConstructor = true;
85
Douglas Gregor64bffa92008-11-05 16:20:31 +000086 // C++ [dcl.init.aggr]p1:
87 // An aggregate is an array or a class (clause 9) with no
88 // user-declared constructors (12.1) [...].
89 Aggregate = false;
90
Douglas Gregor030ff0c2008-10-31 20:25:05 +000091 // Note when we have a user-declared copy constructor, which will
92 // suppress the implicit declaration of a copy constructor.
93 if (ConDecl->isCopyConstructor(Context))
94 UserDeclaredCopyConstructor = true;
95 }
96
97 Constructors.addOverload(ConDecl);
98}
99
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000100CXXMethodDecl *
101CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
102 SourceLocation L, IdentifierInfo *Id,
103 QualType T, bool isStatic, bool isInline,
104 ScopedDecl *PrevDecl) {
105 void *Mem = C.getAllocator().Allocate<CXXMethodDecl>();
106 return new (Mem) CXXMethodDecl(RD, L, Id, T, isStatic, isInline, PrevDecl);
107}
108
109QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000110 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
111 // If the member function is declared const, the type of this is const X*,
112 // if the member function is declared volatile, the type of this is
113 // volatile X*, and if the member function is declared const volatile,
114 // the type of this is const volatile X*.
115
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000116 assert(isInstance() && "No 'this' for static methods!");
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000117 QualType ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000118 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers());
119 return C.getPointerType(ClassTy).withConst();
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000120}
121
Douglas Gregor7ad83902008-11-05 04:29:56 +0000122CXXBaseOrMemberInitializer::
123CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs)
124 : Args(0), NumArgs(0) {
125 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
126 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
127 BaseOrMember |= 0x01;
128
129 if (NumArgs > 0) {
130 this->NumArgs = NumArgs;
131 this->Args = new Expr*[NumArgs];
132 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
133 this->Args[Idx] = Args[Idx];
134 }
135}
136
137CXXBaseOrMemberInitializer::
138CXXBaseOrMemberInitializer(CXXFieldDecl *Member, Expr **Args, unsigned NumArgs)
139 : Args(0), NumArgs(0) {
140 BaseOrMember = reinterpret_cast<uintptr_t>(Member);
141 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
142
143 if (NumArgs > 0) {
144 this->NumArgs = NumArgs;
145 this->Args = new Expr*[NumArgs];
146 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
147 this->Args[Idx] = Args[Idx];
148 }
149}
150
151CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
152 delete [] Args;
153}
154
Douglas Gregorb48fe382008-10-31 09:07:45 +0000155CXXConstructorDecl *
156CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
157 SourceLocation L, IdentifierInfo *Id,
158 QualType T, bool isExplicit,
159 bool isInline, bool isImplicitlyDeclared) {
160 void *Mem = C.getAllocator().Allocate<CXXConstructorDecl>();
161 return new (Mem) CXXConstructorDecl(RD, L, Id, T, isExplicit, isInline,
162 isImplicitlyDeclared);
163}
164
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000165bool CXXConstructorDecl::isDefaultConstructor() const {
166 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000167 // A default constructor for a class X is a constructor of class
168 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000169 return (getNumParams() == 0) ||
Douglas Gregorf03d7c72008-11-05 15:29:30 +0000170 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0);
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000171}
172
173bool
174CXXConstructorDecl::isCopyConstructor(ASTContext &Context,
175 unsigned &TypeQuals) const {
176 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000177 // A non-template constructor for class X is a copy constructor
178 // if its first parameter is of type X&, const X&, volatile X& or
179 // const volatile X&, and either there are no other parameters
180 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000181 if ((getNumParams() < 1) ||
182 (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() == 0))
183 return false;
184
185 const ParmVarDecl *Param = getParamDecl(0);
186
187 // Do we have a reference type?
188 const ReferenceType *ParamRefType = Param->getType()->getAsReferenceType();
189 if (!ParamRefType)
190 return false;
191
192 // Is it a reference to our class type?
193 QualType PointeeType
194 = Context.getCanonicalType(ParamRefType->getPointeeType());
195 QualType ClassTy
196 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent()));
197 if (PointeeType.getUnqualifiedType() != ClassTy)
198 return false;
199
200 // We have a copy constructor.
201 TypeQuals = PointeeType.getCVRQualifiers();
202 return true;
203}
204
Douglas Gregor60d62c22008-10-31 16:23:19 +0000205bool CXXConstructorDecl::isConvertingConstructor() const {
206 // C++ [class.conv.ctor]p1:
207 // A constructor declared without the function-specifier explicit
208 // that can be called with a single parameter specifies a
209 // conversion from the type of its first parameter to the type of
210 // its class. Such a constructor is called a converting
211 // constructor.
212 if (isExplicit())
213 return false;
214
215 return (getNumParams() == 0 &&
216 getType()->getAsFunctionTypeProto()->isVariadic()) ||
217 (getNumParams() == 1) ||
218 (getNumParams() > 1 && getParamDecl(1)->getDefaultArg() != 0);
219}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000220
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000221CXXClassVarDecl *CXXClassVarDecl::Create(ASTContext &C, CXXRecordDecl *RD,
222 SourceLocation L, IdentifierInfo *Id,
223 QualType T, ScopedDecl *PrevDecl) {
224 void *Mem = C.getAllocator().Allocate<CXXClassVarDecl>();
225 return new (Mem) CXXClassVarDecl(RD, L, Id, T, PrevDecl);
226}
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000227
228OverloadedFunctionDecl *
229OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
230 IdentifierInfo *Id) {
231 void *Mem = C.getAllocator().Allocate<OverloadedFunctionDecl>();
232 return new (Mem) OverloadedFunctionDecl(DC, Id);
233}
Chris Lattner21ef7ae2008-11-04 16:51:42 +0000234
235LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
236 SourceLocation L,
237 LanguageIDs Lang, Decl *D) {
238 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
239 return new (Mem) LinkageSpecDecl(L, Lang, D);
240}
241