blob: 0e37bc7801ceee17c5393e1eab83dee764addcdc [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
Douglas Gregord475b8d2009-03-25 21:17:03 +000015#include "clang/AST/DeclTemplate.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000016#include "clang/AST/ASTContext.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000017#include "clang/AST/Expr.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000018#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000019#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000020#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000021#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Decl Allocation/Deallocation Method Implementations
26//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000027
John McCall86ff3082010-02-04 22:26:26 +000028CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
29 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000030 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-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 Jahanian62509212009-09-12 18:26:03 +000034 HasTrivialDestructor(true), ComputedVisibleConversions(false),
Douglas Gregor18274032010-07-03 00:47:00 +000035 DeclaredDefaultConstructor(false), DeclaredCopyConstructor(false),
Douglas Gregora376d102010-07-02 21:50:04 +000036 DeclaredCopyAssignment(false), DeclaredDestructor(false),
Fariborz Jahanian62509212009-09-12 18:26:03 +000037 Bases(0), NumBases(0), VBases(0), NumVBases(0),
John McCalld60e22e2010-03-12 01:19:31 +000038 Definition(D), FirstFriend(0) {
John McCall86ff3082010-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 Gregord475b8d2009-03-25 21:17:03 +000047 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000048
Ted Kremenek4b7c9832008-09-05 17:16:31 +000049CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
50 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000051 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000052 CXXRecordDecl* PrevDecl,
53 bool DelayTypeCreation) {
Mike Stump1eb44332009-09-09 15:08:12 +000054 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000055 PrevDecl, TKL);
Mike Stump1eb44332009-09-09 15:08:12 +000056
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000057 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000058 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000059 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000060 return R;
61}
62
Argyrios Kyrtzidisb8b03e62010-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 Stump1eb44332009-09-09 15:08:12 +000068void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000069CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000070 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000071 ASTContext &C = getASTContext();
72
Mike Stump1eb44332009-09-09 15:08:12 +000073 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000074 // An aggregate is an array or a class (clause 9) with [...]
75 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000076 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000077
John McCall86ff3082010-02-04 22:26:26 +000078 if (data().Bases)
79 C.Deallocate(data().Bases);
Mike Stump1eb44332009-09-09 15:08:12 +000080
Anders Carlsson6f6de732010-03-29 05:13:12 +000081 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000082 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000083
84 // The virtual bases of this class.
85 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000086
John McCall86ff3082010-02-04 22:26:26 +000087 data().Bases = new(C) CXXBaseSpecifier [NumBases];
88 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000089 for (unsigned i = 0; i < NumBases; ++i) {
John McCall86ff3082010-02-04 22:26:26 +000090 data().Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-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 Gregor5fe8c042010-02-27 00:25:28 +000094 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000095 if (BaseType->isDependentType())
96 continue;
97 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +000098 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +000099
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000100 // C++ [dcl.init.aggr]p1:
101 // An aggregate is [...] a class with [...] no base classes [...].
102 data().Aggregate = false;
103
104 // C++ [class]p4:
105 // A POD-struct is an aggregate class...
106 data().PlainOldData = false;
107
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000108 // A class with a non-empty base class is not empty.
109 // FIXME: Standard ref?
110 if (!BaseClassDecl->isEmpty())
111 data().Empty = false;
112
Anders Carlsson6f6de732010-03-29 05:13:12 +0000113 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000114 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000115 BaseClassDecl->vbases_begin(),
116 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000117 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000118 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000119 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000120 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000121
122 if (Base->isVirtual()) {
123 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000124 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000125 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000126
127 // C++0x [meta.unary.prop] is_empty:
128 // T is a class type, but not a union type, with ... no virtual base
129 // classes
130 data().Empty = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000131 }
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000132 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000133
134 if (VBases.empty())
135 return;
136
137 // Create base specifier for any direct or indirect virtual bases.
138 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
139 data().NumVBases = VBases.size();
140 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000141 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
142
Anders Carlsson6f6de732010-03-29 05:13:12 +0000143 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000144 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000145 continue;
146
Nick Lewycky56062202010-07-26 16:56:01 +0000147 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
148 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000149
150 data().VBases[I] =
151 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000152 VBaseClassDecl->getTagKind() == TTK_Class,
Nick Lewycky56062202010-07-26 16:56:01 +0000153 VBases[I]->getAccessSpecifier(), VBaseTypeInfo);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000154 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000155}
156
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000157/// Callback function for CXXRecordDecl::forallBases that acknowledges
158/// that it saw a base class.
159static bool SawBase(const CXXRecordDecl *, void *) {
160 return true;
161}
162
163bool CXXRecordDecl::hasAnyDependentBases() const {
164 if (!isDependentContext())
165 return false;
166
167 return !forallBases(SawBase, 0);
168}
169
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000170bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000171 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000172}
173
Douglas Gregor0d405db2010-07-01 20:59:04 +0000174/// \brief Perform a simplistic form of overload resolution that only considers
175/// cv-qualifiers on a single parameter, and return the best overload candidate
176/// (if there is one).
177static CXXMethodDecl *
178GetBestOverloadCandidateSimple(
179 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
180 if (Cands.empty())
181 return 0;
182 if (Cands.size() == 1)
183 return Cands[0].first;
184
185 unsigned Best = 0, N = Cands.size();
186 for (unsigned I = 1; I != N; ++I)
187 if (Cands[Best].second.isSupersetOf(Cands[I].second))
188 Best = I;
189
190 for (unsigned I = 1; I != N; ++I)
191 if (Cands[Best].second.isSupersetOf(Cands[I].second))
192 return 0;
193
194 return Cands[Best].first;
195}
196
Mike Stump1eb44332009-09-09 15:08:12 +0000197CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000198 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000199 QualType ClassType
200 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000201 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000202 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000203 Context.getCanonicalType(ClassType));
204 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000205 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000206 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000207 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000208 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000209 // C++ [class.copy]p2:
210 // A non-template constructor for class X is a copy constructor if [...]
211 if (isa<FunctionTemplateDecl>(*Con))
212 continue;
213
Douglas Gregor0d405db2010-07-01 20:59:04 +0000214 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
215 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000216 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
217 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000218 Found.push_back(std::make_pair(
219 const_cast<CXXConstructorDecl *>(Constructor),
220 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000221 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000222 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000223
224 return cast_or_null<CXXConstructorDecl>(
225 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000226}
227
Douglas Gregorb87786f2010-07-01 17:48:08 +0000228CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
229 ASTContext &Context = getASTContext();
230 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
231 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
232
233 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
234 DeclContext::lookup_const_iterator Op, OpEnd;
235 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
236 // C++ [class.copy]p9:
237 // A user-declared copy assignment operator is a non-static non-template
238 // member function of class X with exactly one parameter of type X, X&,
239 // const X&, volatile X& or const volatile X&.
240 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
241 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
242 continue;
243
244 const FunctionProtoType *FnType
245 = Method->getType()->getAs<FunctionProtoType>();
246 assert(FnType && "Overloaded operator has no prototype.");
247 // Don't assert on this; an invalid decl might have been left in the AST.
248 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
249 continue;
250
251 QualType ArgType = FnType->getArgType(0);
252 Qualifiers Quals;
253 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
254 ArgType = Ref->getPointeeType();
255 // If we have a const argument and we have a reference to a non-const,
256 // this function does not match.
257 if (ArgIsConst && !ArgType.isConstQualified())
258 continue;
259
260 Quals = ArgType.getQualifiers();
261 } else {
262 // By-value copy-assignment operators are treated like const X&
263 // copy-assignment operators.
264 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
265 }
266
267 if (!Context.hasSameUnqualifiedType(ArgType, Class))
268 continue;
269
270 // Save this copy-assignment operator. It might be "the one".
271 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
272 }
273
274 // Use a simplistic form of overload resolution to find the candidate.
275 return GetBestOverloadCandidateSimple(Found);
276}
277
Sebastian Redl64b45f72009-01-05 20:52:13 +0000278void
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000279CXXRecordDecl::addedMember(Decl *D) {
280 // Ignore friends and invalid declarations.
281 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000282 return;
283
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000284 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
285 if (FunTmpl)
286 D = FunTmpl->getTemplatedDecl();
287
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000288 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
289 if (Method->isVirtual()) {
290 // C++ [dcl.init.aggr]p1:
291 // An aggregate is an array or a class with [...] no virtual functions.
292 data().Aggregate = false;
293
294 // C++ [class]p4:
295 // A POD-struct is an aggregate class...
296 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000297
298 // Virtual functions make the class non-empty.
299 // FIXME: Standard ref?
300 data().Empty = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000301 }
302 }
303
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000304 if (D->isImplicit()) {
305 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
306 // If this is the implicit default constructor, note that we have now
307 // declared it.
308 if (Constructor->isDefaultConstructor())
309 data().DeclaredDefaultConstructor = true;
310 // If this is the implicit copy constructor, note that we have now
311 // declared it.
312 else if (Constructor->isCopyConstructor())
313 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000314 return;
315 }
316
317 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000318 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000319 return;
320 }
321
322 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000323 // If this is the implicit copy constructor, note that we have now
324 // declared it.
325 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000326 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000327 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000328 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000329 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000330
331 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000332 }
333
334 // Handle (user-declared) constructors.
335 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
336 // Note that we have a user-declared constructor.
337 data().UserDeclaredConstructor = true;
338
339 // Note that we have no need of an implicitly-declared default constructor.
340 data().DeclaredDefaultConstructor = true;
341
342 // C++ [dcl.init.aggr]p1:
343 // An aggregate is an array or a class (clause 9) with no
344 // user-declared constructors (12.1) [...].
345 data().Aggregate = false;
346
347 // C++ [class]p4:
348 // A POD-struct is an aggregate class [...]
349 data().PlainOldData = false;
350
351 // C++ [class.ctor]p5:
352 // A constructor is trivial if it is an implicitly-declared default
353 // constructor.
354 // FIXME: C++0x: don't do this for "= default" default constructors.
355 data().HasTrivialConstructor = false;
356
357 // Note when we have a user-declared copy constructor, which will
358 // suppress the implicit declaration of a copy constructor.
359 if (!FunTmpl && Constructor->isCopyConstructor()) {
360 data().UserDeclaredCopyConstructor = true;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000361 data().DeclaredCopyConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000362
363 // C++ [class.copy]p6:
364 // A copy constructor is trivial if it is implicitly declared.
365 // FIXME: C++0x: don't do this for "= default" copy constructors.
366 data().HasTrivialCopyConstructor = false;
367 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000368
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000369 return;
370 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000371
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000372 // Handle (user-declared) destructors.
373 if (isa<CXXDestructorDecl>(D)) {
374 data().DeclaredDestructor = true;
375 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000376
377 // C++ [class]p4:
378 // A POD-struct is an aggregate class that has [...] no user-defined
379 // destructor.
380 data().PlainOldData = false;
381
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000382 return;
383 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000384
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000385 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000386 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
387 if (Method->getOverloadedOperator() == OO_Equal) {
388 // We're interested specifically in copy assignment operators.
389 const FunctionProtoType *FnType
390 = Method->getType()->getAs<FunctionProtoType>();
391 assert(FnType && "Overloaded operator has no proto function type.");
392 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
393
394 // Copy assignment operators must be non-templates.
395 if (Method->getPrimaryTemplate() || FunTmpl)
396 return;
397
398 ASTContext &Context = getASTContext();
399 QualType ArgType = FnType->getArgType(0);
400 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
401 ArgType = Ref->getPointeeType();
402
403 ArgType = ArgType.getUnqualifiedType();
404 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
405 const_cast<CXXRecordDecl*>(this)));
406
407 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
408 return;
409
410 // This is a copy assignment operator.
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000411 // FIXME: Move assignment operators.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000412
413 // Suppress the implicit declaration of a copy constructor.
414 data().UserDeclaredCopyAssignment = true;
415 data().DeclaredCopyAssignment = true;
416
417 // C++ [class.copy]p11:
418 // A copy assignment operator is trivial if it is implicitly declared.
419 // FIXME: C++0x: don't do this for "= default" copy operators.
420 data().HasTrivialCopyAssignment = false;
421
422 // C++ [class]p4:
423 // A POD-struct is an aggregate class that [...] has no user-defined copy
424 // assignment operator [...].
425 data().PlainOldData = false;
426 }
Douglas Gregor22584312010-07-02 23:41:54 +0000427
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000428 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000429 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000430
431 // Handle non-static data members.
432 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
433 // C++ [dcl.init.aggr]p1:
434 // An aggregate is an array or a class (clause 9) with [...] no
435 // private or protected non-static data members (clause 11).
436 //
437 // A POD must be an aggregate.
438 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
439 data().Aggregate = false;
440 data().PlainOldData = false;
441 }
442
443 // C++ [class]p9:
444 // A POD struct is a class that is both a trivial class and a
445 // standard-layout class, and has no non-static data members of type
446 // non-POD struct, non-POD union (or array of such types).
447 ASTContext &Context = getASTContext();
448 QualType T = Context.getBaseElementType(Field->getType());
449 if (!T->isPODType())
450 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000451
452 // If this is not a zero-length bit-field, then the class is not empty.
453 if (data().Empty) {
454 if (!Field->getBitWidth())
455 data().Empty = false;
456 else if (!Field->getBitWidth()->isTypeDependent() &&
457 !Field->getBitWidth()->isValueDependent()) {
458 llvm::APSInt Bits;
459 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
460 if (!!Bits)
461 data().Empty = false;
462 }
463 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000464 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000465}
466
John McCallb05b5f32010-03-15 09:07:48 +0000467static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
468 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000469 if (isa<UsingShadowDecl>(Conv))
470 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000471 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
472 T = ConvTemp->getTemplatedDecl()->getResultType();
473 else
474 T = cast<CXXConversionDecl>(Conv)->getConversionType();
475 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000476}
477
John McCallb05b5f32010-03-15 09:07:48 +0000478/// Collect the visible conversions of a base class.
479///
480/// \param Base a base class of the class we're considering
481/// \param InVirtual whether this base class is a virtual base (or a base
482/// of a virtual base)
483/// \param Access the access along the inheritance path to this base
484/// \param ParentHiddenTypes the conversions provided by the inheritors
485/// of this base
486/// \param Output the set to which to add conversions from non-virtual bases
487/// \param VOutput the set to which to add conversions from virtual bases
488/// \param HiddenVBaseCs the set of conversions which were hidden in a
489/// virtual base along some inheritance path
490static void CollectVisibleConversions(ASTContext &Context,
491 CXXRecordDecl *Record,
492 bool InVirtual,
493 AccessSpecifier Access,
494 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
495 UnresolvedSetImpl &Output,
496 UnresolvedSetImpl &VOutput,
497 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
498 // The set of types which have conversions in this class or its
499 // subclasses. As an optimization, we don't copy the derived set
500 // unless it might change.
501 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
502 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
503
504 // Collect the direct conversions and figure out which conversions
505 // will be hidden in the subclasses.
506 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
507 if (!Cs.empty()) {
508 HiddenTypesBuffer = ParentHiddenTypes;
509 HiddenTypes = &HiddenTypesBuffer;
510
511 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
512 bool Hidden =
513 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
514
515 // If this conversion is hidden and we're in a virtual base,
516 // remember that it's hidden along some inheritance path.
517 if (Hidden && InVirtual)
518 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
519
520 // If this conversion isn't hidden, add it to the appropriate output.
521 else if (!Hidden) {
522 AccessSpecifier IAccess
523 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
524
525 if (InVirtual)
526 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000527 else
John McCallb05b5f32010-03-15 09:07:48 +0000528 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000529 }
530 }
531 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000532
John McCallb05b5f32010-03-15 09:07:48 +0000533 // Collect information recursively from any base classes.
534 for (CXXRecordDecl::base_class_iterator
535 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
536 const RecordType *RT = I->getType()->getAs<RecordType>();
537 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000538
John McCallb05b5f32010-03-15 09:07:48 +0000539 AccessSpecifier BaseAccess
540 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
541 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000542
John McCallb05b5f32010-03-15 09:07:48 +0000543 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
544 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
545 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000546 }
John McCallb05b5f32010-03-15 09:07:48 +0000547}
Sebastian Redl9994a342009-10-25 17:03:50 +0000548
John McCallb05b5f32010-03-15 09:07:48 +0000549/// Collect the visible conversions of a class.
550///
551/// This would be extremely straightforward if it weren't for virtual
552/// bases. It might be worth special-casing that, really.
553static void CollectVisibleConversions(ASTContext &Context,
554 CXXRecordDecl *Record,
555 UnresolvedSetImpl &Output) {
556 // The collection of all conversions in virtual bases that we've
557 // found. These will be added to the output as long as they don't
558 // appear in the hidden-conversions set.
559 UnresolvedSet<8> VBaseCs;
560
561 // The set of conversions in virtual bases that we've determined to
562 // be hidden.
563 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
564
565 // The set of types hidden by classes derived from this one.
566 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
567
568 // Go ahead and collect the direct conversions and add them to the
569 // hidden-types set.
570 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
571 Output.append(Cs.begin(), Cs.end());
572 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
573 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
574
575 // Recursively collect conversions from base classes.
576 for (CXXRecordDecl::base_class_iterator
577 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
578 const RecordType *RT = I->getType()->getAs<RecordType>();
579 if (!RT) continue;
580
581 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
582 I->isVirtual(), I->getAccessSpecifier(),
583 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
584 }
585
586 // Add any unhidden conversions provided by virtual bases.
587 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
588 I != E; ++I) {
589 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
590 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000591 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000592}
593
594/// getVisibleConversionFunctions - get all conversion functions visible
595/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000596const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000597 // If root class, all conversions are visible.
598 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000599 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000600 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000601 if (data().ComputedVisibleConversions)
602 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000603 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000604 data().ComputedVisibleConversions = true;
605 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000606}
607
John McCall32daa422010-03-31 01:36:47 +0000608#ifndef NDEBUG
609void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000610 assert(ConvDecl->getDeclContext() == this &&
611 "conversion function does not belong to this record");
612
John McCall32daa422010-03-31 01:36:47 +0000613 ConvDecl = ConvDecl->getUnderlyingDecl();
614 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
615 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
616 } else {
617 assert(isa<CXXConversionDecl>(ConvDecl));
618 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000619}
John McCall32daa422010-03-31 01:36:47 +0000620#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000621
John McCall32daa422010-03-31 01:36:47 +0000622void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
623 // This operation is O(N) but extremely rare. Sema only uses it to
624 // remove UsingShadowDecls in a class that were followed by a direct
625 // declaration, e.g.:
626 // class A : B {
627 // using B::operator int;
628 // operator int();
629 // };
630 // This is uncommon by itself and even more uncommon in conjunction
631 // with sufficiently large numbers of directly-declared conversions
632 // that asymptotic behavior matters.
633
634 UnresolvedSetImpl &Convs = *getConversionFunctions();
635 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
636 if (Convs[I].getDecl() == ConvDecl) {
637 Convs.erase(I);
638 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
639 && "conversion was found multiple times in unresolved set");
640 return;
641 }
642 }
643
644 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000645}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000646
Bill Wendling2a674e82010-09-28 01:09:49 +0000647void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
648 Method->setVirtualAsWritten(true);
Bill Wendling2a674e82010-09-28 01:09:49 +0000649 setPolymorphic(true);
650 setHasTrivialConstructor(false);
651 setHasTrivialCopyConstructor(false);
652 setHasTrivialCopyAssignment(false);
653}
654
Douglas Gregorf6b11852009-10-08 15:14:33 +0000655CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000656 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000657 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
658
659 return 0;
660}
661
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000662MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
663 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
664}
665
Douglas Gregorf6b11852009-10-08 15:14:33 +0000666void
667CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
668 TemplateSpecializationKind TSK) {
669 assert(TemplateOrInstantiation.isNull() &&
670 "Previous template or instantiation?");
671 assert(!isa<ClassTemplateSpecializationDecl>(this));
672 TemplateOrInstantiation
673 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
674}
675
Anders Carlssonb13e3572009-12-07 06:33:48 +0000676TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
677 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000678 = dyn_cast<ClassTemplateSpecializationDecl>(this))
679 return Spec->getSpecializationKind();
680
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000681 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000682 return MSInfo->getTemplateSpecializationKind();
683
684 return TSK_Undeclared;
685}
686
687void
688CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
689 if (ClassTemplateSpecializationDecl *Spec
690 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
691 Spec->setSpecializationKind(TSK);
692 return;
693 }
694
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000695 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000696 MSInfo->setTemplateSpecializationKind(TSK);
697 return;
698 }
699
700 assert(false && "Not a class template or member class specialization");
701}
702
Douglas Gregor1d110e02010-07-01 14:13:13 +0000703CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
704 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000705 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000706
707 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000708 = Context.DeclarationNames.getCXXDestructorName(
709 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000710
John McCallc0bf4622010-02-23 00:48:20 +0000711 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000712 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000713 if (I == E)
714 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000716 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000717 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Anders Carlsson7267c162009-05-29 21:03:38 +0000719 return Dtor;
720}
721
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000722CXXMethodDecl *
723CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000724 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000725 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000726 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000727 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000728 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000729}
730
Douglas Gregor90916562009-09-29 18:16:17 +0000731bool CXXMethodDecl::isUsualDeallocationFunction() const {
732 if (getOverloadedOperator() != OO_Delete &&
733 getOverloadedOperator() != OO_Array_Delete)
734 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000735
736 // C++ [basic.stc.dynamic.deallocation]p2:
737 // A template instance is never a usual deallocation function,
738 // regardless of its signature.
739 if (getPrimaryTemplate())
740 return false;
741
Douglas Gregor90916562009-09-29 18:16:17 +0000742 // C++ [basic.stc.dynamic.deallocation]p2:
743 // If a class T has a member deallocation function named operator delete
744 // with exactly one parameter, then that function is a usual (non-placement)
745 // deallocation function. [...]
746 if (getNumParams() == 1)
747 return true;
748
749 // C++ [basic.stc.dynamic.deallocation]p2:
750 // [...] If class T does not declare such an operator delete but does
751 // declare a member deallocation function named operator delete with
752 // exactly two parameters, the second of which has type std::size_t (18.1),
753 // then this function is a usual deallocation function.
754 ASTContext &Context = getASTContext();
755 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000756 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
757 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000758 return false;
759
760 // This function is a usual deallocation function if there are no
761 // single-parameter deallocation functions of the same kind.
762 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
763 R.first != R.second; ++R.first) {
764 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
765 if (FD->getNumParams() == 1)
766 return false;
767 }
768
769 return true;
770}
771
Douglas Gregor06a9f362010-05-01 20:49:11 +0000772bool CXXMethodDecl::isCopyAssignmentOperator() const {
773 // C++0x [class.copy]p19:
774 // A user-declared copy assignment operator X::operator= is a non-static
775 // non-template member function of class X with exactly one parameter of
776 // type X, X&, const X&, volatile X& or const volatile X&.
777 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
778 /*non-static*/ isStatic() ||
779 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
780 /*exactly one parameter*/getNumParams() != 1)
781 return false;
782
783 QualType ParamType = getParamDecl(0)->getType();
784 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
785 ParamType = Ref->getPointeeType();
786
787 ASTContext &Context = getASTContext();
788 QualType ClassType
789 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
790 return Context.hasSameUnqualifiedType(ClassType, ParamType);
791}
792
Anders Carlsson05eb2442009-05-16 23:58:37 +0000793void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000794 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000795 assert(!MD->getParent()->isDependentContext() &&
796 "Can't add an overridden method to a class template!");
797
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000798 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000799}
800
801CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000802 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000803}
804
805CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000806 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000807}
808
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000809unsigned CXXMethodDecl::size_overridden_methods() const {
810 return getASTContext().overridden_methods_size(this);
811}
812
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000813QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000814 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
815 // If the member function is declared const, the type of this is const X*,
816 // if the member function is declared volatile, the type of this is
817 // volatile X*, and if the member function is declared const volatile,
818 // the type of this is const volatile X*.
819
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000820 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000821
John McCall3cb0ebd2010-03-10 03:28:59 +0000822 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000823 ClassTy = C.getQualifiedType(ClassTy,
824 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000825 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000826}
827
Eli Friedmand7d7f672009-12-06 20:50:05 +0000828bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000829 // If this function is a template instantiation, look at the template from
830 // which it was instantiated.
831 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
832 if (!CheckFn)
833 CheckFn = this;
834
Eli Friedmand7d7f672009-12-06 20:50:05 +0000835 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000836 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000837}
838
Douglas Gregor7ad83902008-11-05 04:29:56 +0000839CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000840CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +0000841 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000842 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000843 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
844 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
845 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000846{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000847}
848
849CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000850CXXBaseOrMemberInitializer(ASTContext &Context,
851 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000852 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000853 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
854 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
855 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000856{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000857}
858
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000859CXXBaseOrMemberInitializer::
860CXXBaseOrMemberInitializer(ASTContext &Context,
861 FieldDecl *Member, SourceLocation MemberLoc,
862 SourceLocation L, Expr *Init, SourceLocation R,
863 VarDecl **Indices,
864 unsigned NumIndices)
865 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000866 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
867 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000868{
869 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
870 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
871}
872
873CXXBaseOrMemberInitializer *
874CXXBaseOrMemberInitializer::Create(ASTContext &Context,
875 FieldDecl *Member,
876 SourceLocation MemberLoc,
877 SourceLocation L,
878 Expr *Init,
879 SourceLocation R,
880 VarDecl **Indices,
881 unsigned NumIndices) {
882 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
883 sizeof(VarDecl *) * NumIndices,
884 llvm::alignof<CXXBaseOrMemberInitializer>());
885 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
886 L, Init, R, Indices, NumIndices);
887}
888
Douglas Gregor802ab452009-12-02 22:36:29 +0000889TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
890 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000891 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000892 else
893 return TypeLoc();
894}
895
896Type *CXXBaseOrMemberInitializer::getBaseClass() {
897 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000898 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000899 else
900 return 0;
901}
902
903const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
904 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000905 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000906 else
907 return 0;
908}
909
910SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
911 if (isMemberInitializer())
912 return getMemberLocation();
913
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000914 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +0000915}
916
917SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
918 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000919}
920
Douglas Gregorb48fe382008-10-31 09:07:45 +0000921CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000922CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000923 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000924 QualType(), 0, false, false, false);
925}
926
927CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +0000928CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000929 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000930 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000931 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000932 bool isInline,
933 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000934 assert(NameInfo.getName().getNameKind()
935 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000936 "Name must refer to a constructor");
Abramo Bagnara25777432010-08-11 22:01:17 +0000937 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000938 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000939}
940
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000941bool CXXConstructorDecl::isDefaultConstructor() const {
942 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000943 // A default constructor for a class X is a constructor of class
944 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000945 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000946 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000947}
948
Mike Stump1eb44332009-09-09 15:08:12 +0000949bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000950CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000951 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000952 // A non-template constructor for class X is a copy constructor
953 // if its first parameter is of type X&, const X&, volatile X& or
954 // const volatile X&, and either there are no other parameters
955 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000956 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000957 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000958 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000959 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000960 return false;
961
962 const ParmVarDecl *Param = getParamDecl(0);
963
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000964 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000965 const LValueReferenceType *ParamRefType =
966 Param->getType()->getAs<LValueReferenceType>();
967 if (!ParamRefType)
968 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000969
Douglas Gregorfd476482009-11-13 23:59:09 +0000970 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000971 ASTContext &Context = getASTContext();
972
Douglas Gregorfd476482009-11-13 23:59:09 +0000973 CanQualType PointeeType
974 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000975 CanQualType ClassTy
976 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000977 if (PointeeType.getUnqualifiedType() != ClassTy)
978 return false;
979
John McCall0953e762009-09-24 19:53:00 +0000980 // FIXME: other qualifiers?
981
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000982 // We have a copy constructor.
983 TypeQuals = PointeeType.getCVRQualifiers();
984 return true;
985}
986
Anders Carlssonfaccd722009-08-28 16:57:08 +0000987bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000988 // C++ [class.conv.ctor]p1:
989 // A constructor declared without the function-specifier explicit
990 // that can be called with a single parameter specifies a
991 // conversion from the type of its first parameter to the type of
992 // its class. Such a constructor is called a converting
993 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000994 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000995 return false;
996
Mike Stump1eb44332009-09-09 15:08:12 +0000997 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000998 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000999 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001000 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001001}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001002
Douglas Gregor66724ea2009-11-14 01:20:54 +00001003bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
1004 if ((getNumParams() < 1) ||
1005 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1006 (getPrimaryTemplate() == 0) ||
1007 (getDescribedFunctionTemplate() != 0))
1008 return false;
1009
1010 const ParmVarDecl *Param = getParamDecl(0);
1011
1012 ASTContext &Context = getASTContext();
1013 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1014
1015 // Strip off the lvalue reference, if any.
1016 if (CanQual<LValueReferenceType> ParamRefType
1017 = ParamType->getAs<LValueReferenceType>())
1018 ParamType = ParamRefType->getPointeeType();
1019
1020
1021 // Is it the same as our our class type?
1022 CanQualType ClassTy
1023 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1024 if (ParamType.getUnqualifiedType() != ClassTy)
1025 return false;
1026
1027 return true;
1028}
1029
Douglas Gregor42a552f2008-11-05 20:51:48 +00001030CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001031CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001032 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001033 QualType(), false, false);
1034}
1035
1036CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001037CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001038 const DeclarationNameInfo &NameInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001039 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001040 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001041 assert(NameInfo.getName().getNameKind()
1042 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001043 "Name must refer to a destructor");
Abramo Bagnara25777432010-08-11 22:01:17 +00001044 return new (C) CXXDestructorDecl(RD, NameInfo, T, isInline,
1045 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001046}
1047
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001048CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001049CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001050 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001051 QualType(), 0, false, false);
1052}
1053
1054CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001055CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001056 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001057 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001058 bool isInline, bool isExplicit) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001059 assert(NameInfo.getName().getNameKind()
1060 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001061 "Name must refer to a conversion function");
Abramo Bagnara25777432010-08-11 22:01:17 +00001062 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
1063 isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001064}
1065
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001066LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001067 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001068 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +00001069 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +00001070 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001071}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001072
1073UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1074 SourceLocation L,
1075 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001076 SourceRange QualifierRange,
1077 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001078 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001079 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001080 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001081 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1082 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001083 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001084 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001085}
1086
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001087NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1088 if (NamespaceAliasDecl *NA =
1089 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1090 return NA->getNamespace();
1091 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1092}
1093
Mike Stump1eb44332009-09-09 15:08:12 +00001094NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001095 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001096 SourceLocation AliasLoc,
1097 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001098 SourceRange QualifierRange,
1099 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001100 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001101 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001102 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1103 Namespace = NS->getOriginalNamespace();
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001104 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001105 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001106}
1107
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001108UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001109 SourceRange NNR, SourceLocation UL,
1110 NestedNameSpecifier* TargetNNS,
1111 const DeclarationNameInfo &NameInfo,
1112 bool IsTypeNameArg) {
1113 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001114}
1115
John McCall7ba107a2009-11-18 02:36:19 +00001116UnresolvedUsingValueDecl *
1117UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1118 SourceLocation UsingLoc,
1119 SourceRange TargetNNR,
1120 NestedNameSpecifier *TargetNNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001121 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001122 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001123 TargetNNR, TargetNNS, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001124}
1125
1126UnresolvedUsingTypenameDecl *
1127UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1128 SourceLocation UsingLoc,
1129 SourceLocation TypenameLoc,
1130 SourceRange TargetNNR,
1131 NestedNameSpecifier *TargetNNS,
1132 SourceLocation TargetNameLoc,
1133 DeclarationName TargetName) {
1134 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1135 TargetNNR, TargetNNS,
1136 TargetNameLoc,
1137 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001138}
1139
Anders Carlssonfb311762009-03-14 00:25:26 +00001140StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1141 SourceLocation L, Expr *AssertExpr,
1142 StringLiteral *Message) {
1143 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1144}
1145
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001146static const char *getAccessName(AccessSpecifier AS) {
1147 switch (AS) {
1148 default:
1149 case AS_none:
1150 assert("Invalid access specifier!");
1151 return 0;
1152 case AS_public:
1153 return "public";
1154 case AS_private:
1155 return "private";
1156 case AS_protected:
1157 return "protected";
1158 }
1159}
1160
1161const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1162 AccessSpecifier AS) {
1163 return DB << getAccessName(AS);
1164}