blob: a18aeffe574f8fa884002f0da22f06d01229a23e [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),
Sean Hunt37b8c9e2011-05-09 21:45:35 +000041 NeedsImplicitDefaultConstructor(false), DeclaredDefaultConstructor(false),
42 DeclaredCopyConstructor(false), DeclaredCopyAssignment(false),
43 DeclaredDestructor(false), 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.
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000461 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000462 data().DeclaredDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000463 data().NeedsImplicitDefaultConstructor = true;
464 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000465 // If this is the implicit copy constructor, note that we have now
466 // declared it.
467 else if (Constructor->isCopyConstructor())
468 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000469 return;
470 }
471
472 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000473 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000474 return;
475 }
476
477 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000478 // If this is the implicit copy constructor, note that we have now
479 // declared it.
480 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000481 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000482 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000483 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000484 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000485
486 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000487 }
488
489 // Handle (user-declared) constructors.
490 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
491 // Note that we have a user-declared constructor.
492 data().UserDeclaredConstructor = true;
493
494 // Note that we have no need of an implicitly-declared default constructor.
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000495 data().NeedsImplicitDefaultConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000496
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000497 // FIXME: Under C++0x, /only/ special member functions may be user-provided.
498 // This is probably a defect.
499 bool UserProvided = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000500
Sean Hunt023df372011-05-09 18:22:59 +0000501 // C++0x [class.ctor]p5:
502 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000503 if (Constructor->isDefaultConstructor()) {
504 data().DeclaredDefaultConstructor = true;
505 if (Constructor->isUserProvided()) {
506 data().HasTrivialDefaultConstructor = false;
507 UserProvided = true;
508 }
509 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000510
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000511 // Note when we have a user-declared copy or move constructor, which will
512 // suppress the implicit declaration of those constructors.
513 if (!FunTmpl) {
514 if (Constructor->isCopyConstructor()) {
515 data().UserDeclaredCopyConstructor = true;
516 data().DeclaredCopyConstructor = true;
517
518 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000519 // A copy/move constructor for class X is trivial if it is not
520 // user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000521 if (Constructor->isUserProvided()) {
Sean Hunt023df372011-05-09 18:22:59 +0000522 data().HasTrivialCopyConstructor = false;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000523 UserProvided = true;
524 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000525 } else if (Constructor->isMoveConstructor()) {
526 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000527 // A copy/move constructor for class X is trivial if it is not
528 // user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000529 if (Constructor->isUserProvided()) {
Sean Hunt023df372011-05-09 18:22:59 +0000530 data().HasTrivialMoveConstructor = false;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000531 UserProvided = true;
532 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000533 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000534 }
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000535 if (Constructor->isConstExpr() &&
536 !Constructor->isCopyOrMoveConstructor()) {
537 // Record if we see any constexpr constructors which are niether copy
538 // nor move constructors.
539 data().HasConstExprNonCopyMoveConstructor = true;
540 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000541
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000542 // C++ [dcl.init.aggr]p1:
543 // An aggregate is an array or a class with no user-declared
544 // constructors [...].
545 // C++0x [dcl.init.aggr]p1:
546 // An aggregate is an array or a class with no user-provided
547 // constructors [...].
548 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
549 data().Aggregate = false;
550
551 // C++ [class]p4:
552 // A POD-struct is an aggregate class [...]
553 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
554 // type is technically an aggregate in C++0x since it wouldn't be in 03.
555 data().PlainOldData = false;
556
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000557 return;
558 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000559
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000560 // Handle (user-declared) destructors.
561 if (isa<CXXDestructorDecl>(D)) {
562 data().DeclaredDestructor = true;
563 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000564
565 // C++ [class]p4:
566 // A POD-struct is an aggregate class that has [...] no user-defined
567 // destructor.
568 data().PlainOldData = false;
569
Douglas Gregor85606eb2010-09-28 20:50:54 +0000570 // C++ [class.dtor]p3:
571 // A destructor is trivial if it is an implicitly-declared destructor and
572 // [...].
573 //
574 // FIXME: C++0x: don't do this for "= default" destructors
575 data().HasTrivialDestructor = false;
576
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000577 return;
578 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000579
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000580 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000581 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
582 if (Method->getOverloadedOperator() == OO_Equal) {
583 // We're interested specifically in copy assignment operators.
584 const FunctionProtoType *FnType
585 = Method->getType()->getAs<FunctionProtoType>();
586 assert(FnType && "Overloaded operator has no proto function type.");
587 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
588
589 // Copy assignment operators must be non-templates.
590 if (Method->getPrimaryTemplate() || FunTmpl)
591 return;
592
593 ASTContext &Context = getASTContext();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000594 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
595 const_cast<CXXRecordDecl*>(this)));
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000596
597 bool isRValueRefArg = false;
598 QualType ArgType = FnType->getArgType(0);
599 if (const LValueReferenceType *Ref =
600 ArgType->getAs<LValueReferenceType>()) {
601 ArgType = Ref->getPointeeType();
602 } else if (const RValueReferenceType *Ref =
603 ArgType->getAs<RValueReferenceType>()) {
604 ArgType = Ref->getPointeeType();
605 isRValueRefArg = true;
606 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000607 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
608 return;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000609
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000610 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000611 // A POD-struct is an aggregate class that [...] has no user-defined
612 // copy assignment operator [...].
613 // FIXME: This should be probably determined dynamically in terms of
614 // other more precise attributes to correctly model how it is specified
615 // in C++0x. Setting it here happens to do the right thing.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000616 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000617
618 if (!isRValueRefArg) {
619 // This is a copy assignment operator.
620
621 // Suppress the implicit declaration of a copy constructor.
622 data().UserDeclaredCopyAssignment = true;
623 data().DeclaredCopyAssignment = true;
624
625 // C++0x [class.copy]p27:
626 // A copy/move assignment operator for class X is trivial if it is
627 // neither user-provided nor deleted [...]
628 // FIXME: C++0x: don't do this for "= default" copy operators.
629 data().HasTrivialCopyAssignment = false;
630 } else {
631 // This is a move assignment operator.
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 // FIXME: C++0x: don't do this for "= default" copy operators.
637 data().HasTrivialMoveAssignment = false;
638 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000639 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000640
Douglas Gregore80622f2010-09-29 04:25:11 +0000641 // Keep the list of conversion functions up-to-date.
642 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
643 // We don't record specializations.
644 if (Conversion->getPrimaryTemplate())
645 return;
646
647 // FIXME: We intentionally don't use the decl's access here because it
648 // hasn't been set yet. That's really just a misdesign in Sema.
649
650 if (FunTmpl) {
651 if (FunTmpl->getPreviousDeclaration())
652 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
653 FunTmpl);
654 else
655 data().Conversions.addDecl(FunTmpl);
656 } else {
657 if (Conversion->getPreviousDeclaration())
658 data().Conversions.replace(Conversion->getPreviousDeclaration(),
659 Conversion);
660 else
661 data().Conversions.addDecl(Conversion);
662 }
663 }
664
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000665 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000666 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000667
668 // Handle non-static data members.
669 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
670 // C++ [dcl.init.aggr]p1:
671 // An aggregate is an array or a class (clause 9) with [...] no
672 // private or protected non-static data members (clause 11).
673 //
674 // A POD must be an aggregate.
675 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
676 data().Aggregate = false;
677 data().PlainOldData = false;
678 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000679
680 // C++0x [class]p7:
681 // A standard-layout class is a class that:
682 // [...]
683 // -- has the same access control for all non-static data members,
684 switch (D->getAccess()) {
685 case AS_private: data().HasPrivateFields = true; break;
686 case AS_protected: data().HasProtectedFields = true; break;
687 case AS_public: data().HasPublicFields = true; break;
688 case AS_none: assert(0 && "Invalid access specifier");
689 };
690 if ((data().HasPrivateFields + data().HasProtectedFields +
691 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000692 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000693
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000694 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000695 // A POD struct is a class that is both a trivial class and a
696 // standard-layout class, and has no non-static data members of type
697 // non-POD struct, non-POD union (or array of such types).
698 ASTContext &Context = getASTContext();
699 QualType T = Context.getBaseElementType(Field->getType());
700 if (!T->isPODType())
701 data().PlainOldData = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000702 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000703 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000704
Chandler Carrutha8225442011-04-30 09:17:45 +0000705 // C++0x [class]p7:
706 // A standard-layout class is a class that:
707 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000708 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000709 }
710
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000711 // Record if this field is the first non-literal field or base.
712 if (!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType())
713 data().HasNonLiteralTypeFieldsOrBases = true;
714
Douglas Gregor85606eb2010-09-28 20:50:54 +0000715 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
716 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
717 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000718 // C++0x [class.ctor]p5:
719 // A defulat constructor is trivial [...] if:
720 // -- for all the non-static data members of its class that are of
721 // class type (or array thereof), each such class has a trivial
722 // default constructor.
723 if (!FieldRec->hasTrivialDefaultConstructor())
724 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000725
726 // C++0x [class.copy]p13:
727 // A copy/move constructor for class X is trivial if [...]
728 // [...]
729 // -- for each non-static data member of X that is of class type (or
730 // an array thereof), the constructor selected to copy/move that
731 // member is trivial;
732 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000733 if (!FieldRec->hasTrivialCopyConstructor())
734 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000735 if (!FieldRec->hasTrivialMoveConstructor())
736 data().HasTrivialMoveConstructor = false;
737
738 // C++0x [class.copy]p27:
739 // A copy/move assignment operator for class X is trivial if [...]
740 // [...]
741 // -- for each non-static data member of X that is of class type (or
742 // an array thereof), the assignment operator selected to
743 // copy/move that member is trivial;
744 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000745 if (!FieldRec->hasTrivialCopyAssignment())
746 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000747 if (!FieldRec->hasTrivialMoveAssignment())
748 data().HasTrivialMoveAssignment = false;
749
Douglas Gregor85606eb2010-09-28 20:50:54 +0000750 if (!FieldRec->hasTrivialDestructor())
751 data().HasTrivialDestructor = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000752
753 // C++0x [class]p7:
754 // A standard-layout class is a class that:
755 // -- has no non-static data members of type non-standard-layout
756 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000757 if (!FieldRec->isStandardLayout())
758 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000759
760 // C++0x [class]p7:
761 // A standard-layout class is a class that:
762 // [...]
763 // -- has no base classes of the same type as the first non-static
764 // data member.
765 // We don't want to expend bits in the state of the record decl
766 // tracking whether this is the first non-static data member so we
767 // cheat a bit and use some of the existing state: the empty bit.
768 // Virtual bases and virtual methods make a class non-empty, but they
769 // also make it non-standard-layout so we needn't check here.
770 // A non-empty base class may leave the class standard-layout, but not
771 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000772 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000773 // data member must come through here with Empty still true, and Empty
774 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000775 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000776 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
777 BE = bases_end();
778 BI != BE; ++BI) {
779 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000780 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000781 break;
782 }
783 }
784 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000785 }
786 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000787
788 // C++0x [class]p7:
789 // A standard-layout class is a class that:
790 // [...]
791 // -- either has no non-static data members in the most derived
792 // class and at most one base class with non-static data members,
793 // or has no base classes with non-static data members, and
794 // At this point we know that we have a non-static data member, so the last
795 // clause holds.
796 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000797 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000798
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000799 // If this is not a zero-length bit-field, then the class is not empty.
800 if (data().Empty) {
801 if (!Field->getBitWidth())
802 data().Empty = false;
803 else if (!Field->getBitWidth()->isTypeDependent() &&
804 !Field->getBitWidth()->isValueDependent()) {
805 llvm::APSInt Bits;
806 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
807 if (!!Bits)
808 data().Empty = false;
809 }
810 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000811 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000812
813 // Handle using declarations of conversion functions.
814 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
815 if (Shadow->getDeclName().getNameKind()
816 == DeclarationName::CXXConversionFunctionName)
817 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000818}
819
John McCallb05b5f32010-03-15 09:07:48 +0000820static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
821 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000822 if (isa<UsingShadowDecl>(Conv))
823 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000824 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
825 T = ConvTemp->getTemplatedDecl()->getResultType();
826 else
827 T = cast<CXXConversionDecl>(Conv)->getConversionType();
828 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000829}
830
John McCallb05b5f32010-03-15 09:07:48 +0000831/// Collect the visible conversions of a base class.
832///
833/// \param Base a base class of the class we're considering
834/// \param InVirtual whether this base class is a virtual base (or a base
835/// of a virtual base)
836/// \param Access the access along the inheritance path to this base
837/// \param ParentHiddenTypes the conversions provided by the inheritors
838/// of this base
839/// \param Output the set to which to add conversions from non-virtual bases
840/// \param VOutput the set to which to add conversions from virtual bases
841/// \param HiddenVBaseCs the set of conversions which were hidden in a
842/// virtual base along some inheritance path
843static void CollectVisibleConversions(ASTContext &Context,
844 CXXRecordDecl *Record,
845 bool InVirtual,
846 AccessSpecifier Access,
847 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
848 UnresolvedSetImpl &Output,
849 UnresolvedSetImpl &VOutput,
850 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
851 // The set of types which have conversions in this class or its
852 // subclasses. As an optimization, we don't copy the derived set
853 // unless it might change.
854 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
855 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
856
857 // Collect the direct conversions and figure out which conversions
858 // will be hidden in the subclasses.
859 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
860 if (!Cs.empty()) {
861 HiddenTypesBuffer = ParentHiddenTypes;
862 HiddenTypes = &HiddenTypesBuffer;
863
864 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
865 bool Hidden =
866 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
867
868 // If this conversion is hidden and we're in a virtual base,
869 // remember that it's hidden along some inheritance path.
870 if (Hidden && InVirtual)
871 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
872
873 // If this conversion isn't hidden, add it to the appropriate output.
874 else if (!Hidden) {
875 AccessSpecifier IAccess
876 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
877
878 if (InVirtual)
879 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000880 else
John McCallb05b5f32010-03-15 09:07:48 +0000881 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000882 }
883 }
884 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000885
John McCallb05b5f32010-03-15 09:07:48 +0000886 // Collect information recursively from any base classes.
887 for (CXXRecordDecl::base_class_iterator
888 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
889 const RecordType *RT = I->getType()->getAs<RecordType>();
890 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000891
John McCallb05b5f32010-03-15 09:07:48 +0000892 AccessSpecifier BaseAccess
893 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
894 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000895
John McCallb05b5f32010-03-15 09:07:48 +0000896 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
897 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
898 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000899 }
John McCallb05b5f32010-03-15 09:07:48 +0000900}
Sebastian Redl9994a342009-10-25 17:03:50 +0000901
John McCallb05b5f32010-03-15 09:07:48 +0000902/// Collect the visible conversions of a class.
903///
904/// This would be extremely straightforward if it weren't for virtual
905/// bases. It might be worth special-casing that, really.
906static void CollectVisibleConversions(ASTContext &Context,
907 CXXRecordDecl *Record,
908 UnresolvedSetImpl &Output) {
909 // The collection of all conversions in virtual bases that we've
910 // found. These will be added to the output as long as they don't
911 // appear in the hidden-conversions set.
912 UnresolvedSet<8> VBaseCs;
913
914 // The set of conversions in virtual bases that we've determined to
915 // be hidden.
916 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
917
918 // The set of types hidden by classes derived from this one.
919 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
920
921 // Go ahead and collect the direct conversions and add them to the
922 // hidden-types set.
923 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
924 Output.append(Cs.begin(), Cs.end());
925 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
926 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
927
928 // Recursively collect conversions from base classes.
929 for (CXXRecordDecl::base_class_iterator
930 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
931 const RecordType *RT = I->getType()->getAs<RecordType>();
932 if (!RT) continue;
933
934 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
935 I->isVirtual(), I->getAccessSpecifier(),
936 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
937 }
938
939 // Add any unhidden conversions provided by virtual bases.
940 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
941 I != E; ++I) {
942 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
943 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000944 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000945}
946
947/// getVisibleConversionFunctions - get all conversion functions visible
948/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000949const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000950 // If root class, all conversions are visible.
951 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000952 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000953 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000954 if (data().ComputedVisibleConversions)
955 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000956 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000957 data().ComputedVisibleConversions = true;
958 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000959}
960
John McCall32daa422010-03-31 01:36:47 +0000961void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
962 // This operation is O(N) but extremely rare. Sema only uses it to
963 // remove UsingShadowDecls in a class that were followed by a direct
964 // declaration, e.g.:
965 // class A : B {
966 // using B::operator int;
967 // operator int();
968 // };
969 // This is uncommon by itself and even more uncommon in conjunction
970 // with sufficiently large numbers of directly-declared conversions
971 // that asymptotic behavior matters.
972
973 UnresolvedSetImpl &Convs = *getConversionFunctions();
974 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
975 if (Convs[I].getDecl() == ConvDecl) {
976 Convs.erase(I);
977 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
978 && "conversion was found multiple times in unresolved set");
979 return;
980 }
981 }
982
983 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000984}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000985
Douglas Gregorf6b11852009-10-08 15:14:33 +0000986CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000987 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000988 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
989
990 return 0;
991}
992
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000993MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
994 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
995}
996
Douglas Gregorf6b11852009-10-08 15:14:33 +0000997void
998CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
999 TemplateSpecializationKind TSK) {
1000 assert(TemplateOrInstantiation.isNull() &&
1001 "Previous template or instantiation?");
1002 assert(!isa<ClassTemplateSpecializationDecl>(this));
1003 TemplateOrInstantiation
1004 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1005}
1006
Anders Carlssonb13e3572009-12-07 06:33:48 +00001007TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1008 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001009 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1010 return Spec->getSpecializationKind();
1011
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001012 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001013 return MSInfo->getTemplateSpecializationKind();
1014
1015 return TSK_Undeclared;
1016}
1017
1018void
1019CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1020 if (ClassTemplateSpecializationDecl *Spec
1021 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1022 Spec->setSpecializationKind(TSK);
1023 return;
1024 }
1025
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001026 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001027 MSInfo->setTemplateSpecializationKind(TSK);
1028 return;
1029 }
1030
1031 assert(false && "Not a class template or member class specialization");
1032}
1033
Douglas Gregor1d110e02010-07-01 14:13:13 +00001034CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1035 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001036 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001037
1038 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001039 = Context.DeclarationNames.getCXXDestructorName(
1040 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001041
John McCallc0bf4622010-02-23 00:48:20 +00001042 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001043 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001044 if (I == E)
1045 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001047 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001048 return Dtor;
1049}
1050
Douglas Gregorda2142f2011-02-19 18:51:44 +00001051void CXXRecordDecl::completeDefinition() {
1052 completeDefinition(0);
1053}
1054
1055void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1056 RecordDecl::completeDefinition();
1057
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001058 // If the class may be abstract (but hasn't been marked as such), check for
1059 // any pure final overriders.
1060 if (mayBeAbstract()) {
1061 CXXFinalOverriderMap MyFinalOverriders;
1062 if (!FinalOverriders) {
1063 getFinalOverriders(MyFinalOverriders);
1064 FinalOverriders = &MyFinalOverriders;
1065 }
1066
1067 bool Done = false;
1068 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1069 MEnd = FinalOverriders->end();
1070 M != MEnd && !Done; ++M) {
1071 for (OverridingMethods::iterator SO = M->second.begin(),
1072 SOEnd = M->second.end();
1073 SO != SOEnd && !Done; ++SO) {
1074 assert(SO->second.size() > 0 &&
1075 "All virtual functions have overridding virtual functions");
1076
1077 // C++ [class.abstract]p4:
1078 // A class is abstract if it contains or inherits at least one
1079 // pure virtual function for which the final overrider is pure
1080 // virtual.
1081 if (SO->second.front().Method->isPure()) {
1082 data().Abstract = true;
1083 Done = true;
1084 break;
1085 }
1086 }
1087 }
1088 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001089
1090 // Set access bits correctly on the directly-declared conversions.
1091 for (UnresolvedSetIterator I = data().Conversions.begin(),
1092 E = data().Conversions.end();
1093 I != E; ++I)
1094 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001095}
1096
1097bool CXXRecordDecl::mayBeAbstract() const {
1098 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1099 isDependentContext())
1100 return false;
1101
1102 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1103 BEnd = bases_end();
1104 B != BEnd; ++B) {
1105 CXXRecordDecl *BaseDecl
1106 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1107 if (BaseDecl->isAbstract())
1108 return true;
1109 }
1110
1111 return false;
1112}
1113
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001114CXXMethodDecl *
1115CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001116 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001117 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001118 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001119 bool isStatic, StorageClass SCAsWritten, bool isInline,
1120 SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001121 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001122 isStatic, SCAsWritten, isInline, EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001123}
1124
Douglas Gregor90916562009-09-29 18:16:17 +00001125bool CXXMethodDecl::isUsualDeallocationFunction() const {
1126 if (getOverloadedOperator() != OO_Delete &&
1127 getOverloadedOperator() != OO_Array_Delete)
1128 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001129
1130 // C++ [basic.stc.dynamic.deallocation]p2:
1131 // A template instance is never a usual deallocation function,
1132 // regardless of its signature.
1133 if (getPrimaryTemplate())
1134 return false;
1135
Douglas Gregor90916562009-09-29 18:16:17 +00001136 // C++ [basic.stc.dynamic.deallocation]p2:
1137 // If a class T has a member deallocation function named operator delete
1138 // with exactly one parameter, then that function is a usual (non-placement)
1139 // deallocation function. [...]
1140 if (getNumParams() == 1)
1141 return true;
1142
1143 // C++ [basic.stc.dynamic.deallocation]p2:
1144 // [...] If class T does not declare such an operator delete but does
1145 // declare a member deallocation function named operator delete with
1146 // exactly two parameters, the second of which has type std::size_t (18.1),
1147 // then this function is a usual deallocation function.
1148 ASTContext &Context = getASTContext();
1149 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001150 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1151 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001152 return false;
1153
1154 // This function is a usual deallocation function if there are no
1155 // single-parameter deallocation functions of the same kind.
1156 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1157 R.first != R.second; ++R.first) {
1158 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1159 if (FD->getNumParams() == 1)
1160 return false;
1161 }
1162
1163 return true;
1164}
1165
Douglas Gregor06a9f362010-05-01 20:49:11 +00001166bool CXXMethodDecl::isCopyAssignmentOperator() const {
1167 // C++0x [class.copy]p19:
1168 // A user-declared copy assignment operator X::operator= is a non-static
1169 // non-template member function of class X with exactly one parameter of
1170 // type X, X&, const X&, volatile X& or const volatile X&.
1171 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1172 /*non-static*/ isStatic() ||
1173 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1174 /*exactly one parameter*/getNumParams() != 1)
1175 return false;
1176
1177 QualType ParamType = getParamDecl(0)->getType();
1178 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1179 ParamType = Ref->getPointeeType();
1180
1181 ASTContext &Context = getASTContext();
1182 QualType ClassType
1183 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1184 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1185}
1186
Anders Carlsson05eb2442009-05-16 23:58:37 +00001187void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001188 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001189 assert(!MD->getParent()->isDependentContext() &&
1190 "Can't add an overridden method to a class template!");
1191
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001192 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001193}
1194
1195CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001196 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001197}
1198
1199CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001200 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001201}
1202
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001203unsigned CXXMethodDecl::size_overridden_methods() const {
1204 return getASTContext().overridden_methods_size(this);
1205}
1206
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001207QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001208 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1209 // If the member function is declared const, the type of this is const X*,
1210 // if the member function is declared volatile, the type of this is
1211 // volatile X*, and if the member function is declared const volatile,
1212 // the type of this is const volatile X*.
1213
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001214 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001215
John McCall3cb0ebd2010-03-10 03:28:59 +00001216 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001217 ClassTy = C.getQualifiedType(ClassTy,
1218 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001219 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001220}
1221
Eli Friedmand7d7f672009-12-06 20:50:05 +00001222bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001223 // If this function is a template instantiation, look at the template from
1224 // which it was instantiated.
1225 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1226 if (!CheckFn)
1227 CheckFn = this;
1228
Eli Friedmand7d7f672009-12-06 20:50:05 +00001229 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001230 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001231}
1232
Sean Huntcbb67482011-01-08 20:30:50 +00001233CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1234 TypeSourceInfo *TInfo, bool IsVirtual,
1235 SourceLocation L, Expr *Init,
1236 SourceLocation R,
1237 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001238 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001239 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1240 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001241{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001242}
1243
Sean Huntcbb67482011-01-08 20:30:50 +00001244CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1245 FieldDecl *Member,
1246 SourceLocation MemberLoc,
1247 SourceLocation L, Expr *Init,
1248 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001249 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001250 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1251 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1252{
1253}
1254
Sean Huntcbb67482011-01-08 20:30:50 +00001255CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1256 IndirectFieldDecl *Member,
1257 SourceLocation MemberLoc,
1258 SourceLocation L, Expr *Init,
1259 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001260 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001261 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001262 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001263{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001264}
1265
Sean Huntcbb67482011-01-08 20:30:50 +00001266CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Hunt41717662011-02-26 19:13:13 +00001267 SourceLocation D, SourceLocation L,
1268 CXXConstructorDecl *Target, Expr *Init,
1269 SourceLocation R)
1270 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1271 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1272 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1273{
1274}
1275
1276CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001277 FieldDecl *Member,
1278 SourceLocation MemberLoc,
1279 SourceLocation L, Expr *Init,
1280 SourceLocation R,
1281 VarDecl **Indices,
1282 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001283 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001284 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001285 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001286{
1287 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1288 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1289}
1290
Sean Huntcbb67482011-01-08 20:30:50 +00001291CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1292 FieldDecl *Member,
1293 SourceLocation MemberLoc,
1294 SourceLocation L, Expr *Init,
1295 SourceLocation R,
1296 VarDecl **Indices,
1297 unsigned NumIndices) {
1298 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001299 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001300 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001301 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1302 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001303}
1304
Sean Huntcbb67482011-01-08 20:30:50 +00001305TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001306 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001307 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001308 else
1309 return TypeLoc();
1310}
1311
Sean Huntcbb67482011-01-08 20:30:50 +00001312const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001313 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001314 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001315 else
1316 return 0;
1317}
1318
Sean Huntcbb67482011-01-08 20:30:50 +00001319SourceLocation CXXCtorInitializer::getSourceLocation() const {
Sean Hunt41717662011-02-26 19:13:13 +00001320 if (isAnyMemberInitializer() || isDelegatingInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001321 return getMemberLocation();
1322
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001323 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001324}
1325
Sean Huntcbb67482011-01-08 20:30:50 +00001326SourceRange CXXCtorInitializer::getSourceRange() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001327 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001328}
1329
Douglas Gregorb48fe382008-10-31 09:07:45 +00001330CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001331CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001332 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Sean Hunt5f802e52011-05-06 00:11:07 +00001333 QualType(), 0, false, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001334}
1335
1336CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001337CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001338 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001339 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001340 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001341 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001342 bool isInline,
Sean Hunt5f802e52011-05-06 00:11:07 +00001343 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001344 assert(NameInfo.getName().getNameKind()
1345 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001346 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001347 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Sean Hunt5f802e52011-05-06 00:11:07 +00001348 isExplicit, isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001349}
1350
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001351bool CXXConstructorDecl::isDefaultConstructor() const {
1352 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001353 // A default constructor for a class X is a constructor of class
1354 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001355 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001356 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001357}
1358
Mike Stump1eb44332009-09-09 15:08:12 +00001359bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001360CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001361 return isCopyOrMoveConstructor(TypeQuals) &&
1362 getParamDecl(0)->getType()->isLValueReferenceType();
1363}
1364
1365bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1366 return isCopyOrMoveConstructor(TypeQuals) &&
1367 getParamDecl(0)->getType()->isRValueReferenceType();
1368}
1369
1370/// \brief Determine whether this is a copy or move constructor.
1371bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001372 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001373 // A non-template constructor for class X is a copy constructor
1374 // if its first parameter is of type X&, const X&, volatile X& or
1375 // const volatile X&, and either there are no other parameters
1376 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001377 // C++0x [class.copy]p3:
1378 // A non-template constructor for class X is a move constructor if its
1379 // first parameter is of type X&&, const X&&, volatile X&&, or
1380 // const volatile X&&, and either there are no other parameters or else
1381 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001382 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001383 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001384 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001385 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001386 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001387
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001388 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001389
1390 // Do we have a reference type?
1391 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001392 if (!ParamRefType)
1393 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001394
Douglas Gregorfd476482009-11-13 23:59:09 +00001395 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001396 ASTContext &Context = getASTContext();
1397
Douglas Gregorfd476482009-11-13 23:59:09 +00001398 CanQualType PointeeType
1399 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001400 CanQualType ClassTy
1401 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001402 if (PointeeType.getUnqualifiedType() != ClassTy)
1403 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001404
John McCall0953e762009-09-24 19:53:00 +00001405 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001406
1407 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001408 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001409 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001410}
1411
Anders Carlssonfaccd722009-08-28 16:57:08 +00001412bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001413 // C++ [class.conv.ctor]p1:
1414 // A constructor declared without the function-specifier explicit
1415 // that can be called with a single parameter specifies a
1416 // conversion from the type of its first parameter to the type of
1417 // its class. Such a constructor is called a converting
1418 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001419 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001420 return false;
1421
Mike Stump1eb44332009-09-09 15:08:12 +00001422 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001423 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001424 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001425 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001426}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001427
Douglas Gregor6493cc52010-11-08 17:16:59 +00001428bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001429 if ((getNumParams() < 1) ||
1430 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1431 (getPrimaryTemplate() == 0) ||
1432 (getDescribedFunctionTemplate() != 0))
1433 return false;
1434
1435 const ParmVarDecl *Param = getParamDecl(0);
1436
1437 ASTContext &Context = getASTContext();
1438 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1439
Douglas Gregor66724ea2009-11-14 01:20:54 +00001440 // Is it the same as our our class type?
1441 CanQualType ClassTy
1442 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1443 if (ParamType.getUnqualifiedType() != ClassTy)
1444 return false;
1445
1446 return true;
1447}
1448
Sebastian Redlf677ea32011-02-05 19:23:19 +00001449const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1450 // Hack: we store the inherited constructor in the overridden method table
1451 method_iterator It = begin_overridden_methods();
1452 if (It == end_overridden_methods())
1453 return 0;
1454
1455 return cast<CXXConstructorDecl>(*It);
1456}
1457
1458void
1459CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1460 // Hack: we store the inherited constructor in the overridden method table
1461 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1462 addOverriddenMethod(BaseCtor);
1463}
1464
Douglas Gregor42a552f2008-11-05 20:51:48 +00001465CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001466CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001467 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001468 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001469}
1470
1471CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001472CXXDestructorDecl::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,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001475 QualType T, TypeSourceInfo *TInfo,
1476 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001477 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001478 assert(NameInfo.getName().getNameKind()
1479 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001480 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001481 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001482 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001483}
1484
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001485CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001486CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001487 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001488 QualType(), 0, false, false,
1489 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001490}
1491
1492CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001493CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001494 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001495 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001496 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001497 bool isInline, bool isExplicit,
1498 SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001499 assert(NameInfo.getName().getNameKind()
1500 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001501 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001502 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001503 isInline, isExplicit, EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001504}
1505
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001506LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001507 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001508 SourceLocation ExternLoc,
1509 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001510 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001511 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001512 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001513}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001514
1515UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1516 SourceLocation L,
1517 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001518 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001519 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001520 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001521 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001522 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1523 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001524 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1525 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001526}
1527
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001528NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1529 if (NamespaceAliasDecl *NA =
1530 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1531 return NA->getNamespace();
1532 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1533}
1534
Mike Stump1eb44332009-09-09 15:08:12 +00001535NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001536 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001537 SourceLocation AliasLoc,
1538 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001539 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001540 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001541 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001542 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1543 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001544 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1545 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001546}
1547
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001548UsingDecl *UsingShadowDecl::getUsingDecl() const {
1549 const UsingShadowDecl *Shadow = this;
1550 while (const UsingShadowDecl *NextShadow =
1551 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1552 Shadow = NextShadow;
1553 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1554}
1555
1556void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1557 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1558 "declaration already in set");
1559 assert(S->getUsingDecl() == this);
1560
1561 if (FirstUsingShadow)
1562 S->UsingOrNextShadow = FirstUsingShadow;
1563 FirstUsingShadow = S;
1564}
1565
1566void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1567 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1568 "declaration not in set");
1569 assert(S->getUsingDecl() == this);
1570
1571 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1572
1573 if (FirstUsingShadow == S) {
1574 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1575 S->UsingOrNextShadow = this;
1576 return;
1577 }
1578
1579 UsingShadowDecl *Prev = FirstUsingShadow;
1580 while (Prev->UsingOrNextShadow != S)
1581 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1582 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1583 S->UsingOrNextShadow = this;
1584}
1585
Douglas Gregordc355712011-02-25 00:36:19 +00001586UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1587 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001588 const DeclarationNameInfo &NameInfo,
1589 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001590 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001591}
1592
John McCall7ba107a2009-11-18 02:36:19 +00001593UnresolvedUsingValueDecl *
1594UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1595 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001596 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001597 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001598 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001599 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001600}
1601
1602UnresolvedUsingTypenameDecl *
1603UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1604 SourceLocation UsingLoc,
1605 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001606 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001607 SourceLocation TargetNameLoc,
1608 DeclarationName TargetName) {
1609 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001610 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001611 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001612}
1613
Anders Carlssonfb311762009-03-14 00:25:26 +00001614StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001615 SourceLocation StaticAssertLoc,
1616 Expr *AssertExpr,
1617 StringLiteral *Message,
1618 SourceLocation RParenLoc) {
1619 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1620 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001621}
1622
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001623static const char *getAccessName(AccessSpecifier AS) {
1624 switch (AS) {
1625 default:
1626 case AS_none:
1627 assert("Invalid access specifier!");
1628 return 0;
1629 case AS_public:
1630 return "public";
1631 case AS_private:
1632 return "private";
1633 case AS_protected:
1634 return "protected";
1635 }
1636}
1637
1638const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1639 AccessSpecifier AS) {
1640 return DB << getAccessName(AS);
1641}