blob: 08ac2a5be4d96aae789b285af2cc2572e7ee20f1 [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"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000017#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000018#include "clang/AST/CXXInheritance.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000020#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000021#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000022#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000023#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// Decl Allocation/Deallocation Method Implementations
28//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000029
John McCall86ff3082010-02-04 22:26:26 +000030CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
31 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000032 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false),
33 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000034 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000035 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000036 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Douglas Gregor2bb11012011-05-13 01:05:07 +000037 HasMutableFields(false), HasTrivialDefaultConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000038 HasConstExprNonCopyMoveConstructor(false), HasTrivialCopyConstructor(true),
39 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
40 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
41 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Sean Huntcdee3fe2011-05-11 22:34:38 +000042 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Huntffe37fd2011-05-25 20:50:04 +000043 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false),
44 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false),
Sean Hunt37b8c9e2011-05-09 21:45:35 +000045 DeclaredDestructor(false), NumBases(0), NumVBases(0), Bases(), VBases(),
46 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000047}
48
49CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000050 SourceLocation StartLoc, SourceLocation IdLoc,
51 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
52 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000053 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000054 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000055
Jay Foad4ba2a172011-01-12 09:06:06 +000056CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000057 DeclContext *DC, SourceLocation StartLoc,
58 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000059 CXXRecordDecl* PrevDecl,
60 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000061 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
62 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000063
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000064 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000065 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000066 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000067 return R;
68}
69
Jay Foad4ba2a172011-01-12 09:06:06 +000070CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000071 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
72 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000073}
74
Mike Stump1eb44332009-09-09 15:08:12 +000075void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000076CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000077 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000078 ASTContext &C = getASTContext();
79
Mike Stump1eb44332009-09-09 15:08:12 +000080 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000081 // An aggregate is an array or a class (clause 9) with [...]
82 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000083 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000084
Douglas Gregor7c789c12010-10-29 22:39:52 +000085 if (!data().Bases.isOffset() && data().NumBases > 0)
86 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000087
Anders Carlsson6f6de732010-03-29 05:13:12 +000088 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000089 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000090
91 // The virtual bases of this class.
92 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000093
John McCall86ff3082010-02-04 22:26:26 +000094 data().Bases = new(C) CXXBaseSpecifier [NumBases];
95 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000096 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +000097 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000098 // Keep track of inherited vbases for this base class.
99 const CXXBaseSpecifier *Base = Bases[i];
100 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000101 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000102 if (BaseType->isDependentType())
103 continue;
104 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000105 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000106
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000107 // C++ [dcl.init.aggr]p1:
108 // An aggregate is [...] a class with [...] no base classes [...].
109 data().Aggregate = false;
110
111 // C++ [class]p4:
112 // A POD-struct is an aggregate class...
113 data().PlainOldData = false;
114
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000115 // A class with a non-empty base class is not empty.
116 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000117 if (!BaseClassDecl->isEmpty()) {
118 if (!data().Empty) {
119 // C++0x [class]p7:
120 // A standard-layout class is a class that:
121 // [...]
122 // -- either has no non-static data members in the most derived
123 // class and at most one base class with non-static data members,
124 // or has no base classes with non-static data members, and
125 // If this is the second non-empty base, then neither of these two
126 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000127 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000128 }
129
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000130 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000131 data().HasNoNonEmptyBases = false;
132 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000133
Douglas Gregor85606eb2010-09-28 20:50:54 +0000134 // C++ [class.virtual]p1:
135 // A class that declares or inherits a virtual function is called a
136 // polymorphic class.
137 if (BaseClassDecl->isPolymorphic())
138 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000139
Chandler Carrutha8225442011-04-30 09:17:45 +0000140 // C++0x [class]p7:
141 // A standard-layout class is a class that: [...]
142 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000143 if (!BaseClassDecl->isStandardLayout())
144 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000145
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000146 // Record if this base is the first non-literal field or base.
147 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
148 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000149
Anders Carlsson6f6de732010-03-29 05:13:12 +0000150 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000151 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000152 BaseClassDecl->vbases_begin(),
153 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000154 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000155 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000156 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000157 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000158
159 if (Base->isVirtual()) {
160 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000161 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000162 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000163
164 // C++0x [meta.unary.prop] is_empty:
165 // T is a class type, but not a union type, with ... no virtual base
166 // classes
167 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000168
169 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000170 // A default constructor is trivial [...] if:
171 // -- its class has [...] no virtual bases
172 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000173
174 // C++0x [class.copy]p13:
175 // A copy/move constructor for class X is trivial if it is neither
176 // user-provided nor deleted and if
177 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000178 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000179 data().HasTrivialMoveConstructor = false;
180
181 // C++0x [class.copy]p27:
182 // A copy/move assignment operator for class X is trivial if it is
183 // neither user-provided nor deleted and if
184 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000185 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000186 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000187
188 // C++0x [class]p7:
189 // A standard-layout class is a class that: [...]
190 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000191 data().IsStandardLayout = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000192 } else {
193 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000194 // A default constructor is trivial [...] if:
195 // -- all the direct base classes of its class have trivial default
196 // constructors.
197 if (!BaseClassDecl->hasTrivialDefaultConstructor())
198 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000199
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000200 // C++0x [class.copy]p13:
201 // A copy/move constructor for class X is trivial if [...]
202 // [...]
203 // -- the constructor selected to copy/move each direct base class
204 // subobject is trivial, and
205 // FIXME: C++0x: We need to only consider the selected constructor
206 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000207 if (!BaseClassDecl->hasTrivialCopyConstructor())
208 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000209 if (!BaseClassDecl->hasTrivialMoveConstructor())
210 data().HasTrivialMoveConstructor = false;
211
212 // C++0x [class.copy]p27:
213 // A copy/move assignment operator for class X is trivial if [...]
214 // [...]
215 // -- the assignment operator selected to copy/move each direct base
216 // class subobject is trivial, and
217 // FIXME: C++0x: We need to only consider the selected operator instead
218 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000219 if (!BaseClassDecl->hasTrivialCopyAssignment())
220 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000221 if (!BaseClassDecl->hasTrivialMoveAssignment())
222 data().HasTrivialMoveAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000223 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000224
225 // C++ [class.ctor]p3:
226 // A destructor is trivial if all the direct base classes of its class
227 // have trivial destructors.
228 if (!BaseClassDecl->hasTrivialDestructor())
229 data().HasTrivialDestructor = false;
Douglas Gregor2bb11012011-05-13 01:05:07 +0000230
231 // Keep track of the presence of mutable fields.
232 if (BaseClassDecl->hasMutableFields())
233 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000234 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000235
236 if (VBases.empty())
237 return;
238
239 // Create base specifier for any direct or indirect virtual bases.
240 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
241 data().NumVBases = VBases.size();
242 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000243 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
244
Anders Carlsson6f6de732010-03-29 05:13:12 +0000245 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000246 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000247 continue;
248
Nick Lewycky56062202010-07-26 16:56:01 +0000249 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
250 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000251
Douglas Gregor7c789c12010-10-29 22:39:52 +0000252 data().getVBases()[I] =
Anders Carlsson6f6de732010-03-29 05:13:12 +0000253 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000254 VBaseClassDecl->getTagKind() == TTK_Class,
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000255 VBases[I]->getAccessSpecifier(), VBaseTypeInfo,
256 SourceLocation());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000257 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000258}
259
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000260/// Callback function for CXXRecordDecl::forallBases that acknowledges
261/// that it saw a base class.
262static bool SawBase(const CXXRecordDecl *, void *) {
263 return true;
264}
265
266bool CXXRecordDecl::hasAnyDependentBases() const {
267 if (!isDependentContext())
268 return false;
269
270 return !forallBases(SawBase, 0);
271}
272
Sean Huntffe37fd2011-05-25 20:50:04 +0000273bool CXXRecordDecl::hasConstCopyConstructor() const {
274 return getCopyConstructor(Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000275}
276
Chandler Carruthb7e95892011-04-23 10:47:28 +0000277bool CXXRecordDecl::isTriviallyCopyable() const {
278 // C++0x [class]p5:
279 // A trivially copyable class is a class that:
280 // -- has no non-trivial copy constructors,
281 if (!hasTrivialCopyConstructor()) return false;
282 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000283 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000284 // -- has no non-trivial copy assignment operators,
285 if (!hasTrivialCopyAssignment()) return false;
286 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000287 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000288 // -- has a trivial destructor.
289 if (!hasTrivialDestructor()) return false;
290
291 return true;
292}
293
Douglas Gregor0d405db2010-07-01 20:59:04 +0000294/// \brief Perform a simplistic form of overload resolution that only considers
295/// cv-qualifiers on a single parameter, and return the best overload candidate
296/// (if there is one).
297static CXXMethodDecl *
298GetBestOverloadCandidateSimple(
299 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
300 if (Cands.empty())
301 return 0;
302 if (Cands.size() == 1)
303 return Cands[0].first;
304
305 unsigned Best = 0, N = Cands.size();
306 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000307 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000308 Best = I;
309
310 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000311 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000312 return 0;
313
314 return Cands[Best].first;
315}
316
Sean Huntffe37fd2011-05-25 20:50:04 +0000317CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{
318 ASTContext &Context = getASTContext();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000319 QualType ClassType
320 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000321 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000322 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000323 Context.getCanonicalType(ClassType));
324 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000325 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000326 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000327 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000328 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000329 // C++ [class.copy]p2:
330 // A non-template constructor for class X is a copy constructor if [...]
331 if (isa<FunctionTemplateDecl>(*Con))
332 continue;
333
Douglas Gregor0d405db2010-07-01 20:59:04 +0000334 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
335 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000336 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
337 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000338 Found.push_back(std::make_pair(
339 const_cast<CXXConstructorDecl *>(Constructor),
340 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000341 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000342 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000343
344 return cast_or_null<CXXConstructorDecl>(
345 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000346}
347
Sean Huntffe37fd2011-05-25 20:50:04 +0000348CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const {
349 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I)
350 if (I->isMoveConstructor())
351 return *I;
352
353 return 0;
354}
355
Douglas Gregorb87786f2010-07-01 17:48:08 +0000356CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
357 ASTContext &Context = getASTContext();
358 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
359 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
360
361 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
362 DeclContext::lookup_const_iterator Op, OpEnd;
363 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
364 // C++ [class.copy]p9:
365 // A user-declared copy assignment operator is a non-static non-template
366 // member function of class X with exactly one parameter of type X, X&,
367 // const X&, volatile X& or const volatile X&.
368 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
369 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
370 continue;
371
372 const FunctionProtoType *FnType
373 = Method->getType()->getAs<FunctionProtoType>();
374 assert(FnType && "Overloaded operator has no prototype.");
375 // Don't assert on this; an invalid decl might have been left in the AST.
376 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
377 continue;
378
379 QualType ArgType = FnType->getArgType(0);
380 Qualifiers Quals;
381 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
382 ArgType = Ref->getPointeeType();
383 // If we have a const argument and we have a reference to a non-const,
384 // this function does not match.
385 if (ArgIsConst && !ArgType.isConstQualified())
386 continue;
387
388 Quals = ArgType.getQualifiers();
389 } else {
390 // By-value copy-assignment operators are treated like const X&
391 // copy-assignment operators.
392 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
393 }
394
395 if (!Context.hasSameUnqualifiedType(ArgType, Class))
396 continue;
397
398 // Save this copy-assignment operator. It might be "the one".
399 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
400 }
401
402 // Use a simplistic form of overload resolution to find the candidate.
403 return GetBestOverloadCandidateSimple(Found);
404}
405
Sean Huntffe37fd2011-05-25 20:50:04 +0000406CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const {
407 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I)
408 if (I->isMoveAssignmentOperator())
409 return *I;
410
411 return 0;
412}
413
Douglas Gregor21386642010-09-28 21:55:22 +0000414void CXXRecordDecl::markedVirtualFunctionPure() {
415 // C++ [class.abstract]p2:
416 // A class is abstract if it has at least one pure virtual function.
417 data().Abstract = true;
418}
419
420void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000421 // Ignore friends and invalid declarations.
422 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000423 return;
424
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000425 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
426 if (FunTmpl)
427 D = FunTmpl->getTemplatedDecl();
428
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000429 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
430 if (Method->isVirtual()) {
431 // C++ [dcl.init.aggr]p1:
432 // An aggregate is an array or a class with [...] no virtual functions.
433 data().Aggregate = false;
434
435 // C++ [class]p4:
436 // A POD-struct is an aggregate class...
437 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000438
439 // Virtual functions make the class non-empty.
440 // FIXME: Standard ref?
441 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000442
443 // C++ [class.virtual]p1:
444 // A class that declares or inherits a virtual function is called a
445 // polymorphic class.
446 data().Polymorphic = true;
447
Sean Hunt023df372011-05-09 18:22:59 +0000448 // C++0x [class.ctor]p5
449 // A default constructor is trivial [...] if:
450 // -- its class has no virtual functions [...]
451 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000452
453 // C++0x [class.copy]p13:
454 // A copy/move constructor for class X is trivial if [...]
455 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000456 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000457 data().HasTrivialMoveConstructor = false;
458
459 // C++0x [class.copy]p27:
460 // A copy/move assignment operator for class X is trivial if [...]
461 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000462 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000463 data().HasTrivialMoveAssignment = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000464 // FIXME: Destructor?
Chandler Carrutha8225442011-04-30 09:17:45 +0000465
466 // C++0x [class]p7:
467 // A standard-layout class is a class that: [...]
468 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000469 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000470 }
471 }
472
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000473 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000474 // Notify that an implicit member was added after the definition
475 // was completed.
476 if (!isBeingDefined())
477 if (ASTMutationListener *L = getASTMutationListener())
478 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000479
Sean Huntffe37fd2011-05-25 20:50:04 +0000480 // If this is a special member function, note that it was added and then
481 // return early.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000482 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000483 if (Constructor->isDefaultConstructor())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000484 data().DeclaredDefaultConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000485 else if (Constructor->isCopyConstructor())
486 data().DeclaredCopyConstructor = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000487 else if (Constructor->isMoveConstructor())
488 data().DeclaredMoveConstructor = true;
489 else
490 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000491 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000492 } else if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000493 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000494 return;
Sean Huntffe37fd2011-05-25 20:50:04 +0000495 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
496 if (Method->isCopyAssignmentOperator())
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000497 data().DeclaredCopyAssignment = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000498 else if (Method->isMoveAssignmentOperator())
499 data().DeclaredMoveAssignment = true;
500 else
501 goto NotASpecialMember;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000502 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000503 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000504
Sean Huntffe37fd2011-05-25 20:50:04 +0000505NotASpecialMember:;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000506 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000507 }
508
509 // Handle (user-declared) constructors.
510 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
511 // Note that we have a user-declared constructor.
512 data().UserDeclaredConstructor = true;
513
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000514 // FIXME: Under C++0x, /only/ special member functions may be user-provided.
515 // This is probably a defect.
516 bool UserProvided = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000517
Sean Hunt023df372011-05-09 18:22:59 +0000518 // C++0x [class.ctor]p5:
519 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000520 if (Constructor->isDefaultConstructor()) {
521 data().DeclaredDefaultConstructor = true;
522 if (Constructor->isUserProvided()) {
523 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000524 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000525 UserProvided = true;
526 }
527 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000528
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000529 // Note when we have a user-declared copy or move constructor, which will
530 // suppress the implicit declaration of those constructors.
531 if (!FunTmpl) {
532 if (Constructor->isCopyConstructor()) {
533 data().UserDeclaredCopyConstructor = true;
534 data().DeclaredCopyConstructor = true;
535
536 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000537 // A copy/move constructor for class X is trivial if it is not
538 // user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000539 if (Constructor->isUserProvided()) {
Sean Hunt023df372011-05-09 18:22:59 +0000540 data().HasTrivialCopyConstructor = false;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000541 UserProvided = true;
542 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000543 } else if (Constructor->isMoveConstructor()) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000544 data().UserDeclaredMoveConstructor = true;
545 data().DeclaredMoveConstructor = true;
546
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000547 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000548 // A copy/move constructor for class X is trivial if it is not
549 // user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000550 if (Constructor->isUserProvided()) {
Sean Hunt023df372011-05-09 18:22:59 +0000551 data().HasTrivialMoveConstructor = false;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000552 UserProvided = true;
553 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000554 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000555 }
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000556 if (Constructor->isConstExpr() &&
557 !Constructor->isCopyOrMoveConstructor()) {
558 // Record if we see any constexpr constructors which are niether copy
559 // nor move constructors.
560 data().HasConstExprNonCopyMoveConstructor = true;
561 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000562
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000563 // C++ [dcl.init.aggr]p1:
564 // An aggregate is an array or a class with no user-declared
565 // constructors [...].
566 // C++0x [dcl.init.aggr]p1:
567 // An aggregate is an array or a class with no user-provided
568 // constructors [...].
569 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
570 data().Aggregate = false;
571
572 // C++ [class]p4:
573 // A POD-struct is an aggregate class [...]
574 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
575 // type is technically an aggregate in C++0x since it wouldn't be in 03.
576 data().PlainOldData = false;
577
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000578 return;
579 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000580
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000581 // Handle (user-declared) destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000582 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000583 data().DeclaredDestructor = true;
584 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000585
586 // C++ [class]p4:
587 // A POD-struct is an aggregate class that has [...] no user-defined
588 // destructor.
Sean Huntcf34e752011-05-16 22:41:40 +0000589 // This bit is the C++03 POD bit, not the 0x one.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000590 data().PlainOldData = false;
591
Sean Huntcf34e752011-05-16 22:41:40 +0000592 // C++0x [class.dtor]p5:
593 // A destructor is trivial if it is not user-provided and [...]
594 if (DD->isUserProvided())
595 data().HasTrivialDestructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000596
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000597 return;
598 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000599
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000600 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000601 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000602 if (Method->isCopyAssignmentOperator()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000603 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000604 // A POD-struct is an aggregate class that [...] has no user-defined
605 // copy assignment operator [...].
Sean Huntcf34e752011-05-16 22:41:40 +0000606 // This is the C++03 bit only.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000607 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000608
Sean Huntffe37fd2011-05-25 20:50:04 +0000609 // This is a copy assignment operator.
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000610
Sean Huntffe37fd2011-05-25 20:50:04 +0000611 // Suppress the implicit declaration of a copy constructor.
612 data().UserDeclaredCopyAssignment = true;
613 data().DeclaredCopyAssignment = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000614
Sean Huntffe37fd2011-05-25 20:50:04 +0000615 // C++0x [class.copy]p27:
616 // A copy/move assignment operator for class X is trivial if it is
617 // neither user-provided nor deleted [...]
618 if (Method->isUserProvided())
619 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000620
Sean Huntffe37fd2011-05-25 20:50:04 +0000621 return;
622 }
623
624 if (Method->isMoveAssignmentOperator()) {
625 // This is an extension in C++03 mode, but we'll keep consistency by
626 // taking a move assignment operator to induce non-POD-ness
627 data().PlainOldData = false;
628
629 // This is a move assignment operator.
630 data().UserDeclaredMoveAssignment = true;
631 data().DeclaredMoveAssignment = true;
632
633 // C++0x [class.copy]p27:
634 // A copy/move assignment operator for class X is trivial if it is
635 // neither user-provided nor deleted [...]
636 if (Method->isUserProvided())
637 data().HasTrivialMoveAssignment = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000638 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000639
Douglas Gregore80622f2010-09-29 04:25:11 +0000640 // Keep the list of conversion functions up-to-date.
641 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
642 // We don't record specializations.
643 if (Conversion->getPrimaryTemplate())
644 return;
645
646 // FIXME: We intentionally don't use the decl's access here because it
647 // hasn't been set yet. That's really just a misdesign in Sema.
648
649 if (FunTmpl) {
650 if (FunTmpl->getPreviousDeclaration())
651 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
652 FunTmpl);
653 else
654 data().Conversions.addDecl(FunTmpl);
655 } else {
656 if (Conversion->getPreviousDeclaration())
657 data().Conversions.replace(Conversion->getPreviousDeclaration(),
658 Conversion);
659 else
660 data().Conversions.addDecl(Conversion);
661 }
662 }
663
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000664 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000665 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000666
667 // Handle non-static data members.
668 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
669 // C++ [dcl.init.aggr]p1:
670 // An aggregate is an array or a class (clause 9) with [...] no
671 // private or protected non-static data members (clause 11).
672 //
673 // A POD must be an aggregate.
674 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
675 data().Aggregate = false;
676 data().PlainOldData = false;
677 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000678
679 // C++0x [class]p7:
680 // A standard-layout class is a class that:
681 // [...]
682 // -- has the same access control for all non-static data members,
683 switch (D->getAccess()) {
684 case AS_private: data().HasPrivateFields = true; break;
685 case AS_protected: data().HasProtectedFields = true; break;
686 case AS_public: data().HasPublicFields = true; break;
687 case AS_none: assert(0 && "Invalid access specifier");
688 };
689 if ((data().HasPrivateFields + data().HasProtectedFields +
690 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000691 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000692
Douglas Gregor2bb11012011-05-13 01:05:07 +0000693 // Keep track of the presence of mutable fields.
694 if (Field->isMutable())
695 data().HasMutableFields = true;
696
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000697 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000698 // A POD struct is a class that is both a trivial class and a
699 // standard-layout class, and has no non-static data members of type
700 // non-POD struct, non-POD union (or array of such types).
701 ASTContext &Context = getASTContext();
702 QualType T = Context.getBaseElementType(Field->getType());
703 if (!T->isPODType())
704 data().PlainOldData = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000705 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000706 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000707
Chandler Carrutha8225442011-04-30 09:17:45 +0000708 // C++0x [class]p7:
709 // A standard-layout class is a class that:
710 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000711 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000712 }
713
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000714 // Record if this field is the first non-literal field or base.
715 if (!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType())
716 data().HasNonLiteralTypeFieldsOrBases = true;
717
Richard Smith7a614d82011-06-11 17:19:42 +0000718 if (Field->hasInClassInitializer()) {
719 // C++0x [class]p5:
720 // A default constructor is trivial if [...] no non-static data member
721 // of its class has a brace-or-equal-initializer.
722 data().HasTrivialDefaultConstructor = false;
723
724 // C++0x [dcl.init.aggr]p1:
725 // An aggregate is a [...] class with [...] no
726 // brace-or-equal-initializers for non-static data members.
727 data().Aggregate = false;
728
729 // C++0x [class]p10:
730 // A POD struct is [...] a trivial class.
731 data().PlainOldData = false;
732 }
733
Douglas Gregor85606eb2010-09-28 20:50:54 +0000734 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
735 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
736 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000737 // C++0x [class.ctor]p5:
738 // A defulat constructor is trivial [...] if:
739 // -- for all the non-static data members of its class that are of
740 // class type (or array thereof), each such class has a trivial
741 // default constructor.
742 if (!FieldRec->hasTrivialDefaultConstructor())
743 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000744
745 // C++0x [class.copy]p13:
746 // A copy/move constructor for class X is trivial if [...]
747 // [...]
748 // -- for each non-static data member of X that is of class type (or
749 // an array thereof), the constructor selected to copy/move that
750 // member is trivial;
751 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000752 if (!FieldRec->hasTrivialCopyConstructor())
753 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000754 if (!FieldRec->hasTrivialMoveConstructor())
755 data().HasTrivialMoveConstructor = false;
756
757 // C++0x [class.copy]p27:
758 // A copy/move assignment operator for class X is trivial if [...]
759 // [...]
760 // -- for each non-static data member of X that is of class type (or
761 // an array thereof), the assignment operator selected to
762 // copy/move that member is trivial;
763 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000764 if (!FieldRec->hasTrivialCopyAssignment())
765 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000766 if (!FieldRec->hasTrivialMoveAssignment())
767 data().HasTrivialMoveAssignment = false;
768
Douglas Gregor85606eb2010-09-28 20:50:54 +0000769 if (!FieldRec->hasTrivialDestructor())
770 data().HasTrivialDestructor = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000771
772 // C++0x [class]p7:
773 // A standard-layout class is a class that:
774 // -- has no non-static data members of type non-standard-layout
775 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000776 if (!FieldRec->isStandardLayout())
777 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000778
779 // C++0x [class]p7:
780 // A standard-layout class is a class that:
781 // [...]
782 // -- has no base classes of the same type as the first non-static
783 // data member.
784 // We don't want to expend bits in the state of the record decl
785 // tracking whether this is the first non-static data member so we
786 // cheat a bit and use some of the existing state: the empty bit.
787 // Virtual bases and virtual methods make a class non-empty, but they
788 // also make it non-standard-layout so we needn't check here.
789 // A non-empty base class may leave the class standard-layout, but not
790 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000791 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000792 // data member must come through here with Empty still true, and Empty
793 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000794 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000795 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
796 BE = bases_end();
797 BI != BE; ++BI) {
798 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000799 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000800 break;
801 }
802 }
803 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000804
805 // Keep track of the presence of mutable fields.
806 if (FieldRec->hasMutableFields())
807 data().HasMutableFields = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000808 }
809 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000810
811 // C++0x [class]p7:
812 // A standard-layout class is a class that:
813 // [...]
814 // -- either has no non-static data members in the most derived
815 // class and at most one base class with non-static data members,
816 // or has no base classes with non-static data members, and
817 // At this point we know that we have a non-static data member, so the last
818 // clause holds.
819 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000820 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000821
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000822 // If this is not a zero-length bit-field, then the class is not empty.
823 if (data().Empty) {
824 if (!Field->getBitWidth())
825 data().Empty = false;
826 else if (!Field->getBitWidth()->isTypeDependent() &&
827 !Field->getBitWidth()->isValueDependent()) {
828 llvm::APSInt Bits;
829 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
830 if (!!Bits)
831 data().Empty = false;
832 }
833 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000834 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000835
836 // Handle using declarations of conversion functions.
837 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
838 if (Shadow->getDeclName().getNameKind()
839 == DeclarationName::CXXConversionFunctionName)
840 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000841}
842
John McCallb05b5f32010-03-15 09:07:48 +0000843static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
844 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000845 if (isa<UsingShadowDecl>(Conv))
846 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000847 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
848 T = ConvTemp->getTemplatedDecl()->getResultType();
849 else
850 T = cast<CXXConversionDecl>(Conv)->getConversionType();
851 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000852}
853
John McCallb05b5f32010-03-15 09:07:48 +0000854/// Collect the visible conversions of a base class.
855///
856/// \param Base a base class of the class we're considering
857/// \param InVirtual whether this base class is a virtual base (or a base
858/// of a virtual base)
859/// \param Access the access along the inheritance path to this base
860/// \param ParentHiddenTypes the conversions provided by the inheritors
861/// of this base
862/// \param Output the set to which to add conversions from non-virtual bases
863/// \param VOutput the set to which to add conversions from virtual bases
864/// \param HiddenVBaseCs the set of conversions which were hidden in a
865/// virtual base along some inheritance path
866static void CollectVisibleConversions(ASTContext &Context,
867 CXXRecordDecl *Record,
868 bool InVirtual,
869 AccessSpecifier Access,
870 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
871 UnresolvedSetImpl &Output,
872 UnresolvedSetImpl &VOutput,
873 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
874 // The set of types which have conversions in this class or its
875 // subclasses. As an optimization, we don't copy the derived set
876 // unless it might change.
877 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
878 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
879
880 // Collect the direct conversions and figure out which conversions
881 // will be hidden in the subclasses.
882 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
883 if (!Cs.empty()) {
884 HiddenTypesBuffer = ParentHiddenTypes;
885 HiddenTypes = &HiddenTypesBuffer;
886
887 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
888 bool Hidden =
889 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
890
891 // If this conversion is hidden and we're in a virtual base,
892 // remember that it's hidden along some inheritance path.
893 if (Hidden && InVirtual)
894 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
895
896 // If this conversion isn't hidden, add it to the appropriate output.
897 else if (!Hidden) {
898 AccessSpecifier IAccess
899 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
900
901 if (InVirtual)
902 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000903 else
John McCallb05b5f32010-03-15 09:07:48 +0000904 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000905 }
906 }
907 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000908
John McCallb05b5f32010-03-15 09:07:48 +0000909 // Collect information recursively from any base classes.
910 for (CXXRecordDecl::base_class_iterator
911 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
912 const RecordType *RT = I->getType()->getAs<RecordType>();
913 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000914
John McCallb05b5f32010-03-15 09:07:48 +0000915 AccessSpecifier BaseAccess
916 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
917 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000918
John McCallb05b5f32010-03-15 09:07:48 +0000919 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
920 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
921 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000922 }
John McCallb05b5f32010-03-15 09:07:48 +0000923}
Sebastian Redl9994a342009-10-25 17:03:50 +0000924
John McCallb05b5f32010-03-15 09:07:48 +0000925/// Collect the visible conversions of a class.
926///
927/// This would be extremely straightforward if it weren't for virtual
928/// bases. It might be worth special-casing that, really.
929static void CollectVisibleConversions(ASTContext &Context,
930 CXXRecordDecl *Record,
931 UnresolvedSetImpl &Output) {
932 // The collection of all conversions in virtual bases that we've
933 // found. These will be added to the output as long as they don't
934 // appear in the hidden-conversions set.
935 UnresolvedSet<8> VBaseCs;
936
937 // The set of conversions in virtual bases that we've determined to
938 // be hidden.
939 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
940
941 // The set of types hidden by classes derived from this one.
942 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
943
944 // Go ahead and collect the direct conversions and add them to the
945 // hidden-types set.
946 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
947 Output.append(Cs.begin(), Cs.end());
948 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
949 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
950
951 // Recursively collect conversions from base classes.
952 for (CXXRecordDecl::base_class_iterator
953 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
954 const RecordType *RT = I->getType()->getAs<RecordType>();
955 if (!RT) continue;
956
957 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
958 I->isVirtual(), I->getAccessSpecifier(),
959 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
960 }
961
962 // Add any unhidden conversions provided by virtual bases.
963 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
964 I != E; ++I) {
965 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
966 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000967 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000968}
969
970/// getVisibleConversionFunctions - get all conversion functions visible
971/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000972const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000973 // If root class, all conversions are visible.
974 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000975 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000976 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000977 if (data().ComputedVisibleConversions)
978 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000979 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000980 data().ComputedVisibleConversions = true;
981 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000982}
983
John McCall32daa422010-03-31 01:36:47 +0000984void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
985 // This operation is O(N) but extremely rare. Sema only uses it to
986 // remove UsingShadowDecls in a class that were followed by a direct
987 // declaration, e.g.:
988 // class A : B {
989 // using B::operator int;
990 // operator int();
991 // };
992 // This is uncommon by itself and even more uncommon in conjunction
993 // with sufficiently large numbers of directly-declared conversions
994 // that asymptotic behavior matters.
995
996 UnresolvedSetImpl &Convs = *getConversionFunctions();
997 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
998 if (Convs[I].getDecl() == ConvDecl) {
999 Convs.erase(I);
1000 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1001 && "conversion was found multiple times in unresolved set");
1002 return;
1003 }
1004 }
1005
1006 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001007}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001008
Douglas Gregorf6b11852009-10-08 15:14:33 +00001009CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001010 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001011 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1012
1013 return 0;
1014}
1015
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001016MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1017 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1018}
1019
Douglas Gregorf6b11852009-10-08 15:14:33 +00001020void
1021CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1022 TemplateSpecializationKind TSK) {
1023 assert(TemplateOrInstantiation.isNull() &&
1024 "Previous template or instantiation?");
1025 assert(!isa<ClassTemplateSpecializationDecl>(this));
1026 TemplateOrInstantiation
1027 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1028}
1029
Anders Carlssonb13e3572009-12-07 06:33:48 +00001030TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1031 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001032 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1033 return Spec->getSpecializationKind();
1034
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001035 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001036 return MSInfo->getTemplateSpecializationKind();
1037
1038 return TSK_Undeclared;
1039}
1040
1041void
1042CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1043 if (ClassTemplateSpecializationDecl *Spec
1044 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1045 Spec->setSpecializationKind(TSK);
1046 return;
1047 }
1048
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001049 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001050 MSInfo->setTemplateSpecializationKind(TSK);
1051 return;
1052 }
1053
1054 assert(false && "Not a class template or member class specialization");
1055}
1056
Douglas Gregor1d110e02010-07-01 14:13:13 +00001057CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1058 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001059 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001060
1061 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001062 = Context.DeclarationNames.getCXXDestructorName(
1063 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001064
John McCallc0bf4622010-02-23 00:48:20 +00001065 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001066 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001067 if (I == E)
1068 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001070 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001071 return Dtor;
1072}
1073
Douglas Gregorda2142f2011-02-19 18:51:44 +00001074void CXXRecordDecl::completeDefinition() {
1075 completeDefinition(0);
1076}
1077
1078void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1079 RecordDecl::completeDefinition();
1080
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001081 // If the class may be abstract (but hasn't been marked as such), check for
1082 // any pure final overriders.
1083 if (mayBeAbstract()) {
1084 CXXFinalOverriderMap MyFinalOverriders;
1085 if (!FinalOverriders) {
1086 getFinalOverriders(MyFinalOverriders);
1087 FinalOverriders = &MyFinalOverriders;
1088 }
1089
1090 bool Done = false;
1091 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1092 MEnd = FinalOverriders->end();
1093 M != MEnd && !Done; ++M) {
1094 for (OverridingMethods::iterator SO = M->second.begin(),
1095 SOEnd = M->second.end();
1096 SO != SOEnd && !Done; ++SO) {
1097 assert(SO->second.size() > 0 &&
1098 "All virtual functions have overridding virtual functions");
1099
1100 // C++ [class.abstract]p4:
1101 // A class is abstract if it contains or inherits at least one
1102 // pure virtual function for which the final overrider is pure
1103 // virtual.
1104 if (SO->second.front().Method->isPure()) {
1105 data().Abstract = true;
1106 Done = true;
1107 break;
1108 }
1109 }
1110 }
1111 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001112
1113 // Set access bits correctly on the directly-declared conversions.
1114 for (UnresolvedSetIterator I = data().Conversions.begin(),
1115 E = data().Conversions.end();
1116 I != E; ++I)
1117 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001118}
1119
1120bool CXXRecordDecl::mayBeAbstract() const {
1121 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1122 isDependentContext())
1123 return false;
1124
1125 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1126 BEnd = bases_end();
1127 B != BEnd; ++B) {
1128 CXXRecordDecl *BaseDecl
1129 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1130 if (BaseDecl->isAbstract())
1131 return true;
1132 }
1133
1134 return false;
1135}
1136
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001137CXXMethodDecl *
1138CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001139 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001140 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001141 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001142 bool isStatic, StorageClass SCAsWritten, bool isInline,
1143 SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001144 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001145 isStatic, SCAsWritten, isInline, EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001146}
1147
Douglas Gregor90916562009-09-29 18:16:17 +00001148bool CXXMethodDecl::isUsualDeallocationFunction() const {
1149 if (getOverloadedOperator() != OO_Delete &&
1150 getOverloadedOperator() != OO_Array_Delete)
1151 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001152
1153 // C++ [basic.stc.dynamic.deallocation]p2:
1154 // A template instance is never a usual deallocation function,
1155 // regardless of its signature.
1156 if (getPrimaryTemplate())
1157 return false;
1158
Douglas Gregor90916562009-09-29 18:16:17 +00001159 // C++ [basic.stc.dynamic.deallocation]p2:
1160 // If a class T has a member deallocation function named operator delete
1161 // with exactly one parameter, then that function is a usual (non-placement)
1162 // deallocation function. [...]
1163 if (getNumParams() == 1)
1164 return true;
1165
1166 // C++ [basic.stc.dynamic.deallocation]p2:
1167 // [...] If class T does not declare such an operator delete but does
1168 // declare a member deallocation function named operator delete with
1169 // exactly two parameters, the second of which has type std::size_t (18.1),
1170 // then this function is a usual deallocation function.
1171 ASTContext &Context = getASTContext();
1172 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001173 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1174 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001175 return false;
1176
1177 // This function is a usual deallocation function if there are no
1178 // single-parameter deallocation functions of the same kind.
1179 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1180 R.first != R.second; ++R.first) {
1181 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1182 if (FD->getNumParams() == 1)
1183 return false;
1184 }
1185
1186 return true;
1187}
1188
Douglas Gregor06a9f362010-05-01 20:49:11 +00001189bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001190 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001191 // A user-declared copy assignment operator X::operator= is a non-static
1192 // non-template member function of class X with exactly one parameter of
1193 // type X, X&, const X&, volatile X& or const volatile X&.
1194 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1195 /*non-static*/ isStatic() ||
Sean Huntffe37fd2011-05-25 20:50:04 +00001196 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate())
Douglas Gregor06a9f362010-05-01 20:49:11 +00001197 return false;
1198
1199 QualType ParamType = getParamDecl(0)->getType();
1200 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1201 ParamType = Ref->getPointeeType();
1202
1203 ASTContext &Context = getASTContext();
1204 QualType ClassType
1205 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1206 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1207}
1208
Sean Huntffe37fd2011-05-25 20:50:04 +00001209bool CXXMethodDecl::isMoveAssignmentOperator() const {
1210 // C++0x [class.copy]p19:
1211 // A user-declared move assignment operator X::operator= is a non-static
1212 // non-template member function of class X with exactly one parameter of type
1213 // X&&, const X&&, volatile X&&, or const volatile X&&.
1214 if (getOverloadedOperator() != OO_Equal || isStatic() ||
1215 getPrimaryTemplate() || getDescribedFunctionTemplate())
1216 return false;
1217
1218 QualType ParamType = getParamDecl(0)->getType();
1219 if (!isa<RValueReferenceType>(ParamType))
1220 return false;
1221 ParamType = ParamType->getPointeeType();
1222
1223 ASTContext &Context = getASTContext();
1224 QualType ClassType
1225 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1226 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1227}
1228
Anders Carlsson05eb2442009-05-16 23:58:37 +00001229void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001230 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001231 assert(!MD->getParent()->isDependentContext() &&
1232 "Can't add an overridden method to a class template!");
1233
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001234 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001235}
1236
1237CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001238 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001239}
1240
1241CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001242 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001243}
1244
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001245unsigned CXXMethodDecl::size_overridden_methods() const {
1246 return getASTContext().overridden_methods_size(this);
1247}
1248
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001249QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001250 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1251 // If the member function is declared const, the type of this is const X*,
1252 // if the member function is declared volatile, the type of this is
1253 // volatile X*, and if the member function is declared const volatile,
1254 // the type of this is const volatile X*.
1255
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001256 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001257
John McCall3cb0ebd2010-03-10 03:28:59 +00001258 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001259 ClassTy = C.getQualifiedType(ClassTy,
1260 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001261 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001262}
1263
Eli Friedmand7d7f672009-12-06 20:50:05 +00001264bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001265 // If this function is a template instantiation, look at the template from
1266 // which it was instantiated.
1267 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1268 if (!CheckFn)
1269 CheckFn = this;
1270
Eli Friedmand7d7f672009-12-06 20:50:05 +00001271 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001272 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001273}
1274
Sean Huntcbb67482011-01-08 20:30:50 +00001275CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1276 TypeSourceInfo *TInfo, bool IsVirtual,
1277 SourceLocation L, Expr *Init,
1278 SourceLocation R,
1279 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001280 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001281 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1282 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001283{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001284}
1285
Sean Huntcbb67482011-01-08 20:30:50 +00001286CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1287 FieldDecl *Member,
1288 SourceLocation MemberLoc,
1289 SourceLocation L, Expr *Init,
1290 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001291 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001292 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1293 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1294{
1295}
1296
Sean Huntcbb67482011-01-08 20:30:50 +00001297CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1298 IndirectFieldDecl *Member,
1299 SourceLocation MemberLoc,
1300 SourceLocation L, Expr *Init,
1301 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001302 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001303 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001304 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001305{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001306}
1307
Sean Huntcbb67482011-01-08 20:30:50 +00001308CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Hunt41717662011-02-26 19:13:13 +00001309 SourceLocation D, SourceLocation L,
1310 CXXConstructorDecl *Target, Expr *Init,
1311 SourceLocation R)
1312 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1313 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1314 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1315{
1316}
1317
1318CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001319 FieldDecl *Member,
1320 SourceLocation MemberLoc,
1321 SourceLocation L, Expr *Init,
1322 SourceLocation R,
1323 VarDecl **Indices,
1324 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001325 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001326 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001327 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001328{
1329 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1330 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1331}
1332
Sean Huntcbb67482011-01-08 20:30:50 +00001333CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1334 FieldDecl *Member,
1335 SourceLocation MemberLoc,
1336 SourceLocation L, Expr *Init,
1337 SourceLocation R,
1338 VarDecl **Indices,
1339 unsigned NumIndices) {
1340 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001341 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001342 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001343 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1344 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001345}
1346
Sean Huntcbb67482011-01-08 20:30:50 +00001347TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001348 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001349 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001350 else
1351 return TypeLoc();
1352}
1353
Sean Huntcbb67482011-01-08 20:30:50 +00001354const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001355 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001356 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001357 else
1358 return 0;
1359}
1360
Sean Huntcbb67482011-01-08 20:30:50 +00001361SourceLocation CXXCtorInitializer::getSourceLocation() const {
Sean Hunt41717662011-02-26 19:13:13 +00001362 if (isAnyMemberInitializer() || isDelegatingInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001363 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001364
1365 if (isInClassMemberInitializer())
1366 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001367
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001368 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001369}
1370
Sean Huntcbb67482011-01-08 20:30:50 +00001371SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001372 if (isInClassMemberInitializer()) {
1373 FieldDecl *D = getAnyMember();
1374 if (Expr *I = D->getInClassInitializer())
1375 return I->getSourceRange();
1376 return SourceRange();
1377 }
1378
Douglas Gregor802ab452009-12-02 22:36:29 +00001379 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001380}
1381
Douglas Gregorb48fe382008-10-31 09:07:45 +00001382CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001383CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001384 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Sean Hunt5f802e52011-05-06 00:11:07 +00001385 QualType(), 0, false, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001386}
1387
1388CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001389CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001390 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001391 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001392 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001393 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001394 bool isInline,
Sean Hunt5f802e52011-05-06 00:11:07 +00001395 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001396 assert(NameInfo.getName().getNameKind()
1397 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001398 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001399 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Sean Hunt5f802e52011-05-06 00:11:07 +00001400 isExplicit, isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001401}
1402
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001403bool CXXConstructorDecl::isDefaultConstructor() const {
1404 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001405 // A default constructor for a class X is a constructor of class
1406 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001407 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001408 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001409}
1410
Mike Stump1eb44332009-09-09 15:08:12 +00001411bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001412CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001413 return isCopyOrMoveConstructor(TypeQuals) &&
1414 getParamDecl(0)->getType()->isLValueReferenceType();
1415}
1416
1417bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1418 return isCopyOrMoveConstructor(TypeQuals) &&
1419 getParamDecl(0)->getType()->isRValueReferenceType();
1420}
1421
1422/// \brief Determine whether this is a copy or move constructor.
1423bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001424 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001425 // A non-template constructor for class X is a copy constructor
1426 // if its first parameter is of type X&, const X&, volatile X& or
1427 // const volatile X&, and either there are no other parameters
1428 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001429 // C++0x [class.copy]p3:
1430 // A non-template constructor for class X is a move constructor if its
1431 // first parameter is of type X&&, const X&&, volatile X&&, or
1432 // const volatile X&&, and either there are no other parameters or else
1433 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001434 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001435 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001436 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001437 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001438 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001439
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001440 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001441
1442 // Do we have a reference type?
1443 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001444 if (!ParamRefType)
1445 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001446
Douglas Gregorfd476482009-11-13 23:59:09 +00001447 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001448 ASTContext &Context = getASTContext();
1449
Douglas Gregorfd476482009-11-13 23:59:09 +00001450 CanQualType PointeeType
1451 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001452 CanQualType ClassTy
1453 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001454 if (PointeeType.getUnqualifiedType() != ClassTy)
1455 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001456
John McCall0953e762009-09-24 19:53:00 +00001457 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001458
1459 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001460 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001461 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001462}
1463
Anders Carlssonfaccd722009-08-28 16:57:08 +00001464bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001465 // C++ [class.conv.ctor]p1:
1466 // A constructor declared without the function-specifier explicit
1467 // that can be called with a single parameter specifies a
1468 // conversion from the type of its first parameter to the type of
1469 // its class. Such a constructor is called a converting
1470 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001471 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001472 return false;
1473
Mike Stump1eb44332009-09-09 15:08:12 +00001474 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001475 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001476 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001477 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001478}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001479
Douglas Gregor6493cc52010-11-08 17:16:59 +00001480bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001481 if ((getNumParams() < 1) ||
1482 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1483 (getPrimaryTemplate() == 0) ||
1484 (getDescribedFunctionTemplate() != 0))
1485 return false;
1486
1487 const ParmVarDecl *Param = getParamDecl(0);
1488
1489 ASTContext &Context = getASTContext();
1490 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1491
Douglas Gregor66724ea2009-11-14 01:20:54 +00001492 // Is it the same as our our class type?
1493 CanQualType ClassTy
1494 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1495 if (ParamType.getUnqualifiedType() != ClassTy)
1496 return false;
1497
1498 return true;
1499}
1500
Sebastian Redlf677ea32011-02-05 19:23:19 +00001501const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1502 // Hack: we store the inherited constructor in the overridden method table
1503 method_iterator It = begin_overridden_methods();
1504 if (It == end_overridden_methods())
1505 return 0;
1506
1507 return cast<CXXConstructorDecl>(*It);
1508}
1509
1510void
1511CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1512 // Hack: we store the inherited constructor in the overridden method table
1513 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1514 addOverriddenMethod(BaseCtor);
1515}
1516
Douglas Gregor42a552f2008-11-05 20:51:48 +00001517CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001518CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001519 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001520 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001521}
1522
1523CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001524CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001525 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001526 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001527 QualType T, TypeSourceInfo *TInfo,
1528 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001529 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001530 assert(NameInfo.getName().getNameKind()
1531 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001532 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001533 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001534 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001535}
1536
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001537CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001538CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001539 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001540 QualType(), 0, false, false,
1541 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001542}
1543
1544CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001545CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001546 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001547 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001548 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001549 bool isInline, bool isExplicit,
1550 SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001551 assert(NameInfo.getName().getNameKind()
1552 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001553 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001554 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001555 isInline, isExplicit, EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001556}
1557
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001558LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001559 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001560 SourceLocation ExternLoc,
1561 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001562 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001563 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001564 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001565}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001566
1567UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1568 SourceLocation L,
1569 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001570 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001571 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001572 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001573 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001574 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1575 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001576 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1577 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001578}
1579
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001580NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1581 if (NamespaceAliasDecl *NA =
1582 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1583 return NA->getNamespace();
1584 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1585}
1586
Mike Stump1eb44332009-09-09 15:08:12 +00001587NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001588 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001589 SourceLocation AliasLoc,
1590 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001591 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001592 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001593 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001594 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1595 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001596 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1597 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001598}
1599
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001600UsingDecl *UsingShadowDecl::getUsingDecl() const {
1601 const UsingShadowDecl *Shadow = this;
1602 while (const UsingShadowDecl *NextShadow =
1603 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1604 Shadow = NextShadow;
1605 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1606}
1607
1608void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1609 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1610 "declaration already in set");
1611 assert(S->getUsingDecl() == this);
1612
1613 if (FirstUsingShadow)
1614 S->UsingOrNextShadow = FirstUsingShadow;
1615 FirstUsingShadow = S;
1616}
1617
1618void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1619 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1620 "declaration not in set");
1621 assert(S->getUsingDecl() == this);
1622
1623 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1624
1625 if (FirstUsingShadow == S) {
1626 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1627 S->UsingOrNextShadow = this;
1628 return;
1629 }
1630
1631 UsingShadowDecl *Prev = FirstUsingShadow;
1632 while (Prev->UsingOrNextShadow != S)
1633 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1634 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1635 S->UsingOrNextShadow = this;
1636}
1637
Douglas Gregordc355712011-02-25 00:36:19 +00001638UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1639 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001640 const DeclarationNameInfo &NameInfo,
1641 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001642 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001643}
1644
John McCall7ba107a2009-11-18 02:36:19 +00001645UnresolvedUsingValueDecl *
1646UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1647 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001648 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001649 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001650 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001651 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001652}
1653
1654UnresolvedUsingTypenameDecl *
1655UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1656 SourceLocation UsingLoc,
1657 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001658 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001659 SourceLocation TargetNameLoc,
1660 DeclarationName TargetName) {
1661 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001662 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001663 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001664}
1665
Anders Carlssonfb311762009-03-14 00:25:26 +00001666StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001667 SourceLocation StaticAssertLoc,
1668 Expr *AssertExpr,
1669 StringLiteral *Message,
1670 SourceLocation RParenLoc) {
1671 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1672 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001673}
1674
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001675static const char *getAccessName(AccessSpecifier AS) {
1676 switch (AS) {
1677 default:
1678 case AS_none:
1679 assert("Invalid access specifier!");
1680 return 0;
1681 case AS_public:
1682 return "public";
1683 case AS_private:
1684 return "private";
1685 case AS_protected:
1686 return "protected";
1687 }
1688}
1689
1690const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1691 AccessSpecifier AS) {
1692 return DB << getAccessName(AS);
1693}