Implement pack expansions whose pattern is a base-specifier.
[clang.git] / lib / AST / DeclCXX.cpp
blob3304ad9a293d79370cadb957ebeeda7d708ec4cf
1 //===--- 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.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/DeclCXX.h"
15 #include "clang/AST/DeclTemplate.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTMutationListener.h"
18 #include "clang/AST/CXXInheritance.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/TypeLoc.h"
21 #include "clang/Basic/IdentifierTable.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 using namespace clang;
26 //===----------------------------------------------------------------------===//
27 // Decl Allocation/Deallocation Method Implementations
28 //===----------------------------------------------------------------------===//
30 CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
31 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
32 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
33 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
34 Abstract(false), HasTrivialConstructor(true),
35 HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true),
36 HasTrivialDestructor(true), ComputedVisibleConversions(false),
37 DeclaredDefaultConstructor(false), DeclaredCopyConstructor(false),
38 DeclaredCopyAssignment(false), DeclaredDestructor(false),
39 NumBases(0), NumVBases(0), Bases(), VBases(),
40 Definition(D), FirstFriend(0) {
43 CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
44 SourceLocation L, IdentifierInfo *Id,
45 CXXRecordDecl *PrevDecl,
46 SourceLocation TKL)
47 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL),
48 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
49 TemplateOrInstantiation() { }
51 CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
52 SourceLocation L, IdentifierInfo *Id,
53 SourceLocation TKL,
54 CXXRecordDecl* PrevDecl,
55 bool DelayTypeCreation) {
56 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id,
57 PrevDecl, TKL);
59 // FIXME: DelayTypeCreation seems like such a hack
60 if (!DelayTypeCreation)
61 C.getTypeDeclType(R, PrevDecl);
62 return R;
65 CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, EmptyShell Empty) {
66 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(), 0, 0,
67 SourceLocation());
70 void
71 CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
72 unsigned NumBases) {
73 ASTContext &C = getASTContext();
75 // C++ [dcl.init.aggr]p1:
76 // An aggregate is an array or a class (clause 9) with [...]
77 // no base classes [...].
78 data().Aggregate = false;
80 if (!data().Bases.isOffset() && data().NumBases > 0)
81 C.Deallocate(data().getBases());
83 // The set of seen virtual base types.
84 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
86 // The virtual bases of this class.
87 llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases;
89 data().Bases = new(C) CXXBaseSpecifier [NumBases];
90 data().NumBases = NumBases;
91 for (unsigned i = 0; i < NumBases; ++i) {
92 data().getBases()[i] = *Bases[i];
93 // Keep track of inherited vbases for this base class.
94 const CXXBaseSpecifier *Base = Bases[i];
95 QualType BaseType = Base->getType();
96 // Skip dependent types; we can't do any checking on them now.
97 if (BaseType->isDependentType())
98 continue;
99 CXXRecordDecl *BaseClassDecl
100 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
102 // C++ [dcl.init.aggr]p1:
103 // An aggregate is [...] a class with [...] no base classes [...].
104 data().Aggregate = false;
106 // C++ [class]p4:
107 // A POD-struct is an aggregate class...
108 data().PlainOldData = false;
110 // A class with a non-empty base class is not empty.
111 // FIXME: Standard ref?
112 if (!BaseClassDecl->isEmpty())
113 data().Empty = false;
115 // C++ [class.virtual]p1:
116 // A class that declares or inherits a virtual function is called a
117 // polymorphic class.
118 if (BaseClassDecl->isPolymorphic())
119 data().Polymorphic = true;
121 // Now go through all virtual bases of this base and add them.
122 for (CXXRecordDecl::base_class_iterator VBase =
123 BaseClassDecl->vbases_begin(),
124 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
125 // Add this base if it's not already in the list.
126 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType())))
127 VBases.push_back(VBase);
130 if (Base->isVirtual()) {
131 // Add this base if it's not already in the list.
132 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
133 VBases.push_back(Base);
135 // C++0x [meta.unary.prop] is_empty:
136 // T is a class type, but not a union type, with ... no virtual base
137 // classes
138 data().Empty = false;
140 // C++ [class.ctor]p5:
141 // A constructor is trivial if its class has no virtual base classes.
142 data().HasTrivialConstructor = false;
144 // C++ [class.copy]p6:
145 // A copy constructor is trivial if its class has no virtual base
146 // classes.
147 data().HasTrivialCopyConstructor = false;
149 // C++ [class.copy]p11:
150 // A copy assignment operator is trivial if its class has no virtual
151 // base classes.
152 data().HasTrivialCopyAssignment = false;
153 } else {
154 // C++ [class.ctor]p5:
155 // A constructor is trivial if all the direct base classes of its
156 // class have trivial constructors.
157 if (!BaseClassDecl->hasTrivialConstructor())
158 data().HasTrivialConstructor = false;
160 // C++ [class.copy]p6:
161 // A copy constructor is trivial if all the direct base classes of its
162 // class have trivial copy constructors.
163 if (!BaseClassDecl->hasTrivialCopyConstructor())
164 data().HasTrivialCopyConstructor = false;
166 // C++ [class.copy]p11:
167 // A copy assignment operator is trivial if all the direct base classes
168 // of its class have trivial copy assignment operators.
169 if (!BaseClassDecl->hasTrivialCopyAssignment())
170 data().HasTrivialCopyAssignment = false;
173 // C++ [class.ctor]p3:
174 // A destructor is trivial if all the direct base classes of its class
175 // have trivial destructors.
176 if (!BaseClassDecl->hasTrivialDestructor())
177 data().HasTrivialDestructor = false;
180 if (VBases.empty())
181 return;
183 // Create base specifier for any direct or indirect virtual bases.
184 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
185 data().NumVBases = VBases.size();
186 for (int I = 0, E = VBases.size(); I != E; ++I) {
187 TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo();
189 // Skip dependent types; we can't do any checking on them now.
190 if (VBaseTypeInfo->getType()->isDependentType())
191 continue;
193 CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>(
194 VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl());
196 data().getVBases()[I] =
197 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true,
198 VBaseClassDecl->getTagKind() == TTK_Class,
199 VBases[I]->getAccessSpecifier(), VBaseTypeInfo,
200 SourceLocation());
204 /// Callback function for CXXRecordDecl::forallBases that acknowledges
205 /// that it saw a base class.
206 static bool SawBase(const CXXRecordDecl *, void *) {
207 return true;
210 bool CXXRecordDecl::hasAnyDependentBases() const {
211 if (!isDependentContext())
212 return false;
214 return !forallBases(SawBase, 0);
217 bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const {
218 return getCopyConstructor(Context, Qualifiers::Const) != 0;
221 /// \brief Perform a simplistic form of overload resolution that only considers
222 /// cv-qualifiers on a single parameter, and return the best overload candidate
223 /// (if there is one).
224 static CXXMethodDecl *
225 GetBestOverloadCandidateSimple(
226 const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) {
227 if (Cands.empty())
228 return 0;
229 if (Cands.size() == 1)
230 return Cands[0].first;
232 unsigned Best = 0, N = Cands.size();
233 for (unsigned I = 1; I != N; ++I)
234 if (Cands[Best].second.isSupersetOf(Cands[I].second))
235 Best = I;
237 for (unsigned I = 1; I != N; ++I)
238 if (Cands[Best].second.isSupersetOf(Cands[I].second))
239 return 0;
241 return Cands[Best].first;
244 CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context,
245 unsigned TypeQuals) const{
246 QualType ClassType
247 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this));
248 DeclarationName ConstructorName
249 = Context.DeclarationNames.getCXXConstructorName(
250 Context.getCanonicalType(ClassType));
251 unsigned FoundTQs;
252 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
253 DeclContext::lookup_const_iterator Con, ConEnd;
254 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
255 Con != ConEnd; ++Con) {
256 // C++ [class.copy]p2:
257 // A non-template constructor for class X is a copy constructor if [...]
258 if (isa<FunctionTemplateDecl>(*Con))
259 continue;
261 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
262 if (Constructor->isCopyConstructor(FoundTQs)) {
263 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) ||
264 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const)))
265 Found.push_back(std::make_pair(
266 const_cast<CXXConstructorDecl *>(Constructor),
267 Qualifiers::fromCVRMask(FoundTQs)));
271 return cast_or_null<CXXConstructorDecl>(
272 GetBestOverloadCandidateSimple(Found));
275 CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const {
276 ASTContext &Context = getASTContext();
277 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this));
278 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
280 llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found;
281 DeclContext::lookup_const_iterator Op, OpEnd;
282 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) {
283 // C++ [class.copy]p9:
284 // A user-declared copy assignment operator is a non-static non-template
285 // member function of class X with exactly one parameter of type X, X&,
286 // const X&, volatile X& or const volatile X&.
287 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op);
288 if (!Method || Method->isStatic() || Method->getPrimaryTemplate())
289 continue;
291 const FunctionProtoType *FnType
292 = Method->getType()->getAs<FunctionProtoType>();
293 assert(FnType && "Overloaded operator has no prototype.");
294 // Don't assert on this; an invalid decl might have been left in the AST.
295 if (FnType->getNumArgs() != 1 || FnType->isVariadic())
296 continue;
298 QualType ArgType = FnType->getArgType(0);
299 Qualifiers Quals;
300 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) {
301 ArgType = Ref->getPointeeType();
302 // If we have a const argument and we have a reference to a non-const,
303 // this function does not match.
304 if (ArgIsConst && !ArgType.isConstQualified())
305 continue;
307 Quals = ArgType.getQualifiers();
308 } else {
309 // By-value copy-assignment operators are treated like const X&
310 // copy-assignment operators.
311 Quals = Qualifiers::fromCVRMask(Qualifiers::Const);
314 if (!Context.hasSameUnqualifiedType(ArgType, Class))
315 continue;
317 // Save this copy-assignment operator. It might be "the one".
318 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals));
321 // Use a simplistic form of overload resolution to find the candidate.
322 return GetBestOverloadCandidateSimple(Found);
325 void CXXRecordDecl::markedVirtualFunctionPure() {
326 // C++ [class.abstract]p2:
327 // A class is abstract if it has at least one pure virtual function.
328 data().Abstract = true;
331 void CXXRecordDecl::addedMember(Decl *D) {
332 // Ignore friends and invalid declarations.
333 if (D->getFriendObjectKind() || D->isInvalidDecl())
334 return;
336 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
337 if (FunTmpl)
338 D = FunTmpl->getTemplatedDecl();
340 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
341 if (Method->isVirtual()) {
342 // C++ [dcl.init.aggr]p1:
343 // An aggregate is an array or a class with [...] no virtual functions.
344 data().Aggregate = false;
346 // C++ [class]p4:
347 // A POD-struct is an aggregate class...
348 data().PlainOldData = false;
350 // Virtual functions make the class non-empty.
351 // FIXME: Standard ref?
352 data().Empty = false;
354 // C++ [class.virtual]p1:
355 // A class that declares or inherits a virtual function is called a
356 // polymorphic class.
357 data().Polymorphic = true;
359 // None of the special member functions are trivial.
360 data().HasTrivialConstructor = false;
361 data().HasTrivialCopyConstructor = false;
362 data().HasTrivialCopyAssignment = false;
363 // FIXME: Destructor?
367 if (D->isImplicit()) {
368 // Notify that an implicit member was added after the definition
369 // was completed.
370 if (!isBeingDefined())
371 if (ASTMutationListener *L = getASTMutationListener())
372 L->AddedCXXImplicitMember(data().Definition, D);
374 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
375 // If this is the implicit default constructor, note that we have now
376 // declared it.
377 if (Constructor->isDefaultConstructor())
378 data().DeclaredDefaultConstructor = true;
379 // If this is the implicit copy constructor, note that we have now
380 // declared it.
381 else if (Constructor->isCopyConstructor())
382 data().DeclaredCopyConstructor = true;
383 return;
386 if (isa<CXXDestructorDecl>(D)) {
387 data().DeclaredDestructor = true;
388 return;
391 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
392 // If this is the implicit copy constructor, note that we have now
393 // declared it.
394 // FIXME: Move constructors
395 if (Method->getOverloadedOperator() == OO_Equal)
396 data().DeclaredCopyAssignment = true;
397 return;
400 // Any other implicit declarations are handled like normal declarations.
403 // Handle (user-declared) constructors.
404 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
405 // Note that we have a user-declared constructor.
406 data().UserDeclaredConstructor = true;
408 // Note that we have no need of an implicitly-declared default constructor.
409 data().DeclaredDefaultConstructor = true;
411 // C++ [dcl.init.aggr]p1:
412 // An aggregate is an array or a class (clause 9) with no
413 // user-declared constructors (12.1) [...].
414 data().Aggregate = false;
416 // C++ [class]p4:
417 // A POD-struct is an aggregate class [...]
418 data().PlainOldData = false;
420 // C++ [class.ctor]p5:
421 // A constructor is trivial if it is an implicitly-declared default
422 // constructor.
423 // FIXME: C++0x: don't do this for "= default" default constructors.
424 data().HasTrivialConstructor = false;
426 // Note when we have a user-declared copy constructor, which will
427 // suppress the implicit declaration of a copy constructor.
428 if (!FunTmpl && Constructor->isCopyConstructor()) {
429 data().UserDeclaredCopyConstructor = true;
430 data().DeclaredCopyConstructor = true;
432 // C++ [class.copy]p6:
433 // A copy constructor is trivial if it is implicitly declared.
434 // FIXME: C++0x: don't do this for "= default" copy constructors.
435 data().HasTrivialCopyConstructor = false;
438 return;
441 // Handle (user-declared) destructors.
442 if (isa<CXXDestructorDecl>(D)) {
443 data().DeclaredDestructor = true;
444 data().UserDeclaredDestructor = true;
446 // C++ [class]p4:
447 // A POD-struct is an aggregate class that has [...] no user-defined
448 // destructor.
449 data().PlainOldData = false;
451 // C++ [class.dtor]p3:
452 // A destructor is trivial if it is an implicitly-declared destructor and
453 // [...].
455 // FIXME: C++0x: don't do this for "= default" destructors
456 data().HasTrivialDestructor = false;
458 return;
461 // Handle (user-declared) member functions.
462 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
463 if (Method->getOverloadedOperator() == OO_Equal) {
464 // We're interested specifically in copy assignment operators.
465 const FunctionProtoType *FnType
466 = Method->getType()->getAs<FunctionProtoType>();
467 assert(FnType && "Overloaded operator has no proto function type.");
468 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic());
470 // Copy assignment operators must be non-templates.
471 if (Method->getPrimaryTemplate() || FunTmpl)
472 return;
474 ASTContext &Context = getASTContext();
475 QualType ArgType = FnType->getArgType(0);
476 if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>())
477 ArgType = Ref->getPointeeType();
479 ArgType = ArgType.getUnqualifiedType();
480 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType(
481 const_cast<CXXRecordDecl*>(this)));
483 if (!Context.hasSameUnqualifiedType(ClassType, ArgType))
484 return;
486 // This is a copy assignment operator.
487 // FIXME: Move assignment operators.
489 // Suppress the implicit declaration of a copy constructor.
490 data().UserDeclaredCopyAssignment = true;
491 data().DeclaredCopyAssignment = true;
493 // C++ [class.copy]p11:
494 // A copy assignment operator is trivial if it is implicitly declared.
495 // FIXME: C++0x: don't do this for "= default" copy operators.
496 data().HasTrivialCopyAssignment = false;
498 // C++ [class]p4:
499 // A POD-struct is an aggregate class that [...] has no user-defined copy
500 // assignment operator [...].
501 data().PlainOldData = false;
504 // Keep the list of conversion functions up-to-date.
505 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
506 // We don't record specializations.
507 if (Conversion->getPrimaryTemplate())
508 return;
510 // FIXME: We intentionally don't use the decl's access here because it
511 // hasn't been set yet. That's really just a misdesign in Sema.
513 if (FunTmpl) {
514 if (FunTmpl->getPreviousDeclaration())
515 data().Conversions.replace(FunTmpl->getPreviousDeclaration(),
516 FunTmpl);
517 else
518 data().Conversions.addDecl(FunTmpl);
519 } else {
520 if (Conversion->getPreviousDeclaration())
521 data().Conversions.replace(Conversion->getPreviousDeclaration(),
522 Conversion);
523 else
524 data().Conversions.addDecl(Conversion);
528 return;
531 // Handle non-static data members.
532 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
533 // C++ [dcl.init.aggr]p1:
534 // An aggregate is an array or a class (clause 9) with [...] no
535 // private or protected non-static data members (clause 11).
537 // A POD must be an aggregate.
538 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
539 data().Aggregate = false;
540 data().PlainOldData = false;
543 // C++ [class]p9:
544 // A POD struct is a class that is both a trivial class and a
545 // standard-layout class, and has no non-static data members of type
546 // non-POD struct, non-POD union (or array of such types).
547 ASTContext &Context = getASTContext();
548 QualType T = Context.getBaseElementType(Field->getType());
549 if (!T->isPODType())
550 data().PlainOldData = false;
551 if (T->isReferenceType())
552 data().HasTrivialConstructor = false;
554 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
555 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
556 if (FieldRec->getDefinition()) {
557 if (!FieldRec->hasTrivialConstructor())
558 data().HasTrivialConstructor = false;
559 if (!FieldRec->hasTrivialCopyConstructor())
560 data().HasTrivialCopyConstructor = false;
561 if (!FieldRec->hasTrivialCopyAssignment())
562 data().HasTrivialCopyAssignment = false;
563 if (!FieldRec->hasTrivialDestructor())
564 data().HasTrivialDestructor = false;
568 // If this is not a zero-length bit-field, then the class is not empty.
569 if (data().Empty) {
570 if (!Field->getBitWidth())
571 data().Empty = false;
572 else if (!Field->getBitWidth()->isTypeDependent() &&
573 !Field->getBitWidth()->isValueDependent()) {
574 llvm::APSInt Bits;
575 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context))
576 if (!!Bits)
577 data().Empty = false;
582 // Handle using declarations of conversion functions.
583 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
584 if (Shadow->getDeclName().getNameKind()
585 == DeclarationName::CXXConversionFunctionName)
586 data().Conversions.addDecl(Shadow, Shadow->getAccess());
589 static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
590 QualType T;
591 if (isa<UsingShadowDecl>(Conv))
592 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
593 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
594 T = ConvTemp->getTemplatedDecl()->getResultType();
595 else
596 T = cast<CXXConversionDecl>(Conv)->getConversionType();
597 return Context.getCanonicalType(T);
600 /// Collect the visible conversions of a base class.
602 /// \param Base a base class of the class we're considering
603 /// \param InVirtual whether this base class is a virtual base (or a base
604 /// of a virtual base)
605 /// \param Access the access along the inheritance path to this base
606 /// \param ParentHiddenTypes the conversions provided by the inheritors
607 /// of this base
608 /// \param Output the set to which to add conversions from non-virtual bases
609 /// \param VOutput the set to which to add conversions from virtual bases
610 /// \param HiddenVBaseCs the set of conversions which were hidden in a
611 /// virtual base along some inheritance path
612 static void CollectVisibleConversions(ASTContext &Context,
613 CXXRecordDecl *Record,
614 bool InVirtual,
615 AccessSpecifier Access,
616 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
617 UnresolvedSetImpl &Output,
618 UnresolvedSetImpl &VOutput,
619 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
620 // The set of types which have conversions in this class or its
621 // subclasses. As an optimization, we don't copy the derived set
622 // unless it might change.
623 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
624 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
626 // Collect the direct conversions and figure out which conversions
627 // will be hidden in the subclasses.
628 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
629 if (!Cs.empty()) {
630 HiddenTypesBuffer = ParentHiddenTypes;
631 HiddenTypes = &HiddenTypesBuffer;
633 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) {
634 bool Hidden =
635 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl()));
637 // If this conversion is hidden and we're in a virtual base,
638 // remember that it's hidden along some inheritance path.
639 if (Hidden && InVirtual)
640 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
642 // If this conversion isn't hidden, add it to the appropriate output.
643 else if (!Hidden) {
644 AccessSpecifier IAccess
645 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
647 if (InVirtual)
648 VOutput.addDecl(I.getDecl(), IAccess);
649 else
650 Output.addDecl(I.getDecl(), IAccess);
655 // Collect information recursively from any base classes.
656 for (CXXRecordDecl::base_class_iterator
657 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
658 const RecordType *RT = I->getType()->getAs<RecordType>();
659 if (!RT) continue;
661 AccessSpecifier BaseAccess
662 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
663 bool BaseInVirtual = InVirtual || I->isVirtual();
665 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
666 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
667 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
671 /// Collect the visible conversions of a class.
673 /// This would be extremely straightforward if it weren't for virtual
674 /// bases. It might be worth special-casing that, really.
675 static void CollectVisibleConversions(ASTContext &Context,
676 CXXRecordDecl *Record,
677 UnresolvedSetImpl &Output) {
678 // The collection of all conversions in virtual bases that we've
679 // found. These will be added to the output as long as they don't
680 // appear in the hidden-conversions set.
681 UnresolvedSet<8> VBaseCs;
683 // The set of conversions in virtual bases that we've determined to
684 // be hidden.
685 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
687 // The set of types hidden by classes derived from this one.
688 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
690 // Go ahead and collect the direct conversions and add them to the
691 // hidden-types set.
692 UnresolvedSetImpl &Cs = *Record->getConversionFunctions();
693 Output.append(Cs.begin(), Cs.end());
694 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I)
695 HiddenTypes.insert(GetConversionType(Context, I.getDecl()));
697 // Recursively collect conversions from base classes.
698 for (CXXRecordDecl::base_class_iterator
699 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
700 const RecordType *RT = I->getType()->getAs<RecordType>();
701 if (!RT) continue;
703 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
704 I->isVirtual(), I->getAccessSpecifier(),
705 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
708 // Add any unhidden conversions provided by virtual bases.
709 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
710 I != E; ++I) {
711 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
712 Output.addDecl(I.getDecl(), I.getAccess());
716 /// getVisibleConversionFunctions - get all conversion functions visible
717 /// in current class; including conversion function templates.
718 const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() {
719 // If root class, all conversions are visible.
720 if (bases_begin() == bases_end())
721 return &data().Conversions;
722 // If visible conversion list is already evaluated, return it.
723 if (data().ComputedVisibleConversions)
724 return &data().VisibleConversions;
725 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions);
726 data().ComputedVisibleConversions = true;
727 return &data().VisibleConversions;
730 void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
731 // This operation is O(N) but extremely rare. Sema only uses it to
732 // remove UsingShadowDecls in a class that were followed by a direct
733 // declaration, e.g.:
734 // class A : B {
735 // using B::operator int;
736 // operator int();
737 // };
738 // This is uncommon by itself and even more uncommon in conjunction
739 // with sufficiently large numbers of directly-declared conversions
740 // that asymptotic behavior matters.
742 UnresolvedSetImpl &Convs = *getConversionFunctions();
743 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
744 if (Convs[I].getDecl() == ConvDecl) {
745 Convs.erase(I);
746 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
747 && "conversion was found multiple times in unresolved set");
748 return;
752 llvm_unreachable("conversion not found in set!");
755 CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
756 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
757 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
759 return 0;
762 MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
763 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
766 void
767 CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
768 TemplateSpecializationKind TSK) {
769 assert(TemplateOrInstantiation.isNull() &&
770 "Previous template or instantiation?");
771 assert(!isa<ClassTemplateSpecializationDecl>(this));
772 TemplateOrInstantiation
773 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
776 TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
777 if (const ClassTemplateSpecializationDecl *Spec
778 = dyn_cast<ClassTemplateSpecializationDecl>(this))
779 return Spec->getSpecializationKind();
781 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
782 return MSInfo->getTemplateSpecializationKind();
784 return TSK_Undeclared;
787 void
788 CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
789 if (ClassTemplateSpecializationDecl *Spec
790 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
791 Spec->setSpecializationKind(TSK);
792 return;
795 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
796 MSInfo->setTemplateSpecializationKind(TSK);
797 return;
800 assert(false && "Not a class template or member class specialization");
803 CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
804 ASTContext &Context = getASTContext();
805 QualType ClassType = Context.getTypeDeclType(this);
807 DeclarationName Name
808 = Context.DeclarationNames.getCXXDestructorName(
809 Context.getCanonicalType(ClassType));
811 DeclContext::lookup_const_iterator I, E;
812 llvm::tie(I, E) = lookup(Name);
813 if (I == E)
814 return 0;
816 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I);
817 assert(++I == E && "Found more than one destructor!");
819 return Dtor;
822 void CXXRecordDecl::completeDefinition() {
823 completeDefinition(0);
826 void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
827 RecordDecl::completeDefinition();
829 // If the class may be abstract (but hasn't been marked as such), check for
830 // any pure final overriders.
831 if (mayBeAbstract()) {
832 CXXFinalOverriderMap MyFinalOverriders;
833 if (!FinalOverriders) {
834 getFinalOverriders(MyFinalOverriders);
835 FinalOverriders = &MyFinalOverriders;
838 bool Done = false;
839 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
840 MEnd = FinalOverriders->end();
841 M != MEnd && !Done; ++M) {
842 for (OverridingMethods::iterator SO = M->second.begin(),
843 SOEnd = M->second.end();
844 SO != SOEnd && !Done; ++SO) {
845 assert(SO->second.size() > 0 &&
846 "All virtual functions have overridding virtual functions");
848 // C++ [class.abstract]p4:
849 // A class is abstract if it contains or inherits at least one
850 // pure virtual function for which the final overrider is pure
851 // virtual.
852 if (SO->second.front().Method->isPure()) {
853 data().Abstract = true;
854 Done = true;
855 break;
861 // Set access bits correctly on the directly-declared conversions.
862 for (UnresolvedSetIterator I = data().Conversions.begin(),
863 E = data().Conversions.end();
864 I != E; ++I)
865 data().Conversions.setAccess(I, (*I)->getAccess());
868 bool CXXRecordDecl::mayBeAbstract() const {
869 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
870 isDependentContext())
871 return false;
873 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
874 BEnd = bases_end();
875 B != BEnd; ++B) {
876 CXXRecordDecl *BaseDecl
877 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
878 if (BaseDecl->isAbstract())
879 return true;
882 return false;
885 CXXMethodDecl *
886 CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
887 const DeclarationNameInfo &NameInfo,
888 QualType T, TypeSourceInfo *TInfo,
889 bool isStatic, StorageClass SCAsWritten, bool isInline) {
890 return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo,
891 isStatic, SCAsWritten, isInline);
894 bool CXXMethodDecl::isUsualDeallocationFunction() const {
895 if (getOverloadedOperator() != OO_Delete &&
896 getOverloadedOperator() != OO_Array_Delete)
897 return false;
899 // C++ [basic.stc.dynamic.deallocation]p2:
900 // A template instance is never a usual deallocation function,
901 // regardless of its signature.
902 if (getPrimaryTemplate())
903 return false;
905 // C++ [basic.stc.dynamic.deallocation]p2:
906 // If a class T has a member deallocation function named operator delete
907 // with exactly one parameter, then that function is a usual (non-placement)
908 // deallocation function. [...]
909 if (getNumParams() == 1)
910 return true;
912 // C++ [basic.stc.dynamic.deallocation]p2:
913 // [...] If class T does not declare such an operator delete but does
914 // declare a member deallocation function named operator delete with
915 // exactly two parameters, the second of which has type std::size_t (18.1),
916 // then this function is a usual deallocation function.
917 ASTContext &Context = getASTContext();
918 if (getNumParams() != 2 ||
919 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
920 Context.getSizeType()))
921 return false;
923 // This function is a usual deallocation function if there are no
924 // single-parameter deallocation functions of the same kind.
925 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
926 R.first != R.second; ++R.first) {
927 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first))
928 if (FD->getNumParams() == 1)
929 return false;
932 return true;
935 bool CXXMethodDecl::isCopyAssignmentOperator() const {
936 // C++0x [class.copy]p19:
937 // A user-declared copy assignment operator X::operator= is a non-static
938 // non-template member function of class X with exactly one parameter of
939 // type X, X&, const X&, volatile X& or const volatile X&.
940 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
941 /*non-static*/ isStatic() ||
942 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
943 /*exactly one parameter*/getNumParams() != 1)
944 return false;
946 QualType ParamType = getParamDecl(0)->getType();
947 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
948 ParamType = Ref->getPointeeType();
950 ASTContext &Context = getASTContext();
951 QualType ClassType
952 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
953 return Context.hasSameUnqualifiedType(ClassType, ParamType);
956 void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
957 assert(MD->isCanonicalDecl() && "Method is not canonical!");
958 assert(!MD->getParent()->isDependentContext() &&
959 "Can't add an overridden method to a class template!");
961 getASTContext().addOverriddenMethod(this, MD);
964 CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
965 return getASTContext().overridden_methods_begin(this);
968 CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
969 return getASTContext().overridden_methods_end(this);
972 unsigned CXXMethodDecl::size_overridden_methods() const {
973 return getASTContext().overridden_methods_size(this);
976 QualType CXXMethodDecl::getThisType(ASTContext &C) const {
977 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
978 // If the member function is declared const, the type of this is const X*,
979 // if the member function is declared volatile, the type of this is
980 // volatile X*, and if the member function is declared const volatile,
981 // the type of this is const volatile X*.
983 assert(isInstance() && "No 'this' for static methods!");
985 QualType ClassTy = C.getTypeDeclType(getParent());
986 ClassTy = C.getQualifiedType(ClassTy,
987 Qualifiers::fromCVRMask(getTypeQualifiers()));
988 return C.getPointerType(ClassTy);
991 bool CXXMethodDecl::hasInlineBody() const {
992 // If this function is a template instantiation, look at the template from
993 // which it was instantiated.
994 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
995 if (!CheckFn)
996 CheckFn = this;
998 const FunctionDecl *fn;
999 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
1002 CXXBaseOrMemberInitializer::
1003 CXXBaseOrMemberInitializer(ASTContext &Context,
1004 TypeSourceInfo *TInfo, bool IsVirtual,
1005 SourceLocation L, Expr *Init, SourceLocation R)
1006 : BaseOrMember(TInfo), Init(Init),
1007 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false),
1008 SourceOrderOrNumArrayIndices(0)
1012 CXXBaseOrMemberInitializer::
1013 CXXBaseOrMemberInitializer(ASTContext &Context,
1014 FieldDecl *Member, SourceLocation MemberLoc,
1015 SourceLocation L, Expr *Init, SourceLocation R)
1016 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
1017 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1018 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1022 CXXBaseOrMemberInitializer::
1023 CXXBaseOrMemberInitializer(ASTContext &Context,
1024 IndirectFieldDecl *Member, SourceLocation MemberLoc,
1025 SourceLocation L, Expr *Init, SourceLocation R)
1026 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
1027 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1028 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1032 CXXBaseOrMemberInitializer::
1033 CXXBaseOrMemberInitializer(ASTContext &Context,
1034 FieldDecl *Member, SourceLocation MemberLoc,
1035 SourceLocation L, Expr *Init, SourceLocation R,
1036 VarDecl **Indices,
1037 unsigned NumIndices)
1038 : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init),
1039 LParenLoc(L), RParenLoc(R), IsVirtual(false),
1040 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
1042 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1043 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1046 CXXBaseOrMemberInitializer *
1047 CXXBaseOrMemberInitializer::Create(ASTContext &Context,
1048 FieldDecl *Member,
1049 SourceLocation MemberLoc,
1050 SourceLocation L,
1051 Expr *Init,
1052 SourceLocation R,
1053 VarDecl **Indices,
1054 unsigned NumIndices) {
1055 void *Mem = Context.Allocate(sizeof(CXXBaseOrMemberInitializer) +
1056 sizeof(VarDecl *) * NumIndices,
1057 llvm::alignOf<CXXBaseOrMemberInitializer>());
1058 return new (Mem) CXXBaseOrMemberInitializer(Context, Member, MemberLoc,
1059 L, Init, R, Indices, NumIndices);
1062 TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const {
1063 if (isBaseInitializer())
1064 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc();
1065 else
1066 return TypeLoc();
1069 Type *CXXBaseOrMemberInitializer::getBaseClass() {
1070 if (isBaseInitializer())
1071 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
1072 else
1073 return 0;
1076 const Type *CXXBaseOrMemberInitializer::getBaseClass() const {
1077 if (isBaseInitializer())
1078 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr();
1079 else
1080 return 0;
1083 SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const {
1084 if (isAnyMemberInitializer())
1085 return getMemberLocation();
1087 return getBaseClassLoc().getLocalSourceRange().getBegin();
1090 SourceRange CXXBaseOrMemberInitializer::getSourceRange() const {
1091 return SourceRange(getSourceLocation(), getRParenLoc());
1094 CXXConstructorDecl *
1095 CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) {
1096 return new (C) CXXConstructorDecl(0, DeclarationNameInfo(),
1097 QualType(), 0, false, false, false);
1100 CXXConstructorDecl *
1101 CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
1102 const DeclarationNameInfo &NameInfo,
1103 QualType T, TypeSourceInfo *TInfo,
1104 bool isExplicit,
1105 bool isInline,
1106 bool isImplicitlyDeclared) {
1107 assert(NameInfo.getName().getNameKind()
1108 == DeclarationName::CXXConstructorName &&
1109 "Name must refer to a constructor");
1110 return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit,
1111 isInline, isImplicitlyDeclared);
1114 bool CXXConstructorDecl::isDefaultConstructor() const {
1115 // C++ [class.ctor]p5:
1116 // A default constructor for a class X is a constructor of class
1117 // X that can be called without an argument.
1118 return (getNumParams() == 0) ||
1119 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
1122 bool
1123 CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
1124 // C++ [class.copy]p2:
1125 // A non-template constructor for class X is a copy constructor
1126 // if its first parameter is of type X&, const X&, volatile X& or
1127 // const volatile X&, and either there are no other parameters
1128 // or else all other parameters have default arguments (8.3.6).
1129 if ((getNumParams() < 1) ||
1130 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1131 (getPrimaryTemplate() != 0) ||
1132 (getDescribedFunctionTemplate() != 0))
1133 return false;
1135 const ParmVarDecl *Param = getParamDecl(0);
1137 // Do we have a reference type? Rvalue references don't count.
1138 const LValueReferenceType *ParamRefType =
1139 Param->getType()->getAs<LValueReferenceType>();
1140 if (!ParamRefType)
1141 return false;
1143 // Is it a reference to our class type?
1144 ASTContext &Context = getASTContext();
1146 CanQualType PointeeType
1147 = Context.getCanonicalType(ParamRefType->getPointeeType());
1148 CanQualType ClassTy
1149 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1150 if (PointeeType.getUnqualifiedType() != ClassTy)
1151 return false;
1153 // FIXME: other qualifiers?
1155 // We have a copy constructor.
1156 TypeQuals = PointeeType.getCVRQualifiers();
1157 return true;
1160 bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
1161 // C++ [class.conv.ctor]p1:
1162 // A constructor declared without the function-specifier explicit
1163 // that can be called with a single parameter specifies a
1164 // conversion from the type of its first parameter to the type of
1165 // its class. Such a constructor is called a converting
1166 // constructor.
1167 if (isExplicit() && !AllowExplicit)
1168 return false;
1170 return (getNumParams() == 0 &&
1171 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
1172 (getNumParams() == 1) ||
1173 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg());
1176 bool CXXConstructorDecl::isSpecializationCopyingObject() const {
1177 if ((getNumParams() < 1) ||
1178 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1179 (getPrimaryTemplate() == 0) ||
1180 (getDescribedFunctionTemplate() != 0))
1181 return false;
1183 const ParmVarDecl *Param = getParamDecl(0);
1185 ASTContext &Context = getASTContext();
1186 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1188 // Is it the same as our our class type?
1189 CanQualType ClassTy
1190 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1191 if (ParamType.getUnqualifiedType() != ClassTy)
1192 return false;
1194 return true;
1197 CXXDestructorDecl *
1198 CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) {
1199 return new (C) CXXDestructorDecl(0, DeclarationNameInfo(),
1200 QualType(), 0, false, false);
1203 CXXDestructorDecl *
1204 CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
1205 const DeclarationNameInfo &NameInfo,
1206 QualType T, TypeSourceInfo *TInfo,
1207 bool isInline,
1208 bool isImplicitlyDeclared) {
1209 assert(NameInfo.getName().getNameKind()
1210 == DeclarationName::CXXDestructorName &&
1211 "Name must refer to a destructor");
1212 return new (C) CXXDestructorDecl(RD, NameInfo, T, TInfo, isInline,
1213 isImplicitlyDeclared);
1216 CXXConversionDecl *
1217 CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) {
1218 return new (C) CXXConversionDecl(0, DeclarationNameInfo(),
1219 QualType(), 0, false, false);
1222 CXXConversionDecl *
1223 CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
1224 const DeclarationNameInfo &NameInfo,
1225 QualType T, TypeSourceInfo *TInfo,
1226 bool isInline, bool isExplicit) {
1227 assert(NameInfo.getName().getNameKind()
1228 == DeclarationName::CXXConversionFunctionName &&
1229 "Name must refer to a conversion function");
1230 return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo,
1231 isInline, isExplicit);
1234 LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
1235 DeclContext *DC,
1236 SourceLocation L,
1237 LanguageIDs Lang, bool Braces) {
1238 return new (C) LinkageSpecDecl(DC, L, Lang, Braces);
1241 UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1242 SourceLocation L,
1243 SourceLocation NamespaceLoc,
1244 SourceRange QualifierRange,
1245 NestedNameSpecifier *Qualifier,
1246 SourceLocation IdentLoc,
1247 NamedDecl *Used,
1248 DeclContext *CommonAncestor) {
1249 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1250 Used = NS->getOriginalNamespace();
1251 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
1252 Qualifier, IdentLoc, Used, CommonAncestor);
1255 NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1256 if (NamespaceAliasDecl *NA =
1257 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1258 return NA->getNamespace();
1259 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1262 NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
1263 SourceLocation UsingLoc,
1264 SourceLocation AliasLoc,
1265 IdentifierInfo *Alias,
1266 SourceRange QualifierRange,
1267 NestedNameSpecifier *Qualifier,
1268 SourceLocation IdentLoc,
1269 NamedDecl *Namespace) {
1270 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1271 Namespace = NS->getOriginalNamespace();
1272 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange,
1273 Qualifier, IdentLoc, Namespace);
1276 UsingDecl *UsingShadowDecl::getUsingDecl() const {
1277 const UsingShadowDecl *Shadow = this;
1278 while (const UsingShadowDecl *NextShadow =
1279 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1280 Shadow = NextShadow;
1281 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1284 void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1285 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1286 "declaration already in set");
1287 assert(S->getUsingDecl() == this);
1289 if (FirstUsingShadow)
1290 S->UsingOrNextShadow = FirstUsingShadow;
1291 FirstUsingShadow = S;
1294 void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1295 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1296 "declaration not in set");
1297 assert(S->getUsingDecl() == this);
1299 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
1301 if (FirstUsingShadow == S) {
1302 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow);
1303 S->UsingOrNextShadow = this;
1304 return;
1307 UsingShadowDecl *Prev = FirstUsingShadow;
1308 while (Prev->UsingOrNextShadow != S)
1309 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
1310 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
1311 S->UsingOrNextShadow = this;
1314 UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC,
1315 SourceRange NNR, SourceLocation UL,
1316 NestedNameSpecifier* TargetNNS,
1317 const DeclarationNameInfo &NameInfo,
1318 bool IsTypeNameArg) {
1319 return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg);
1322 UnresolvedUsingValueDecl *
1323 UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
1324 SourceLocation UsingLoc,
1325 SourceRange TargetNNR,
1326 NestedNameSpecifier *TargetNNS,
1327 const DeclarationNameInfo &NameInfo) {
1328 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
1329 TargetNNR, TargetNNS, NameInfo);
1332 UnresolvedUsingTypenameDecl *
1333 UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
1334 SourceLocation UsingLoc,
1335 SourceLocation TypenameLoc,
1336 SourceRange TargetNNR,
1337 NestedNameSpecifier *TargetNNS,
1338 SourceLocation TargetNameLoc,
1339 DeclarationName TargetName) {
1340 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
1341 TargetNNR, TargetNNS,
1342 TargetNameLoc,
1343 TargetName.getAsIdentifierInfo());
1346 StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
1347 SourceLocation L, Expr *AssertExpr,
1348 StringLiteral *Message) {
1349 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message);
1352 static const char *getAccessName(AccessSpecifier AS) {
1353 switch (AS) {
1354 default:
1355 case AS_none:
1356 assert("Invalid access specifier!");
1357 return 0;
1358 case AS_public:
1359 return "public";
1360 case AS_private:
1361 return "private";
1362 case AS_protected:
1363 return "protected";
1367 const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
1368 AccessSpecifier AS) {
1369 return DB << getAccessName(AS);