blob: 9099cd524f1a29e949e3938bae2e7219a03d7822 [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),
36 HasTrivialConstructor(true), HasConstExprNonCopyMoveConstructor(false),
Chandler Carruth4d6e5a22011-04-23 23:10:33 +000037 HasTrivialCopyConstructor(true), HasTrivialMoveConstructor(true),
38 HasTrivialCopyAssignment(true), HasTrivialMoveAssignment(true),
Chandler Carruth9b6347c2011-04-24 02:49:34 +000039 HasTrivialDestructor(true), HasNonLiteralTypeFieldsOrBases(false),
40 ComputedVisibleConversions(false),
Douglas Gregor18274032010-07-03 00:47:00 +000041 DeclaredDefaultConstructor(false), DeclaredCopyConstructor(false),
Douglas Gregora376d102010-07-02 21:50:04 +000042 DeclaredCopyAssignment(false), DeclaredDestructor(false),
Anders Carlssoncb88a1f2011-01-24 16:26:15 +000043 NumBases(0), NumVBases(0), Bases(), VBases(),
44 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000045}
46
47CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000048 SourceLocation StartLoc, SourceLocation IdLoc,
49 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
50 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000051 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000052 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000053
Jay Foad4ba2a172011-01-12 09:06:06 +000054CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000055 DeclContext *DC, SourceLocation StartLoc,
56 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000057 CXXRecordDecl* PrevDecl,
58 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000059 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
60 Id, PrevDecl);
Mike Stump1eb44332009-09-09 15:08:12 +000061
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000062 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000063 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000064 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000065 return R;
66}
67
Jay Foad4ba2a172011-01-12 09:06:06 +000068CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000069 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(),
70 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000071}
72
Mike Stump1eb44332009-09-09 15:08:12 +000073void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000074CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000075 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000076 ASTContext &C = getASTContext();
77
Mike Stump1eb44332009-09-09 15:08:12 +000078 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000079 // An aggregate is an array or a class (clause 9) with [...]
80 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000081 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000082
Douglas Gregor7c789c12010-10-29 22:39:52 +000083 if (!data().Bases.isOffset() && data().NumBases > 0)
84 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +000085
Anders Carlsson6f6de732010-03-29 05:13:12 +000086 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000087 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000088
89 // The virtual bases of this class.
90 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000091
John McCall86ff3082010-02-04 22:26:26 +000092 data().Bases = new(C) CXXBaseSpecifier [NumBases];
93 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000094 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +000095 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000096 // Keep track of inherited vbases for this base class.
97 const CXXBaseSpecifier *Base = Bases[i];
98 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +000099 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000100 if (BaseType->isDependentType())
101 continue;
102 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000103 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000104
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000105 // C++ [dcl.init.aggr]p1:
106 // An aggregate is [...] a class with [...] no base classes [...].
107 data().Aggregate = false;
108
109 // C++ [class]p4:
110 // A POD-struct is an aggregate class...
111 data().PlainOldData = false;
112
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000113 // A class with a non-empty base class is not empty.
114 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000115 if (!BaseClassDecl->isEmpty()) {
116 if (!data().Empty) {
117 // C++0x [class]p7:
118 // A standard-layout class is a class that:
119 // [...]
120 // -- either has no non-static data members in the most derived
121 // class and at most one base class with non-static data members,
122 // or has no base classes with non-static data members, and
123 // If this is the second non-empty base, then neither of these two
124 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000125 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000126 }
127
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000128 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000129 data().HasNoNonEmptyBases = false;
130 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000131
Douglas Gregor85606eb2010-09-28 20:50:54 +0000132 // C++ [class.virtual]p1:
133 // A class that declares or inherits a virtual function is called a
134 // polymorphic class.
135 if (BaseClassDecl->isPolymorphic())
136 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000137
Chandler Carrutha8225442011-04-30 09:17:45 +0000138 // C++0x [class]p7:
139 // A standard-layout class is a class that: [...]
140 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000141 if (!BaseClassDecl->isStandardLayout())
142 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000143
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000144 // Record if this base is the first non-literal field or base.
145 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType())
146 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000147
Anders Carlsson6f6de732010-03-29 05:13:12 +0000148 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000149 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000150 BaseClassDecl->vbases_begin(),
151 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000152 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000153 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000154 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000155 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000156
157 if (Base->isVirtual()) {
158 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000159 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000160 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000161
162 // C++0x [meta.unary.prop] is_empty:
163 // T is a class type, but not a union type, with ... no virtual base
164 // classes
165 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000166
167 // C++ [class.ctor]p5:
168 // A constructor is trivial if its class has no virtual base classes.
169 data().HasTrivialConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000170
171 // C++0x [class.copy]p13:
172 // A copy/move constructor for class X is trivial if it is neither
173 // user-provided nor deleted and if
174 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000175 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000176 data().HasTrivialMoveConstructor = false;
177
178 // C++0x [class.copy]p27:
179 // A copy/move assignment operator for class X is trivial if it is
180 // neither user-provided nor deleted and if
181 // -- class X has no virtual functions and no virtual base classes, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000182 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000183 data().HasTrivialMoveAssignment = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000184
185 // C++0x [class]p7:
186 // A standard-layout class is a class that: [...]
187 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000188 data().IsStandardLayout = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000189 } else {
190 // C++ [class.ctor]p5:
191 // A constructor is trivial if all the direct base classes of its
192 // class have trivial constructors.
193 if (!BaseClassDecl->hasTrivialConstructor())
194 data().HasTrivialConstructor = false;
195
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000196 // C++0x [class.copy]p13:
197 // A copy/move constructor for class X is trivial if [...]
198 // [...]
199 // -- the constructor selected to copy/move each direct base class
200 // subobject is trivial, and
201 // FIXME: C++0x: We need to only consider the selected constructor
202 // instead of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000203 if (!BaseClassDecl->hasTrivialCopyConstructor())
204 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000205 if (!BaseClassDecl->hasTrivialMoveConstructor())
206 data().HasTrivialMoveConstructor = false;
207
208 // C++0x [class.copy]p27:
209 // A copy/move assignment operator for class X is trivial if [...]
210 // [...]
211 // -- the assignment operator selected to copy/move each direct base
212 // class subobject is trivial, and
213 // FIXME: C++0x: We need to only consider the selected operator instead
214 // of all of them.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000215 if (!BaseClassDecl->hasTrivialCopyAssignment())
216 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000217 if (!BaseClassDecl->hasTrivialMoveAssignment())
218 data().HasTrivialMoveAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000219 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000220
221 // C++ [class.ctor]p3:
222 // A destructor is trivial if all the direct base classes of its class
223 // have trivial destructors.
224 if (!BaseClassDecl->hasTrivialDestructor())
225 data().HasTrivialDestructor = false;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000226 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000227
228 if (VBases.empty())
229 return;
230
231 // Create base specifier for any direct or indirect virtual bases.
232 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
233 data().NumVBases = VBases.size();
234 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000235 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
236
Anders Carlsson6f6de732010-03-29 05:13:12 +0000237 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000238 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000239 continue;
240
Nick Lewycky56062202010-07-26 16:56:01 +0000241 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
242 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000243
Douglas Gregor7c789c12010-10-29 22:39:52 +0000244 data().getVBases()[I] =
Anders Carlsson6f6de732010-03-29 05:13:12 +0000245 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000246 VBaseClassDecl->getTagKind() == TTK_Class,
Douglas Gregorf90b27a2011-01-03 22:36:02 +0000247 VBases[I]->getAccessSpecifier(), VBaseTypeInfo,
248 SourceLocation());
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000249 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000250}
251
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000252/// Callback function for CXXRecordDecl::forallBases that acknowledges
253/// that it saw a base class.
254static bool SawBase(const CXXRecordDecl *, void *) {
255 return true;
256}
257
258bool CXXRecordDecl::hasAnyDependentBases() const {
259 if (!isDependentContext())
260 return false;
261
262 return !forallBases(SawBase, 0);
263}
264
Jay Foad4ba2a172011-01-12 09:06:06 +0000265bool CXXRecordDecl::hasConstCopyConstructor(const ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000266 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000267}
268
Chandler Carruthb7e95892011-04-23 10:47:28 +0000269bool CXXRecordDecl::isTriviallyCopyable() const {
270 // C++0x [class]p5:
271 // A trivially copyable class is a class that:
272 // -- has no non-trivial copy constructors,
273 if (!hasTrivialCopyConstructor()) return false;
274 // -- has no non-trivial move constructors,
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000275 if (!hasTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000276 // -- has no non-trivial copy assignment operators,
277 if (!hasTrivialCopyAssignment()) return false;
278 // -- has no non-trivial move assignment operators, and
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000279 if (!hasTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000280 // -- has a trivial destructor.
281 if (!hasTrivialDestructor()) return false;
282
283 return true;
284}
285
Douglas Gregor0d405db2010-07-01 20:59:04 +0000286/// \brief Perform a simplistic form of overload resolution that only considers
287/// cv-qualifiers on a single parameter, and return the best overload candidate
288/// (if there is one).
289static CXXMethodDecl *
290GetBestOverloadCandidateSimple(
291 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
292 if (Cands.empty())
293 return 0;
294 if (Cands.size() == 1)
295 return Cands[0].first;
296
297 unsigned Best = 0, N = Cands.size();
298 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000299 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000300 Best = I;
301
302 for (unsigned I = 1; I != N; ++I)
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000303 if (Cands[Best].second.compatiblyIncludes(Cands[I].second))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000304 return 0;
305
306 return Cands[Best].first;
307}
308
Jay Foad4ba2a172011-01-12 09:06:06 +0000309CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(const ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000310 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000311 QualType ClassType
312 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000313 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000314 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000315 Context.getCanonicalType(ClassType));
316 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000317 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000318 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000319 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000320 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000321 // C++ [class.copy]p2:
322 // A non-template constructor for class X is a copy constructor if [...]
323 if (isa<FunctionTemplateDecl>(*Con))
324 continue;
325
Douglas Gregor0d405db2010-07-01 20:59:04 +0000326 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
327 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000328 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
329 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000330 Found.push_back(std::make_pair(
331 const_cast<CXXConstructorDecl *>(Constructor),
332 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000333 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000334 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000335
336 return cast_or_null<CXXConstructorDecl>(
337 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000338}
339
Douglas Gregorb87786f2010-07-01 17:48:08 +0000340CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
341 ASTContext &Context = getASTContext();
342 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
343 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
344
345 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
346 DeclContext::lookup_const_iterator Op, OpEnd;
347 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
348 // C++ [class.copy]p9:
349 // A user-declared copy assignment operator is a non-static non-template
350 // member function of class X with exactly one parameter of type X, X&,
351 // const X&, volatile X& or const volatile X&.
352 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
353 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
354 continue;
355
356 const FunctionProtoType *FnType
357 = Method->getType()->getAs<FunctionProtoType>();
358 assert(FnType && "Overloaded operator has no prototype.");
359 // Don't assert on this; an invalid decl might have been left in the AST.
360 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
361 continue;
362
363 QualType ArgType = FnType->getArgType(0);
364 Qualifiers Quals;
365 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
366 ArgType = Ref->getPointeeType();
367 // If we have a const argument and we have a reference to a non-const,
368 // this function does not match.
369 if (ArgIsConst && !ArgType.isConstQualified())
370 continue;
371
372 Quals = ArgType.getQualifiers();
373 } else {
374 // By-value copy-assignment operators are treated like const X&
375 // copy-assignment operators.
376 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
377 }
378
379 if (!Context.hasSameUnqualifiedType(ArgType, Class))
380 continue;
381
382 // Save this copy-assignment operator. It might be "the one".
383 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
384 }
385
386 // Use a simplistic form of overload resolution to find the candidate.
387 return GetBestOverloadCandidateSimple(Found);
388}
389
Douglas Gregor21386642010-09-28 21:55:22 +0000390void CXXRecordDecl::markedVirtualFunctionPure() {
391 // C++ [class.abstract]p2:
392 // A class is abstract if it has at least one pure virtual function.
393 data().Abstract = true;
394}
395
396void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000397 // Ignore friends and invalid declarations.
398 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000399 return;
400
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000401 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
402 if (FunTmpl)
403 D = FunTmpl->getTemplatedDecl();
404
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000405 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
406 if (Method->isVirtual()) {
407 // C++ [dcl.init.aggr]p1:
408 // An aggregate is an array or a class with [...] no virtual functions.
409 data().Aggregate = false;
410
411 // C++ [class]p4:
412 // A POD-struct is an aggregate class...
413 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000414
415 // Virtual functions make the class non-empty.
416 // FIXME: Standard ref?
417 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000418
419 // C++ [class.virtual]p1:
420 // A class that declares or inherits a virtual function is called a
421 // polymorphic class.
422 data().Polymorphic = true;
423
424 // None of the special member functions are trivial.
425 data().HasTrivialConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000426
427 // C++0x [class.copy]p13:
428 // A copy/move constructor for class X is trivial if [...]
429 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000430 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000431 data().HasTrivialMoveConstructor = false;
432
433 // C++0x [class.copy]p27:
434 // A copy/move assignment operator for class X is trivial if [...]
435 // -- class X has no virtual functions [...]
Douglas Gregor85606eb2010-09-28 20:50:54 +0000436 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000437 data().HasTrivialMoveAssignment = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000438 // FIXME: Destructor?
Chandler Carrutha8225442011-04-30 09:17:45 +0000439
440 // C++0x [class]p7:
441 // A standard-layout class is a class that: [...]
442 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000443 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000444 }
445 }
446
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000447 if (D->isImplicit()) {
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +0000448 // Notify that an implicit member was added after the definition
449 // was completed.
450 if (!isBeingDefined())
451 if (ASTMutationListener *L = getASTMutationListener())
452 L->AddedCXXImplicitMember(data().Definition, D);
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000453
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000454 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
455 // If this is the implicit default constructor, note that we have now
456 // declared it.
457 if (Constructor->isDefaultConstructor())
458 data().DeclaredDefaultConstructor = true;
459 // If this is the implicit copy constructor, note that we have now
460 // declared it.
461 else if (Constructor->isCopyConstructor())
462 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000463 return;
464 }
465
466 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000467 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000468 return;
469 }
470
471 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000472 // If this is the implicit copy constructor, note that we have now
473 // declared it.
474 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000475 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000476 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000477 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000478 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000479
480 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000481 }
482
483 // Handle (user-declared) constructors.
484 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
485 // Note that we have a user-declared constructor.
486 data().UserDeclaredConstructor = true;
487
488 // Note that we have no need of an implicitly-declared default constructor.
489 data().DeclaredDefaultConstructor = true;
490
491 // C++ [dcl.init.aggr]p1:
492 // An aggregate is an array or a class (clause 9) with no
493 // user-declared constructors (12.1) [...].
494 data().Aggregate = false;
495
496 // C++ [class]p4:
497 // A POD-struct is an aggregate class [...]
498 data().PlainOldData = false;
499
500 // C++ [class.ctor]p5:
501 // A constructor is trivial if it is an implicitly-declared default
502 // constructor.
503 // FIXME: C++0x: don't do this for "= default" default constructors.
504 data().HasTrivialConstructor = false;
505
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000506 // Note when we have a user-declared copy or move constructor, which will
507 // suppress the implicit declaration of those constructors.
508 if (!FunTmpl) {
509 if (Constructor->isCopyConstructor()) {
510 data().UserDeclaredCopyConstructor = true;
511 data().DeclaredCopyConstructor = true;
512
513 // C++0x [class.copy]p13:
514 // A copy/move constructor for class X is trivial if it is neither
515 // user-provided nor deleted
516 // FIXME: C++0x: don't do this for "= default" copy constructors.
517 data().HasTrivialCopyConstructor = false;
518 } else if (Constructor->isMoveConstructor()) {
519 // C++0x [class.copy]p13:
520 // A copy/move constructor for class X is trivial if it is neither
521 // user-provided nor deleted
522 // FIXME: C++0x: don't do this for "= default" move constructors.
523 data().HasTrivialMoveConstructor = false;
524 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000525 }
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000526 if (Constructor->isConstExpr() &&
527 !Constructor->isCopyOrMoveConstructor()) {
528 // Record if we see any constexpr constructors which are niether copy
529 // nor move constructors.
530 data().HasConstExprNonCopyMoveConstructor = true;
531 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000532
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000533 return;
534 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000535
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000536 // Handle (user-declared) destructors.
537 if (isa<CXXDestructorDecl>(D)) {
538 data().DeclaredDestructor = true;
539 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000540
541 // C++ [class]p4:
542 // A POD-struct is an aggregate class that has [...] no user-defined
543 // destructor.
544 data().PlainOldData = false;
545
Douglas Gregor85606eb2010-09-28 20:50:54 +0000546 // C++ [class.dtor]p3:
547 // A destructor is trivial if it is an implicitly-declared destructor and
548 // [...].
549 //
550 // FIXME: C++0x: don't do this for "= default" destructors
551 data().HasTrivialDestructor = false;
552
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000553 return;
554 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000555
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000556 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000557 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
558 if (Method->getOverloadedOperator() == OO_Equal) {
559 // We're interested specifically in copy assignment operators.
560 const FunctionProtoType *FnType
561 = Method->getType()->getAs<FunctionProtoType>();
562 assert(FnType && "Overloaded operator has no proto function type.");
563 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
564
565 // Copy assignment operators must be non-templates.
566 if (Method->getPrimaryTemplate() || FunTmpl)
567 return;
568
569 ASTContext &Context = getASTContext();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000570 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
571 const_cast<CXXRecordDecl*>(this)));
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000572
573 bool isRValueRefArg = false;
574 QualType ArgType = FnType->getArgType(0);
575 if (const LValueReferenceType *Ref =
576 ArgType->getAs<LValueReferenceType>()) {
577 ArgType = Ref->getPointeeType();
578 } else if (const RValueReferenceType *Ref =
579 ArgType->getAs<RValueReferenceType>()) {
580 ArgType = Ref->getPointeeType();
581 isRValueRefArg = true;
582 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000583 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
584 return;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000585
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000586 // C++ [class]p4:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000587 // A POD-struct is an aggregate class that [...] has no user-defined
588 // copy assignment operator [...].
589 // FIXME: This should be probably determined dynamically in terms of
590 // other more precise attributes to correctly model how it is specified
591 // in C++0x. Setting it here happens to do the right thing.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000592 data().PlainOldData = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000593
594 if (!isRValueRefArg) {
595 // This is a copy assignment operator.
596
597 // Suppress the implicit declaration of a copy constructor.
598 data().UserDeclaredCopyAssignment = true;
599 data().DeclaredCopyAssignment = true;
600
601 // C++0x [class.copy]p27:
602 // A copy/move assignment operator for class X is trivial if it is
603 // neither user-provided nor deleted [...]
604 // FIXME: C++0x: don't do this for "= default" copy operators.
605 data().HasTrivialCopyAssignment = false;
606 } else {
607 // This is a move assignment operator.
608
609 // C++0x [class.copy]p27:
610 // A copy/move assignment operator for class X is trivial if it is
611 // neither user-provided nor deleted [...]
612 // FIXME: C++0x: don't do this for "= default" copy operators.
613 data().HasTrivialMoveAssignment = false;
614 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000615 }
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000616
Douglas Gregore80622f2010-09-29 04:25:11 +0000617 // Keep the list of conversion functions up-to-date.
618 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
619 // We don't record specializations.
620 if (Conversion->getPrimaryTemplate())
621 return;
622
623 // FIXME: We intentionally don't use the decl's access here because it
624 // hasn't been set yet. That's really just a misdesign in Sema.
625
626 if (FunTmpl) {
627 if (FunTmpl->getPreviousDeclaration())
628 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
629 FunTmpl);
630 else
631 data().Conversions.addDecl(FunTmpl);
632 } else {
633 if (Conversion->getPreviousDeclaration())
634 data().Conversions.replace(Conversion->getPreviousDeclaration(),
635 Conversion);
636 else
637 data().Conversions.addDecl(Conversion);
638 }
639 }
640
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000641 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000642 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000643
644 // Handle non-static data members.
645 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
646 // C++ [dcl.init.aggr]p1:
647 // An aggregate is an array or a class (clause 9) with [...] no
648 // private or protected non-static data members (clause 11).
649 //
650 // A POD must be an aggregate.
651 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
652 data().Aggregate = false;
653 data().PlainOldData = false;
654 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000655
656 // C++0x [class]p7:
657 // A standard-layout class is a class that:
658 // [...]
659 // -- has the same access control for all non-static data members,
660 switch (D->getAccess()) {
661 case AS_private: data().HasPrivateFields = true; break;
662 case AS_protected: data().HasProtectedFields = true; break;
663 case AS_public: data().HasPublicFields = true; break;
664 case AS_none: assert(0 && "Invalid access specifier");
665 };
666 if ((data().HasPrivateFields + data().HasProtectedFields +
667 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000668 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000669
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000670 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000671 // A POD struct is a class that is both a trivial class and a
672 // standard-layout class, and has no non-static data members of type
673 // non-POD struct, non-POD union (or array of such types).
674 ASTContext &Context = getASTContext();
675 QualType T = Context.getBaseElementType(Field->getType());
676 if (!T->isPODType())
677 data().PlainOldData = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000678 if (T->isReferenceType()) {
Douglas Gregor85606eb2010-09-28 20:50:54 +0000679 data().HasTrivialConstructor = false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000680
Chandler Carrutha8225442011-04-30 09:17:45 +0000681 // C++0x [class]p7:
682 // A standard-layout class is a class that:
683 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000684 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000685 }
686
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000687 // Record if this field is the first non-literal field or base.
688 if (!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType())
689 data().HasNonLiteralTypeFieldsOrBases = true;
690
Douglas Gregor85606eb2010-09-28 20:50:54 +0000691 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
692 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
693 if (FieldRec->getDefinition()) {
694 if (!FieldRec->hasTrivialConstructor())
695 data().HasTrivialConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000696
697 // C++0x [class.copy]p13:
698 // A copy/move constructor for class X is trivial if [...]
699 // [...]
700 // -- for each non-static data member of X that is of class type (or
701 // an array thereof), the constructor selected to copy/move that
702 // member is trivial;
703 // FIXME: C++0x: We don't correctly model 'selected' constructors.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000704 if (!FieldRec->hasTrivialCopyConstructor())
705 data().HasTrivialCopyConstructor = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000706 if (!FieldRec->hasTrivialMoveConstructor())
707 data().HasTrivialMoveConstructor = false;
708
709 // C++0x [class.copy]p27:
710 // A copy/move assignment operator for class X is trivial if [...]
711 // [...]
712 // -- for each non-static data member of X that is of class type (or
713 // an array thereof), the assignment operator selected to
714 // copy/move that member is trivial;
715 // FIXME: C++0x: We don't correctly model 'selected' operators.
Douglas Gregor85606eb2010-09-28 20:50:54 +0000716 if (!FieldRec->hasTrivialCopyAssignment())
717 data().HasTrivialCopyAssignment = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000718 if (!FieldRec->hasTrivialMoveAssignment())
719 data().HasTrivialMoveAssignment = false;
720
Douglas Gregor85606eb2010-09-28 20:50:54 +0000721 if (!FieldRec->hasTrivialDestructor())
722 data().HasTrivialDestructor = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000723
724 // C++0x [class]p7:
725 // A standard-layout class is a class that:
726 // -- has no non-static data members of type non-standard-layout
727 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000728 if (!FieldRec->isStandardLayout())
729 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000730
731 // C++0x [class]p7:
732 // A standard-layout class is a class that:
733 // [...]
734 // -- has no base classes of the same type as the first non-static
735 // data member.
736 // We don't want to expend bits in the state of the record decl
737 // tracking whether this is the first non-static data member so we
738 // cheat a bit and use some of the existing state: the empty bit.
739 // Virtual bases and virtual methods make a class non-empty, but they
740 // also make it non-standard-layout so we needn't check here.
741 // A non-empty base class may leave the class standard-layout, but not
742 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000743 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000744 // data member must come through here with Empty still true, and Empty
745 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000746 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000747 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
748 BE = bases_end();
749 BI != BE; ++BI) {
750 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000751 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000752 break;
753 }
754 }
755 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000756 }
757 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000758
759 // C++0x [class]p7:
760 // A standard-layout class is a class that:
761 // [...]
762 // -- either has no non-static data members in the most derived
763 // class and at most one base class with non-static data members,
764 // or has no base classes with non-static data members, and
765 // At this point we know that we have a non-static data member, so the last
766 // clause holds.
767 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000768 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000769
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000770 // If this is not a zero-length bit-field, then the class is not empty.
771 if (data().Empty) {
772 if (!Field->getBitWidth())
773 data().Empty = false;
774 else if (!Field->getBitWidth()->isTypeDependent() &&
775 !Field->getBitWidth()->isValueDependent()) {
776 llvm::APSInt Bits;
777 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
778 if (!!Bits)
779 data().Empty = false;
780 }
781 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000782 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000783
784 // Handle using declarations of conversion functions.
785 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
786 if (Shadow->getDeclName().getNameKind()
787 == DeclarationName::CXXConversionFunctionName)
788 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000789}
790
John McCallb05b5f32010-03-15 09:07:48 +0000791static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
792 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000793 if (isa<UsingShadowDecl>(Conv))
794 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000795 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
796 T = ConvTemp->getTemplatedDecl()->getResultType();
797 else
798 T = cast<CXXConversionDecl>(Conv)->getConversionType();
799 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000800}
801
John McCallb05b5f32010-03-15 09:07:48 +0000802/// Collect the visible conversions of a base class.
803///
804/// \param Base a base class of the class we're considering
805/// \param InVirtual whether this base class is a virtual base (or a base
806/// of a virtual base)
807/// \param Access the access along the inheritance path to this base
808/// \param ParentHiddenTypes the conversions provided by the inheritors
809/// of this base
810/// \param Output the set to which to add conversions from non-virtual bases
811/// \param VOutput the set to which to add conversions from virtual bases
812/// \param HiddenVBaseCs the set of conversions which were hidden in a
813/// virtual base along some inheritance path
814static void CollectVisibleConversions(ASTContext &Context,
815 CXXRecordDecl *Record,
816 bool InVirtual,
817 AccessSpecifier Access,
818 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
819 UnresolvedSetImpl &Output,
820 UnresolvedSetImpl &VOutput,
821 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
822 // The set of types which have conversions in this class or its
823 // subclasses. As an optimization, we don't copy the derived set
824 // unless it might change.
825 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
826 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
827
828 // Collect the direct conversions and figure out which conversions
829 // will be hidden in the subclasses.
830 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
831 if (!Cs.empty()) {
832 HiddenTypesBuffer = ParentHiddenTypes;
833 HiddenTypes = &HiddenTypesBuffer;
834
835 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
836 bool Hidden =
837 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
838
839 // If this conversion is hidden and we're in a virtual base,
840 // remember that it's hidden along some inheritance path.
841 if (Hidden && InVirtual)
842 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
843
844 // If this conversion isn't hidden, add it to the appropriate output.
845 else if (!Hidden) {
846 AccessSpecifier IAccess
847 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
848
849 if (InVirtual)
850 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000851 else
John McCallb05b5f32010-03-15 09:07:48 +0000852 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000853 }
854 }
855 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000856
John McCallb05b5f32010-03-15 09:07:48 +0000857 // Collect information recursively from any base classes.
858 for (CXXRecordDecl::base_class_iterator
859 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
860 const RecordType *RT = I->getType()->getAs<RecordType>();
861 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000862
John McCallb05b5f32010-03-15 09:07:48 +0000863 AccessSpecifier BaseAccess
864 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
865 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000866
John McCallb05b5f32010-03-15 09:07:48 +0000867 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
868 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
869 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000870 }
John McCallb05b5f32010-03-15 09:07:48 +0000871}
Sebastian Redl9994a342009-10-25 17:03:50 +0000872
John McCallb05b5f32010-03-15 09:07:48 +0000873/// Collect the visible conversions of a class.
874///
875/// This would be extremely straightforward if it weren't for virtual
876/// bases. It might be worth special-casing that, really.
877static void CollectVisibleConversions(ASTContext &Context,
878 CXXRecordDecl *Record,
879 UnresolvedSetImpl &Output) {
880 // The collection of all conversions in virtual bases that we've
881 // found. These will be added to the output as long as they don't
882 // appear in the hidden-conversions set.
883 UnresolvedSet<8> VBaseCs;
884
885 // The set of conversions in virtual bases that we've determined to
886 // be hidden.
887 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
888
889 // The set of types hidden by classes derived from this one.
890 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
891
892 // Go ahead and collect the direct conversions and add them to the
893 // hidden-types set.
894 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
895 Output.append(Cs.begin(), Cs.end());
896 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
897 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
898
899 // Recursively collect conversions from base classes.
900 for (CXXRecordDecl::base_class_iterator
901 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
902 const RecordType *RT = I->getType()->getAs<RecordType>();
903 if (!RT) continue;
904
905 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
906 I->isVirtual(), I->getAccessSpecifier(),
907 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
908 }
909
910 // Add any unhidden conversions provided by virtual bases.
911 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
912 I != E; ++I) {
913 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
914 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000915 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000916}
917
918/// getVisibleConversionFunctions - get all conversion functions visible
919/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000920const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000921 // If root class, all conversions are visible.
922 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000923 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000924 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000925 if (data().ComputedVisibleConversions)
926 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000927 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000928 data().ComputedVisibleConversions = true;
929 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000930}
931
John McCall32daa422010-03-31 01:36:47 +0000932void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
933 // This operation is O(N) but extremely rare. Sema only uses it to
934 // remove UsingShadowDecls in a class that were followed by a direct
935 // declaration, e.g.:
936 // class A : B {
937 // using B::operator int;
938 // operator int();
939 // };
940 // This is uncommon by itself and even more uncommon in conjunction
941 // with sufficiently large numbers of directly-declared conversions
942 // that asymptotic behavior matters.
943
944 UnresolvedSetImpl &Convs = *getConversionFunctions();
945 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
946 if (Convs[I].getDecl() == ConvDecl) {
947 Convs.erase(I);
948 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
949 && "conversion was found multiple times in unresolved set");
950 return;
951 }
952 }
953
954 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000955}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000956
Douglas Gregorf6b11852009-10-08 15:14:33 +0000957CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000958 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000959 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
960
961 return 0;
962}
963
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000964MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
965 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
966}
967
Douglas Gregorf6b11852009-10-08 15:14:33 +0000968void
969CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
970 TemplateSpecializationKind TSK) {
971 assert(TemplateOrInstantiation.isNull() &&
972 "Previous template or instantiation?");
973 assert(!isa<ClassTemplateSpecializationDecl>(this));
974 TemplateOrInstantiation
975 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
976}
977
Anders Carlssonb13e3572009-12-07 06:33:48 +0000978TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
979 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000980 = dyn_cast<ClassTemplateSpecializationDecl>(this))
981 return Spec->getSpecializationKind();
982
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000983 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000984 return MSInfo->getTemplateSpecializationKind();
985
986 return TSK_Undeclared;
987}
988
989void
990CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
991 if (ClassTemplateSpecializationDecl *Spec
992 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
993 Spec->setSpecializationKind(TSK);
994 return;
995 }
996
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000997 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000998 MSInfo->setTemplateSpecializationKind(TSK);
999 return;
1000 }
1001
1002 assert(false && "Not a class template or member class specialization");
1003}
1004
Douglas Gregor1d110e02010-07-01 14:13:13 +00001005CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1006 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001007 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001008
1009 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001010 = Context.DeclarationNames.getCXXDestructorName(
1011 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001012
John McCallc0bf4622010-02-23 00:48:20 +00001013 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +00001014 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001015 if (I == E)
1016 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001018 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +00001019 return Dtor;
1020}
1021
Douglas Gregorda2142f2011-02-19 18:51:44 +00001022void CXXRecordDecl::completeDefinition() {
1023 completeDefinition(0);
1024}
1025
1026void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1027 RecordDecl::completeDefinition();
1028
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001029 // If the class may be abstract (but hasn't been marked as such), check for
1030 // any pure final overriders.
1031 if (mayBeAbstract()) {
1032 CXXFinalOverriderMap MyFinalOverriders;
1033 if (!FinalOverriders) {
1034 getFinalOverriders(MyFinalOverriders);
1035 FinalOverriders = &MyFinalOverriders;
1036 }
1037
1038 bool Done = false;
1039 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1040 MEnd = FinalOverriders->end();
1041 M != MEnd && !Done; ++M) {
1042 for (OverridingMethods::iterator SO = M->second.begin(),
1043 SOEnd = M->second.end();
1044 SO != SOEnd && !Done; ++SO) {
1045 assert(SO->second.size() > 0 &&
1046 "All virtual functions have overridding virtual functions");
1047
1048 // C++ [class.abstract]p4:
1049 // A class is abstract if it contains or inherits at least one
1050 // pure virtual function for which the final overrider is pure
1051 // virtual.
1052 if (SO->second.front().Method->isPure()) {
1053 data().Abstract = true;
1054 Done = true;
1055 break;
1056 }
1057 }
1058 }
1059 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001060
1061 // Set access bits correctly on the directly-declared conversions.
1062 for (UnresolvedSetIterator I = data().Conversions.begin(),
1063 E = data().Conversions.end();
1064 I != E; ++I)
1065 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001066}
1067
1068bool CXXRecordDecl::mayBeAbstract() const {
1069 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1070 isDependentContext())
1071 return false;
1072
1073 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1074 BEnd = bases_end();
1075 B != BEnd; ++B) {
1076 CXXRecordDecl *BaseDecl
1077 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1078 if (BaseDecl->isAbstract())
1079 return true;
1080 }
1081
1082 return false;
1083}
1084
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001085CXXMethodDecl *
1086CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001087 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001088 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001089 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001090 bool isStatic, StorageClass SCAsWritten, bool isInline,
1091 SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001092 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001093 isStatic, SCAsWritten, isInline, EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001094}
1095
Douglas Gregor90916562009-09-29 18:16:17 +00001096bool CXXMethodDecl::isUsualDeallocationFunction() const {
1097 if (getOverloadedOperator() != OO_Delete &&
1098 getOverloadedOperator() != OO_Array_Delete)
1099 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001100
1101 // C++ [basic.stc.dynamic.deallocation]p2:
1102 // A template instance is never a usual deallocation function,
1103 // regardless of its signature.
1104 if (getPrimaryTemplate())
1105 return false;
1106
Douglas Gregor90916562009-09-29 18:16:17 +00001107 // C++ [basic.stc.dynamic.deallocation]p2:
1108 // If a class T has a member deallocation function named operator delete
1109 // with exactly one parameter, then that function is a usual (non-placement)
1110 // deallocation function. [...]
1111 if (getNumParams() == 1)
1112 return true;
1113
1114 // C++ [basic.stc.dynamic.deallocation]p2:
1115 // [...] If class T does not declare such an operator delete but does
1116 // declare a member deallocation function named operator delete with
1117 // exactly two parameters, the second of which has type std::size_t (18.1),
1118 // then this function is a usual deallocation function.
1119 ASTContext &Context = getASTContext();
1120 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001121 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1122 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001123 return false;
1124
1125 // This function is a usual deallocation function if there are no
1126 // single-parameter deallocation functions of the same kind.
1127 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1128 R.first != R.second; ++R.first) {
1129 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
1130 if (FD->getNumParams() == 1)
1131 return false;
1132 }
1133
1134 return true;
1135}
1136
Douglas Gregor06a9f362010-05-01 20:49:11 +00001137bool CXXMethodDecl::isCopyAssignmentOperator() const {
1138 // C++0x [class.copy]p19:
1139 // A user-declared copy assignment operator X::operator= is a non-static
1140 // non-template member function of class X with exactly one parameter of
1141 // type X, X&, const X&, volatile X& or const volatile X&.
1142 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1143 /*non-static*/ isStatic() ||
1144 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1145 /*exactly one parameter*/getNumParams() != 1)
1146 return false;
1147
1148 QualType ParamType = getParamDecl(0)->getType();
1149 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1150 ParamType = Ref->getPointeeType();
1151
1152 ASTContext &Context = getASTContext();
1153 QualType ClassType
1154 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1155 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1156}
1157
Anders Carlsson05eb2442009-05-16 23:58:37 +00001158void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001159 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001160 assert(!MD->getParent()->isDependentContext() &&
1161 "Can't add an overridden method to a class template!");
1162
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001163 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001164}
1165
1166CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001167 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001168}
1169
1170CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001171 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001172}
1173
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001174unsigned CXXMethodDecl::size_overridden_methods() const {
1175 return getASTContext().overridden_methods_size(this);
1176}
1177
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001178QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001179 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1180 // If the member function is declared const, the type of this is const X*,
1181 // if the member function is declared volatile, the type of this is
1182 // volatile X*, and if the member function is declared const volatile,
1183 // the type of this is const volatile X*.
1184
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001185 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001186
John McCall3cb0ebd2010-03-10 03:28:59 +00001187 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001188 ClassTy = C.getQualifiedType(ClassTy,
1189 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001190 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001191}
1192
Eli Friedmand7d7f672009-12-06 20:50:05 +00001193bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001194 // If this function is a template instantiation, look at the template from
1195 // which it was instantiated.
1196 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1197 if (!CheckFn)
1198 CheckFn = this;
1199
Eli Friedmand7d7f672009-12-06 20:50:05 +00001200 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001201 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001202}
1203
Sean Huntcbb67482011-01-08 20:30:50 +00001204CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1205 TypeSourceInfo *TInfo, bool IsVirtual,
1206 SourceLocation L, Expr *Init,
1207 SourceLocation R,
1208 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001209 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001210 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1211 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001212{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001213}
1214
Sean Huntcbb67482011-01-08 20:30:50 +00001215CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1216 FieldDecl *Member,
1217 SourceLocation MemberLoc,
1218 SourceLocation L, Expr *Init,
1219 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001220 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001221 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1222 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1223{
1224}
1225
Sean Huntcbb67482011-01-08 20:30:50 +00001226CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1227 IndirectFieldDecl *Member,
1228 SourceLocation MemberLoc,
1229 SourceLocation L, Expr *Init,
1230 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001231 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001232 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001233 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001234{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001235}
1236
Sean Huntcbb67482011-01-08 20:30:50 +00001237CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Hunt41717662011-02-26 19:13:13 +00001238 SourceLocation D, SourceLocation L,
1239 CXXConstructorDecl *Target, Expr *Init,
1240 SourceLocation R)
1241 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init),
1242 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1243 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1244{
1245}
1246
1247CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001248 FieldDecl *Member,
1249 SourceLocation MemberLoc,
1250 SourceLocation L, Expr *Init,
1251 SourceLocation R,
1252 VarDecl **Indices,
1253 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001254 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001255 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001256 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001257{
1258 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1259 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1260}
1261
Sean Huntcbb67482011-01-08 20:30:50 +00001262CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1263 FieldDecl *Member,
1264 SourceLocation MemberLoc,
1265 SourceLocation L, Expr *Init,
1266 SourceLocation R,
1267 VarDecl **Indices,
1268 unsigned NumIndices) {
1269 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001270 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001271 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001272 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1273 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001274}
1275
Sean Huntcbb67482011-01-08 20:30:50 +00001276TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001277 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001278 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001279 else
1280 return TypeLoc();
1281}
1282
Sean Huntcbb67482011-01-08 20:30:50 +00001283const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001284 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001285 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001286 else
1287 return 0;
1288}
1289
Sean Huntcbb67482011-01-08 20:30:50 +00001290SourceLocation CXXCtorInitializer::getSourceLocation() const {
Sean Hunt41717662011-02-26 19:13:13 +00001291 if (isAnyMemberInitializer() || isDelegatingInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001292 return getMemberLocation();
1293
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001294 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001295}
1296
Sean Huntcbb67482011-01-08 20:30:50 +00001297SourceRange CXXCtorInitializer::getSourceRange() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001298 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001299}
1300
Douglas Gregorb48fe382008-10-31 09:07:45 +00001301CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001302CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001303 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001304 QualType(), 0, false, false, false);
1305}
1306
1307CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001308CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001309 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001310 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001311 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001312 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001313 bool isInline,
1314 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001315 assert(NameInfo.getName().getNameKind()
1316 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001317 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001318 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
1319 isExplicit, isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001320}
1321
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001322bool CXXConstructorDecl::isDefaultConstructor() const {
1323 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001324 // A default constructor for a class X is a constructor of class
1325 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001326 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001327 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001328}
1329
Mike Stump1eb44332009-09-09 15:08:12 +00001330bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001331CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001332 return isCopyOrMoveConstructor(TypeQuals) &&
1333 getParamDecl(0)->getType()->isLValueReferenceType();
1334}
1335
1336bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1337 return isCopyOrMoveConstructor(TypeQuals) &&
1338 getParamDecl(0)->getType()->isRValueReferenceType();
1339}
1340
1341/// \brief Determine whether this is a copy or move constructor.
1342bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001343 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001344 // A non-template constructor for class X is a copy constructor
1345 // if its first parameter is of type X&, const X&, volatile X& or
1346 // const volatile X&, and either there are no other parameters
1347 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001348 // C++0x [class.copy]p3:
1349 // A non-template constructor for class X is a move constructor if its
1350 // first parameter is of type X&&, const X&&, volatile X&&, or
1351 // const volatile X&&, and either there are no other parameters or else
1352 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001353 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001354 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001355 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001356 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001357 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001358
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001359 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001360
1361 // Do we have a reference type?
1362 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001363 if (!ParamRefType)
1364 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001365
Douglas Gregorfd476482009-11-13 23:59:09 +00001366 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001367 ASTContext &Context = getASTContext();
1368
Douglas Gregorfd476482009-11-13 23:59:09 +00001369 CanQualType PointeeType
1370 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001371 CanQualType ClassTy
1372 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001373 if (PointeeType.getUnqualifiedType() != ClassTy)
1374 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001375
John McCall0953e762009-09-24 19:53:00 +00001376 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001377
1378 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001379 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001380 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001381}
1382
Anders Carlssonfaccd722009-08-28 16:57:08 +00001383bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001384 // C++ [class.conv.ctor]p1:
1385 // A constructor declared without the function-specifier explicit
1386 // that can be called with a single parameter specifies a
1387 // conversion from the type of its first parameter to the type of
1388 // its class. Such a constructor is called a converting
1389 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001390 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001391 return false;
1392
Mike Stump1eb44332009-09-09 15:08:12 +00001393 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001394 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001395 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001396 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001397}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001398
Douglas Gregor6493cc52010-11-08 17:16:59 +00001399bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001400 if ((getNumParams() < 1) ||
1401 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1402 (getPrimaryTemplate() == 0) ||
1403 (getDescribedFunctionTemplate() != 0))
1404 return false;
1405
1406 const ParmVarDecl *Param = getParamDecl(0);
1407
1408 ASTContext &Context = getASTContext();
1409 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1410
Douglas Gregor66724ea2009-11-14 01:20:54 +00001411 // Is it the same as our our class type?
1412 CanQualType ClassTy
1413 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1414 if (ParamType.getUnqualifiedType() != ClassTy)
1415 return false;
1416
1417 return true;
1418}
1419
Sebastian Redlf677ea32011-02-05 19:23:19 +00001420const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1421 // Hack: we store the inherited constructor in the overridden method table
1422 method_iterator It = begin_overridden_methods();
1423 if (It == end_overridden_methods())
1424 return 0;
1425
1426 return cast<CXXConstructorDecl>(*It);
1427}
1428
1429void
1430CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1431 // Hack: we store the inherited constructor in the overridden method table
1432 assert(size_overridden_methods() == 0 && "Base ctor already set.");
1433 addOverriddenMethod(BaseCtor);
1434}
1435
Douglas Gregor42a552f2008-11-05 20:51:48 +00001436CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001437CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001438 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001439 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001440}
1441
1442CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001443CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001444 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001445 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001446 QualType T, TypeSourceInfo *TInfo,
1447 bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001448 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001449 assert(NameInfo.getName().getNameKind()
1450 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001451 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001452 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001453 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001454}
1455
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001456CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001457CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001458 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
Douglas Gregorf5251602011-03-08 17:10:18 +00001459 QualType(), 0, false, false,
1460 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001461}
1462
1463CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001464CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001465 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001466 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001467 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001468 bool isInline, bool isExplicit,
1469 SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001470 assert(NameInfo.getName().getNameKind()
1471 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001472 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001473 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001474 isInline, isExplicit, EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001475}
1476
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001477LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001478 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001479 SourceLocation ExternLoc,
1480 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001481 LanguageIDs Lang,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001482 SourceLocation RBraceLoc) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001483 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001484}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001485
1486UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1487 SourceLocation L,
1488 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001489 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001490 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001491 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001492 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001493 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1494 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001495 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1496 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001497}
1498
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001499NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1500 if (NamespaceAliasDecl *NA =
1501 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1502 return NA->getNamespace();
1503 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1504}
1505
Mike Stump1eb44332009-09-09 15:08:12 +00001506NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001507 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001508 SourceLocation AliasLoc,
1509 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001510 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001511 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001512 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001513 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1514 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001515 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1516 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001517}
1518
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001519UsingDecl *UsingShadowDecl::getUsingDecl() const {
1520 const UsingShadowDecl *Shadow = this;
1521 while (const UsingShadowDecl *NextShadow =
1522 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1523 Shadow = NextShadow;
1524 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1525}
1526
1527void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1528 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1529 "declaration already in set");
1530 assert(S->getUsingDecl() == this);
1531
1532 if (FirstUsingShadow)
1533 S->UsingOrNextShadow = FirstUsingShadow;
1534 FirstUsingShadow = S;
1535}
1536
1537void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1538 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1539 "declaration not in set");
1540 assert(S->getUsingDecl() == this);
1541
1542 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1543
1544 if (FirstUsingShadow == S) {
1545 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1546 S->UsingOrNextShadow = this;
1547 return;
1548 }
1549
1550 UsingShadowDecl *Prev = FirstUsingShadow;
1551 while (Prev->UsingOrNextShadow != S)
1552 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1553 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1554 S->UsingOrNextShadow = this;
1555}
1556
Douglas Gregordc355712011-02-25 00:36:19 +00001557UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
1558 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001559 const DeclarationNameInfo &NameInfo,
1560 bool IsTypeNameArg) {
Douglas Gregordc355712011-02-25 00:36:19 +00001561 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001562}
1563
John McCall7ba107a2009-11-18 02:36:19 +00001564UnresolvedUsingValueDecl *
1565UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1566 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001567 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001568 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001569 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001570 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001571}
1572
1573UnresolvedUsingTypenameDecl *
1574UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1575 SourceLocation UsingLoc,
1576 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001577 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001578 SourceLocation TargetNameLoc,
1579 DeclarationName TargetName) {
1580 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00001581 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00001582 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001583}
1584
Anders Carlssonfb311762009-03-14 00:25:26 +00001585StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001586 SourceLocation StaticAssertLoc,
1587 Expr *AssertExpr,
1588 StringLiteral *Message,
1589 SourceLocation RParenLoc) {
1590 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
1591 RParenLoc);
Anders Carlssonfb311762009-03-14 00:25:26 +00001592}
1593
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001594static const char *getAccessName(AccessSpecifier AS) {
1595 switch (AS) {
1596 default:
1597 case AS_none:
1598 assert("Invalid access specifier!");
1599 return 0;
1600 case AS_public:
1601 return "public";
1602 case AS_private:
1603 return "private";
1604 case AS_protected:
1605 return "protected";
1606 }
1607}
1608
1609const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1610 AccessSpecifier AS) {
1611 return DB << getAccessName(AS);
1612}