blob: e900165353cee470f7b381a4dc08f9228db0182a [file] [log] [blame]
Ted Kremenek21475702008-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 Gregor8ea8fd42009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek21475702008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Anders Carlsson5bbe1d72009-03-14 00:25:26 +000017#include "clang/AST/Expr.h"
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +000018#include "clang/AST/TypeLoc.h"
Douglas Gregorb6acda02008-11-12 23:21:09 +000019#include "clang/Basic/IdentifierTable.h"
Douglas Gregor74a34442008-12-23 21:31:30 +000020#include "llvm/ADT/STLExtras.h"
Fariborz Jahaniana9540492009-09-12 19:52:10 +000021#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek21475702008-09-05 17:16:31 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Decl Allocation/Deallocation Method Implementations
26//===----------------------------------------------------------------------===//
Douglas Gregor5101c242008-12-05 18:15:24 +000027
John McCall67da35c2010-02-04 22:26:26 +000028CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
29 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redlbaad4e72009-01-05 20:52:13 +000030 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedmanc80ca682009-08-15 22:23:00 +000031 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
32 Abstract(false), HasTrivialConstructor(true),
33 HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true),
Fariborz Jahanianb394f502009-09-12 18:26:03 +000034 HasTrivialDestructor(true), ComputedVisibleConversions(false),
35 Bases(0), NumBases(0), VBases(0), NumVBases(0),
John McCall16927f62010-03-12 01:19:31 +000036 Definition(D), FirstFriend(0) {
John McCall67da35c2010-02-04 22:26:26 +000037}
38
39CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
40 SourceLocation L, IdentifierInfo *Id,
41 CXXRecordDecl *PrevDecl,
42 SourceLocation TKL)
43 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
44 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +000045 TemplateOrInstantiation() { }
Douglas Gregorb6acda02008-11-12 23:21:09 +000046
Ted Kremenek21475702008-09-05 17:16:31 +000047CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
48 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +000049 SourceLocation TKL,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000050 CXXRecordDecl* PrevDecl,
51 bool DelayTypeCreation) {
Mike Stump11289f42009-09-09 15:08:12 +000052 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +000053 PrevDecl, TKL);
Mike Stump11289f42009-09-09 15:08:12 +000054
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +000055 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000056 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +000057 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +000058 return R;
59}
60
Douglas Gregor29a92472008-10-22 17:49:05 +000061CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian9fa077c2009-07-02 18:26:15 +000062}
63
64void CXXRecordDecl::Destroy(ASTContext &C) {
John McCall67da35c2010-02-04 22:26:26 +000065 if (data().Definition == this) {
66 C.Deallocate(data().Bases);
67 C.Deallocate(data().VBases);
68 C.Deallocate(&data());
69 }
Fariborz Jahanian9fa077c2009-07-02 18:26:15 +000070 this->RecordDecl::Destroy(C);
Douglas Gregor29a92472008-10-22 17:49:05 +000071}
72
Mike Stump11289f42009-09-09 15:08:12 +000073void
Douglas Gregor4a62bdf2010-02-11 01:30:34 +000074CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor9d6290b2008-10-23 18:13:27 +000075 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +000076 ASTContext &C = getASTContext();
77
Mike Stump11289f42009-09-09 15:08:12 +000078 // C++ [dcl.init.aggr]p1:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +000079 // An aggregate is an array or a class (clause 9) with [...]
80 // no base classes [...].
John McCall67da35c2010-02-04 22:26:26 +000081 data().Aggregate = false;
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +000082
John McCall67da35c2010-02-04 22:26:26 +000083 if (data().Bases)
84 C.Deallocate(data().Bases);
Mike Stump11289f42009-09-09 15:08:12 +000085
Anders Carlssonabb20e62010-03-29 05:13:12 +000086 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +000087 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlssonabb20e62010-03-29 05:13:12 +000088
89 // The virtual bases of this class.
90 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +000091
John McCall67da35c2010-02-04 22:26:26 +000092 data().Bases = new(C) CXXBaseSpecifier [NumBases];
93 data().NumBases = NumBases;
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +000094 for (unsigned i = 0; i < NumBases; ++i) {
John McCall67da35c2010-02-04 22:26:26 +000095 data().Bases[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +000096 // Keep track of inherited vbases for this base class.
97 const CXXBaseSpecifier *Base = Bases[i];
98 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +000099 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000100 if (BaseType->isDependentType())
101 continue;
102 CXXRecordDecl *BaseClassDecl
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000103 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000104
105 // Now go through all virtual bases of this base and add them.
Mike Stump11289f42009-09-09 15:08:12 +0000106 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000107 BaseClassDecl->vbases_begin(),
108 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000109 // Add this base if it's not already in the list.
Anders Carlssone47380f2010-03-29 19:49:09 +0000110 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlssonabb20e62010-03-29 05:13:12 +0000111 VBases.push_back(VBase);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000112 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000113
114 if (Base->isVirtual()) {
115 // Add this base if it's not already in the list.
Anders Carlssone47380f2010-03-29 19:49:09 +0000116 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlssonabb20e62010-03-29 05:13:12 +0000117 VBases.push_back(Base);
118 }
119
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000120 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000121
122 if (VBases.empty())
123 return;
124
125 // Create base specifier for any direct or indirect virtual bases.
126 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
127 data().NumVBases = VBases.size();
128 for (int I = 0, E = VBases.size(); I != E; ++I) {
129 QualType VBaseType = VBases[I]->getType();
130
131 // Skip dependent types; we can't do any checking on them now.
132 if (VBaseType->isDependentType())
133 continue;
134
135 CXXRecordDecl *VBaseClassDecl
136 = cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
137
138 data().VBases[I] =
139 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000140 VBaseClassDecl->getTagKind() == TTK_Class,
Anders Carlssonabb20e62010-03-29 05:13:12 +0000141 VBases[I]->getAccessSpecifier(), VBaseType);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000142 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000143}
144
Douglas Gregord2e6a452010-01-14 17:47:39 +0000145/// Callback function for CXXRecordDecl::forallBases that acknowledges
146/// that it saw a base class.
147static bool SawBase(const CXXRecordDecl *, void *) {
148 return true;
149}
150
151bool CXXRecordDecl::hasAnyDependentBases() const {
152 if (!isDependentContext())
153 return false;
154
155 return !forallBases(SawBase, 0);
156}
157
Douglas Gregor05379422008-11-03 17:51:48 +0000158bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall8ccfcb52009-09-24 19:53:00 +0000159 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000160}
161
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000162/// \brief Perform a simplistic form of overload resolution that only considers
163/// cv-qualifiers on a single parameter, and return the best overload candidate
164/// (if there is one).
165static CXXMethodDecl *
166GetBestOverloadCandidateSimple(
167 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
168 if (Cands.empty())
169 return 0;
170 if (Cands.size() == 1)
171 return Cands[0].first;
172
173 unsigned Best = 0, N = Cands.size();
174 for (unsigned I = 1; I != N; ++I)
175 if (Cands[Best].second.isSupersetOf(Cands[I].second))
176 Best = I;
177
178 for (unsigned I = 1; I != N; ++I)
179 if (Cands[Best].second.isSupersetOf(Cands[I].second))
180 return 0;
181
182 return Cands[Best].first;
183}
184
Mike Stump11289f42009-09-09 15:08:12 +0000185CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000186 unsigned TypeQuals) const{
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000187 QualType ClassType
188 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump11289f42009-09-09 15:08:12 +0000189 DeclarationName ConstructorName
Douglas Gregor1349b452008-12-15 21:24:18 +0000190 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000191 Context.getCanonicalType(ClassType));
192 unsigned FoundTQs;
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000193 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregor74a34442008-12-23 21:31:30 +0000194 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000195 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregor74a34442008-12-23 21:31:30 +0000196 Con != ConEnd; ++Con) {
Douglas Gregor2d2282e2009-09-04 14:46:39 +0000197 // C++ [class.copy]p2:
198 // A non-template constructor for class X is a copy constructor if [...]
199 if (isa<FunctionTemplateDecl>(*Con))
200 continue;
201
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000202 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
203 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall8ccfcb52009-09-24 19:53:00 +0000204 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
205 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000206 Found.push_back(std::make_pair(
207 const_cast<CXXConstructorDecl *>(Constructor),
208 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000209 }
Douglas Gregor05379422008-11-03 17:51:48 +0000210 }
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000211
212 return cast_or_null<CXXConstructorDecl>(
213 GetBestOverloadCandidateSimple(Found));
Douglas Gregor05379422008-11-03 17:51:48 +0000214}
215
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +0000216bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context,
217 const CXXMethodDecl *& MD) const {
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000218 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
219 const_cast<CXXRecordDecl*>(this)));
220 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
221
222 DeclContext::lookup_const_iterator Op, OpEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000223 for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000224 Op != OpEnd; ++Op) {
225 // C++ [class.copy]p9:
226 // A user-declared copy assignment operator is a non-static non-template
227 // member function of class X with exactly one parameter of type X, X&,
228 // const X&, volatile X& or const volatile X&.
Douglas Gregor62b885d2009-10-30 22:48:49 +0000229 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
230 if (!Method)
231 continue;
232
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000233 if (Method->isStatic())
234 continue;
Douglas Gregora14b43b2009-10-13 23:45:19 +0000235 if (Method->getPrimaryTemplate())
236 continue;
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000237 const FunctionProtoType *FnType =
John McCall9dd450b2009-09-21 23:43:11 +0000238 Method->getType()->getAs<FunctionProtoType>();
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000239 assert(FnType && "Overloaded operator has no prototype.");
240 // Don't assert on this; an invalid decl might have been left in the AST.
241 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
242 continue;
243 bool AcceptsConst = true;
244 QualType ArgType = FnType->getArgType(0);
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000245 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000246 ArgType = Ref->getPointeeType();
Douglas Gregor63b4ff62009-03-20 20:21:37 +0000247 // Is it a non-const lvalue reference?
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000248 if (!ArgType.isConstQualified())
249 AcceptsConst = false;
250 }
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000251 if (!Context.hasSameUnqualifiedType(ArgType, ClassType))
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000252 continue;
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +0000253 MD = Method;
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000254 // We have a single argument of type cv X or cv X&, i.e. we've found the
255 // copy assignment operator. Return whether it accepts const arguments.
256 return AcceptsConst;
257 }
258 assert(isInvalidDecl() &&
259 "No copy assignment operator declared in valid code.");
260 return false;
261}
262
Douglas Gregor68e11362010-07-01 17:48:08 +0000263CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
264 ASTContext &Context = getASTContext();
265 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
266 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
267
268 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
269 DeclContext::lookup_const_iterator Op, OpEnd;
270 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
271 // C++ [class.copy]p9:
272 // A user-declared copy assignment operator is a non-static non-template
273 // member function of class X with exactly one parameter of type X, X&,
274 // const X&, volatile X& or const volatile X&.
275 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
276 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
277 continue;
278
279 const FunctionProtoType *FnType
280 = Method->getType()->getAs<FunctionProtoType>();
281 assert(FnType && "Overloaded operator has no prototype.");
282 // Don't assert on this; an invalid decl might have been left in the AST.
283 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
284 continue;
285
286 QualType ArgType = FnType->getArgType(0);
287 Qualifiers Quals;
288 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
289 ArgType = Ref->getPointeeType();
290 // If we have a const argument and we have a reference to a non-const,
291 // this function does not match.
292 if (ArgIsConst && !ArgType.isConstQualified())
293 continue;
294
295 Quals = ArgType.getQualifiers();
296 } else {
297 // By-value copy-assignment operators are treated like const X&
298 // copy-assignment operators.
299 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
300 }
301
302 if (!Context.hasSameUnqualifiedType(ArgType, Class))
303 continue;
304
305 // Save this copy-assignment operator. It might be "the one".
306 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
307 }
308
309 // Use a simplistic form of overload resolution to find the candidate.
310 return GetBestOverloadCandidateSimple(Found);
311}
312
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000313void
Mike Stump11289f42009-09-09 15:08:12 +0000314CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor1349b452008-12-15 21:24:18 +0000315 CXXConstructorDecl *ConDecl) {
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000316 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
317 // Note that we have a user-declared constructor.
John McCall67da35c2010-02-04 22:26:26 +0000318 data().UserDeclaredConstructor = true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000319
Mike Stump11289f42009-09-09 15:08:12 +0000320 // C++ [dcl.init.aggr]p1:
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000321 // An aggregate is an array or a class (clause 9) with no
322 // user-declared constructors (12.1) [...].
John McCall67da35c2010-02-04 22:26:26 +0000323 data().Aggregate = false;
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000324
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000325 // C++ [class]p4:
326 // A POD-struct is an aggregate class [...]
John McCall67da35c2010-02-04 22:26:26 +0000327 data().PlainOldData = false;
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000328
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000329 // C++ [class.ctor]p5:
330 // A constructor is trivial if it is an implicitly-declared default
331 // constructor.
Douglas Gregor8a273912009-07-22 18:25:24 +0000332 // FIXME: C++0x: don't do this for "= default" default constructors.
John McCall67da35c2010-02-04 22:26:26 +0000333 data().HasTrivialConstructor = false;
Mike Stump11289f42009-09-09 15:08:12 +0000334
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000335 // Note when we have a user-declared copy constructor, which will
336 // suppress the implicit declaration of a copy constructor.
Douglas Gregor507eb872009-12-22 00:34:07 +0000337 if (ConDecl->isCopyConstructor()) {
John McCall67da35c2010-02-04 22:26:26 +0000338 data().UserDeclaredCopyConstructor = true;
Douglas Gregor8a273912009-07-22 18:25:24 +0000339
340 // C++ [class.copy]p6:
341 // A copy constructor is trivial if it is implicitly declared.
342 // FIXME: C++0x: don't do this for "= default" copy constructors.
John McCall67da35c2010-02-04 22:26:26 +0000343 data().HasTrivialCopyConstructor = false;
Douglas Gregor8a273912009-07-22 18:25:24 +0000344 }
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000345}
346
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000347void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
348 CXXMethodDecl *OpDecl) {
349 // We're interested specifically in copy assignment operators.
John McCall9dd450b2009-09-21 23:43:11 +0000350 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000351 assert(FnType && "Overloaded operator has no proto function type.");
352 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregora14b43b2009-10-13 23:45:19 +0000353
354 // Copy assignment operators must be non-templates.
355 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
356 return;
357
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000358 QualType ArgType = FnType->getArgType(0);
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000359 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000360 ArgType = Ref->getPointeeType();
361
362 ArgType = ArgType.getUnqualifiedType();
363 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
364 const_cast<CXXRecordDecl*>(this)));
365
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000366 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000367 return;
368
369 // This is a copy assignment operator.
Eli Friedman01cad4c2009-11-07 00:02:45 +0000370 // Note on the decl that it is a copy assignment operator.
371 OpDecl->setCopyAssignment(true);
372
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000373 // Suppress the implicit declaration of a copy constructor.
John McCall67da35c2010-02-04 22:26:26 +0000374 data().UserDeclaredCopyAssignment = true;
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000375
Douglas Gregor8a273912009-07-22 18:25:24 +0000376 // C++ [class.copy]p11:
377 // A copy assignment operator is trivial if it is implicitly declared.
378 // FIXME: C++0x: don't do this for "= default" copy operators.
John McCall67da35c2010-02-04 22:26:26 +0000379 data().HasTrivialCopyAssignment = false;
Douglas Gregor8a273912009-07-22 18:25:24 +0000380
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000381 // C++ [class]p4:
382 // A POD-struct is an aggregate class that [...] has no user-defined copy
383 // assignment operator [...].
John McCall67da35c2010-02-04 22:26:26 +0000384 data().PlainOldData = false;
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000385}
386
John McCall1e3a1a72010-03-15 09:07:48 +0000387static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
388 QualType T;
John McCallda4458e2010-03-31 01:36:47 +0000389 if (isa<UsingShadowDecl>(Conv))
390 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCall1e3a1a72010-03-15 09:07:48 +0000391 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
392 T = ConvTemp->getTemplatedDecl()->getResultType();
393 else
394 T = cast<CXXConversionDecl>(Conv)->getConversionType();
395 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +0000396}
397
John McCall1e3a1a72010-03-15 09:07:48 +0000398/// Collect the visible conversions of a base class.
399///
400/// \param Base a base class of the class we're considering
401/// \param InVirtual whether this base class is a virtual base (or a base
402/// of a virtual base)
403/// \param Access the access along the inheritance path to this base
404/// \param ParentHiddenTypes the conversions provided by the inheritors
405/// of this base
406/// \param Output the set to which to add conversions from non-virtual bases
407/// \param VOutput the set to which to add conversions from virtual bases
408/// \param HiddenVBaseCs the set of conversions which were hidden in a
409/// virtual base along some inheritance path
410static void CollectVisibleConversions(ASTContext &Context,
411 CXXRecordDecl *Record,
412 bool InVirtual,
413 AccessSpecifier Access,
414 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
415 UnresolvedSetImpl &Output,
416 UnresolvedSetImpl &VOutput,
417 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
418 // The set of types which have conversions in this class or its
419 // subclasses. As an optimization, we don't copy the derived set
420 // unless it might change.
421 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
422 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
423
424 // Collect the direct conversions and figure out which conversions
425 // will be hidden in the subclasses.
426 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
427 if (!Cs.empty()) {
428 HiddenTypesBuffer = ParentHiddenTypes;
429 HiddenTypes = &HiddenTypesBuffer;
430
431 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
432 bool Hidden =
433 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
434
435 // If this conversion is hidden and we're in a virtual base,
436 // remember that it's hidden along some inheritance path.
437 if (Hidden && InVirtual)
438 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
439
440 // If this conversion isn't hidden, add it to the appropriate output.
441 else if (!Hidden) {
442 AccessSpecifier IAccess
443 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
444
445 if (InVirtual)
446 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000447 else
John McCall1e3a1a72010-03-15 09:07:48 +0000448 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000449 }
450 }
451 }
Sebastian Redl1054fae2009-10-25 17:03:50 +0000452
John McCall1e3a1a72010-03-15 09:07:48 +0000453 // Collect information recursively from any base classes.
454 for (CXXRecordDecl::base_class_iterator
455 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
456 const RecordType *RT = I->getType()->getAs<RecordType>();
457 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +0000458
John McCall1e3a1a72010-03-15 09:07:48 +0000459 AccessSpecifier BaseAccess
460 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
461 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +0000462
John McCall1e3a1a72010-03-15 09:07:48 +0000463 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
464 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
465 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000466 }
John McCall1e3a1a72010-03-15 09:07:48 +0000467}
Sebastian Redl1054fae2009-10-25 17:03:50 +0000468
John McCall1e3a1a72010-03-15 09:07:48 +0000469/// Collect the visible conversions of a class.
470///
471/// This would be extremely straightforward if it weren't for virtual
472/// bases. It might be worth special-casing that, really.
473static void CollectVisibleConversions(ASTContext &Context,
474 CXXRecordDecl *Record,
475 UnresolvedSetImpl &Output) {
476 // The collection of all conversions in virtual bases that we've
477 // found. These will be added to the output as long as they don't
478 // appear in the hidden-conversions set.
479 UnresolvedSet<8> VBaseCs;
480
481 // The set of conversions in virtual bases that we've determined to
482 // be hidden.
483 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
484
485 // The set of types hidden by classes derived from this one.
486 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
487
488 // Go ahead and collect the direct conversions and add them to the
489 // hidden-types set.
490 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
491 Output.append(Cs.begin(), Cs.end());
492 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
493 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
494
495 // Recursively collect conversions from base classes.
496 for (CXXRecordDecl::base_class_iterator
497 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
498 const RecordType *RT = I->getType()->getAs<RecordType>();
499 if (!RT) continue;
500
501 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
502 I->isVirtual(), I->getAccessSpecifier(),
503 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
504 }
505
506 // Add any unhidden conversions provided by virtual bases.
507 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
508 I != E; ++I) {
509 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
510 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000511 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000512}
513
514/// getVisibleConversionFunctions - get all conversion functions visible
515/// in current class; including conversion function templates.
John McCallad371252010-01-20 00:46:10 +0000516const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000517 // If root class, all conversions are visible.
518 if (bases_begin() == bases_end())
John McCall67da35c2010-02-04 22:26:26 +0000519 return &data().Conversions;
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000520 // If visible conversion list is already evaluated, return it.
John McCall67da35c2010-02-04 22:26:26 +0000521 if (data().ComputedVisibleConversions)
522 return &data().VisibleConversions;
John McCall1e3a1a72010-03-15 09:07:48 +0000523 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall67da35c2010-02-04 22:26:26 +0000524 data().ComputedVisibleConversions = true;
525 return &data().VisibleConversions;
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000526}
527
John McCallda4458e2010-03-31 01:36:47 +0000528#ifndef NDEBUG
529void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCall1e3a1a72010-03-15 09:07:48 +0000530 assert(ConvDecl->getDeclContext() == this &&
531 "conversion function does not belong to this record");
532
John McCallda4458e2010-03-31 01:36:47 +0000533 ConvDecl = ConvDecl->getUnderlyingDecl();
534 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
535 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
536 } else {
537 assert(isa<CXXConversionDecl>(ConvDecl));
538 }
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000539}
John McCallda4458e2010-03-31 01:36:47 +0000540#endif
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000541
John McCallda4458e2010-03-31 01:36:47 +0000542void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
543 // This operation is O(N) but extremely rare. Sema only uses it to
544 // remove UsingShadowDecls in a class that were followed by a direct
545 // declaration, e.g.:
546 // class A : B {
547 // using B::operator int;
548 // operator int();
549 // };
550 // This is uncommon by itself and even more uncommon in conjunction
551 // with sufficiently large numbers of directly-declared conversions
552 // that asymptotic behavior matters.
553
554 UnresolvedSetImpl &Convs = *getConversionFunctions();
555 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
556 if (Convs[I].getDecl() == ConvDecl) {
557 Convs.erase(I);
558 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
559 && "conversion was found multiple times in unresolved set");
560 return;
561 }
562 }
563
564 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +0000565}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000566
Fariborz Jahanian6dfc1972009-12-03 18:44:40 +0000567void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
568 Method->setVirtualAsWritten(true);
569 setAggregate(false);
570 setPOD(false);
571 setEmpty(false);
572 setPolymorphic(true);
573 setHasTrivialConstructor(false);
574 setHasTrivialCopyConstructor(false);
575 setHasTrivialCopyAssignment(false);
576}
577
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000578CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000579 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000580 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
581
582 return 0;
583}
584
Douglas Gregor06db9f52009-10-12 20:18:28 +0000585MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
586 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
587}
588
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000589void
590CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
591 TemplateSpecializationKind TSK) {
592 assert(TemplateOrInstantiation.isNull() &&
593 "Previous template or instantiation?");
594 assert(!isa<ClassTemplateSpecializationDecl>(this));
595 TemplateOrInstantiation
596 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
597}
598
Anders Carlsson27cfc6e2009-12-07 06:33:48 +0000599TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
600 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000601 = dyn_cast<ClassTemplateSpecializationDecl>(this))
602 return Spec->getSpecializationKind();
603
Douglas Gregor06db9f52009-10-12 20:18:28 +0000604 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000605 return MSInfo->getTemplateSpecializationKind();
606
607 return TSK_Undeclared;
608}
609
610void
611CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
612 if (ClassTemplateSpecializationDecl *Spec
613 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
614 Spec->setSpecializationKind(TSK);
615 return;
616 }
617
Douglas Gregor06db9f52009-10-12 20:18:28 +0000618 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000619 MSInfo->setTemplateSpecializationKind(TSK);
620 return;
621 }
622
623 assert(false && "Not a class template or member class specialization");
624}
625
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000626CXXConstructorDecl *
627CXXRecordDecl::getDefaultConstructor(ASTContext &Context) {
628 QualType ClassType = Context.getTypeDeclType(this);
629 DeclarationName ConstructorName
630 = Context.DeclarationNames.getCXXConstructorName(
631 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump11289f42009-09-09 15:08:12 +0000632
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000633 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000634 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000635 Con != ConEnd; ++Con) {
Douglas Gregor2d2282e2009-09-04 14:46:39 +0000636 // FIXME: In C++0x, a constructor template can be a default constructor.
637 if (isa<FunctionTemplateDecl>(*Con))
638 continue;
639
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000640 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
641 if (Constructor->isDefaultConstructor())
642 return Constructor;
643 }
644 return 0;
645}
646
Douglas Gregorbac74902010-07-01 14:13:13 +0000647CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
648 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +0000649 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +0000650
651 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +0000652 = Context.DeclarationNames.getCXXDestructorName(
653 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +0000654
John McCallf8ff7b92010-02-23 00:48:20 +0000655 DeclContext::lookup_const_iterator I, E;
Mike Stump11289f42009-09-09 15:08:12 +0000656 llvm::tie(I, E) = lookup(Name);
Anders Carlsson0a637412009-05-29 21:03:38 +0000657 assert(I != E && "Did not find a destructor!");
Mike Stump11289f42009-09-09 15:08:12 +0000658
Anders Carlssonf98849e2009-12-02 17:15:43 +0000659 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson0a637412009-05-29 21:03:38 +0000660 assert(++I == E && "Found more than one destructor!");
Mike Stump11289f42009-09-09 15:08:12 +0000661
Anders Carlsson0a637412009-05-29 21:03:38 +0000662 return Dtor;
663}
664
Ted Kremenek21475702008-09-05 17:16:31 +0000665CXXMethodDecl *
666CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor92751d42008-11-17 22:58:34 +0000667 SourceLocation L, DeclarationName N,
John McCallbcd03502009-12-07 02:54:59 +0000668 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000669 bool isStatic, StorageClass SCAsWritten, bool isInline) {
John McCallbcd03502009-12-07 02:54:59 +0000670 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000671 isStatic, SCAsWritten, isInline);
Ted Kremenek21475702008-09-05 17:16:31 +0000672}
673
Douglas Gregorbb3e12f2009-09-29 18:16:17 +0000674bool CXXMethodDecl::isUsualDeallocationFunction() const {
675 if (getOverloadedOperator() != OO_Delete &&
676 getOverloadedOperator() != OO_Array_Delete)
677 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +0000678
679 // C++ [basic.stc.dynamic.deallocation]p2:
680 // A template instance is never a usual deallocation function,
681 // regardless of its signature.
682 if (getPrimaryTemplate())
683 return false;
684
Douglas Gregorbb3e12f2009-09-29 18:16:17 +0000685 // C++ [basic.stc.dynamic.deallocation]p2:
686 // If a class T has a member deallocation function named operator delete
687 // with exactly one parameter, then that function is a usual (non-placement)
688 // deallocation function. [...]
689 if (getNumParams() == 1)
690 return true;
691
692 // C++ [basic.stc.dynamic.deallocation]p2:
693 // [...] If class T does not declare such an operator delete but does
694 // declare a member deallocation function named operator delete with
695 // exactly two parameters, the second of which has type std::size_t (18.1),
696 // then this function is a usual deallocation function.
697 ASTContext &Context = getASTContext();
698 if (getNumParams() != 2 ||
Chandler Carruth75cc3592010-02-08 18:54:05 +0000699 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
700 Context.getSizeType()))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +0000701 return false;
702
703 // This function is a usual deallocation function if there are no
704 // single-parameter deallocation functions of the same kind.
705 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
706 R.first != R.second; ++R.first) {
707 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
708 if (FD->getNumParams() == 1)
709 return false;
710 }
711
712 return true;
713}
714
Douglas Gregorb139cd52010-05-01 20:49:11 +0000715bool CXXMethodDecl::isCopyAssignmentOperator() const {
716 // C++0x [class.copy]p19:
717 // A user-declared copy assignment operator X::operator= is a non-static
718 // non-template member function of class X with exactly one parameter of
719 // type X, X&, const X&, volatile X& or const volatile X&.
720 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
721 /*non-static*/ isStatic() ||
722 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
723 /*exactly one parameter*/getNumParams() != 1)
724 return false;
725
726 QualType ParamType = getParamDecl(0)->getType();
727 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
728 ParamType = Ref->getPointeeType();
729
730 ASTContext &Context = getASTContext();
731 QualType ClassType
732 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
733 return Context.hasSameUnqualifiedType(ClassType, ParamType);
734}
735
Anders Carlsson36d87e12009-05-16 23:58:37 +0000736void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +0000737 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +0000738 assert(!MD->getParent()->isDependentContext() &&
739 "Can't add an overridden method to a class template!");
740
Douglas Gregor832940b2010-03-02 23:58:15 +0000741 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +0000742}
743
744CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor832940b2010-03-02 23:58:15 +0000745 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +0000746}
747
748CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor832940b2010-03-02 23:58:15 +0000749 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +0000750}
751
Ted Kremenek21475702008-09-05 17:16:31 +0000752QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +0000753 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
754 // If the member function is declared const, the type of this is const X*,
755 // if the member function is declared volatile, the type of this is
756 // volatile X*, and if the member function is declared const volatile,
757 // the type of this is const volatile X*.
758
Ted Kremenek21475702008-09-05 17:16:31 +0000759 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +0000760
John McCalle78aac42010-03-10 03:28:59 +0000761 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +0000762 ClassTy = C.getQualifiedType(ClassTy,
763 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +0000764 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +0000765}
766
Eli Friedman71a26d82009-12-06 20:50:05 +0000767bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +0000768 // If this function is a template instantiation, look at the template from
769 // which it was instantiated.
770 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
771 if (!CheckFn)
772 CheckFn = this;
773
Eli Friedman71a26d82009-12-06 20:50:05 +0000774 const FunctionDecl *fn;
Douglas Gregora318efd2010-01-05 19:06:31 +0000775 return CheckFn->getBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +0000776}
777
Douglas Gregore8381c02008-11-05 04:29:56 +0000778CXXBaseOrMemberInitializer::
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000779CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson1c0f8bb2010-04-12 00:51:03 +0000780 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000781 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnara341d7832010-05-26 18:09:23 +0000782 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
783 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
784 SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000785{
Douglas Gregore8381c02008-11-05 04:29:56 +0000786}
787
788CXXBaseOrMemberInitializer::
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000789CXXBaseOrMemberInitializer(ASTContext &Context,
790 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000791 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnara341d7832010-05-26 18:09:23 +0000792 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
793 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
794 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000795{
Douglas Gregore8381c02008-11-05 04:29:56 +0000796}
797
Douglas Gregor94f9a482010-05-05 05:51:00 +0000798CXXBaseOrMemberInitializer::
799CXXBaseOrMemberInitializer(ASTContext &Context,
800 FieldDecl *Member, SourceLocation MemberLoc,
801 SourceLocation L, Expr *Init, SourceLocation R,
802 VarDecl **Indices,
803 unsigned NumIndices)
804 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnara341d7832010-05-26 18:09:23 +0000805 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
806 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregor94f9a482010-05-05 05:51:00 +0000807{
808 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
809 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
810}
811
812CXXBaseOrMemberInitializer *
813CXXBaseOrMemberInitializer::Create(ASTContext &Context,
814 FieldDecl *Member,
815 SourceLocation MemberLoc,
816 SourceLocation L,
817 Expr *Init,
818 SourceLocation R,
819 VarDecl **Indices,
820 unsigned NumIndices) {
821 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
822 sizeof(VarDecl *) * NumIndices,
823 llvm::alignof<CXXBaseOrMemberInitializer>());
824 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
825 L, Init, R, Indices, NumIndices);
826}
827
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000828void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) {
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000829 if (Init)
830 Init->Destroy(Context);
Douglas Gregor94f9a482010-05-05 05:51:00 +0000831 // FIXME: Destroy indices
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000832 this->~CXXBaseOrMemberInitializer();
833}
834
835TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
836 if (isBaseInitializer())
John McCallbcd03502009-12-07 02:54:59 +0000837 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000838 else
839 return TypeLoc();
840}
841
842Type *CXXBaseOrMemberInitializer::getBaseClass() {
843 if (isBaseInitializer())
John McCallbcd03502009-12-07 02:54:59 +0000844 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000845 else
846 return 0;
847}
848
849const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
850 if (isBaseInitializer())
John McCallbcd03502009-12-07 02:54:59 +0000851 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000852 else
853 return 0;
854}
855
856SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
857 if (isMemberInitializer())
858 return getMemberLocation();
859
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000860 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000861}
862
863SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
864 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +0000865}
866
Douglas Gregor61956c42008-10-31 09:07:45 +0000867CXXConstructorDecl *
Chris Lattnerca025db2010-05-07 21:43:38 +0000868CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
869 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationName(),
870 QualType(), 0, false, false, false);
871}
872
873CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +0000874CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor77324f32008-11-17 14:58:09 +0000875 SourceLocation L, DeclarationName N,
John McCallbcd03502009-12-07 02:54:59 +0000876 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000877 bool isExplicit,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000878 bool isInline,
879 bool isImplicitlyDeclared) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000880 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
881 "Name must refer to a constructor");
Douglas Gregorc4df4072010-04-19 22:54:31 +0000882 return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit,
883 isInline, isImplicitlyDeclared);
Douglas Gregor61956c42008-10-31 09:07:45 +0000884}
885
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000886bool CXXConstructorDecl::isDefaultConstructor() const {
887 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000888 // A default constructor for a class X is a constructor of class
889 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000890 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +0000891 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000892}
893
Mike Stump11289f42009-09-09 15:08:12 +0000894bool
Douglas Gregor507eb872009-12-22 00:34:07 +0000895CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000896 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000897 // A non-template constructor for class X is a copy constructor
898 // if its first parameter is of type X&, const X&, volatile X& or
899 // const volatile X&, and either there are no other parameters
900 // or else all other parameters have default arguments (8.3.6).
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000901 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +0000902 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorff7028a2009-11-13 23:59:09 +0000903 (getPrimaryTemplate() != 0) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +0000904 (getDescribedFunctionTemplate() != 0))
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000905 return false;
906
907 const ParmVarDecl *Param = getParamDecl(0);
908
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000909 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorff7028a2009-11-13 23:59:09 +0000910 const LValueReferenceType *ParamRefType =
911 Param->getType()->getAs<LValueReferenceType>();
912 if (!ParamRefType)
913 return false;
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000914
Douglas Gregorff7028a2009-11-13 23:59:09 +0000915 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +0000916 ASTContext &Context = getASTContext();
917
Douglas Gregorff7028a2009-11-13 23:59:09 +0000918 CanQualType PointeeType
919 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +0000920 CanQualType ClassTy
921 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000922 if (PointeeType.getUnqualifiedType() != ClassTy)
923 return false;
924
John McCall8ccfcb52009-09-24 19:53:00 +0000925 // FIXME: other qualifiers?
926
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000927 // We have a copy constructor.
928 TypeQuals = PointeeType.getCVRQualifiers();
929 return true;
930}
931
Anders Carlssond20e7952009-08-28 16:57:08 +0000932bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000933 // C++ [class.conv.ctor]p1:
934 // A constructor declared without the function-specifier explicit
935 // that can be called with a single parameter specifies a
936 // conversion from the type of its first parameter to the type of
937 // its class. Such a constructor is called a converting
938 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +0000939 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000940 return false;
941
Mike Stump11289f42009-09-09 15:08:12 +0000942 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +0000943 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000944 (getNumParams() == 1) ||
Anders Carlsson85446472009-06-06 04:14:07 +0000945 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000946}
Douglas Gregor61956c42008-10-31 09:07:45 +0000947
Douglas Gregorffe14e32009-11-14 01:20:54 +0000948bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
949 if ((getNumParams() < 1) ||
950 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
951 (getPrimaryTemplate() == 0) ||
952 (getDescribedFunctionTemplate() != 0))
953 return false;
954
955 const ParmVarDecl *Param = getParamDecl(0);
956
957 ASTContext &Context = getASTContext();
958 CanQualType ParamType = Context.getCanonicalType(Param->getType());
959
960 // Strip off the lvalue reference, if any.
961 if (CanQual<LValueReferenceType> ParamRefType
962 = ParamType->getAs<LValueReferenceType>())
963 ParamType = ParamRefType->getPointeeType();
964
965
966 // Is it the same as our our class type?
967 CanQualType ClassTy
968 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
969 if (ParamType.getUnqualifiedType() != ClassTy)
970 return false;
971
972 return true;
973}
974
Douglas Gregor831c93f2008-11-05 20:51:48 +0000975CXXDestructorDecl *
Chris Lattnerca025db2010-05-07 21:43:38 +0000976CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
977 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationName(),
978 QualType(), false, false);
979}
980
981CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +0000982CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor77324f32008-11-17 14:58:09 +0000983 SourceLocation L, DeclarationName N,
Mike Stump11289f42009-09-09 15:08:12 +0000984 QualType T, bool isInline,
Douglas Gregor831c93f2008-11-05 20:51:48 +0000985 bool isImplicitlyDeclared) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000986 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
987 "Name must refer to a destructor");
Douglas Gregorc4df4072010-04-19 22:54:31 +0000988 return new (C) CXXDestructorDecl(RD, L, N, T, isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000989}
990
Fariborz Jahanian52337412009-07-01 21:05:43 +0000991void
Fariborz Jahanian5c6af0a2009-07-01 23:35:25 +0000992CXXConstructorDecl::Destroy(ASTContext& C) {
993 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian003a8802009-07-07 16:24:08 +0000994 CXXMethodDecl::Destroy(C);
Fariborz Jahanian5c6af0a2009-07-01 23:35:25 +0000995}
996
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000997CXXConversionDecl *
Chris Lattnerca025db2010-05-07 21:43:38 +0000998CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
999 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationName(),
1000 QualType(), 0, false, false);
1001}
1002
1003CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001004CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor77324f32008-11-17 14:58:09 +00001005 SourceLocation L, DeclarationName N,
John McCallbcd03502009-12-07 02:54:59 +00001006 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001007 bool isInline, bool isExplicit) {
Douglas Gregor77324f32008-11-17 14:58:09 +00001008 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
1009 "Name must refer to a conversion function");
John McCallbcd03502009-12-07 02:54:59 +00001010 return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001011}
1012
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00001013LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00001014 DeclContext *DC,
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00001015 SourceLocation L,
Douglas Gregor07665a62009-01-05 19:45:36 +00001016 LanguageIDs Lang, bool Braces) {
Steve Naroff13ae6f42009-01-27 21:25:57 +00001017 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00001018}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001019
1020UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1021 SourceLocation L,
1022 SourceLocation NamespaceLoc,
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +00001023 SourceRange QualifierRange,
1024 NestedNameSpecifier *Qualifier,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001025 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00001026 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001027 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001028 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1029 Used = NS->getOriginalNamespace();
Mike Stump11289f42009-09-09 15:08:12 +00001030 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +00001031 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00001032}
1033
Sebastian Redla6602e92009-11-23 15:34:23 +00001034NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1035 if (NamespaceAliasDecl *NA =
1036 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1037 return NA->getNamespace();
1038 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1039}
1040
Chris Lattnerca025db2010-05-07 21:43:38 +00001041void UsingDirectiveDecl::setNominatedNamespace(NamedDecl* ND) {
1042 assert((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) &&
1043 "expected a NamespaceDecl or NamespaceAliasDecl");
1044 NominatedNamespace = ND;
1045}
1046
Mike Stump11289f42009-09-09 15:08:12 +00001047NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
1048 SourceLocation L,
1049 SourceLocation AliasLoc,
1050 IdentifierInfo *Alias,
Douglas Gregor18231932009-05-30 06:48:27 +00001051 SourceRange QualifierRange,
1052 NestedNameSpecifier *Qualifier,
Mike Stump11289f42009-09-09 15:08:12 +00001053 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00001054 NamedDecl *Namespace) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001055 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1056 Namespace = NS->getOriginalNamespace();
Mike Stump11289f42009-09-09 15:08:12 +00001057 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor18231932009-05-30 06:48:27 +00001058 Qualifier, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00001059}
1060
Douglas Gregorfec52632009-06-20 00:51:54 +00001061UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
John McCall3f746822009-11-17 05:59:44 +00001062 SourceLocation L, SourceRange NNR, SourceLocation UL,
1063 NestedNameSpecifier* TargetNNS, DeclarationName Name,
1064 bool IsTypeNameArg) {
1065 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg);
Douglas Gregorfec52632009-06-20 00:51:54 +00001066}
1067
John McCalle61f2ba2009-11-18 02:36:19 +00001068UnresolvedUsingValueDecl *
1069UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1070 SourceLocation UsingLoc,
1071 SourceRange TargetNNR,
1072 NestedNameSpecifier *TargetNNS,
1073 SourceLocation TargetNameLoc,
1074 DeclarationName TargetName) {
1075 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
1076 TargetNNR, TargetNNS,
1077 TargetNameLoc, TargetName);
1078}
1079
1080UnresolvedUsingTypenameDecl *
1081UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1082 SourceLocation UsingLoc,
1083 SourceLocation TypenameLoc,
1084 SourceRange TargetNNR,
1085 NestedNameSpecifier *TargetNNS,
1086 SourceLocation TargetNameLoc,
1087 DeclarationName TargetName) {
1088 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1089 TargetNNR, TargetNNS,
1090 TargetNameLoc,
1091 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +00001092}
1093
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00001094StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1095 SourceLocation L, Expr *AssertExpr,
1096 StringLiteral *Message) {
1097 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1098}
1099
1100void StaticAssertDecl::Destroy(ASTContext& C) {
1101 AssertExpr->Destroy(C);
1102 Message->Destroy(C);
John McCall3e11ebe2010-03-15 10:12:16 +00001103 Decl::Destroy(C);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00001104}
1105
1106StaticAssertDecl::~StaticAssertDecl() {
1107}
1108
Anders Carlsson6750d162009-03-26 23:46:50 +00001109static const char *getAccessName(AccessSpecifier AS) {
1110 switch (AS) {
1111 default:
1112 case AS_none:
1113 assert("Invalid access specifier!");
1114 return 0;
1115 case AS_public:
1116 return "public";
1117 case AS_private:
1118 return "private";
1119 case AS_protected:
1120 return "protected";
1121 }
1122}
1123
1124const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1125 AccessSpecifier AS) {
1126 return DB << getAccessName(AS);
1127}
1128
1129