blob: 036292b6a56751b629729018822d151199237deb [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
Douglas Gregor21386642010-09-28 21:55:22 +0000322void CXXRecordDecl::markedVirtualFunctionPure() {
323 // C++ [class.abstract]p2:
324 // A class is abstract if it has at least one pure virtual function.
325 data().Abstract = true;
326}
327
328void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000329 // Ignore friends and invalid declarations.
330 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000331 return;
332
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000333 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
334 if (FunTmpl)
335 D = FunTmpl->getTemplatedDecl();
336
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000337 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
338 if (Method->isVirtual()) {
339 // C++ [dcl.init.aggr]p1:
340 // An aggregate is an array or a class with [...] no virtual functions.
341 data().Aggregate = false;
342
343 // C++ [class]p4:
344 // A POD-struct is an aggregate class...
345 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000346
347 // Virtual functions make the class non-empty.
348 // FIXME: Standard ref?
349 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000350
351 // C++ [class.virtual]p1:
352 // A class that declares or inherits a virtual function is called a
353 // polymorphic class.
354 data().Polymorphic = true;
355
356 // None of the special member functions are trivial.
357 data().HasTrivialConstructor = false;
358 data().HasTrivialCopyConstructor = false;
359 data().HasTrivialCopyAssignment = false;
360 // FIXME: Destructor?
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000361 }
362 }
363
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000364 if (D->isImplicit()) {
365 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
366 // If this is the implicit default constructor, note that we have now
367 // declared it.
368 if (Constructor->isDefaultConstructor())
369 data().DeclaredDefaultConstructor = true;
370 // If this is the implicit copy constructor, note that we have now
371 // declared it.
372 else if (Constructor->isCopyConstructor())
373 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000374 return;
375 }
376
377 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000378 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000379 return;
380 }
381
382 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000383 // If this is the implicit copy constructor, note that we have now
384 // declared it.
385 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000386 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000387 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000388 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000389 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000390
391 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000392 }
393
394 // Handle (user-declared) constructors.
395 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
396 // Note that we have a user-declared constructor.
397 data().UserDeclaredConstructor = true;
398
399 // Note that we have no need of an implicitly-declared default constructor.
400 data().DeclaredDefaultConstructor = true;
401
402 // C++ [dcl.init.aggr]p1:
403 // An aggregate is an array or a class (clause 9) with no
404 // user-declared constructors (12.1) [...].
405 data().Aggregate = false;
406
407 // C++ [class]p4:
408 // A POD-struct is an aggregate class [...]
409 data().PlainOldData = false;
410
411 // C++ [class.ctor]p5:
412 // A constructor is trivial if it is an implicitly-declared default
413 // constructor.
414 // FIXME: C++0x: don't do this for "= default" default constructors.
415 data().HasTrivialConstructor = false;
416
417 // Note when we have a user-declared copy constructor, which will
418 // suppress the implicit declaration of a copy constructor.
419 if (!FunTmpl && Constructor->isCopyConstructor()) {
420 data().UserDeclaredCopyConstructor = true;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000421 data().DeclaredCopyConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000422
423 // C++ [class.copy]p6:
424 // A copy constructor is trivial if it is implicitly declared.
425 // FIXME: C++0x: don't do this for "= default" copy constructors.
426 data().HasTrivialCopyConstructor = false;
427 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000428
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000429 return;
430 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000431
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000432 // Handle (user-declared) destructors.
433 if (isa<CXXDestructorDecl>(D)) {
434 data().DeclaredDestructor = true;
435 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000436
437 // C++ [class]p4:
438 // A POD-struct is an aggregate class that has [...] no user-defined
439 // destructor.
440 data().PlainOldData = false;
441
Douglas Gregor85606eb2010-09-28 20:50:54 +0000442 // C++ [class.dtor]p3:
443 // A destructor is trivial if it is an implicitly-declared destructor and
444 // [...].
445 //
446 // FIXME: C++0x: don't do this for "= default" destructors
447 data().HasTrivialDestructor = false;
448
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000449 return;
450 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000451
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000452 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000453 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
454 if (Method->getOverloadedOperator() == OO_Equal) {
455 // We're interested specifically in copy assignment operators.
456 const FunctionProtoType *FnType
457 = Method->getType()->getAs<FunctionProtoType>();
458 assert(FnType && "Overloaded operator has no proto function type.");
459 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
460
461 // Copy assignment operators must be non-templates.
462 if (Method->getPrimaryTemplate() || FunTmpl)
463 return;
464
465 ASTContext &Context = getASTContext();
466 QualType ArgType = FnType->getArgType(0);
467 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
468 ArgType = Ref->getPointeeType();
469
470 ArgType = ArgType.getUnqualifiedType();
471 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
472 const_cast<CXXRecordDecl*>(this)));
473
474 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
475 return;
476
477 // This is a copy assignment operator.
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000478 // FIXME: Move assignment operators.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000479
480 // Suppress the implicit declaration of a copy constructor.
481 data().UserDeclaredCopyAssignment = true;
482 data().DeclaredCopyAssignment = true;
483
484 // C++ [class.copy]p11:
485 // A copy assignment operator is trivial if it is implicitly declared.
486 // FIXME: C++0x: don't do this for "= default" copy operators.
487 data().HasTrivialCopyAssignment = false;
488
489 // C++ [class]p4:
490 // A POD-struct is an aggregate class that [...] has no user-defined copy
491 // assignment operator [...].
492 data().PlainOldData = false;
493 }
Douglas Gregor22584312010-07-02 23:41:54 +0000494
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000495 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000496 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000497
498 // Handle non-static data members.
499 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
500 // C++ [dcl.init.aggr]p1:
501 // An aggregate is an array or a class (clause 9) with [...] no
502 // private or protected non-static data members (clause 11).
503 //
504 // A POD must be an aggregate.
505 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
506 data().Aggregate = false;
507 data().PlainOldData = false;
508 }
509
510 // C++ [class]p9:
511 // A POD struct is a class that is both a trivial class and a
512 // standard-layout class, and has no non-static data members of type
513 // non-POD struct, non-POD union (or array of such types).
514 ASTContext &Context = getASTContext();
515 QualType T = Context.getBaseElementType(Field->getType());
516 if (!T->isPODType())
517 data().PlainOldData = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000518 if (T->isReferenceType())
519 data().HasTrivialConstructor = false;
520
521 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
522 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
523 if (FieldRec->getDefinition()) {
524 if (!FieldRec->hasTrivialConstructor())
525 data().HasTrivialConstructor = false;
526 if (!FieldRec->hasTrivialCopyConstructor())
527 data().HasTrivialCopyConstructor = false;
528 if (!FieldRec->hasTrivialCopyAssignment())
529 data().HasTrivialCopyAssignment = false;
530 if (!FieldRec->hasTrivialDestructor())
531 data().HasTrivialDestructor = false;
532 }
533 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000534
535 // If this is not a zero-length bit-field, then the class is not empty.
536 if (data().Empty) {
537 if (!Field->getBitWidth())
538 data().Empty = false;
539 else if (!Field->getBitWidth()->isTypeDependent() &&
540 !Field->getBitWidth()->isValueDependent()) {
541 llvm::APSInt Bits;
542 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
543 if (!!Bits)
544 data().Empty = false;
545 }
546 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000547 }
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000548}
549
John McCallb05b5f32010-03-15 09:07:48 +0000550static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
551 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000552 if (isa<UsingShadowDecl>(Conv))
553 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000554 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
555 T = ConvTemp->getTemplatedDecl()->getResultType();
556 else
557 T = cast<CXXConversionDecl>(Conv)->getConversionType();
558 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000559}
560
John McCallb05b5f32010-03-15 09:07:48 +0000561/// Collect the visible conversions of a base class.
562///
563/// \param Base a base class of the class we're considering
564/// \param InVirtual whether this base class is a virtual base (or a base
565/// of a virtual base)
566/// \param Access the access along the inheritance path to this base
567/// \param ParentHiddenTypes the conversions provided by the inheritors
568/// of this base
569/// \param Output the set to which to add conversions from non-virtual bases
570/// \param VOutput the set to which to add conversions from virtual bases
571/// \param HiddenVBaseCs the set of conversions which were hidden in a
572/// virtual base along some inheritance path
573static void CollectVisibleConversions(ASTContext &Context,
574 CXXRecordDecl *Record,
575 bool InVirtual,
576 AccessSpecifier Access,
577 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
578 UnresolvedSetImpl &Output,
579 UnresolvedSetImpl &VOutput,
580 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
581 // The set of types which have conversions in this class or its
582 // subclasses. As an optimization, we don't copy the derived set
583 // unless it might change.
584 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
585 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
586
587 // Collect the direct conversions and figure out which conversions
588 // will be hidden in the subclasses.
589 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
590 if (!Cs.empty()) {
591 HiddenTypesBuffer = ParentHiddenTypes;
592 HiddenTypes = &HiddenTypesBuffer;
593
594 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
595 bool Hidden =
596 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
597
598 // If this conversion is hidden and we're in a virtual base,
599 // remember that it's hidden along some inheritance path.
600 if (Hidden && InVirtual)
601 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
602
603 // If this conversion isn't hidden, add it to the appropriate output.
604 else if (!Hidden) {
605 AccessSpecifier IAccess
606 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
607
608 if (InVirtual)
609 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000610 else
John McCallb05b5f32010-03-15 09:07:48 +0000611 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000612 }
613 }
614 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000615
John McCallb05b5f32010-03-15 09:07:48 +0000616 // Collect information recursively from any base classes.
617 for (CXXRecordDecl::base_class_iterator
618 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
619 const RecordType *RT = I->getType()->getAs<RecordType>();
620 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000621
John McCallb05b5f32010-03-15 09:07:48 +0000622 AccessSpecifier BaseAccess
623 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
624 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000625
John McCallb05b5f32010-03-15 09:07:48 +0000626 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
627 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
628 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000629 }
John McCallb05b5f32010-03-15 09:07:48 +0000630}
Sebastian Redl9994a342009-10-25 17:03:50 +0000631
John McCallb05b5f32010-03-15 09:07:48 +0000632/// Collect the visible conversions of a class.
633///
634/// This would be extremely straightforward if it weren't for virtual
635/// bases. It might be worth special-casing that, really.
636static void CollectVisibleConversions(ASTContext &Context,
637 CXXRecordDecl *Record,
638 UnresolvedSetImpl &Output) {
639 // The collection of all conversions in virtual bases that we've
640 // found. These will be added to the output as long as they don't
641 // appear in the hidden-conversions set.
642 UnresolvedSet<8> VBaseCs;
643
644 // The set of conversions in virtual bases that we've determined to
645 // be hidden.
646 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
647
648 // The set of types hidden by classes derived from this one.
649 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
650
651 // Go ahead and collect the direct conversions and add them to the
652 // hidden-types set.
653 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
654 Output.append(Cs.begin(), Cs.end());
655 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
656 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
657
658 // Recursively collect conversions from base classes.
659 for (CXXRecordDecl::base_class_iterator
660 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
661 const RecordType *RT = I->getType()->getAs<RecordType>();
662 if (!RT) continue;
663
664 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
665 I->isVirtual(), I->getAccessSpecifier(),
666 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
667 }
668
669 // Add any unhidden conversions provided by virtual bases.
670 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
671 I != E; ++I) {
672 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
673 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000674 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000675}
676
677/// getVisibleConversionFunctions - get all conversion functions visible
678/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000679const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000680 // If root class, all conversions are visible.
681 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000682 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000683 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000684 if (data().ComputedVisibleConversions)
685 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000686 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000687 data().ComputedVisibleConversions = true;
688 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000689}
690
John McCall32daa422010-03-31 01:36:47 +0000691#ifndef NDEBUG
692void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000693 assert(ConvDecl->getDeclContext() == this &&
694 "conversion function does not belong to this record");
695
John McCall32daa422010-03-31 01:36:47 +0000696 ConvDecl = ConvDecl->getUnderlyingDecl();
697 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
698 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
699 } else {
700 assert(isa<CXXConversionDecl>(ConvDecl));
701 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000702}
John McCall32daa422010-03-31 01:36:47 +0000703#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000704
John McCall32daa422010-03-31 01:36:47 +0000705void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
706 // This operation is O(N) but extremely rare. Sema only uses it to
707 // remove UsingShadowDecls in a class that were followed by a direct
708 // declaration, e.g.:
709 // class A : B {
710 // using B::operator int;
711 // operator int();
712 // };
713 // This is uncommon by itself and even more uncommon in conjunction
714 // with sufficiently large numbers of directly-declared conversions
715 // that asymptotic behavior matters.
716
717 UnresolvedSetImpl &Convs = *getConversionFunctions();
718 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
719 if (Convs[I].getDecl() == ConvDecl) {
720 Convs.erase(I);
721 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
722 && "conversion was found multiple times in unresolved set");
723 return;
724 }
725 }
726
727 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000728}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000729
Douglas Gregorf6b11852009-10-08 15:14:33 +0000730CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000731 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000732 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
733
734 return 0;
735}
736
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000737MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
738 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
739}
740
Douglas Gregorf6b11852009-10-08 15:14:33 +0000741void
742CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
743 TemplateSpecializationKind TSK) {
744 assert(TemplateOrInstantiation.isNull() &&
745 "Previous template or instantiation?");
746 assert(!isa<ClassTemplateSpecializationDecl>(this));
747 TemplateOrInstantiation
748 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
749}
750
Anders Carlssonb13e3572009-12-07 06:33:48 +0000751TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
752 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000753 = dyn_cast<ClassTemplateSpecializationDecl>(this))
754 return Spec->getSpecializationKind();
755
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000756 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000757 return MSInfo->getTemplateSpecializationKind();
758
759 return TSK_Undeclared;
760}
761
762void
763CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
764 if (ClassTemplateSpecializationDecl *Spec
765 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
766 Spec->setSpecializationKind(TSK);
767 return;
768 }
769
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000770 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000771 MSInfo->setTemplateSpecializationKind(TSK);
772 return;
773 }
774
775 assert(false && "Not a class template or member class specialization");
776}
777
Douglas Gregor1d110e02010-07-01 14:13:13 +0000778CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
779 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000780 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000781
782 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000783 = Context.DeclarationNames.getCXXDestructorName(
784 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000785
John McCallc0bf4622010-02-23 00:48:20 +0000786 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000787 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000788 if (I == E)
789 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000791 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000792 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Anders Carlsson7267c162009-05-29 21:03:38 +0000794 return Dtor;
795}
796
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000797CXXMethodDecl *
798CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000799 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000800 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000801 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000802 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000803 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000804}
805
Douglas Gregor90916562009-09-29 18:16:17 +0000806bool CXXMethodDecl::isUsualDeallocationFunction() const {
807 if (getOverloadedOperator() != OO_Delete &&
808 getOverloadedOperator() != OO_Array_Delete)
809 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000810
811 // C++ [basic.stc.dynamic.deallocation]p2:
812 // A template instance is never a usual deallocation function,
813 // regardless of its signature.
814 if (getPrimaryTemplate())
815 return false;
816
Douglas Gregor90916562009-09-29 18:16:17 +0000817 // C++ [basic.stc.dynamic.deallocation]p2:
818 // If a class T has a member deallocation function named operator delete
819 // with exactly one parameter, then that function is a usual (non-placement)
820 // deallocation function. [...]
821 if (getNumParams() == 1)
822 return true;
823
824 // C++ [basic.stc.dynamic.deallocation]p2:
825 // [...] If class T does not declare such an operator delete but does
826 // declare a member deallocation function named operator delete with
827 // exactly two parameters, the second of which has type std::size_t (18.1),
828 // then this function is a usual deallocation function.
829 ASTContext &Context = getASTContext();
830 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000831 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
832 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000833 return false;
834
835 // This function is a usual deallocation function if there are no
836 // single-parameter deallocation functions of the same kind.
837 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
838 R.first != R.second; ++R.first) {
839 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
840 if (FD->getNumParams() == 1)
841 return false;
842 }
843
844 return true;
845}
846
Douglas Gregor06a9f362010-05-01 20:49:11 +0000847bool CXXMethodDecl::isCopyAssignmentOperator() const {
848 // C++0x [class.copy]p19:
849 // A user-declared copy assignment operator X::operator= is a non-static
850 // non-template member function of class X with exactly one parameter of
851 // type X, X&, const X&, volatile X& or const volatile X&.
852 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
853 /*non-static*/ isStatic() ||
854 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
855 /*exactly one parameter*/getNumParams() != 1)
856 return false;
857
858 QualType ParamType = getParamDecl(0)->getType();
859 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
860 ParamType = Ref->getPointeeType();
861
862 ASTContext &Context = getASTContext();
863 QualType ClassType
864 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
865 return Context.hasSameUnqualifiedType(ClassType, ParamType);
866}
867
Anders Carlsson05eb2442009-05-16 23:58:37 +0000868void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000869 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000870 assert(!MD->getParent()->isDependentContext() &&
871 "Can't add an overridden method to a class template!");
872
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000873 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000874}
875
876CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000877 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000878}
879
880CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000881 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000882}
883
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000884unsigned CXXMethodDecl::size_overridden_methods() const {
885 return getASTContext().overridden_methods_size(this);
886}
887
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000888QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000889 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
890 // If the member function is declared const, the type of this is const X*,
891 // if the member function is declared volatile, the type of this is
892 // volatile X*, and if the member function is declared const volatile,
893 // the type of this is const volatile X*.
894
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000895 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000896
John McCall3cb0ebd2010-03-10 03:28:59 +0000897 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000898 ClassTy = C.getQualifiedType(ClassTy,
899 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000900 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000901}
902
Eli Friedmand7d7f672009-12-06 20:50:05 +0000903bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000904 // If this function is a template instantiation, look at the template from
905 // which it was instantiated.
906 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
907 if (!CheckFn)
908 CheckFn = this;
909
Eli Friedmand7d7f672009-12-06 20:50:05 +0000910 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000911 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +0000912}
913
Douglas Gregor7ad83902008-11-05 04:29:56 +0000914CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000915CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +0000916 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000917 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000918 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
919 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
920 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000921{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000922}
923
924CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +0000925CXXBaseOrMemberInitializer(ASTContext &Context,
926 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000927 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000928 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
929 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
930 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +0000931{
Douglas Gregor7ad83902008-11-05 04:29:56 +0000932}
933
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000934CXXBaseOrMemberInitializer::
935CXXBaseOrMemberInitializer(ASTContext &Context,
936 FieldDecl *Member, SourceLocation MemberLoc,
937 SourceLocation L, Expr *Init, SourceLocation R,
938 VarDecl **Indices,
939 unsigned NumIndices)
940 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +0000941 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
942 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +0000943{
944 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
945 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
946}
947
948CXXBaseOrMemberInitializer *
949CXXBaseOrMemberInitializer::Create(ASTContext &Context,
950 FieldDecl *Member,
951 SourceLocation MemberLoc,
952 SourceLocation L,
953 Expr *Init,
954 SourceLocation R,
955 VarDecl **Indices,
956 unsigned NumIndices) {
957 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
958 sizeof(VarDecl *) * NumIndices,
959 llvm::alignof<CXXBaseOrMemberInitializer>());
960 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
961 L, Init, R, Indices, NumIndices);
962}
963
Douglas Gregor802ab452009-12-02 22:36:29 +0000964TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
965 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000966 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +0000967 else
968 return TypeLoc();
969}
970
971Type *CXXBaseOrMemberInitializer::getBaseClass() {
972 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000973 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000974 else
975 return 0;
976}
977
978const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
979 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +0000980 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +0000981 else
982 return 0;
983}
984
985SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
986 if (isMemberInitializer())
987 return getMemberLocation();
988
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000989 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +0000990}
991
992SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
993 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +0000994}
995
Douglas Gregorb48fe382008-10-31 09:07:45 +0000996CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000997CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000998 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000999 QualType(), 0, false, false, false);
1000}
1001
1002CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001003CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001004 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001005 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001006 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001007 bool isInline,
1008 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001009 assert(NameInfo.getName().getNameKind()
1010 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001011 "Name must refer to a constructor");
Abramo Bagnara25777432010-08-11 22:01:17 +00001012 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001013 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001014}
1015
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001016bool CXXConstructorDecl::isDefaultConstructor() const {
1017 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001018 // A default constructor for a class X is a constructor of class
1019 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001020 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001021 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001022}
1023
Mike Stump1eb44332009-09-09 15:08:12 +00001024bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001025CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001026 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001027 // A non-template constructor for class X is a copy constructor
1028 // if its first parameter is of type X&, const X&, volatile X& or
1029 // const volatile X&, and either there are no other parameters
1030 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001031 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001032 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001033 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001034 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001035 return false;
1036
1037 const ParmVarDecl *Param = getParamDecl(0);
1038
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001039 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +00001040 const LValueReferenceType *ParamRefType =
1041 Param->getType()->getAs<LValueReferenceType>();
1042 if (!ParamRefType)
1043 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001044
Douglas Gregorfd476482009-11-13 23:59:09 +00001045 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001046 ASTContext &Context = getASTContext();
1047
Douglas Gregorfd476482009-11-13 23:59:09 +00001048 CanQualType PointeeType
1049 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001050 CanQualType ClassTy
1051 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001052 if (PointeeType.getUnqualifiedType() != ClassTy)
1053 return false;
1054
John McCall0953e762009-09-24 19:53:00 +00001055 // FIXME: other qualifiers?
1056
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001057 // We have a copy constructor.
1058 TypeQuals = PointeeType.getCVRQualifiers();
1059 return true;
1060}
1061
Anders Carlssonfaccd722009-08-28 16:57:08 +00001062bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001063 // C++ [class.conv.ctor]p1:
1064 // A constructor declared without the function-specifier explicit
1065 // that can be called with a single parameter specifies a
1066 // conversion from the type of its first parameter to the type of
1067 // its class. Such a constructor is called a converting
1068 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001069 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001070 return false;
1071
Mike Stump1eb44332009-09-09 15:08:12 +00001072 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001073 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001074 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001075 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001076}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001077
Douglas Gregor66724ea2009-11-14 01:20:54 +00001078bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
1079 if ((getNumParams() < 1) ||
1080 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1081 (getPrimaryTemplate() == 0) ||
1082 (getDescribedFunctionTemplate() != 0))
1083 return false;
1084
1085 const ParmVarDecl *Param = getParamDecl(0);
1086
1087 ASTContext &Context = getASTContext();
1088 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1089
1090 // Strip off the lvalue reference, if any.
1091 if (CanQual<LValueReferenceType> ParamRefType
1092 = ParamType->getAs<LValueReferenceType>())
1093 ParamType = ParamRefType->getPointeeType();
1094
1095
1096 // Is it the same as our our class type?
1097 CanQualType ClassTy
1098 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1099 if (ParamType.getUnqualifiedType() != ClassTy)
1100 return false;
1101
1102 return true;
1103}
1104
Douglas Gregor42a552f2008-11-05 20:51:48 +00001105CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001106CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001107 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001108 QualType(), false, false);
1109}
1110
1111CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001112CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001113 const DeclarationNameInfo &NameInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001114 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001115 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001116 assert(NameInfo.getName().getNameKind()
1117 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001118 "Name must refer to a destructor");
Abramo Bagnara25777432010-08-11 22:01:17 +00001119 return new (C) CXXDestructorDecl(RD, NameInfo, T, isInline,
1120 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001121}
1122
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001123CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001124CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001125 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001126 QualType(), 0, false, false);
1127}
1128
1129CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001130CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001131 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001132 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001133 bool isInline, bool isExplicit) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001134 assert(NameInfo.getName().getNameKind()
1135 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001136 "Name must refer to a conversion function");
Abramo Bagnara25777432010-08-11 22:01:17 +00001137 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
1138 isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001139}
1140
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001141LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001142 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001143 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +00001144 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +00001145 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001146}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001147
1148UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1149 SourceLocation L,
1150 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001151 SourceRange QualifierRange,
1152 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001153 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001154 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001155 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001156 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1157 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001158 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001159 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001160}
1161
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001162NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1163 if (NamespaceAliasDecl *NA =
1164 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1165 return NA->getNamespace();
1166 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1167}
1168
Mike Stump1eb44332009-09-09 15:08:12 +00001169NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001170 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001171 SourceLocation AliasLoc,
1172 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001173 SourceRange QualifierRange,
1174 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001175 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001176 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001177 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1178 Namespace = NS->getOriginalNamespace();
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001179 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001180 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001181}
1182
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001183UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001184 SourceRange NNR, SourceLocation UL,
1185 NestedNameSpecifier* TargetNNS,
1186 const DeclarationNameInfo &NameInfo,
1187 bool IsTypeNameArg) {
1188 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001189}
1190
John McCall7ba107a2009-11-18 02:36:19 +00001191UnresolvedUsingValueDecl *
1192UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1193 SourceLocation UsingLoc,
1194 SourceRange TargetNNR,
1195 NestedNameSpecifier *TargetNNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001196 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001197 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001198 TargetNNR, TargetNNS, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001199}
1200
1201UnresolvedUsingTypenameDecl *
1202UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1203 SourceLocation UsingLoc,
1204 SourceLocation TypenameLoc,
1205 SourceRange TargetNNR,
1206 NestedNameSpecifier *TargetNNS,
1207 SourceLocation TargetNameLoc,
1208 DeclarationName TargetName) {
1209 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1210 TargetNNR, TargetNNS,
1211 TargetNameLoc,
1212 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001213}
1214
Anders Carlssonfb311762009-03-14 00:25:26 +00001215StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1216 SourceLocation L, Expr *AssertExpr,
1217 StringLiteral *Message) {
1218 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1219}
1220
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001221static const char *getAccessName(AccessSpecifier AS) {
1222 switch (AS) {
1223 default:
1224 case AS_none:
1225 assert("Invalid access specifier!");
1226 return 0;
1227 case AS_public:
1228 return "public";
1229 case AS_private:
1230 return "private";
1231 case AS_protected:
1232 return "protected";
1233 }
1234}
1235
1236const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1237 AccessSpecifier AS) {
1238 return DB << getAccessName(AS);
1239}