blob: f32c85629c5a4f0f78d1ed0b7f050015038d965e [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 Huntcdee3fe2011-05-11 22:34:38 +000041 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false),
Sean Hunt37b8c9e2011-05-09 21:45:35 +000042 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 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000464 // If this is the implicit copy constructor, note that we have now
465 // declared it.
466 else if (Constructor->isCopyConstructor())
467 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000468 return;
469 }
470
471 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000472 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000473 return;
474 }
475
476 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000477 // If this is the implicit copy constructor, note that we have now
478 // declared it.
479 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000480 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000481 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000482 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000483 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000484
485 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000486 }
487
488 // Handle (user-declared) constructors.
489 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
490 // Note that we have a user-declared constructor.
491 data().UserDeclaredConstructor = true;
492
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000493 // FIXME: Under C++0x, /only/ special member functions may be user-provided.
494 // This is probably a defect.
495 bool UserProvided = false;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000496
Sean Hunt023df372011-05-09 18:22:59 +0000497 // C++0x [class.ctor]p5:
498 // A default constructor is trivial if it is not user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000499 if (Constructor->isDefaultConstructor()) {
500 data().DeclaredDefaultConstructor = true;
501 if (Constructor->isUserProvided()) {
502 data().HasTrivialDefaultConstructor = false;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000503 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000504 UserProvided = true;
505 }
506 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000507
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000508 // Note when we have a user-declared copy or move constructor, which will
509 // suppress the implicit declaration of those constructors.
510 if (!FunTmpl) {
511 if (Constructor->isCopyConstructor()) {
512 data().UserDeclaredCopyConstructor = true;
513 data().DeclaredCopyConstructor = true;
514
515 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000516 // A copy/move constructor for class X is trivial if it is not
517 // user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000518 if (Constructor->isUserProvided()) {
Sean Hunt023df372011-05-09 18:22:59 +0000519 data().HasTrivialCopyConstructor = false;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000520 UserProvided = true;
521 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000522 } else if (Constructor->isMoveConstructor()) {
523 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000524 // A copy/move constructor for class X is trivial if it is not
525 // user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000526 if (Constructor->isUserProvided()) {
Sean Hunt023df372011-05-09 18:22:59 +0000527 data().HasTrivialMoveConstructor = false;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000528 UserProvided = true;
529 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000530 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000531 }
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000532 if (Constructor->isConstExpr() &&
533 !Constructor->isCopyOrMoveConstructor()) {
534 // Record if we see any constexpr constructors which are niether copy
535 // nor move constructors.
536 data().HasConstExprNonCopyMoveConstructor = true;
537 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000538
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000539 // C++ [dcl.init.aggr]p1:
540 // An aggregate is an array or a class with no user-declared
541 // constructors [...].
542 // C++0x [dcl.init.aggr]p1:
543 // An aggregate is an array or a class with no user-provided
544 // constructors [...].
545 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
546 data().Aggregate = false;
547
548 // C++ [class]p4:
549 // A POD-struct is an aggregate class [...]
550 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
551 // type is technically an aggregate in C++0x since it wouldn't be in 03.
552 data().PlainOldData = false;
553
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000554 return;
555 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000556
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000557 // Handle (user-declared) destructors.
558 if (isa<CXXDestructorDecl>(D)) {
559 data().DeclaredDestructor = true;
560 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000561
562 // C++ [class]p4:
563 // A POD-struct is an aggregate class that has [...] no user-defined
564 // destructor.
565 data().PlainOldData = false;
566
Douglas Gregor85606eb2010-09-28 20:50:54 +0000567 // C++ [class.dtor]p3:
568 // A destructor is trivial if it is an implicitly-declared destructor and
569 // [...].
570 //
571 // FIXME: C++0x: don't do this for "= default" destructors
572 data().HasTrivialDestructor = false;
573
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000574 return;
575 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000576
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000577 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000578 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
579 if (Method->getOverloadedOperator() == OO_Equal) {
580 // We're interested specifically in copy assignment operators.
581 const FunctionProtoType *FnType
582 = Method->getType()->getAs<FunctionProtoType>();
583 assert(FnType && "Overloaded operator has no proto function type.");
584 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
585
586 // Copy assignment operators must be non-templates.
587 if (Method->getPrimaryTemplate() || FunTmpl)
588 return;
589
590 ASTContext &Context = getASTContext();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000591 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
592 const_cast<CXXRecordDecl*>(this)));
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000593
594 bool isRValueRefArg = false;
595 QualType ArgType = FnType->getArgType(0);
596 if (const LValueReferenceType *Ref =
597 ArgType->getAs<LValueReferenceType>()) {
598 ArgType = Ref->getPointeeType();
599 } else if (const RValueReferenceType *Ref =
600 ArgType->getAs<RValueReferenceType>()) {
601 ArgType = Ref->getPointeeType();
602 isRValueRefArg = true;
603 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000604 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
605 return;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000606
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000607 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000608 // A POD-struct is an aggregate class that [...] has no user-defined
609 // copy assignment operator [...].
610 // FIXME: This should be probably determined dynamically in terms of
611 // other more precise attributes to correctly model how it is specified
612 // in C++0x. Setting it here happens to do the right thing.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000613 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000614
615 if (!isRValueRefArg) {
616 // This is a copy assignment operator.
617
618 // Suppress the implicit declaration of a copy constructor.
619 data().UserDeclaredCopyAssignment = true;
620 data().DeclaredCopyAssignment = true;
621
622 // C++0x [class.copy]p27:
623 // A copy/move assignment operator for class X is trivial if it is
624 // neither user-provided nor deleted [...]
625 // FIXME: C++0x: don't do this for "= default" copy operators.
626 data().HasTrivialCopyAssignment = false;
627 } else {
628 // This is a move assignment operator.
629
630 // C++0x [class.copy]p27:
631 // A copy/move assignment operator for class X is trivial if it is
632 // neither user-provided nor deleted [...]
633 // FIXME: C++0x: don't do this for "= default" copy operators.
634 data().HasTrivialMoveAssignment = false;
635 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000636 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000637
Douglas Gregore80622f2010-09-29 04:25:11 +0000638 // Keep the list of conversion functions up-to-date.
639 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
640 // We don't record specializations.
641 if (Conversion->getPrimaryTemplate())
642 return;
643
644 // FIXME: We intentionally don't use the decl's access here because it
645 // hasn't been set yet. That's really just a misdesign in Sema.
646
647 if (FunTmpl) {
648 if (FunTmpl->getPreviousDeclaration())
649 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
650 FunTmpl);
651 else
652 data().Conversions.addDecl(FunTmpl);
653 } else {
654 if (Conversion->getPreviousDeclaration())
655 data().Conversions.replace(Conversion->getPreviousDeclaration(),
656 Conversion);
657 else
658 data().Conversions.addDecl(Conversion);
659 }
660 }
661
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000662 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000663 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000664
665 // Handle non-static data members.
666 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
667 // C++ [dcl.init.aggr]p1:
668 // An aggregate is an array or a class (clause 9) with [...] no
669 // private or protected non-static data members (clause 11).
670 //
671 // A POD must be an aggregate.
672 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
673 data().Aggregate = false;
674 data().PlainOldData = false;
675 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000676
677 // C++0x [class]p7:
678 // A standard-layout class is a class that:
679 // [...]
680 // -- has the same access control for all non-static data members,
681 switch (D->getAccess()) {
682 case AS_private: data().HasPrivateFields = true; break;
683 case AS_protected: data().HasProtectedFields = true; break;
684 case AS_public: data().HasPublicFields = true; break;
685 case AS_none: assert(0 && "Invalid access specifier");
686 };
687 if ((data().HasPrivateFields + data().HasProtectedFields +
688 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000689 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000690
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000691 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000692 // A POD struct is a class that is both a trivial class and a
693 // standard-layout class, and has no non-static data members of type
694 // non-POD struct, non-POD union (or array of such types).
695 ASTContext &Context = getASTContext();
696 QualType T = Context.getBaseElementType(Field->getType());
697 if (!T->isPODType())
698 data().PlainOldData = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000699 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000700 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000701
Chandler Carrutha8225442011-04-30 09:17:45 +0000702 // C++0x [class]p7:
703 // A standard-layout class is a class that:
704 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000705 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000706 }
707
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000708 // Record if this field is the first non-literal field or base.
709 if (!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType())
710 data().HasNonLiteralTypeFieldsOrBases = true;
711
Douglas Gregor85606eb2010-09-28 20:50:54 +0000712 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
713 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
714 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000715 // C++0x [class.ctor]p5:
716 // A defulat constructor is trivial [...] if:
717 // -- for all the non-static data members of its class that are of
718 // class type (or array thereof), each such class has a trivial
719 // default constructor.
720 if (!FieldRec->hasTrivialDefaultConstructor())
721 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000722
723 // C++0x [class.copy]p13:
724 // A copy/move constructor for class X is trivial if [...]
725 // [...]
726 // -- for each non-static data member of X that is of class type (or
727 // an array thereof), the constructor selected to copy/move that
728 // member is trivial;
729 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000730 if (!FieldRec->hasTrivialCopyConstructor())
731 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000732 if (!FieldRec->hasTrivialMoveConstructor())
733 data().HasTrivialMoveConstructor = false;
734
735 // C++0x [class.copy]p27:
736 // A copy/move assignment operator for class X is trivial if [...]
737 // [...]
738 // -- for each non-static data member of X that is of class type (or
739 // an array thereof), the assignment operator selected to
740 // copy/move that member is trivial;
741 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000742 if (!FieldRec->hasTrivialCopyAssignment())
743 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000744 if (!FieldRec->hasTrivialMoveAssignment())
745 data().HasTrivialMoveAssignment = false;
746
Douglas Gregor85606eb2010-09-28 20:50:54 +0000747 if (!FieldRec->hasTrivialDestructor())
748 data().HasTrivialDestructor = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000749
750 // C++0x [class]p7:
751 // A standard-layout class is a class that:
752 // -- has no non-static data members of type non-standard-layout
753 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000754 if (!FieldRec->isStandardLayout())
755 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000756
757 // C++0x [class]p7:
758 // A standard-layout class is a class that:
759 // [...]
760 // -- has no base classes of the same type as the first non-static
761 // data member.
762 // We don't want to expend bits in the state of the record decl
763 // tracking whether this is the first non-static data member so we
764 // cheat a bit and use some of the existing state: the empty bit.
765 // Virtual bases and virtual methods make a class non-empty, but they
766 // also make it non-standard-layout so we needn't check here.
767 // A non-empty base class may leave the class standard-layout, but not
768 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000769 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000770 // data member must come through here with Empty still true, and Empty
771 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000772 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000773 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
774 BE = bases_end();
775 BI != BE; ++BI) {
776 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000777 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000778 break;
779 }
780 }
781 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000782 }
783 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000784
785 // C++0x [class]p7:
786 // A standard-layout class is a class that:
787 // [...]
788 // -- either has no non-static data members in the most derived
789 // class and at most one base class with non-static data members,
790 // or has no base classes with non-static data members, and
791 // At this point we know that we have a non-static data member, so the last
792 // clause holds.
793 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000794 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000795
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000796 // If this is not a zero-length bit-field, then the class is not empty.
797 if (data().Empty) {
798 if (!Field->getBitWidth())
799 data().Empty = false;
800 else if (!Field->getBitWidth()->isTypeDependent() &&
801 !Field->getBitWidth()->isValueDependent()) {
802 llvm::APSInt Bits;
803 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
804 if (!!Bits)
805 data().Empty = false;
806 }
807 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000808 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000809
810 // Handle using declarations of conversion functions.
811 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
812 if (Shadow->getDeclName().getNameKind()
813 == DeclarationName::CXXConversionFunctionName)
814 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000815}
816
John McCallb05b5f32010-03-15 09:07:48 +0000817static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
818 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000819 if (isa<UsingShadowDecl>(Conv))
820 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000821 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
822 T = ConvTemp->getTemplatedDecl()->getResultType();
823 else
824 T = cast<CXXConversionDecl>(Conv)->getConversionType();
825 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000826}
827
John McCallb05b5f32010-03-15 09:07:48 +0000828/// Collect the visible conversions of a base class.
829///
830/// \param Base a base class of the class we're considering
831/// \param InVirtual whether this base class is a virtual base (or a base
832/// of a virtual base)
833/// \param Access the access along the inheritance path to this base
834/// \param ParentHiddenTypes the conversions provided by the inheritors
835/// of this base
836/// \param Output the set to which to add conversions from non-virtual bases
837/// \param VOutput the set to which to add conversions from virtual bases
838/// \param HiddenVBaseCs the set of conversions which were hidden in a
839/// virtual base along some inheritance path
840static void CollectVisibleConversions(ASTContext &Context,
841 CXXRecordDecl *Record,
842 bool InVirtual,
843 AccessSpecifier Access,
844 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
845 UnresolvedSetImpl &Output,
846 UnresolvedSetImpl &VOutput,
847 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
848 // The set of types which have conversions in this class or its
849 // subclasses. As an optimization, we don't copy the derived set
850 // unless it might change.
851 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
852 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
853
854 // Collect the direct conversions and figure out which conversions
855 // will be hidden in the subclasses.
856 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
857 if (!Cs.empty()) {
858 HiddenTypesBuffer = ParentHiddenTypes;
859 HiddenTypes = &HiddenTypesBuffer;
860
861 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
862 bool Hidden =
863 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
864
865 // If this conversion is hidden and we're in a virtual base,
866 // remember that it's hidden along some inheritance path.
867 if (Hidden && InVirtual)
868 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
869
870 // If this conversion isn't hidden, add it to the appropriate output.
871 else if (!Hidden) {
872 AccessSpecifier IAccess
873 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
874
875 if (InVirtual)
876 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000877 else
John McCallb05b5f32010-03-15 09:07:48 +0000878 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000879 }
880 }
881 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000882
John McCallb05b5f32010-03-15 09:07:48 +0000883 // Collect information recursively from any base classes.
884 for (CXXRecordDecl::base_class_iterator
885 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
886 const RecordType *RT = I->getType()->getAs<RecordType>();
887 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000888
John McCallb05b5f32010-03-15 09:07:48 +0000889 AccessSpecifier BaseAccess
890 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
891 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000892
John McCallb05b5f32010-03-15 09:07:48 +0000893 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
894 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
895 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000896 }
John McCallb05b5f32010-03-15 09:07:48 +0000897}
Sebastian Redl9994a342009-10-25 17:03:50 +0000898
John McCallb05b5f32010-03-15 09:07:48 +0000899/// Collect the visible conversions of a class.
900///
901/// This would be extremely straightforward if it weren't for virtual
902/// bases. It might be worth special-casing that, really.
903static void CollectVisibleConversions(ASTContext &Context,
904 CXXRecordDecl *Record,
905 UnresolvedSetImpl &Output) {
906 // The collection of all conversions in virtual bases that we've
907 // found. These will be added to the output as long as they don't
908 // appear in the hidden-conversions set.
909 UnresolvedSet<8> VBaseCs;
910
911 // The set of conversions in virtual bases that we've determined to
912 // be hidden.
913 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
914
915 // The set of types hidden by classes derived from this one.
916 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
917
918 // Go ahead and collect the direct conversions and add them to the
919 // hidden-types set.
920 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
921 Output.append(Cs.begin(), Cs.end());
922 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
923 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
924
925 // Recursively collect conversions from base classes.
926 for (CXXRecordDecl::base_class_iterator
927 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
928 const RecordType *RT = I->getType()->getAs<RecordType>();
929 if (!RT) continue;
930
931 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
932 I->isVirtual(), I->getAccessSpecifier(),
933 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
934 }
935
936 // Add any unhidden conversions provided by virtual bases.
937 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
938 I != E; ++I) {
939 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
940 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000941 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000942}
943
944/// getVisibleConversionFunctions - get all conversion functions visible
945/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000946const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000947 // If root class, all conversions are visible.
948 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000949 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000950 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000951 if (data().ComputedVisibleConversions)
952 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000953 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000954 data().ComputedVisibleConversions = true;
955 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000956}
957
John McCall32daa422010-03-31 01:36:47 +0000958void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
959 // This operation is O(N) but extremely rare. Sema only uses it to
960 // remove UsingShadowDecls in a class that were followed by a direct
961 // declaration, e.g.:
962 // class A : B {
963 // using B::operator int;
964 // operator int();
965 // };
966 // This is uncommon by itself and even more uncommon in conjunction
967 // with sufficiently large numbers of directly-declared conversions
968 // that asymptotic behavior matters.
969
970 UnresolvedSetImpl &Convs = *getConversionFunctions();
971 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
972 if (Convs[I].getDecl() == ConvDecl) {
973 Convs.erase(I);
974 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
975 && "conversion was found multiple times in unresolved set");
976 return;
977 }
978 }
979
980 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000981}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000982
Douglas Gregorf6b11852009-10-08 15:14:33 +0000983CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000984 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000985 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
986
987 return 0;
988}
989
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000990MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
991 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
992}
993
Douglas Gregorf6b11852009-10-08 15:14:33 +0000994void
995CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
996 TemplateSpecializationKind TSK) {
997 assert(TemplateOrInstantiation.isNull() &&
998 "Previous template or instantiation?");
999 assert(!isa<ClassTemplateSpecializationDecl>(this));
1000 TemplateOrInstantiation
1001 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1002}
1003
Anders Carlssonb13e3572009-12-07 06:33:48 +00001004TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1005 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001006 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1007 return Spec->getSpecializationKind();
1008
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001009 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001010 return MSInfo->getTemplateSpecializationKind();
1011
1012 return TSK_Undeclared;
1013}
1014
1015void
1016CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1017 if (ClassTemplateSpecializationDecl *Spec
1018 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1019 Spec->setSpecializationKind(TSK);
1020 return;
1021 }
1022
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001023 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001024 MSInfo->setTemplateSpecializationKind(TSK);
1025 return;
1026 }
1027
1028 assert(false && "Not a class template or member class specialization");
1029}
1030
Douglas Gregor1d110e02010-07-01 14:13:13 +00001031CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1032 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001033 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001034
1035 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001036 = Context.DeclarationNames.getCXXDestructorName(
1037 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001038
John McCallc0bf4622010-02-23 00:48:20 +00001039 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001040 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001041 if (I == E)
1042 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001044 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001045 return Dtor;
1046}
1047
Douglas Gregorda2142f2011-02-19 18:51:44 +00001048void CXXRecordDecl::completeDefinition() {
1049 completeDefinition(0);
1050}
1051
1052void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1053 RecordDecl::completeDefinition();
1054
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001055 // If the class may be abstract (but hasn't been marked as such), check for
1056 // any pure final overriders.
1057 if (mayBeAbstract()) {
1058 CXXFinalOverriderMap MyFinalOverriders;
1059 if (!FinalOverriders) {
1060 getFinalOverriders(MyFinalOverriders);
1061 FinalOverriders = &MyFinalOverriders;
1062 }
1063
1064 bool Done = false;
1065 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1066 MEnd = FinalOverriders->end();
1067 M != MEnd && !Done; ++M) {
1068 for (OverridingMethods::iterator SO = M->second.begin(),
1069 SOEnd = M->second.end();
1070 SO != SOEnd && !Done; ++SO) {
1071 assert(SO->second.size() > 0 &&
1072 "All virtual functions have overridding virtual functions");
1073
1074 // C++ [class.abstract]p4:
1075 // A class is abstract if it contains or inherits at least one
1076 // pure virtual function for which the final overrider is pure
1077 // virtual.
1078 if (SO->second.front().Method->isPure()) {
1079 data().Abstract = true;
1080 Done = true;
1081 break;
1082 }
1083 }
1084 }
1085 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001086
1087 // Set access bits correctly on the directly-declared conversions.
1088 for (UnresolvedSetIterator I = data().Conversions.begin(),
1089 E = data().Conversions.end();
1090 I != E; ++I)
1091 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001092}
1093
1094bool CXXRecordDecl::mayBeAbstract() const {
1095 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1096 isDependentContext())
1097 return false;
1098
1099 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1100 BEnd = bases_end();
1101 B != BEnd; ++B) {
1102 CXXRecordDecl *BaseDecl
1103 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1104 if (BaseDecl->isAbstract())
1105 return true;
1106 }
1107
1108 return false;
1109}
1110
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001111CXXMethodDecl *
1112CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001113 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001114 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001115 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001116 bool isStatic, StorageClass SCAsWritten, bool isInline,
1117 SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001118 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001119 isStatic, SCAsWritten, isInline, EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001120}
1121
Douglas Gregor90916562009-09-29 18:16:17 +00001122bool CXXMethodDecl::isUsualDeallocationFunction() const {
1123 if (getOverloadedOperator() != OO_Delete &&
1124 getOverloadedOperator() != OO_Array_Delete)
1125 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001126
1127 // C++ [basic.stc.dynamic.deallocation]p2:
1128 // A template instance is never a usual deallocation function,
1129 // regardless of its signature.
1130 if (getPrimaryTemplate())
1131 return false;
1132
Douglas Gregor90916562009-09-29 18:16:17 +00001133 // C++ [basic.stc.dynamic.deallocation]p2:
1134 // If a class T has a member deallocation function named operator delete
1135 // with exactly one parameter, then that function is a usual (non-placement)
1136 // deallocation function. [...]
1137 if (getNumParams() == 1)
1138 return true;
1139
1140 // C++ [basic.stc.dynamic.deallocation]p2:
1141 // [...] If class T does not declare such an operator delete but does
1142 // declare a member deallocation function named operator delete with
1143 // exactly two parameters, the second of which has type std::size_t (18.1),
1144 // then this function is a usual deallocation function.
1145 ASTContext &Context = getASTContext();
1146 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001147 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1148 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001149 return false;
1150
1151 // This function is a usual deallocation function if there are no
1152 // single-parameter deallocation functions of the same kind.
1153 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1154 R.first != R.second; ++R.first) {
1155 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1156 if (FD->getNumParams() == 1)
1157 return false;
1158 }
1159
1160 return true;
1161}
1162
Douglas Gregor06a9f362010-05-01 20:49:11 +00001163bool CXXMethodDecl::isCopyAssignmentOperator() const {
1164 // C++0x [class.copy]p19:
1165 // A user-declared copy assignment operator X::operator= is a non-static
1166 // non-template member function of class X with exactly one parameter of
1167 // type X, X&, const X&, volatile X& or const volatile X&.
1168 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1169 /*non-static*/ isStatic() ||
1170 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1171 /*exactly one parameter*/getNumParams() != 1)
1172 return false;
1173
1174 QualType ParamType = getParamDecl(0)->getType();
1175 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1176 ParamType = Ref->getPointeeType();
1177
1178 ASTContext &Context = getASTContext();
1179 QualType ClassType
1180 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1181 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1182}
1183
Anders Carlsson05eb2442009-05-16 23:58:37 +00001184void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001185 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001186 assert(!MD->getParent()->isDependentContext() &&
1187 "Can't add an overridden method to a class template!");
1188
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001189 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001190}
1191
1192CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001193 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001194}
1195
1196CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001197 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001198}
1199
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001200unsigned CXXMethodDecl::size_overridden_methods() const {
1201 return getASTContext().overridden_methods_size(this);
1202}
1203
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001204QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001205 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1206 // If the member function is declared const, the type of this is const X*,
1207 // if the member function is declared volatile, the type of this is
1208 // volatile X*, and if the member function is declared const volatile,
1209 // the type of this is const volatile X*.
1210
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001211 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001212
John McCall3cb0ebd2010-03-10 03:28:59 +00001213 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001214 ClassTy = C.getQualifiedType(ClassTy,
1215 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001216 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001217}
1218
Eli Friedmand7d7f672009-12-06 20:50:05 +00001219bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001220 // If this function is a template instantiation, look at the template from
1221 // which it was instantiated.
1222 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1223 if (!CheckFn)
1224 CheckFn = this;
1225
Eli Friedmand7d7f672009-12-06 20:50:05 +00001226 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001227 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001228}
1229
Sean Huntcbb67482011-01-08 20:30:50 +00001230CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1231 TypeSourceInfo *TInfo, bool IsVirtual,
1232 SourceLocation L, Expr *Init,
1233 SourceLocation R,
1234 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001235 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001236 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1237 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001238{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001239}
1240
Sean Huntcbb67482011-01-08 20:30:50 +00001241CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1242 FieldDecl *Member,
1243 SourceLocation MemberLoc,
1244 SourceLocation L, Expr *Init,
1245 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001246 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001247 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1248 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1249{
1250}
1251
Sean Huntcbb67482011-01-08 20:30:50 +00001252CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1253 IndirectFieldDecl *Member,
1254 SourceLocation MemberLoc,
1255 SourceLocation L, Expr *Init,
1256 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001257 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001258 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001259 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001260{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001261}
1262
Sean Huntcbb67482011-01-08 20:30:50 +00001263CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Hunt41717662011-02-26 19:13:13 +00001264 SourceLocation D, SourceLocation L,
1265 CXXConstructorDecl *Target, Expr *Init,
1266 SourceLocation R)
1267 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1268 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1269 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1270{
1271}
1272
1273CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001274 FieldDecl *Member,
1275 SourceLocation MemberLoc,
1276 SourceLocation L, Expr *Init,
1277 SourceLocation R,
1278 VarDecl **Indices,
1279 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001280 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001281 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001282 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001283{
1284 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1285 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1286}
1287
Sean Huntcbb67482011-01-08 20:30:50 +00001288CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1289 FieldDecl *Member,
1290 SourceLocation MemberLoc,
1291 SourceLocation L, Expr *Init,
1292 SourceLocation R,
1293 VarDecl **Indices,
1294 unsigned NumIndices) {
1295 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001296 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001297 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001298 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1299 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001300}
1301
Sean Huntcbb67482011-01-08 20:30:50 +00001302TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001303 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001304 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001305 else
1306 return TypeLoc();
1307}
1308
Sean Huntcbb67482011-01-08 20:30:50 +00001309const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001310 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001311 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001312 else
1313 return 0;
1314}
1315
Sean Huntcbb67482011-01-08 20:30:50 +00001316SourceLocation CXXCtorInitializer::getSourceLocation() const {
Sean Hunt41717662011-02-26 19:13:13 +00001317 if (isAnyMemberInitializer() || isDelegatingInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001318 return getMemberLocation();
1319
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001320 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001321}
1322
Sean Huntcbb67482011-01-08 20:30:50 +00001323SourceRange CXXCtorInitializer::getSourceRange() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001324 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001325}
1326
Douglas Gregorb48fe382008-10-31 09:07:45 +00001327CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001328CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001329 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Sean Hunt5f802e52011-05-06 00:11:07 +00001330 QualType(), 0, false, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001331}
1332
1333CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001334CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001335 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001336 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001337 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001338 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001339 bool isInline,
Sean Hunt5f802e52011-05-06 00:11:07 +00001340 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001341 assert(NameInfo.getName().getNameKind()
1342 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001343 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001344 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Sean Hunt5f802e52011-05-06 00:11:07 +00001345 isExplicit, isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001346}
1347
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001348bool CXXConstructorDecl::isDefaultConstructor() const {
1349 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001350 // A default constructor for a class X is a constructor of class
1351 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001352 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001353 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001354}
1355
Mike Stump1eb44332009-09-09 15:08:12 +00001356bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001357CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001358 return isCopyOrMoveConstructor(TypeQuals) &&
1359 getParamDecl(0)->getType()->isLValueReferenceType();
1360}
1361
1362bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1363 return isCopyOrMoveConstructor(TypeQuals) &&
1364 getParamDecl(0)->getType()->isRValueReferenceType();
1365}
1366
1367/// \brief Determine whether this is a copy or move constructor.
1368bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001369 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001370 // A non-template constructor for class X is a copy constructor
1371 // if its first parameter is of type X&, const X&, volatile X& or
1372 // const volatile X&, and either there are no other parameters
1373 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001374 // C++0x [class.copy]p3:
1375 // A non-template constructor for class X is a move constructor if its
1376 // first parameter is of type X&&, const X&&, volatile X&&, or
1377 // const volatile X&&, and either there are no other parameters or else
1378 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001379 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001380 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001381 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001382 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001383 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001384
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001385 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001386
1387 // Do we have a reference type?
1388 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001389 if (!ParamRefType)
1390 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001391
Douglas Gregorfd476482009-11-13 23:59:09 +00001392 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001393 ASTContext &Context = getASTContext();
1394
Douglas Gregorfd476482009-11-13 23:59:09 +00001395 CanQualType PointeeType
1396 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001397 CanQualType ClassTy
1398 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001399 if (PointeeType.getUnqualifiedType() != ClassTy)
1400 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001401
John McCall0953e762009-09-24 19:53:00 +00001402 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001403
1404 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001405 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001406 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001407}
1408
Anders Carlssonfaccd722009-08-28 16:57:08 +00001409bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001410 // C++ [class.conv.ctor]p1:
1411 // A constructor declared without the function-specifier explicit
1412 // that can be called with a single parameter specifies a
1413 // conversion from the type of its first parameter to the type of
1414 // its class. Such a constructor is called a converting
1415 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001416 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001417 return false;
1418
Mike Stump1eb44332009-09-09 15:08:12 +00001419 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001420 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001421 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001422 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001423}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001424
Douglas Gregor6493cc52010-11-08 17:16:59 +00001425bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001426 if ((getNumParams() < 1) ||
1427 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1428 (getPrimaryTemplate() == 0) ||
1429 (getDescribedFunctionTemplate() != 0))
1430 return false;
1431
1432 const ParmVarDecl *Param = getParamDecl(0);
1433
1434 ASTContext &Context = getASTContext();
1435 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1436
Douglas Gregor66724ea2009-11-14 01:20:54 +00001437 // Is it the same as our our class type?
1438 CanQualType ClassTy
1439 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1440 if (ParamType.getUnqualifiedType() != ClassTy)
1441 return false;
1442
1443 return true;
1444}
1445
Sebastian Redlf677ea32011-02-05 19:23:19 +00001446const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1447 // Hack: we store the inherited constructor in the overridden method table
1448 method_iterator It = begin_overridden_methods();
1449 if (It == end_overridden_methods())
1450 return 0;
1451
1452 return cast<CXXConstructorDecl>(*It);
1453}
1454
1455void
1456CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1457 // Hack: we store the inherited constructor in the overridden method table
1458 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1459 addOverriddenMethod(BaseCtor);
1460}
1461
Douglas Gregor42a552f2008-11-05 20:51:48 +00001462CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001463CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001464 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001465 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001466}
1467
1468CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001469CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001470 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001471 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001472 QualType T, TypeSourceInfo *TInfo,
1473 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001474 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001475 assert(NameInfo.getName().getNameKind()
1476 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001477 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001478 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001479 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001480}
1481
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001482CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001483CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001484 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001485 QualType(), 0, false, false,
1486 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001487}
1488
1489CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001490CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001491 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001492 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001493 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001494 bool isInline, bool isExplicit,
1495 SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001496 assert(NameInfo.getName().getNameKind()
1497 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001498 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001499 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001500 isInline, isExplicit, EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001501}
1502
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001503LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001504 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001505 SourceLocation ExternLoc,
1506 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001507 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001508 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001509 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001510}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001511
1512UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1513 SourceLocation L,
1514 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001515 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001516 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001517 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001518 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001519 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1520 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001521 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1522 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001523}
1524
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001525NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1526 if (NamespaceAliasDecl *NA =
1527 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1528 return NA->getNamespace();
1529 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1530}
1531
Mike Stump1eb44332009-09-09 15:08:12 +00001532NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001533 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001534 SourceLocation AliasLoc,
1535 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001536 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001537 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001538 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001539 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1540 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001541 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1542 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001543}
1544
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001545UsingDecl *UsingShadowDecl::getUsingDecl() const {
1546 const UsingShadowDecl *Shadow = this;
1547 while (const UsingShadowDecl *NextShadow =
1548 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1549 Shadow = NextShadow;
1550 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1551}
1552
1553void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1554 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1555 "declaration already in set");
1556 assert(S->getUsingDecl() == this);
1557
1558 if (FirstUsingShadow)
1559 S->UsingOrNextShadow = FirstUsingShadow;
1560 FirstUsingShadow = S;
1561}
1562
1563void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1564 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1565 "declaration not in set");
1566 assert(S->getUsingDecl() == this);
1567
1568 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1569
1570 if (FirstUsingShadow == S) {
1571 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1572 S->UsingOrNextShadow = this;
1573 return;
1574 }
1575
1576 UsingShadowDecl *Prev = FirstUsingShadow;
1577 while (Prev->UsingOrNextShadow != S)
1578 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1579 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1580 S->UsingOrNextShadow = this;
1581}
1582
Douglas Gregordc355712011-02-25 00:36:19 +00001583UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1584 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001585 const DeclarationNameInfo &NameInfo,
1586 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001587 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001588}
1589
John McCall7ba107a2009-11-18 02:36:19 +00001590UnresolvedUsingValueDecl *
1591UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1592 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001593 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001594 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001595 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001596 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001597}
1598
1599UnresolvedUsingTypenameDecl *
1600UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1601 SourceLocation UsingLoc,
1602 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001603 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001604 SourceLocation TargetNameLoc,
1605 DeclarationName TargetName) {
1606 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001607 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001608 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001609}
1610
Anders Carlssonfb311762009-03-14 00:25:26 +00001611StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001612 SourceLocation StaticAssertLoc,
1613 Expr *AssertExpr,
1614 StringLiteral *Message,
1615 SourceLocation RParenLoc) {
1616 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1617 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001618}
1619
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001620static const char *getAccessName(AccessSpecifier AS) {
1621 switch (AS) {
1622 default:
1623 case AS_none:
1624 assert("Invalid access specifier!");
1625 return 0;
1626 case AS_public:
1627 return "public";
1628 case AS_private:
1629 return "private";
1630 case AS_protected:
1631 return "protected";
1632 }
1633}
1634
1635const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1636 AccessSpecifier AS) {
1637 return DB << getAccessName(AS);
1638}