blob: 440eaddb7c160160d8765aff70599beea980dd17 [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
Anders Carlsson6f6de732010-03-29 05:13:12 +0000108 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000109 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000110 BaseClassDecl->vbases_begin(),
111 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000112 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000113 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000114 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000115 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000116
117 if (Base->isVirtual()) {
118 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000119 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000120 VBases.push_back(Base);
121 }
Bill Wendling2a674e82010-09-28 01:09:49 +0000122
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000123 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000124
125 if (VBases.empty())
126 return;
127
128 // Create base specifier for any direct or indirect virtual bases.
129 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
130 data().NumVBases = VBases.size();
131 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000132 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
133
Anders Carlsson6f6de732010-03-29 05:13:12 +0000134 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000135 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000136 continue;
137
Nick Lewycky56062202010-07-26 16:56:01 +0000138 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
139 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000140
141 data().VBases[I] =
142 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000143 VBaseClassDecl->getTagKind() == TTK_Class,
Nick Lewycky56062202010-07-26 16:56:01 +0000144 VBases[I]->getAccessSpecifier(), VBaseTypeInfo);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000145 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000146}
147
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000148/// Callback function for CXXRecordDecl::forallBases that acknowledges
149/// that it saw a base class.
150static bool SawBase(const CXXRecordDecl *, void *) {
151 return true;
152}
153
154bool CXXRecordDecl::hasAnyDependentBases() const {
155 if (!isDependentContext())
156 return false;
157
158 return !forallBases(SawBase, 0);
159}
160
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000161bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000162 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000163}
164
Douglas Gregor0d405db2010-07-01 20:59:04 +0000165/// \brief Perform a simplistic form of overload resolution that only considers
166/// cv-qualifiers on a single parameter, and return the best overload candidate
167/// (if there is one).
168static CXXMethodDecl *
169GetBestOverloadCandidateSimple(
170 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
171 if (Cands.empty())
172 return 0;
173 if (Cands.size() == 1)
174 return Cands[0].first;
175
176 unsigned Best = 0, N = Cands.size();
177 for (unsigned I = 1; I != N; ++I)
178 if (Cands[Best].second.isSupersetOf(Cands[I].second))
179 Best = I;
180
181 for (unsigned I = 1; I != N; ++I)
182 if (Cands[Best].second.isSupersetOf(Cands[I].second))
183 return 0;
184
185 return Cands[Best].first;
186}
187
Mike Stump1eb44332009-09-09 15:08:12 +0000188CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000189 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000190 QualType ClassType
191 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000192 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000193 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000194 Context.getCanonicalType(ClassType));
195 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000196 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000197 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000198 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000199 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000200 // C++ [class.copy]p2:
201 // A non-template constructor for class X is a copy constructor if [...]
202 if (isa<FunctionTemplateDecl>(*Con))
203 continue;
204
Douglas Gregor0d405db2010-07-01 20:59:04 +0000205 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
206 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000207 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
208 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000209 Found.push_back(std::make_pair(
210 const_cast<CXXConstructorDecl *>(Constructor),
211 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000212 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000213 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000214
215 return cast_or_null<CXXConstructorDecl>(
216 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000217}
218
Douglas Gregorb87786f2010-07-01 17:48:08 +0000219CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
220 ASTContext &Context = getASTContext();
221 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
222 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
223
224 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
225 DeclContext::lookup_const_iterator Op, OpEnd;
226 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
227 // C++ [class.copy]p9:
228 // A user-declared copy assignment operator is a non-static non-template
229 // member function of class X with exactly one parameter of type X, X&,
230 // const X&, volatile X& or const volatile X&.
231 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
232 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
233 continue;
234
235 const FunctionProtoType *FnType
236 = Method->getType()->getAs<FunctionProtoType>();
237 assert(FnType && "Overloaded operator has no prototype.");
238 // Don't assert on this; an invalid decl might have been left in the AST.
239 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
240 continue;
241
242 QualType ArgType = FnType->getArgType(0);
243 Qualifiers Quals;
244 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
245 ArgType = Ref->getPointeeType();
246 // If we have a const argument and we have a reference to a non-const,
247 // this function does not match.
248 if (ArgIsConst && !ArgType.isConstQualified())
249 continue;
250
251 Quals = ArgType.getQualifiers();
252 } else {
253 // By-value copy-assignment operators are treated like const X&
254 // copy-assignment operators.
255 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
256 }
257
258 if (!Context.hasSameUnqualifiedType(ArgType, Class))
259 continue;
260
261 // Save this copy-assignment operator. It might be "the one".
262 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
263 }
264
265 // Use a simplistic form of overload resolution to find the candidate.
266 return GetBestOverloadCandidateSimple(Found);
267}
268
Sebastian Redl64b45f72009-01-05 20:52:13 +0000269void
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000270CXXRecordDecl::addedMember(Decl *D) {
271 // Ignore friends and invalid declarations.
272 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000273 return;
274
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000275 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
276 if (FunTmpl)
277 D = FunTmpl->getTemplatedDecl();
278
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000279 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
280 if (Method->isVirtual()) {
281 // C++ [dcl.init.aggr]p1:
282 // An aggregate is an array or a class with [...] no virtual functions.
283 data().Aggregate = false;
284
285 // C++ [class]p4:
286 // A POD-struct is an aggregate class...
287 data().PlainOldData = false;
288 }
289 }
290
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000291 if (D->isImplicit()) {
292 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
293 // If this is the implicit default constructor, note that we have now
294 // declared it.
295 if (Constructor->isDefaultConstructor())
296 data().DeclaredDefaultConstructor = true;
297 // If this is the implicit copy constructor, note that we have now
298 // declared it.
299 else if (Constructor->isCopyConstructor())
300 data().DeclaredCopyConstructor = true;
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000301 } else if (isa<CXXDestructorDecl>(D)) {
302 data().DeclaredDestructor = true;
303 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000304 // If this is the implicit copy constructor, note that we have now
305 // declared it.
306 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000307 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000308 data().DeclaredCopyAssignment = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000309 }
310
311 // Nothing else to do for implicitly-declared members.
312 return;
313 }
314
315 // Handle (user-declared) constructors.
316 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
317 // Note that we have a user-declared constructor.
318 data().UserDeclaredConstructor = true;
319
320 // Note that we have no need of an implicitly-declared default constructor.
321 data().DeclaredDefaultConstructor = true;
322
323 // C++ [dcl.init.aggr]p1:
324 // An aggregate is an array or a class (clause 9) with no
325 // user-declared constructors (12.1) [...].
326 data().Aggregate = false;
327
328 // C++ [class]p4:
329 // A POD-struct is an aggregate class [...]
330 data().PlainOldData = false;
331
332 // C++ [class.ctor]p5:
333 // A constructor is trivial if it is an implicitly-declared default
334 // constructor.
335 // FIXME: C++0x: don't do this for "= default" default constructors.
336 data().HasTrivialConstructor = false;
337
338 // Note when we have a user-declared copy constructor, which will
339 // suppress the implicit declaration of a copy constructor.
340 if (!FunTmpl && Constructor->isCopyConstructor()) {
341 data().UserDeclaredCopyConstructor = true;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000342 data().DeclaredCopyConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000343
344 // C++ [class.copy]p6:
345 // A copy constructor is trivial if it is implicitly declared.
346 // FIXME: C++0x: don't do this for "= default" copy constructors.
347 data().HasTrivialCopyConstructor = false;
348 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000349
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000350 return;
351 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000352
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000353 // Handle (user-declared) destructors.
354 if (isa<CXXDestructorDecl>(D)) {
355 data().DeclaredDestructor = true;
356 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000357
358 // C++ [class]p4:
359 // A POD-struct is an aggregate class that has [...] no user-defined
360 // destructor.
361 data().PlainOldData = false;
362
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000363 return;
364 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000365
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000366 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000367 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
368 if (Method->getOverloadedOperator() == OO_Equal) {
369 // We're interested specifically in copy assignment operators.
370 const FunctionProtoType *FnType
371 = Method->getType()->getAs<FunctionProtoType>();
372 assert(FnType && "Overloaded operator has no proto function type.");
373 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
374
375 // Copy assignment operators must be non-templates.
376 if (Method->getPrimaryTemplate() || FunTmpl)
377 return;
378
379 ASTContext &Context = getASTContext();
380 QualType ArgType = FnType->getArgType(0);
381 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
382 ArgType = Ref->getPointeeType();
383
384 ArgType = ArgType.getUnqualifiedType();
385 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
386 const_cast<CXXRecordDecl*>(this)));
387
388 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
389 return;
390
391 // This is a copy assignment operator.
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000392 // FIXME: Move assignment operators.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000393
394 // Suppress the implicit declaration of a copy constructor.
395 data().UserDeclaredCopyAssignment = true;
396 data().DeclaredCopyAssignment = true;
397
398 // C++ [class.copy]p11:
399 // A copy assignment operator is trivial if it is implicitly declared.
400 // FIXME: C++0x: don't do this for "= default" copy operators.
401 data().HasTrivialCopyAssignment = false;
402
403 // C++ [class]p4:
404 // A POD-struct is an aggregate class that [...] has no user-defined copy
405 // assignment operator [...].
406 data().PlainOldData = false;
407 }
Douglas Gregor22584312010-07-02 23:41:54 +0000408
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000409 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000410 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000411
412 // Handle non-static data members.
413 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
414 // C++ [dcl.init.aggr]p1:
415 // An aggregate is an array or a class (clause 9) with [...] no
416 // private or protected non-static data members (clause 11).
417 //
418 // A POD must be an aggregate.
419 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
420 data().Aggregate = false;
421 data().PlainOldData = false;
422 }
423
424 // C++ [class]p9:
425 // A POD struct is a class that is both a trivial class and a
426 // standard-layout class, and has no non-static data members of type
427 // non-POD struct, non-POD union (or array of such types).
428 ASTContext &Context = getASTContext();
429 QualType T = Context.getBaseElementType(Field->getType());
430 if (!T->isPODType())
431 data().PlainOldData = false;
432 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000433}
434
John McCallb05b5f32010-03-15 09:07:48 +0000435static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
436 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000437 if (isa<UsingShadowDecl>(Conv))
438 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000439 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
440 T = ConvTemp->getTemplatedDecl()->getResultType();
441 else
442 T = cast<CXXConversionDecl>(Conv)->getConversionType();
443 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000444}
445
John McCallb05b5f32010-03-15 09:07:48 +0000446/// Collect the visible conversions of a base class.
447///
448/// \param Base a base class of the class we're considering
449/// \param InVirtual whether this base class is a virtual base (or a base
450/// of a virtual base)
451/// \param Access the access along the inheritance path to this base
452/// \param ParentHiddenTypes the conversions provided by the inheritors
453/// of this base
454/// \param Output the set to which to add conversions from non-virtual bases
455/// \param VOutput the set to which to add conversions from virtual bases
456/// \param HiddenVBaseCs the set of conversions which were hidden in a
457/// virtual base along some inheritance path
458static void CollectVisibleConversions(ASTContext &Context,
459 CXXRecordDecl *Record,
460 bool InVirtual,
461 AccessSpecifier Access,
462 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
463 UnresolvedSetImpl &Output,
464 UnresolvedSetImpl &VOutput,
465 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
466 // The set of types which have conversions in this class or its
467 // subclasses. As an optimization, we don't copy the derived set
468 // unless it might change.
469 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
470 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
471
472 // Collect the direct conversions and figure out which conversions
473 // will be hidden in the subclasses.
474 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
475 if (!Cs.empty()) {
476 HiddenTypesBuffer = ParentHiddenTypes;
477 HiddenTypes = &HiddenTypesBuffer;
478
479 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
480 bool Hidden =
481 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
482
483 // If this conversion is hidden and we're in a virtual base,
484 // remember that it's hidden along some inheritance path.
485 if (Hidden && InVirtual)
486 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
487
488 // If this conversion isn't hidden, add it to the appropriate output.
489 else if (!Hidden) {
490 AccessSpecifier IAccess
491 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
492
493 if (InVirtual)
494 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000495 else
John McCallb05b5f32010-03-15 09:07:48 +0000496 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000497 }
498 }
499 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000500
John McCallb05b5f32010-03-15 09:07:48 +0000501 // Collect information recursively from any base classes.
502 for (CXXRecordDecl::base_class_iterator
503 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
504 const RecordType *RT = I->getType()->getAs<RecordType>();
505 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000506
John McCallb05b5f32010-03-15 09:07:48 +0000507 AccessSpecifier BaseAccess
508 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
509 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000510
John McCallb05b5f32010-03-15 09:07:48 +0000511 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
512 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
513 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000514 }
John McCallb05b5f32010-03-15 09:07:48 +0000515}
Sebastian Redl9994a342009-10-25 17:03:50 +0000516
John McCallb05b5f32010-03-15 09:07:48 +0000517/// Collect the visible conversions of a class.
518///
519/// This would be extremely straightforward if it weren't for virtual
520/// bases. It might be worth special-casing that, really.
521static void CollectVisibleConversions(ASTContext &Context,
522 CXXRecordDecl *Record,
523 UnresolvedSetImpl &Output) {
524 // The collection of all conversions in virtual bases that we've
525 // found. These will be added to the output as long as they don't
526 // appear in the hidden-conversions set.
527 UnresolvedSet<8> VBaseCs;
528
529 // The set of conversions in virtual bases that we've determined to
530 // be hidden.
531 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
532
533 // The set of types hidden by classes derived from this one.
534 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
535
536 // Go ahead and collect the direct conversions and add them to the
537 // hidden-types set.
538 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
539 Output.append(Cs.begin(), Cs.end());
540 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
541 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
542
543 // Recursively collect conversions from base classes.
544 for (CXXRecordDecl::base_class_iterator
545 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
546 const RecordType *RT = I->getType()->getAs<RecordType>();
547 if (!RT) continue;
548
549 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
550 I->isVirtual(), I->getAccessSpecifier(),
551 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
552 }
553
554 // Add any unhidden conversions provided by virtual bases.
555 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
556 I != E; ++I) {
557 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
558 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000559 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000560}
561
562/// getVisibleConversionFunctions - get all conversion functions visible
563/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000564const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000565 // If root class, all conversions are visible.
566 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000567 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000568 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000569 if (data().ComputedVisibleConversions)
570 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000571 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000572 data().ComputedVisibleConversions = true;
573 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000574}
575
John McCall32daa422010-03-31 01:36:47 +0000576#ifndef NDEBUG
577void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000578 assert(ConvDecl->getDeclContext() == this &&
579 "conversion function does not belong to this record");
580
John McCall32daa422010-03-31 01:36:47 +0000581 ConvDecl = ConvDecl->getUnderlyingDecl();
582 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
583 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
584 } else {
585 assert(isa<CXXConversionDecl>(ConvDecl));
586 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000587}
John McCall32daa422010-03-31 01:36:47 +0000588#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000589
John McCall32daa422010-03-31 01:36:47 +0000590void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
591 // This operation is O(N) but extremely rare. Sema only uses it to
592 // remove UsingShadowDecls in a class that were followed by a direct
593 // declaration, e.g.:
594 // class A : B {
595 // using B::operator int;
596 // operator int();
597 // };
598 // This is uncommon by itself and even more uncommon in conjunction
599 // with sufficiently large numbers of directly-declared conversions
600 // that asymptotic behavior matters.
601
602 UnresolvedSetImpl &Convs = *getConversionFunctions();
603 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
604 if (Convs[I].getDecl() == ConvDecl) {
605 Convs.erase(I);
606 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
607 && "conversion was found multiple times in unresolved set");
608 return;
609 }
610 }
611
612 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000613}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000614
Bill Wendling2a674e82010-09-28 01:09:49 +0000615void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) {
616 Method->setVirtualAsWritten(true);
Bill Wendling2a674e82010-09-28 01:09:49 +0000617 setEmpty(false);
618 setPolymorphic(true);
619 setHasTrivialConstructor(false);
620 setHasTrivialCopyConstructor(false);
621 setHasTrivialCopyAssignment(false);
622}
623
Douglas Gregorf6b11852009-10-08 15:14:33 +0000624CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000625 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000626 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
627
628 return 0;
629}
630
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000631MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
632 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
633}
634
Douglas Gregorf6b11852009-10-08 15:14:33 +0000635void
636CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
637 TemplateSpecializationKind TSK) {
638 assert(TemplateOrInstantiation.isNull() &&
639 "Previous template or instantiation?");
640 assert(!isa<ClassTemplateSpecializationDecl>(this));
641 TemplateOrInstantiation
642 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
643}
644
Anders Carlssonb13e3572009-12-07 06:33:48 +0000645TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
646 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000647 = dyn_cast<ClassTemplateSpecializationDecl>(this))
648 return Spec->getSpecializationKind();
649
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000650 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000651 return MSInfo->getTemplateSpecializationKind();
652
653 return TSK_Undeclared;
654}
655
656void
657CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
658 if (ClassTemplateSpecializationDecl *Spec
659 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
660 Spec->setSpecializationKind(TSK);
661 return;
662 }
663
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000664 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000665 MSInfo->setTemplateSpecializationKind(TSK);
666 return;
667 }
668
669 assert(false && "Not a class template or member class specialization");
670}
671
Douglas Gregor1d110e02010-07-01 14:13:13 +0000672CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
673 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000674 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000675
676 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000677 = Context.DeclarationNames.getCXXDestructorName(
678 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000679
John McCallc0bf4622010-02-23 00:48:20 +0000680 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000681 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000682 if (I == E)
683 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000685 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000686 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Anders Carlsson7267c162009-05-29 21:03:38 +0000688 return Dtor;
689}
690
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000691CXXMethodDecl *
692CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000693 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000694 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000695 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000696 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000697 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000698}
699
Douglas Gregor90916562009-09-29 18:16:17 +0000700bool CXXMethodDecl::isUsualDeallocationFunction() const {
701 if (getOverloadedOperator() != OO_Delete &&
702 getOverloadedOperator() != OO_Array_Delete)
703 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000704
705 // C++ [basic.stc.dynamic.deallocation]p2:
706 // A template instance is never a usual deallocation function,
707 // regardless of its signature.
708 if (getPrimaryTemplate())
709 return false;
710
Douglas Gregor90916562009-09-29 18:16:17 +0000711 // C++ [basic.stc.dynamic.deallocation]p2:
712 // If a class T has a member deallocation function named operator delete
713 // with exactly one parameter, then that function is a usual (non-placement)
714 // deallocation function. [...]
715 if (getNumParams() == 1)
716 return true;
717
718 // C++ [basic.stc.dynamic.deallocation]p2:
719 // [...] If class T does not declare such an operator delete but does
720 // declare a member deallocation function named operator delete with
721 // exactly two parameters, the second of which has type std::size_t (18.1),
722 // then this function is a usual deallocation function.
723 ASTContext &Context = getASTContext();
724 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000725 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
726 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000727 return false;
728
729 // This function is a usual deallocation function if there are no
730 // single-parameter deallocation functions of the same kind.
731 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
732 R.first != R.second; ++R.first) {
733 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
734 if (FD->getNumParams() == 1)
735 return false;
736 }
737
738 return true;
739}
740
Douglas Gregor06a9f362010-05-01 20:49:11 +0000741bool CXXMethodDecl::isCopyAssignmentOperator() const {
742 // C++0x [class.copy]p19:
743 // A user-declared copy assignment operator X::operator= is a non-static
744 // non-template member function of class X with exactly one parameter of
745 // type X, X&, const X&, volatile X& or const volatile X&.
746 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
747 /*non-static*/ isStatic() ||
748 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
749 /*exactly one parameter*/getNumParams() != 1)
750 return false;
751
752 QualType ParamType = getParamDecl(0)->getType();
753 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
754 ParamType = Ref->getPointeeType();
755
756 ASTContext &Context = getASTContext();
757 QualType ClassType
758 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
759 return Context.hasSameUnqualifiedType(ClassType, ParamType);
760}
761
Anders Carlsson05eb2442009-05-16 23:58:37 +0000762void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000763 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000764 assert(!MD->getParent()->isDependentContext() &&
765 "Can't add an overridden method to a class template!");
766
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000767 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000768}
769
770CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000771 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000772}
773
774CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000775 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000776}
777
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000778unsigned CXXMethodDecl::size_overridden_methods() const {
779 return getASTContext().overridden_methods_size(this);
780}
781
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000782QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000783 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
784 // If the member function is declared const, the type of this is const X*,
785 // if the member function is declared volatile, the type of this is
786 // volatile X*, and if the member function is declared const volatile,
787 // the type of this is const volatile X*.
788
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000789 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000790
John McCall3cb0ebd2010-03-10 03:28:59 +0000791 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000792 ClassTy = C.getQualifiedType(ClassTy,
793 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000794 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000795}
796
Eli Friedmand7d7f672009-12-06 20:50:05 +0000797bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000798 // If this function is a template instantiation, look at the template from
799 // which it was instantiated.
800 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
801 if (!CheckFn)
802 CheckFn = this;
803
Eli Friedmand7d7f672009-12-06 20:50:05 +0000804 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000805 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000806}
807
Douglas Gregor7ad83902008-11-05 04:29:56 +0000808CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000809CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +0000810 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000811 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000812 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
813 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
814 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000815{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000816}
817
818CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000819CXXBaseOrMemberInitializer(ASTContext &Context,
820 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000821 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000822 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
823 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
824 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000825{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000826}
827
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000828CXXBaseOrMemberInitializer::
829CXXBaseOrMemberInitializer(ASTContext &Context,
830 FieldDecl *Member, SourceLocation MemberLoc,
831 SourceLocation L, Expr *Init, SourceLocation R,
832 VarDecl **Indices,
833 unsigned NumIndices)
834 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000835 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
836 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000837{
838 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
839 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
840}
841
842CXXBaseOrMemberInitializer *
843CXXBaseOrMemberInitializer::Create(ASTContext &Context,
844 FieldDecl *Member,
845 SourceLocation MemberLoc,
846 SourceLocation L,
847 Expr *Init,
848 SourceLocation R,
849 VarDecl **Indices,
850 unsigned NumIndices) {
851 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
852 sizeof(VarDecl *) * NumIndices,
853 llvm::alignof<CXXBaseOrMemberInitializer>());
854 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
855 L, Init, R, Indices, NumIndices);
856}
857
Douglas Gregor802ab452009-12-02 22:36:29 +0000858TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
859 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000860 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000861 else
862 return TypeLoc();
863}
864
865Type *CXXBaseOrMemberInitializer::getBaseClass() {
866 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000867 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000868 else
869 return 0;
870}
871
872const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
873 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000874 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000875 else
876 return 0;
877}
878
879SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
880 if (isMemberInitializer())
881 return getMemberLocation();
882
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000883 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +0000884}
885
886SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
887 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000888}
889
Douglas Gregorb48fe382008-10-31 09:07:45 +0000890CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000891CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000892 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000893 QualType(), 0, false, false, false);
894}
895
896CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +0000897CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000898 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000899 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000900 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000901 bool isInline,
902 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000903 assert(NameInfo.getName().getNameKind()
904 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000905 "Name must refer to a constructor");
Abramo Bagnara25777432010-08-11 22:01:17 +0000906 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000907 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000908}
909
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000910bool CXXConstructorDecl::isDefaultConstructor() const {
911 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000912 // A default constructor for a class X is a constructor of class
913 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000914 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +0000915 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000916}
917
Mike Stump1eb44332009-09-09 15:08:12 +0000918bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000919CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000920 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +0000921 // A non-template constructor for class X is a copy constructor
922 // if its first parameter is of type X&, const X&, volatile X& or
923 // const volatile X&, and either there are no other parameters
924 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000925 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000926 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +0000927 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +0000928 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000929 return false;
930
931 const ParmVarDecl *Param = getParamDecl(0);
932
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000933 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +0000934 const LValueReferenceType *ParamRefType =
935 Param->getType()->getAs<LValueReferenceType>();
936 if (!ParamRefType)
937 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000938
Douglas Gregorfd476482009-11-13 23:59:09 +0000939 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +0000940 ASTContext &Context = getASTContext();
941
Douglas Gregorfd476482009-11-13 23:59:09 +0000942 CanQualType PointeeType
943 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +0000944 CanQualType ClassTy
945 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000946 if (PointeeType.getUnqualifiedType() != ClassTy)
947 return false;
948
John McCall0953e762009-09-24 19:53:00 +0000949 // FIXME: other qualifiers?
950
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000951 // We have a copy constructor.
952 TypeQuals = PointeeType.getCVRQualifiers();
953 return true;
954}
955
Anders Carlssonfaccd722009-08-28 16:57:08 +0000956bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +0000957 // C++ [class.conv.ctor]p1:
958 // A constructor declared without the function-specifier explicit
959 // that can be called with a single parameter specifies a
960 // conversion from the type of its first parameter to the type of
961 // its class. Such a constructor is called a converting
962 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +0000963 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +0000964 return false;
965
Mike Stump1eb44332009-09-09 15:08:12 +0000966 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +0000967 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +0000968 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +0000969 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +0000970}
Douglas Gregorb48fe382008-10-31 09:07:45 +0000971
Douglas Gregor66724ea2009-11-14 01:20:54 +0000972bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
973 if ((getNumParams() < 1) ||
974 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
975 (getPrimaryTemplate() == 0) ||
976 (getDescribedFunctionTemplate() != 0))
977 return false;
978
979 const ParmVarDecl *Param = getParamDecl(0);
980
981 ASTContext &Context = getASTContext();
982 CanQualType ParamType = Context.getCanonicalType(Param->getType());
983
984 // Strip off the lvalue reference, if any.
985 if (CanQual<LValueReferenceType> ParamRefType
986 = ParamType->getAs<LValueReferenceType>())
987 ParamType = ParamRefType->getPointeeType();
988
989
990 // Is it the same as our our class type?
991 CanQualType ClassTy
992 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
993 if (ParamType.getUnqualifiedType() != ClassTy)
994 return false;
995
996 return true;
997}
998
Douglas Gregor42a552f2008-11-05 20:51:48 +0000999CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001000CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001001 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001002 QualType(), false, false);
1003}
1004
1005CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001006CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001007 const DeclarationNameInfo &NameInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001008 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001009 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001010 assert(NameInfo.getName().getNameKind()
1011 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001012 "Name must refer to a destructor");
Abramo Bagnara25777432010-08-11 22:01:17 +00001013 return new (C) CXXDestructorDecl(RD, NameInfo, T, isInline,
1014 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001015}
1016
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001017CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001018CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001019 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001020 QualType(), 0, false, false);
1021}
1022
1023CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001024CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001025 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001026 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001027 bool isInline, bool isExplicit) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001028 assert(NameInfo.getName().getNameKind()
1029 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001030 "Name must refer to a conversion function");
Abramo Bagnara25777432010-08-11 22:01:17 +00001031 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
1032 isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001033}
1034
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001035LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001036 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001037 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +00001038 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +00001039 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001040}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001041
1042UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1043 SourceLocation L,
1044 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001045 SourceRange QualifierRange,
1046 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001047 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001048 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001049 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001050 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1051 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001052 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001053 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001054}
1055
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001056NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1057 if (NamespaceAliasDecl *NA =
1058 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1059 return NA->getNamespace();
1060 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1061}
1062
Mike Stump1eb44332009-09-09 15:08:12 +00001063NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001064 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001065 SourceLocation AliasLoc,
1066 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001067 SourceRange QualifierRange,
1068 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001069 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001070 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001071 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1072 Namespace = NS->getOriginalNamespace();
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001073 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001074 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001075}
1076
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001077UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001078 SourceRange NNR, SourceLocation UL,
1079 NestedNameSpecifier* TargetNNS,
1080 const DeclarationNameInfo &NameInfo,
1081 bool IsTypeNameArg) {
1082 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001083}
1084
John McCall7ba107a2009-11-18 02:36:19 +00001085UnresolvedUsingValueDecl *
1086UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1087 SourceLocation UsingLoc,
1088 SourceRange TargetNNR,
1089 NestedNameSpecifier *TargetNNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001090 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001091 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001092 TargetNNR, TargetNNS, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001093}
1094
1095UnresolvedUsingTypenameDecl *
1096UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1097 SourceLocation UsingLoc,
1098 SourceLocation TypenameLoc,
1099 SourceRange TargetNNR,
1100 NestedNameSpecifier *TargetNNS,
1101 SourceLocation TargetNameLoc,
1102 DeclarationName TargetName) {
1103 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1104 TargetNNR, TargetNNS,
1105 TargetNameLoc,
1106 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001107}
1108
Anders Carlssonfb311762009-03-14 00:25:26 +00001109StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1110 SourceLocation L, Expr *AssertExpr,
1111 StringLiteral *Message) {
1112 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1113}
1114
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001115static const char *getAccessName(AccessSpecifier AS) {
1116 switch (AS) {
1117 default:
1118 case AS_none:
1119 assert("Invalid access specifier!");
1120 return 0;
1121 case AS_public:
1122 return "public";
1123 case AS_private:
1124 return "private";
1125 case AS_protected:
1126 return "protected";
1127 }
1128}
1129
1130const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1131 AccessSpecifier AS) {
1132 return DB << getAccessName(AS);
1133}