blob: ab097e4b3006cfa157a3afca21beb008fa89f99b [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),
Sebastian Redl64b45f72009-01-05 20:52:13 +000032 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000033 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000034 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000035 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Sean Hunt023df372011-05-09 18:22:59 +000036 HasTrivialDefaultConstructor(true),
37 HasConstExprNonCopyMoveConstructor(false), HasTrivialCopyConstructor(true),
38 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true),
39 HasTrivialMoveAssignment(true), HasTrivialDestructor(true),
40 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Douglas Gregor18274032010-07-03 00:47:00 +000041 DeclaredDefaultConstructor(false), DeclaredCopyConstructor(false),
Douglas Gregora376d102010-07-02 21:50:04 +000042 DeclaredCopyAssignment(false), DeclaredDestructor(false),
Anders Carlssoncb88a1f2011-01-24 16:26:15 +000043 NumBases(0), NumVBases(0), Bases(), VBases(),
44 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000045}
46
47CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000048 SourceLocation StartLoc, SourceLocation IdLoc,
49 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
50 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000051 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000052 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000053
Jay Foad4ba2a172011-01-12 09:06:06 +000054CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000055 DeclContext *DC, SourceLocation StartLoc,
56 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000057 CXXRecordDecl* PrevDecl,
58 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000059 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
60 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000061
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000062 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000063 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000064 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000065 return R;
66}
67
Jay Foad4ba2a172011-01-12 09:06:06 +000068CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000069 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
70 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000071}
72
Mike Stump1eb44332009-09-09 15:08:12 +000073void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000074CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000075 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000076 ASTContext &C = getASTContext();
77
Mike Stump1eb44332009-09-09 15:08:12 +000078 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000079 // An aggregate is an array or a class (clause 9) with [...]
80 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000081 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000082
Douglas Gregor7c789c12010-10-29 22:39:52 +000083 if (!data().Bases.isOffset() && data().NumBases > 0)
84 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000085
Anders Carlsson6f6de732010-03-29 05:13:12 +000086 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000087 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000088
89 // The virtual bases of this class.
90 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000091
John McCall86ff3082010-02-04 22:26:26 +000092 data().Bases = new(C) CXXBaseSpecifier [NumBases];
93 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000094 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +000095 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000096 // Keep track of inherited vbases for this base class.
97 const CXXBaseSpecifier *Base = Bases[i];
98 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +000099 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000100 if (BaseType->isDependentType())
101 continue;
102 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000103 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000104
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000105 // C++ [dcl.init.aggr]p1:
106 // An aggregate is [...] a class with [...] no base classes [...].
107 data().Aggregate = false;
108
109 // C++ [class]p4:
110 // A POD-struct is an aggregate class...
111 data().PlainOldData = false;
112
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000113 // A class with a non-empty base class is not empty.
114 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000115 if (!BaseClassDecl->isEmpty()) {
116 if (!data().Empty) {
117 // C++0x [class]p7:
118 // A standard-layout class is a class that:
119 // [...]
120 // -- either has no non-static data members in the most derived
121 // class and at most one base class with non-static data members,
122 // or has no base classes with non-static data members, and
123 // If this is the second non-empty base, then neither of these two
124 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000125 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000126 }
127
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000128 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000129 data().HasNoNonEmptyBases = false;
130 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000131
Douglas Gregor85606eb2010-09-28 20:50:54 +0000132 // C++ [class.virtual]p1:
133 // A class that declares or inherits a virtual function is called a
134 // polymorphic class.
135 if (BaseClassDecl->isPolymorphic())
136 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000137
Chandler Carrutha8225442011-04-30 09:17:45 +0000138 // C++0x [class]p7:
139 // A standard-layout class is a class that: [...]
140 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000141 if (!BaseClassDecl->isStandardLayout())
142 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000143
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000144 // Record if this base is the first non-literal field or base.
145 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
146 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000147
Anders Carlsson6f6de732010-03-29 05:13:12 +0000148 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000149 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000150 BaseClassDecl->vbases_begin(),
151 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000152 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000153 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000154 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000155 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000156
157 if (Base->isVirtual()) {
158 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000159 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000160 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000161
162 // C++0x [meta.unary.prop] is_empty:
163 // T is a class type, but not a union type, with ... no virtual base
164 // classes
165 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000166
167 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000168 // A default constructor is trivial [...] if:
169 // -- its class has [...] no virtual bases
170 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000171
172 // C++0x [class.copy]p13:
173 // A copy/move constructor for class X is trivial if it is neither
174 // user-provided nor deleted and if
175 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000176 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000177 data().HasTrivialMoveConstructor = false;
178
179 // C++0x [class.copy]p27:
180 // A copy/move assignment operator for class X is trivial if it is
181 // neither user-provided nor deleted and if
182 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000183 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000184 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000185
186 // C++0x [class]p7:
187 // A standard-layout class is a class that: [...]
188 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000189 data().IsStandardLayout = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000190 } else {
191 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000192 // A default constructor is trivial [...] if:
193 // -- all the direct base classes of its class have trivial default
194 // constructors.
195 if (!BaseClassDecl->hasTrivialDefaultConstructor())
196 data().HasTrivialDefaultConstructor = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000197
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000198 // C++0x [class.copy]p13:
199 // A copy/move constructor for class X is trivial if [...]
200 // [...]
201 // -- the constructor selected to copy/move each direct base class
202 // subobject is trivial, and
203 // FIXME: C++0x: We need to only consider the selected constructor
204 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000205 if (!BaseClassDecl->hasTrivialCopyConstructor())
206 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000207 if (!BaseClassDecl->hasTrivialMoveConstructor())
208 data().HasTrivialMoveConstructor = false;
209
210 // C++0x [class.copy]p27:
211 // A copy/move assignment operator for class X is trivial if [...]
212 // [...]
213 // -- the assignment operator selected to copy/move each direct base
214 // class subobject is trivial, and
215 // FIXME: C++0x: We need to only consider the selected operator instead
216 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000217 if (!BaseClassDecl->hasTrivialCopyAssignment())
218 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000219 if (!BaseClassDecl->hasTrivialMoveAssignment())
220 data().HasTrivialMoveAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000221 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000222
223 // C++ [class.ctor]p3:
224 // A destructor is trivial if all the direct base classes of its class
225 // have trivial destructors.
226 if (!BaseClassDecl->hasTrivialDestructor())
227 data().HasTrivialDestructor = false;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000228 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000229
230 if (VBases.empty())
231 return;
232
233 // Create base specifier for any direct or indirect virtual bases.
234 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
235 data().NumVBases = VBases.size();
236 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000237 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
238
Anders Carlsson6f6de732010-03-29 05:13:12 +0000239 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000240 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000241 continue;
242
Nick Lewycky56062202010-07-26 16:56:01 +0000243 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
244 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000245
Douglas Gregor7c789c12010-10-29 22:39:52 +0000246 data().getVBases()[I] =
Anders Carlsson6f6de732010-03-29 05:13:12 +0000247 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000248 VBaseClassDecl->getTagKind() == TTK_Class,
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000249 VBases[I]->getAccessSpecifier(), VBaseTypeInfo,
250 SourceLocation());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000251 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000252}
253
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000254/// Callback function for CXXRecordDecl::forallBases that acknowledges
255/// that it saw a base class.
256static bool SawBase(const CXXRecordDecl *, void *) {
257 return true;
258}
259
260bool CXXRecordDecl::hasAnyDependentBases() const {
261 if (!isDependentContext())
262 return false;
263
264 return !forallBases(SawBase, 0);
265}
266
Jay Foad4ba2a172011-01-12 09:06:06 +0000267bool CXXRecordDecl::hasConstCopyConstructor(const ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000268 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000269}
270
Chandler Carruthb7e95892011-04-23 10:47:28 +0000271bool CXXRecordDecl::isTriviallyCopyable() const {
272 // C++0x [class]p5:
273 // A trivially copyable class is a class that:
274 // -- has no non-trivial copy constructors,
275 if (!hasTrivialCopyConstructor()) return false;
276 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000277 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000278 // -- has no non-trivial copy assignment operators,
279 if (!hasTrivialCopyAssignment()) return false;
280 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000281 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000282 // -- has a trivial destructor.
283 if (!hasTrivialDestructor()) return false;
284
285 return true;
286}
287
Douglas Gregor0d405db2010-07-01 20:59:04 +0000288/// \brief Perform a simplistic form of overload resolution that only considers
289/// cv-qualifiers on a single parameter, and return the best overload candidate
290/// (if there is one).
291static CXXMethodDecl *
292GetBestOverloadCandidateSimple(
293 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
294 if (Cands.empty())
295 return 0;
296 if (Cands.size() == 1)
297 return Cands[0].first;
298
299 unsigned Best = 0, N = Cands.size();
300 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000301 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000302 Best = I;
303
304 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000305 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000306 return 0;
307
308 return Cands[Best].first;
309}
310
Jay Foad4ba2a172011-01-12 09:06:06 +0000311CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(const ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000312 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000313 QualType ClassType
314 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000315 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000316 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000317 Context.getCanonicalType(ClassType));
318 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000319 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000320 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000321 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000322 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000323 // C++ [class.copy]p2:
324 // A non-template constructor for class X is a copy constructor if [...]
325 if (isa<FunctionTemplateDecl>(*Con))
326 continue;
327
Douglas Gregor0d405db2010-07-01 20:59:04 +0000328 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
329 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000330 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
331 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000332 Found.push_back(std::make_pair(
333 const_cast<CXXConstructorDecl *>(Constructor),
334 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000335 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000336 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000337
338 return cast_or_null<CXXConstructorDecl>(
339 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000340}
341
Douglas Gregorb87786f2010-07-01 17:48:08 +0000342CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
343 ASTContext &Context = getASTContext();
344 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
345 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
346
347 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
348 DeclContext::lookup_const_iterator Op, OpEnd;
349 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
350 // C++ [class.copy]p9:
351 // A user-declared copy assignment operator is a non-static non-template
352 // member function of class X with exactly one parameter of type X, X&,
353 // const X&, volatile X& or const volatile X&.
354 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
355 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
356 continue;
357
358 const FunctionProtoType *FnType
359 = Method->getType()->getAs<FunctionProtoType>();
360 assert(FnType && "Overloaded operator has no prototype.");
361 // Don't assert on this; an invalid decl might have been left in the AST.
362 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
363 continue;
364
365 QualType ArgType = FnType->getArgType(0);
366 Qualifiers Quals;
367 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
368 ArgType = Ref->getPointeeType();
369 // If we have a const argument and we have a reference to a non-const,
370 // this function does not match.
371 if (ArgIsConst && !ArgType.isConstQualified())
372 continue;
373
374 Quals = ArgType.getQualifiers();
375 } else {
376 // By-value copy-assignment operators are treated like const X&
377 // copy-assignment operators.
378 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
379 }
380
381 if (!Context.hasSameUnqualifiedType(ArgType, Class))
382 continue;
383
384 // Save this copy-assignment operator. It might be "the one".
385 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
386 }
387
388 // Use a simplistic form of overload resolution to find the candidate.
389 return GetBestOverloadCandidateSimple(Found);
390}
391
Douglas Gregor21386642010-09-28 21:55:22 +0000392void CXXRecordDecl::markedVirtualFunctionPure() {
393 // C++ [class.abstract]p2:
394 // A class is abstract if it has at least one pure virtual function.
395 data().Abstract = true;
396}
397
398void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000399 // Ignore friends and invalid declarations.
400 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000401 return;
402
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000403 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
404 if (FunTmpl)
405 D = FunTmpl->getTemplatedDecl();
406
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000407 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
408 if (Method->isVirtual()) {
409 // C++ [dcl.init.aggr]p1:
410 // An aggregate is an array or a class with [...] no virtual functions.
411 data().Aggregate = false;
412
413 // C++ [class]p4:
414 // A POD-struct is an aggregate class...
415 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000416
417 // Virtual functions make the class non-empty.
418 // FIXME: Standard ref?
419 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000420
421 // C++ [class.virtual]p1:
422 // A class that declares or inherits a virtual function is called a
423 // polymorphic class.
424 data().Polymorphic = true;
425
Sean Hunt023df372011-05-09 18:22:59 +0000426 // C++0x [class.ctor]p5
427 // A default constructor is trivial [...] if:
428 // -- its class has no virtual functions [...]
429 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000430
431 // C++0x [class.copy]p13:
432 // A copy/move constructor for class X is trivial if [...]
433 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000434 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000435 data().HasTrivialMoveConstructor = false;
436
437 // C++0x [class.copy]p27:
438 // A copy/move assignment operator for class X is trivial if [...]
439 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000440 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000441 data().HasTrivialMoveAssignment = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000442 // FIXME: Destructor?
Chandler Carrutha8225442011-04-30 09:17:45 +0000443
444 // C++0x [class]p7:
445 // A standard-layout class is a class that: [...]
446 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000447 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000448 }
449 }
450
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000451 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000452 // Notify that an implicit member was added after the definition
453 // was completed.
454 if (!isBeingDefined())
455 if (ASTMutationListener *L = getASTMutationListener())
456 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000457
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000458 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
459 // If this is the implicit default constructor, note that we have now
460 // declared it.
461 if (Constructor->isDefaultConstructor())
462 data().DeclaredDefaultConstructor = true;
463 // If this is the implicit copy constructor, note that we have now
464 // declared it.
465 else if (Constructor->isCopyConstructor())
466 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000467 return;
468 }
469
470 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000471 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000472 return;
473 }
474
475 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000476 // If this is the implicit copy constructor, note that we have now
477 // declared it.
478 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000479 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000480 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000481 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000482 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000483
484 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000485 }
486
487 // Handle (user-declared) constructors.
488 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
489 // Note that we have a user-declared constructor.
490 data().UserDeclaredConstructor = true;
491
492 // Note that we have no need of an implicitly-declared default constructor.
493 data().DeclaredDefaultConstructor = true;
494
495 // C++ [dcl.init.aggr]p1:
496 // An aggregate is an array or a class (clause 9) with no
497 // user-declared constructors (12.1) [...].
498 data().Aggregate = false;
499
500 // C++ [class]p4:
501 // A POD-struct is an aggregate class [...]
502 data().PlainOldData = false;
503
Sean Hunt023df372011-05-09 18:22:59 +0000504 // C++0x [class.ctor]p5:
505 // A default constructor is trivial if it is not user-provided [...]
506 if (Constructor->isUserProvided())
507 data().HasTrivialDefaultConstructor = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000508
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000509 // Note when we have a user-declared copy or move constructor, which will
510 // suppress the implicit declaration of those constructors.
511 if (!FunTmpl) {
512 if (Constructor->isCopyConstructor()) {
513 data().UserDeclaredCopyConstructor = true;
514 data().DeclaredCopyConstructor = true;
515
516 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000517 // A copy/move constructor for class X is trivial if it is not
518 // user-provided [...]
519 if (Constructor->isUserProvided())
520 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000521 } else if (Constructor->isMoveConstructor()) {
522 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000523 // A copy/move constructor for class X is trivial if it is not
524 // user-provided [...]
525 if (Constructor->isUserProvided())
526 data().HasTrivialMoveConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000527 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000528 }
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000529 if (Constructor->isConstExpr() &&
530 !Constructor->isCopyOrMoveConstructor()) {
531 // Record if we see any constexpr constructors which are niether copy
532 // nor move constructors.
533 data().HasConstExprNonCopyMoveConstructor = true;
534 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000535
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000536 return;
537 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000538
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000539 // Handle (user-declared) destructors.
540 if (isa<CXXDestructorDecl>(D)) {
541 data().DeclaredDestructor = true;
542 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000543
544 // C++ [class]p4:
545 // A POD-struct is an aggregate class that has [...] no user-defined
546 // destructor.
547 data().PlainOldData = false;
548
Douglas Gregor85606eb2010-09-28 20:50:54 +0000549 // C++ [class.dtor]p3:
550 // A destructor is trivial if it is an implicitly-declared destructor and
551 // [...].
552 //
553 // FIXME: C++0x: don't do this for "= default" destructors
554 data().HasTrivialDestructor = false;
555
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000556 return;
557 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000558
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000559 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000560 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
561 if (Method->getOverloadedOperator() == OO_Equal) {
562 // We're interested specifically in copy assignment operators.
563 const FunctionProtoType *FnType
564 = Method->getType()->getAs<FunctionProtoType>();
565 assert(FnType && "Overloaded operator has no proto function type.");
566 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
567
568 // Copy assignment operators must be non-templates.
569 if (Method->getPrimaryTemplate() || FunTmpl)
570 return;
571
572 ASTContext &Context = getASTContext();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000573 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
574 const_cast<CXXRecordDecl*>(this)));
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000575
576 bool isRValueRefArg = false;
577 QualType ArgType = FnType->getArgType(0);
578 if (const LValueReferenceType *Ref =
579 ArgType->getAs<LValueReferenceType>()) {
580 ArgType = Ref->getPointeeType();
581 } else if (const RValueReferenceType *Ref =
582 ArgType->getAs<RValueReferenceType>()) {
583 ArgType = Ref->getPointeeType();
584 isRValueRefArg = true;
585 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000586 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
587 return;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000588
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000589 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000590 // A POD-struct is an aggregate class that [...] has no user-defined
591 // copy assignment operator [...].
592 // FIXME: This should be probably determined dynamically in terms of
593 // other more precise attributes to correctly model how it is specified
594 // in C++0x. Setting it here happens to do the right thing.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000595 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000596
597 if (!isRValueRefArg) {
598 // This is a copy assignment operator.
599
600 // Suppress the implicit declaration of a copy constructor.
601 data().UserDeclaredCopyAssignment = true;
602 data().DeclaredCopyAssignment = true;
603
604 // C++0x [class.copy]p27:
605 // A copy/move assignment operator for class X is trivial if it is
606 // neither user-provided nor deleted [...]
607 // FIXME: C++0x: don't do this for "= default" copy operators.
608 data().HasTrivialCopyAssignment = false;
609 } else {
610 // This is a move assignment operator.
611
612 // C++0x [class.copy]p27:
613 // A copy/move assignment operator for class X is trivial if it is
614 // neither user-provided nor deleted [...]
615 // FIXME: C++0x: don't do this for "= default" copy operators.
616 data().HasTrivialMoveAssignment = false;
617 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000618 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000619
Douglas Gregore80622f2010-09-29 04:25:11 +0000620 // Keep the list of conversion functions up-to-date.
621 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
622 // We don't record specializations.
623 if (Conversion->getPrimaryTemplate())
624 return;
625
626 // FIXME: We intentionally don't use the decl's access here because it
627 // hasn't been set yet. That's really just a misdesign in Sema.
628
629 if (FunTmpl) {
630 if (FunTmpl->getPreviousDeclaration())
631 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
632 FunTmpl);
633 else
634 data().Conversions.addDecl(FunTmpl);
635 } else {
636 if (Conversion->getPreviousDeclaration())
637 data().Conversions.replace(Conversion->getPreviousDeclaration(),
638 Conversion);
639 else
640 data().Conversions.addDecl(Conversion);
641 }
642 }
643
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000644 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000645 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000646
647 // Handle non-static data members.
648 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
649 // C++ [dcl.init.aggr]p1:
650 // An aggregate is an array or a class (clause 9) with [...] no
651 // private or protected non-static data members (clause 11).
652 //
653 // A POD must be an aggregate.
654 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
655 data().Aggregate = false;
656 data().PlainOldData = false;
657 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000658
659 // C++0x [class]p7:
660 // A standard-layout class is a class that:
661 // [...]
662 // -- has the same access control for all non-static data members,
663 switch (D->getAccess()) {
664 case AS_private: data().HasPrivateFields = true; break;
665 case AS_protected: data().HasProtectedFields = true; break;
666 case AS_public: data().HasPublicFields = true; break;
667 case AS_none: assert(0 && "Invalid access specifier");
668 };
669 if ((data().HasPrivateFields + data().HasProtectedFields +
670 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000671 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000672
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000673 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000674 // A POD struct is a class that is both a trivial class and a
675 // standard-layout class, and has no non-static data members of type
676 // non-POD struct, non-POD union (or array of such types).
677 ASTContext &Context = getASTContext();
678 QualType T = Context.getBaseElementType(Field->getType());
679 if (!T->isPODType())
680 data().PlainOldData = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000681 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000682 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000683
Chandler Carrutha8225442011-04-30 09:17:45 +0000684 // C++0x [class]p7:
685 // A standard-layout class is a class that:
686 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000687 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000688 }
689
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000690 // Record if this field is the first non-literal field or base.
691 if (!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType())
692 data().HasNonLiteralTypeFieldsOrBases = true;
693
Douglas Gregor85606eb2010-09-28 20:50:54 +0000694 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
695 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
696 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000697 // C++0x [class.ctor]p5:
698 // A defulat constructor is trivial [...] if:
699 // -- for all the non-static data members of its class that are of
700 // class type (or array thereof), each such class has a trivial
701 // default constructor.
702 if (!FieldRec->hasTrivialDefaultConstructor())
703 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000704
705 // C++0x [class.copy]p13:
706 // A copy/move constructor for class X is trivial if [...]
707 // [...]
708 // -- for each non-static data member of X that is of class type (or
709 // an array thereof), the constructor selected to copy/move that
710 // member is trivial;
711 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000712 if (!FieldRec->hasTrivialCopyConstructor())
713 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000714 if (!FieldRec->hasTrivialMoveConstructor())
715 data().HasTrivialMoveConstructor = false;
716
717 // C++0x [class.copy]p27:
718 // A copy/move assignment operator for class X is trivial if [...]
719 // [...]
720 // -- for each non-static data member of X that is of class type (or
721 // an array thereof), the assignment operator selected to
722 // copy/move that member is trivial;
723 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000724 if (!FieldRec->hasTrivialCopyAssignment())
725 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000726 if (!FieldRec->hasTrivialMoveAssignment())
727 data().HasTrivialMoveAssignment = false;
728
Douglas Gregor85606eb2010-09-28 20:50:54 +0000729 if (!FieldRec->hasTrivialDestructor())
730 data().HasTrivialDestructor = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000731
732 // C++0x [class]p7:
733 // A standard-layout class is a class that:
734 // -- has no non-static data members of type non-standard-layout
735 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000736 if (!FieldRec->isStandardLayout())
737 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000738
739 // C++0x [class]p7:
740 // A standard-layout class is a class that:
741 // [...]
742 // -- has no base classes of the same type as the first non-static
743 // data member.
744 // We don't want to expend bits in the state of the record decl
745 // tracking whether this is the first non-static data member so we
746 // cheat a bit and use some of the existing state: the empty bit.
747 // Virtual bases and virtual methods make a class non-empty, but they
748 // also make it non-standard-layout so we needn't check here.
749 // A non-empty base class may leave the class standard-layout, but not
750 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000751 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000752 // data member must come through here with Empty still true, and Empty
753 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000754 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000755 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
756 BE = bases_end();
757 BI != BE; ++BI) {
758 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000759 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000760 break;
761 }
762 }
763 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000764 }
765 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000766
767 // C++0x [class]p7:
768 // A standard-layout class is a class that:
769 // [...]
770 // -- either has no non-static data members in the most derived
771 // class and at most one base class with non-static data members,
772 // or has no base classes with non-static data members, and
773 // At this point we know that we have a non-static data member, so the last
774 // clause holds.
775 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000776 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000777
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000778 // If this is not a zero-length bit-field, then the class is not empty.
779 if (data().Empty) {
780 if (!Field->getBitWidth())
781 data().Empty = false;
782 else if (!Field->getBitWidth()->isTypeDependent() &&
783 !Field->getBitWidth()->isValueDependent()) {
784 llvm::APSInt Bits;
785 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
786 if (!!Bits)
787 data().Empty = false;
788 }
789 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000790 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000791
792 // Handle using declarations of conversion functions.
793 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
794 if (Shadow->getDeclName().getNameKind()
795 == DeclarationName::CXXConversionFunctionName)
796 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000797}
798
John McCallb05b5f32010-03-15 09:07:48 +0000799static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
800 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000801 if (isa<UsingShadowDecl>(Conv))
802 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000803 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
804 T = ConvTemp->getTemplatedDecl()->getResultType();
805 else
806 T = cast<CXXConversionDecl>(Conv)->getConversionType();
807 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000808}
809
John McCallb05b5f32010-03-15 09:07:48 +0000810/// Collect the visible conversions of a base class.
811///
812/// \param Base a base class of the class we're considering
813/// \param InVirtual whether this base class is a virtual base (or a base
814/// of a virtual base)
815/// \param Access the access along the inheritance path to this base
816/// \param ParentHiddenTypes the conversions provided by the inheritors
817/// of this base
818/// \param Output the set to which to add conversions from non-virtual bases
819/// \param VOutput the set to which to add conversions from virtual bases
820/// \param HiddenVBaseCs the set of conversions which were hidden in a
821/// virtual base along some inheritance path
822static void CollectVisibleConversions(ASTContext &Context,
823 CXXRecordDecl *Record,
824 bool InVirtual,
825 AccessSpecifier Access,
826 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
827 UnresolvedSetImpl &Output,
828 UnresolvedSetImpl &VOutput,
829 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
830 // The set of types which have conversions in this class or its
831 // subclasses. As an optimization, we don't copy the derived set
832 // unless it might change.
833 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
834 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
835
836 // Collect the direct conversions and figure out which conversions
837 // will be hidden in the subclasses.
838 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
839 if (!Cs.empty()) {
840 HiddenTypesBuffer = ParentHiddenTypes;
841 HiddenTypes = &HiddenTypesBuffer;
842
843 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
844 bool Hidden =
845 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
846
847 // If this conversion is hidden and we're in a virtual base,
848 // remember that it's hidden along some inheritance path.
849 if (Hidden && InVirtual)
850 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
851
852 // If this conversion isn't hidden, add it to the appropriate output.
853 else if (!Hidden) {
854 AccessSpecifier IAccess
855 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
856
857 if (InVirtual)
858 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000859 else
John McCallb05b5f32010-03-15 09:07:48 +0000860 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000861 }
862 }
863 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000864
John McCallb05b5f32010-03-15 09:07:48 +0000865 // Collect information recursively from any base classes.
866 for (CXXRecordDecl::base_class_iterator
867 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
868 const RecordType *RT = I->getType()->getAs<RecordType>();
869 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000870
John McCallb05b5f32010-03-15 09:07:48 +0000871 AccessSpecifier BaseAccess
872 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
873 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000874
John McCallb05b5f32010-03-15 09:07:48 +0000875 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
876 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
877 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000878 }
John McCallb05b5f32010-03-15 09:07:48 +0000879}
Sebastian Redl9994a342009-10-25 17:03:50 +0000880
John McCallb05b5f32010-03-15 09:07:48 +0000881/// Collect the visible conversions of a class.
882///
883/// This would be extremely straightforward if it weren't for virtual
884/// bases. It might be worth special-casing that, really.
885static void CollectVisibleConversions(ASTContext &Context,
886 CXXRecordDecl *Record,
887 UnresolvedSetImpl &Output) {
888 // The collection of all conversions in virtual bases that we've
889 // found. These will be added to the output as long as they don't
890 // appear in the hidden-conversions set.
891 UnresolvedSet<8> VBaseCs;
892
893 // The set of conversions in virtual bases that we've determined to
894 // be hidden.
895 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
896
897 // The set of types hidden by classes derived from this one.
898 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
899
900 // Go ahead and collect the direct conversions and add them to the
901 // hidden-types set.
902 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
903 Output.append(Cs.begin(), Cs.end());
904 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
905 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
906
907 // Recursively collect conversions from base classes.
908 for (CXXRecordDecl::base_class_iterator
909 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
910 const RecordType *RT = I->getType()->getAs<RecordType>();
911 if (!RT) continue;
912
913 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
914 I->isVirtual(), I->getAccessSpecifier(),
915 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
916 }
917
918 // Add any unhidden conversions provided by virtual bases.
919 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
920 I != E; ++I) {
921 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
922 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000923 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000924}
925
926/// getVisibleConversionFunctions - get all conversion functions visible
927/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000928const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000929 // If root class, all conversions are visible.
930 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000931 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000932 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000933 if (data().ComputedVisibleConversions)
934 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000935 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000936 data().ComputedVisibleConversions = true;
937 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000938}
939
John McCall32daa422010-03-31 01:36:47 +0000940void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
941 // This operation is O(N) but extremely rare. Sema only uses it to
942 // remove UsingShadowDecls in a class that were followed by a direct
943 // declaration, e.g.:
944 // class A : B {
945 // using B::operator int;
946 // operator int();
947 // };
948 // This is uncommon by itself and even more uncommon in conjunction
949 // with sufficiently large numbers of directly-declared conversions
950 // that asymptotic behavior matters.
951
952 UnresolvedSetImpl &Convs = *getConversionFunctions();
953 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
954 if (Convs[I].getDecl() == ConvDecl) {
955 Convs.erase(I);
956 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
957 && "conversion was found multiple times in unresolved set");
958 return;
959 }
960 }
961
962 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000963}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000964
Douglas Gregorf6b11852009-10-08 15:14:33 +0000965CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000966 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000967 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
968
969 return 0;
970}
971
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000972MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
973 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
974}
975
Douglas Gregorf6b11852009-10-08 15:14:33 +0000976void
977CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
978 TemplateSpecializationKind TSK) {
979 assert(TemplateOrInstantiation.isNull() &&
980 "Previous template or instantiation?");
981 assert(!isa<ClassTemplateSpecializationDecl>(this));
982 TemplateOrInstantiation
983 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
984}
985
Anders Carlssonb13e3572009-12-07 06:33:48 +0000986TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
987 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000988 = dyn_cast<ClassTemplateSpecializationDecl>(this))
989 return Spec->getSpecializationKind();
990
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000991 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000992 return MSInfo->getTemplateSpecializationKind();
993
994 return TSK_Undeclared;
995}
996
997void
998CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
999 if (ClassTemplateSpecializationDecl *Spec
1000 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1001 Spec->setSpecializationKind(TSK);
1002 return;
1003 }
1004
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001005 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001006 MSInfo->setTemplateSpecializationKind(TSK);
1007 return;
1008 }
1009
1010 assert(false && "Not a class template or member class specialization");
1011}
1012
Douglas Gregor1d110e02010-07-01 14:13:13 +00001013CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1014 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001015 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001016
1017 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001018 = Context.DeclarationNames.getCXXDestructorName(
1019 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001020
John McCallc0bf4622010-02-23 00:48:20 +00001021 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001022 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001023 if (I == E)
1024 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001026 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001027 return Dtor;
1028}
1029
Douglas Gregorda2142f2011-02-19 18:51:44 +00001030void CXXRecordDecl::completeDefinition() {
1031 completeDefinition(0);
1032}
1033
1034void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1035 RecordDecl::completeDefinition();
1036
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001037 // If the class may be abstract (but hasn't been marked as such), check for
1038 // any pure final overriders.
1039 if (mayBeAbstract()) {
1040 CXXFinalOverriderMap MyFinalOverriders;
1041 if (!FinalOverriders) {
1042 getFinalOverriders(MyFinalOverriders);
1043 FinalOverriders = &MyFinalOverriders;
1044 }
1045
1046 bool Done = false;
1047 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1048 MEnd = FinalOverriders->end();
1049 M != MEnd && !Done; ++M) {
1050 for (OverridingMethods::iterator SO = M->second.begin(),
1051 SOEnd = M->second.end();
1052 SO != SOEnd && !Done; ++SO) {
1053 assert(SO->second.size() > 0 &&
1054 "All virtual functions have overridding virtual functions");
1055
1056 // C++ [class.abstract]p4:
1057 // A class is abstract if it contains or inherits at least one
1058 // pure virtual function for which the final overrider is pure
1059 // virtual.
1060 if (SO->second.front().Method->isPure()) {
1061 data().Abstract = true;
1062 Done = true;
1063 break;
1064 }
1065 }
1066 }
1067 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001068
1069 // Set access bits correctly on the directly-declared conversions.
1070 for (UnresolvedSetIterator I = data().Conversions.begin(),
1071 E = data().Conversions.end();
1072 I != E; ++I)
1073 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001074}
1075
1076bool CXXRecordDecl::mayBeAbstract() const {
1077 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1078 isDependentContext())
1079 return false;
1080
1081 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1082 BEnd = bases_end();
1083 B != BEnd; ++B) {
1084 CXXRecordDecl *BaseDecl
1085 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1086 if (BaseDecl->isAbstract())
1087 return true;
1088 }
1089
1090 return false;
1091}
1092
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001093CXXMethodDecl *
1094CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001095 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001096 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001097 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001098 bool isStatic, StorageClass SCAsWritten, bool isInline,
1099 SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001100 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001101 isStatic, SCAsWritten, isInline, EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001102}
1103
Douglas Gregor90916562009-09-29 18:16:17 +00001104bool CXXMethodDecl::isUsualDeallocationFunction() const {
1105 if (getOverloadedOperator() != OO_Delete &&
1106 getOverloadedOperator() != OO_Array_Delete)
1107 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001108
1109 // C++ [basic.stc.dynamic.deallocation]p2:
1110 // A template instance is never a usual deallocation function,
1111 // regardless of its signature.
1112 if (getPrimaryTemplate())
1113 return false;
1114
Douglas Gregor90916562009-09-29 18:16:17 +00001115 // C++ [basic.stc.dynamic.deallocation]p2:
1116 // If a class T has a member deallocation function named operator delete
1117 // with exactly one parameter, then that function is a usual (non-placement)
1118 // deallocation function. [...]
1119 if (getNumParams() == 1)
1120 return true;
1121
1122 // C++ [basic.stc.dynamic.deallocation]p2:
1123 // [...] If class T does not declare such an operator delete but does
1124 // declare a member deallocation function named operator delete with
1125 // exactly two parameters, the second of which has type std::size_t (18.1),
1126 // then this function is a usual deallocation function.
1127 ASTContext &Context = getASTContext();
1128 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001129 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1130 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001131 return false;
1132
1133 // This function is a usual deallocation function if there are no
1134 // single-parameter deallocation functions of the same kind.
1135 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1136 R.first != R.second; ++R.first) {
1137 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1138 if (FD->getNumParams() == 1)
1139 return false;
1140 }
1141
1142 return true;
1143}
1144
Douglas Gregor06a9f362010-05-01 20:49:11 +00001145bool CXXMethodDecl::isCopyAssignmentOperator() const {
1146 // C++0x [class.copy]p19:
1147 // A user-declared copy assignment operator X::operator= is a non-static
1148 // non-template member function of class X with exactly one parameter of
1149 // type X, X&, const X&, volatile X& or const volatile X&.
1150 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1151 /*non-static*/ isStatic() ||
1152 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1153 /*exactly one parameter*/getNumParams() != 1)
1154 return false;
1155
1156 QualType ParamType = getParamDecl(0)->getType();
1157 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1158 ParamType = Ref->getPointeeType();
1159
1160 ASTContext &Context = getASTContext();
1161 QualType ClassType
1162 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1163 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1164}
1165
Anders Carlsson05eb2442009-05-16 23:58:37 +00001166void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001167 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001168 assert(!MD->getParent()->isDependentContext() &&
1169 "Can't add an overridden method to a class template!");
1170
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001171 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001172}
1173
1174CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001175 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001176}
1177
1178CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001179 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001180}
1181
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001182unsigned CXXMethodDecl::size_overridden_methods() const {
1183 return getASTContext().overridden_methods_size(this);
1184}
1185
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001186QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001187 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1188 // If the member function is declared const, the type of this is const X*,
1189 // if the member function is declared volatile, the type of this is
1190 // volatile X*, and if the member function is declared const volatile,
1191 // the type of this is const volatile X*.
1192
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001193 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001194
John McCall3cb0ebd2010-03-10 03:28:59 +00001195 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001196 ClassTy = C.getQualifiedType(ClassTy,
1197 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001198 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001199}
1200
Eli Friedmand7d7f672009-12-06 20:50:05 +00001201bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001202 // If this function is a template instantiation, look at the template from
1203 // which it was instantiated.
1204 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1205 if (!CheckFn)
1206 CheckFn = this;
1207
Eli Friedmand7d7f672009-12-06 20:50:05 +00001208 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001209 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001210}
1211
Sean Huntcbb67482011-01-08 20:30:50 +00001212CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1213 TypeSourceInfo *TInfo, bool IsVirtual,
1214 SourceLocation L, Expr *Init,
1215 SourceLocation R,
1216 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001217 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001218 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1219 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001220{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001221}
1222
Sean Huntcbb67482011-01-08 20:30:50 +00001223CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1224 FieldDecl *Member,
1225 SourceLocation MemberLoc,
1226 SourceLocation L, Expr *Init,
1227 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001228 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001229 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1230 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1231{
1232}
1233
Sean Huntcbb67482011-01-08 20:30:50 +00001234CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1235 IndirectFieldDecl *Member,
1236 SourceLocation MemberLoc,
1237 SourceLocation L, Expr *Init,
1238 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001239 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001240 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001241 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001242{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001243}
1244
Sean Huntcbb67482011-01-08 20:30:50 +00001245CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Hunt41717662011-02-26 19:13:13 +00001246 SourceLocation D, SourceLocation L,
1247 CXXConstructorDecl *Target, Expr *Init,
1248 SourceLocation R)
1249 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1250 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1251 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1252{
1253}
1254
1255CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001256 FieldDecl *Member,
1257 SourceLocation MemberLoc,
1258 SourceLocation L, Expr *Init,
1259 SourceLocation R,
1260 VarDecl **Indices,
1261 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001262 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001263 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001264 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001265{
1266 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1267 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1268}
1269
Sean Huntcbb67482011-01-08 20:30:50 +00001270CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1271 FieldDecl *Member,
1272 SourceLocation MemberLoc,
1273 SourceLocation L, Expr *Init,
1274 SourceLocation R,
1275 VarDecl **Indices,
1276 unsigned NumIndices) {
1277 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001278 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001279 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001280 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1281 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001282}
1283
Sean Huntcbb67482011-01-08 20:30:50 +00001284TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001285 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001286 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001287 else
1288 return TypeLoc();
1289}
1290
Sean Huntcbb67482011-01-08 20:30:50 +00001291const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001292 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001293 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001294 else
1295 return 0;
1296}
1297
Sean Huntcbb67482011-01-08 20:30:50 +00001298SourceLocation CXXCtorInitializer::getSourceLocation() const {
Sean Hunt41717662011-02-26 19:13:13 +00001299 if (isAnyMemberInitializer() || isDelegatingInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001300 return getMemberLocation();
1301
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001302 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001303}
1304
Sean Huntcbb67482011-01-08 20:30:50 +00001305SourceRange CXXCtorInitializer::getSourceRange() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001306 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001307}
1308
Douglas Gregorb48fe382008-10-31 09:07:45 +00001309CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001310CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001311 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Sean Hunt5f802e52011-05-06 00:11:07 +00001312 QualType(), 0, false, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001313}
1314
1315CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001316CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001317 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001318 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001319 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001320 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001321 bool isInline,
Sean Hunt5f802e52011-05-06 00:11:07 +00001322 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001323 assert(NameInfo.getName().getNameKind()
1324 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001325 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001326 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Sean Hunt5f802e52011-05-06 00:11:07 +00001327 isExplicit, isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001328}
1329
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001330bool CXXConstructorDecl::isDefaultConstructor() const {
1331 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001332 // A default constructor for a class X is a constructor of class
1333 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001334 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001335 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001336}
1337
Mike Stump1eb44332009-09-09 15:08:12 +00001338bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001339CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001340 return isCopyOrMoveConstructor(TypeQuals) &&
1341 getParamDecl(0)->getType()->isLValueReferenceType();
1342}
1343
1344bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1345 return isCopyOrMoveConstructor(TypeQuals) &&
1346 getParamDecl(0)->getType()->isRValueReferenceType();
1347}
1348
1349/// \brief Determine whether this is a copy or move constructor.
1350bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001351 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001352 // A non-template constructor for class X is a copy constructor
1353 // if its first parameter is of type X&, const X&, volatile X& or
1354 // const volatile X&, and either there are no other parameters
1355 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001356 // C++0x [class.copy]p3:
1357 // A non-template constructor for class X is a move constructor if its
1358 // first parameter is of type X&&, const X&&, volatile X&&, or
1359 // const volatile X&&, and either there are no other parameters or else
1360 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001361 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001362 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001363 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001364 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001365 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001366
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001367 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001368
1369 // Do we have a reference type?
1370 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001371 if (!ParamRefType)
1372 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001373
Douglas Gregorfd476482009-11-13 23:59:09 +00001374 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001375 ASTContext &Context = getASTContext();
1376
Douglas Gregorfd476482009-11-13 23:59:09 +00001377 CanQualType PointeeType
1378 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001379 CanQualType ClassTy
1380 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001381 if (PointeeType.getUnqualifiedType() != ClassTy)
1382 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001383
John McCall0953e762009-09-24 19:53:00 +00001384 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001385
1386 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001387 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001388 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001389}
1390
Anders Carlssonfaccd722009-08-28 16:57:08 +00001391bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001392 // C++ [class.conv.ctor]p1:
1393 // A constructor declared without the function-specifier explicit
1394 // that can be called with a single parameter specifies a
1395 // conversion from the type of its first parameter to the type of
1396 // its class. Such a constructor is called a converting
1397 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001398 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001399 return false;
1400
Mike Stump1eb44332009-09-09 15:08:12 +00001401 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001402 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001403 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001404 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001405}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001406
Douglas Gregor6493cc52010-11-08 17:16:59 +00001407bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001408 if ((getNumParams() < 1) ||
1409 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1410 (getPrimaryTemplate() == 0) ||
1411 (getDescribedFunctionTemplate() != 0))
1412 return false;
1413
1414 const ParmVarDecl *Param = getParamDecl(0);
1415
1416 ASTContext &Context = getASTContext();
1417 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1418
Douglas Gregor66724ea2009-11-14 01:20:54 +00001419 // Is it the same as our our class type?
1420 CanQualType ClassTy
1421 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1422 if (ParamType.getUnqualifiedType() != ClassTy)
1423 return false;
1424
1425 return true;
1426}
1427
Sebastian Redlf677ea32011-02-05 19:23:19 +00001428const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1429 // Hack: we store the inherited constructor in the overridden method table
1430 method_iterator It = begin_overridden_methods();
1431 if (It == end_overridden_methods())
1432 return 0;
1433
1434 return cast<CXXConstructorDecl>(*It);
1435}
1436
1437void
1438CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1439 // Hack: we store the inherited constructor in the overridden method table
1440 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1441 addOverriddenMethod(BaseCtor);
1442}
1443
Douglas Gregor42a552f2008-11-05 20:51:48 +00001444CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001445CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001446 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001447 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001448}
1449
1450CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001451CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001452 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001453 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001454 QualType T, TypeSourceInfo *TInfo,
1455 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001456 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001457 assert(NameInfo.getName().getNameKind()
1458 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001459 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001460 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001461 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001462}
1463
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001464CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001465CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001466 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001467 QualType(), 0, false, false,
1468 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001469}
1470
1471CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001472CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001473 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001474 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001475 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001476 bool isInline, bool isExplicit,
1477 SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001478 assert(NameInfo.getName().getNameKind()
1479 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001480 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001481 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001482 isInline, isExplicit, EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001483}
1484
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001485LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001486 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001487 SourceLocation ExternLoc,
1488 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001489 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001490 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001491 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001492}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001493
1494UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1495 SourceLocation L,
1496 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001497 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001498 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001499 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001500 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001501 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1502 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001503 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1504 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001505}
1506
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001507NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1508 if (NamespaceAliasDecl *NA =
1509 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1510 return NA->getNamespace();
1511 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1512}
1513
Mike Stump1eb44332009-09-09 15:08:12 +00001514NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001515 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001516 SourceLocation AliasLoc,
1517 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001518 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001519 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001520 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001521 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1522 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001523 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1524 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001525}
1526
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001527UsingDecl *UsingShadowDecl::getUsingDecl() const {
1528 const UsingShadowDecl *Shadow = this;
1529 while (const UsingShadowDecl *NextShadow =
1530 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1531 Shadow = NextShadow;
1532 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1533}
1534
1535void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1536 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1537 "declaration already in set");
1538 assert(S->getUsingDecl() == this);
1539
1540 if (FirstUsingShadow)
1541 S->UsingOrNextShadow = FirstUsingShadow;
1542 FirstUsingShadow = S;
1543}
1544
1545void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1546 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1547 "declaration not in set");
1548 assert(S->getUsingDecl() == this);
1549
1550 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1551
1552 if (FirstUsingShadow == S) {
1553 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1554 S->UsingOrNextShadow = this;
1555 return;
1556 }
1557
1558 UsingShadowDecl *Prev = FirstUsingShadow;
1559 while (Prev->UsingOrNextShadow != S)
1560 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1561 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1562 S->UsingOrNextShadow = this;
1563}
1564
Douglas Gregordc355712011-02-25 00:36:19 +00001565UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1566 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001567 const DeclarationNameInfo &NameInfo,
1568 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001569 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001570}
1571
John McCall7ba107a2009-11-18 02:36:19 +00001572UnresolvedUsingValueDecl *
1573UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1574 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001575 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001576 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001577 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001578 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001579}
1580
1581UnresolvedUsingTypenameDecl *
1582UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1583 SourceLocation UsingLoc,
1584 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001585 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001586 SourceLocation TargetNameLoc,
1587 DeclarationName TargetName) {
1588 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001589 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001590 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001591}
1592
Anders Carlssonfb311762009-03-14 00:25:26 +00001593StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001594 SourceLocation StaticAssertLoc,
1595 Expr *AssertExpr,
1596 StringLiteral *Message,
1597 SourceLocation RParenLoc) {
1598 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1599 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001600}
1601
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001602static const char *getAccessName(AccessSpecifier AS) {
1603 switch (AS) {
1604 default:
1605 case AS_none:
1606 assert("Invalid access specifier!");
1607 return 0;
1608 case AS_public:
1609 return "public";
1610 case AS_private:
1611 return "private";
1612 case AS_protected:
1613 return "protected";
1614 }
1615}
1616
1617const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1618 AccessSpecifier AS) {
1619 return DB << getAccessName(AS);
1620}