blob: 19caae5b7f2b37b12289a98041e364623afcd58e [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"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000017#include "clang/AST/CXXInheritance.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000018#include "clang/AST/Expr.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000019#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000020#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000021#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000022#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Decl Allocation/Deallocation Method Implementations
27//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000028
John McCall86ff3082010-02-04 22:26:26 +000029CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
30 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
Sebastian Redl64b45f72009-01-05 20:52:13 +000031 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Eli Friedman97c134e2009-08-15 22:23:00 +000032 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
33 Abstract(false), HasTrivialConstructor(true),
34 HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true),
Fariborz Jahanian62509212009-09-12 18:26:03 +000035 HasTrivialDestructor(true), ComputedVisibleConversions(false),
Douglas Gregor18274032010-07-03 00:47:00 +000036 DeclaredDefaultConstructor(false), DeclaredCopyConstructor(false),
Douglas Gregora376d102010-07-02 21:50:04 +000037 DeclaredCopyAssignment(false), DeclaredDestructor(false),
Fariborz Jahanian62509212009-09-12 18:26:03 +000038 Bases(0), NumBases(0), VBases(0), NumVBases(0),
John McCalld60e22e2010-03-12 01:19:31 +000039 Definition(D), FirstFriend(0) {
John McCall86ff3082010-02-04 22:26:26 +000040}
41
42CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
43 SourceLocation L, IdentifierInfo *Id,
44 CXXRecordDecl *PrevDecl,
45 SourceLocation TKL)
46 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
47 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000048 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000049
Ted Kremenek4b7c9832008-09-05 17:16:31 +000050CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
51 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +000052 SourceLocation TKL,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000053 CXXRecordDecl* PrevDecl,
54 bool DelayTypeCreation) {
Mike Stump1eb44332009-09-09 15:08:12 +000055 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000056 PrevDecl, TKL);
Mike Stump1eb44332009-09-09 15:08:12 +000057
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +000058 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000059 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +000060 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +000061 return R;
62}
63
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +000064CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, EmptyShell Empty) {
65 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(), 0, 0,
66 SourceLocation());
67}
68
Mike Stump1eb44332009-09-09 15:08:12 +000069void
Douglas Gregor2d5b7032010-02-11 01:30:34 +000070CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +000071 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +000072 ASTContext &C = getASTContext();
73
Mike Stump1eb44332009-09-09 15:08:12 +000074 // C++ [dcl.init.aggr]p1:
Douglas Gregor64bffa92008-11-05 16:20:31 +000075 // An aggregate is an array or a class (clause 9) with [...]
76 // no base classes [...].
John McCall86ff3082010-02-04 22:26:26 +000077 data().Aggregate = false;
Douglas Gregor64bffa92008-11-05 16:20:31 +000078
John McCall86ff3082010-02-04 22:26:26 +000079 if (data().Bases)
80 C.Deallocate(data().Bases);
Mike Stump1eb44332009-09-09 15:08:12 +000081
Anders Carlsson6f6de732010-03-29 05:13:12 +000082 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +000083 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +000084
85 // The virtual bases of this class.
86 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +000087
John McCall86ff3082010-02-04 22:26:26 +000088 data().Bases = new(C) CXXBaseSpecifier [NumBases];
89 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000090 for (unsigned i = 0; i < NumBases; ++i) {
John McCall86ff3082010-02-04 22:26:26 +000091 data().Bases[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000092 // Keep track of inherited vbases for this base class.
93 const CXXBaseSpecifier *Base = Bases[i];
94 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +000095 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +000096 if (BaseType->isDependentType())
97 continue;
98 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +000099 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000100
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000101 // C++ [dcl.init.aggr]p1:
102 // An aggregate is [...] a class with [...] no base classes [...].
103 data().Aggregate = false;
104
105 // C++ [class]p4:
106 // A POD-struct is an aggregate class...
107 data().PlainOldData = false;
108
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000109 // A class with a non-empty base class is not empty.
110 // FIXME: Standard ref?
111 if (!BaseClassDecl->isEmpty())
112 data().Empty = false;
113
Douglas Gregor85606eb2010-09-28 20:50:54 +0000114 // C++ [class.virtual]p1:
115 // A class that declares or inherits a virtual function is called a
116 // polymorphic class.
117 if (BaseClassDecl->isPolymorphic())
118 data().Polymorphic = true;
119
Anders Carlsson6f6de732010-03-29 05:13:12 +0000120 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000121 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000122 BaseClassDecl->vbases_begin(),
123 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000124 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000125 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000126 VBases.push_back(VBase);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000127 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000128
129 if (Base->isVirtual()) {
130 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000131 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Anders Carlsson6f6de732010-03-29 05:13:12 +0000132 VBases.push_back(Base);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000133
134 // C++0x [meta.unary.prop] is_empty:
135 // T is a class type, but not a union type, with ... no virtual base
136 // classes
137 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000138
139 // C++ [class.ctor]p5:
140 // A constructor is trivial if its class has no virtual base classes.
141 data().HasTrivialConstructor = false;
142
143 // C++ [class.copy]p6:
144 // A copy constructor is trivial if its class has no virtual base
145 // classes.
146 data().HasTrivialCopyConstructor = false;
147
148 // C++ [class.copy]p11:
149 // A copy assignment operator is trivial if its class has no virtual
150 // base classes.
151 data().HasTrivialCopyAssignment = false;
152 } else {
153 // C++ [class.ctor]p5:
154 // A constructor is trivial if all the direct base classes of its
155 // class have trivial constructors.
156 if (!BaseClassDecl->hasTrivialConstructor())
157 data().HasTrivialConstructor = false;
158
159 // C++ [class.copy]p6:
160 // A copy constructor is trivial if all the direct base classes of its
161 // class have trivial copy constructors.
162 if (!BaseClassDecl->hasTrivialCopyConstructor())
163 data().HasTrivialCopyConstructor = false;
164
165 // C++ [class.copy]p11:
166 // A copy assignment operator is trivial if all the direct base classes
167 // of its class have trivial copy assignment operators.
168 if (!BaseClassDecl->hasTrivialCopyAssignment())
169 data().HasTrivialCopyAssignment = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000170 }
Douglas Gregor85606eb2010-09-28 20:50:54 +0000171
172 // C++ [class.ctor]p3:
173 // A destructor is trivial if all the direct base classes of its class
174 // have trivial destructors.
175 if (!BaseClassDecl->hasTrivialDestructor())
176 data().HasTrivialDestructor = false;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000177 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000178
179 if (VBases.empty())
180 return;
181
182 // Create base specifier for any direct or indirect virtual bases.
183 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
184 data().NumVBases = VBases.size();
185 for (int I = 0, E = VBases.size(); I != E; ++I) {
Nick Lewycky56062202010-07-26 16:56:01 +0000186 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
187
Anders Carlsson6f6de732010-03-29 05:13:12 +0000188 // Skip dependent types; we can't do any checking on them now.
Nick Lewycky56062202010-07-26 16:56:01 +0000189 if (VBaseTypeInfo->getType()->isDependentType())
Anders Carlsson6f6de732010-03-29 05:13:12 +0000190 continue;
191
Nick Lewycky56062202010-07-26 16:56:01 +0000192 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
193 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000194
195 data().VBases[I] =
196 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000197 VBaseClassDecl->getTagKind() == TTK_Class,
Nick Lewycky56062202010-07-26 16:56:01 +0000198 VBases[I]->getAccessSpecifier(), VBaseTypeInfo);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000199 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000200}
201
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000202/// Callback function for CXXRecordDecl::forallBases that acknowledges
203/// that it saw a base class.
204static bool SawBase(const CXXRecordDecl *, void *) {
205 return true;
206}
207
208bool CXXRecordDecl::hasAnyDependentBases() const {
209 if (!isDependentContext())
210 return false;
211
212 return !forallBases(SawBase, 0);
213}
214
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000215bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
John McCall0953e762009-09-24 19:53:00 +0000216 return getCopyConstructor(Context, Qualifiers::Const) != 0;
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000217}
218
Douglas Gregor0d405db2010-07-01 20:59:04 +0000219/// \brief Perform a simplistic form of overload resolution that only considers
220/// cv-qualifiers on a single parameter, and return the best overload candidate
221/// (if there is one).
222static CXXMethodDecl *
223GetBestOverloadCandidateSimple(
224 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
225 if (Cands.empty())
226 return 0;
227 if (Cands.size() == 1)
228 return Cands[0].first;
229
230 unsigned Best = 0, N = Cands.size();
231 for (unsigned I = 1; I != N; ++I)
232 if (Cands[Best].second.isSupersetOf(Cands[I].second))
233 Best = I;
234
235 for (unsigned I = 1; I != N; ++I)
236 if (Cands[Best].second.isSupersetOf(Cands[I].second))
237 return 0;
238
239 return Cands[Best].first;
240}
241
Mike Stump1eb44332009-09-09 15:08:12 +0000242CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000243 unsigned TypeQuals) const{
Sebastian Redl64b45f72009-01-05 20:52:13 +0000244 QualType ClassType
245 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000246 DeclarationName ConstructorName
Douglas Gregor9e7d9de2008-12-15 21:24:18 +0000247 = Context.DeclarationNames.getCXXConstructorName(
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000248 Context.getCanonicalType(ClassType));
249 unsigned FoundTQs;
Douglas Gregor0d405db2010-07-01 20:59:04 +0000250 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000251 DeclContext::lookup_const_iterator Con, ConEnd;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000252 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
Douglas Gregorfdfab6b2008-12-23 21:31:30 +0000253 Con != ConEnd; ++Con) {
Douglas Gregord93bacf2009-09-04 14:46:39 +0000254 // C++ [class.copy]p2:
255 // A non-template constructor for class X is a copy constructor if [...]
256 if (isa<FunctionTemplateDecl>(*Con))
257 continue;
258
Douglas Gregor0d405db2010-07-01 20:59:04 +0000259 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
260 if (Constructor->isCopyConstructor(FoundTQs)) {
John McCall0953e762009-09-24 19:53:00 +0000261 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
262 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
Douglas Gregor0d405db2010-07-01 20:59:04 +0000263 Found.push_back(std::make_pair(
264 const_cast<CXXConstructorDecl *>(Constructor),
265 Qualifiers::fromCVRMask(FoundTQs)));
Fariborz Jahanian485f0872009-06-22 23:34:40 +0000266 }
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000267 }
Douglas Gregor0d405db2010-07-01 20:59:04 +0000268
269 return cast_or_null<CXXConstructorDecl>(
270 GetBestOverloadCandidateSimple(Found));
Douglas Gregor396b7cd2008-11-03 17:51:48 +0000271}
272
Douglas Gregorb87786f2010-07-01 17:48:08 +0000273CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
274 ASTContext &Context = getASTContext();
275 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
276 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
277
278 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
279 DeclContext::lookup_const_iterator Op, OpEnd;
280 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
281 // C++ [class.copy]p9:
282 // A user-declared copy assignment operator is a non-static non-template
283 // member function of class X with exactly one parameter of type X, X&,
284 // const X&, volatile X& or const volatile X&.
285 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
286 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
287 continue;
288
289 const FunctionProtoType *FnType
290 = Method->getType()->getAs<FunctionProtoType>();
291 assert(FnType && "Overloaded operator has no prototype.");
292 // Don't assert on this; an invalid decl might have been left in the AST.
293 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
294 continue;
295
296 QualType ArgType = FnType->getArgType(0);
297 Qualifiers Quals;
298 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
299 ArgType = Ref->getPointeeType();
300 // If we have a const argument and we have a reference to a non-const,
301 // this function does not match.
302 if (ArgIsConst && !ArgType.isConstQualified())
303 continue;
304
305 Quals = ArgType.getQualifiers();
306 } else {
307 // By-value copy-assignment operators are treated like const X&
308 // copy-assignment operators.
309 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
310 }
311
312 if (!Context.hasSameUnqualifiedType(ArgType, Class))
313 continue;
314
315 // Save this copy-assignment operator. It might be "the one".
316 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
317 }
318
319 // Use a simplistic form of overload resolution to find the candidate.
320 return GetBestOverloadCandidateSimple(Found);
321}
322
Douglas Gregor21386642010-09-28 21:55:22 +0000323void CXXRecordDecl::markedVirtualFunctionPure() {
324 // C++ [class.abstract]p2:
325 // A class is abstract if it has at least one pure virtual function.
326 data().Abstract = true;
327}
328
329void CXXRecordDecl::addedMember(Decl *D) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000330 // Ignore friends and invalid declarations.
331 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000332 return;
333
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000334 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
335 if (FunTmpl)
336 D = FunTmpl->getTemplatedDecl();
337
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000338 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
339 if (Method->isVirtual()) {
340 // C++ [dcl.init.aggr]p1:
341 // An aggregate is an array or a class with [...] no virtual functions.
342 data().Aggregate = false;
343
344 // C++ [class]p4:
345 // A POD-struct is an aggregate class...
346 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000347
348 // Virtual functions make the class non-empty.
349 // FIXME: Standard ref?
350 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000351
352 // C++ [class.virtual]p1:
353 // A class that declares or inherits a virtual function is called a
354 // polymorphic class.
355 data().Polymorphic = true;
356
357 // None of the special member functions are trivial.
358 data().HasTrivialConstructor = false;
359 data().HasTrivialCopyConstructor = false;
360 data().HasTrivialCopyAssignment = false;
361 // FIXME: Destructor?
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000362 }
363 }
364
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000365 if (D->isImplicit()) {
366 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
367 // If this is the implicit default constructor, note that we have now
368 // declared it.
369 if (Constructor->isDefaultConstructor())
370 data().DeclaredDefaultConstructor = true;
371 // If this is the implicit copy constructor, note that we have now
372 // declared it.
373 else if (Constructor->isCopyConstructor())
374 data().DeclaredCopyConstructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000375 return;
376 }
377
378 if (isa<CXXDestructorDecl>(D)) {
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000379 data().DeclaredDestructor = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000380 return;
381 }
382
383 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000384 // If this is the implicit copy constructor, note that we have now
385 // declared it.
386 // FIXME: Move constructors
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000387 if (Method->getOverloadedOperator() == OO_Equal)
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000388 data().DeclaredCopyAssignment = true;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000389 return;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000390 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000391
392 // Any other implicit declarations are handled like normal declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000393 }
394
395 // Handle (user-declared) constructors.
396 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
397 // Note that we have a user-declared constructor.
398 data().UserDeclaredConstructor = true;
399
400 // Note that we have no need of an implicitly-declared default constructor.
401 data().DeclaredDefaultConstructor = true;
402
403 // C++ [dcl.init.aggr]p1:
404 // An aggregate is an array or a class (clause 9) with no
405 // user-declared constructors (12.1) [...].
406 data().Aggregate = false;
407
408 // C++ [class]p4:
409 // A POD-struct is an aggregate class [...]
410 data().PlainOldData = false;
411
412 // C++ [class.ctor]p5:
413 // A constructor is trivial if it is an implicitly-declared default
414 // constructor.
415 // FIXME: C++0x: don't do this for "= default" default constructors.
416 data().HasTrivialConstructor = false;
417
418 // Note when we have a user-declared copy constructor, which will
419 // suppress the implicit declaration of a copy constructor.
420 if (!FunTmpl && Constructor->isCopyConstructor()) {
421 data().UserDeclaredCopyConstructor = true;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000422 data().DeclaredCopyConstructor = true;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000423
424 // C++ [class.copy]p6:
425 // A copy constructor is trivial if it is implicitly declared.
426 // FIXME: C++0x: don't do this for "= default" copy constructors.
427 data().HasTrivialCopyConstructor = false;
428 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000429
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000430 return;
431 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000432
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000433 // Handle (user-declared) destructors.
434 if (isa<CXXDestructorDecl>(D)) {
435 data().DeclaredDestructor = true;
436 data().UserDeclaredDestructor = true;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000437
438 // C++ [class]p4:
439 // A POD-struct is an aggregate class that has [...] no user-defined
440 // destructor.
441 data().PlainOldData = false;
442
Douglas Gregor85606eb2010-09-28 20:50:54 +0000443 // C++ [class.dtor]p3:
444 // A destructor is trivial if it is an implicitly-declared destructor and
445 // [...].
446 //
447 // FIXME: C++0x: don't do this for "= default" destructors
448 data().HasTrivialDestructor = false;
449
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000450 return;
451 }
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000452
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000453 // Handle (user-declared) member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000454 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
455 if (Method->getOverloadedOperator() == OO_Equal) {
456 // We're interested specifically in copy assignment operators.
457 const FunctionProtoType *FnType
458 = Method->getType()->getAs<FunctionProtoType>();
459 assert(FnType && "Overloaded operator has no proto function type.");
460 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
461
462 // Copy assignment operators must be non-templates.
463 if (Method->getPrimaryTemplate() || FunTmpl)
464 return;
465
466 ASTContext &Context = getASTContext();
467 QualType ArgType = FnType->getArgType(0);
468 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
469 ArgType = Ref->getPointeeType();
470
471 ArgType = ArgType.getUnqualifiedType();
472 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
473 const_cast<CXXRecordDecl*>(this)));
474
475 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
476 return;
477
478 // This is a copy assignment operator.
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000479 // FIXME: Move assignment operators.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000480
481 // Suppress the implicit declaration of a copy constructor.
482 data().UserDeclaredCopyAssignment = true;
483 data().DeclaredCopyAssignment = true;
484
485 // C++ [class.copy]p11:
486 // A copy assignment operator is trivial if it is implicitly declared.
487 // FIXME: C++0x: don't do this for "= default" copy operators.
488 data().HasTrivialCopyAssignment = false;
489
490 // C++ [class]p4:
491 // A POD-struct is an aggregate class that [...] has no user-defined copy
492 // assignment operator [...].
493 data().PlainOldData = false;
494 }
Douglas Gregor22584312010-07-02 23:41:54 +0000495
Douglas Gregore80622f2010-09-29 04:25:11 +0000496 // Keep the list of conversion functions up-to-date.
497 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
498 // We don't record specializations.
499 if (Conversion->getPrimaryTemplate())
500 return;
501
502 // FIXME: We intentionally don't use the decl's access here because it
503 // hasn't been set yet. That's really just a misdesign in Sema.
504
505 if (FunTmpl) {
506 if (FunTmpl->getPreviousDeclaration())
507 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
508 FunTmpl);
509 else
510 data().Conversions.addDecl(FunTmpl);
511 } else {
512 if (Conversion->getPreviousDeclaration())
513 data().Conversions.replace(Conversion->getPreviousDeclaration(),
514 Conversion);
515 else
516 data().Conversions.addDecl(Conversion);
517 }
518 }
519
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000520 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000521 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000522
523 // Handle non-static data members.
524 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
525 // C++ [dcl.init.aggr]p1:
526 // An aggregate is an array or a class (clause 9) with [...] no
527 // private or protected non-static data members (clause 11).
528 //
529 // A POD must be an aggregate.
530 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
531 data().Aggregate = false;
532 data().PlainOldData = false;
533 }
534
535 // C++ [class]p9:
536 // A POD struct is a class that is both a trivial class and a
537 // standard-layout class, and has no non-static data members of type
538 // non-POD struct, non-POD union (or array of such types).
539 ASTContext &Context = getASTContext();
540 QualType T = Context.getBaseElementType(Field->getType());
541 if (!T->isPODType())
542 data().PlainOldData = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000543 if (T->isReferenceType())
544 data().HasTrivialConstructor = false;
545
546 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
547 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
548 if (FieldRec->getDefinition()) {
549 if (!FieldRec->hasTrivialConstructor())
550 data().HasTrivialConstructor = false;
551 if (!FieldRec->hasTrivialCopyConstructor())
552 data().HasTrivialCopyConstructor = false;
553 if (!FieldRec->hasTrivialCopyAssignment())
554 data().HasTrivialCopyAssignment = false;
555 if (!FieldRec->hasTrivialDestructor())
556 data().HasTrivialDestructor = false;
557 }
558 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000559
560 // If this is not a zero-length bit-field, then the class is not empty.
561 if (data().Empty) {
562 if (!Field->getBitWidth())
563 data().Empty = false;
564 else if (!Field->getBitWidth()->isTypeDependent() &&
565 !Field->getBitWidth()->isValueDependent()) {
566 llvm::APSInt Bits;
567 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
568 if (!!Bits)
569 data().Empty = false;
570 }
571 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000572 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000573
574 // Handle using declarations of conversion functions.
575 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
576 if (Shadow->getDeclName().getNameKind()
577 == DeclarationName::CXXConversionFunctionName)
578 data().Conversions.addDecl(Shadow, Shadow->getAccess());
Douglas Gregor030ff0c2008-10-31 20:25:05 +0000579}
580
John McCallb05b5f32010-03-15 09:07:48 +0000581static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
582 QualType T;
John McCall32daa422010-03-31 01:36:47 +0000583 if (isa<UsingShadowDecl>(Conv))
584 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +0000585 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
586 T = ConvTemp->getTemplatedDecl()->getResultType();
587 else
588 T = cast<CXXConversionDecl>(Conv)->getConversionType();
589 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +0000590}
591
John McCallb05b5f32010-03-15 09:07:48 +0000592/// Collect the visible conversions of a base class.
593///
594/// \param Base a base class of the class we're considering
595/// \param InVirtual whether this base class is a virtual base (or a base
596/// of a virtual base)
597/// \param Access the access along the inheritance path to this base
598/// \param ParentHiddenTypes the conversions provided by the inheritors
599/// of this base
600/// \param Output the set to which to add conversions from non-virtual bases
601/// \param VOutput the set to which to add conversions from virtual bases
602/// \param HiddenVBaseCs the set of conversions which were hidden in a
603/// virtual base along some inheritance path
604static void CollectVisibleConversions(ASTContext &Context,
605 CXXRecordDecl *Record,
606 bool InVirtual,
607 AccessSpecifier Access,
608 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
609 UnresolvedSetImpl &Output,
610 UnresolvedSetImpl &VOutput,
611 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
612 // The set of types which have conversions in this class or its
613 // subclasses. As an optimization, we don't copy the derived set
614 // unless it might change.
615 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
616 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
617
618 // Collect the direct conversions and figure out which conversions
619 // will be hidden in the subclasses.
620 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
621 if (!Cs.empty()) {
622 HiddenTypesBuffer = ParentHiddenTypes;
623 HiddenTypes = &HiddenTypesBuffer;
624
625 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
626 bool Hidden =
627 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
628
629 // If this conversion is hidden and we're in a virtual base,
630 // remember that it's hidden along some inheritance path.
631 if (Hidden && InVirtual)
632 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
633
634 // If this conversion isn't hidden, add it to the appropriate output.
635 else if (!Hidden) {
636 AccessSpecifier IAccess
637 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
638
639 if (InVirtual)
640 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +0000641 else
John McCallb05b5f32010-03-15 09:07:48 +0000642 Output.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000643 }
644 }
645 }
Sebastian Redl9994a342009-10-25 17:03:50 +0000646
John McCallb05b5f32010-03-15 09:07:48 +0000647 // Collect information recursively from any base classes.
648 for (CXXRecordDecl::base_class_iterator
649 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
650 const RecordType *RT = I->getType()->getAs<RecordType>();
651 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +0000652
John McCallb05b5f32010-03-15 09:07:48 +0000653 AccessSpecifier BaseAccess
654 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
655 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +0000656
John McCallb05b5f32010-03-15 09:07:48 +0000657 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
658 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
659 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +0000660 }
John McCallb05b5f32010-03-15 09:07:48 +0000661}
Sebastian Redl9994a342009-10-25 17:03:50 +0000662
John McCallb05b5f32010-03-15 09:07:48 +0000663/// Collect the visible conversions of a class.
664///
665/// This would be extremely straightforward if it weren't for virtual
666/// bases. It might be worth special-casing that, really.
667static void CollectVisibleConversions(ASTContext &Context,
668 CXXRecordDecl *Record,
669 UnresolvedSetImpl &Output) {
670 // The collection of all conversions in virtual bases that we've
671 // found. These will be added to the output as long as they don't
672 // appear in the hidden-conversions set.
673 UnresolvedSet<8> VBaseCs;
674
675 // The set of conversions in virtual bases that we've determined to
676 // be hidden.
677 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
678
679 // The set of types hidden by classes derived from this one.
680 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
681
682 // Go ahead and collect the direct conversions and add them to the
683 // hidden-types set.
684 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
685 Output.append(Cs.begin(), Cs.end());
686 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
687 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
688
689 // Recursively collect conversions from base classes.
690 for (CXXRecordDecl::base_class_iterator
691 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
692 const RecordType *RT = I->getType()->getAs<RecordType>();
693 if (!RT) continue;
694
695 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
696 I->isVirtual(), I->getAccessSpecifier(),
697 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
698 }
699
700 // Add any unhidden conversions provided by virtual bases.
701 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
702 I != E; ++I) {
703 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
704 Output.addDecl(I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +0000705 }
Fariborz Jahanian62509212009-09-12 18:26:03 +0000706}
707
708/// getVisibleConversionFunctions - get all conversion functions visible
709/// in current class; including conversion function templates.
John McCalleec51cf2010-01-20 00:46:10 +0000710const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
Fariborz Jahanian62509212009-09-12 18:26:03 +0000711 // If root class, all conversions are visible.
712 if (bases_begin() == bases_end())
John McCall86ff3082010-02-04 22:26:26 +0000713 return &data().Conversions;
Fariborz Jahanian62509212009-09-12 18:26:03 +0000714 // If visible conversion list is already evaluated, return it.
John McCall86ff3082010-02-04 22:26:26 +0000715 if (data().ComputedVisibleConversions)
716 return &data().VisibleConversions;
John McCallb05b5f32010-03-15 09:07:48 +0000717 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
John McCall86ff3082010-02-04 22:26:26 +0000718 data().ComputedVisibleConversions = true;
719 return &data().VisibleConversions;
Fariborz Jahanian53462782009-09-11 21:44:33 +0000720}
721
John McCall32daa422010-03-31 01:36:47 +0000722#ifndef NDEBUG
723void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) {
John McCallb05b5f32010-03-15 09:07:48 +0000724 assert(ConvDecl->getDeclContext() == this &&
725 "conversion function does not belong to this record");
726
John McCall32daa422010-03-31 01:36:47 +0000727 ConvDecl = ConvDecl->getUnderlyingDecl();
728 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) {
729 assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl()));
730 } else {
731 assert(isa<CXXConversionDecl>(ConvDecl));
732 }
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000733}
John McCall32daa422010-03-31 01:36:47 +0000734#endif
Douglas Gregor2f1bc522008-11-07 20:08:42 +0000735
John McCall32daa422010-03-31 01:36:47 +0000736void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
737 // This operation is O(N) but extremely rare. Sema only uses it to
738 // remove UsingShadowDecls in a class that were followed by a direct
739 // declaration, e.g.:
740 // class A : B {
741 // using B::operator int;
742 // operator int();
743 // };
744 // This is uncommon by itself and even more uncommon in conjunction
745 // with sufficiently large numbers of directly-declared conversions
746 // that asymptotic behavior matters.
747
748 UnresolvedSetImpl &Convs = *getConversionFunctions();
749 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
750 if (Convs[I].getDecl() == ConvDecl) {
751 Convs.erase(I);
752 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
753 && "conversion was found multiple times in unresolved set");
754 return;
755 }
756 }
757
758 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000759}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +0000760
Douglas Gregorf6b11852009-10-08 15:14:33 +0000761CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000762 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000763 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
764
765 return 0;
766}
767
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000768MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
769 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
770}
771
Douglas Gregorf6b11852009-10-08 15:14:33 +0000772void
773CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
774 TemplateSpecializationKind TSK) {
775 assert(TemplateOrInstantiation.isNull() &&
776 "Previous template or instantiation?");
777 assert(!isa<ClassTemplateSpecializationDecl>(this));
778 TemplateOrInstantiation
779 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
780}
781
Anders Carlssonb13e3572009-12-07 06:33:48 +0000782TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
783 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +0000784 = dyn_cast<ClassTemplateSpecializationDecl>(this))
785 return Spec->getSpecializationKind();
786
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000787 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000788 return MSInfo->getTemplateSpecializationKind();
789
790 return TSK_Undeclared;
791}
792
793void
794CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
795 if (ClassTemplateSpecializationDecl *Spec
796 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
797 Spec->setSpecializationKind(TSK);
798 return;
799 }
800
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000801 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +0000802 MSInfo->setTemplateSpecializationKind(TSK);
803 return;
804 }
805
806 assert(false && "Not a class template or member class specialization");
807}
808
Douglas Gregor1d110e02010-07-01 14:13:13 +0000809CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
810 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +0000811 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000812
813 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +0000814 = Context.DeclarationNames.getCXXDestructorName(
815 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +0000816
John McCallc0bf4622010-02-23 00:48:20 +0000817 DeclContext::lookup_const_iterator I, E;
Mike Stump1eb44332009-09-09 15:08:12 +0000818 llvm::tie(I, E) = lookup(Name);
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000819 if (I == E)
820 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Anders Carlsson5ec02ae2009-12-02 17:15:43 +0000822 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
Anders Carlsson7267c162009-05-29 21:03:38 +0000823 assert(++I == E && "Found more than one destructor!");
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Anders Carlsson7267c162009-05-29 21:03:38 +0000825 return Dtor;
826}
827
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000828void CXXRecordDecl::completeDefinition() {
829 completeDefinition(0);
830}
831
832void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
833 RecordDecl::completeDefinition();
834
835 // If the class may be abstract (but hasn't been marked as such), check for
836 // any pure final overriders.
837 if (mayBeAbstract()) {
838 CXXFinalOverriderMap MyFinalOverriders;
839 if (!FinalOverriders) {
840 getFinalOverriders(MyFinalOverriders);
841 FinalOverriders = &MyFinalOverriders;
842 }
843
844 bool Done = false;
845 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
846 MEnd = FinalOverriders->end();
847 M != MEnd && !Done; ++M) {
848 for (OverridingMethods::iterator SO = M->second.begin(),
849 SOEnd = M->second.end();
850 SO != SOEnd && !Done; ++SO) {
851 assert(SO->second.size() > 0 &&
852 "All virtual functions have overridding virtual functions");
853
854 // C++ [class.abstract]p4:
855 // A class is abstract if it contains or inherits at least one
856 // pure virtual function for which the final overrider is pure
857 // virtual.
858 if (SO->second.front().Method->isPure()) {
859 data().Abstract = true;
860 Done = true;
861 break;
862 }
863 }
864 }
865 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000866
867 // Set access bits correctly on the directly-declared conversions.
868 for (UnresolvedSetIterator I = data().Conversions.begin(),
869 E = data().Conversions.end();
870 I != E; ++I)
871 data().Conversions.setAccess(I, (*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +0000872}
873
874bool CXXRecordDecl::mayBeAbstract() const {
875 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
876 isDependentContext())
877 return false;
878
879 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
880 BEnd = bases_end();
881 B != BEnd; ++B) {
882 CXXRecordDecl *BaseDecl
883 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
884 if (BaseDecl->isAbstract())
885 return true;
886 }
887
888 return false;
889}
890
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000891CXXMethodDecl *
892CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000893 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +0000894 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000895 bool isStatic, StorageClass SCAsWritten, bool isInline) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000896 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000897 isStatic, SCAsWritten, isInline);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000898}
899
Douglas Gregor90916562009-09-29 18:16:17 +0000900bool CXXMethodDecl::isUsualDeallocationFunction() const {
901 if (getOverloadedOperator() != OO_Delete &&
902 getOverloadedOperator() != OO_Array_Delete)
903 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +0000904
905 // C++ [basic.stc.dynamic.deallocation]p2:
906 // A template instance is never a usual deallocation function,
907 // regardless of its signature.
908 if (getPrimaryTemplate())
909 return false;
910
Douglas Gregor90916562009-09-29 18:16:17 +0000911 // C++ [basic.stc.dynamic.deallocation]p2:
912 // If a class T has a member deallocation function named operator delete
913 // with exactly one parameter, then that function is a usual (non-placement)
914 // deallocation function. [...]
915 if (getNumParams() == 1)
916 return true;
917
918 // C++ [basic.stc.dynamic.deallocation]p2:
919 // [...] If class T does not declare such an operator delete but does
920 // declare a member deallocation function named operator delete with
921 // exactly two parameters, the second of which has type std::size_t (18.1),
922 // then this function is a usual deallocation function.
923 ASTContext &Context = getASTContext();
924 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +0000925 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
926 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +0000927 return false;
928
929 // This function is a usual deallocation function if there are no
930 // single-parameter deallocation functions of the same kind.
931 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
932 R.first != R.second; ++R.first) {
933 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
934 if (FD->getNumParams() == 1)
935 return false;
936 }
937
938 return true;
939}
940
Douglas Gregor06a9f362010-05-01 20:49:11 +0000941bool CXXMethodDecl::isCopyAssignmentOperator() const {
942 // C++0x [class.copy]p19:
943 // A user-declared copy assignment operator X::operator= is a non-static
944 // non-template member function of class X with exactly one parameter of
945 // type X, X&, const X&, volatile X& or const volatile X&.
946 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
947 /*non-static*/ isStatic() ||
948 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
949 /*exactly one parameter*/getNumParams() != 1)
950 return false;
951
952 QualType ParamType = getParamDecl(0)->getType();
953 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
954 ParamType = Ref->getPointeeType();
955
956 ASTContext &Context = getASTContext();
957 QualType ClassType
958 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
959 return Context.hasSameUnqualifiedType(ClassType, ParamType);
960}
961
Anders Carlsson05eb2442009-05-16 23:58:37 +0000962void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +0000963 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +0000964 assert(!MD->getParent()->isDependentContext() &&
965 "Can't add an overridden method to a class template!");
966
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000967 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000968}
969
970CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000971 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000972}
973
974CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000975 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +0000976}
977
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000978unsigned CXXMethodDecl::size_overridden_methods() const {
979 return getASTContext().overridden_methods_size(this);
980}
981
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000982QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +0000983 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
984 // If the member function is declared const, the type of this is const X*,
985 // if the member function is declared volatile, the type of this is
986 // volatile X*, and if the member function is declared const volatile,
987 // the type of this is const volatile X*.
988
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000989 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +0000990
John McCall3cb0ebd2010-03-10 03:28:59 +0000991 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +0000992 ClassTy = C.getQualifiedType(ClassTy,
993 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +0000994 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000995}
996
Eli Friedmand7d7f672009-12-06 20:50:05 +0000997bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +0000998 // If this function is a template instantiation, look at the template from
999 // which it was instantiated.
1000 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1001 if (!CheckFn)
1002 CheckFn = this;
1003
Eli Friedmand7d7f672009-12-06 20:50:05 +00001004 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001005 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001006}
1007
Douglas Gregor7ad83902008-11-05 04:29:56 +00001008CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +00001009CXXBaseOrMemberInitializer(ASTContext &Context,
Anders Carlsson80638c52010-04-12 00:51:03 +00001010 TypeSourceInfo *TInfo, bool IsVirtual,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001011 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001012 : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0),
1013 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1014 SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001015{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001016}
1017
1018CXXBaseOrMemberInitializer::
Douglas Gregor802ab452009-12-02 22:36:29 +00001019CXXBaseOrMemberInitializer(ASTContext &Context,
1020 FieldDecl *Member, SourceLocation MemberLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001021 SourceLocation L, Expr *Init, SourceLocation R)
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001022 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
1023 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
1024 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001025{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001026}
1027
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001028CXXBaseOrMemberInitializer::
1029CXXBaseOrMemberInitializer(ASTContext &Context,
1030 FieldDecl *Member, SourceLocation MemberLoc,
1031 SourceLocation L, Expr *Init, SourceLocation R,
1032 VarDecl **Indices,
1033 unsigned NumIndices)
1034 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001035 AnonUnionMember(0), LParenLoc(L), RParenLoc(R), IsVirtual(false),
1036 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001037{
1038 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1039 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1040}
1041
1042CXXBaseOrMemberInitializer *
1043CXXBaseOrMemberInitializer::Create(ASTContext &Context,
1044 FieldDecl *Member,
1045 SourceLocation MemberLoc,
1046 SourceLocation L,
1047 Expr *Init,
1048 SourceLocation R,
1049 VarDecl **Indices,
1050 unsigned NumIndices) {
1051 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
1052 sizeof(VarDecl *) * NumIndices,
1053 llvm::alignof<CXXBaseOrMemberInitializer>());
1054 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
1055 L, Init, R, Indices, NumIndices);
1056}
1057
Douglas Gregor802ab452009-12-02 22:36:29 +00001058TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
1059 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +00001060 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001061 else
1062 return TypeLoc();
1063}
1064
1065Type *CXXBaseOrMemberInitializer::getBaseClass() {
1066 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +00001067 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001068 else
1069 return 0;
1070}
1071
1072const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
1073 if (isBaseInitializer())
John McCalla93c9342009-12-07 02:54:59 +00001074 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001075 else
1076 return 0;
1077}
1078
1079SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
1080 if (isMemberInitializer())
1081 return getMemberLocation();
1082
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001083 return getBaseClassLoc().getLocalSourceRange().getBegin();
Douglas Gregor802ab452009-12-02 22:36:29 +00001084}
1085
1086SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
1087 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001088}
1089
Douglas Gregorb48fe382008-10-31 09:07:45 +00001090CXXConstructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001091CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001092 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001093 QualType(), 0, false, false, false);
1094}
1095
1096CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001097CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001098 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001099 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001100 bool isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001101 bool isInline,
1102 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001103 assert(NameInfo.getName().getNameKind()
1104 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001105 "Name must refer to a constructor");
Abramo Bagnara25777432010-08-11 22:01:17 +00001106 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001107 isInline, isImplicitlyDeclared);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001108}
1109
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001110bool CXXConstructorDecl::isDefaultConstructor() const {
1111 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001112 // A default constructor for a class X is a constructor of class
1113 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001114 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001115 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001116}
1117
Mike Stump1eb44332009-09-09 15:08:12 +00001118bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001119CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001120 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001121 // A non-template constructor for class X is a copy constructor
1122 // if its first parameter is of type X&, const X&, volatile X& or
1123 // const volatile X&, and either there are no other parameters
1124 // or else all other parameters have default arguments (8.3.6).
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001125 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001126 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001127 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001128 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001129 return false;
1130
1131 const ParmVarDecl *Param = getParamDecl(0);
1132
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001133 // Do we have a reference type? Rvalue references don't count.
Douglas Gregorfd476482009-11-13 23:59:09 +00001134 const LValueReferenceType *ParamRefType =
1135 Param->getType()->getAs<LValueReferenceType>();
1136 if (!ParamRefType)
1137 return false;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001138
Douglas Gregorfd476482009-11-13 23:59:09 +00001139 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001140 ASTContext &Context = getASTContext();
1141
Douglas Gregorfd476482009-11-13 23:59:09 +00001142 CanQualType PointeeType
1143 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001144 CanQualType ClassTy
1145 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001146 if (PointeeType.getUnqualifiedType() != ClassTy)
1147 return false;
1148
John McCall0953e762009-09-24 19:53:00 +00001149 // FIXME: other qualifiers?
1150
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001151 // We have a copy constructor.
1152 TypeQuals = PointeeType.getCVRQualifiers();
1153 return true;
1154}
1155
Anders Carlssonfaccd722009-08-28 16:57:08 +00001156bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001157 // C++ [class.conv.ctor]p1:
1158 // A constructor declared without the function-specifier explicit
1159 // that can be called with a single parameter specifies a
1160 // conversion from the type of its first parameter to the type of
1161 // its class. Such a constructor is called a converting
1162 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001163 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001164 return false;
1165
Mike Stump1eb44332009-09-09 15:08:12 +00001166 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001167 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001168 (getNumParams() == 1) ||
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001169 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
Douglas Gregor60d62c22008-10-31 16:23:19 +00001170}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001171
Douglas Gregor66724ea2009-11-14 01:20:54 +00001172bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const {
1173 if ((getNumParams() < 1) ||
1174 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1175 (getPrimaryTemplate() == 0) ||
1176 (getDescribedFunctionTemplate() != 0))
1177 return false;
1178
1179 const ParmVarDecl *Param = getParamDecl(0);
1180
1181 ASTContext &Context = getASTContext();
1182 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1183
1184 // Strip off the lvalue reference, if any.
1185 if (CanQual<LValueReferenceType> ParamRefType
1186 = ParamType->getAs<LValueReferenceType>())
1187 ParamType = ParamRefType->getPointeeType();
1188
1189
1190 // Is it the same as our our class type?
1191 CanQualType ClassTy
1192 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1193 if (ParamType.getUnqualifiedType() != ClassTy)
1194 return false;
1195
1196 return true;
1197}
1198
Douglas Gregor42a552f2008-11-05 20:51:48 +00001199CXXDestructorDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001200CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001201 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001202 QualType(), false, false);
1203}
1204
1205CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001206CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001207 const DeclarationNameInfo &NameInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001208 QualType T, bool isInline,
Douglas Gregor42a552f2008-11-05 20:51:48 +00001209 bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001210 assert(NameInfo.getName().getNameKind()
1211 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001212 "Name must refer to a destructor");
Abramo Bagnara25777432010-08-11 22:01:17 +00001213 return new (C) CXXDestructorDecl(RD, NameInfo, T, isInline,
1214 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001215}
1216
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001217CXXConversionDecl *
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001218CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001219 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001220 QualType(), 0, false, false);
1221}
1222
1223CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001224CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnara25777432010-08-11 22:01:17 +00001225 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001226 QualType T, TypeSourceInfo *TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001227 bool isInline, bool isExplicit) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001228 assert(NameInfo.getName().getNameKind()
1229 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001230 "Name must refer to a conversion function");
Abramo Bagnara25777432010-08-11 22:01:17 +00001231 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
1232 isInline, isExplicit);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001233}
1234
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001235LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001236 DeclContext *DC,
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001237 SourceLocation L,
Douglas Gregor074149e2009-01-05 19:45:36 +00001238 LanguageIDs Lang, bool Braces) {
Steve Naroff3e970492009-01-27 21:25:57 +00001239 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001240}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001241
1242UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1243 SourceLocation L,
1244 SourceLocation NamespaceLoc,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001245 SourceRange QualifierRange,
1246 NestedNameSpecifier *Qualifier,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001247 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001248 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001249 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001250 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1251 Used = NS->getOriginalNamespace();
Mike Stump1eb44332009-09-09 15:08:12 +00001252 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
Douglas Gregor8419fa32009-05-30 06:31:56 +00001253 Qualifier, IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001254}
1255
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001256NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1257 if (NamespaceAliasDecl *NA =
1258 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1259 return NA->getNamespace();
1260 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1261}
1262
Mike Stump1eb44332009-09-09 15:08:12 +00001263NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001264 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001265 SourceLocation AliasLoc,
1266 IdentifierInfo *Alias,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001267 SourceRange QualifierRange,
1268 NestedNameSpecifier *Qualifier,
Mike Stump1eb44332009-09-09 15:08:12 +00001269 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001270 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001271 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1272 Namespace = NS->getOriginalNamespace();
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001273 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
Douglas Gregor6c9c9402009-05-30 06:48:27 +00001274 Qualifier, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001275}
1276
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001277UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001278 SourceRange NNR, SourceLocation UL,
1279 NestedNameSpecifier* TargetNNS,
1280 const DeclarationNameInfo &NameInfo,
1281 bool IsTypeNameArg) {
1282 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001283}
1284
John McCall7ba107a2009-11-18 02:36:19 +00001285UnresolvedUsingValueDecl *
1286UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1287 SourceLocation UsingLoc,
1288 SourceRange TargetNNR,
1289 NestedNameSpecifier *TargetNNS,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001290 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00001291 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001292 TargetNNR, TargetNNS, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00001293}
1294
1295UnresolvedUsingTypenameDecl *
1296UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1297 SourceLocation UsingLoc,
1298 SourceLocation TypenameLoc,
1299 SourceRange TargetNNR,
1300 NestedNameSpecifier *TargetNNS,
1301 SourceLocation TargetNameLoc,
1302 DeclarationName TargetName) {
1303 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1304 TargetNNR, TargetNNS,
1305 TargetNameLoc,
1306 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00001307}
1308
Anders Carlssonfb311762009-03-14 00:25:26 +00001309StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1310 SourceLocation L, Expr *AssertExpr,
1311 StringLiteral *Message) {
1312 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1313}
1314
Anders Carlsson05bf2c72009-03-26 23:46:50 +00001315static const char *getAccessName(AccessSpecifier AS) {
1316 switch (AS) {
1317 default:
1318 case AS_none:
1319 assert("Invalid access specifier!");
1320 return 0;
1321 case AS_public:
1322 return "public";
1323 case AS_private:
1324 return "private";
1325 case AS_protected:
1326 return "protected";
1327 }
1328}
1329
1330const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1331 AccessSpecifier AS) {
1332 return DB << getAccessName(AS);
1333}