blob: 7cdffb7b44ec270d1a7fd8f7262a07f74f3f8e73 [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 Gregor9fe183a2010-09-28 19:45:33 +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 Gregor2cf9d652010-09-28 20:38:10 +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 Gregor85606eb2010-09-28 20:50:54 +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 Gregor2cf9d652010-09-28 20:38:10 +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 Gregor85606eb2010-09-28 20:50:54 +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 Gregor85606eb2010-09-28 20:50:54 +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 Gregor9fe183a2010-09-28 19:45:33 +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 Gregor2cf9d652010-09-28 20:38:10 +0000341
342 // Virtual functions make the class non-empty.
343 // FIXME: Standard ref?
344 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +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;
350
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 Gregor9fe183a2010-09-28 19:45:33 +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 Gregor2cf9d652010-09-28 20:38:10 +0000369 return;
370 }
371
372 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000373 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000374 return;
375 }
376
377 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000378 // If this is the implicit copy constructor, note that we have now
379 // declared it.
380 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000381 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000382 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000383 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000384 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000385
386 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000387 }
388
389 // Handle (user-declared) constructors.
390 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
391 // Note that we have a user-declared constructor.
392 data().UserDeclaredConstructor = true;
393
394 // Note that we have no need of an implicitly-declared default constructor.
395 data().DeclaredDefaultConstructor = true;
396
397 // C++ [dcl.init.aggr]p1:
398 // An aggregate is an array or a class (clause 9) with no
399 // user-declared constructors (12.1) [...].
400 data().Aggregate = false;
401
402 // C++ [class]p4:
403 // A POD-struct is an aggregate class [...]
404 data().PlainOldData = false;
405
406 // C++ [class.ctor]p5:
407 // A constructor is trivial if it is an implicitly-declared default
408 // constructor.
409 // FIXME: C++0x: don't do this for "= default" default constructors.
410 data().HasTrivialConstructor = false;
411
412 // Note when we have a user-declared copy constructor, which will
413 // suppress the implicit declaration of a copy constructor.
414 if (!FunTmpl && Constructor->isCopyConstructor()) {
415 data().UserDeclaredCopyConstructor = true;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000416 data().DeclaredCopyConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000417
418 // C++ [class.copy]p6:
419 // A copy constructor is trivial if it is implicitly declared.
420 // FIXME: C++0x: don't do this for "= default" copy constructors.
421 data().HasTrivialCopyConstructor = false;
422 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000423
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000424 return;
425 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000426
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000427 // Handle (user-declared) destructors.
428 if (isa<CXXDestructorDecl>(D)) {
429 data().DeclaredDestructor = true;
430 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000431
432 // C++ [class]p4:
433 // A POD-struct is an aggregate class that has [...] no user-defined
434 // destructor.
435 data().PlainOldData = false;
436
Douglas Gregor85606eb2010-09-28 20:50:54 +0000437 // C++ [class.dtor]p3:
438 // A destructor is trivial if it is an implicitly-declared destructor and
439 // [...].
440 //
441 // FIXME: C++0x: don't do this for "= default" destructors
442 data().HasTrivialDestructor = false;
443
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000444 return;
445 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000446
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000447 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000448 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
449 if (Method->getOverloadedOperator() == OO_Equal) {
450 // We're interested specifically in copy assignment operators.
451 const FunctionProtoType *FnType
452 = Method->getType()->getAs<FunctionProtoType>();
453 assert(FnType && "Overloaded operator has no proto function type.");
454 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
455
456 // Copy assignment operators must be non-templates.
457 if (Method->getPrimaryTemplate() || FunTmpl)
458 return;
459
460 ASTContext &Context = getASTContext();
461 QualType ArgType = FnType->getArgType(0);
462 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
463 ArgType = Ref->getPointeeType();
464
465 ArgType = ArgType.getUnqualifiedType();
466 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
467 const_cast<CXXRecordDecl*>(this)));
468
469 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
470 return;
471
472 // This is a copy assignment operator.
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000473 // FIXME: Move assignment operators.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000474
475 // Suppress the implicit declaration of a copy constructor.
476 data().UserDeclaredCopyAssignment = true;
477 data().DeclaredCopyAssignment = true;
478
479 // C++ [class.copy]p11:
480 // A copy assignment operator is trivial if it is implicitly declared.
481 // FIXME: C++0x: don't do this for "= default" copy operators.
482 data().HasTrivialCopyAssignment = false;
483
484 // C++ [class]p4:
485 // A POD-struct is an aggregate class that [...] has no user-defined copy
486 // assignment operator [...].
487 data().PlainOldData = false;
488 }
Douglas Gregor22584312010-07-02 23:41:54 +0000489
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000490 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000491 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000492
493 // Handle non-static data members.
494 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
495 // C++ [dcl.init.aggr]p1:
496 // An aggregate is an array or a class (clause 9) with [...] no
497 // private or protected non-static data members (clause 11).
498 //
499 // A POD must be an aggregate.
500 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
501 data().Aggregate = false;
502 data().PlainOldData = false;
503 }
504
505 // C++ [class]p9:
506 // A POD struct is a class that is both a trivial class and a
507 // standard-layout class, and has no non-static data members of type
508 // non-POD struct, non-POD union (or array of such types).
509 ASTContext &Context = getASTContext();
510 QualType T = Context.getBaseElementType(Field->getType());
511 if (!T->isPODType())
512 data().PlainOldData = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000513 if (T->isReferenceType())
514 data().HasTrivialConstructor = false;
515
516 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
517 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
518 if (FieldRec->getDefinition()) {
519 if (!FieldRec->hasTrivialConstructor())
520 data().HasTrivialConstructor = false;
521 if (!FieldRec->hasTrivialCopyConstructor())
522 data().HasTrivialCopyConstructor = false;
523 if (!FieldRec->hasTrivialCopyAssignment())
524 data().HasTrivialCopyAssignment = false;
525 if (!FieldRec->hasTrivialDestructor())
526 data().HasTrivialDestructor = false;
527 }
528 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000529
530 // If this is not a zero-length bit-field, then the class is not empty.
531 if (data().Empty) {
532 if (!Field->getBitWidth())
533 data().Empty = false;
534 else if (!Field->getBitWidth()->isTypeDependent() &&
535 !Field->getBitWidth()->isValueDependent()) {
536 llvm::APSInt Bits;
537 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
538 if (!!Bits)
539 data().Empty = false;
540 }
541 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000542 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000543}
544
John McCallb05b5f32010-03-15 09:07:48 +0000545static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
546 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000547 if (isa<UsingShadowDecl>(Conv))
548 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000549 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
550 T = ConvTemp->getTemplatedDecl()->getResultType();
551 else
552 T = cast<CXXConversionDecl>(Conv)->getConversionType();
553 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000554}
555
John McCallb05b5f32010-03-15 09:07:48 +0000556/// Collect the visible conversions of a base class.
557///
558/// \param Base a base class of the class we're considering
559/// \param InVirtual whether this base class is a virtual base (or a base
560/// of a virtual base)
561/// \param Access the access along the inheritance path to this base
562/// \param ParentHiddenTypes the conversions provided by the inheritors
563/// of this base
564/// \param Output the set to which to add conversions from non-virtual bases
565/// \param VOutput the set to which to add conversions from virtual bases
566/// \param HiddenVBaseCs the set of conversions which were hidden in a
567/// virtual base along some inheritance path
568static void CollectVisibleConversions(ASTContext &Context,
569 CXXRecordDecl *Record,
570 bool InVirtual,
571 AccessSpecifier Access,
572 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
573 UnresolvedSetImpl &Output,
574 UnresolvedSetImpl &VOutput,
575 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
576 // The set of types which have conversions in this class or its
577 // subclasses. As an optimization, we don't copy the derived set
578 // unless it might change.
579 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
580 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
581
582 // Collect the direct conversions and figure out which conversions
583 // will be hidden in the subclasses.
584 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
585 if (!Cs.empty()) {
586 HiddenTypesBuffer = ParentHiddenTypes;
587 HiddenTypes = &HiddenTypesBuffer;
588
589 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
590 bool Hidden =
591 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
592
593 // If this conversion is hidden and we're in a virtual base,
594 // remember that it's hidden along some inheritance path.
595 if (Hidden && InVirtual)
596 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
597
598 // If this conversion isn't hidden, add it to the appropriate output.
599 else if (!Hidden) {
600 AccessSpecifier IAccess
601 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
602
603 if (InVirtual)
604 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000605 else
John McCallb05b5f32010-03-15 09:07:48 +0000606 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000607 }
608 }
609 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000610
John McCallb05b5f32010-03-15 09:07:48 +0000611 // Collect information recursively from any base classes.
612 for (CXXRecordDecl::base_class_iterator
613 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
614 const RecordType *RT = I->getType()->getAs<RecordType>();
615 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000616
John McCallb05b5f32010-03-15 09:07:48 +0000617 AccessSpecifier BaseAccess
618 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
619 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000620
John McCallb05b5f32010-03-15 09:07:48 +0000621 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
622 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
623 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000624 }
John McCallb05b5f32010-03-15 09:07:48 +0000625}
Sebastian Redl9994a342009-10-25 17:03:50 +0000626
John McCallb05b5f32010-03-15 09:07:48 +0000627/// Collect the visible conversions of a class.
628///
629/// This would be extremely straightforward if it weren't for virtual
630/// bases. It might be worth special-casing that, really.
631static void CollectVisibleConversions(ASTContext &Context,
632 CXXRecordDecl *Record,
633 UnresolvedSetImpl &Output) {
634 // The collection of all conversions in virtual bases that we've
635 // found. These will be added to the output as long as they don't
636 // appear in the hidden-conversions set.
637 UnresolvedSet<8> VBaseCs;
638
639 // The set of conversions in virtual bases that we've determined to
640 // be hidden.
641 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
642
643 // The set of types hidden by classes derived from this one.
644 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
645
646 // Go ahead and collect the direct conversions and add them to the
647 // hidden-types set.
648 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
649 Output.append(Cs.begin(), Cs.end());
650 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
651 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
652
653 // Recursively collect conversions from base classes.
654 for (CXXRecordDecl::base_class_iterator
655 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
656 const RecordType *RT = I->getType()->getAs<RecordType>();
657 if (!RT) continue;
658
659 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
660 I->isVirtual(), I->getAccessSpecifier(),
661 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
662 }
663
664 // Add any unhidden conversions provided by virtual bases.
665 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
666 I != E; ++I) {
667 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
668 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000669 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000670}
671
672/// getVisibleConversionFunctions - get all conversion functions visible
673/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000674const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000675 // If root class, all conversions are visible.
676 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000677 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000678 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000679 if (data().ComputedVisibleConversions)
680 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000681 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000682 data().ComputedVisibleConversions = true;
683 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000684}
685
John McCall32daa422010-03-31 01:36:47 +0000686#ifndef NDEBUG
687void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000688 assert(ConvDecl->getDeclContext() == this &&
689 "conversion function does not belong to this record");
690
John McCall32daa422010-03-31 01:36:47 +0000691 ConvDecl = ConvDecl->getUnderlyingDecl();
692 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
693 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
694 } else {
695 assert(isa<CXXConversionDecl>(ConvDecl));
696 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000697}
John McCall32daa422010-03-31 01:36:47 +0000698#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000699
John McCall32daa422010-03-31 01:36:47 +0000700void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
701 // This operation is O(N) but extremely rare. Sema only uses it to
702 // remove UsingShadowDecls in a class that were followed by a direct
703 // declaration, e.g.:
704 // class A : B {
705 // using B::operator int;
706 // operator int();
707 // };
708 // This is uncommon by itself and even more uncommon in conjunction
709 // with sufficiently large numbers of directly-declared conversions
710 // that asymptotic behavior matters.
711
712 UnresolvedSetImpl &Convs = *getConversionFunctions();
713 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
714 if (Convs[I].getDecl() == ConvDecl) {
715 Convs.erase(I);
716 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
717 && "conversion was found multiple times in unresolved set");
718 return;
719 }
720 }
721
722 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000723}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000724
Douglas Gregorf6b11852009-10-08 15:14:33 +0000725CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000726 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000727 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
728
729 return 0;
730}
731
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000732MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
733 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
734}
735
Douglas Gregorf6b11852009-10-08 15:14:33 +0000736void
737CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
738 TemplateSpecializationKind TSK) {
739 assert(TemplateOrInstantiation.isNull() &&
740 "Previous template or instantiation?");
741 assert(!isa<ClassTemplateSpecializationDecl>(this));
742 TemplateOrInstantiation
743 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
744}
745
Anders Carlssonb13e3572009-12-07 06:33:48 +0000746TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
747 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000748 = dyn_cast<ClassTemplateSpecializationDecl>(this))
749 return Spec->getSpecializationKind();
750
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000751 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000752 return MSInfo->getTemplateSpecializationKind();
753
754 return TSK_Undeclared;
755}
756
757void
758CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
759 if (ClassTemplateSpecializationDecl *Spec
760 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
761 Spec->setSpecializationKind(TSK);
762 return;
763 }
764
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000765 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000766 MSInfo->setTemplateSpecializationKind(TSK);
767 return;
768 }
769
770 assert(false && "Not a class template or member class specialization");
771}
772
Douglas Gregor1d110e02010-07-01 14:13:13 +0000773CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
774 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000775 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000776
777 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000778 = Context.DeclarationNames.getCXXDestructorName(
779 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000780
John McCallc0bf4622010-02-23 00:48:20 +0000781 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000782 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000783 if (I == E)
784 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000786 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000787 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Anders Carlsson7267c162009-05-29 21:03:38 +0000789 return Dtor;
790}
791
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000792CXXMethodDecl *
793CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000794 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000795 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000796 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000797 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000798 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000799}
800
Douglas Gregor90916562009-09-29 18:16:17 +0000801bool CXXMethodDecl::isUsualDeallocationFunction() const {
802 if (getOverloadedOperator() != OO_Delete &&
803 getOverloadedOperator() != OO_Array_Delete)
804 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000805
806 // C++ [basic.stc.dynamic.deallocation]p2:
807 // A template instance is never a usual deallocation function,
808 // regardless of its signature.
809 if (getPrimaryTemplate())
810 return false;
811
Douglas Gregor90916562009-09-29 18:16:17 +0000812 // C++ [basic.stc.dynamic.deallocation]p2:
813 // If a class T has a member deallocation function named operator delete
814 // with exactly one parameter, then that function is a usual (non-placement)
815 // deallocation function. [...]
816 if (getNumParams() == 1)
817 return true;
818
819 // C++ [basic.stc.dynamic.deallocation]p2:
820 // [...] If class T does not declare such an operator delete but does
821 // declare a member deallocation function named operator delete with
822 // exactly two parameters, the second of which has type std::size_t (18.1),
823 // then this function is a usual deallocation function.
824 ASTContext &Context = getASTContext();
825 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000826 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
827 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000828 return false;
829
830 // This function is a usual deallocation function if there are no
831 // single-parameter deallocation functions of the same kind.
832 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
833 R.first != R.second; ++R.first) {
834 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
835 if (FD->getNumParams() == 1)
836 return false;
837 }
838
839 return true;
840}
841
Douglas Gregor06a9f362010-05-01 20:49:11 +0000842bool CXXMethodDecl::isCopyAssignmentOperator() const {
843 // C++0x [class.copy]p19:
844 // A user-declared copy assignment operator X::operator= is a non-static
845 // non-template member function of class X with exactly one parameter of
846 // type X, X&, const X&, volatile X& or const volatile X&.
847 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
848 /*non-static*/ isStatic() ||
849 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
850 /*exactly one parameter*/getNumParams() != 1)
851 return false;
852
853 QualType ParamType = getParamDecl(0)->getType();
854 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
855 ParamType = Ref->getPointeeType();
856
857 ASTContext &Context = getASTContext();
858 QualType ClassType
859 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
860 return Context.hasSameUnqualifiedType(ClassType, ParamType);
861}
862
Anders Carlsson05eb2442009-05-16 23:58:37 +0000863void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000864 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000865 assert(!MD->getParent()->isDependentContext() &&
866 "Can't add an overridden method to a class template!");
867
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000868 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000869}
870
871CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000872 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000873}
874
875CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000876 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000877}
878
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000879unsigned CXXMethodDecl::size_overridden_methods() const {
880 return getASTContext().overridden_methods_size(this);
881}
882
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000883QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000884 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
885 // If the member function is declared const, the type of this is const X*,
886 // if the member function is declared volatile, the type of this is
887 // volatile X*, and if the member function is declared const volatile,
888 // the type of this is const volatile X*.
889
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000890 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000891
John McCall3cb0ebd2010-03-10 03:28:59 +0000892 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000893 ClassTy = C.getQualifiedType(ClassTy,
894 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000895 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000896}
897
Eli Friedmand7d7f672009-12-06 20:50:05 +0000898bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000899 // If this function is a template instantiation, look at the template from
900 // which it was instantiated.
901 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
902 if (!CheckFn)
903 CheckFn = this;
904
Eli Friedmand7d7f672009-12-06 20:50:05 +0000905 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000906 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000907}
908
Douglas Gregor7ad83902008-11-05 04:29:56 +0000909CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000910CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +0000911 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000912 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000913 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
914 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
915 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000916{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000917}
918
919CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000920CXXBaseOrMemberInitializer(ASTContext &Context,
921 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000922 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000923 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
924 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
925 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000926{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000927}
928
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000929CXXBaseOrMemberInitializer::
930CXXBaseOrMemberInitializer(ASTContext &Context,
931 FieldDecl *Member, SourceLocation MemberLoc,
932 SourceLocation L, Expr *Init, SourceLocation R,
933 VarDecl **Indices,
934 unsigned NumIndices)
935 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000936 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
937 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000938{
939 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
940 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
941}
942
943CXXBaseOrMemberInitializer *
944CXXBaseOrMemberInitializer::Create(ASTContext &Context,
945 FieldDecl *Member,
946 SourceLocation MemberLoc,
947 SourceLocation L,
948 Expr *Init,
949 SourceLocation R,
950 VarDecl **Indices,
951 unsigned NumIndices) {
952 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
953 sizeof(VarDecl *) * NumIndices,
954 llvm::alignof<CXXBaseOrMemberInitializer>());
955 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
956 L, Init, R, Indices, NumIndices);
957}
958
Douglas Gregor802ab452009-12-02 22:36:29 +0000959TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
960 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000961 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000962 else
963 return TypeLoc();
964}
965
966Type *CXXBaseOrMemberInitializer::getBaseClass() {
967 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000968 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000969 else
970 return 0;
971}
972
973const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
974 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000975 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000976 else
977 return 0;
978}
979
980SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
981 if (isMemberInitializer())
982 return getMemberLocation();
983
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000984 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +0000985}
986
987SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
988 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000989}
990
Douglas Gregorb48fe382008-10-31 09:07:45 +0000991CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000992CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000993 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000994 QualType(), 0, false, false, false);
995}
996
997CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +0000998CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000999 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001000 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001001 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001002 bool isInline,
1003 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001004 assert(NameInfo.getName().getNameKind()
1005 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001006 "Name must refer to a constructor");
Abramo Bagnara25777432010-08-11 22:01:17 +00001007 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001008 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001009}
1010
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001011bool CXXConstructorDecl::isDefaultConstructor() const {
1012 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001013 // A default constructor for a class X is a constructor of class
1014 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001015 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001016 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001017}
1018
Mike Stump1eb44332009-09-09 15:08:12 +00001019bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001020CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001021 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001022 // A non-template constructor for class X is a copy constructor
1023 // if its first parameter is of type X&, const X&, volatile X& or
1024 // const volatile X&, and either there are no other parameters
1025 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001026 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001027 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001028 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001029 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001030 return false;
1031
1032 const ParmVarDecl *Param = getParamDecl(0);
1033
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001034 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +00001035 const LValueReferenceType *ParamRefType =
1036 Param->getType()->getAs<LValueReferenceType>();
1037 if (!ParamRefType)
1038 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001039
Douglas Gregorfd476482009-11-13 23:59:09 +00001040 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001041 ASTContext &Context = getASTContext();
1042
Douglas Gregorfd476482009-11-13 23:59:09 +00001043 CanQualType PointeeType
1044 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001045 CanQualType ClassTy
1046 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001047 if (PointeeType.getUnqualifiedType() != ClassTy)
1048 return false;
1049
John McCall0953e762009-09-24 19:53:00 +00001050 // FIXME: other qualifiers?
1051
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001052 // We have a copy constructor.
1053 TypeQuals = PointeeType.getCVRQualifiers();
1054 return true;
1055}
1056
Anders Carlssonfaccd722009-08-28 16:57:08 +00001057bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001058 // C++ [class.conv.ctor]p1:
1059 // A constructor declared without the function-specifier explicit
1060 // that can be called with a single parameter specifies a
1061 // conversion from the type of its first parameter to the type of
1062 // its class. Such a constructor is called a converting
1063 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001064 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001065 return false;
1066
Mike Stump1eb44332009-09-09 15:08:12 +00001067 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001068 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001069 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001070 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001071}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001072
Douglas Gregor66724ea2009-11-14 01:20:54 +00001073bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
1074 if ((getNumParams() < 1) ||
1075 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1076 (getPrimaryTemplate() == 0) ||
1077 (getDescribedFunctionTemplate() != 0))
1078 return false;
1079
1080 const ParmVarDecl *Param = getParamDecl(0);
1081
1082 ASTContext &Context = getASTContext();
1083 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1084
1085 // Strip off the lvalue reference, if any.
1086 if (CanQual<LValueReferenceType> ParamRefType
1087 = ParamType->getAs<LValueReferenceType>())
1088 ParamType = ParamRefType->getPointeeType();
1089
1090
1091 // Is it the same as our our class type?
1092 CanQualType ClassTy
1093 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1094 if (ParamType.getUnqualifiedType() != ClassTy)
1095 return false;
1096
1097 return true;
1098}
1099
Douglas Gregor42a552f2008-11-05 20:51:48 +00001100CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001101CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001102 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001103 QualType(), false, false);
1104}
1105
1106CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001107CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001108 const DeclarationNameInfo &NameInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001109 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001110 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001111 assert(NameInfo.getName().getNameKind()
1112 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001113 "Name must refer to a destructor");
Abramo Bagnara25777432010-08-11 22:01:17 +00001114 return new (C) CXXDestructorDecl(RD, NameInfo, T, isInline,
1115 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001116}
1117
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001118CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001119CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001120 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001121 QualType(), 0, false, false);
1122}
1123
1124CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001125CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001126 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001127 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001128 bool isInline, bool isExplicit) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001129 assert(NameInfo.getName().getNameKind()
1130 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001131 "Name must refer to a conversion function");
Abramo Bagnara25777432010-08-11 22:01:17 +00001132 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
1133 isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001134}
1135
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001136LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001137 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001138 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +00001139 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +00001140 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001141}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001142
1143UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1144 SourceLocation L,
1145 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001146 SourceRange QualifierRange,
1147 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001148 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001149 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001150 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001151 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1152 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001153 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001154 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001155}
1156
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001157NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1158 if (NamespaceAliasDecl *NA =
1159 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1160 return NA->getNamespace();
1161 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1162}
1163
Mike Stump1eb44332009-09-09 15:08:12 +00001164NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001165 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001166 SourceLocation AliasLoc,
1167 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001168 SourceRange QualifierRange,
1169 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001170 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001171 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001172 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1173 Namespace = NS->getOriginalNamespace();
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001174 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001175 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001176}
1177
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001178UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001179 SourceRange NNR, SourceLocation UL,
1180 NestedNameSpecifier* TargetNNS,
1181 const DeclarationNameInfo &NameInfo,
1182 bool IsTypeNameArg) {
1183 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001184}
1185
John McCall7ba107a2009-11-18 02:36:19 +00001186UnresolvedUsingValueDecl *
1187UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1188 SourceLocation UsingLoc,
1189 SourceRange TargetNNR,
1190 NestedNameSpecifier *TargetNNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001191 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001192 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001193 TargetNNR, TargetNNS, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001194}
1195
1196UnresolvedUsingTypenameDecl *
1197UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1198 SourceLocation UsingLoc,
1199 SourceLocation TypenameLoc,
1200 SourceRange TargetNNR,
1201 NestedNameSpecifier *TargetNNS,
1202 SourceLocation TargetNameLoc,
1203 DeclarationName TargetName) {
1204 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1205 TargetNNR, TargetNNS,
1206 TargetNameLoc,
1207 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001208}
1209
Anders Carlssonfb311762009-03-14 00:25:26 +00001210StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1211 SourceLocation L, Expr *AssertExpr,
1212 StringLiteral *Message) {
1213 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1214}
1215
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001216static const char *getAccessName(AccessSpecifier AS) {
1217 switch (AS) {
1218 default:
1219 case AS_none:
1220 assert("Invalid access specifier!");
1221 return 0;
1222 case AS_public:
1223 return "public";
1224 case AS_private:
1225 return "private";
1226 case AS_protected:
1227 return "protected";
1228 }
1229}
1230
1231const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1232 AccessSpecifier AS) {
1233 return DB << getAccessName(AS);
1234}