1 //===---- SemaAccess.cpp - C++ Access Control -------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file provides Sema routines for C++ access control semantics.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Basic/Specifiers.h"
14 #include "clang/Sema/SemaInternal.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/CXXInheritance.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclFriend.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "clang/AST/DependentDiagnostic.h"
21 #include "clang/AST/ExprCXX.h"
22 #include "clang/Sema/DelayedDiagnostic.h"
23 #include "clang/Sema/Initialization.h"
24 #include "clang/Sema/Lookup.h"
26 using namespace clang
;
29 /// A copy of Sema's enum without AR_delayed.
36 /// SetMemberAccessSpecifier - Set the access specifier of a member.
37 /// Returns true on error (when the previous member decl access specifier
38 /// is different from the new member decl access specifier).
39 bool Sema::SetMemberAccessSpecifier(NamedDecl
*MemberDecl
,
40 NamedDecl
*PrevMemberDecl
,
41 AccessSpecifier LexicalAS
) {
42 if (!PrevMemberDecl
) {
43 // Use the lexical access specifier.
44 MemberDecl
->setAccess(LexicalAS
);
48 // C++ [class.access.spec]p3: When a member is redeclared its access
49 // specifier must be same as its initial declaration.
50 if (LexicalAS
!= AS_none
&& LexicalAS
!= PrevMemberDecl
->getAccess()) {
51 Diag(MemberDecl
->getLocation(),
52 diag::err_class_redeclared_with_different_access
)
53 << MemberDecl
<< LexicalAS
;
54 Diag(PrevMemberDecl
->getLocation(), diag::note_previous_access_declaration
)
55 << PrevMemberDecl
<< PrevMemberDecl
->getAccess();
57 MemberDecl
->setAccess(LexicalAS
);
61 MemberDecl
->setAccess(PrevMemberDecl
->getAccess());
65 static CXXRecordDecl
*FindDeclaringClass(NamedDecl
*D
) {
66 DeclContext
*DC
= D
->getDeclContext();
68 // This can only happen at top: enum decls only "publish" their
70 if (isa
<EnumDecl
>(DC
))
71 DC
= cast
<EnumDecl
>(DC
)->getDeclContext();
73 CXXRecordDecl
*DeclaringClass
= cast
<CXXRecordDecl
>(DC
);
74 while (DeclaringClass
->isAnonymousStructOrUnion())
75 DeclaringClass
= cast
<CXXRecordDecl
>(DeclaringClass
->getDeclContext());
76 return DeclaringClass
;
80 struct EffectiveContext
{
81 EffectiveContext() : Inner(nullptr), Dependent(false) {}
83 explicit EffectiveContext(DeclContext
*DC
)
85 Dependent(DC
->isDependentContext()) {
87 // An implicit deduction guide is semantically in the context enclosing the
88 // class template, but for access purposes behaves like the constructor
89 // from which it was produced.
90 if (auto *DGD
= dyn_cast
<CXXDeductionGuideDecl
>(DC
)) {
91 if (DGD
->isImplicit()) {
92 DC
= DGD
->getCorrespondingConstructor();
94 // The copy deduction candidate doesn't have a corresponding
96 DC
= cast
<DeclContext
>(DGD
->getDeducedTemplate()->getTemplatedDecl());
101 // C++11 [class.access.nest]p1:
102 // A nested class is a member and as such has the same access
103 // rights as any other member.
104 // C++11 [class.access]p2:
105 // A member of a class can also access all the names to which
106 // the class has access. A local class of a member function
107 // may access the same names that the member function itself
109 // This almost implies that the privileges of nesting are transitive.
110 // Technically it says nothing about the local classes of non-member
111 // functions (which can gain privileges through friendship), but we
112 // take that as an oversight.
114 // We want to add canonical declarations to the EC lists for
115 // simplicity of checking, but we need to walk up through the
116 // actual current DC chain. Otherwise, something like a local
117 // extern or friend which happens to be the canonical
118 // declaration will really mess us up.
120 if (isa
<CXXRecordDecl
>(DC
)) {
121 CXXRecordDecl
*Record
= cast
<CXXRecordDecl
>(DC
);
122 Records
.push_back(Record
->getCanonicalDecl());
123 DC
= Record
->getDeclContext();
124 } else if (isa
<FunctionDecl
>(DC
)) {
125 FunctionDecl
*Function
= cast
<FunctionDecl
>(DC
);
126 Functions
.push_back(Function
->getCanonicalDecl());
127 if (Function
->getFriendObjectKind())
128 DC
= Function
->getLexicalDeclContext();
130 DC
= Function
->getDeclContext();
131 } else if (DC
->isFileContext()) {
134 DC
= DC
->getParent();
139 bool isDependent() const { return Dependent
; }
141 bool includesClass(const CXXRecordDecl
*R
) const {
142 R
= R
->getCanonicalDecl();
143 return llvm::is_contained(Records
, R
);
146 /// Retrieves the innermost "useful" context. Can be null if we're
147 /// doing access-control without privileges.
148 DeclContext
*getInnerContext() const {
152 typedef SmallVectorImpl
<CXXRecordDecl
*>::const_iterator record_iterator
;
155 SmallVector
<FunctionDecl
*, 4> Functions
;
156 SmallVector
<CXXRecordDecl
*, 4> Records
;
160 /// Like sema::AccessedEntity, but kindly lets us scribble all over
162 struct AccessTarget
: public AccessedEntity
{
163 AccessTarget(const AccessedEntity
&Entity
)
164 : AccessedEntity(Entity
) {
168 AccessTarget(ASTContext
&Context
,
170 CXXRecordDecl
*NamingClass
,
171 DeclAccessPair FoundDecl
,
172 QualType BaseObjectType
)
173 : AccessedEntity(Context
.getDiagAllocator(), Member
, NamingClass
,
174 FoundDecl
, BaseObjectType
) {
178 AccessTarget(ASTContext
&Context
,
180 CXXRecordDecl
*BaseClass
,
181 CXXRecordDecl
*DerivedClass
,
182 AccessSpecifier Access
)
183 : AccessedEntity(Context
.getDiagAllocator(), Base
, BaseClass
, DerivedClass
,
188 bool isInstanceMember() const {
189 return (isMemberAccess() && getTargetDecl()->isCXXInstanceMember());
192 bool hasInstanceContext() const {
193 return HasInstanceContext
;
196 class SavedInstanceContext
{
198 SavedInstanceContext(SavedInstanceContext
&&S
)
199 : Target(S
.Target
), Has(S
.Has
) {
203 // The move assignment operator is defined as deleted pending further
205 SavedInstanceContext
&operator=(SavedInstanceContext
&&) = delete;
207 // The copy constrcutor and copy assignment operator is defined as deleted
208 // pending further motivation.
209 SavedInstanceContext(const SavedInstanceContext
&) = delete;
210 SavedInstanceContext
&operator=(const SavedInstanceContext
&) = delete;
212 ~SavedInstanceContext() {
214 Target
->HasInstanceContext
= Has
;
218 friend struct AccessTarget
;
219 explicit SavedInstanceContext(AccessTarget
&Target
)
220 : Target(&Target
), Has(Target
.HasInstanceContext
) {}
221 AccessTarget
*Target
;
225 SavedInstanceContext
saveInstanceContext() {
226 return SavedInstanceContext(*this);
229 void suppressInstanceContext() {
230 HasInstanceContext
= false;
233 const CXXRecordDecl
*resolveInstanceContext(Sema
&S
) const {
234 assert(HasInstanceContext
);
235 if (CalculatedInstanceContext
)
236 return InstanceContext
;
238 CalculatedInstanceContext
= true;
239 DeclContext
*IC
= S
.computeDeclContext(getBaseObjectType());
240 InstanceContext
= (IC
? cast
<CXXRecordDecl
>(IC
)->getCanonicalDecl()
242 return InstanceContext
;
245 const CXXRecordDecl
*getDeclaringClass() const {
246 return DeclaringClass
;
249 /// The "effective" naming class is the canonical non-anonymous
250 /// class containing the actual naming class.
251 const CXXRecordDecl
*getEffectiveNamingClass() const {
252 const CXXRecordDecl
*namingClass
= getNamingClass();
253 while (namingClass
->isAnonymousStructOrUnion())
254 namingClass
= cast
<CXXRecordDecl
>(namingClass
->getParent());
255 return namingClass
->getCanonicalDecl();
260 HasInstanceContext
= (isMemberAccess() &&
261 !getBaseObjectType().isNull() &&
262 getTargetDecl()->isCXXInstanceMember());
263 CalculatedInstanceContext
= false;
264 InstanceContext
= nullptr;
266 if (isMemberAccess())
267 DeclaringClass
= FindDeclaringClass(getTargetDecl());
269 DeclaringClass
= getBaseClass();
270 DeclaringClass
= DeclaringClass
->getCanonicalDecl();
273 bool HasInstanceContext
: 1;
274 mutable bool CalculatedInstanceContext
: 1;
275 mutable const CXXRecordDecl
*InstanceContext
;
276 const CXXRecordDecl
*DeclaringClass
;
281 /// Checks whether one class might instantiate to the other.
282 static bool MightInstantiateTo(const CXXRecordDecl
*From
,
283 const CXXRecordDecl
*To
) {
284 // Declaration names are always preserved by instantiation.
285 if (From
->getDeclName() != To
->getDeclName())
288 const DeclContext
*FromDC
= From
->getDeclContext()->getPrimaryContext();
289 const DeclContext
*ToDC
= To
->getDeclContext()->getPrimaryContext();
290 if (FromDC
== ToDC
) return true;
291 if (FromDC
->isFileContext() || ToDC
->isFileContext()) return false;
297 /// Checks whether one class is derived from another, inclusively.
298 /// Properly indicates when it couldn't be determined due to
301 /// This should probably be donated to AST or at least Sema.
302 static AccessResult
IsDerivedFromInclusive(const CXXRecordDecl
*Derived
,
303 const CXXRecordDecl
*Target
) {
304 assert(Derived
->getCanonicalDecl() == Derived
);
305 assert(Target
->getCanonicalDecl() == Target
);
307 if (Derived
== Target
) return AR_accessible
;
309 bool CheckDependent
= Derived
->isDependentContext();
310 if (CheckDependent
&& MightInstantiateTo(Derived
, Target
))
313 AccessResult OnFailure
= AR_inaccessible
;
314 SmallVector
<const CXXRecordDecl
*, 8> Queue
; // actually a stack
317 if (Derived
->isDependentContext() && !Derived
->hasDefinition() &&
318 !Derived
->isLambda())
321 for (const auto &I
: Derived
->bases()) {
322 const CXXRecordDecl
*RD
;
324 QualType T
= I
.getType();
325 if (const RecordType
*RT
= T
->getAs
<RecordType
>()) {
326 RD
= cast
<CXXRecordDecl
>(RT
->getDecl());
327 } else if (const InjectedClassNameType
*IT
328 = T
->getAs
<InjectedClassNameType
>()) {
331 assert(T
->isDependentType() && "non-dependent base wasn't a record?");
332 OnFailure
= AR_dependent
;
336 RD
= RD
->getCanonicalDecl();
337 if (RD
== Target
) return AR_accessible
;
338 if (CheckDependent
&& MightInstantiateTo(RD
, Target
))
339 OnFailure
= AR_dependent
;
344 if (Queue
.empty()) break;
346 Derived
= Queue
.pop_back_val();
353 static bool MightInstantiateTo(Sema
&S
, DeclContext
*Context
,
354 DeclContext
*Friend
) {
355 if (Friend
== Context
)
358 assert(!Friend
->isDependentContext() &&
359 "can't handle friends with dependent contexts here");
361 if (!Context
->isDependentContext())
364 if (Friend
->isFileContext())
367 // TODO: this is very conservative
371 // Asks whether the type in 'context' can ever instantiate to the type
373 static bool MightInstantiateTo(Sema
&S
, CanQualType Context
, CanQualType Friend
) {
374 if (Friend
== Context
)
377 if (!Friend
->isDependentType() && !Context
->isDependentType())
380 // TODO: this is very conservative.
384 static bool MightInstantiateTo(Sema
&S
,
385 FunctionDecl
*Context
,
386 FunctionDecl
*Friend
) {
387 if (Context
->getDeclName() != Friend
->getDeclName())
390 if (!MightInstantiateTo(S
,
391 Context
->getDeclContext(),
392 Friend
->getDeclContext()))
395 CanQual
<FunctionProtoType
> FriendTy
396 = S
.Context
.getCanonicalType(Friend
->getType())
397 ->getAs
<FunctionProtoType
>();
398 CanQual
<FunctionProtoType
> ContextTy
399 = S
.Context
.getCanonicalType(Context
->getType())
400 ->getAs
<FunctionProtoType
>();
402 // There isn't any way that I know of to add qualifiers
403 // during instantiation.
404 if (FriendTy
.getQualifiers() != ContextTy
.getQualifiers())
407 if (FriendTy
->getNumParams() != ContextTy
->getNumParams())
410 if (!MightInstantiateTo(S
, ContextTy
->getReturnType(),
411 FriendTy
->getReturnType()))
414 for (unsigned I
= 0, E
= FriendTy
->getNumParams(); I
!= E
; ++I
)
415 if (!MightInstantiateTo(S
, ContextTy
->getParamType(I
),
416 FriendTy
->getParamType(I
)))
422 static bool MightInstantiateTo(Sema
&S
,
423 FunctionTemplateDecl
*Context
,
424 FunctionTemplateDecl
*Friend
) {
425 return MightInstantiateTo(S
,
426 Context
->getTemplatedDecl(),
427 Friend
->getTemplatedDecl());
430 static AccessResult
MatchesFriend(Sema
&S
,
431 const EffectiveContext
&EC
,
432 const CXXRecordDecl
*Friend
) {
433 if (EC
.includesClass(Friend
))
434 return AR_accessible
;
436 if (EC
.isDependent()) {
437 for (const CXXRecordDecl
*Context
: EC
.Records
) {
438 if (MightInstantiateTo(Context
, Friend
))
443 return AR_inaccessible
;
446 static AccessResult
MatchesFriend(Sema
&S
,
447 const EffectiveContext
&EC
,
448 CanQualType Friend
) {
449 if (const RecordType
*RT
= Friend
->getAs
<RecordType
>())
450 return MatchesFriend(S
, EC
, cast
<CXXRecordDecl
>(RT
->getDecl()));
452 // TODO: we can do better than this
453 if (Friend
->isDependentType())
456 return AR_inaccessible
;
459 /// Determines whether the given friend class template matches
460 /// anything in the effective context.
461 static AccessResult
MatchesFriend(Sema
&S
,
462 const EffectiveContext
&EC
,
463 ClassTemplateDecl
*Friend
) {
464 AccessResult OnFailure
= AR_inaccessible
;
466 // Check whether the friend is the template of a class in the
468 for (SmallVectorImpl
<CXXRecordDecl
*>::const_iterator
469 I
= EC
.Records
.begin(), E
= EC
.Records
.end(); I
!= E
; ++I
) {
470 CXXRecordDecl
*Record
= *I
;
472 // Figure out whether the current class has a template:
473 ClassTemplateDecl
*CTD
;
475 // A specialization of the template...
476 if (isa
<ClassTemplateSpecializationDecl
>(Record
)) {
477 CTD
= cast
<ClassTemplateSpecializationDecl
>(Record
)
478 ->getSpecializedTemplate();
480 // ... or the template pattern itself.
482 CTD
= Record
->getDescribedClassTemplate();
487 if (Friend
== CTD
->getCanonicalDecl())
488 return AR_accessible
;
490 // If the context isn't dependent, it can't be a dependent match.
491 if (!EC
.isDependent())
494 // If the template names don't match, it can't be a dependent
496 if (CTD
->getDeclName() != Friend
->getDeclName())
499 // If the class's context can't instantiate to the friend's
500 // context, it can't be a dependent match.
501 if (!MightInstantiateTo(S
, CTD
->getDeclContext(),
502 Friend
->getDeclContext()))
505 // Otherwise, it's a dependent match.
506 OnFailure
= AR_dependent
;
512 /// Determines whether the given friend function matches anything in
513 /// the effective context.
514 static AccessResult
MatchesFriend(Sema
&S
,
515 const EffectiveContext
&EC
,
516 FunctionDecl
*Friend
) {
517 AccessResult OnFailure
= AR_inaccessible
;
519 for (SmallVectorImpl
<FunctionDecl
*>::const_iterator
520 I
= EC
.Functions
.begin(), E
= EC
.Functions
.end(); I
!= E
; ++I
) {
522 return AR_accessible
;
524 if (EC
.isDependent() && MightInstantiateTo(S
, *I
, Friend
))
525 OnFailure
= AR_dependent
;
531 /// Determines whether the given friend function template matches
532 /// anything in the effective context.
533 static AccessResult
MatchesFriend(Sema
&S
,
534 const EffectiveContext
&EC
,
535 FunctionTemplateDecl
*Friend
) {
536 if (EC
.Functions
.empty()) return AR_inaccessible
;
538 AccessResult OnFailure
= AR_inaccessible
;
540 for (SmallVectorImpl
<FunctionDecl
*>::const_iterator
541 I
= EC
.Functions
.begin(), E
= EC
.Functions
.end(); I
!= E
; ++I
) {
543 FunctionTemplateDecl
*FTD
= (*I
)->getPrimaryTemplate();
545 FTD
= (*I
)->getDescribedFunctionTemplate();
549 FTD
= FTD
->getCanonicalDecl();
552 return AR_accessible
;
554 if (EC
.isDependent() && MightInstantiateTo(S
, FTD
, Friend
))
555 OnFailure
= AR_dependent
;
561 /// Determines whether the given friend declaration matches anything
562 /// in the effective context.
563 static AccessResult
MatchesFriend(Sema
&S
,
564 const EffectiveContext
&EC
,
565 FriendDecl
*FriendD
) {
566 // Whitelist accesses if there's an invalid or unsupported friend
568 if (FriendD
->isInvalidDecl() || FriendD
->isUnsupportedFriend())
569 return AR_accessible
;
571 if (TypeSourceInfo
*T
= FriendD
->getFriendType())
572 return MatchesFriend(S
, EC
, T
->getType()->getCanonicalTypeUnqualified());
575 = cast
<NamedDecl
>(FriendD
->getFriendDecl()->getCanonicalDecl());
577 // FIXME: declarations with dependent or templated scope.
579 if (isa
<ClassTemplateDecl
>(Friend
))
580 return MatchesFriend(S
, EC
, cast
<ClassTemplateDecl
>(Friend
));
582 if (isa
<FunctionTemplateDecl
>(Friend
))
583 return MatchesFriend(S
, EC
, cast
<FunctionTemplateDecl
>(Friend
));
585 if (isa
<CXXRecordDecl
>(Friend
))
586 return MatchesFriend(S
, EC
, cast
<CXXRecordDecl
>(Friend
));
588 assert(isa
<FunctionDecl
>(Friend
) && "unknown friend decl kind");
589 return MatchesFriend(S
, EC
, cast
<FunctionDecl
>(Friend
));
592 static AccessResult
GetFriendKind(Sema
&S
,
593 const EffectiveContext
&EC
,
594 const CXXRecordDecl
*Class
) {
595 AccessResult OnFailure
= AR_inaccessible
;
597 // Okay, check friends.
598 for (auto *Friend
: Class
->friends()) {
599 switch (MatchesFriend(S
, EC
, Friend
)) {
601 return AR_accessible
;
603 case AR_inaccessible
:
607 OnFailure
= AR_dependent
;
612 // That's it, give up.
618 /// A helper class for checking for a friend which will grant access
619 /// to a protected instance member.
620 struct ProtectedFriendContext
{
622 const EffectiveContext
&EC
;
623 const CXXRecordDecl
*NamingClass
;
627 /// The path down to the current base class.
628 SmallVector
<const CXXRecordDecl
*, 20> CurPath
;
630 ProtectedFriendContext(Sema
&S
, const EffectiveContext
&EC
,
631 const CXXRecordDecl
*InstanceContext
,
632 const CXXRecordDecl
*NamingClass
)
633 : S(S
), EC(EC
), NamingClass(NamingClass
),
634 CheckDependent(InstanceContext
->isDependentContext() ||
635 NamingClass
->isDependentContext()),
636 EverDependent(false) {}
638 /// Check classes in the current path for friendship, starting at
640 bool checkFriendshipAlongPath(unsigned I
) {
641 assert(I
< CurPath
.size());
642 for (unsigned E
= CurPath
.size(); I
!= E
; ++I
) {
643 switch (GetFriendKind(S
, EC
, CurPath
[I
])) {
644 case AR_accessible
: return true;
645 case AR_inaccessible
: continue;
646 case AR_dependent
: EverDependent
= true; continue;
652 /// Perform a search starting at the given class.
654 /// PrivateDepth is the index of the last (least derived) class
655 /// along the current path such that a notional public member of
656 /// the final class in the path would have access in that class.
657 bool findFriendship(const CXXRecordDecl
*Cur
, unsigned PrivateDepth
) {
658 // If we ever reach the naming class, check the current path for
659 // friendship. We can also stop recursing because we obviously
660 // won't find the naming class there again.
661 if (Cur
== NamingClass
)
662 return checkFriendshipAlongPath(PrivateDepth
);
664 if (CheckDependent
&& MightInstantiateTo(Cur
, NamingClass
))
665 EverDependent
= true;
667 // Recurse into the base classes.
668 for (const auto &I
: Cur
->bases()) {
669 // If this is private inheritance, then a public member of the
670 // base will not have any access in classes derived from Cur.
671 unsigned BasePrivateDepth
= PrivateDepth
;
672 if (I
.getAccessSpecifier() == AS_private
)
673 BasePrivateDepth
= CurPath
.size() - 1;
675 const CXXRecordDecl
*RD
;
677 QualType T
= I
.getType();
678 if (const RecordType
*RT
= T
->getAs
<RecordType
>()) {
679 RD
= cast
<CXXRecordDecl
>(RT
->getDecl());
680 } else if (const InjectedClassNameType
*IT
681 = T
->getAs
<InjectedClassNameType
>()) {
684 assert(T
->isDependentType() && "non-dependent base wasn't a record?");
685 EverDependent
= true;
689 // Recurse. We don't need to clean up if this returns true.
690 CurPath
.push_back(RD
);
691 if (findFriendship(RD
->getCanonicalDecl(), BasePrivateDepth
))
699 bool findFriendship(const CXXRecordDecl
*Cur
) {
700 assert(CurPath
.empty());
701 CurPath
.push_back(Cur
);
702 return findFriendship(Cur
, 0);
707 /// Search for a class P that EC is a friend of, under the constraint
708 /// InstanceContext <= P
709 /// if InstanceContext exists, or else
711 /// and with the additional restriction that a protected member of
712 /// NamingClass would have some natural access in P, which implicitly
713 /// imposes the constraint that P <= NamingClass.
715 /// This isn't quite the condition laid out in the standard.
716 /// Instead of saying that a notional protected member of NamingClass
717 /// would have to have some natural access in P, it says the actual
718 /// target has to have some natural access in P, which opens up the
719 /// possibility that the target (which is not necessarily a member
720 /// of NamingClass) might be more accessible along some path not
721 /// passing through it. That's really a bad idea, though, because it
722 /// introduces two problems:
723 /// - Most importantly, it breaks encapsulation because you can
724 /// access a forbidden base class's members by directly subclassing
726 /// - It also makes access substantially harder to compute because it
727 /// breaks the hill-climbing algorithm: knowing that the target is
728 /// accessible in some base class would no longer let you change
729 /// the question solely to whether the base class is accessible,
730 /// because the original target might have been more accessible
731 /// because of crazy subclassing.
732 /// So we don't implement that.
733 static AccessResult
GetProtectedFriendKind(Sema
&S
, const EffectiveContext
&EC
,
734 const CXXRecordDecl
*InstanceContext
,
735 const CXXRecordDecl
*NamingClass
) {
736 assert(InstanceContext
== nullptr ||
737 InstanceContext
->getCanonicalDecl() == InstanceContext
);
738 assert(NamingClass
->getCanonicalDecl() == NamingClass
);
740 // If we don't have an instance context, our constraints give us
741 // that NamingClass <= P <= NamingClass, i.e. P == NamingClass.
742 // This is just the usual friendship check.
743 if (!InstanceContext
) return GetFriendKind(S
, EC
, NamingClass
);
745 ProtectedFriendContext
PRC(S
, EC
, InstanceContext
, NamingClass
);
746 if (PRC
.findFriendship(InstanceContext
)) return AR_accessible
;
747 if (PRC
.EverDependent
) return AR_dependent
;
748 return AR_inaccessible
;
751 static AccessResult
HasAccess(Sema
&S
,
752 const EffectiveContext
&EC
,
753 const CXXRecordDecl
*NamingClass
,
754 AccessSpecifier Access
,
755 const AccessTarget
&Target
) {
756 assert(NamingClass
->getCanonicalDecl() == NamingClass
&&
757 "declaration should be canonicalized before being passed here");
759 if (Access
== AS_public
) return AR_accessible
;
760 assert(Access
== AS_private
|| Access
== AS_protected
);
762 AccessResult OnFailure
= AR_inaccessible
;
764 for (EffectiveContext::record_iterator
765 I
= EC
.Records
.begin(), E
= EC
.Records
.end(); I
!= E
; ++I
) {
766 // All the declarations in EC have been canonicalized, so pointer
767 // equality from this point on will work fine.
768 const CXXRecordDecl
*ECRecord
= *I
;
771 if (Access
== AS_private
) {
772 if (ECRecord
== NamingClass
)
773 return AR_accessible
;
775 if (EC
.isDependent() && MightInstantiateTo(ECRecord
, NamingClass
))
776 OnFailure
= AR_dependent
;
780 assert(Access
== AS_protected
);
781 switch (IsDerivedFromInclusive(ECRecord
, NamingClass
)) {
782 case AR_accessible
: break;
783 case AR_inaccessible
: continue;
784 case AR_dependent
: OnFailure
= AR_dependent
; continue;
787 // C++ [class.protected]p1:
788 // An additional access check beyond those described earlier in
789 // [class.access] is applied when a non-static data member or
790 // non-static member function is a protected member of its naming
791 // class. As described earlier, access to a protected member is
792 // granted because the reference occurs in a friend or member of
793 // some class C. If the access is to form a pointer to member,
794 // the nested-name-specifier shall name C or a class derived from
795 // C. All other accesses involve a (possibly implicit) object
796 // expression. In this case, the class of the object expression
797 // shall be C or a class derived from C.
799 // We interpret this as a restriction on [M3].
801 // In this part of the code, 'C' is just our context class ECRecord.
803 // These rules are different if we don't have an instance context.
804 if (!Target
.hasInstanceContext()) {
805 // If it's not an instance member, these restrictions don't apply.
806 if (!Target
.isInstanceMember()) return AR_accessible
;
808 // If it's an instance member, use the pointer-to-member rule
809 // that the naming class has to be derived from the effective
812 // Emulate a MSVC bug where the creation of pointer-to-member
813 // to protected member of base class is allowed but only from
814 // static member functions.
815 if (S
.getLangOpts().MSVCCompat
&& !EC
.Functions
.empty())
816 if (CXXMethodDecl
* MD
= dyn_cast
<CXXMethodDecl
>(EC
.Functions
.front()))
817 if (MD
->isStatic()) return AR_accessible
;
819 // Despite the standard's confident wording, there is a case
820 // where you can have an instance member that's neither in a
821 // pointer-to-member expression nor in a member access: when
822 // it names a field in an unevaluated context that can't be an
823 // implicit member. Pending clarification, we just apply the
824 // same naming-class restriction here.
825 // FIXME: we're probably not correctly adding the
826 // protected-member restriction when we retroactively convert
827 // an expression to being evaluated.
829 // We know that ECRecord derives from NamingClass. The
830 // restriction says to check whether NamingClass derives from
831 // ECRecord, but that's not really necessary: two distinct
832 // classes can't be recursively derived from each other. So
833 // along this path, we just need to check whether the classes
835 if (NamingClass
== ECRecord
) return AR_accessible
;
837 // Otherwise, this context class tells us nothing; on to the next.
841 assert(Target
.isInstanceMember());
843 const CXXRecordDecl
*InstanceContext
= Target
.resolveInstanceContext(S
);
844 if (!InstanceContext
) {
845 OnFailure
= AR_dependent
;
849 switch (IsDerivedFromInclusive(InstanceContext
, ECRecord
)) {
850 case AR_accessible
: return AR_accessible
;
851 case AR_inaccessible
: continue;
852 case AR_dependent
: OnFailure
= AR_dependent
; continue;
857 // [M3] and [B3] say that, if the target is protected in N, we grant
858 // access if the access occurs in a friend or member of some class P
859 // that's a subclass of N and where the target has some natural
860 // access in P. The 'member' aspect is easy to handle because P
861 // would necessarily be one of the effective-context records, and we
862 // address that above. The 'friend' aspect is completely ridiculous
863 // to implement because there are no restrictions at all on P
864 // *unless* the [class.protected] restriction applies. If it does,
865 // however, we should ignore whether the naming class is a friend,
866 // and instead rely on whether any potential P is a friend.
867 if (Access
== AS_protected
&& Target
.isInstanceMember()) {
868 // Compute the instance context if possible.
869 const CXXRecordDecl
*InstanceContext
= nullptr;
870 if (Target
.hasInstanceContext()) {
871 InstanceContext
= Target
.resolveInstanceContext(S
);
872 if (!InstanceContext
) return AR_dependent
;
875 switch (GetProtectedFriendKind(S
, EC
, InstanceContext
, NamingClass
)) {
876 case AR_accessible
: return AR_accessible
;
877 case AR_inaccessible
: return OnFailure
;
878 case AR_dependent
: return AR_dependent
;
880 llvm_unreachable("impossible friendship kind");
883 switch (GetFriendKind(S
, EC
, NamingClass
)) {
884 case AR_accessible
: return AR_accessible
;
885 case AR_inaccessible
: return OnFailure
;
886 case AR_dependent
: return AR_dependent
;
889 // Silence bogus warnings
890 llvm_unreachable("impossible friendship kind");
893 /// Finds the best path from the naming class to the declaring class,
894 /// taking friend declarations into account.
896 /// C++0x [class.access.base]p5:
897 /// A member m is accessible at the point R when named in class N if
898 /// [M1] m as a member of N is public, or
899 /// [M2] m as a member of N is private, and R occurs in a member or
900 /// friend of class N, or
901 /// [M3] m as a member of N is protected, and R occurs in a member or
902 /// friend of class N, or in a member or friend of a class P
903 /// derived from N, where m as a member of P is public, private,
905 /// [M4] there exists a base class B of N that is accessible at R, and
906 /// m is accessible at R when named in class B.
908 /// C++0x [class.access.base]p4:
909 /// A base class B of N is accessible at R, if
910 /// [B1] an invented public member of B would be a public member of N, or
911 /// [B2] R occurs in a member or friend of class N, and an invented public
912 /// member of B would be a private or protected member of N, or
913 /// [B3] R occurs in a member or friend of a class P derived from N, and an
914 /// invented public member of B would be a private or protected member
916 /// [B4] there exists a class S such that B is a base class of S accessible
917 /// at R and S is a base class of N accessible at R.
919 /// Along a single inheritance path we can restate both of these
922 /// First, we note that M1-4 are equivalent to B1-4 if the member is
923 /// treated as a notional base of its declaring class with inheritance
924 /// access equivalent to the member's access. Therefore we need only
925 /// ask whether a class B is accessible from a class N in context R.
927 /// Let B_1 .. B_n be the inheritance path in question (i.e. where
928 /// B_1 = N, B_n = B, and for all i, B_{i+1} is a direct base class of
929 /// B_i). For i in 1..n, we will calculate ACAB(i), the access to the
930 /// closest accessible base in the path:
931 /// Access(a, b) = (* access on the base specifier from a to b *)
932 /// Merge(a, forbidden) = forbidden
933 /// Merge(a, private) = forbidden
934 /// Merge(a, b) = min(a,b)
935 /// Accessible(c, forbidden) = false
936 /// Accessible(c, private) = (R is c) || IsFriend(c, R)
937 /// Accessible(c, protected) = (R derived from c) || IsFriend(c, R)
938 /// Accessible(c, public) = true
941 /// let AccessToBase = Merge(Access(B_i, B_{i+1}), ACAB(i+1)) in
942 /// if Accessible(B_i, AccessToBase) then public else AccessToBase
944 /// B is an accessible base of N at R iff ACAB(1) = public.
946 /// \param FinalAccess the access of the "final step", or AS_public if
947 /// there is no final step.
948 /// \return null if friendship is dependent
949 static CXXBasePath
*FindBestPath(Sema
&S
,
950 const EffectiveContext
&EC
,
951 AccessTarget
&Target
,
952 AccessSpecifier FinalAccess
,
953 CXXBasePaths
&Paths
) {
954 // Derive the paths to the desired base.
955 const CXXRecordDecl
*Derived
= Target
.getNamingClass();
956 const CXXRecordDecl
*Base
= Target
.getDeclaringClass();
958 // FIXME: fail correctly when there are dependent paths.
959 bool isDerived
= Derived
->isDerivedFrom(const_cast<CXXRecordDecl
*>(Base
),
961 assert(isDerived
&& "derived class not actually derived from base");
964 CXXBasePath
*BestPath
= nullptr;
966 assert(FinalAccess
!= AS_none
&& "forbidden access after declaring class");
968 bool AnyDependent
= false;
970 // Derive the friend-modified access along each path.
971 for (CXXBasePaths::paths_iterator PI
= Paths
.begin(), PE
= Paths
.end();
973 AccessTarget::SavedInstanceContext _
= Target
.saveInstanceContext();
975 // Walk through the path backwards.
976 AccessSpecifier PathAccess
= FinalAccess
;
977 CXXBasePath::iterator I
= PI
->end(), E
= PI
->begin();
981 assert(PathAccess
!= AS_none
);
983 // If the declaration is a private member of a base class, there
984 // is no level of friendship in derived classes that can make it
986 if (PathAccess
== AS_private
) {
987 PathAccess
= AS_none
;
991 const CXXRecordDecl
*NC
= I
->Class
->getCanonicalDecl();
993 AccessSpecifier BaseAccess
= I
->Base
->getAccessSpecifier();
994 PathAccess
= std::max(PathAccess
, BaseAccess
);
996 switch (HasAccess(S
, EC
, NC
, PathAccess
, Target
)) {
997 case AR_inaccessible
: break;
999 PathAccess
= AS_public
;
1001 // Future tests are not against members and so do not have
1002 // instance context.
1003 Target
.suppressInstanceContext();
1006 AnyDependent
= true;
1011 // Note that we modify the path's Access field to the
1012 // friend-modified access.
1013 if (BestPath
== nullptr || PathAccess
< BestPath
->Access
) {
1015 BestPath
->Access
= PathAccess
;
1017 // Short-circuit if we found a public path.
1018 if (BestPath
->Access
== AS_public
)
1025 assert((!BestPath
|| BestPath
->Access
!= AS_public
) &&
1026 "fell out of loop with public path");
1028 // We didn't find a public path, but at least one path was subject
1029 // to dependent friendship, so delay the check.
1036 /// Given that an entity has protected natural access, check whether
1037 /// access might be denied because of the protected member access
1040 /// \return true if a note was emitted
1041 static bool TryDiagnoseProtectedAccess(Sema
&S
, const EffectiveContext
&EC
,
1042 AccessTarget
&Target
) {
1043 // Only applies to instance accesses.
1044 if (!Target
.isInstanceMember())
1047 assert(Target
.isMemberAccess());
1049 const CXXRecordDecl
*NamingClass
= Target
.getEffectiveNamingClass();
1051 for (EffectiveContext::record_iterator
1052 I
= EC
.Records
.begin(), E
= EC
.Records
.end(); I
!= E
; ++I
) {
1053 const CXXRecordDecl
*ECRecord
= *I
;
1054 switch (IsDerivedFromInclusive(ECRecord
, NamingClass
)) {
1055 case AR_accessible
: break;
1056 case AR_inaccessible
: continue;
1057 case AR_dependent
: continue;
1060 // The effective context is a subclass of the declaring class.
1061 // Check whether the [class.protected] restriction is limiting
1064 // To get this exactly right, this might need to be checked more
1065 // holistically; it's not necessarily the case that gaining
1066 // access here would grant us access overall.
1068 NamedDecl
*D
= Target
.getTargetDecl();
1070 // If we don't have an instance context, [class.protected] says the
1071 // naming class has to equal the context class.
1072 if (!Target
.hasInstanceContext()) {
1073 // If it does, the restriction doesn't apply.
1074 if (NamingClass
== ECRecord
) continue;
1076 // TODO: it would be great to have a fixit here, since this is
1077 // such an obvious error.
1078 S
.Diag(D
->getLocation(), diag::note_access_protected_restricted_noobject
)
1079 << S
.Context
.getTypeDeclType(ECRecord
);
1083 const CXXRecordDecl
*InstanceContext
= Target
.resolveInstanceContext(S
);
1084 assert(InstanceContext
&& "diagnosing dependent access");
1086 switch (IsDerivedFromInclusive(InstanceContext
, ECRecord
)) {
1087 case AR_accessible
: continue;
1088 case AR_dependent
: continue;
1089 case AR_inaccessible
:
1093 // Okay, the restriction seems to be what's limiting us.
1095 // Use a special diagnostic for constructors and destructors.
1096 if (isa
<CXXConstructorDecl
>(D
) || isa
<CXXDestructorDecl
>(D
) ||
1097 (isa
<FunctionTemplateDecl
>(D
) &&
1098 isa
<CXXConstructorDecl
>(
1099 cast
<FunctionTemplateDecl
>(D
)->getTemplatedDecl()))) {
1100 return S
.Diag(D
->getLocation(),
1101 diag::note_access_protected_restricted_ctordtor
)
1102 << isa
<CXXDestructorDecl
>(D
->getAsFunction());
1105 // Otherwise, use the generic diagnostic.
1106 return S
.Diag(D
->getLocation(),
1107 diag::note_access_protected_restricted_object
)
1108 << S
.Context
.getTypeDeclType(ECRecord
);
1114 /// We are unable to access a given declaration due to its direct
1115 /// access control; diagnose that.
1116 static void diagnoseBadDirectAccess(Sema
&S
,
1117 const EffectiveContext
&EC
,
1118 AccessTarget
&entity
) {
1119 assert(entity
.isMemberAccess());
1120 NamedDecl
*D
= entity
.getTargetDecl();
1122 if (D
->getAccess() == AS_protected
&&
1123 TryDiagnoseProtectedAccess(S
, EC
, entity
))
1126 // Find an original declaration.
1127 while (D
->isOutOfLine()) {
1128 NamedDecl
*PrevDecl
= nullptr;
1129 if (VarDecl
*VD
= dyn_cast
<VarDecl
>(D
))
1130 PrevDecl
= VD
->getPreviousDecl();
1131 else if (FunctionDecl
*FD
= dyn_cast
<FunctionDecl
>(D
))
1132 PrevDecl
= FD
->getPreviousDecl();
1133 else if (TypedefNameDecl
*TND
= dyn_cast
<TypedefNameDecl
>(D
))
1134 PrevDecl
= TND
->getPreviousDecl();
1135 else if (TagDecl
*TD
= dyn_cast
<TagDecl
>(D
)) {
1136 if (isa
<RecordDecl
>(D
) && cast
<RecordDecl
>(D
)->isInjectedClassName())
1138 PrevDecl
= TD
->getPreviousDecl();
1140 if (!PrevDecl
) break;
1144 CXXRecordDecl
*DeclaringClass
= FindDeclaringClass(D
);
1145 Decl
*ImmediateChild
;
1146 if (D
->getDeclContext() == DeclaringClass
)
1149 DeclContext
*DC
= D
->getDeclContext();
1150 while (DC
->getParent() != DeclaringClass
)
1151 DC
= DC
->getParent();
1152 ImmediateChild
= cast
<Decl
>(DC
);
1155 // Check whether there's an AccessSpecDecl preceding this in the
1156 // chain of the DeclContext.
1157 bool isImplicit
= true;
1158 for (const auto *I
: DeclaringClass
->decls()) {
1159 if (I
== ImmediateChild
) break;
1160 if (isa
<AccessSpecDecl
>(I
)) {
1166 S
.Diag(D
->getLocation(), diag::note_access_natural
)
1167 << (unsigned) (D
->getAccess() == AS_protected
)
1171 /// Diagnose the path which caused the given declaration or base class
1172 /// to become inaccessible.
1173 static void DiagnoseAccessPath(Sema
&S
,
1174 const EffectiveContext
&EC
,
1175 AccessTarget
&entity
) {
1176 // Save the instance context to preserve invariants.
1177 AccessTarget::SavedInstanceContext _
= entity
.saveInstanceContext();
1179 // This basically repeats the main algorithm but keeps some more
1182 // The natural access so far.
1183 AccessSpecifier accessSoFar
= AS_public
;
1185 // Check whether we have special rights to the declaring class.
1186 if (entity
.isMemberAccess()) {
1187 NamedDecl
*D
= entity
.getTargetDecl();
1188 accessSoFar
= D
->getAccess();
1189 const CXXRecordDecl
*declaringClass
= entity
.getDeclaringClass();
1191 switch (HasAccess(S
, EC
, declaringClass
, accessSoFar
, entity
)) {
1192 // If the declaration is accessible when named in its declaring
1193 // class, then we must be constrained by the path.
1195 accessSoFar
= AS_public
;
1196 entity
.suppressInstanceContext();
1199 case AR_inaccessible
:
1200 if (accessSoFar
== AS_private
||
1201 declaringClass
== entity
.getEffectiveNamingClass())
1202 return diagnoseBadDirectAccess(S
, EC
, entity
);
1206 llvm_unreachable("cannot diagnose dependent access");
1211 CXXBasePath
&path
= *FindBestPath(S
, EC
, entity
, accessSoFar
, paths
);
1212 assert(path
.Access
!= AS_public
);
1214 CXXBasePath::iterator i
= path
.end(), e
= path
.begin();
1215 CXXBasePath::iterator constrainingBase
= i
;
1219 assert(accessSoFar
!= AS_none
&& accessSoFar
!= AS_private
);
1221 // Is the entity accessible when named in the deriving class, as
1222 // modified by the base specifier?
1223 const CXXRecordDecl
*derivingClass
= i
->Class
->getCanonicalDecl();
1224 const CXXBaseSpecifier
*base
= i
->Base
;
1226 // If the access to this base is worse than the access we have to
1227 // the declaration, remember it.
1228 AccessSpecifier baseAccess
= base
->getAccessSpecifier();
1229 if (baseAccess
> accessSoFar
) {
1230 constrainingBase
= i
;
1231 accessSoFar
= baseAccess
;
1234 switch (HasAccess(S
, EC
, derivingClass
, accessSoFar
, entity
)) {
1235 case AR_inaccessible
: break;
1237 accessSoFar
= AS_public
;
1238 entity
.suppressInstanceContext();
1239 constrainingBase
= nullptr;
1242 llvm_unreachable("cannot diagnose dependent access");
1245 // If this was private inheritance, but we don't have access to
1246 // the deriving class, we're done.
1247 if (accessSoFar
== AS_private
) {
1248 assert(baseAccess
== AS_private
);
1249 assert(constrainingBase
== i
);
1254 // If we don't have a constraining base, the access failure must be
1255 // due to the original declaration.
1256 if (constrainingBase
== path
.end())
1257 return diagnoseBadDirectAccess(S
, EC
, entity
);
1259 // We're constrained by inheritance, but we want to say
1260 // "declared private here" if we're diagnosing a hierarchy
1261 // conversion and this is the final step.
1262 unsigned diagnostic
;
1263 if (entity
.isMemberAccess() ||
1264 constrainingBase
+ 1 != path
.end()) {
1265 diagnostic
= diag::note_access_constrained_by_path
;
1267 diagnostic
= diag::note_access_natural
;
1270 const CXXBaseSpecifier
*base
= constrainingBase
->Base
;
1272 S
.Diag(base
->getSourceRange().getBegin(), diagnostic
)
1273 << base
->getSourceRange()
1274 << (base
->getAccessSpecifier() == AS_protected
)
1275 << (base
->getAccessSpecifierAsWritten() == AS_none
);
1277 if (entity
.isMemberAccess())
1278 S
.Diag(entity
.getTargetDecl()->getLocation(),
1279 diag::note_member_declared_at
);
1282 static void DiagnoseBadAccess(Sema
&S
, SourceLocation Loc
,
1283 const EffectiveContext
&EC
,
1284 AccessTarget
&Entity
) {
1285 const CXXRecordDecl
*NamingClass
= Entity
.getNamingClass();
1286 const CXXRecordDecl
*DeclaringClass
= Entity
.getDeclaringClass();
1287 NamedDecl
*D
= (Entity
.isMemberAccess() ? Entity
.getTargetDecl() : nullptr);
1289 S
.Diag(Loc
, Entity
.getDiag())
1290 << (Entity
.getAccess() == AS_protected
)
1291 << (D
? D
->getDeclName() : DeclarationName())
1292 << S
.Context
.getTypeDeclType(NamingClass
)
1293 << S
.Context
.getTypeDeclType(DeclaringClass
);
1294 DiagnoseAccessPath(S
, EC
, Entity
);
1297 /// MSVC has a bug where if during an using declaration name lookup,
1298 /// the declaration found is unaccessible (private) and that declaration
1299 /// was bring into scope via another using declaration whose target
1300 /// declaration is accessible (public) then no error is generated.
1306 /// class B : public A {
1310 /// class C : public B {
1315 /// Here, B::f is private so this should fail in Standard C++, but
1316 /// because B::f refers to A::f which is public MSVC accepts it.
1317 static bool IsMicrosoftUsingDeclarationAccessBug(Sema
& S
,
1318 SourceLocation AccessLoc
,
1319 AccessTarget
&Entity
) {
1320 if (UsingShadowDecl
*Shadow
=
1321 dyn_cast
<UsingShadowDecl
>(Entity
.getTargetDecl()))
1322 if (UsingDecl
*UD
= dyn_cast
<UsingDecl
>(Shadow
->getIntroducer())) {
1323 const NamedDecl
*OrigDecl
= Entity
.getTargetDecl()->getUnderlyingDecl();
1324 if (Entity
.getTargetDecl()->getAccess() == AS_private
&&
1325 (OrigDecl
->getAccess() == AS_public
||
1326 OrigDecl
->getAccess() == AS_protected
)) {
1327 S
.Diag(AccessLoc
, diag::ext_ms_using_declaration_inaccessible
)
1328 << UD
->getQualifiedNameAsString()
1329 << OrigDecl
->getQualifiedNameAsString();
1336 /// Determines whether the accessed entity is accessible. Public members
1337 /// have been weeded out by this point.
1338 static AccessResult
IsAccessible(Sema
&S
,
1339 const EffectiveContext
&EC
,
1340 AccessTarget
&Entity
) {
1341 // Determine the actual naming class.
1342 const CXXRecordDecl
*NamingClass
= Entity
.getEffectiveNamingClass();
1344 AccessSpecifier UnprivilegedAccess
= Entity
.getAccess();
1345 assert(UnprivilegedAccess
!= AS_public
&& "public access not weeded out");
1347 // Before we try to recalculate access paths, try to white-list
1348 // accesses which just trade in on the final step, i.e. accesses
1349 // which don't require [M4] or [B4]. These are by far the most
1350 // common forms of privileged access.
1351 if (UnprivilegedAccess
!= AS_none
) {
1352 switch (HasAccess(S
, EC
, NamingClass
, UnprivilegedAccess
, Entity
)) {
1354 // This is actually an interesting policy decision. We don't
1355 // *have* to delay immediately here: we can do the full access
1356 // calculation in the hope that friendship on some intermediate
1357 // class will make the declaration accessible non-dependently.
1358 // But that's not cheap, and odds are very good (note: assertion
1359 // made without data) that the friend declaration will determine
1361 return AR_dependent
;
1363 case AR_accessible
: return AR_accessible
;
1364 case AR_inaccessible
: break;
1368 AccessTarget::SavedInstanceContext _
= Entity
.saveInstanceContext();
1370 // We lower member accesses to base accesses by pretending that the
1371 // member is a base class of its declaring class.
1372 AccessSpecifier FinalAccess
;
1374 if (Entity
.isMemberAccess()) {
1375 // Determine if the declaration is accessible from EC when named
1376 // in its declaring class.
1377 NamedDecl
*Target
= Entity
.getTargetDecl();
1378 const CXXRecordDecl
*DeclaringClass
= Entity
.getDeclaringClass();
1380 FinalAccess
= Target
->getAccess();
1381 switch (HasAccess(S
, EC
, DeclaringClass
, FinalAccess
, Entity
)) {
1383 // Target is accessible at EC when named in its declaring class.
1384 // We can now hill-climb and simply check whether the declaring
1385 // class is accessible as a base of the naming class. This is
1386 // equivalent to checking the access of a notional public
1387 // member with no instance context.
1388 FinalAccess
= AS_public
;
1389 Entity
.suppressInstanceContext();
1391 case AR_inaccessible
: break;
1392 case AR_dependent
: return AR_dependent
; // see above
1395 if (DeclaringClass
== NamingClass
)
1396 return (FinalAccess
== AS_public
? AR_accessible
: AR_inaccessible
);
1398 FinalAccess
= AS_public
;
1401 assert(Entity
.getDeclaringClass() != NamingClass
);
1403 // Append the declaration's access if applicable.
1405 CXXBasePath
*Path
= FindBestPath(S
, EC
, Entity
, FinalAccess
, Paths
);
1407 return AR_dependent
;
1409 assert(Path
->Access
<= UnprivilegedAccess
&&
1410 "access along best path worse than direct?");
1411 if (Path
->Access
== AS_public
)
1412 return AR_accessible
;
1413 return AR_inaccessible
;
1416 static void DelayDependentAccess(Sema
&S
,
1417 const EffectiveContext
&EC
,
1419 const AccessTarget
&Entity
) {
1420 assert(EC
.isDependent() && "delaying non-dependent access");
1421 DeclContext
*DC
= EC
.getInnerContext();
1422 assert(DC
->isDependentContext() && "delaying non-dependent access");
1423 DependentDiagnostic::Create(S
.Context
, DC
, DependentDiagnostic::Access
,
1425 Entity
.isMemberAccess(),
1427 Entity
.getTargetDecl(),
1428 Entity
.getNamingClass(),
1429 Entity
.getBaseObjectType(),
1433 /// Checks access to an entity from the given effective context.
1434 static AccessResult
CheckEffectiveAccess(Sema
&S
,
1435 const EffectiveContext
&EC
,
1437 AccessTarget
&Entity
) {
1438 assert(Entity
.getAccess() != AS_public
&& "called for public access!");
1440 switch (IsAccessible(S
, EC
, Entity
)) {
1442 DelayDependentAccess(S
, EC
, Loc
, Entity
);
1443 return AR_dependent
;
1445 case AR_inaccessible
:
1446 if (S
.getLangOpts().MSVCCompat
&&
1447 IsMicrosoftUsingDeclarationAccessBug(S
, Loc
, Entity
))
1448 return AR_accessible
;
1449 if (!Entity
.isQuiet())
1450 DiagnoseBadAccess(S
, Loc
, EC
, Entity
);
1451 return AR_inaccessible
;
1454 return AR_accessible
;
1457 // silence unnecessary warning
1458 llvm_unreachable("invalid access result");
1461 static Sema::AccessResult
CheckAccess(Sema
&S
, SourceLocation Loc
,
1462 AccessTarget
&Entity
) {
1463 // If the access path is public, it's accessible everywhere.
1464 if (Entity
.getAccess() == AS_public
)
1465 return Sema::AR_accessible
;
1467 // If we're currently parsing a declaration, we may need to delay
1468 // access control checking, because our effective context might be
1469 // different based on what the declaration comes out as.
1471 // For example, we might be parsing a declaration with a scope
1472 // specifier, like this:
1473 // A::private_type A::foo() { ... }
1475 // Or we might be parsing something that will turn out to be a friend:
1476 // void foo(A::private_type);
1477 // void B::foo(A::private_type);
1478 if (S
.DelayedDiagnostics
.shouldDelayDiagnostics()) {
1479 S
.DelayedDiagnostics
.add(DelayedDiagnostic::makeAccess(Loc
, Entity
));
1480 return Sema::AR_delayed
;
1483 EffectiveContext
EC(S
.CurContext
);
1484 switch (CheckEffectiveAccess(S
, EC
, Loc
, Entity
)) {
1485 case AR_accessible
: return Sema::AR_accessible
;
1486 case AR_inaccessible
: return Sema::AR_inaccessible
;
1487 case AR_dependent
: return Sema::AR_dependent
;
1489 llvm_unreachable("invalid access result");
1492 void Sema::HandleDelayedAccessCheck(DelayedDiagnostic
&DD
, Decl
*D
) {
1493 // Access control for names used in the declarations of functions
1494 // and function templates should normally be evaluated in the context
1495 // of the declaration, just in case it's a friend of something.
1496 // However, this does not apply to local extern declarations.
1498 DeclContext
*DC
= D
->getDeclContext();
1499 if (D
->isLocalExternDecl()) {
1500 DC
= D
->getLexicalDeclContext();
1501 } else if (FunctionDecl
*FN
= dyn_cast
<FunctionDecl
>(D
)) {
1503 } else if (TemplateDecl
*TD
= dyn_cast
<TemplateDecl
>(D
)) {
1504 if (isa
<DeclContext
>(TD
->getTemplatedDecl()))
1505 DC
= cast
<DeclContext
>(TD
->getTemplatedDecl());
1506 } else if (auto *RD
= dyn_cast
<RequiresExprBodyDecl
>(D
)) {
1510 EffectiveContext
EC(DC
);
1512 AccessTarget
Target(DD
.getAccessData());
1514 if (CheckEffectiveAccess(*this, EC
, DD
.Loc
, Target
) == ::AR_inaccessible
)
1515 DD
.Triggered
= true;
1518 void Sema::HandleDependentAccessCheck(const DependentDiagnostic
&DD
,
1519 const MultiLevelTemplateArgumentList
&TemplateArgs
) {
1520 SourceLocation Loc
= DD
.getAccessLoc();
1521 AccessSpecifier Access
= DD
.getAccess();
1523 Decl
*NamingD
= FindInstantiatedDecl(Loc
, DD
.getAccessNamingClass(),
1525 if (!NamingD
) return;
1526 Decl
*TargetD
= FindInstantiatedDecl(Loc
, DD
.getAccessTarget(),
1528 if (!TargetD
) return;
1530 if (DD
.isAccessToMember()) {
1531 CXXRecordDecl
*NamingClass
= cast
<CXXRecordDecl
>(NamingD
);
1532 NamedDecl
*TargetDecl
= cast
<NamedDecl
>(TargetD
);
1533 QualType BaseObjectType
= DD
.getAccessBaseObjectType();
1534 if (!BaseObjectType
.isNull()) {
1535 BaseObjectType
= SubstType(BaseObjectType
, TemplateArgs
, Loc
,
1537 if (BaseObjectType
.isNull()) return;
1540 AccessTarget
Entity(Context
,
1541 AccessTarget::Member
,
1543 DeclAccessPair::make(TargetDecl
, Access
),
1545 Entity
.setDiag(DD
.getDiagnostic());
1546 CheckAccess(*this, Loc
, Entity
);
1548 AccessTarget
Entity(Context
,
1550 cast
<CXXRecordDecl
>(TargetD
),
1551 cast
<CXXRecordDecl
>(NamingD
),
1553 Entity
.setDiag(DD
.getDiagnostic());
1554 CheckAccess(*this, Loc
, Entity
);
1558 Sema::AccessResult
Sema::CheckUnresolvedLookupAccess(UnresolvedLookupExpr
*E
,
1559 DeclAccessPair Found
) {
1560 if (!getLangOpts().AccessControl
||
1561 !E
->getNamingClass() ||
1562 Found
.getAccess() == AS_public
)
1563 return AR_accessible
;
1565 AccessTarget
Entity(Context
, AccessTarget::Member
, E
->getNamingClass(),
1567 Entity
.setDiag(diag::err_access
) << E
->getSourceRange();
1569 return CheckAccess(*this, E
->getNameLoc(), Entity
);
1572 /// Perform access-control checking on a previously-unresolved member
1573 /// access which has now been resolved to a member.
1574 Sema::AccessResult
Sema::CheckUnresolvedMemberAccess(UnresolvedMemberExpr
*E
,
1575 DeclAccessPair Found
) {
1576 if (!getLangOpts().AccessControl
||
1577 Found
.getAccess() == AS_public
)
1578 return AR_accessible
;
1580 QualType BaseType
= E
->getBaseType();
1582 BaseType
= BaseType
->castAs
<PointerType
>()->getPointeeType();
1584 AccessTarget
Entity(Context
, AccessTarget::Member
, E
->getNamingClass(),
1586 Entity
.setDiag(diag::err_access
) << E
->getSourceRange();
1588 return CheckAccess(*this, E
->getMemberLoc(), Entity
);
1591 /// Is the given member accessible for the purposes of deciding whether to
1592 /// define a special member function as deleted?
1593 bool Sema::isMemberAccessibleForDeletion(CXXRecordDecl
*NamingClass
,
1594 DeclAccessPair Found
,
1595 QualType ObjectType
,
1597 const PartialDiagnostic
&Diag
) {
1599 if (Found
.getAccess() == AS_public
|| !getLangOpts().AccessControl
)
1602 AccessTarget
Entity(Context
, AccessTarget::Member
, NamingClass
, Found
,
1605 // Suppress diagnostics.
1606 Entity
.setDiag(Diag
);
1608 switch (CheckAccess(*this, Loc
, Entity
)) {
1609 case AR_accessible
: return true;
1610 case AR_inaccessible
: return false;
1611 case AR_dependent
: llvm_unreachable("dependent for =delete computation");
1612 case AR_delayed
: llvm_unreachable("cannot delay =delete computation");
1614 llvm_unreachable("bad access result");
1617 Sema::AccessResult
Sema::CheckDestructorAccess(SourceLocation Loc
,
1618 CXXDestructorDecl
*Dtor
,
1619 const PartialDiagnostic
&PDiag
,
1620 QualType ObjectTy
) {
1621 if (!getLangOpts().AccessControl
)
1622 return AR_accessible
;
1624 // There's never a path involved when checking implicit destructor access.
1625 AccessSpecifier Access
= Dtor
->getAccess();
1626 if (Access
== AS_public
)
1627 return AR_accessible
;
1629 CXXRecordDecl
*NamingClass
= Dtor
->getParent();
1630 if (ObjectTy
.isNull()) ObjectTy
= Context
.getTypeDeclType(NamingClass
);
1632 AccessTarget
Entity(Context
, AccessTarget::Member
, NamingClass
,
1633 DeclAccessPair::make(Dtor
, Access
),
1635 Entity
.setDiag(PDiag
); // TODO: avoid copy
1637 return CheckAccess(*this, Loc
, Entity
);
1640 /// Checks access to a constructor.
1641 Sema::AccessResult
Sema::CheckConstructorAccess(SourceLocation UseLoc
,
1642 CXXConstructorDecl
*Constructor
,
1643 DeclAccessPair Found
,
1644 const InitializedEntity
&Entity
,
1645 bool IsCopyBindingRefToTemp
) {
1646 if (!getLangOpts().AccessControl
|| Found
.getAccess() == AS_public
)
1647 return AR_accessible
;
1649 PartialDiagnostic
PD(PDiag());
1650 switch (Entity
.getKind()) {
1652 PD
= PDiag(IsCopyBindingRefToTemp
1653 ? diag::ext_rvalue_to_reference_access_ctor
1654 : diag::err_access_ctor
);
1658 case InitializedEntity::EK_Base
:
1659 PD
= PDiag(diag::err_access_base_ctor
);
1660 PD
<< Entity
.isInheritedVirtualBase()
1661 << Entity
.getBaseSpecifier()->getType() << getSpecialMember(Constructor
);
1664 case InitializedEntity::EK_Member
:
1665 case InitializedEntity::EK_ParenAggInitMember
: {
1666 const FieldDecl
*Field
= cast
<FieldDecl
>(Entity
.getDecl());
1667 PD
= PDiag(diag::err_access_field_ctor
);
1668 PD
<< Field
->getType() << getSpecialMember(Constructor
);
1672 case InitializedEntity::EK_LambdaCapture
: {
1673 StringRef VarName
= Entity
.getCapturedVarName();
1674 PD
= PDiag(diag::err_access_lambda_capture
);
1675 PD
<< VarName
<< Entity
.getType() << getSpecialMember(Constructor
);
1681 return CheckConstructorAccess(UseLoc
, Constructor
, Found
, Entity
, PD
);
1684 /// Checks access to a constructor.
1685 Sema::AccessResult
Sema::CheckConstructorAccess(SourceLocation UseLoc
,
1686 CXXConstructorDecl
*Constructor
,
1687 DeclAccessPair Found
,
1688 const InitializedEntity
&Entity
,
1689 const PartialDiagnostic
&PD
) {
1690 if (!getLangOpts().AccessControl
||
1691 Found
.getAccess() == AS_public
)
1692 return AR_accessible
;
1694 CXXRecordDecl
*NamingClass
= Constructor
->getParent();
1696 // Initializing a base sub-object is an instance method call on an
1697 // object of the derived class. Otherwise, we have an instance method
1698 // call on an object of the constructed type.
1700 // FIXME: If we have a parent, we're initializing the base class subobject
1701 // in aggregate initialization. It's not clear whether the object class
1702 // should be the base class or the derived class in that case.
1703 CXXRecordDecl
*ObjectClass
;
1704 if ((Entity
.getKind() == InitializedEntity::EK_Base
||
1705 Entity
.getKind() == InitializedEntity::EK_Delegating
) &&
1706 !Entity
.getParent()) {
1707 ObjectClass
= cast
<CXXConstructorDecl
>(CurContext
)->getParent();
1708 } else if (auto *Shadow
=
1709 dyn_cast
<ConstructorUsingShadowDecl
>(Found
.getDecl())) {
1710 // If we're using an inheriting constructor to construct an object,
1711 // the object class is the derived class, not the base class.
1712 ObjectClass
= Shadow
->getParent();
1714 ObjectClass
= NamingClass
;
1717 AccessTarget
AccessEntity(
1718 Context
, AccessTarget::Member
, NamingClass
,
1719 DeclAccessPair::make(Constructor
, Found
.getAccess()),
1720 Context
.getTypeDeclType(ObjectClass
));
1721 AccessEntity
.setDiag(PD
);
1723 return CheckAccess(*this, UseLoc
, AccessEntity
);
1726 /// Checks access to an overloaded operator new or delete.
1727 Sema::AccessResult
Sema::CheckAllocationAccess(SourceLocation OpLoc
,
1728 SourceRange PlacementRange
,
1729 CXXRecordDecl
*NamingClass
,
1730 DeclAccessPair Found
,
1732 if (!getLangOpts().AccessControl
||
1734 Found
.getAccess() == AS_public
)
1735 return AR_accessible
;
1737 AccessTarget
Entity(Context
, AccessTarget::Member
, NamingClass
, Found
,
1740 Entity
.setDiag(diag::err_access
)
1743 return CheckAccess(*this, OpLoc
, Entity
);
1746 /// Checks access to a member.
1747 Sema::AccessResult
Sema::CheckMemberAccess(SourceLocation UseLoc
,
1748 CXXRecordDecl
*NamingClass
,
1749 DeclAccessPair Found
) {
1750 if (!getLangOpts().AccessControl
||
1752 Found
.getAccess() == AS_public
)
1753 return AR_accessible
;
1755 AccessTarget
Entity(Context
, AccessTarget::Member
, NamingClass
,
1758 return CheckAccess(*this, UseLoc
, Entity
);
1761 /// Checks implicit access to a member in a structured binding.
1763 Sema::CheckStructuredBindingMemberAccess(SourceLocation UseLoc
,
1764 CXXRecordDecl
*DecomposedClass
,
1765 DeclAccessPair Field
) {
1766 if (!getLangOpts().AccessControl
||
1767 Field
.getAccess() == AS_public
)
1768 return AR_accessible
;
1770 AccessTarget
Entity(Context
, AccessTarget::Member
, DecomposedClass
, Field
,
1771 Context
.getRecordType(DecomposedClass
));
1772 Entity
.setDiag(diag::err_decomp_decl_inaccessible_field
);
1774 return CheckAccess(*this, UseLoc
, Entity
);
1777 Sema::AccessResult
Sema::CheckMemberOperatorAccess(SourceLocation OpLoc
,
1779 const SourceRange
&Range
,
1780 DeclAccessPair Found
) {
1781 if (!getLangOpts().AccessControl
|| Found
.getAccess() == AS_public
)
1782 return AR_accessible
;
1784 const RecordType
*RT
= ObjectExpr
->getType()->castAs
<RecordType
>();
1785 CXXRecordDecl
*NamingClass
= cast
<CXXRecordDecl
>(RT
->getDecl());
1787 AccessTarget
Entity(Context
, AccessTarget::Member
, NamingClass
, Found
,
1788 ObjectExpr
->getType());
1789 Entity
.setDiag(diag::err_access
) << ObjectExpr
->getSourceRange() << Range
;
1791 return CheckAccess(*this, OpLoc
, Entity
);
1794 /// Checks access to an overloaded member operator, including
1795 /// conversion operators.
1796 Sema::AccessResult
Sema::CheckMemberOperatorAccess(SourceLocation OpLoc
,
1799 DeclAccessPair Found
) {
1800 return CheckMemberOperatorAccess(
1801 OpLoc
, ObjectExpr
, ArgExpr
? ArgExpr
->getSourceRange() : SourceRange(),
1805 Sema::AccessResult
Sema::CheckMemberOperatorAccess(SourceLocation OpLoc
,
1807 ArrayRef
<Expr
*> ArgExprs
,
1808 DeclAccessPair FoundDecl
) {
1810 if (!ArgExprs
.empty()) {
1811 R
= SourceRange(ArgExprs
.front()->getBeginLoc(),
1812 ArgExprs
.back()->getEndLoc());
1815 return CheckMemberOperatorAccess(OpLoc
, ObjectExpr
, R
, FoundDecl
);
1818 /// Checks access to the target of a friend declaration.
1819 Sema::AccessResult
Sema::CheckFriendAccess(NamedDecl
*target
) {
1820 assert(isa
<CXXMethodDecl
>(target
->getAsFunction()));
1822 // Friendship lookup is a redeclaration lookup, so there's never an
1823 // inheritance path modifying access.
1824 AccessSpecifier access
= target
->getAccess();
1826 if (!getLangOpts().AccessControl
|| access
== AS_public
)
1827 return AR_accessible
;
1829 CXXMethodDecl
*method
= cast
<CXXMethodDecl
>(target
->getAsFunction());
1831 AccessTarget
entity(Context
, AccessTarget::Member
,
1832 cast
<CXXRecordDecl
>(target
->getDeclContext()),
1833 DeclAccessPair::make(target
, access
),
1834 /*no instance context*/ QualType());
1835 entity
.setDiag(diag::err_access_friend_function
)
1836 << (method
->getQualifier() ? method
->getQualifierLoc().getSourceRange()
1837 : method
->getNameInfo().getSourceRange());
1839 // We need to bypass delayed-diagnostics because we might be called
1840 // while the ParsingDeclarator is active.
1841 EffectiveContext
EC(CurContext
);
1842 switch (CheckEffectiveAccess(*this, EC
, target
->getLocation(), entity
)) {
1843 case ::AR_accessible
: return Sema::AR_accessible
;
1844 case ::AR_inaccessible
: return Sema::AR_inaccessible
;
1845 case ::AR_dependent
: return Sema::AR_dependent
;
1847 llvm_unreachable("invalid access result");
1850 Sema::AccessResult
Sema::CheckAddressOfMemberAccess(Expr
*OvlExpr
,
1851 DeclAccessPair Found
) {
1852 if (!getLangOpts().AccessControl
||
1853 Found
.getAccess() == AS_none
||
1854 Found
.getAccess() == AS_public
)
1855 return AR_accessible
;
1857 OverloadExpr
*Ovl
= OverloadExpr::find(OvlExpr
).Expression
;
1858 CXXRecordDecl
*NamingClass
= Ovl
->getNamingClass();
1860 AccessTarget
Entity(Context
, AccessTarget::Member
, NamingClass
, Found
,
1861 /*no instance context*/ QualType());
1862 Entity
.setDiag(diag::err_access
)
1863 << Ovl
->getSourceRange();
1865 return CheckAccess(*this, Ovl
->getNameLoc(), Entity
);
1868 /// Checks access for a hierarchy conversion.
1870 /// \param ForceCheck true if this check should be performed even if access
1871 /// control is disabled; some things rely on this for semantics
1872 /// \param ForceUnprivileged true if this check should proceed as if the
1873 /// context had no special privileges
1874 Sema::AccessResult
Sema::CheckBaseClassAccess(SourceLocation AccessLoc
,
1877 const CXXBasePath
&Path
,
1880 bool ForceUnprivileged
) {
1881 if (!ForceCheck
&& !getLangOpts().AccessControl
)
1882 return AR_accessible
;
1884 if (Path
.Access
== AS_public
)
1885 return AR_accessible
;
1887 CXXRecordDecl
*BaseD
, *DerivedD
;
1888 BaseD
= cast
<CXXRecordDecl
>(Base
->castAs
<RecordType
>()->getDecl());
1889 DerivedD
= cast
<CXXRecordDecl
>(Derived
->castAs
<RecordType
>()->getDecl());
1891 AccessTarget
Entity(Context
, AccessTarget::Base
, BaseD
, DerivedD
,
1894 Entity
.setDiag(DiagID
) << Derived
<< Base
;
1896 if (ForceUnprivileged
) {
1897 switch (CheckEffectiveAccess(*this, EffectiveContext(),
1898 AccessLoc
, Entity
)) {
1899 case ::AR_accessible
: return Sema::AR_accessible
;
1900 case ::AR_inaccessible
: return Sema::AR_inaccessible
;
1901 case ::AR_dependent
: return Sema::AR_dependent
;
1903 llvm_unreachable("unexpected result from CheckEffectiveAccess");
1905 return CheckAccess(*this, AccessLoc
, Entity
);
1908 /// Checks access to all the declarations in the given result set.
1909 void Sema::CheckLookupAccess(const LookupResult
&R
) {
1910 assert(getLangOpts().AccessControl
1911 && "performing access check without access control");
1912 assert(R
.getNamingClass() && "performing access check without naming class");
1914 for (LookupResult::iterator I
= R
.begin(), E
= R
.end(); I
!= E
; ++I
) {
1915 if (I
.getAccess() != AS_public
) {
1916 AccessTarget
Entity(Context
, AccessedEntity::Member
,
1917 R
.getNamingClass(), I
.getPair(),
1918 R
.getBaseObjectType());
1919 Entity
.setDiag(diag::err_access
);
1920 CheckAccess(*this, R
.getNameLoc(), Entity
);
1925 /// Checks access to Target from the given class. The check will take access
1926 /// specifiers into account, but no member access expressions and such.
1928 /// \param Target the declaration to check if it can be accessed
1929 /// \param NamingClass the class in which the lookup was started.
1930 /// \param BaseType type of the left side of member access expression.
1931 /// \p BaseType and \p NamingClass are used for C++ access control.
1932 /// Depending on the lookup case, they should be set to the following:
1933 /// - lhs.target (member access without a qualifier):
1934 /// \p BaseType and \p NamingClass are both the type of 'lhs'.
1935 /// - lhs.X::target (member access with a qualifier):
1936 /// BaseType is the type of 'lhs', NamingClass is 'X'
1937 /// - X::target (qualified lookup without member access):
1938 /// BaseType is null, NamingClass is 'X'.
1939 /// - target (unqualified lookup).
1940 /// BaseType is null, NamingClass is the parent class of 'target'.
1941 /// \return true if the Target is accessible from the Class, false otherwise.
1942 bool Sema::IsSimplyAccessible(NamedDecl
*Target
, CXXRecordDecl
*NamingClass
,
1943 QualType BaseType
) {
1944 // Perform the C++ accessibility checks first.
1945 if (Target
->isCXXClassMember() && NamingClass
) {
1946 if (!getLangOpts().CPlusPlus
)
1948 // The unprivileged access is AS_none as we don't know how the member was
1949 // accessed, which is described by the access in DeclAccessPair.
1950 // `IsAccessible` will examine the actual access of Target (i.e.
1951 // Decl->getAccess()) when calculating the access.
1952 AccessTarget
Entity(Context
, AccessedEntity::Member
, NamingClass
,
1953 DeclAccessPair::make(Target
, AS_none
), BaseType
);
1954 EffectiveContext
EC(CurContext
);
1955 return ::IsAccessible(*this, EC
, Entity
) != ::AR_inaccessible
;
1958 if (ObjCIvarDecl
*Ivar
= dyn_cast
<ObjCIvarDecl
>(Target
)) {
1959 // @public and @package ivars are always accessible.
1960 if (Ivar
->getCanonicalAccessControl() == ObjCIvarDecl::Public
||
1961 Ivar
->getCanonicalAccessControl() == ObjCIvarDecl::Package
)
1964 // If we are inside a class or category implementation, determine the
1965 // interface we're in.
1966 ObjCInterfaceDecl
*ClassOfMethodDecl
= nullptr;
1967 if (ObjCMethodDecl
*MD
= getCurMethodDecl())
1968 ClassOfMethodDecl
= MD
->getClassInterface();
1969 else if (FunctionDecl
*FD
= getCurFunctionDecl()) {
1970 if (ObjCImplDecl
*Impl
1971 = dyn_cast
<ObjCImplDecl
>(FD
->getLexicalDeclContext())) {
1972 if (ObjCImplementationDecl
*IMPD
1973 = dyn_cast
<ObjCImplementationDecl
>(Impl
))
1974 ClassOfMethodDecl
= IMPD
->getClassInterface();
1975 else if (ObjCCategoryImplDecl
* CatImplClass
1976 = dyn_cast
<ObjCCategoryImplDecl
>(Impl
))
1977 ClassOfMethodDecl
= CatImplClass
->getClassInterface();
1981 // If we're not in an interface, this ivar is inaccessible.
1982 if (!ClassOfMethodDecl
)
1985 // If we're inside the same interface that owns the ivar, we're fine.
1986 if (declaresSameEntity(ClassOfMethodDecl
, Ivar
->getContainingInterface()))
1989 // If the ivar is private, it's inaccessible.
1990 if (Ivar
->getCanonicalAccessControl() == ObjCIvarDecl::Private
)
1993 return Ivar
->getContainingInterface()->isSuperClassOf(ClassOfMethodDecl
);