blob: b4c130428c50a0a9816929f9785a35d5414f40d5 [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 Gregor6e3c7712010-09-27 23:16:44 +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 Gregorcdbfa6c2010-09-27 23:31:14 +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 Gregorcdbfa6c2010-09-27 23:31:14 +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 Gregor6e3c7712010-09-27 23:16:44 +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 Gregorcdbfa6c2010-09-27 23:31:14 +0000297
298 // Virtual functions make the class non-empty.
299 // FIXME: Standard ref?
300 data().Empty = false;
Douglas Gregor6e3c7712010-09-27 23:16:44 +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 Gregor0ed2e082010-09-27 22:48:58 +0000314 } else if (isa<CXXDestructorDecl>(D)) {
315 data().DeclaredDestructor = true;
316 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000317 // If this is the implicit copy constructor, note that we have now
318 // declared it.
319 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000320 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000321 data().DeclaredCopyAssignment = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000322 }
323
324 // Nothing else to do for implicitly-declared members.
325 return;
326 }
327
328 // Handle (user-declared) constructors.
329 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
330 // Note that we have a user-declared constructor.
331 data().UserDeclaredConstructor = true;
332
333 // Note that we have no need of an implicitly-declared default constructor.
334 data().DeclaredDefaultConstructor = true;
335
336 // C++ [dcl.init.aggr]p1:
337 // An aggregate is an array or a class (clause 9) with no
338 // user-declared constructors (12.1) [...].
339 data().Aggregate = false;
340
341 // C++ [class]p4:
342 // A POD-struct is an aggregate class [...]
343 data().PlainOldData = false;
344
345 // C++ [class.ctor]p5:
346 // A constructor is trivial if it is an implicitly-declared default
347 // constructor.
348 // FIXME: C++0x: don't do this for "= default" default constructors.
349 data().HasTrivialConstructor = false;
350
351 // Note when we have a user-declared copy constructor, which will
352 // suppress the implicit declaration of a copy constructor.
353 if (!FunTmpl && Constructor->isCopyConstructor()) {
354 data().UserDeclaredCopyConstructor = true;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000355 data().DeclaredCopyConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000356
357 // C++ [class.copy]p6:
358 // A copy constructor is trivial if it is implicitly declared.
359 // FIXME: C++0x: don't do this for "= default" copy constructors.
360 data().HasTrivialCopyConstructor = false;
361 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000362
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000363 return;
364 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000365
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000366 // Handle (user-declared) destructors.
367 if (isa<CXXDestructorDecl>(D)) {
368 data().DeclaredDestructor = true;
369 data().UserDeclaredDestructor = true;
Douglas Gregor6e3c7712010-09-27 23:16:44 +0000370
371 // C++ [class]p4:
372 // A POD-struct is an aggregate class that has [...] no user-defined
373 // destructor.
374 data().PlainOldData = false;
375
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000376 return;
377 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000378
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000379 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000380 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
381 if (Method->getOverloadedOperator() == OO_Equal) {
382 // We're interested specifically in copy assignment operators.
383 const FunctionProtoType *FnType
384 = Method->getType()->getAs<FunctionProtoType>();
385 assert(FnType && "Overloaded operator has no proto function type.");
386 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
387
388 // Copy assignment operators must be non-templates.
389 if (Method->getPrimaryTemplate() || FunTmpl)
390 return;
391
392 ASTContext &Context = getASTContext();
393 QualType ArgType = FnType->getArgType(0);
394 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
395 ArgType = Ref->getPointeeType();
396
397 ArgType = ArgType.getUnqualifiedType();
398 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
399 const_cast<CXXRecordDecl*>(this)));
400
401 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
402 return;
403
404 // This is a copy assignment operator.
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000405 // FIXME: Move assignment operators.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000406
407 // Suppress the implicit declaration of a copy constructor.
408 data().UserDeclaredCopyAssignment = true;
409 data().DeclaredCopyAssignment = true;
410
411 // C++ [class.copy]p11:
412 // A copy assignment operator is trivial if it is implicitly declared.
413 // FIXME: C++0x: don't do this for "= default" copy operators.
414 data().HasTrivialCopyAssignment = false;
415
416 // C++ [class]p4:
417 // A POD-struct is an aggregate class that [...] has no user-defined copy
418 // assignment operator [...].
419 data().PlainOldData = false;
420 }
Douglas Gregor22584312010-07-02 23:41:54 +0000421
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000422 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000423 }
Douglas Gregor6e3c7712010-09-27 23:16:44 +0000424
425 // Handle non-static data members.
426 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
427 // C++ [dcl.init.aggr]p1:
428 // An aggregate is an array or a class (clause 9) with [...] no
429 // private or protected non-static data members (clause 11).
430 //
431 // A POD must be an aggregate.
432 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
433 data().Aggregate = false;
434 data().PlainOldData = false;
435 }
436
437 // C++ [class]p9:
438 // A POD struct is a class that is both a trivial class and a
439 // standard-layout class, and has no non-static data members of type
440 // non-POD struct, non-POD union (or array of such types).
441 ASTContext &Context = getASTContext();
442 QualType T = Context.getBaseElementType(Field->getType());
443 if (!T->isPODType())
444 data().PlainOldData = false;
Douglas Gregorcdbfa6c2010-09-27 23:31:14 +0000445
446 // If this is not a zero-length bit-field, then the class is not empty.
447 if (data().Empty) {
448 if (!Field->getBitWidth())
449 data().Empty = false;
450 else if (!Field->getBitWidth()->isTypeDependent() &&
451 !Field->getBitWidth()->isValueDependent()) {
452 llvm::APSInt Bits;
453 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
454 if (!!Bits)
455 data().Empty = false;
456 }
457 }
Douglas Gregor6e3c7712010-09-27 23:16:44 +0000458 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000459}
460
John McCallb05b5f32010-03-15 09:07:48 +0000461static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
462 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000463 if (isa<UsingShadowDecl>(Conv))
464 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000465 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
466 T = ConvTemp->getTemplatedDecl()->getResultType();
467 else
468 T = cast<CXXConversionDecl>(Conv)->getConversionType();
469 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000470}
471
John McCallb05b5f32010-03-15 09:07:48 +0000472/// Collect the visible conversions of a base class.
473///
474/// \param Base a base class of the class we're considering
475/// \param InVirtual whether this base class is a virtual base (or a base
476/// of a virtual base)
477/// \param Access the access along the inheritance path to this base
478/// \param ParentHiddenTypes the conversions provided by the inheritors
479/// of this base
480/// \param Output the set to which to add conversions from non-virtual bases
481/// \param VOutput the set to which to add conversions from virtual bases
482/// \param HiddenVBaseCs the set of conversions which were hidden in a
483/// virtual base along some inheritance path
484static void CollectVisibleConversions(ASTContext &Context,
485 CXXRecordDecl *Record,
486 bool InVirtual,
487 AccessSpecifier Access,
488 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
489 UnresolvedSetImpl &Output,
490 UnresolvedSetImpl &VOutput,
491 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
492 // The set of types which have conversions in this class or its
493 // subclasses. As an optimization, we don't copy the derived set
494 // unless it might change.
495 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
496 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
497
498 // Collect the direct conversions and figure out which conversions
499 // will be hidden in the subclasses.
500 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
501 if (!Cs.empty()) {
502 HiddenTypesBuffer = ParentHiddenTypes;
503 HiddenTypes = &HiddenTypesBuffer;
504
505 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
506 bool Hidden =
507 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
508
509 // If this conversion is hidden and we're in a virtual base,
510 // remember that it's hidden along some inheritance path.
511 if (Hidden && InVirtual)
512 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
513
514 // If this conversion isn't hidden, add it to the appropriate output.
515 else if (!Hidden) {
516 AccessSpecifier IAccess
517 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
518
519 if (InVirtual)
520 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000521 else
John McCallb05b5f32010-03-15 09:07:48 +0000522 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000523 }
524 }
525 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000526
John McCallb05b5f32010-03-15 09:07:48 +0000527 // Collect information recursively from any base classes.
528 for (CXXRecordDecl::base_class_iterator
529 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
530 const RecordType *RT = I->getType()->getAs<RecordType>();
531 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000532
John McCallb05b5f32010-03-15 09:07:48 +0000533 AccessSpecifier BaseAccess
534 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
535 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000536
John McCallb05b5f32010-03-15 09:07:48 +0000537 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
538 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
539 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000540 }
John McCallb05b5f32010-03-15 09:07:48 +0000541}
Sebastian Redl9994a342009-10-25 17:03:50 +0000542
John McCallb05b5f32010-03-15 09:07:48 +0000543/// Collect the visible conversions of a class.
544///
545/// This would be extremely straightforward if it weren't for virtual
546/// bases. It might be worth special-casing that, really.
547static void CollectVisibleConversions(ASTContext &Context,
548 CXXRecordDecl *Record,
549 UnresolvedSetImpl &Output) {
550 // The collection of all conversions in virtual bases that we've
551 // found. These will be added to the output as long as they don't
552 // appear in the hidden-conversions set.
553 UnresolvedSet<8> VBaseCs;
554
555 // The set of conversions in virtual bases that we've determined to
556 // be hidden.
557 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
558
559 // The set of types hidden by classes derived from this one.
560 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
561
562 // Go ahead and collect the direct conversions and add them to the
563 // hidden-types set.
564 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
565 Output.append(Cs.begin(), Cs.end());
566 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
567 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
568
569 // Recursively collect conversions from base classes.
570 for (CXXRecordDecl::base_class_iterator
571 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
572 const RecordType *RT = I->getType()->getAs<RecordType>();
573 if (!RT) continue;
574
575 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
576 I->isVirtual(), I->getAccessSpecifier(),
577 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
578 }
579
580 // Add any unhidden conversions provided by virtual bases.
581 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
582 I != E; ++I) {
583 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
584 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000585 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000586}
587
588/// getVisibleConversionFunctions - get all conversion functions visible
589/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000590const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000591 // If root class, all conversions are visible.
592 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000593 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000594 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000595 if (data().ComputedVisibleConversions)
596 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000597 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000598 data().ComputedVisibleConversions = true;
599 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000600}
601
John McCall32daa422010-03-31 01:36:47 +0000602#ifndef NDEBUG
603void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000604 assert(ConvDecl->getDeclContext() == this &&
605 "conversion function does not belong to this record");
606
John McCall32daa422010-03-31 01:36:47 +0000607 ConvDecl = ConvDecl->getUnderlyingDecl();
608 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
609 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
610 } else {
611 assert(isa<CXXConversionDecl>(ConvDecl));
612 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000613}
John McCall32daa422010-03-31 01:36:47 +0000614#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000615
John McCall32daa422010-03-31 01:36:47 +0000616void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
617 // This operation is O(N) but extremely rare. Sema only uses it to
618 // remove UsingShadowDecls in a class that were followed by a direct
619 // declaration, e.g.:
620 // class A : B {
621 // using B::operator int;
622 // operator int();
623 // };
624 // This is uncommon by itself and even more uncommon in conjunction
625 // with sufficiently large numbers of directly-declared conversions
626 // that asymptotic behavior matters.
627
628 UnresolvedSetImpl &Convs = *getConversionFunctions();
629 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
630 if (Convs[I].getDecl() == ConvDecl) {
631 Convs.erase(I);
632 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
633 && "conversion was found multiple times in unresolved set");
634 return;
635 }
636 }
637
638 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000639}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000640
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000641void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
642 Method->setVirtualAsWritten(true);
Fariborz Jahaniane7184df2009-12-03 18:44:40 +0000643 setPolymorphic(true);
644 setHasTrivialConstructor(false);
645 setHasTrivialCopyConstructor(false);
646 setHasTrivialCopyAssignment(false);
647}
648
Douglas Gregorf6b11852009-10-08 15:14:33 +0000649CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000650 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000651 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
652
653 return 0;
654}
655
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000656MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
657 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
658}
659
Douglas Gregorf6b11852009-10-08 15:14:33 +0000660void
661CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
662 TemplateSpecializationKind TSK) {
663 assert(TemplateOrInstantiation.isNull() &&
664 "Previous template or instantiation?");
665 assert(!isa<ClassTemplateSpecializationDecl>(this));
666 TemplateOrInstantiation
667 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
668}
669
Anders Carlssonb13e3572009-12-07 06:33:48 +0000670TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
671 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000672 = dyn_cast<ClassTemplateSpecializationDecl>(this))
673 return Spec->getSpecializationKind();
674
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000675 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000676 return MSInfo->getTemplateSpecializationKind();
677
678 return TSK_Undeclared;
679}
680
681void
682CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
683 if (ClassTemplateSpecializationDecl *Spec
684 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
685 Spec->setSpecializationKind(TSK);
686 return;
687 }
688
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000689 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000690 MSInfo->setTemplateSpecializationKind(TSK);
691 return;
692 }
693
694 assert(false && "Not a class template or member class specialization");
695}
696
Douglas Gregor1d110e02010-07-01 14:13:13 +0000697CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
698 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000699 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000700
701 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000702 = Context.DeclarationNames.getCXXDestructorName(
703 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000704
John McCallc0bf4622010-02-23 00:48:20 +0000705 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000706 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000707 if (I == E)
708 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000710 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000711 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Anders Carlsson7267c162009-05-29 21:03:38 +0000713 return Dtor;
714}
715
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000716CXXMethodDecl *
717CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000718 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000719 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000720 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000721 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000722 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000723}
724
Douglas Gregor90916562009-09-29 18:16:17 +0000725bool CXXMethodDecl::isUsualDeallocationFunction() const {
726 if (getOverloadedOperator() != OO_Delete &&
727 getOverloadedOperator() != OO_Array_Delete)
728 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000729
730 // C++ [basic.stc.dynamic.deallocation]p2:
731 // A template instance is never a usual deallocation function,
732 // regardless of its signature.
733 if (getPrimaryTemplate())
734 return false;
735
Douglas Gregor90916562009-09-29 18:16:17 +0000736 // C++ [basic.stc.dynamic.deallocation]p2:
737 // If a class T has a member deallocation function named operator delete
738 // with exactly one parameter, then that function is a usual (non-placement)
739 // deallocation function. [...]
740 if (getNumParams() == 1)
741 return true;
742
743 // C++ [basic.stc.dynamic.deallocation]p2:
744 // [...] If class T does not declare such an operator delete but does
745 // declare a member deallocation function named operator delete with
746 // exactly two parameters, the second of which has type std::size_t (18.1),
747 // then this function is a usual deallocation function.
748 ASTContext &Context = getASTContext();
749 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000750 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
751 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000752 return false;
753
754 // This function is a usual deallocation function if there are no
755 // single-parameter deallocation functions of the same kind.
756 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
757 R.first != R.second; ++R.first) {
758 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
759 if (FD->getNumParams() == 1)
760 return false;
761 }
762
763 return true;
764}
765
Douglas Gregor06a9f362010-05-01 20:49:11 +0000766bool CXXMethodDecl::isCopyAssignmentOperator() const {
767 // C++0x [class.copy]p19:
768 // A user-declared copy assignment operator X::operator= is a non-static
769 // non-template member function of class X with exactly one parameter of
770 // type X, X&, const X&, volatile X& or const volatile X&.
771 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
772 /*non-static*/ isStatic() ||
773 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
774 /*exactly one parameter*/getNumParams() != 1)
775 return false;
776
777 QualType ParamType = getParamDecl(0)->getType();
778 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
779 ParamType = Ref->getPointeeType();
780
781 ASTContext &Context = getASTContext();
782 QualType ClassType
783 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
784 return Context.hasSameUnqualifiedType(ClassType, ParamType);
785}
786
Anders Carlsson05eb2442009-05-16 23:58:37 +0000787void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000788 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000789 assert(!MD->getParent()->isDependentContext() &&
790 "Can't add an overridden method to a class template!");
791
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000792 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000793}
794
795CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000796 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000797}
798
799CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000800 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000801}
802
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000803unsigned CXXMethodDecl::size_overridden_methods() const {
804 return getASTContext().overridden_methods_size(this);
805}
806
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000807QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000808 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
809 // If the member function is declared const, the type of this is const X*,
810 // if the member function is declared volatile, the type of this is
811 // volatile X*, and if the member function is declared const volatile,
812 // the type of this is const volatile X*.
813
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000814 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000815
John McCall3cb0ebd2010-03-10 03:28:59 +0000816 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000817 ClassTy = C.getQualifiedType(ClassTy,
818 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000819 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000820}
821
Eli Friedmand7d7f672009-12-06 20:50:05 +0000822bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000823 // If this function is a template instantiation, look at the template from
824 // which it was instantiated.
825 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
826 if (!CheckFn)
827 CheckFn = this;
828
Eli Friedmand7d7f672009-12-06 20:50:05 +0000829 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000830 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000831}
832
Douglas Gregor7ad83902008-11-05 04:29:56 +0000833CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000834CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +0000835 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000836 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000837 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
838 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
839 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000840{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000841}
842
843CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000844CXXBaseOrMemberInitializer(ASTContext &Context,
845 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000846 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000847 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
848 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
849 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000850{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000851}
852
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000853CXXBaseOrMemberInitializer::
854CXXBaseOrMemberInitializer(ASTContext &Context,
855 FieldDecl *Member, SourceLocation MemberLoc,
856 SourceLocation L, Expr *Init, SourceLocation R,
857 VarDecl **Indices,
858 unsigned NumIndices)
859 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000860 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
861 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000862{
863 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
864 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
865}
866
867CXXBaseOrMemberInitializer *
868CXXBaseOrMemberInitializer::Create(ASTContext &Context,
869 FieldDecl *Member,
870 SourceLocation MemberLoc,
871 SourceLocation L,
872 Expr *Init,
873 SourceLocation R,
874 VarDecl **Indices,
875 unsigned NumIndices) {
876 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
877 sizeof(VarDecl *) * NumIndices,
878 llvm::alignof<CXXBaseOrMemberInitializer>());
879 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
880 L, Init, R, Indices, NumIndices);
881}
882
Douglas Gregor802ab452009-12-02 22:36:29 +0000883TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
884 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000885 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000886 else
887 return TypeLoc();
888}
889
890Type *CXXBaseOrMemberInitializer::getBaseClass() {
891 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000892 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000893 else
894 return 0;
895}
896
897const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
898 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000899 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000900 else
901 return 0;
902}
903
904SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
905 if (isMemberInitializer())
906 return getMemberLocation();
907
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000908 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +0000909}
910
911SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
912 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000913}
914
Douglas Gregorb48fe382008-10-31 09:07:45 +0000915CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000916CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000917 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000918 QualType(), 0, false, false, false);
919}
920
921CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +0000922CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000923 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000924 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000925 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000926 bool isInline,
927 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000928 assert(NameInfo.getName().getNameKind()
929 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000930 "Name must refer to a constructor");
Abramo Bagnara25777432010-08-11 22:01:17 +0000931 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000932 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000933}
934
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000935bool CXXConstructorDecl::isDefaultConstructor() const {
936 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000937 // A default constructor for a class X is a constructor of class
938 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000939 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000940 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000941}
942
Mike Stump1eb44332009-09-09 15:08:12 +0000943bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000944CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000945 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000946 // A non-template constructor for class X is a copy constructor
947 // if its first parameter is of type X&, const X&, volatile X& or
948 // const volatile X&, and either there are no other parameters
949 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000950 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000951 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000952 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000953 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000954 return false;
955
956 const ParmVarDecl *Param = getParamDecl(0);
957
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000958 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000959 const LValueReferenceType *ParamRefType =
960 Param->getType()->getAs<LValueReferenceType>();
961 if (!ParamRefType)
962 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000963
Douglas Gregorfd476482009-11-13 23:59:09 +0000964 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000965 ASTContext &Context = getASTContext();
966
Douglas Gregorfd476482009-11-13 23:59:09 +0000967 CanQualType PointeeType
968 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000969 CanQualType ClassTy
970 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000971 if (PointeeType.getUnqualifiedType() != ClassTy)
972 return false;
973
John McCall0953e762009-09-24 19:53:00 +0000974 // FIXME: other qualifiers?
975
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000976 // We have a copy constructor.
977 TypeQuals = PointeeType.getCVRQualifiers();
978 return true;
979}
980
Anders Carlssonfaccd722009-08-28 16:57:08 +0000981bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000982 // C++ [class.conv.ctor]p1:
983 // A constructor declared without the function-specifier explicit
984 // that can be called with a single parameter specifies a
985 // conversion from the type of its first parameter to the type of
986 // its class. Such a constructor is called a converting
987 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000988 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000989 return false;
990
Mike Stump1eb44332009-09-09 15:08:12 +0000991 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000992 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000993 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000994 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000995}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000996
Douglas Gregor66724ea2009-11-14 01:20:54 +0000997bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
998 if ((getNumParams() < 1) ||
999 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1000 (getPrimaryTemplate() == 0) ||
1001 (getDescribedFunctionTemplate() != 0))
1002 return false;
1003
1004 const ParmVarDecl *Param = getParamDecl(0);
1005
1006 ASTContext &Context = getASTContext();
1007 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1008
1009 // Strip off the lvalue reference, if any.
1010 if (CanQual<LValueReferenceType> ParamRefType
1011 = ParamType->getAs<LValueReferenceType>())
1012 ParamType = ParamRefType->getPointeeType();
1013
1014
1015 // Is it the same as our our class type?
1016 CanQualType ClassTy
1017 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1018 if (ParamType.getUnqualifiedType() != ClassTy)
1019 return false;
1020
1021 return true;
1022}
1023
Douglas Gregor42a552f2008-11-05 20:51:48 +00001024CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001025CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001026 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001027 QualType(), false, false);
1028}
1029
1030CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001031CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001032 const DeclarationNameInfo &NameInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001033 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001034 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001035 assert(NameInfo.getName().getNameKind()
1036 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001037 "Name must refer to a destructor");
Abramo Bagnara25777432010-08-11 22:01:17 +00001038 return new (C) CXXDestructorDecl(RD, NameInfo, T, isInline,
1039 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001040}
1041
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001042CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001043CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001044 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001045 QualType(), 0, false, false);
1046}
1047
1048CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001049CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001050 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001051 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001052 bool isInline, bool isExplicit) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001053 assert(NameInfo.getName().getNameKind()
1054 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001055 "Name must refer to a conversion function");
Abramo Bagnara25777432010-08-11 22:01:17 +00001056 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
1057 isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001058}
1059
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001060LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001061 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001062 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +00001063 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +00001064 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001065}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001066
1067UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1068 SourceLocation L,
1069 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001070 SourceRange QualifierRange,
1071 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001072 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001073 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001074 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001075 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1076 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001077 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001078 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001079}
1080
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001081NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1082 if (NamespaceAliasDecl *NA =
1083 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1084 return NA->getNamespace();
1085 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1086}
1087
Mike Stump1eb44332009-09-09 15:08:12 +00001088NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001089 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001090 SourceLocation AliasLoc,
1091 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001092 SourceRange QualifierRange,
1093 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001094 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001095 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001096 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1097 Namespace = NS->getOriginalNamespace();
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001098 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001099 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001100}
1101
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001102UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001103 SourceRange NNR, SourceLocation UL,
1104 NestedNameSpecifier* TargetNNS,
1105 const DeclarationNameInfo &NameInfo,
1106 bool IsTypeNameArg) {
1107 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001108}
1109
John McCall7ba107a2009-11-18 02:36:19 +00001110UnresolvedUsingValueDecl *
1111UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1112 SourceLocation UsingLoc,
1113 SourceRange TargetNNR,
1114 NestedNameSpecifier *TargetNNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001115 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001116 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001117 TargetNNR, TargetNNS, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001118}
1119
1120UnresolvedUsingTypenameDecl *
1121UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1122 SourceLocation UsingLoc,
1123 SourceLocation TypenameLoc,
1124 SourceRange TargetNNR,
1125 NestedNameSpecifier *TargetNNS,
1126 SourceLocation TargetNameLoc,
1127 DeclarationName TargetName) {
1128 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1129 TargetNNR, TargetNNS,
1130 TargetNameLoc,
1131 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001132}
1133
Anders Carlssonfb311762009-03-14 00:25:26 +00001134StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1135 SourceLocation L, Expr *AssertExpr,
1136 StringLiteral *Message) {
1137 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1138}
1139
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001140static const char *getAccessName(AccessSpecifier AS) {
1141 switch (AS) {
1142 default:
1143 case AS_none:
1144 assert("Invalid access specifier!");
1145 return 0;
1146 case AS_public:
1147 return "public";
1148 case AS_private:
1149 return "private";
1150 case AS_protected:
1151 return "protected";
1152 }
1153}
1154
1155const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1156 AccessSpecifier AS) {
1157 return DB << getAccessName(AS);
1158}