blob: ffdb98d7516bc8a67d96f97ae5e22bbd4e5f6161 [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
Mike Stump11289f42009-09-09 15:08:12 +000068void
Douglas Gregor4a62bdf2010-02-11 01:30:34 +000069CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor9d6290b2008-10-23 18:13:27 +000070 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +000071 ASTContext &C = getASTContext();
72
Mike Stump11289f42009-09-09 15:08:12 +000073 // C++ [dcl.init.aggr]p1:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +000074 // An aggregate is an array or a class (clause 9) with [...]
75 // no base classes [...].
John McCall67da35c2010-02-04 22:26:26 +000076 data().Aggregate = false;
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +000077
John McCall67da35c2010-02-04 22:26:26 +000078 if (data().Bases)
79 C.Deallocate(data().Bases);
Mike Stump11289f42009-09-09 15:08:12 +000080
Anders Carlssonabb20e62010-03-29 05:13:12 +000081 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +000082 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlssonabb20e62010-03-29 05:13:12 +000083
84 // The virtual bases of this class.
85 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +000086
John McCall67da35c2010-02-04 22:26:26 +000087 data().Bases = new(C) CXXBaseSpecifier [NumBases];
88 data().NumBases = NumBases;
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +000089 for (unsigned i = 0; i < NumBases; ++i) {
John McCall67da35c2010-02-04 22:26:26 +000090 data().Bases[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +000091 // Keep track of inherited vbases for this base class.
92 const CXXBaseSpecifier *Base = Bases[i];
93 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +000094 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +000095 if (BaseType->isDependentType())
96 continue;
97 CXXRecordDecl *BaseClassDecl
Ted Kremenekc23c7e62009-07-29 21:53:49 +000098 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +000099
100 // Now go through all virtual bases of this base and add them.
Mike Stump11289f42009-09-09 15:08:12 +0000101 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000102 BaseClassDecl->vbases_begin(),
103 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000104 // Add this base if it's not already in the list.
Anders Carlssone47380f2010-03-29 19:49:09 +0000105 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlssonabb20e62010-03-29 05:13:12 +0000106 VBases.push_back(VBase);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000107 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000108
109 if (Base->isVirtual()) {
110 // Add this base if it's not already in the list.
Anders Carlssone47380f2010-03-29 19:49:09 +0000111 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlssonabb20e62010-03-29 05:13:12 +0000112 VBases.push_back(Base);
113 }
114
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000115 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000116
117 if (VBases.empty())
118 return;
119
120 // Create base specifier for any direct or indirect virtual bases.
121 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
122 data().NumVBases = VBases.size();
123 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky19b9f952010-07-26 16:56:01 +0000124 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
125
Anders Carlssonabb20e62010-03-29 05:13:12 +0000126 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky19b9f952010-07-26 16:56:01 +0000127 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlssonabb20e62010-03-29 05:13:12 +0000128 continue;
129
Nick Lewycky19b9f952010-07-26 16:56:01 +0000130 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
131 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000132
133 data().VBases[I] =
134 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000135 VBaseClassDecl->getTagKind() == TTK_Class,
Nick Lewycky19b9f952010-07-26 16:56:01 +0000136 VBases[I]->getAccessSpecifier(), VBaseTypeInfo);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000137 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000138}
139
Douglas Gregord2e6a452010-01-14 17:47:39 +0000140/// Callback function for CXXRecordDecl::forallBases that acknowledges
141/// that it saw a base class.
142static bool SawBase(const CXXRecordDecl *, void *) {
143 return true;
144}
145
146bool CXXRecordDecl::hasAnyDependentBases() const {
147 if (!isDependentContext())
148 return false;
149
150 return !forallBases(SawBase, 0);
151}
152
Douglas Gregor05379422008-11-03 17:51:48 +0000153bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall8ccfcb52009-09-24 19:53:00 +0000154 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000155}
156
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000157/// \brief Perform a simplistic form of overload resolution that only considers
158/// cv-qualifiers on a single parameter, and return the best overload candidate
159/// (if there is one).
160static CXXMethodDecl *
161GetBestOverloadCandidateSimple(
162 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
163 if (Cands.empty())
164 return 0;
165 if (Cands.size() == 1)
166 return Cands[0].first;
167
168 unsigned Best = 0, N = Cands.size();
169 for (unsigned I = 1; I != N; ++I)
170 if (Cands[Best].second.isSupersetOf(Cands[I].second))
171 Best = I;
172
173 for (unsigned I = 1; I != N; ++I)
174 if (Cands[Best].second.isSupersetOf(Cands[I].second))
175 return 0;
176
177 return Cands[Best].first;
178}
179
Mike Stump11289f42009-09-09 15:08:12 +0000180CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000181 unsigned TypeQuals) const{
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000182 QualType ClassType
183 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump11289f42009-09-09 15:08:12 +0000184 DeclarationName ConstructorName
Douglas Gregor1349b452008-12-15 21:24:18 +0000185 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000186 Context.getCanonicalType(ClassType));
187 unsigned FoundTQs;
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000188 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregor74a34442008-12-23 21:31:30 +0000189 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000190 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregor74a34442008-12-23 21:31:30 +0000191 Con != ConEnd; ++Con) {
Douglas Gregor2d2282e2009-09-04 14:46:39 +0000192 // C++ [class.copy]p2:
193 // A non-template constructor for class X is a copy constructor if [...]
194 if (isa<FunctionTemplateDecl>(*Con))
195 continue;
196
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000197 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
198 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall8ccfcb52009-09-24 19:53:00 +0000199 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
200 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000201 Found.push_back(std::make_pair(
202 const_cast<CXXConstructorDecl *>(Constructor),
203 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian477d2422009-06-22 23:34:40 +0000204 }
Douglas Gregor05379422008-11-03 17:51:48 +0000205 }
Douglas Gregor8453ddb2010-07-01 20:59:04 +0000206
207 return cast_or_null<CXXConstructorDecl>(
208 GetBestOverloadCandidateSimple(Found));
Douglas Gregor05379422008-11-03 17:51:48 +0000209}
210
Douglas Gregor68e11362010-07-01 17:48:08 +0000211CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
212 ASTContext &Context = getASTContext();
213 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
214 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
215
216 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
217 DeclContext::lookup_const_iterator Op, OpEnd;
218 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
219 // C++ [class.copy]p9:
220 // A user-declared copy assignment operator is a non-static non-template
221 // member function of class X with exactly one parameter of type X, X&,
222 // const X&, volatile X& or const volatile X&.
223 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
224 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
225 continue;
226
227 const FunctionProtoType *FnType
228 = Method->getType()->getAs<FunctionProtoType>();
229 assert(FnType && "Overloaded operator has no prototype.");
230 // Don't assert on this; an invalid decl might have been left in the AST.
231 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
232 continue;
233
234 QualType ArgType = FnType->getArgType(0);
235 Qualifiers Quals;
236 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
237 ArgType = Ref->getPointeeType();
238 // If we have a const argument and we have a reference to a non-const,
239 // this function does not match.
240 if (ArgIsConst && !ArgType.isConstQualified())
241 continue;
242
243 Quals = ArgType.getQualifiers();
244 } else {
245 // By-value copy-assignment operators are treated like const X&
246 // copy-assignment operators.
247 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
248 }
249
250 if (!Context.hasSameUnqualifiedType(ArgType, Class))
251 continue;
252
253 // Save this copy-assignment operator. It might be "the one".
254 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
255 }
256
257 // Use a simplistic form of overload resolution to find the candidate.
258 return GetBestOverloadCandidateSimple(Found);
259}
260
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000261void
Douglas Gregord30e79f2010-09-27 21:17:54 +0000262CXXRecordDecl::addedConstructor(CXXConstructorDecl *Constructor) {
263 // Ignore friends.
264 if (Constructor->getFriendObjectKind())
265 return;
266
267 if (Constructor->isImplicit()) {
268 // If this is the implicit default constructor, note that we have now
269 // declared it.
270 if (Constructor->isDefaultConstructor())
271 data().DeclaredDefaultConstructor = true;
272 // If this is the implicit copy constructor, note that we have now
273 // declared it.
274 else if (Constructor->isCopyConstructor())
275 data().DeclaredCopyConstructor = true;
276
277 // Nothing else to do for implicitly-declared constructors.
278 return;
279 }
280
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000281 // Note that we have a user-declared constructor.
John McCall67da35c2010-02-04 22:26:26 +0000282 data().UserDeclaredConstructor = true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000283
Douglas Gregor9672f922010-07-03 00:47:00 +0000284 // Note that we have no need of an implicitly-declared default constructor.
285 data().DeclaredDefaultConstructor = true;
286
Mike Stump11289f42009-09-09 15:08:12 +0000287 // C++ [dcl.init.aggr]p1:
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000288 // An aggregate is an array or a class (clause 9) with no
289 // user-declared constructors (12.1) [...].
John McCall67da35c2010-02-04 22:26:26 +0000290 data().Aggregate = false;
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000291
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000292 // C++ [class]p4:
293 // A POD-struct is an aggregate class [...]
John McCall67da35c2010-02-04 22:26:26 +0000294 data().PlainOldData = false;
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000295
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000296 // C++ [class.ctor]p5:
297 // A constructor is trivial if it is an implicitly-declared default
298 // constructor.
Douglas Gregor8a273912009-07-22 18:25:24 +0000299 // FIXME: C++0x: don't do this for "= default" default constructors.
John McCall67da35c2010-02-04 22:26:26 +0000300 data().HasTrivialConstructor = false;
Mike Stump11289f42009-09-09 15:08:12 +0000301
Fariborz Jahanianb1743252009-06-17 22:44:31 +0000302 // Note when we have a user-declared copy constructor, which will
303 // suppress the implicit declaration of a copy constructor.
Douglas Gregord30e79f2010-09-27 21:17:54 +0000304 if (!Constructor->getDescribedFunctionTemplate() &&
305 Constructor->isCopyConstructor()) {
John McCall67da35c2010-02-04 22:26:26 +0000306 data().UserDeclaredCopyConstructor = true;
Douglas Gregora6d69502010-07-02 23:41:54 +0000307 data().DeclaredCopyConstructor = true;
308
Douglas Gregor8a273912009-07-22 18:25:24 +0000309 // C++ [class.copy]p6:
310 // A copy constructor is trivial if it is implicitly declared.
311 // FIXME: C++0x: don't do this for "= default" copy constructors.
John McCall67da35c2010-02-04 22:26:26 +0000312 data().HasTrivialCopyConstructor = false;
Douglas Gregor8a273912009-07-22 18:25:24 +0000313 }
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000314}
315
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000316void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context,
317 CXXMethodDecl *OpDecl) {
318 // We're interested specifically in copy assignment operators.
John McCall9dd450b2009-09-21 23:43:11 +0000319 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>();
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000320 assert(FnType && "Overloaded operator has no proto function type.");
321 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
Douglas Gregora14b43b2009-10-13 23:45:19 +0000322
323 // Copy assignment operators must be non-templates.
324 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate())
325 return;
326
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000327 QualType ArgType = FnType->getArgType(0);
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000328 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>())
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000329 ArgType = Ref->getPointeeType();
330
331 ArgType = ArgType.getUnqualifiedType();
332 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
333 const_cast<CXXRecordDecl*>(this)));
334
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000335 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000336 return;
337
338 // This is a copy assignment operator.
Eli Friedman01cad4c2009-11-07 00:02:45 +0000339 // Note on the decl that it is a copy assignment operator.
340 OpDecl->setCopyAssignment(true);
341
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000342 // Suppress the implicit declaration of a copy constructor.
John McCall67da35c2010-02-04 22:26:26 +0000343 data().UserDeclaredCopyAssignment = true;
Douglas Gregor330b9cf2010-07-02 21:50:04 +0000344 data().DeclaredCopyAssignment = true;
345
Douglas Gregor8a273912009-07-22 18:25:24 +0000346 // C++ [class.copy]p11:
347 // A copy assignment operator is trivial if it is implicitly declared.
348 // FIXME: C++0x: don't do this for "= default" copy operators.
John McCall67da35c2010-02-04 22:26:26 +0000349 data().HasTrivialCopyAssignment = false;
Douglas Gregor8a273912009-07-22 18:25:24 +0000350
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000351 // C++ [class]p4:
352 // A POD-struct is an aggregate class that [...] has no user-defined copy
353 // assignment operator [...].
John McCall67da35c2010-02-04 22:26:26 +0000354 data().PlainOldData = false;
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000355}
356
John McCall1e3a1a72010-03-15 09:07:48 +0000357static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
358 QualType T;
John McCallda4458e2010-03-31 01:36:47 +0000359 if (isa<UsingShadowDecl>(Conv))
360 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCall1e3a1a72010-03-15 09:07:48 +0000361 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
362 T = ConvTemp->getTemplatedDecl()->getResultType();
363 else
364 T = cast<CXXConversionDecl>(Conv)->getConversionType();
365 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +0000366}
367
John McCall1e3a1a72010-03-15 09:07:48 +0000368/// Collect the visible conversions of a base class.
369///
370/// \param Base a base class of the class we're considering
371/// \param InVirtual whether this base class is a virtual base (or a base
372/// of a virtual base)
373/// \param Access the access along the inheritance path to this base
374/// \param ParentHiddenTypes the conversions provided by the inheritors
375/// of this base
376/// \param Output the set to which to add conversions from non-virtual bases
377/// \param VOutput the set to which to add conversions from virtual bases
378/// \param HiddenVBaseCs the set of conversions which were hidden in a
379/// virtual base along some inheritance path
380static void CollectVisibleConversions(ASTContext &Context,
381 CXXRecordDecl *Record,
382 bool InVirtual,
383 AccessSpecifier Access,
384 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
385 UnresolvedSetImpl &Output,
386 UnresolvedSetImpl &VOutput,
387 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
388 // The set of types which have conversions in this class or its
389 // subclasses. As an optimization, we don't copy the derived set
390 // unless it might change.
391 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
392 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
393
394 // Collect the direct conversions and figure out which conversions
395 // will be hidden in the subclasses.
396 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
397 if (!Cs.empty()) {
398 HiddenTypesBuffer = ParentHiddenTypes;
399 HiddenTypes = &HiddenTypesBuffer;
400
401 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
402 bool Hidden =
403 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
404
405 // If this conversion is hidden and we're in a virtual base,
406 // remember that it's hidden along some inheritance path.
407 if (Hidden && InVirtual)
408 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
409
410 // If this conversion isn't hidden, add it to the appropriate output.
411 else if (!Hidden) {
412 AccessSpecifier IAccess
413 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
414
415 if (InVirtual)
416 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000417 else
John McCall1e3a1a72010-03-15 09:07:48 +0000418 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000419 }
420 }
421 }
Sebastian Redl1054fae2009-10-25 17:03:50 +0000422
John McCall1e3a1a72010-03-15 09:07:48 +0000423 // Collect information recursively from any base classes.
424 for (CXXRecordDecl::base_class_iterator
425 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
426 const RecordType *RT = I->getType()->getAs<RecordType>();
427 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +0000428
John McCall1e3a1a72010-03-15 09:07:48 +0000429 AccessSpecifier BaseAccess
430 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
431 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +0000432
John McCall1e3a1a72010-03-15 09:07:48 +0000433 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
434 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
435 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000436 }
John McCall1e3a1a72010-03-15 09:07:48 +0000437}
Sebastian Redl1054fae2009-10-25 17:03:50 +0000438
John McCall1e3a1a72010-03-15 09:07:48 +0000439/// Collect the visible conversions of a class.
440///
441/// This would be extremely straightforward if it weren't for virtual
442/// bases. It might be worth special-casing that, really.
443static void CollectVisibleConversions(ASTContext &Context,
444 CXXRecordDecl *Record,
445 UnresolvedSetImpl &Output) {
446 // The collection of all conversions in virtual bases that we've
447 // found. These will be added to the output as long as they don't
448 // appear in the hidden-conversions set.
449 UnresolvedSet<8> VBaseCs;
450
451 // The set of conversions in virtual bases that we've determined to
452 // be hidden.
453 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
454
455 // The set of types hidden by classes derived from this one.
456 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
457
458 // Go ahead and collect the direct conversions and add them to the
459 // hidden-types set.
460 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
461 Output.append(Cs.begin(), Cs.end());
462 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
463 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
464
465 // Recursively collect conversions from base classes.
466 for (CXXRecordDecl::base_class_iterator
467 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
468 const RecordType *RT = I->getType()->getAs<RecordType>();
469 if (!RT) continue;
470
471 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
472 I->isVirtual(), I->getAccessSpecifier(),
473 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
474 }
475
476 // Add any unhidden conversions provided by virtual bases.
477 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
478 I != E; ++I) {
479 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
480 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000481 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000482}
483
484/// getVisibleConversionFunctions - get all conversion functions visible
485/// in current class; including conversion function templates.
John McCallad371252010-01-20 00:46:10 +0000486const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000487 // If root class, all conversions are visible.
488 if (bases_begin() == bases_end())
John McCall67da35c2010-02-04 22:26:26 +0000489 return &data().Conversions;
Fariborz Jahanianb394f502009-09-12 18:26:03 +0000490 // If visible conversion list is already evaluated, return it.
John McCall67da35c2010-02-04 22:26:26 +0000491 if (data().ComputedVisibleConversions)
492 return &data().VisibleConversions;
John McCall1e3a1a72010-03-15 09:07:48 +0000493 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall67da35c2010-02-04 22:26:26 +0000494 data().ComputedVisibleConversions = true;
495 return &data().VisibleConversions;
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +0000496}
497
John McCallda4458e2010-03-31 01:36:47 +0000498#ifndef NDEBUG
499void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCall1e3a1a72010-03-15 09:07:48 +0000500 assert(ConvDecl->getDeclContext() == this &&
501 "conversion function does not belong to this record");
502
John McCallda4458e2010-03-31 01:36:47 +0000503 ConvDecl = ConvDecl->getUnderlyingDecl();
504 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
505 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
506 } else {
507 assert(isa<CXXConversionDecl>(ConvDecl));
508 }
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000509}
John McCallda4458e2010-03-31 01:36:47 +0000510#endif
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000511
John McCallda4458e2010-03-31 01:36:47 +0000512void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
513 // This operation is O(N) but extremely rare. Sema only uses it to
514 // remove UsingShadowDecls in a class that were followed by a direct
515 // declaration, e.g.:
516 // class A : B {
517 // using B::operator int;
518 // operator int();
519 // };
520 // This is uncommon by itself and even more uncommon in conjunction
521 // with sufficiently large numbers of directly-declared conversions
522 // that asymptotic behavior matters.
523
524 UnresolvedSetImpl &Convs = *getConversionFunctions();
525 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
526 if (Convs[I].getDecl() == ConvDecl) {
527 Convs.erase(I);
528 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
529 && "conversion was found multiple times in unresolved set");
530 return;
531 }
532 }
533
534 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +0000535}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +0000536
Fariborz Jahanian6dfc1972009-12-03 18:44:40 +0000537void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
538 Method->setVirtualAsWritten(true);
539 setAggregate(false);
540 setPOD(false);
541 setEmpty(false);
542 setPolymorphic(true);
543 setHasTrivialConstructor(false);
544 setHasTrivialCopyConstructor(false);
545 setHasTrivialCopyAssignment(false);
546}
547
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000548CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000549 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000550 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
551
552 return 0;
553}
554
Douglas Gregor06db9f52009-10-12 20:18:28 +0000555MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
556 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
557}
558
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000559void
560CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
561 TemplateSpecializationKind TSK) {
562 assert(TemplateOrInstantiation.isNull() &&
563 "Previous template or instantiation?");
564 assert(!isa<ClassTemplateSpecializationDecl>(this));
565 TemplateOrInstantiation
566 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
567}
568
Anders Carlsson27cfc6e2009-12-07 06:33:48 +0000569TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
570 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000571 = dyn_cast<ClassTemplateSpecializationDecl>(this))
572 return Spec->getSpecializationKind();
573
Douglas Gregor06db9f52009-10-12 20:18:28 +0000574 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000575 return MSInfo->getTemplateSpecializationKind();
576
577 return TSK_Undeclared;
578}
579
580void
581CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
582 if (ClassTemplateSpecializationDecl *Spec
583 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
584 Spec->setSpecializationKind(TSK);
585 return;
586 }
587
Douglas Gregor06db9f52009-10-12 20:18:28 +0000588 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +0000589 MSInfo->setTemplateSpecializationKind(TSK);
590 return;
591 }
592
593 assert(false && "Not a class template or member class specialization");
594}
595
Douglas Gregorbac74902010-07-01 14:13:13 +0000596CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
597 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +0000598 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +0000599
600 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +0000601 = Context.DeclarationNames.getCXXDestructorName(
602 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +0000603
John McCallf8ff7b92010-02-23 00:48:20 +0000604 DeclContext::lookup_const_iterator I, E;
Mike Stump11289f42009-09-09 15:08:12 +0000605 llvm::tie(I, E) = lookup(Name);
Sebastian Redlb469afb2010-09-02 23:19:42 +0000606 if (I == E)
607 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000608
Anders Carlssonf98849e2009-12-02 17:15:43 +0000609 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson0a637412009-05-29 21:03:38 +0000610 assert(++I == E && "Found more than one destructor!");
Mike Stump11289f42009-09-09 15:08:12 +0000611
Anders Carlsson0a637412009-05-29 21:03:38 +0000612 return Dtor;
613}
614
Ted Kremenek21475702008-09-05 17:16:31 +0000615CXXMethodDecl *
616CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000617 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +0000618 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000619 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000620 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000621 isStatic, SCAsWritten, isInline);
Ted Kremenek21475702008-09-05 17:16:31 +0000622}
623
Douglas Gregorbb3e12f2009-09-29 18:16:17 +0000624bool CXXMethodDecl::isUsualDeallocationFunction() const {
625 if (getOverloadedOperator() != OO_Delete &&
626 getOverloadedOperator() != OO_Array_Delete)
627 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +0000628
629 // C++ [basic.stc.dynamic.deallocation]p2:
630 // A template instance is never a usual deallocation function,
631 // regardless of its signature.
632 if (getPrimaryTemplate())
633 return false;
634
Douglas Gregorbb3e12f2009-09-29 18:16:17 +0000635 // C++ [basic.stc.dynamic.deallocation]p2:
636 // If a class T has a member deallocation function named operator delete
637 // with exactly one parameter, then that function is a usual (non-placement)
638 // deallocation function. [...]
639 if (getNumParams() == 1)
640 return true;
641
642 // C++ [basic.stc.dynamic.deallocation]p2:
643 // [...] If class T does not declare such an operator delete but does
644 // declare a member deallocation function named operator delete with
645 // exactly two parameters, the second of which has type std::size_t (18.1),
646 // then this function is a usual deallocation function.
647 ASTContext &Context = getASTContext();
648 if (getNumParams() != 2 ||
Chandler Carruth75cc3592010-02-08 18:54:05 +0000649 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
650 Context.getSizeType()))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +0000651 return false;
652
653 // This function is a usual deallocation function if there are no
654 // single-parameter deallocation functions of the same kind.
655 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
656 R.first != R.second; ++R.first) {
657 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
658 if (FD->getNumParams() == 1)
659 return false;
660 }
661
662 return true;
663}
664
Douglas Gregorb139cd52010-05-01 20:49:11 +0000665bool CXXMethodDecl::isCopyAssignmentOperator() const {
666 // C++0x [class.copy]p19:
667 // A user-declared copy assignment operator X::operator= is a non-static
668 // non-template member function of class X with exactly one parameter of
669 // type X, X&, const X&, volatile X& or const volatile X&.
670 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
671 /*non-static*/ isStatic() ||
672 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
673 /*exactly one parameter*/getNumParams() != 1)
674 return false;
675
676 QualType ParamType = getParamDecl(0)->getType();
677 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
678 ParamType = Ref->getPointeeType();
679
680 ASTContext &Context = getASTContext();
681 QualType ClassType
682 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
683 return Context.hasSameUnqualifiedType(ClassType, ParamType);
684}
685
Anders Carlsson36d87e12009-05-16 23:58:37 +0000686void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +0000687 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +0000688 assert(!MD->getParent()->isDependentContext() &&
689 "Can't add an overridden method to a class template!");
690
Douglas Gregor832940b2010-03-02 23:58:15 +0000691 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +0000692}
693
694CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor832940b2010-03-02 23:58:15 +0000695 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +0000696}
697
698CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor832940b2010-03-02 23:58:15 +0000699 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +0000700}
701
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +0000702unsigned CXXMethodDecl::size_overridden_methods() const {
703 return getASTContext().overridden_methods_size(this);
704}
705
Ted Kremenek21475702008-09-05 17:16:31 +0000706QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +0000707 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
708 // If the member function is declared const, the type of this is const X*,
709 // if the member function is declared volatile, the type of this is
710 // volatile X*, and if the member function is declared const volatile,
711 // the type of this is const volatile X*.
712
Ted Kremenek21475702008-09-05 17:16:31 +0000713 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +0000714
John McCalle78aac42010-03-10 03:28:59 +0000715 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +0000716 ClassTy = C.getQualifiedType(ClassTy,
717 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +0000718 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +0000719}
720
Eli Friedman71a26d82009-12-06 20:50:05 +0000721bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +0000722 // If this function is a template instantiation, look at the template from
723 // which it was instantiated.
724 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
725 if (!CheckFn)
726 CheckFn = this;
727
Eli Friedman71a26d82009-12-06 20:50:05 +0000728 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000729 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +0000730}
731
Douglas Gregore8381c02008-11-05 04:29:56 +0000732CXXBaseOrMemberInitializer::
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000733CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson1c0f8bb2010-04-12 00:51:03 +0000734 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000735 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnara341d7832010-05-26 18:09:23 +0000736 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
737 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
738 SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000739{
Douglas Gregore8381c02008-11-05 04:29:56 +0000740}
741
742CXXBaseOrMemberInitializer::
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000743CXXBaseOrMemberInitializer(ASTContext &Context,
744 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000745 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnara341d7832010-05-26 18:09:23 +0000746 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
747 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
748 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000749{
Douglas Gregore8381c02008-11-05 04:29:56 +0000750}
751
Douglas Gregor94f9a482010-05-05 05:51:00 +0000752CXXBaseOrMemberInitializer::
753CXXBaseOrMemberInitializer(ASTContext &Context,
754 FieldDecl *Member, SourceLocation MemberLoc,
755 SourceLocation L, Expr *Init, SourceLocation R,
756 VarDecl **Indices,
757 unsigned NumIndices)
758 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnara341d7832010-05-26 18:09:23 +0000759 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
760 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregor94f9a482010-05-05 05:51:00 +0000761{
762 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
763 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
764}
765
766CXXBaseOrMemberInitializer *
767CXXBaseOrMemberInitializer::Create(ASTContext &Context,
768 FieldDecl *Member,
769 SourceLocation MemberLoc,
770 SourceLocation L,
771 Expr *Init,
772 SourceLocation R,
773 VarDecl **Indices,
774 unsigned NumIndices) {
775 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
776 sizeof(VarDecl *) * NumIndices,
777 llvm::alignof<CXXBaseOrMemberInitializer>());
778 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
779 L, Init, R, Indices, NumIndices);
780}
781
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000782TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
783 if (isBaseInitializer())
John McCallbcd03502009-12-07 02:54:59 +0000784 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000785 else
786 return TypeLoc();
787}
788
789Type *CXXBaseOrMemberInitializer::getBaseClass() {
790 if (isBaseInitializer())
John McCallbcd03502009-12-07 02:54:59 +0000791 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000792 else
793 return 0;
794}
795
796const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
797 if (isBaseInitializer())
John McCallbcd03502009-12-07 02:54:59 +0000798 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000799 else
800 return 0;
801}
802
803SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
804 if (isMemberInitializer())
805 return getMemberLocation();
806
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000807 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +0000808}
809
810SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
811 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +0000812}
813
Douglas Gregor61956c42008-10-31 09:07:45 +0000814CXXConstructorDecl *
Chris Lattnerca025db2010-05-07 21:43:38 +0000815CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000816 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattnerca025db2010-05-07 21:43:38 +0000817 QualType(), 0, false, false, false);
818}
819
820CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +0000821CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000822 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +0000823 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000824 bool isExplicit,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000825 bool isInline,
826 bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000827 assert(NameInfo.getName().getNameKind()
828 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +0000829 "Name must refer to a constructor");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000830 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000831 isInline, isImplicitlyDeclared);
Douglas Gregor61956c42008-10-31 09:07:45 +0000832}
833
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000834bool CXXConstructorDecl::isDefaultConstructor() const {
835 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000836 // A default constructor for a class X is a constructor of class
837 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000838 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +0000839 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000840}
841
Mike Stump11289f42009-09-09 15:08:12 +0000842bool
Douglas Gregor507eb872009-12-22 00:34:07 +0000843CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000844 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000845 // A non-template constructor for class X is a copy constructor
846 // if its first parameter is of type X&, const X&, volatile X& or
847 // const volatile X&, and either there are no other parameters
848 // or else all other parameters have default arguments (8.3.6).
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000849 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +0000850 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorff7028a2009-11-13 23:59:09 +0000851 (getPrimaryTemplate() != 0) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +0000852 (getDescribedFunctionTemplate() != 0))
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000853 return false;
854
855 const ParmVarDecl *Param = getParamDecl(0);
856
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000857 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorff7028a2009-11-13 23:59:09 +0000858 const LValueReferenceType *ParamRefType =
859 Param->getType()->getAs<LValueReferenceType>();
860 if (!ParamRefType)
861 return false;
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000862
Douglas Gregorff7028a2009-11-13 23:59:09 +0000863 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +0000864 ASTContext &Context = getASTContext();
865
Douglas Gregorff7028a2009-11-13 23:59:09 +0000866 CanQualType PointeeType
867 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +0000868 CanQualType ClassTy
869 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000870 if (PointeeType.getUnqualifiedType() != ClassTy)
871 return false;
872
John McCall8ccfcb52009-09-24 19:53:00 +0000873 // FIXME: other qualifiers?
874
Douglas Gregoreebb5c12008-10-31 20:25:05 +0000875 // We have a copy constructor.
876 TypeQuals = PointeeType.getCVRQualifiers();
877 return true;
878}
879
Anders Carlssond20e7952009-08-28 16:57:08 +0000880bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000881 // C++ [class.conv.ctor]p1:
882 // A constructor declared without the function-specifier explicit
883 // that can be called with a single parameter specifies a
884 // conversion from the type of its first parameter to the type of
885 // its class. Such a constructor is called a converting
886 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +0000887 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000888 return false;
889
Mike Stump11289f42009-09-09 15:08:12 +0000890 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +0000891 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000892 (getNumParams() == 1) ||
Anders Carlsson85446472009-06-06 04:14:07 +0000893 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor26bee0b2008-10-31 16:23:19 +0000894}
Douglas Gregor61956c42008-10-31 09:07:45 +0000895
Douglas Gregorffe14e32009-11-14 01:20:54 +0000896bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
897 if ((getNumParams() < 1) ||
898 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
899 (getPrimaryTemplate() == 0) ||
900 (getDescribedFunctionTemplate() != 0))
901 return false;
902
903 const ParmVarDecl *Param = getParamDecl(0);
904
905 ASTContext &Context = getASTContext();
906 CanQualType ParamType = Context.getCanonicalType(Param->getType());
907
908 // Strip off the lvalue reference, if any.
909 if (CanQual<LValueReferenceType> ParamRefType
910 = ParamType->getAs<LValueReferenceType>())
911 ParamType = ParamRefType->getPointeeType();
912
913
914 // Is it the same as our our class type?
915 CanQualType ClassTy
916 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
917 if (ParamType.getUnqualifiedType() != ClassTy)
918 return false;
919
920 return true;
921}
922
Douglas Gregor831c93f2008-11-05 20:51:48 +0000923CXXDestructorDecl *
Chris Lattnerca025db2010-05-07 21:43:38 +0000924CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000925 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Chris Lattnerca025db2010-05-07 21:43:38 +0000926 QualType(), false, false);
927}
928
929CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +0000930CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000931 const DeclarationNameInfo &NameInfo,
Mike Stump11289f42009-09-09 15:08:12 +0000932 QualType T, bool isInline,
Douglas Gregor831c93f2008-11-05 20:51:48 +0000933 bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000934 assert(NameInfo.getName().getNameKind()
935 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +0000936 "Name must refer to a destructor");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000937 return new (C) CXXDestructorDecl(RD, NameInfo, T, isInline,
938 isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000939}
940
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000941CXXConversionDecl *
Chris Lattnerca025db2010-05-07 21:43:38 +0000942CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000943 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattnerca025db2010-05-07 21:43:38 +0000944 QualType(), 0, false, false);
945}
946
947CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000948CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000949 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +0000950 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000951 bool isInline, bool isExplicit) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000952 assert(NameInfo.getName().getNameKind()
953 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +0000954 "Name must refer to a conversion function");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000955 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
956 isInline, isExplicit);
Douglas Gregordbc5daf2008-11-07 20:08:42 +0000957}
958
Chris Lattnerb8c18fa2008-11-04 16:51:42 +0000959LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +0000960 DeclContext *DC,
Chris Lattnerb8c18fa2008-11-04 16:51:42 +0000961 SourceLocation L,
Douglas Gregor07665a62009-01-05 19:45:36 +0000962 LanguageIDs Lang, bool Braces) {
Steve Naroff13ae6f42009-01-27 21:25:57 +0000963 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000964}
Douglas Gregor889ceb72009-02-03 19:21:40 +0000965
966UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
967 SourceLocation L,
968 SourceLocation NamespaceLoc,
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000969 SourceRange QualifierRange,
970 NestedNameSpecifier *Qualifier,
Douglas Gregor889ceb72009-02-03 19:21:40 +0000971 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +0000972 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +0000973 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +0000974 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
975 Used = NS->getOriginalNamespace();
Mike Stump11289f42009-09-09 15:08:12 +0000976 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000977 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +0000978}
979
Sebastian Redla6602e92009-11-23 15:34:23 +0000980NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
981 if (NamespaceAliasDecl *NA =
982 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
983 return NA->getNamespace();
984 return cast_or_null<NamespaceDecl>(NominatedNamespace);
985}
986
Mike Stump11289f42009-09-09 15:08:12 +0000987NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +0000988 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000989 SourceLocation AliasLoc,
990 IdentifierInfo *Alias,
Douglas Gregor18231932009-05-30 06:48:27 +0000991 SourceRange QualifierRange,
992 NestedNameSpecifier *Qualifier,
Mike Stump11289f42009-09-09 15:08:12 +0000993 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +0000994 NamedDecl *Namespace) {
Sebastian Redla6602e92009-11-23 15:34:23 +0000995 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
996 Namespace = NS->getOriginalNamespace();
Douglas Gregor01a430132010-09-01 03:07:18 +0000997 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor18231932009-05-30 06:48:27 +0000998 Qualifier, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +0000999}
1000
Douglas Gregorfec52632009-06-20 00:51:54 +00001001UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00001002 SourceRange NNR, SourceLocation UL,
1003 NestedNameSpecifier* TargetNNS,
1004 const DeclarationNameInfo &NameInfo,
1005 bool IsTypeNameArg) {
1006 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
Douglas Gregorfec52632009-06-20 00:51:54 +00001007}
1008
John McCalle61f2ba2009-11-18 02:36:19 +00001009UnresolvedUsingValueDecl *
1010UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1011 SourceLocation UsingLoc,
1012 SourceRange TargetNNR,
1013 NestedNameSpecifier *TargetNNS,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00001014 const DeclarationNameInfo &NameInfo) {
John McCalle61f2ba2009-11-18 02:36:19 +00001015 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00001016 TargetNNR, TargetNNS, NameInfo);
John McCalle61f2ba2009-11-18 02:36:19 +00001017}
1018
1019UnresolvedUsingTypenameDecl *
1020UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1021 SourceLocation UsingLoc,
1022 SourceLocation TypenameLoc,
1023 SourceRange TargetNNR,
1024 NestedNameSpecifier *TargetNNS,
1025 SourceLocation TargetNameLoc,
1026 DeclarationName TargetName) {
1027 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1028 TargetNNR, TargetNNS,
1029 TargetNameLoc,
1030 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +00001031}
1032
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00001033StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1034 SourceLocation L, Expr *AssertExpr,
1035 StringLiteral *Message) {
1036 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1037}
1038
Anders Carlsson6750d162009-03-26 23:46:50 +00001039static const char *getAccessName(AccessSpecifier AS) {
1040 switch (AS) {
1041 default:
1042 case AS_none:
1043 assert("Invalid access specifier!");
1044 return 0;
1045 case AS_public:
1046 return "public";
1047 case AS_private:
1048 return "private";
1049 case AS_protected:
1050 return "protected";
1051 }
1052}
1053
1054const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1055 AccessSpecifier AS) {
1056 return DB << getAccessName(AS);
1057}