blob: 63fa9a77af0c63bb36b78aae700a58d441e04a9b [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),
Douglas Gregor2bb11012011-05-13 01:05:07 +000036 HasMutableFields(false), HasTrivialDefaultConstructor(true),
Sean Hunt023df372011-05-09 18:22:59 +000037 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;
Douglas Gregor2bb11012011-05-13 01:05:07 +0000228
229 // Keep track of the presence of mutable fields.
230 if (BaseClassDecl->hasMutableFields())
231 data().HasMutableFields = true;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000232 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000233
234 if (VBases.empty())
235 return;
236
237 // Create base specifier for any direct or indirect virtual bases.
238 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
239 data().NumVBases = VBases.size();
240 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000241 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
242
Anders Carlsson6f6de732010-03-29 05:13:12 +0000243 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000244 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000245 continue;
246
Nick Lewycky56062202010-07-26 16:56:01 +0000247 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
248 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000249
Douglas Gregor7c789c12010-10-29 22:39:52 +0000250 data().getVBases()[I] =
Anders Carlsson6f6de732010-03-29 05:13:12 +0000251 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000252 VBaseClassDecl->getTagKind() == TTK_Class,
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000253 VBases[I]->getAccessSpecifier(), VBaseTypeInfo,
254 SourceLocation());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000255 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000256}
257
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000258/// Callback function for CXXRecordDecl::forallBases that acknowledges
259/// that it saw a base class.
260static bool SawBase(const CXXRecordDecl *, void *) {
261 return true;
262}
263
264bool CXXRecordDecl::hasAnyDependentBases() const {
265 if (!isDependentContext())
266 return false;
267
268 return !forallBases(SawBase, 0);
269}
270
Jay Foad4ba2a172011-01-12 09:06:06 +0000271bool CXXRecordDecl::hasConstCopyConstructor(const ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000272 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000273}
274
Chandler Carruthb7e95892011-04-23 10:47:28 +0000275bool CXXRecordDecl::isTriviallyCopyable() const {
276 // C++0x [class]p5:
277 // A trivially copyable class is a class that:
278 // -- has no non-trivial copy constructors,
279 if (!hasTrivialCopyConstructor()) return false;
280 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000281 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000282 // -- has no non-trivial copy assignment operators,
283 if (!hasTrivialCopyAssignment()) return false;
284 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000285 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000286 // -- has a trivial destructor.
287 if (!hasTrivialDestructor()) return false;
288
289 return true;
290}
291
Douglas Gregor0d405db2010-07-01 20:59:04 +0000292/// \brief Perform a simplistic form of overload resolution that only considers
293/// cv-qualifiers on a single parameter, and return the best overload candidate
294/// (if there is one).
295static CXXMethodDecl *
296GetBestOverloadCandidateSimple(
297 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
298 if (Cands.empty())
299 return 0;
300 if (Cands.size() == 1)
301 return Cands[0].first;
302
303 unsigned Best = 0, N = Cands.size();
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 Best = I;
307
308 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000309 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000310 return 0;
311
312 return Cands[Best].first;
313}
314
Jay Foad4ba2a172011-01-12 09:06:06 +0000315CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(const ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000316 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000317 QualType ClassType
318 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000319 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000320 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000321 Context.getCanonicalType(ClassType));
322 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000323 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000324 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000325 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000326 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000327 // C++ [class.copy]p2:
328 // A non-template constructor for class X is a copy constructor if [...]
329 if (isa<FunctionTemplateDecl>(*Con))
330 continue;
331
Douglas Gregor0d405db2010-07-01 20:59:04 +0000332 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
333 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000334 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
335 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000336 Found.push_back(std::make_pair(
337 const_cast<CXXConstructorDecl *>(Constructor),
338 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000339 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000340 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000341
342 return cast_or_null<CXXConstructorDecl>(
343 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000344}
345
Douglas Gregorb87786f2010-07-01 17:48:08 +0000346CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
347 ASTContext &Context = getASTContext();
348 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
349 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
350
351 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
352 DeclContext::lookup_const_iterator Op, OpEnd;
353 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
354 // C++ [class.copy]p9:
355 // A user-declared copy assignment operator is a non-static non-template
356 // member function of class X with exactly one parameter of type X, X&,
357 // const X&, volatile X& or const volatile X&.
358 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
359 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
360 continue;
361
362 const FunctionProtoType *FnType
363 = Method->getType()->getAs<FunctionProtoType>();
364 assert(FnType && "Overloaded operator has no prototype.");
365 // Don't assert on this; an invalid decl might have been left in the AST.
366 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
367 continue;
368
369 QualType ArgType = FnType->getArgType(0);
370 Qualifiers Quals;
371 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
372 ArgType = Ref->getPointeeType();
373 // If we have a const argument and we have a reference to a non-const,
374 // this function does not match.
375 if (ArgIsConst && !ArgType.isConstQualified())
376 continue;
377
378 Quals = ArgType.getQualifiers();
379 } else {
380 // By-value copy-assignment operators are treated like const X&
381 // copy-assignment operators.
382 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
383 }
384
385 if (!Context.hasSameUnqualifiedType(ArgType, Class))
386 continue;
387
388 // Save this copy-assignment operator. It might be "the one".
389 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
390 }
391
392 // Use a simplistic form of overload resolution to find the candidate.
393 return GetBestOverloadCandidateSimple(Found);
394}
395
Douglas Gregor21386642010-09-28 21:55:22 +0000396void CXXRecordDecl::markedVirtualFunctionPure() {
397 // C++ [class.abstract]p2:
398 // A class is abstract if it has at least one pure virtual function.
399 data().Abstract = true;
400}
401
402void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000403 // Ignore friends and invalid declarations.
404 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000405 return;
406
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000407 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
408 if (FunTmpl)
409 D = FunTmpl->getTemplatedDecl();
410
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000411 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
412 if (Method->isVirtual()) {
413 // C++ [dcl.init.aggr]p1:
414 // An aggregate is an array or a class with [...] no virtual functions.
415 data().Aggregate = false;
416
417 // C++ [class]p4:
418 // A POD-struct is an aggregate class...
419 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000420
421 // Virtual functions make the class non-empty.
422 // FIXME: Standard ref?
423 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000424
425 // C++ [class.virtual]p1:
426 // A class that declares or inherits a virtual function is called a
427 // polymorphic class.
428 data().Polymorphic = true;
429
Sean Hunt023df372011-05-09 18:22:59 +0000430 // C++0x [class.ctor]p5
431 // A default constructor is trivial [...] if:
432 // -- its class has no virtual functions [...]
433 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000434
435 // C++0x [class.copy]p13:
436 // A copy/move constructor for class X is trivial if [...]
437 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000438 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000439 data().HasTrivialMoveConstructor = false;
440
441 // C++0x [class.copy]p27:
442 // A copy/move assignment operator for class X is trivial if [...]
443 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000444 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000445 data().HasTrivialMoveAssignment = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000446 // FIXME: Destructor?
Chandler Carrutha8225442011-04-30 09:17:45 +0000447
448 // C++0x [class]p7:
449 // A standard-layout class is a class that: [...]
450 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000451 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000452 }
453 }
454
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000455 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000456 // Notify that an implicit member was added after the definition
457 // was completed.
458 if (!isBeingDefined())
459 if (ASTMutationListener *L = getASTMutationListener())
460 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000461
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000462 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
463 // If this is the implicit default constructor, note that we have now
464 // declared it.
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000465 if (Constructor->isDefaultConstructor()) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000466 data().DeclaredDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000467 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000468 // If this is the implicit copy constructor, note that we have now
469 // declared it.
470 else if (Constructor->isCopyConstructor())
471 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000472 return;
473 }
474
475 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000476 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000477 return;
478 }
479
480 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000481 // If this is the implicit copy constructor, note that we have now
482 // declared it.
483 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000484 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000485 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000486 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000487 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000488
489 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000490 }
491
492 // Handle (user-declared) constructors.
493 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
494 // Note that we have a user-declared constructor.
495 data().UserDeclaredConstructor = true;
496
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;
Sean Huntcdee3fe2011-05-11 22:34:38 +0000507 data().UserProvidedDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000508 UserProvided = true;
509 }
510 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000511
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000512 // Note when we have a user-declared copy or move constructor, which will
513 // suppress the implicit declaration of those constructors.
514 if (!FunTmpl) {
515 if (Constructor->isCopyConstructor()) {
516 data().UserDeclaredCopyConstructor = true;
517 data().DeclaredCopyConstructor = true;
518
519 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000520 // A copy/move constructor for class X is trivial if it is not
521 // user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000522 if (Constructor->isUserProvided()) {
Sean Hunt023df372011-05-09 18:22:59 +0000523 data().HasTrivialCopyConstructor = false;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000524 UserProvided = true;
525 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000526 } else if (Constructor->isMoveConstructor()) {
527 // C++0x [class.copy]p13:
Sean Hunt023df372011-05-09 18:22:59 +0000528 // A copy/move constructor for class X is trivial if it is not
529 // user-provided [...]
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000530 if (Constructor->isUserProvided()) {
Sean Hunt023df372011-05-09 18:22:59 +0000531 data().HasTrivialMoveConstructor = false;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000532 UserProvided = true;
533 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000534 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000535 }
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000536 if (Constructor->isConstExpr() &&
537 !Constructor->isCopyOrMoveConstructor()) {
538 // Record if we see any constexpr constructors which are niether copy
539 // nor move constructors.
540 data().HasConstExprNonCopyMoveConstructor = true;
541 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000542
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000543 // C++ [dcl.init.aggr]p1:
544 // An aggregate is an array or a class with no user-declared
545 // constructors [...].
546 // C++0x [dcl.init.aggr]p1:
547 // An aggregate is an array or a class with no user-provided
548 // constructors [...].
549 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided)
550 data().Aggregate = false;
551
552 // C++ [class]p4:
553 // A POD-struct is an aggregate class [...]
554 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
555 // type is technically an aggregate in C++0x since it wouldn't be in 03.
556 data().PlainOldData = false;
557
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000558 return;
559 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000560
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000561 // Handle (user-declared) destructors.
562 if (isa<CXXDestructorDecl>(D)) {
563 data().DeclaredDestructor = true;
564 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000565
566 // C++ [class]p4:
567 // A POD-struct is an aggregate class that has [...] no user-defined
568 // destructor.
569 data().PlainOldData = false;
570
Douglas Gregor85606eb2010-09-28 20:50:54 +0000571 // C++ [class.dtor]p3:
572 // A destructor is trivial if it is an implicitly-declared destructor and
573 // [...].
574 //
575 // FIXME: C++0x: don't do this for "= default" destructors
576 data().HasTrivialDestructor = false;
577
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000578 return;
579 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000580
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000581 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000582 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
583 if (Method->getOverloadedOperator() == OO_Equal) {
584 // We're interested specifically in copy assignment operators.
585 const FunctionProtoType *FnType
586 = Method->getType()->getAs<FunctionProtoType>();
587 assert(FnType && "Overloaded operator has no proto function type.");
588 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
589
590 // Copy assignment operators must be non-templates.
591 if (Method->getPrimaryTemplate() || FunTmpl)
592 return;
593
594 ASTContext &Context = getASTContext();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000595 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
596 const_cast<CXXRecordDecl*>(this)));
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000597
598 bool isRValueRefArg = false;
599 QualType ArgType = FnType->getArgType(0);
600 if (const LValueReferenceType *Ref =
601 ArgType->getAs<LValueReferenceType>()) {
602 ArgType = Ref->getPointeeType();
603 } else if (const RValueReferenceType *Ref =
604 ArgType->getAs<RValueReferenceType>()) {
605 ArgType = Ref->getPointeeType();
606 isRValueRefArg = true;
607 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000608 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
609 return;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000610
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000611 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000612 // A POD-struct is an aggregate class that [...] has no user-defined
613 // copy assignment operator [...].
614 // FIXME: This should be probably determined dynamically in terms of
615 // other more precise attributes to correctly model how it is specified
616 // in C++0x. Setting it here happens to do the right thing.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000617 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000618
619 if (!isRValueRefArg) {
620 // This is a copy assignment operator.
621
622 // Suppress the implicit declaration of a copy constructor.
623 data().UserDeclaredCopyAssignment = true;
624 data().DeclaredCopyAssignment = true;
625
626 // C++0x [class.copy]p27:
627 // A copy/move assignment operator for class X is trivial if it is
628 // neither user-provided nor deleted [...]
629 // FIXME: C++0x: don't do this for "= default" copy operators.
630 data().HasTrivialCopyAssignment = false;
631 } else {
632 // This is a move assignment operator.
633
634 // C++0x [class.copy]p27:
635 // A copy/move assignment operator for class X is trivial if it is
636 // neither user-provided nor deleted [...]
637 // FIXME: C++0x: don't do this for "= default" copy operators.
638 data().HasTrivialMoveAssignment = false;
639 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000640 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000641
Douglas Gregore80622f2010-09-29 04:25:11 +0000642 // Keep the list of conversion functions up-to-date.
643 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
644 // We don't record specializations.
645 if (Conversion->getPrimaryTemplate())
646 return;
647
648 // FIXME: We intentionally don't use the decl's access here because it
649 // hasn't been set yet. That's really just a misdesign in Sema.
650
651 if (FunTmpl) {
652 if (FunTmpl->getPreviousDeclaration())
653 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
654 FunTmpl);
655 else
656 data().Conversions.addDecl(FunTmpl);
657 } else {
658 if (Conversion->getPreviousDeclaration())
659 data().Conversions.replace(Conversion->getPreviousDeclaration(),
660 Conversion);
661 else
662 data().Conversions.addDecl(Conversion);
663 }
664 }
665
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000666 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000667 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000668
669 // Handle non-static data members.
670 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
671 // C++ [dcl.init.aggr]p1:
672 // An aggregate is an array or a class (clause 9) with [...] no
673 // private or protected non-static data members (clause 11).
674 //
675 // A POD must be an aggregate.
676 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
677 data().Aggregate = false;
678 data().PlainOldData = false;
679 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000680
681 // C++0x [class]p7:
682 // A standard-layout class is a class that:
683 // [...]
684 // -- has the same access control for all non-static data members,
685 switch (D->getAccess()) {
686 case AS_private: data().HasPrivateFields = true; break;
687 case AS_protected: data().HasProtectedFields = true; break;
688 case AS_public: data().HasPublicFields = true; break;
689 case AS_none: assert(0 && "Invalid access specifier");
690 };
691 if ((data().HasPrivateFields + data().HasProtectedFields +
692 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000693 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000694
Douglas Gregor2bb11012011-05-13 01:05:07 +0000695 // Keep track of the presence of mutable fields.
696 if (Field->isMutable())
697 data().HasMutableFields = true;
698
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000699 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000700 // A POD struct is a class that is both a trivial class and a
701 // standard-layout class, and has no non-static data members of type
702 // non-POD struct, non-POD union (or array of such types).
703 ASTContext &Context = getASTContext();
704 QualType T = Context.getBaseElementType(Field->getType());
705 if (!T->isPODType())
706 data().PlainOldData = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000707 if (T->isReferenceType()) {
Sean Hunt023df372011-05-09 18:22:59 +0000708 data().HasTrivialDefaultConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000709
Chandler Carrutha8225442011-04-30 09:17:45 +0000710 // C++0x [class]p7:
711 // A standard-layout class is a class that:
712 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000713 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000714 }
715
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000716 // Record if this field is the first non-literal field or base.
717 if (!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType())
718 data().HasNonLiteralTypeFieldsOrBases = true;
719
Douglas Gregor85606eb2010-09-28 20:50:54 +0000720 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
721 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
722 if (FieldRec->getDefinition()) {
Sean Hunt023df372011-05-09 18:22:59 +0000723 // C++0x [class.ctor]p5:
724 // A defulat constructor is trivial [...] if:
725 // -- for all the non-static data members of its class that are of
726 // class type (or array thereof), each such class has a trivial
727 // default constructor.
728 if (!FieldRec->hasTrivialDefaultConstructor())
729 data().HasTrivialDefaultConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000730
731 // C++0x [class.copy]p13:
732 // A copy/move constructor for class X is trivial if [...]
733 // [...]
734 // -- for each non-static data member of X that is of class type (or
735 // an array thereof), the constructor selected to copy/move that
736 // member is trivial;
737 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000738 if (!FieldRec->hasTrivialCopyConstructor())
739 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000740 if (!FieldRec->hasTrivialMoveConstructor())
741 data().HasTrivialMoveConstructor = false;
742
743 // C++0x [class.copy]p27:
744 // A copy/move assignment operator for class X is trivial if [...]
745 // [...]
746 // -- for each non-static data member of X that is of class type (or
747 // an array thereof), the assignment operator selected to
748 // copy/move that member is trivial;
749 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000750 if (!FieldRec->hasTrivialCopyAssignment())
751 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000752 if (!FieldRec->hasTrivialMoveAssignment())
753 data().HasTrivialMoveAssignment = false;
754
Douglas Gregor85606eb2010-09-28 20:50:54 +0000755 if (!FieldRec->hasTrivialDestructor())
756 data().HasTrivialDestructor = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000757
758 // C++0x [class]p7:
759 // A standard-layout class is a class that:
760 // -- has no non-static data members of type non-standard-layout
761 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000762 if (!FieldRec->isStandardLayout())
763 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000764
765 // C++0x [class]p7:
766 // A standard-layout class is a class that:
767 // [...]
768 // -- has no base classes of the same type as the first non-static
769 // data member.
770 // We don't want to expend bits in the state of the record decl
771 // tracking whether this is the first non-static data member so we
772 // cheat a bit and use some of the existing state: the empty bit.
773 // Virtual bases and virtual methods make a class non-empty, but they
774 // also make it non-standard-layout so we needn't check here.
775 // A non-empty base class may leave the class standard-layout, but not
776 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000777 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000778 // data member must come through here with Empty still true, and Empty
779 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000780 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000781 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
782 BE = bases_end();
783 BI != BE; ++BI) {
784 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000785 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000786 break;
787 }
788 }
789 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000790
791 // Keep track of the presence of mutable fields.
792 if (FieldRec->hasMutableFields())
793 data().HasMutableFields = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000794 }
795 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000796
797 // C++0x [class]p7:
798 // A standard-layout class is a class that:
799 // [...]
800 // -- either has no non-static data members in the most derived
801 // class and at most one base class with non-static data members,
802 // or has no base classes with non-static data members, and
803 // At this point we know that we have a non-static data member, so the last
804 // clause holds.
805 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000806 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000807
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000808 // If this is not a zero-length bit-field, then the class is not empty.
809 if (data().Empty) {
810 if (!Field->getBitWidth())
811 data().Empty = false;
812 else if (!Field->getBitWidth()->isTypeDependent() &&
813 !Field->getBitWidth()->isValueDependent()) {
814 llvm::APSInt Bits;
815 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
816 if (!!Bits)
817 data().Empty = false;
818 }
819 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000820 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000821
822 // Handle using declarations of conversion functions.
823 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
824 if (Shadow->getDeclName().getNameKind()
825 == DeclarationName::CXXConversionFunctionName)
826 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000827}
828
John McCallb05b5f32010-03-15 09:07:48 +0000829static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
830 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000831 if (isa<UsingShadowDecl>(Conv))
832 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000833 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
834 T = ConvTemp->getTemplatedDecl()->getResultType();
835 else
836 T = cast<CXXConversionDecl>(Conv)->getConversionType();
837 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000838}
839
John McCallb05b5f32010-03-15 09:07:48 +0000840/// Collect the visible conversions of a base class.
841///
842/// \param Base a base class of the class we're considering
843/// \param InVirtual whether this base class is a virtual base (or a base
844/// of a virtual base)
845/// \param Access the access along the inheritance path to this base
846/// \param ParentHiddenTypes the conversions provided by the inheritors
847/// of this base
848/// \param Output the set to which to add conversions from non-virtual bases
849/// \param VOutput the set to which to add conversions from virtual bases
850/// \param HiddenVBaseCs the set of conversions which were hidden in a
851/// virtual base along some inheritance path
852static void CollectVisibleConversions(ASTContext &Context,
853 CXXRecordDecl *Record,
854 bool InVirtual,
855 AccessSpecifier Access,
856 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
857 UnresolvedSetImpl &Output,
858 UnresolvedSetImpl &VOutput,
859 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
860 // The set of types which have conversions in this class or its
861 // subclasses. As an optimization, we don't copy the derived set
862 // unless it might change.
863 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
864 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
865
866 // Collect the direct conversions and figure out which conversions
867 // will be hidden in the subclasses.
868 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
869 if (!Cs.empty()) {
870 HiddenTypesBuffer = ParentHiddenTypes;
871 HiddenTypes = &HiddenTypesBuffer;
872
873 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
874 bool Hidden =
875 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
876
877 // If this conversion is hidden and we're in a virtual base,
878 // remember that it's hidden along some inheritance path.
879 if (Hidden && InVirtual)
880 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
881
882 // If this conversion isn't hidden, add it to the appropriate output.
883 else if (!Hidden) {
884 AccessSpecifier IAccess
885 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
886
887 if (InVirtual)
888 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000889 else
John McCallb05b5f32010-03-15 09:07:48 +0000890 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000891 }
892 }
893 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000894
John McCallb05b5f32010-03-15 09:07:48 +0000895 // Collect information recursively from any base classes.
896 for (CXXRecordDecl::base_class_iterator
897 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
898 const RecordType *RT = I->getType()->getAs<RecordType>();
899 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000900
John McCallb05b5f32010-03-15 09:07:48 +0000901 AccessSpecifier BaseAccess
902 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
903 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000904
John McCallb05b5f32010-03-15 09:07:48 +0000905 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
906 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
907 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000908 }
John McCallb05b5f32010-03-15 09:07:48 +0000909}
Sebastian Redl9994a342009-10-25 17:03:50 +0000910
John McCallb05b5f32010-03-15 09:07:48 +0000911/// Collect the visible conversions of a class.
912///
913/// This would be extremely straightforward if it weren't for virtual
914/// bases. It might be worth special-casing that, really.
915static void CollectVisibleConversions(ASTContext &Context,
916 CXXRecordDecl *Record,
917 UnresolvedSetImpl &Output) {
918 // The collection of all conversions in virtual bases that we've
919 // found. These will be added to the output as long as they don't
920 // appear in the hidden-conversions set.
921 UnresolvedSet<8> VBaseCs;
922
923 // The set of conversions in virtual bases that we've determined to
924 // be hidden.
925 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
926
927 // The set of types hidden by classes derived from this one.
928 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
929
930 // Go ahead and collect the direct conversions and add them to the
931 // hidden-types set.
932 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
933 Output.append(Cs.begin(), Cs.end());
934 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
935 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
936
937 // Recursively collect conversions from base classes.
938 for (CXXRecordDecl::base_class_iterator
939 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
940 const RecordType *RT = I->getType()->getAs<RecordType>();
941 if (!RT) continue;
942
943 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
944 I->isVirtual(), I->getAccessSpecifier(),
945 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
946 }
947
948 // Add any unhidden conversions provided by virtual bases.
949 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
950 I != E; ++I) {
951 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
952 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000953 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000954}
955
956/// getVisibleConversionFunctions - get all conversion functions visible
957/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000958const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000959 // If root class, all conversions are visible.
960 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000961 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000962 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000963 if (data().ComputedVisibleConversions)
964 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000965 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000966 data().ComputedVisibleConversions = true;
967 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000968}
969
John McCall32daa422010-03-31 01:36:47 +0000970void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
971 // This operation is O(N) but extremely rare. Sema only uses it to
972 // remove UsingShadowDecls in a class that were followed by a direct
973 // declaration, e.g.:
974 // class A : B {
975 // using B::operator int;
976 // operator int();
977 // };
978 // This is uncommon by itself and even more uncommon in conjunction
979 // with sufficiently large numbers of directly-declared conversions
980 // that asymptotic behavior matters.
981
982 UnresolvedSetImpl &Convs = *getConversionFunctions();
983 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
984 if (Convs[I].getDecl() == ConvDecl) {
985 Convs.erase(I);
986 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
987 && "conversion was found multiple times in unresolved set");
988 return;
989 }
990 }
991
992 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000993}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000994
Douglas Gregorf6b11852009-10-08 15:14:33 +0000995CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000996 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000997 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
998
999 return 0;
1000}
1001
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001002MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1003 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1004}
1005
Douglas Gregorf6b11852009-10-08 15:14:33 +00001006void
1007CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1008 TemplateSpecializationKind TSK) {
1009 assert(TemplateOrInstantiation.isNull() &&
1010 "Previous template or instantiation?");
1011 assert(!isa<ClassTemplateSpecializationDecl>(this));
1012 TemplateOrInstantiation
1013 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1014}
1015
Anders Carlssonb13e3572009-12-07 06:33:48 +00001016TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1017 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001018 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1019 return Spec->getSpecializationKind();
1020
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001021 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001022 return MSInfo->getTemplateSpecializationKind();
1023
1024 return TSK_Undeclared;
1025}
1026
1027void
1028CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1029 if (ClassTemplateSpecializationDecl *Spec
1030 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1031 Spec->setSpecializationKind(TSK);
1032 return;
1033 }
1034
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001035 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001036 MSInfo->setTemplateSpecializationKind(TSK);
1037 return;
1038 }
1039
1040 assert(false && "Not a class template or member class specialization");
1041}
1042
Douglas Gregor1d110e02010-07-01 14:13:13 +00001043CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1044 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001045 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001046
1047 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001048 = Context.DeclarationNames.getCXXDestructorName(
1049 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001050
John McCallc0bf4622010-02-23 00:48:20 +00001051 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001052 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001053 if (I == E)
1054 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001056 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001057 return Dtor;
1058}
1059
Douglas Gregorda2142f2011-02-19 18:51:44 +00001060void CXXRecordDecl::completeDefinition() {
1061 completeDefinition(0);
1062}
1063
1064void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1065 RecordDecl::completeDefinition();
1066
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001067 // If the class may be abstract (but hasn't been marked as such), check for
1068 // any pure final overriders.
1069 if (mayBeAbstract()) {
1070 CXXFinalOverriderMap MyFinalOverriders;
1071 if (!FinalOverriders) {
1072 getFinalOverriders(MyFinalOverriders);
1073 FinalOverriders = &MyFinalOverriders;
1074 }
1075
1076 bool Done = false;
1077 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1078 MEnd = FinalOverriders->end();
1079 M != MEnd && !Done; ++M) {
1080 for (OverridingMethods::iterator SO = M->second.begin(),
1081 SOEnd = M->second.end();
1082 SO != SOEnd && !Done; ++SO) {
1083 assert(SO->second.size() > 0 &&
1084 "All virtual functions have overridding virtual functions");
1085
1086 // C++ [class.abstract]p4:
1087 // A class is abstract if it contains or inherits at least one
1088 // pure virtual function for which the final overrider is pure
1089 // virtual.
1090 if (SO->second.front().Method->isPure()) {
1091 data().Abstract = true;
1092 Done = true;
1093 break;
1094 }
1095 }
1096 }
1097 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001098
1099 // Set access bits correctly on the directly-declared conversions.
1100 for (UnresolvedSetIterator I = data().Conversions.begin(),
1101 E = data().Conversions.end();
1102 I != E; ++I)
1103 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001104}
1105
1106bool CXXRecordDecl::mayBeAbstract() const {
1107 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1108 isDependentContext())
1109 return false;
1110
1111 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1112 BEnd = bases_end();
1113 B != BEnd; ++B) {
1114 CXXRecordDecl *BaseDecl
1115 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1116 if (BaseDecl->isAbstract())
1117 return true;
1118 }
1119
1120 return false;
1121}
1122
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001123CXXMethodDecl *
1124CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001125 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001126 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001127 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001128 bool isStatic, StorageClass SCAsWritten, bool isInline,
1129 SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001130 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001131 isStatic, SCAsWritten, isInline, EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001132}
1133
Douglas Gregor90916562009-09-29 18:16:17 +00001134bool CXXMethodDecl::isUsualDeallocationFunction() const {
1135 if (getOverloadedOperator() != OO_Delete &&
1136 getOverloadedOperator() != OO_Array_Delete)
1137 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001138
1139 // C++ [basic.stc.dynamic.deallocation]p2:
1140 // A template instance is never a usual deallocation function,
1141 // regardless of its signature.
1142 if (getPrimaryTemplate())
1143 return false;
1144
Douglas Gregor90916562009-09-29 18:16:17 +00001145 // C++ [basic.stc.dynamic.deallocation]p2:
1146 // If a class T has a member deallocation function named operator delete
1147 // with exactly one parameter, then that function is a usual (non-placement)
1148 // deallocation function. [...]
1149 if (getNumParams() == 1)
1150 return true;
1151
1152 // C++ [basic.stc.dynamic.deallocation]p2:
1153 // [...] If class T does not declare such an operator delete but does
1154 // declare a member deallocation function named operator delete with
1155 // exactly two parameters, the second of which has type std::size_t (18.1),
1156 // then this function is a usual deallocation function.
1157 ASTContext &Context = getASTContext();
1158 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001159 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1160 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001161 return false;
1162
1163 // This function is a usual deallocation function if there are no
1164 // single-parameter deallocation functions of the same kind.
1165 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1166 R.first != R.second; ++R.first) {
1167 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1168 if (FD->getNumParams() == 1)
1169 return false;
1170 }
1171
1172 return true;
1173}
1174
Douglas Gregor06a9f362010-05-01 20:49:11 +00001175bool CXXMethodDecl::isCopyAssignmentOperator() const {
1176 // C++0x [class.copy]p19:
1177 // A user-declared copy assignment operator X::operator= is a non-static
1178 // non-template member function of class X with exactly one parameter of
1179 // type X, X&, const X&, volatile X& or const volatile X&.
1180 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1181 /*non-static*/ isStatic() ||
1182 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1183 /*exactly one parameter*/getNumParams() != 1)
1184 return false;
1185
1186 QualType ParamType = getParamDecl(0)->getType();
1187 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1188 ParamType = Ref->getPointeeType();
1189
1190 ASTContext &Context = getASTContext();
1191 QualType ClassType
1192 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1193 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1194}
1195
Anders Carlsson05eb2442009-05-16 23:58:37 +00001196void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001197 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001198 assert(!MD->getParent()->isDependentContext() &&
1199 "Can't add an overridden method to a class template!");
1200
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001201 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001202}
1203
1204CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001205 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001206}
1207
1208CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001209 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001210}
1211
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001212unsigned CXXMethodDecl::size_overridden_methods() const {
1213 return getASTContext().overridden_methods_size(this);
1214}
1215
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001216QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001217 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1218 // If the member function is declared const, the type of this is const X*,
1219 // if the member function is declared volatile, the type of this is
1220 // volatile X*, and if the member function is declared const volatile,
1221 // the type of this is const volatile X*.
1222
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001223 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001224
John McCall3cb0ebd2010-03-10 03:28:59 +00001225 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001226 ClassTy = C.getQualifiedType(ClassTy,
1227 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001228 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001229}
1230
Eli Friedmand7d7f672009-12-06 20:50:05 +00001231bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001232 // If this function is a template instantiation, look at the template from
1233 // which it was instantiated.
1234 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1235 if (!CheckFn)
1236 CheckFn = this;
1237
Eli Friedmand7d7f672009-12-06 20:50:05 +00001238 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001239 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001240}
1241
Sean Huntcbb67482011-01-08 20:30:50 +00001242CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1243 TypeSourceInfo *TInfo, bool IsVirtual,
1244 SourceLocation L, Expr *Init,
1245 SourceLocation R,
1246 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001247 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001248 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1249 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001250{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001251}
1252
Sean Huntcbb67482011-01-08 20:30:50 +00001253CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1254 FieldDecl *Member,
1255 SourceLocation MemberLoc,
1256 SourceLocation L, Expr *Init,
1257 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001258 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001259 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1260 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1261{
1262}
1263
Sean Huntcbb67482011-01-08 20:30:50 +00001264CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1265 IndirectFieldDecl *Member,
1266 SourceLocation MemberLoc,
1267 SourceLocation L, Expr *Init,
1268 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001269 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001270 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001271 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001272{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001273}
1274
Sean Huntcbb67482011-01-08 20:30:50 +00001275CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Hunt41717662011-02-26 19:13:13 +00001276 SourceLocation D, SourceLocation L,
1277 CXXConstructorDecl *Target, Expr *Init,
1278 SourceLocation R)
1279 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1280 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1281 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1282{
1283}
1284
1285CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001286 FieldDecl *Member,
1287 SourceLocation MemberLoc,
1288 SourceLocation L, Expr *Init,
1289 SourceLocation R,
1290 VarDecl **Indices,
1291 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001292 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001293 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001294 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001295{
1296 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1297 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1298}
1299
Sean Huntcbb67482011-01-08 20:30:50 +00001300CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1301 FieldDecl *Member,
1302 SourceLocation MemberLoc,
1303 SourceLocation L, Expr *Init,
1304 SourceLocation R,
1305 VarDecl **Indices,
1306 unsigned NumIndices) {
1307 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001308 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001309 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001310 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1311 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001312}
1313
Sean Huntcbb67482011-01-08 20:30:50 +00001314TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001315 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001316 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001317 else
1318 return TypeLoc();
1319}
1320
Sean Huntcbb67482011-01-08 20:30:50 +00001321const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001322 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001323 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001324 else
1325 return 0;
1326}
1327
Sean Huntcbb67482011-01-08 20:30:50 +00001328SourceLocation CXXCtorInitializer::getSourceLocation() const {
Sean Hunt41717662011-02-26 19:13:13 +00001329 if (isAnyMemberInitializer() || isDelegatingInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001330 return getMemberLocation();
1331
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001332 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001333}
1334
Sean Huntcbb67482011-01-08 20:30:50 +00001335SourceRange CXXCtorInitializer::getSourceRange() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001336 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001337}
1338
Douglas Gregorb48fe382008-10-31 09:07:45 +00001339CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001340CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001341 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Sean Hunt5f802e52011-05-06 00:11:07 +00001342 QualType(), 0, false, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001343}
1344
1345CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001346CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001347 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001348 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001349 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001350 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001351 bool isInline,
Sean Hunt5f802e52011-05-06 00:11:07 +00001352 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001353 assert(NameInfo.getName().getNameKind()
1354 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001355 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001356 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Sean Hunt5f802e52011-05-06 00:11:07 +00001357 isExplicit, isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001358}
1359
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001360bool CXXConstructorDecl::isDefaultConstructor() const {
1361 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001362 // A default constructor for a class X is a constructor of class
1363 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001364 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001365 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001366}
1367
Mike Stump1eb44332009-09-09 15:08:12 +00001368bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001369CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001370 return isCopyOrMoveConstructor(TypeQuals) &&
1371 getParamDecl(0)->getType()->isLValueReferenceType();
1372}
1373
1374bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1375 return isCopyOrMoveConstructor(TypeQuals) &&
1376 getParamDecl(0)->getType()->isRValueReferenceType();
1377}
1378
1379/// \brief Determine whether this is a copy or move constructor.
1380bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001381 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001382 // A non-template constructor for class X is a copy constructor
1383 // if its first parameter is of type X&, const X&, volatile X& or
1384 // const volatile X&, and either there are no other parameters
1385 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001386 // C++0x [class.copy]p3:
1387 // A non-template constructor for class X is a move constructor if its
1388 // first parameter is of type X&&, const X&&, volatile X&&, or
1389 // const volatile X&&, and either there are no other parameters or else
1390 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001391 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001392 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001393 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001394 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001395 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001396
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001397 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001398
1399 // Do we have a reference type?
1400 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001401 if (!ParamRefType)
1402 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001403
Douglas Gregorfd476482009-11-13 23:59:09 +00001404 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001405 ASTContext &Context = getASTContext();
1406
Douglas Gregorfd476482009-11-13 23:59:09 +00001407 CanQualType PointeeType
1408 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001409 CanQualType ClassTy
1410 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001411 if (PointeeType.getUnqualifiedType() != ClassTy)
1412 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001413
John McCall0953e762009-09-24 19:53:00 +00001414 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001415
1416 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001417 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001418 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001419}
1420
Anders Carlssonfaccd722009-08-28 16:57:08 +00001421bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001422 // C++ [class.conv.ctor]p1:
1423 // A constructor declared without the function-specifier explicit
1424 // that can be called with a single parameter specifies a
1425 // conversion from the type of its first parameter to the type of
1426 // its class. Such a constructor is called a converting
1427 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001428 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001429 return false;
1430
Mike Stump1eb44332009-09-09 15:08:12 +00001431 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001432 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001433 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001434 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001435}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001436
Douglas Gregor6493cc52010-11-08 17:16:59 +00001437bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001438 if ((getNumParams() < 1) ||
1439 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1440 (getPrimaryTemplate() == 0) ||
1441 (getDescribedFunctionTemplate() != 0))
1442 return false;
1443
1444 const ParmVarDecl *Param = getParamDecl(0);
1445
1446 ASTContext &Context = getASTContext();
1447 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1448
Douglas Gregor66724ea2009-11-14 01:20:54 +00001449 // Is it the same as our our class type?
1450 CanQualType ClassTy
1451 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1452 if (ParamType.getUnqualifiedType() != ClassTy)
1453 return false;
1454
1455 return true;
1456}
1457
Sebastian Redlf677ea32011-02-05 19:23:19 +00001458const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1459 // Hack: we store the inherited constructor in the overridden method table
1460 method_iterator It = begin_overridden_methods();
1461 if (It == end_overridden_methods())
1462 return 0;
1463
1464 return cast<CXXConstructorDecl>(*It);
1465}
1466
1467void
1468CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1469 // Hack: we store the inherited constructor in the overridden method table
1470 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1471 addOverriddenMethod(BaseCtor);
1472}
1473
Douglas Gregor42a552f2008-11-05 20:51:48 +00001474CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001475CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001476 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001477 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001478}
1479
1480CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001481CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001482 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001483 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001484 QualType T, TypeSourceInfo *TInfo,
1485 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001486 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001487 assert(NameInfo.getName().getNameKind()
1488 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001489 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001490 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001491 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001492}
1493
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001494CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001495CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001496 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001497 QualType(), 0, false, false,
1498 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001499}
1500
1501CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001502CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001503 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001504 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001505 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001506 bool isInline, bool isExplicit,
1507 SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001508 assert(NameInfo.getName().getNameKind()
1509 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001510 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001511 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001512 isInline, isExplicit, EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001513}
1514
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001515LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001516 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001517 SourceLocation ExternLoc,
1518 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001519 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001520 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001521 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001522}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001523
1524UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1525 SourceLocation L,
1526 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001527 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001528 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001529 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001530 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001531 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1532 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001533 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1534 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001535}
1536
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001537NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1538 if (NamespaceAliasDecl *NA =
1539 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1540 return NA->getNamespace();
1541 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1542}
1543
Mike Stump1eb44332009-09-09 15:08:12 +00001544NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001545 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001546 SourceLocation AliasLoc,
1547 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001548 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001549 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001550 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001551 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1552 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001553 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1554 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001555}
1556
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001557UsingDecl *UsingShadowDecl::getUsingDecl() const {
1558 const UsingShadowDecl *Shadow = this;
1559 while (const UsingShadowDecl *NextShadow =
1560 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1561 Shadow = NextShadow;
1562 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1563}
1564
1565void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1566 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1567 "declaration already in set");
1568 assert(S->getUsingDecl() == this);
1569
1570 if (FirstUsingShadow)
1571 S->UsingOrNextShadow = FirstUsingShadow;
1572 FirstUsingShadow = S;
1573}
1574
1575void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1576 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1577 "declaration not in set");
1578 assert(S->getUsingDecl() == this);
1579
1580 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1581
1582 if (FirstUsingShadow == S) {
1583 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1584 S->UsingOrNextShadow = this;
1585 return;
1586 }
1587
1588 UsingShadowDecl *Prev = FirstUsingShadow;
1589 while (Prev->UsingOrNextShadow != S)
1590 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1591 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1592 S->UsingOrNextShadow = this;
1593}
1594
Douglas Gregordc355712011-02-25 00:36:19 +00001595UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1596 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001597 const DeclarationNameInfo &NameInfo,
1598 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001599 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001600}
1601
John McCall7ba107a2009-11-18 02:36:19 +00001602UnresolvedUsingValueDecl *
1603UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1604 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001605 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001606 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001607 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001608 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001609}
1610
1611UnresolvedUsingTypenameDecl *
1612UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1613 SourceLocation UsingLoc,
1614 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001615 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001616 SourceLocation TargetNameLoc,
1617 DeclarationName TargetName) {
1618 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001619 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001620 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001621}
1622
Anders Carlssonfb311762009-03-14 00:25:26 +00001623StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001624 SourceLocation StaticAssertLoc,
1625 Expr *AssertExpr,
1626 StringLiteral *Message,
1627 SourceLocation RParenLoc) {
1628 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1629 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001630}
1631
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001632static const char *getAccessName(AccessSpecifier AS) {
1633 switch (AS) {
1634 default:
1635 case AS_none:
1636 assert("Invalid access specifier!");
1637 return 0;
1638 case AS_public:
1639 return "public";
1640 case AS_private:
1641 return "private";
1642 case AS_protected:
1643 return "protected";
1644 }
1645}
1646
1647const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1648 AccessSpecifier AS) {
1649 return DB << getAccessName(AS);
1650}