blob: 19c3ac10119eb11e42f99da34785ea7fe6161d51 [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),
Douglas Gregor9672f922010-07-03 00:47:00 +000035 DeclaredDefaultConstructor(false), DeclaredCopyConstructor(false),
Douglas Gregor330b9cf2010-07-02 21:50:04 +000036 DeclaredCopyAssignment(false), DeclaredDestructor(false),
Fariborz Jahanianb394f502009-09-12 18:26:03 +000037 Bases(0), NumBases(0), VBases(0), NumVBases(0),
John McCall16927f62010-03-12 01:19:31 +000038 Definition(D), FirstFriend(0) {
John McCall67da35c2010-02-04 22:26:26 +000039}
40
41CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
42 SourceLocation L, IdentifierInfo *Id,
43 CXXRecordDecl *PrevDecl,
44 SourceLocation TKL)
45 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
46 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +000047 TemplateOrInstantiation() { }
Douglas Gregorb6acda02008-11-12 23:21:09 +000048
Ted Kremenek21475702008-09-05 17:16:31 +000049CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
50 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +000051 SourceLocation TKL,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000052 CXXRecordDecl* PrevDecl,
53 bool DelayTypeCreation) {
Mike Stump11289f42009-09-09 15:08:12 +000054 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +000055 PrevDecl, TKL);
Mike Stump11289f42009-09-09 15:08:12 +000056
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +000057 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000058 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +000059 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +000060 return R;
61}
62
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +000063CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, EmptyShell Empty) {
64 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(), 0, 0,
65 SourceLocation());
66}
67
Douglas Gregor29a92472008-10-22 17:49:05 +000068CXXRecordDecl::~CXXRecordDecl() {
Fariborz Jahanian9fa077c2009-07-02 18:26:15 +000069}
70
71void CXXRecordDecl::Destroy(ASTContext &C) {
John McCall67da35c2010-02-04 22:26:26 +000072 if (data().Definition == this) {
73 C.Deallocate(data().Bases);
74 C.Deallocate(data().VBases);
75 C.Deallocate(&data());
76 }
Fariborz Jahanian9fa077c2009-07-02 18:26:15 +000077 this->RecordDecl::Destroy(C);
Douglas Gregor29a92472008-10-22 17:49:05 +000078}
79
Mike Stump11289f42009-09-09 15:08:12 +000080void
Douglas Gregor4a62bdf2010-02-11 01:30:34 +000081CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor9d6290b2008-10-23 18:13:27 +000082 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +000083 ASTContext &C = getASTContext();
84
Mike Stump11289f42009-09-09 15:08:12 +000085 // C++ [dcl.init.aggr]p1:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +000086 // An aggregate is an array or a class (clause 9) with [...]
87 // no base classes [...].
John McCall67da35c2010-02-04 22:26:26 +000088 data().Aggregate = false;
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +000089
John McCall67da35c2010-02-04 22:26:26 +000090 if (data().Bases)
91 C.Deallocate(data().Bases);
Mike Stump11289f42009-09-09 15:08:12 +000092
Anders Carlssonabb20e62010-03-29 05:13:12 +000093 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +000094 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlssonabb20e62010-03-29 05:13:12 +000095
96 // The virtual bases of this class.
97 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +000098
John McCall67da35c2010-02-04 22:26:26 +000099 data().Bases = new(C) CXXBaseSpecifier [NumBases];
100 data().NumBases = NumBases;
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000101 for (unsigned i = 0; i < NumBases; ++i) {
John McCall67da35c2010-02-04 22:26:26 +0000102 data().Bases[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000103 // Keep track of inherited vbases for this base class.
104 const CXXBaseSpecifier *Base = Bases[i];
105 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000106 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000107 if (BaseType->isDependentType())
108 continue;
109 CXXRecordDecl *BaseClassDecl
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000110 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000111
112 // Now go through all virtual bases of this base and add them.
Mike Stump11289f42009-09-09 15:08:12 +0000113 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000114 BaseClassDecl->vbases_begin(),
115 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000116 // Add this base if it's not already in the list.
Anders Carlssone47380f2010-03-29 19:49:09 +0000117 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlssonabb20e62010-03-29 05:13:12 +0000118 VBases.push_back(VBase);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000119 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000120
121 if (Base->isVirtual()) {
122 // Add this base if it's not already in the list.
Anders Carlssone47380f2010-03-29 19:49:09 +0000123 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlssonabb20e62010-03-29 05:13:12 +0000124 VBases.push_back(Base);
125 }
126
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000127 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000128
129 if (VBases.empty())
130 return;
131
132 // Create base specifier for any direct or indirect virtual bases.
133 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
134 data().NumVBases = VBases.size();
135 for (int I = 0, E = VBases.size(); I != E; ++I) {
136 QualType VBaseType = VBases[I]->getType();
137
138 // Skip dependent types; we can't do any checking on them now.
139 if (VBaseType->isDependentType())
140 continue;
141
142 CXXRecordDecl *VBaseClassDecl
143 = cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
144
145 data().VBases[I] =
146 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000147 VBaseClassDecl->getTagKind() == TTK_Class,
Anders Carlssonabb20e62010-03-29 05:13:12 +0000148 VBases[I]->getAccessSpecifier(), VBaseType);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000149 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000150}
151
Douglas Gregord2e6a452010-01-14 17:47:39 +0000152/// Callback function for CXXRecordDecl::forallBases that acknowledges
153/// that it saw a base class.
154static bool SawBase(const CXXRecordDecl *, void *) {
155 return true;
156}
157
158bool CXXRecordDecl::hasAnyDependentBases() const {
159 if (!isDependentContext())
160 return false;
161
162 return !forallBases(SawBase, 0);
163}
164
Douglas Gregor05379422008-11-03 17:51:48 +0000165bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall8ccfcb52009-09-24 19:53:00 +0000166 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000167}
168
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000169/// \brief Perform a simplistic form of overload resolution that only considers
170/// cv-qualifiers on a single parameter, and return the best overload candidate
171/// (if there is one).
172static CXXMethodDecl *
173GetBestOverloadCandidateSimple(
174 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
175 if (Cands.empty())
176 return 0;
177 if (Cands.size() == 1)
178 return Cands[0].first;
179
180 unsigned Best = 0, N = Cands.size();
181 for (unsigned I = 1; I != N; ++I)
182 if (Cands[Best].second.isSupersetOf(Cands[I].second))
183 Best = I;
184
185 for (unsigned I = 1; I != N; ++I)
186 if (Cands[Best].second.isSupersetOf(Cands[I].second))
187 return 0;
188
189 return Cands[Best].first;
190}
191
Mike Stump11289f42009-09-09 15:08:12 +0000192CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000193 unsigned TypeQuals) const{
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000194 QualType ClassType
195 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump11289f42009-09-09 15:08:12 +0000196 DeclarationName ConstructorName
Douglas Gregor1349b452008-12-15 21:24:18 +0000197 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000198 Context.getCanonicalType(ClassType));
199 unsigned FoundTQs;
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000200 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregor74a34442008-12-23 21:31:30 +0000201 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000202 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregor74a34442008-12-23 21:31:30 +0000203 Con != ConEnd; ++Con) {
Douglas Gregor2d2282e2009-09-04 14:46:39 +0000204 // C++ [class.copy]p2:
205 // A non-template constructor for class X is a copy constructor if [...]
206 if (isa<FunctionTemplateDecl>(*Con))
207 continue;
208
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000209 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
210 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall8ccfcb52009-09-24 19:53:00 +0000211 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
212 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000213 Found.push_back(std::make_pair(
214 const_cast<CXXConstructorDecl *>(Constructor),
215 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000216 }
Douglas Gregor05379422008-11-03 17:51:48 +0000217 }
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000218
219 return cast_or_null<CXXConstructorDecl>(
220 GetBestOverloadCandidateSimple(Found));
Douglas Gregor05379422008-11-03 17:51:48 +0000221}
222
Douglas Gregor68e11362010-07-01 17:48:08 +0000223CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
224 ASTContext &Context = getASTContext();
225 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
226 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
227
228 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
229 DeclContext::lookup_const_iterator Op, OpEnd;
230 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
231 // C++ [class.copy]p9:
232 // A user-declared copy assignment operator is a non-static non-template
233 // member function of class X with exactly one parameter of type X, X&,
234 // const X&, volatile X& or const volatile X&.
235 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
236 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
237 continue;
238
239 const FunctionProtoType *FnType
240 = Method->getType()->getAs<FunctionProtoType>();
241 assert(FnType && "Overloaded operator has no prototype.");
242 // Don't assert on this; an invalid decl might have been left in the AST.
243 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
244 continue;
245
246 QualType ArgType = FnType->getArgType(0);
247 Qualifiers Quals;
248 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
249 ArgType = Ref->getPointeeType();
250 // If we have a const argument and we have a reference to a non-const,
251 // this function does not match.
252 if (ArgIsConst && !ArgType.isConstQualified())
253 continue;
254
255 Quals = ArgType.getQualifiers();
256 } else {
257 // By-value copy-assignment operators are treated like const X&
258 // copy-assignment operators.
259 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
260 }
261
262 if (!Context.hasSameUnqualifiedType(ArgType, Class))
263 continue;
264
265 // Save this copy-assignment operator. It might be "the one".
266 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
267 }
268
269 // Use a simplistic form of overload resolution to find the candidate.
270 return GetBestOverloadCandidateSimple(Found);
271}
272
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000273void
Mike Stump11289f42009-09-09 15:08:12 +0000274CXXRecordDecl::addedConstructor(ASTContext &Context,
Douglas Gregor1349b452008-12-15 21:24:18 +0000275 CXXConstructorDecl *ConDecl) {
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000276 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
277 // Note that we have a user-declared constructor.
John McCall67da35c2010-02-04 22:26:26 +0000278 data().UserDeclaredConstructor = true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000279
Douglas Gregor9672f922010-07-03 00:47:00 +0000280 // Note that we have no need of an implicitly-declared default constructor.
281 data().DeclaredDefaultConstructor = true;
282
Mike Stump11289f42009-09-09 15:08:12 +0000283 // C++ [dcl.init.aggr]p1:
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000284 // An aggregate is an array or a class (clause 9) with no
285 // user-declared constructors (12.1) [...].
John McCall67da35c2010-02-04 22:26:26 +0000286 data().Aggregate = false;
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000287
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000288 // C++ [class]p4:
289 // A POD-struct is an aggregate class [...]
John McCall67da35c2010-02-04 22:26:26 +0000290 data().PlainOldData = false;
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000291
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000292 // C++ [class.ctor]p5:
293 // A constructor is trivial if it is an implicitly-declared default
294 // constructor.
Douglas Gregor8a273912009-07-22 18:25:24 +0000295 // FIXME: C++0x: don't do this for "= default" default constructors.
John McCall67da35c2010-02-04 22:26:26 +0000296 data().HasTrivialConstructor = false;
Mike Stump11289f42009-09-09 15:08:12 +0000297
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000298 // Note when we have a user-declared copy constructor, which will
299 // suppress the implicit declaration of a copy constructor.
Douglas Gregor507eb872009-12-22 00:34:07 +0000300 if (ConDecl->isCopyConstructor()) {
John McCall67da35c2010-02-04 22:26:26 +0000301 data().UserDeclaredCopyConstructor = true;
Douglas Gregora6d69502010-07-02 23:41:54 +0000302 data().DeclaredCopyConstructor = true;
303
Douglas Gregor8a273912009-07-22 18:25:24 +0000304 // C++ [class.copy]p6:
305 // A copy constructor is trivial if it is implicitly declared.
306 // FIXME: C++0x: don't do this for "= default" copy constructors.
John McCall67da35c2010-02-04 22:26:26 +0000307 data().HasTrivialCopyConstructor = false;
Douglas Gregora6d69502010-07-02 23:41:54 +0000308
Douglas Gregor8a273912009-07-22 18:25:24 +0000309 }
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000310}
311
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000312void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
313 CXXMethodDecl *OpDecl) {
314 // We're interested specifically in copy assignment operators.
John McCall9dd450b2009-09-21 23:43:11 +0000315 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000316 assert(FnType && "Overloaded operator has no proto function type.");
317 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregora14b43b2009-10-13 23:45:19 +0000318
319 // Copy assignment operators must be non-templates.
320 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
321 return;
322
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000323 QualType ArgType = FnType->getArgType(0);
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000324 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000325 ArgType = Ref->getPointeeType();
326
327 ArgType = ArgType.getUnqualifiedType();
328 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
329 const_cast<CXXRecordDecl*>(this)));
330
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000331 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000332 return;
333
334 // This is a copy assignment operator.
Eli Friedman01cad4c2009-11-07 00:02:45 +0000335 // Note on the decl that it is a copy assignment operator.
336 OpDecl->setCopyAssignment(true);
337
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000338 // Suppress the implicit declaration of a copy constructor.
John McCall67da35c2010-02-04 22:26:26 +0000339 data().UserDeclaredCopyAssignment = true;
Douglas Gregor330b9cf2010-07-02 21:50:04 +0000340 data().DeclaredCopyAssignment = true;
341
Douglas Gregor8a273912009-07-22 18:25:24 +0000342 // C++ [class.copy]p11:
343 // A copy assignment operator is trivial if it is implicitly declared.
344 // FIXME: C++0x: don't do this for "= default" copy operators.
John McCall67da35c2010-02-04 22:26:26 +0000345 data().HasTrivialCopyAssignment = false;
Douglas Gregor8a273912009-07-22 18:25:24 +0000346
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000347 // C++ [class]p4:
348 // A POD-struct is an aggregate class that [...] has no user-defined copy
349 // assignment operator [...].
John McCall67da35c2010-02-04 22:26:26 +0000350 data().PlainOldData = false;
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000351}
352
John McCall1e3a1a72010-03-15 09:07:48 +0000353static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
354 QualType T;
John McCallda4458e2010-03-31 01:36:47 +0000355 if (isa<UsingShadowDecl>(Conv))
356 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCall1e3a1a72010-03-15 09:07:48 +0000357 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
358 T = ConvTemp->getTemplatedDecl()->getResultType();
359 else
360 T = cast<CXXConversionDecl>(Conv)->getConversionType();
361 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +0000362}
363
John McCall1e3a1a72010-03-15 09:07:48 +0000364/// Collect the visible conversions of a base class.
365///
366/// \param Base a base class of the class we're considering
367/// \param InVirtual whether this base class is a virtual base (or a base
368/// of a virtual base)
369/// \param Access the access along the inheritance path to this base
370/// \param ParentHiddenTypes the conversions provided by the inheritors
371/// of this base
372/// \param Output the set to which to add conversions from non-virtual bases
373/// \param VOutput the set to which to add conversions from virtual bases
374/// \param HiddenVBaseCs the set of conversions which were hidden in a
375/// virtual base along some inheritance path
376static void CollectVisibleConversions(ASTContext &Context,
377 CXXRecordDecl *Record,
378 bool InVirtual,
379 AccessSpecifier Access,
380 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
381 UnresolvedSetImpl &Output,
382 UnresolvedSetImpl &VOutput,
383 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
384 // The set of types which have conversions in this class or its
385 // subclasses. As an optimization, we don't copy the derived set
386 // unless it might change.
387 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
388 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
389
390 // Collect the direct conversions and figure out which conversions
391 // will be hidden in the subclasses.
392 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
393 if (!Cs.empty()) {
394 HiddenTypesBuffer = ParentHiddenTypes;
395 HiddenTypes = &HiddenTypesBuffer;
396
397 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
398 bool Hidden =
399 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
400
401 // If this conversion is hidden and we're in a virtual base,
402 // remember that it's hidden along some inheritance path.
403 if (Hidden && InVirtual)
404 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
405
406 // If this conversion isn't hidden, add it to the appropriate output.
407 else if (!Hidden) {
408 AccessSpecifier IAccess
409 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
410
411 if (InVirtual)
412 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000413 else
John McCall1e3a1a72010-03-15 09:07:48 +0000414 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000415 }
416 }
417 }
Sebastian Redl1054fae2009-10-25 17:03:50 +0000418
John McCall1e3a1a72010-03-15 09:07:48 +0000419 // Collect information recursively from any base classes.
420 for (CXXRecordDecl::base_class_iterator
421 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
422 const RecordType *RT = I->getType()->getAs<RecordType>();
423 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +0000424
John McCall1e3a1a72010-03-15 09:07:48 +0000425 AccessSpecifier BaseAccess
426 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
427 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +0000428
John McCall1e3a1a72010-03-15 09:07:48 +0000429 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
430 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
431 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000432 }
John McCall1e3a1a72010-03-15 09:07:48 +0000433}
Sebastian Redl1054fae2009-10-25 17:03:50 +0000434
John McCall1e3a1a72010-03-15 09:07:48 +0000435/// Collect the visible conversions of a class.
436///
437/// This would be extremely straightforward if it weren't for virtual
438/// bases. It might be worth special-casing that, really.
439static void CollectVisibleConversions(ASTContext &Context,
440 CXXRecordDecl *Record,
441 UnresolvedSetImpl &Output) {
442 // The collection of all conversions in virtual bases that we've
443 // found. These will be added to the output as long as they don't
444 // appear in the hidden-conversions set.
445 UnresolvedSet<8> VBaseCs;
446
447 // The set of conversions in virtual bases that we've determined to
448 // be hidden.
449 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
450
451 // The set of types hidden by classes derived from this one.
452 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
453
454 // Go ahead and collect the direct conversions and add them to the
455 // hidden-types set.
456 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
457 Output.append(Cs.begin(), Cs.end());
458 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
459 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
460
461 // Recursively collect conversions from base classes.
462 for (CXXRecordDecl::base_class_iterator
463 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
464 const RecordType *RT = I->getType()->getAs<RecordType>();
465 if (!RT) continue;
466
467 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
468 I->isVirtual(), I->getAccessSpecifier(),
469 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
470 }
471
472 // Add any unhidden conversions provided by virtual bases.
473 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
474 I != E; ++I) {
475 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
476 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000477 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000478}
479
480/// getVisibleConversionFunctions - get all conversion functions visible
481/// in current class; including conversion function templates.
John McCallad371252010-01-20 00:46:10 +0000482const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000483 // If root class, all conversions are visible.
484 if (bases_begin() == bases_end())
John McCall67da35c2010-02-04 22:26:26 +0000485 return &data().Conversions;
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000486 // If visible conversion list is already evaluated, return it.
John McCall67da35c2010-02-04 22:26:26 +0000487 if (data().ComputedVisibleConversions)
488 return &data().VisibleConversions;
John McCall1e3a1a72010-03-15 09:07:48 +0000489 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall67da35c2010-02-04 22:26:26 +0000490 data().ComputedVisibleConversions = true;
491 return &data().VisibleConversions;
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000492}
493
John McCallda4458e2010-03-31 01:36:47 +0000494#ifndef NDEBUG
495void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCall1e3a1a72010-03-15 09:07:48 +0000496 assert(ConvDecl->getDeclContext() == this &&
497 "conversion function does not belong to this record");
498
John McCallda4458e2010-03-31 01:36:47 +0000499 ConvDecl = ConvDecl->getUnderlyingDecl();
500 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
501 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
502 } else {
503 assert(isa<CXXConversionDecl>(ConvDecl));
504 }
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000505}
John McCallda4458e2010-03-31 01:36:47 +0000506#endif
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000507
John McCallda4458e2010-03-31 01:36:47 +0000508void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
509 // This operation is O(N) but extremely rare. Sema only uses it to
510 // remove UsingShadowDecls in a class that were followed by a direct
511 // declaration, e.g.:
512 // class A : B {
513 // using B::operator int;
514 // operator int();
515 // };
516 // This is uncommon by itself and even more uncommon in conjunction
517 // with sufficiently large numbers of directly-declared conversions
518 // that asymptotic behavior matters.
519
520 UnresolvedSetImpl &Convs = *getConversionFunctions();
521 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
522 if (Convs[I].getDecl() == ConvDecl) {
523 Convs.erase(I);
524 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
525 && "conversion was found multiple times in unresolved set");
526 return;
527 }
528 }
529
530 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +0000531}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000532
Fariborz Jahanian6dfc1972009-12-03 18:44:40 +0000533void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
534 Method->setVirtualAsWritten(true);
535 setAggregate(false);
536 setPOD(false);
537 setEmpty(false);
538 setPolymorphic(true);
539 setHasTrivialConstructor(false);
540 setHasTrivialCopyConstructor(false);
541 setHasTrivialCopyAssignment(false);
542}
543
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000544CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000545 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000546 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
547
548 return 0;
549}
550
Douglas Gregor06db9f52009-10-12 20:18:28 +0000551MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
552 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
553}
554
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000555void
556CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
557 TemplateSpecializationKind TSK) {
558 assert(TemplateOrInstantiation.isNull() &&
559 "Previous template or instantiation?");
560 assert(!isa<ClassTemplateSpecializationDecl>(this));
561 TemplateOrInstantiation
562 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
563}
564
Anders Carlsson27cfc6e2009-12-07 06:33:48 +0000565TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
566 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000567 = dyn_cast<ClassTemplateSpecializationDecl>(this))
568 return Spec->getSpecializationKind();
569
Douglas Gregor06db9f52009-10-12 20:18:28 +0000570 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000571 return MSInfo->getTemplateSpecializationKind();
572
573 return TSK_Undeclared;
574}
575
576void
577CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
578 if (ClassTemplateSpecializationDecl *Spec
579 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
580 Spec->setSpecializationKind(TSK);
581 return;
582 }
583
Douglas Gregor06db9f52009-10-12 20:18:28 +0000584 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000585 MSInfo->setTemplateSpecializationKind(TSK);
586 return;
587 }
588
589 assert(false && "Not a class template or member class specialization");
590}
591
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000592CXXConstructorDecl *
Douglas Gregor6d880b12010-07-01 22:31:05 +0000593CXXRecordDecl::getDefaultConstructor() {
594 ASTContext &Context = getASTContext();
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000595 QualType ClassType = Context.getTypeDeclType(this);
596 DeclarationName ConstructorName
597 = Context.DeclarationNames.getCXXConstructorName(
598 Context.getCanonicalType(ClassType.getUnqualifiedType()));
Mike Stump11289f42009-09-09 15:08:12 +0000599
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000600 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000601 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName);
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000602 Con != ConEnd; ++Con) {
Douglas Gregor2d2282e2009-09-04 14:46:39 +0000603 // FIXME: In C++0x, a constructor template can be a default constructor.
604 if (isa<FunctionTemplateDecl>(*Con))
605 continue;
606
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000607 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
608 if (Constructor->isDefaultConstructor())
609 return Constructor;
610 }
611 return 0;
612}
613
Douglas Gregorbac74902010-07-01 14:13:13 +0000614CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
615 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +0000616 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +0000617
618 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +0000619 = Context.DeclarationNames.getCXXDestructorName(
620 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +0000621
John McCallf8ff7b92010-02-23 00:48:20 +0000622 DeclContext::lookup_const_iterator I, E;
Mike Stump11289f42009-09-09 15:08:12 +0000623 llvm::tie(I, E) = lookup(Name);
Anders Carlsson0a637412009-05-29 21:03:38 +0000624 assert(I != E && "Did not find a destructor!");
Mike Stump11289f42009-09-09 15:08:12 +0000625
Anders Carlssonf98849e2009-12-02 17:15:43 +0000626 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson0a637412009-05-29 21:03:38 +0000627 assert(++I == E && "Found more than one destructor!");
Mike Stump11289f42009-09-09 15:08:12 +0000628
Anders Carlsson0a637412009-05-29 21:03:38 +0000629 return Dtor;
630}
631
Ted Kremenek21475702008-09-05 17:16:31 +0000632CXXMethodDecl *
633CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor92751d42008-11-17 22:58:34 +0000634 SourceLocation L, DeclarationName N,
John McCallbcd03502009-12-07 02:54:59 +0000635 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000636 bool isStatic, StorageClass SCAsWritten, bool isInline) {
John McCallbcd03502009-12-07 02:54:59 +0000637 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000638 isStatic, SCAsWritten, isInline);
Ted Kremenek21475702008-09-05 17:16:31 +0000639}
640
Douglas Gregorbb3e12f2009-09-29 18:16:17 +0000641bool CXXMethodDecl::isUsualDeallocationFunction() const {
642 if (getOverloadedOperator() != OO_Delete &&
643 getOverloadedOperator() != OO_Array_Delete)
644 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +0000645
646 // C++ [basic.stc.dynamic.deallocation]p2:
647 // A template instance is never a usual deallocation function,
648 // regardless of its signature.
649 if (getPrimaryTemplate())
650 return false;
651
Douglas Gregorbb3e12f2009-09-29 18:16:17 +0000652 // C++ [basic.stc.dynamic.deallocation]p2:
653 // If a class T has a member deallocation function named operator delete
654 // with exactly one parameter, then that function is a usual (non-placement)
655 // deallocation function. [...]
656 if (getNumParams() == 1)
657 return true;
658
659 // C++ [basic.stc.dynamic.deallocation]p2:
660 // [...] If class T does not declare such an operator delete but does
661 // declare a member deallocation function named operator delete with
662 // exactly two parameters, the second of which has type std::size_t (18.1),
663 // then this function is a usual deallocation function.
664 ASTContext &Context = getASTContext();
665 if (getNumParams() != 2 ||
Chandler Carruth75cc3592010-02-08 18:54:05 +0000666 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
667 Context.getSizeType()))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +0000668 return false;
669
670 // This function is a usual deallocation function if there are no
671 // single-parameter deallocation functions of the same kind.
672 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
673 R.first != R.second; ++R.first) {
674 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
675 if (FD->getNumParams() == 1)
676 return false;
677 }
678
679 return true;
680}
681
Douglas Gregorb139cd52010-05-01 20:49:11 +0000682bool CXXMethodDecl::isCopyAssignmentOperator() const {
683 // C++0x [class.copy]p19:
684 // A user-declared copy assignment operator X::operator= is a non-static
685 // non-template member function of class X with exactly one parameter of
686 // type X, X&, const X&, volatile X& or const volatile X&.
687 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
688 /*non-static*/ isStatic() ||
689 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
690 /*exactly one parameter*/getNumParams() != 1)
691 return false;
692
693 QualType ParamType = getParamDecl(0)->getType();
694 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
695 ParamType = Ref->getPointeeType();
696
697 ASTContext &Context = getASTContext();
698 QualType ClassType
699 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
700 return Context.hasSameUnqualifiedType(ClassType, ParamType);
701}
702
Anders Carlsson36d87e12009-05-16 23:58:37 +0000703void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +0000704 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +0000705 assert(!MD->getParent()->isDependentContext() &&
706 "Can't add an overridden method to a class template!");
707
Douglas Gregor832940b2010-03-02 23:58:15 +0000708 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +0000709}
710
711CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor832940b2010-03-02 23:58:15 +0000712 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +0000713}
714
715CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor832940b2010-03-02 23:58:15 +0000716 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +0000717}
718
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +0000719unsigned CXXMethodDecl::size_overridden_methods() const {
720 return getASTContext().overridden_methods_size(this);
721}
722
Ted Kremenek21475702008-09-05 17:16:31 +0000723QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +0000724 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
725 // If the member function is declared const, the type of this is const X*,
726 // if the member function is declared volatile, the type of this is
727 // volatile X*, and if the member function is declared const volatile,
728 // the type of this is const volatile X*.
729
Ted Kremenek21475702008-09-05 17:16:31 +0000730 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +0000731
John McCalle78aac42010-03-10 03:28:59 +0000732 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +0000733 ClassTy = C.getQualifiedType(ClassTy,
734 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +0000735 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +0000736}
737
Eli Friedman71a26d82009-12-06 20:50:05 +0000738bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +0000739 // If this function is a template instantiation, look at the template from
740 // which it was instantiated.
741 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
742 if (!CheckFn)
743 CheckFn = this;
744
Eli Friedman71a26d82009-12-06 20:50:05 +0000745 const FunctionDecl *fn;
Douglas Gregora318efd2010-01-05 19:06:31 +0000746 return CheckFn->getBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +0000747}
748
Douglas Gregore8381c02008-11-05 04:29:56 +0000749CXXBaseOrMemberInitializer::
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000750CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson1c0f8bb2010-04-12 00:51:03 +0000751 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000752 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnara341d7832010-05-26 18:09:23 +0000753 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
754 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
755 SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000756{
Douglas Gregore8381c02008-11-05 04:29:56 +0000757}
758
759CXXBaseOrMemberInitializer::
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000760CXXBaseOrMemberInitializer(ASTContext &Context,
761 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000762 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnara341d7832010-05-26 18:09:23 +0000763 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
764 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
765 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000766{
Douglas Gregore8381c02008-11-05 04:29:56 +0000767}
768
Douglas Gregor94f9a482010-05-05 05:51:00 +0000769CXXBaseOrMemberInitializer::
770CXXBaseOrMemberInitializer(ASTContext &Context,
771 FieldDecl *Member, SourceLocation MemberLoc,
772 SourceLocation L, Expr *Init, SourceLocation R,
773 VarDecl **Indices,
774 unsigned NumIndices)
775 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnara341d7832010-05-26 18:09:23 +0000776 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
777 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregor94f9a482010-05-05 05:51:00 +0000778{
779 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
780 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
781}
782
783CXXBaseOrMemberInitializer *
784CXXBaseOrMemberInitializer::Create(ASTContext &Context,
785 FieldDecl *Member,
786 SourceLocation MemberLoc,
787 SourceLocation L,
788 Expr *Init,
789 SourceLocation R,
790 VarDecl **Indices,
791 unsigned NumIndices) {
792 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
793 sizeof(VarDecl *) * NumIndices,
794 llvm::alignof<CXXBaseOrMemberInitializer>());
795 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
796 L, Init, R, Indices, NumIndices);
797}
798
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000799void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) {
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000800 if (Init)
801 Init->Destroy(Context);
Douglas Gregor94f9a482010-05-05 05:51:00 +0000802 // FIXME: Destroy indices
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000803 this->~CXXBaseOrMemberInitializer();
804}
805
806TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
807 if (isBaseInitializer())
John McCallbcd03502009-12-07 02:54:59 +0000808 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000809 else
810 return TypeLoc();
811}
812
813Type *CXXBaseOrMemberInitializer::getBaseClass() {
814 if (isBaseInitializer())
John McCallbcd03502009-12-07 02:54:59 +0000815 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000816 else
817 return 0;
818}
819
820const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
821 if (isBaseInitializer())
John McCallbcd03502009-12-07 02:54:59 +0000822 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000823 else
824 return 0;
825}
826
827SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
828 if (isMemberInitializer())
829 return getMemberLocation();
830
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000831 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000832}
833
834SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
835 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +0000836}
837
Douglas Gregor61956c42008-10-31 09:07:45 +0000838CXXConstructorDecl *
Chris Lattnerca025db2010-05-07 21:43:38 +0000839CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
840 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationName(),
841 QualType(), 0, false, false, false);
842}
843
844CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +0000845CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor77324f32008-11-17 14:58:09 +0000846 SourceLocation L, DeclarationName N,
John McCallbcd03502009-12-07 02:54:59 +0000847 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000848 bool isExplicit,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000849 bool isInline,
850 bool isImplicitlyDeclared) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000851 assert(N.getNameKind() == DeclarationName::CXXConstructorName &&
852 "Name must refer to a constructor");
Douglas Gregorc4df4072010-04-19 22:54:31 +0000853 return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit,
854 isInline, isImplicitlyDeclared);
Douglas Gregor61956c42008-10-31 09:07:45 +0000855}
856
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000857bool CXXConstructorDecl::isDefaultConstructor() const {
858 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000859 // A default constructor for a class X is a constructor of class
860 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000861 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +0000862 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000863}
864
Mike Stump11289f42009-09-09 15:08:12 +0000865bool
Douglas Gregor507eb872009-12-22 00:34:07 +0000866CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000867 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000868 // A non-template constructor for class X is a copy constructor
869 // if its first parameter is of type X&, const X&, volatile X& or
870 // const volatile X&, and either there are no other parameters
871 // or else all other parameters have default arguments (8.3.6).
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000872 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +0000873 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorff7028a2009-11-13 23:59:09 +0000874 (getPrimaryTemplate() != 0) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +0000875 (getDescribedFunctionTemplate() != 0))
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000876 return false;
877
878 const ParmVarDecl *Param = getParamDecl(0);
879
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000880 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorff7028a2009-11-13 23:59:09 +0000881 const LValueReferenceType *ParamRefType =
882 Param->getType()->getAs<LValueReferenceType>();
883 if (!ParamRefType)
884 return false;
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000885
Douglas Gregorff7028a2009-11-13 23:59:09 +0000886 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +0000887 ASTContext &Context = getASTContext();
888
Douglas Gregorff7028a2009-11-13 23:59:09 +0000889 CanQualType PointeeType
890 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +0000891 CanQualType ClassTy
892 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000893 if (PointeeType.getUnqualifiedType() != ClassTy)
894 return false;
895
John McCall8ccfcb52009-09-24 19:53:00 +0000896 // FIXME: other qualifiers?
897
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000898 // We have a copy constructor.
899 TypeQuals = PointeeType.getCVRQualifiers();
900 return true;
901}
902
Anders Carlssond20e7952009-08-28 16:57:08 +0000903bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000904 // C++ [class.conv.ctor]p1:
905 // A constructor declared without the function-specifier explicit
906 // that can be called with a single parameter specifies a
907 // conversion from the type of its first parameter to the type of
908 // its class. Such a constructor is called a converting
909 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +0000910 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000911 return false;
912
Mike Stump11289f42009-09-09 15:08:12 +0000913 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +0000914 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000915 (getNumParams() == 1) ||
Anders Carlsson85446472009-06-06 04:14:07 +0000916 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000917}
Douglas Gregor61956c42008-10-31 09:07:45 +0000918
Douglas Gregorffe14e32009-11-14 01:20:54 +0000919bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
920 if ((getNumParams() < 1) ||
921 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
922 (getPrimaryTemplate() == 0) ||
923 (getDescribedFunctionTemplate() != 0))
924 return false;
925
926 const ParmVarDecl *Param = getParamDecl(0);
927
928 ASTContext &Context = getASTContext();
929 CanQualType ParamType = Context.getCanonicalType(Param->getType());
930
931 // Strip off the lvalue reference, if any.
932 if (CanQual<LValueReferenceType> ParamRefType
933 = ParamType->getAs<LValueReferenceType>())
934 ParamType = ParamRefType->getPointeeType();
935
936
937 // Is it the same as our our class type?
938 CanQualType ClassTy
939 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
940 if (ParamType.getUnqualifiedType() != ClassTy)
941 return false;
942
943 return true;
944}
945
Douglas Gregor831c93f2008-11-05 20:51:48 +0000946CXXDestructorDecl *
Chris Lattnerca025db2010-05-07 21:43:38 +0000947CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
948 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationName(),
949 QualType(), false, false);
950}
951
952CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +0000953CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor77324f32008-11-17 14:58:09 +0000954 SourceLocation L, DeclarationName N,
Mike Stump11289f42009-09-09 15:08:12 +0000955 QualType T, bool isInline,
Douglas Gregor831c93f2008-11-05 20:51:48 +0000956 bool isImplicitlyDeclared) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000957 assert(N.getNameKind() == DeclarationName::CXXDestructorName &&
958 "Name must refer to a destructor");
Douglas Gregorc4df4072010-04-19 22:54:31 +0000959 return new (C) CXXDestructorDecl(RD, L, N, T, isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000960}
961
Fariborz Jahanian52337412009-07-01 21:05:43 +0000962void
Fariborz Jahanian5c6af0a2009-07-01 23:35:25 +0000963CXXConstructorDecl::Destroy(ASTContext& C) {
964 C.Deallocate(BaseOrMemberInitializers);
Fariborz Jahanian003a8802009-07-07 16:24:08 +0000965 CXXMethodDecl::Destroy(C);
Fariborz Jahanian5c6af0a2009-07-01 23:35:25 +0000966}
967
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000968CXXConversionDecl *
Chris Lattnerca025db2010-05-07 21:43:38 +0000969CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
970 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationName(),
971 QualType(), 0, false, false);
972}
973
974CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000975CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Douglas Gregor77324f32008-11-17 14:58:09 +0000976 SourceLocation L, DeclarationName N,
John McCallbcd03502009-12-07 02:54:59 +0000977 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000978 bool isInline, bool isExplicit) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000979 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName &&
980 "Name must refer to a conversion function");
John McCallbcd03502009-12-07 02:54:59 +0000981 return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit);
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000982}
983
Chris Lattnerb8c18fa2008-11-04 16:51:42 +0000984LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +0000985 DeclContext *DC,
Chris Lattnerb8c18fa2008-11-04 16:51:42 +0000986 SourceLocation L,
Douglas Gregor07665a62009-01-05 19:45:36 +0000987 LanguageIDs Lang, bool Braces) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000988 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000989}
Douglas Gregor889ceb72009-02-03 19:21:40 +0000990
991UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
992 SourceLocation L,
993 SourceLocation NamespaceLoc,
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000994 SourceRange QualifierRange,
995 NestedNameSpecifier *Qualifier,
Douglas Gregor889ceb72009-02-03 19:21:40 +0000996 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +0000997 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +0000998 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +0000999 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1000 Used = NS->getOriginalNamespace();
Mike Stump11289f42009-09-09 15:08:12 +00001001 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +00001002 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00001003}
1004
Sebastian Redla6602e92009-11-23 15:34:23 +00001005NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1006 if (NamespaceAliasDecl *NA =
1007 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1008 return NA->getNamespace();
1009 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1010}
1011
Chris Lattnerca025db2010-05-07 21:43:38 +00001012void UsingDirectiveDecl::setNominatedNamespace(NamedDecl* ND) {
1013 assert((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) &&
1014 "expected a NamespaceDecl or NamespaceAliasDecl");
1015 NominatedNamespace = ND;
1016}
1017
Mike Stump11289f42009-09-09 15:08:12 +00001018NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
1019 SourceLocation L,
1020 SourceLocation AliasLoc,
1021 IdentifierInfo *Alias,
Douglas Gregor18231932009-05-30 06:48:27 +00001022 SourceRange QualifierRange,
1023 NestedNameSpecifier *Qualifier,
Mike Stump11289f42009-09-09 15:08:12 +00001024 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00001025 NamedDecl *Namespace) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001026 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1027 Namespace = NS->getOriginalNamespace();
Mike Stump11289f42009-09-09 15:08:12 +00001028 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
Douglas Gregor18231932009-05-30 06:48:27 +00001029 Qualifier, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00001030}
1031
Douglas Gregorfec52632009-06-20 00:51:54 +00001032UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
John McCall3f746822009-11-17 05:59:44 +00001033 SourceLocation L, SourceRange NNR, SourceLocation UL,
1034 NestedNameSpecifier* TargetNNS, DeclarationName Name,
1035 bool IsTypeNameArg) {
1036 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg);
Douglas Gregorfec52632009-06-20 00:51:54 +00001037}
1038
John McCalle61f2ba2009-11-18 02:36:19 +00001039UnresolvedUsingValueDecl *
1040UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1041 SourceLocation UsingLoc,
1042 SourceRange TargetNNR,
1043 NestedNameSpecifier *TargetNNS,
1044 SourceLocation TargetNameLoc,
1045 DeclarationName TargetName) {
1046 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
1047 TargetNNR, TargetNNS,
1048 TargetNameLoc, TargetName);
1049}
1050
1051UnresolvedUsingTypenameDecl *
1052UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1053 SourceLocation UsingLoc,
1054 SourceLocation TypenameLoc,
1055 SourceRange TargetNNR,
1056 NestedNameSpecifier *TargetNNS,
1057 SourceLocation TargetNameLoc,
1058 DeclarationName TargetName) {
1059 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1060 TargetNNR, TargetNNS,
1061 TargetNameLoc,
1062 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +00001063}
1064
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00001065StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1066 SourceLocation L, Expr *AssertExpr,
1067 StringLiteral *Message) {
1068 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1069}
1070
1071void StaticAssertDecl::Destroy(ASTContext& C) {
1072 AssertExpr->Destroy(C);
1073 Message->Destroy(C);
John McCall3e11ebe2010-03-15 10:12:16 +00001074 Decl::Destroy(C);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00001075}
1076
1077StaticAssertDecl::~StaticAssertDecl() {
1078}
1079
Anders Carlsson6750d162009-03-26 23:46:50 +00001080static const char *getAccessName(AccessSpecifier AS) {
1081 switch (AS) {
1082 default:
1083 case AS_none:
1084 assert("Invalid access specifier!");
1085 return 0;
1086 case AS_public:
1087 return "public";
1088 case AS_private:
1089 return "private";
1090 case AS_protected:
1091 return "protected";
1092 }
1093}
1094
1095const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1096 AccessSpecifier AS) {
1097 return DB << getAccessName(AS);
1098}
1099
1100