blob: 3e6d8b379ff4a942ca9ae87872abb051907a9218 [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"
Anders Carlssonfb311762009-03-14 00:25:26 +000017#include "clang/AST/Expr.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000018#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000019#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000020#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000021#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Decl Allocation/Deallocation Method Implementations
26//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000027
John McCall86ff3082010-02-04 22:26:26 +000028CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
29 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000030 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000031 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
32 Abstract(false), HasTrivialConstructor(true),
33 HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true),
Fariborz Jahanian62509212009-09-12 18:26:03 +000034 HasTrivialDestructor(true), ComputedVisibleConversions(false),
Douglas Gregor18274032010-07-03 00:47:00 +000035 DeclaredDefaultConstructor(false), DeclaredCopyConstructor(false),
Douglas Gregora376d102010-07-02 21:50:04 +000036 DeclaredCopyAssignment(false), DeclaredDestructor(false),
Fariborz Jahanian62509212009-09-12 18:26:03 +000037 Bases(0), NumBases(0), VBases(0), NumVBases(0),
John McCalld60e22e2010-03-12 01:19:31 +000038 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000039}
40
41CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
42 SourceLocation L, IdentifierInfo *Id,
43 CXXRecordDecl *PrevDecl,
44 SourceLocation TKL)
45 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
46 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000047 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000048
Ted Kremenek4b7c9832008-09-05 17:16:31 +000049CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
50 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000051 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000052 CXXRecordDecl* PrevDecl,
53 bool DelayTypeCreation) {
Mike Stump1eb44332009-09-09 15:08:12 +000054 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000055 PrevDecl, TKL);
Mike Stump1eb44332009-09-09 15:08:12 +000056
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000057 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000058 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000059 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000060 return R;
61}
62
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000063CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, EmptyShell Empty) {
64 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(), 0, 0,
65 SourceLocation());
66}
67
Mike Stump1eb44332009-09-09 15:08:12 +000068void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000069CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000070 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000071 ASTContext &C = getASTContext();
72
Mike Stump1eb44332009-09-09 15:08:12 +000073 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000074 // An aggregate is an array or a class (clause 9) with [...]
75 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000076 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000077
John McCall86ff3082010-02-04 22:26:26 +000078 if (data().Bases)
79 C.Deallocate(data().Bases);
Mike Stump1eb44332009-09-09 15:08:12 +000080
Anders Carlsson6f6de732010-03-29 05:13:12 +000081 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000082 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000083
84 // The virtual bases of this class.
85 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000086
John McCall86ff3082010-02-04 22:26:26 +000087 data().Bases = new(C) CXXBaseSpecifier [NumBases];
88 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000089 for (unsigned i = 0; i < NumBases; ++i) {
John McCall86ff3082010-02-04 22:26:26 +000090 data().Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000091 // Keep track of inherited vbases for this base class.
92 const CXXBaseSpecifier *Base = Bases[i];
93 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +000094 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000095 if (BaseType->isDependentType())
96 continue;
97 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +000098 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +000099
Douglas Gregor6e3c7712010-09-27 23:16:44 +0000100 // C++ [dcl.init.aggr]p1:
101 // An aggregate is [...] a class with [...] no base classes [...].
102 data().Aggregate = false;
103
104 // C++ [class]p4:
105 // A POD-struct is an aggregate class...
106 data().PlainOldData = false;
107
Douglas Gregorcdbfa6c2010-09-27 23:31:14 +0000108 // A class with a non-empty base class is not empty.
109 // FIXME: Standard ref?
110 if (!BaseClassDecl->isEmpty())
111 data().Empty = false;
112
Douglas Gregor4a74df52010-09-27 23:39:06 +0000113 // C++ [class.virtual]p1:
114 // A class that declares or inherits a virtual function is called a
115 // polymorphic class.
116 if (BaseClassDecl->isPolymorphic())
117 data().Polymorphic = true;
118
Anders Carlsson6f6de732010-03-29 05:13:12 +0000119 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000120 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000121 BaseClassDecl->vbases_begin(),
122 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000123 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000124 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000125 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000126 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000127
128 if (Base->isVirtual()) {
129 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000130 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000131 VBases.push_back(Base);
Douglas Gregorcdbfa6c2010-09-27 23:31:14 +0000132
133 // C++0x [meta.unary.prop] is_empty:
134 // T is a class type, but not a union type, with ... no virtual base
135 // classes
136 data().Empty = false;
Douglas Gregore10288c2010-09-28 00:00:00 +0000137
138 // C++ [class.ctor]p5:
139 // A constructor is trivial if its class has no virtual base classes.
140 data().HasTrivialConstructor = false;
141
142 // C++ [class.copy]p6:
143 // A copy constructor is trivial if its class has no virtual base
144 // classes.
145 data().HasTrivialCopyConstructor = false;
146
147 // C++ [class.copy]p11:
148 // A copy assignment operator is trivial if its class has no virtual
149 // base classes.
150 data().HasTrivialCopyAssignment = false;
151 } else {
152 // C++ [class.ctor]p5:
153 // A constructor is trivial if all the direct base classes of its
154 // class have trivial constructors.
155 if (!BaseClassDecl->hasTrivialConstructor())
156 data().HasTrivialConstructor = false;
157
158 // C++ [class.copy]p6:
159 // A copy constructor is trivial if all the direct base classes of its
160 // class have trivial copy constructors.
161 if (!BaseClassDecl->hasTrivialCopyConstructor())
162 data().HasTrivialCopyConstructor = false;
163
164 // C++ [class.copy]p11:
165 // A copy assignment operator is trivial if all the direct base classes
166 // of its class have trivial copy assignment operators.
167 if (!BaseClassDecl->hasTrivialCopyAssignment())
168 data().HasTrivialCopyAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000169 }
Douglas Gregore10288c2010-09-28 00:00:00 +0000170
171 // C++ [class.ctor]p3:
172 // A destructor is trivial if all the direct base classes of its class
173 // have trivial destructors.
174 if (!BaseClassDecl->hasTrivialDestructor())
175 data().HasTrivialDestructor = false;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000176 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000177
178 if (VBases.empty())
179 return;
180
181 // Create base specifier for any direct or indirect virtual bases.
182 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
183 data().NumVBases = VBases.size();
184 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000185 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
186
Anders Carlsson6f6de732010-03-29 05:13:12 +0000187 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000188 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000189 continue;
190
Nick Lewycky56062202010-07-26 16:56:01 +0000191 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
192 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000193
194 data().VBases[I] =
195 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000196 VBaseClassDecl->getTagKind() == TTK_Class,
Nick Lewycky56062202010-07-26 16:56:01 +0000197 VBases[I]->getAccessSpecifier(), VBaseTypeInfo);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000198 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000199}
200
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000201/// Callback function for CXXRecordDecl::forallBases that acknowledges
202/// that it saw a base class.
203static bool SawBase(const CXXRecordDecl *, void *) {
204 return true;
205}
206
207bool CXXRecordDecl::hasAnyDependentBases() const {
208 if (!isDependentContext())
209 return false;
210
211 return !forallBases(SawBase, 0);
212}
213
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000214bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000215 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000216}
217
Douglas Gregor0d405db2010-07-01 20:59:04 +0000218/// \brief Perform a simplistic form of overload resolution that only considers
219/// cv-qualifiers on a single parameter, and return the best overload candidate
220/// (if there is one).
221static CXXMethodDecl *
222GetBestOverloadCandidateSimple(
223 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
224 if (Cands.empty())
225 return 0;
226 if (Cands.size() == 1)
227 return Cands[0].first;
228
229 unsigned Best = 0, N = Cands.size();
230 for (unsigned I = 1; I != N; ++I)
231 if (Cands[Best].second.isSupersetOf(Cands[I].second))
232 Best = I;
233
234 for (unsigned I = 1; I != N; ++I)
235 if (Cands[Best].second.isSupersetOf(Cands[I].second))
236 return 0;
237
238 return Cands[Best].first;
239}
240
Mike Stump1eb44332009-09-09 15:08:12 +0000241CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000242 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000243 QualType ClassType
244 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000245 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000246 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000247 Context.getCanonicalType(ClassType));
248 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000249 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000250 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000251 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000252 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000253 // C++ [class.copy]p2:
254 // A non-template constructor for class X is a copy constructor if [...]
255 if (isa<FunctionTemplateDecl>(*Con))
256 continue;
257
Douglas Gregor0d405db2010-07-01 20:59:04 +0000258 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
259 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000260 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
261 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000262 Found.push_back(std::make_pair(
263 const_cast<CXXConstructorDecl *>(Constructor),
264 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000265 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000266 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000267
268 return cast_or_null<CXXConstructorDecl>(
269 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000270}
271
Douglas Gregorb87786f2010-07-01 17:48:08 +0000272CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
273 ASTContext &Context = getASTContext();
274 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
275 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
276
277 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
278 DeclContext::lookup_const_iterator Op, OpEnd;
279 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
280 // C++ [class.copy]p9:
281 // A user-declared copy assignment operator is a non-static non-template
282 // member function of class X with exactly one parameter of type X, X&,
283 // const X&, volatile X& or const volatile X&.
284 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
285 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
286 continue;
287
288 const FunctionProtoType *FnType
289 = Method->getType()->getAs<FunctionProtoType>();
290 assert(FnType && "Overloaded operator has no prototype.");
291 // Don't assert on this; an invalid decl might have been left in the AST.
292 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
293 continue;
294
295 QualType ArgType = FnType->getArgType(0);
296 Qualifiers Quals;
297 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
298 ArgType = Ref->getPointeeType();
299 // If we have a const argument and we have a reference to a non-const,
300 // this function does not match.
301 if (ArgIsConst && !ArgType.isConstQualified())
302 continue;
303
304 Quals = ArgType.getQualifiers();
305 } else {
306 // By-value copy-assignment operators are treated like const X&
307 // copy-assignment operators.
308 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
309 }
310
311 if (!Context.hasSameUnqualifiedType(ArgType, Class))
312 continue;
313
314 // Save this copy-assignment operator. It might be "the one".
315 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
316 }
317
318 // Use a simplistic form of overload resolution to find the candidate.
319 return GetBestOverloadCandidateSimple(Found);
320}
321
Sebastian Redl64b45f72009-01-05 20:52:13 +0000322void
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000323CXXRecordDecl::addedMember(Decl *D) {
324 // Ignore friends and invalid declarations.
325 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000326 return;
327
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000328 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
329 if (FunTmpl)
330 D = FunTmpl->getTemplatedDecl();
331
Douglas Gregor6e3c7712010-09-27 23:16:44 +0000332 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
333 if (Method->isVirtual()) {
334 // C++ [dcl.init.aggr]p1:
335 // An aggregate is an array or a class with [...] no virtual functions.
336 data().Aggregate = false;
337
338 // C++ [class]p4:
339 // A POD-struct is an aggregate class...
340 data().PlainOldData = false;
Douglas Gregorcdbfa6c2010-09-27 23:31:14 +0000341
342 // Virtual functions make the class non-empty.
343 // FIXME: Standard ref?
344 data().Empty = false;
Douglas Gregor4a74df52010-09-27 23:39:06 +0000345
346 // C++ [class.virtual]p1:
347 // A class that declares or inherits a virtual function is called a
348 // polymorphic class.
349 data().Polymorphic = true;
Douglas Gregore10288c2010-09-28 00:00:00 +0000350
351 // None of the special member functions are trivial.
352 data().HasTrivialConstructor = false;
353 data().HasTrivialCopyConstructor = false;
354 data().HasTrivialCopyAssignment = false;
355 // FIXME: Destructor?
Douglas Gregor6e3c7712010-09-27 23:16:44 +0000356 }
357 }
358
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000359 if (D->isImplicit()) {
360 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
361 // If this is the implicit default constructor, note that we have now
362 // declared it.
363 if (Constructor->isDefaultConstructor())
364 data().DeclaredDefaultConstructor = true;
365 // If this is the implicit copy constructor, note that we have now
366 // declared it.
367 else if (Constructor->isCopyConstructor())
368 data().DeclaredCopyConstructor = true;
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000369 } else if (isa<CXXDestructorDecl>(D)) {
370 data().DeclaredDestructor = true;
371 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000372 // If this is the implicit copy constructor, note that we have now
373 // declared it.
374 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000375 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000376 data().DeclaredCopyAssignment = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000377 }
378
379 // Nothing else to do for implicitly-declared members.
380 return;
381 }
382
383 // Handle (user-declared) constructors.
384 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
385 // Note that we have a user-declared constructor.
386 data().UserDeclaredConstructor = true;
387
388 // Note that we have no need of an implicitly-declared default constructor.
389 data().DeclaredDefaultConstructor = true;
390
391 // C++ [dcl.init.aggr]p1:
392 // An aggregate is an array or a class (clause 9) with no
393 // user-declared constructors (12.1) [...].
394 data().Aggregate = false;
395
396 // C++ [class]p4:
397 // A POD-struct is an aggregate class [...]
398 data().PlainOldData = false;
399
400 // C++ [class.ctor]p5:
401 // A constructor is trivial if it is an implicitly-declared default
402 // constructor.
403 // FIXME: C++0x: don't do this for "= default" default constructors.
404 data().HasTrivialConstructor = false;
405
406 // Note when we have a user-declared copy constructor, which will
407 // suppress the implicit declaration of a copy constructor.
408 if (!FunTmpl && Constructor->isCopyConstructor()) {
409 data().UserDeclaredCopyConstructor = true;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000410 data().DeclaredCopyConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000411
412 // C++ [class.copy]p6:
413 // A copy constructor is trivial if it is implicitly declared.
414 // FIXME: C++0x: don't do this for "= default" copy constructors.
415 data().HasTrivialCopyConstructor = false;
416 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000417
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000418 return;
419 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000420
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000421 // Handle (user-declared) destructors.
422 if (isa<CXXDestructorDecl>(D)) {
423 data().DeclaredDestructor = true;
424 data().UserDeclaredDestructor = true;
Douglas Gregor6e3c7712010-09-27 23:16:44 +0000425
426 // C++ [class]p4:
427 // A POD-struct is an aggregate class that has [...] no user-defined
428 // destructor.
429 data().PlainOldData = false;
430
Douglas Gregore10288c2010-09-28 00:00:00 +0000431 // C++ [class.dtor]p3:
432 // A destructor is trivial if it is an implicitly-declared destructor and
433 // [...].
434 //
435 // FIXME: C++0x: don't do this for "= default" destructors
436 data().HasTrivialDestructor = false;
437
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000438 return;
439 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000440
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000441 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000442 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
443 if (Method->getOverloadedOperator() == OO_Equal) {
444 // We're interested specifically in copy assignment operators.
445 const FunctionProtoType *FnType
446 = Method->getType()->getAs<FunctionProtoType>();
447 assert(FnType && "Overloaded operator has no proto function type.");
448 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
449
450 // Copy assignment operators must be non-templates.
451 if (Method->getPrimaryTemplate() || FunTmpl)
452 return;
453
454 ASTContext &Context = getASTContext();
455 QualType ArgType = FnType->getArgType(0);
456 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
457 ArgType = Ref->getPointeeType();
458
459 ArgType = ArgType.getUnqualifiedType();
460 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
461 const_cast<CXXRecordDecl*>(this)));
462
463 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
464 return;
465
466 // This is a copy assignment operator.
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000467 // FIXME: Move assignment operators.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000468
469 // Suppress the implicit declaration of a copy constructor.
470 data().UserDeclaredCopyAssignment = true;
471 data().DeclaredCopyAssignment = true;
472
473 // C++ [class.copy]p11:
474 // A copy assignment operator is trivial if it is implicitly declared.
475 // FIXME: C++0x: don't do this for "= default" copy operators.
476 data().HasTrivialCopyAssignment = false;
477
478 // C++ [class]p4:
479 // A POD-struct is an aggregate class that [...] has no user-defined copy
480 // assignment operator [...].
481 data().PlainOldData = false;
482 }
Douglas Gregor22584312010-07-02 23:41:54 +0000483
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000484 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000485 }
Douglas Gregor6e3c7712010-09-27 23:16:44 +0000486
487 // Handle non-static data members.
488 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
489 // C++ [dcl.init.aggr]p1:
490 // An aggregate is an array or a class (clause 9) with [...] no
491 // private or protected non-static data members (clause 11).
492 //
493 // A POD must be an aggregate.
494 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
495 data().Aggregate = false;
496 data().PlainOldData = false;
497 }
498
499 // C++ [class]p9:
500 // A POD struct is a class that is both a trivial class and a
501 // standard-layout class, and has no non-static data members of type
502 // non-POD struct, non-POD union (or array of such types).
503 ASTContext &Context = getASTContext();
504 QualType T = Context.getBaseElementType(Field->getType());
505 if (!T->isPODType())
506 data().PlainOldData = false;
Douglas Gregore10288c2010-09-28 00:00:00 +0000507 if (T->isReferenceType())
508 data().HasTrivialConstructor = false;
509
510 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
511 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
512 if (FieldRec->getDefinition()) {
513 if (!FieldRec->hasTrivialConstructor())
514 data().HasTrivialConstructor = false;
515 if (!FieldRec->hasTrivialCopyConstructor())
516 data().HasTrivialCopyConstructor = false;
517 if (!FieldRec->hasTrivialCopyAssignment())
518 data().HasTrivialCopyAssignment = false;
519 if (!FieldRec->hasTrivialDestructor())
520 data().HasTrivialDestructor = false;
521 }
522 }
Douglas Gregorcdbfa6c2010-09-27 23:31:14 +0000523
524 // If this is not a zero-length bit-field, then the class is not empty.
525 if (data().Empty) {
526 if (!Field->getBitWidth())
527 data().Empty = false;
528 else if (!Field->getBitWidth()->isTypeDependent() &&
529 !Field->getBitWidth()->isValueDependent()) {
530 llvm::APSInt Bits;
531 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
532 if (!!Bits)
533 data().Empty = false;
534 }
535 }
Douglas Gregor6e3c7712010-09-27 23:16:44 +0000536 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000537}
538
John McCallb05b5f32010-03-15 09:07:48 +0000539static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
540 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000541 if (isa<UsingShadowDecl>(Conv))
542 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000543 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
544 T = ConvTemp->getTemplatedDecl()->getResultType();
545 else
546 T = cast<CXXConversionDecl>(Conv)->getConversionType();
547 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000548}
549
John McCallb05b5f32010-03-15 09:07:48 +0000550/// Collect the visible conversions of a base class.
551///
552/// \param Base a base class of the class we're considering
553/// \param InVirtual whether this base class is a virtual base (or a base
554/// of a virtual base)
555/// \param Access the access along the inheritance path to this base
556/// \param ParentHiddenTypes the conversions provided by the inheritors
557/// of this base
558/// \param Output the set to which to add conversions from non-virtual bases
559/// \param VOutput the set to which to add conversions from virtual bases
560/// \param HiddenVBaseCs the set of conversions which were hidden in a
561/// virtual base along some inheritance path
562static void CollectVisibleConversions(ASTContext &Context,
563 CXXRecordDecl *Record,
564 bool InVirtual,
565 AccessSpecifier Access,
566 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
567 UnresolvedSetImpl &Output,
568 UnresolvedSetImpl &VOutput,
569 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
570 // The set of types which have conversions in this class or its
571 // subclasses. As an optimization, we don't copy the derived set
572 // unless it might change.
573 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
574 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
575
576 // Collect the direct conversions and figure out which conversions
577 // will be hidden in the subclasses.
578 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
579 if (!Cs.empty()) {
580 HiddenTypesBuffer = ParentHiddenTypes;
581 HiddenTypes = &HiddenTypesBuffer;
582
583 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
584 bool Hidden =
585 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
586
587 // If this conversion is hidden and we're in a virtual base,
588 // remember that it's hidden along some inheritance path.
589 if (Hidden && InVirtual)
590 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
591
592 // If this conversion isn't hidden, add it to the appropriate output.
593 else if (!Hidden) {
594 AccessSpecifier IAccess
595 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
596
597 if (InVirtual)
598 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000599 else
John McCallb05b5f32010-03-15 09:07:48 +0000600 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000601 }
602 }
603 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000604
John McCallb05b5f32010-03-15 09:07:48 +0000605 // Collect information recursively from any base classes.
606 for (CXXRecordDecl::base_class_iterator
607 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
608 const RecordType *RT = I->getType()->getAs<RecordType>();
609 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000610
John McCallb05b5f32010-03-15 09:07:48 +0000611 AccessSpecifier BaseAccess
612 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
613 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000614
John McCallb05b5f32010-03-15 09:07:48 +0000615 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
616 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
617 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000618 }
John McCallb05b5f32010-03-15 09:07:48 +0000619}
Sebastian Redl9994a342009-10-25 17:03:50 +0000620
John McCallb05b5f32010-03-15 09:07:48 +0000621/// Collect the visible conversions of a class.
622///
623/// This would be extremely straightforward if it weren't for virtual
624/// bases. It might be worth special-casing that, really.
625static void CollectVisibleConversions(ASTContext &Context,
626 CXXRecordDecl *Record,
627 UnresolvedSetImpl &Output) {
628 // The collection of all conversions in virtual bases that we've
629 // found. These will be added to the output as long as they don't
630 // appear in the hidden-conversions set.
631 UnresolvedSet<8> VBaseCs;
632
633 // The set of conversions in virtual bases that we've determined to
634 // be hidden.
635 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
636
637 // The set of types hidden by classes derived from this one.
638 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
639
640 // Go ahead and collect the direct conversions and add them to the
641 // hidden-types set.
642 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
643 Output.append(Cs.begin(), Cs.end());
644 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
645 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
646
647 // Recursively collect conversions from base classes.
648 for (CXXRecordDecl::base_class_iterator
649 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
650 const RecordType *RT = I->getType()->getAs<RecordType>();
651 if (!RT) continue;
652
653 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
654 I->isVirtual(), I->getAccessSpecifier(),
655 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
656 }
657
658 // Add any unhidden conversions provided by virtual bases.
659 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
660 I != E; ++I) {
661 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
662 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000663 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000664}
665
666/// getVisibleConversionFunctions - get all conversion functions visible
667/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000668const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000669 // If root class, all conversions are visible.
670 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000671 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000672 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000673 if (data().ComputedVisibleConversions)
674 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000675 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000676 data().ComputedVisibleConversions = true;
677 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000678}
679
John McCall32daa422010-03-31 01:36:47 +0000680#ifndef NDEBUG
681void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000682 assert(ConvDecl->getDeclContext() == this &&
683 "conversion function does not belong to this record");
684
John McCall32daa422010-03-31 01:36:47 +0000685 ConvDecl = ConvDecl->getUnderlyingDecl();
686 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
687 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
688 } else {
689 assert(isa<CXXConversionDecl>(ConvDecl));
690 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000691}
John McCall32daa422010-03-31 01:36:47 +0000692#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000693
John McCall32daa422010-03-31 01:36:47 +0000694void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
695 // This operation is O(N) but extremely rare. Sema only uses it to
696 // remove UsingShadowDecls in a class that were followed by a direct
697 // declaration, e.g.:
698 // class A : B {
699 // using B::operator int;
700 // operator int();
701 // };
702 // This is uncommon by itself and even more uncommon in conjunction
703 // with sufficiently large numbers of directly-declared conversions
704 // that asymptotic behavior matters.
705
706 UnresolvedSetImpl &Convs = *getConversionFunctions();
707 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
708 if (Convs[I].getDecl() == ConvDecl) {
709 Convs.erase(I);
710 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
711 && "conversion was found multiple times in unresolved set");
712 return;
713 }
714 }
715
716 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000717}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000718
Douglas Gregorf6b11852009-10-08 15:14:33 +0000719CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000720 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000721 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
722
723 return 0;
724}
725
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000726MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
727 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
728}
729
Douglas Gregorf6b11852009-10-08 15:14:33 +0000730void
731CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
732 TemplateSpecializationKind TSK) {
733 assert(TemplateOrInstantiation.isNull() &&
734 "Previous template or instantiation?");
735 assert(!isa<ClassTemplateSpecializationDecl>(this));
736 TemplateOrInstantiation
737 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
738}
739
Anders Carlssonb13e3572009-12-07 06:33:48 +0000740TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
741 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000742 = dyn_cast<ClassTemplateSpecializationDecl>(this))
743 return Spec->getSpecializationKind();
744
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000745 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000746 return MSInfo->getTemplateSpecializationKind();
747
748 return TSK_Undeclared;
749}
750
751void
752CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
753 if (ClassTemplateSpecializationDecl *Spec
754 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
755 Spec->setSpecializationKind(TSK);
756 return;
757 }
758
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000759 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000760 MSInfo->setTemplateSpecializationKind(TSK);
761 return;
762 }
763
764 assert(false && "Not a class template or member class specialization");
765}
766
Douglas Gregor1d110e02010-07-01 14:13:13 +0000767CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
768 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000769 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000770
771 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000772 = Context.DeclarationNames.getCXXDestructorName(
773 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000774
John McCallc0bf4622010-02-23 00:48:20 +0000775 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000776 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000777 if (I == E)
778 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000780 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000781 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Anders Carlsson7267c162009-05-29 21:03:38 +0000783 return Dtor;
784}
785
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000786CXXMethodDecl *
787CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000788 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000789 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000790 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000791 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000792 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000793}
794
Douglas Gregor90916562009-09-29 18:16:17 +0000795bool CXXMethodDecl::isUsualDeallocationFunction() const {
796 if (getOverloadedOperator() != OO_Delete &&
797 getOverloadedOperator() != OO_Array_Delete)
798 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000799
800 // C++ [basic.stc.dynamic.deallocation]p2:
801 // A template instance is never a usual deallocation function,
802 // regardless of its signature.
803 if (getPrimaryTemplate())
804 return false;
805
Douglas Gregor90916562009-09-29 18:16:17 +0000806 // C++ [basic.stc.dynamic.deallocation]p2:
807 // If a class T has a member deallocation function named operator delete
808 // with exactly one parameter, then that function is a usual (non-placement)
809 // deallocation function. [...]
810 if (getNumParams() == 1)
811 return true;
812
813 // C++ [basic.stc.dynamic.deallocation]p2:
814 // [...] If class T does not declare such an operator delete but does
815 // declare a member deallocation function named operator delete with
816 // exactly two parameters, the second of which has type std::size_t (18.1),
817 // then this function is a usual deallocation function.
818 ASTContext &Context = getASTContext();
819 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000820 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
821 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000822 return false;
823
824 // This function is a usual deallocation function if there are no
825 // single-parameter deallocation functions of the same kind.
826 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
827 R.first != R.second; ++R.first) {
828 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
829 if (FD->getNumParams() == 1)
830 return false;
831 }
832
833 return true;
834}
835
Douglas Gregor06a9f362010-05-01 20:49:11 +0000836bool CXXMethodDecl::isCopyAssignmentOperator() const {
837 // C++0x [class.copy]p19:
838 // A user-declared copy assignment operator X::operator= is a non-static
839 // non-template member function of class X with exactly one parameter of
840 // type X, X&, const X&, volatile X& or const volatile X&.
841 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
842 /*non-static*/ isStatic() ||
843 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
844 /*exactly one parameter*/getNumParams() != 1)
845 return false;
846
847 QualType ParamType = getParamDecl(0)->getType();
848 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
849 ParamType = Ref->getPointeeType();
850
851 ASTContext &Context = getASTContext();
852 QualType ClassType
853 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
854 return Context.hasSameUnqualifiedType(ClassType, ParamType);
855}
856
Anders Carlsson05eb2442009-05-16 23:58:37 +0000857void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000858 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000859 assert(!MD->getParent()->isDependentContext() &&
860 "Can't add an overridden method to a class template!");
861
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000862 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000863}
864
865CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000866 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000867}
868
869CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000870 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000871}
872
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000873unsigned CXXMethodDecl::size_overridden_methods() const {
874 return getASTContext().overridden_methods_size(this);
875}
876
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000877QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000878 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
879 // If the member function is declared const, the type of this is const X*,
880 // if the member function is declared volatile, the type of this is
881 // volatile X*, and if the member function is declared const volatile,
882 // the type of this is const volatile X*.
883
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000884 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000885
John McCall3cb0ebd2010-03-10 03:28:59 +0000886 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000887 ClassTy = C.getQualifiedType(ClassTy,
888 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000889 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000890}
891
Eli Friedmand7d7f672009-12-06 20:50:05 +0000892bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000893 // If this function is a template instantiation, look at the template from
894 // which it was instantiated.
895 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
896 if (!CheckFn)
897 CheckFn = this;
898
Eli Friedmand7d7f672009-12-06 20:50:05 +0000899 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000900 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000901}
902
Douglas Gregor7ad83902008-11-05 04:29:56 +0000903CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000904CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +0000905 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000906 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000907 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
908 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
909 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000910{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000911}
912
913CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000914CXXBaseOrMemberInitializer(ASTContext &Context,
915 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000916 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000917 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
918 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
919 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000920{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000921}
922
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000923CXXBaseOrMemberInitializer::
924CXXBaseOrMemberInitializer(ASTContext &Context,
925 FieldDecl *Member, SourceLocation MemberLoc,
926 SourceLocation L, Expr *Init, SourceLocation R,
927 VarDecl **Indices,
928 unsigned NumIndices)
929 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000930 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
931 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000932{
933 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
934 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
935}
936
937CXXBaseOrMemberInitializer *
938CXXBaseOrMemberInitializer::Create(ASTContext &Context,
939 FieldDecl *Member,
940 SourceLocation MemberLoc,
941 SourceLocation L,
942 Expr *Init,
943 SourceLocation R,
944 VarDecl **Indices,
945 unsigned NumIndices) {
946 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
947 sizeof(VarDecl *) * NumIndices,
948 llvm::alignof<CXXBaseOrMemberInitializer>());
949 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
950 L, Init, R, Indices, NumIndices);
951}
952
Douglas Gregor802ab452009-12-02 22:36:29 +0000953TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
954 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000955 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000956 else
957 return TypeLoc();
958}
959
960Type *CXXBaseOrMemberInitializer::getBaseClass() {
961 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000962 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000963 else
964 return 0;
965}
966
967const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
968 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000969 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000970 else
971 return 0;
972}
973
974SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
975 if (isMemberInitializer())
976 return getMemberLocation();
977
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000978 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +0000979}
980
981SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
982 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000983}
984
Douglas Gregorb48fe382008-10-31 09:07:45 +0000985CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000986CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000987 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000988 QualType(), 0, false, false, false);
989}
990
991CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +0000992CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000993 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000994 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000995 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000996 bool isInline,
997 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000998 assert(NameInfo.getName().getNameKind()
999 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001000 "Name must refer to a constructor");
Abramo Bagnara25777432010-08-11 22:01:17 +00001001 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001002 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001003}
1004
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001005bool CXXConstructorDecl::isDefaultConstructor() const {
1006 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001007 // A default constructor for a class X is a constructor of class
1008 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001009 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001010 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001011}
1012
Mike Stump1eb44332009-09-09 15:08:12 +00001013bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001014CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001015 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001016 // A non-template constructor for class X is a copy constructor
1017 // if its first parameter is of type X&, const X&, volatile X& or
1018 // const volatile X&, and either there are no other parameters
1019 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001020 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001021 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001022 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001023 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001024 return false;
1025
1026 const ParmVarDecl *Param = getParamDecl(0);
1027
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001028 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +00001029 const LValueReferenceType *ParamRefType =
1030 Param->getType()->getAs<LValueReferenceType>();
1031 if (!ParamRefType)
1032 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001033
Douglas Gregorfd476482009-11-13 23:59:09 +00001034 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001035 ASTContext &Context = getASTContext();
1036
Douglas Gregorfd476482009-11-13 23:59:09 +00001037 CanQualType PointeeType
1038 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001039 CanQualType ClassTy
1040 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001041 if (PointeeType.getUnqualifiedType() != ClassTy)
1042 return false;
1043
John McCall0953e762009-09-24 19:53:00 +00001044 // FIXME: other qualifiers?
1045
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001046 // We have a copy constructor.
1047 TypeQuals = PointeeType.getCVRQualifiers();
1048 return true;
1049}
1050
Anders Carlssonfaccd722009-08-28 16:57:08 +00001051bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001052 // C++ [class.conv.ctor]p1:
1053 // A constructor declared without the function-specifier explicit
1054 // that can be called with a single parameter specifies a
1055 // conversion from the type of its first parameter to the type of
1056 // its class. Such a constructor is called a converting
1057 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001058 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001059 return false;
1060
Mike Stump1eb44332009-09-09 15:08:12 +00001061 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001062 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001063 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001064 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001065}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001066
Douglas Gregor66724ea2009-11-14 01:20:54 +00001067bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
1068 if ((getNumParams() < 1) ||
1069 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1070 (getPrimaryTemplate() == 0) ||
1071 (getDescribedFunctionTemplate() != 0))
1072 return false;
1073
1074 const ParmVarDecl *Param = getParamDecl(0);
1075
1076 ASTContext &Context = getASTContext();
1077 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1078
1079 // Strip off the lvalue reference, if any.
1080 if (CanQual<LValueReferenceType> ParamRefType
1081 = ParamType->getAs<LValueReferenceType>())
1082 ParamType = ParamRefType->getPointeeType();
1083
1084
1085 // Is it the same as our our class type?
1086 CanQualType ClassTy
1087 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1088 if (ParamType.getUnqualifiedType() != ClassTy)
1089 return false;
1090
1091 return true;
1092}
1093
Douglas Gregor42a552f2008-11-05 20:51:48 +00001094CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001095CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001096 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001097 QualType(), false, false);
1098}
1099
1100CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001101CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001102 const DeclarationNameInfo &NameInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001103 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001104 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001105 assert(NameInfo.getName().getNameKind()
1106 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001107 "Name must refer to a destructor");
Abramo Bagnara25777432010-08-11 22:01:17 +00001108 return new (C) CXXDestructorDecl(RD, NameInfo, T, isInline,
1109 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001110}
1111
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001112CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001113CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001114 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001115 QualType(), 0, false, false);
1116}
1117
1118CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001119CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001120 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001121 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001122 bool isInline, bool isExplicit) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001123 assert(NameInfo.getName().getNameKind()
1124 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001125 "Name must refer to a conversion function");
Abramo Bagnara25777432010-08-11 22:01:17 +00001126 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
1127 isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001128}
1129
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001130LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001131 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001132 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +00001133 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +00001134 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001135}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001136
1137UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1138 SourceLocation L,
1139 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001140 SourceRange QualifierRange,
1141 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001142 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001143 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001144 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001145 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1146 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001147 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001148 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001149}
1150
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001151NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1152 if (NamespaceAliasDecl *NA =
1153 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1154 return NA->getNamespace();
1155 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1156}
1157
Mike Stump1eb44332009-09-09 15:08:12 +00001158NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001159 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001160 SourceLocation AliasLoc,
1161 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001162 SourceRange QualifierRange,
1163 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001164 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001165 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001166 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1167 Namespace = NS->getOriginalNamespace();
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001168 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001169 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001170}
1171
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001172UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001173 SourceRange NNR, SourceLocation UL,
1174 NestedNameSpecifier* TargetNNS,
1175 const DeclarationNameInfo &NameInfo,
1176 bool IsTypeNameArg) {
1177 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001178}
1179
John McCall7ba107a2009-11-18 02:36:19 +00001180UnresolvedUsingValueDecl *
1181UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1182 SourceLocation UsingLoc,
1183 SourceRange TargetNNR,
1184 NestedNameSpecifier *TargetNNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001185 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001186 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001187 TargetNNR, TargetNNS, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001188}
1189
1190UnresolvedUsingTypenameDecl *
1191UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1192 SourceLocation UsingLoc,
1193 SourceLocation TypenameLoc,
1194 SourceRange TargetNNR,
1195 NestedNameSpecifier *TargetNNS,
1196 SourceLocation TargetNameLoc,
1197 DeclarationName TargetName) {
1198 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1199 TargetNNR, TargetNNS,
1200 TargetNameLoc,
1201 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001202}
1203
Anders Carlssonfb311762009-03-14 00:25:26 +00001204StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1205 SourceLocation L, Expr *AssertExpr,
1206 StringLiteral *Message) {
1207 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1208}
1209
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001210static const char *getAccessName(AccessSpecifier AS) {
1211 switch (AS) {
1212 default:
1213 case AS_none:
1214 assert("Invalid access specifier!");
1215 return 0;
1216 case AS_public:
1217 return "public";
1218 case AS_private:
1219 return "private";
1220 case AS_protected:
1221 return "protected";
1222 }
1223}
1224
1225const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1226 AccessSpecifier AS) {
1227 return DB << getAccessName(AS);
1228}