Anders Carlsson | 4742a9c | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 1 | //===---- SemaAccess.cpp - C++ Access Control -------------------*- C++ -*-===// |
Anders Carlsson | 8ed6f36 | 2009-03-27 04:43:36 +0000 | [diff] [blame] | 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 provides Sema routines for C++ access control semantics. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Anders Carlsson | 1794112 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 13 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
| 17 | #include "clang/AST/DeclCXX.h" |
John McCall | 16927f6 | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | 21ceb18 | 2011-11-03 19:00:24 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 20 | #include "clang/AST/DependentDiagnostic.h" |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Sema/DelayedDiagnostic.h" |
| 23 | #include "clang/Sema/Initialization.h" |
| 24 | #include "clang/Sema/Lookup.h" |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 25 | |
Anders Carlsson | 1794112 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 26 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 27 | using namespace sema; |
Anders Carlsson | 1794112 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 28 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 29 | /// A copy of Sema's enum without AR_delayed. |
| 30 | enum AccessResult { |
| 31 | AR_accessible, |
| 32 | AR_inaccessible, |
| 33 | AR_dependent |
| 34 | }; |
| 35 | |
Anders Carlsson | 4742a9c | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 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). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | bool Sema::SetMemberAccessSpecifier(NamedDecl *MemberDecl, |
Anders Carlsson | 1794112 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 40 | NamedDecl *PrevMemberDecl, |
| 41 | AccessSpecifier LexicalAS) { |
| 42 | if (!PrevMemberDecl) { |
| 43 | // Use the lexical access specifier. |
| 44 | MemberDecl->setAccess(LexicalAS); |
| 45 | return false; |
| 46 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Anders Carlsson | 1794112 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 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()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | Diag(MemberDecl->getLocation(), |
| 52 | diag::err_class_redeclared_with_different_access) |
Anders Carlsson | 1794112 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 53 | << MemberDecl << LexicalAS; |
| 54 | Diag(PrevMemberDecl->getLocation(), diag::note_previous_access_declaration) |
| 55 | << PrevMemberDecl << PrevMemberDecl->getAccess(); |
John McCall | 0a4bb26 | 2009-12-23 00:37:40 +0000 | [diff] [blame] | 56 | |
| 57 | MemberDecl->setAccess(LexicalAS); |
Anders Carlsson | 1794112 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 58 | return true; |
| 59 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Anders Carlsson | 1794112 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 61 | MemberDecl->setAccess(PrevMemberDecl->getAccess()); |
| 62 | return false; |
| 63 | } |
Anders Carlsson | 4742a9c | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 64 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 65 | static CXXRecordDecl *FindDeclaringClass(NamedDecl *D) { |
| 66 | DeclContext *DC = D->getDeclContext(); |
| 67 | |
| 68 | // This can only happen at top: enum decls only "publish" their |
| 69 | // immediate members. |
| 70 | if (isa<EnumDecl>(DC)) |
| 71 | DC = cast<EnumDecl>(DC)->getDeclContext(); |
| 72 | |
| 73 | CXXRecordDecl *DeclaringClass = cast<CXXRecordDecl>(DC); |
| 74 | while (DeclaringClass->isAnonymousStructOrUnion()) |
| 75 | DeclaringClass = cast<CXXRecordDecl>(DeclaringClass->getDeclContext()); |
| 76 | return DeclaringClass; |
| 77 | } |
| 78 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 79 | namespace { |
| 80 | struct EffectiveContext { |
John McCall | 3dc81f7 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 81 | EffectiveContext() : Inner(0), Dependent(false) {} |
Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 82 | |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 83 | explicit EffectiveContext(DeclContext *DC) |
| 84 | : Inner(DC), |
| 85 | Dependent(DC->isDependentContext()) { |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 86 | |
Richard Smith | ad5c1ca | 2013-04-29 10:13:55 +0000 | [diff] [blame] | 87 | // C++11 [class.access.nest]p1: |
John McCall | fb803d7 | 2010-03-17 04:58:56 +0000 | [diff] [blame] | 88 | // A nested class is a member and as such has the same access |
| 89 | // rights as any other member. |
Richard Smith | ad5c1ca | 2013-04-29 10:13:55 +0000 | [diff] [blame] | 90 | // C++11 [class.access]p2: |
John McCall | fb803d7 | 2010-03-17 04:58:56 +0000 | [diff] [blame] | 91 | // A member of a class can also access all the names to which |
John McCall | 3dc81f7 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 92 | // the class has access. A local class of a member function |
| 93 | // may access the same names that the member function itself |
| 94 | // may access. |
| 95 | // This almost implies that the privileges of nesting are transitive. |
| 96 | // Technically it says nothing about the local classes of non-member |
| 97 | // functions (which can gain privileges through friendship), but we |
| 98 | // take that as an oversight. |
| 99 | while (true) { |
John McCall | e91aec7 | 2012-08-24 22:54:02 +0000 | [diff] [blame] | 100 | // We want to add canonical declarations to the EC lists for |
| 101 | // simplicity of checking, but we need to walk up through the |
| 102 | // actual current DC chain. Otherwise, something like a local |
| 103 | // extern or friend which happens to be the canonical |
| 104 | // declaration will really mess us up. |
| 105 | |
John McCall | 3dc81f7 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 106 | if (isa<CXXRecordDecl>(DC)) { |
John McCall | e91aec7 | 2012-08-24 22:54:02 +0000 | [diff] [blame] | 107 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 108 | Records.push_back(Record->getCanonicalDecl()); |
John McCall | 3dc81f7 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 109 | DC = Record->getDeclContext(); |
| 110 | } else if (isa<FunctionDecl>(DC)) { |
John McCall | e91aec7 | 2012-08-24 22:54:02 +0000 | [diff] [blame] | 111 | FunctionDecl *Function = cast<FunctionDecl>(DC); |
| 112 | Functions.push_back(Function->getCanonicalDecl()); |
Douglas Gregor | 5663658 | 2011-10-09 22:38:36 +0000 | [diff] [blame] | 113 | if (Function->getFriendObjectKind()) |
| 114 | DC = Function->getLexicalDeclContext(); |
| 115 | else |
| 116 | DC = Function->getDeclContext(); |
John McCall | 3dc81f7 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 117 | } else if (DC->isFileContext()) { |
| 118 | break; |
| 119 | } else { |
| 120 | DC = DC->getParent(); |
| 121 | } |
John McCall | fb803d7 | 2010-03-17 04:58:56 +0000 | [diff] [blame] | 122 | } |
Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 123 | } |
Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 124 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 125 | bool isDependent() const { return Dependent; } |
| 126 | |
John McCall | fb803d7 | 2010-03-17 04:58:56 +0000 | [diff] [blame] | 127 | bool includesClass(const CXXRecordDecl *R) const { |
| 128 | R = R->getCanonicalDecl(); |
| 129 | return std::find(Records.begin(), Records.end(), R) |
| 130 | != Records.end(); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 131 | } |
| 132 | |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 133 | /// Retrieves the innermost "useful" context. Can be null if we're |
| 134 | /// doing access-control without privileges. |
| 135 | DeclContext *getInnerContext() const { |
| 136 | return Inner; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 139 | typedef SmallVectorImpl<CXXRecordDecl*>::const_iterator record_iterator; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 140 | |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 141 | DeclContext *Inner; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 142 | SmallVector<FunctionDecl*, 4> Functions; |
| 143 | SmallVector<CXXRecordDecl*, 4> Records; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 144 | bool Dependent; |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 145 | }; |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 146 | |
Nico Weber | 20c9f1d | 2010-11-28 22:53:37 +0000 | [diff] [blame] | 147 | /// Like sema::AccessedEntity, but kindly lets us scribble all over |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 148 | /// it. |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 149 | struct AccessTarget : public AccessedEntity { |
| 150 | AccessTarget(const AccessedEntity &Entity) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 151 | : AccessedEntity(Entity) { |
| 152 | initialize(); |
| 153 | } |
| 154 | |
| 155 | AccessTarget(ASTContext &Context, |
| 156 | MemberNonce _, |
| 157 | CXXRecordDecl *NamingClass, |
| 158 | DeclAccessPair FoundDecl, |
Erik Verbruggen | 631dfc6 | 2011-09-19 15:10:40 +0000 | [diff] [blame] | 159 | QualType BaseObjectType) |
Benjamin Kramer | 1ea8e09 | 2012-07-04 17:04:04 +0000 | [diff] [blame] | 160 | : AccessedEntity(Context.getDiagAllocator(), Member, NamingClass, |
| 161 | FoundDecl, BaseObjectType) { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 162 | initialize(); |
| 163 | } |
| 164 | |
| 165 | AccessTarget(ASTContext &Context, |
| 166 | BaseNonce _, |
| 167 | CXXRecordDecl *BaseClass, |
| 168 | CXXRecordDecl *DerivedClass, |
| 169 | AccessSpecifier Access) |
Benjamin Kramer | 1ea8e09 | 2012-07-04 17:04:04 +0000 | [diff] [blame] | 170 | : AccessedEntity(Context.getDiagAllocator(), Base, BaseClass, DerivedClass, |
| 171 | Access) { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 172 | initialize(); |
| 173 | } |
| 174 | |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 175 | bool isInstanceMember() const { |
| 176 | return (isMemberAccess() && getTargetDecl()->isCXXInstanceMember()); |
| 177 | } |
| 178 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 179 | bool hasInstanceContext() const { |
| 180 | return HasInstanceContext; |
| 181 | } |
| 182 | |
| 183 | class SavedInstanceContext { |
| 184 | public: |
| 185 | ~SavedInstanceContext() { |
| 186 | Target.HasInstanceContext = Has; |
| 187 | } |
| 188 | |
| 189 | private: |
John McCall | 8e36d53 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 190 | friend struct AccessTarget; |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 191 | explicit SavedInstanceContext(AccessTarget &Target) |
| 192 | : Target(Target), Has(Target.HasInstanceContext) {} |
| 193 | AccessTarget &Target; |
| 194 | bool Has; |
| 195 | }; |
| 196 | |
| 197 | SavedInstanceContext saveInstanceContext() { |
| 198 | return SavedInstanceContext(*this); |
| 199 | } |
| 200 | |
| 201 | void suppressInstanceContext() { |
| 202 | HasInstanceContext = false; |
| 203 | } |
| 204 | |
| 205 | const CXXRecordDecl *resolveInstanceContext(Sema &S) const { |
| 206 | assert(HasInstanceContext); |
| 207 | if (CalculatedInstanceContext) |
| 208 | return InstanceContext; |
| 209 | |
| 210 | CalculatedInstanceContext = true; |
| 211 | DeclContext *IC = S.computeDeclContext(getBaseObjectType()); |
| 212 | InstanceContext = (IC ? cast<CXXRecordDecl>(IC)->getCanonicalDecl() : 0); |
| 213 | return InstanceContext; |
| 214 | } |
| 215 | |
| 216 | const CXXRecordDecl *getDeclaringClass() const { |
| 217 | return DeclaringClass; |
| 218 | } |
| 219 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 220 | /// The "effective" naming class is the canonical non-anonymous |
| 221 | /// class containing the actual naming class. |
| 222 | const CXXRecordDecl *getEffectiveNamingClass() const { |
| 223 | const CXXRecordDecl *namingClass = getNamingClass(); |
| 224 | while (namingClass->isAnonymousStructOrUnion()) |
| 225 | namingClass = cast<CXXRecordDecl>(namingClass->getParent()); |
| 226 | return namingClass->getCanonicalDecl(); |
| 227 | } |
| 228 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 229 | private: |
| 230 | void initialize() { |
| 231 | HasInstanceContext = (isMemberAccess() && |
| 232 | !getBaseObjectType().isNull() && |
| 233 | getTargetDecl()->isCXXInstanceMember()); |
| 234 | CalculatedInstanceContext = false; |
| 235 | InstanceContext = 0; |
| 236 | |
| 237 | if (isMemberAccess()) |
| 238 | DeclaringClass = FindDeclaringClass(getTargetDecl()); |
| 239 | else |
| 240 | DeclaringClass = getBaseClass(); |
| 241 | DeclaringClass = DeclaringClass->getCanonicalDecl(); |
| 242 | } |
| 243 | |
| 244 | bool HasInstanceContext : 1; |
| 245 | mutable bool CalculatedInstanceContext : 1; |
| 246 | mutable const CXXRecordDecl *InstanceContext; |
| 247 | const CXXRecordDecl *DeclaringClass; |
| 248 | }; |
| 249 | |
Anders Carlsson | 4742a9c | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 250 | } |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 251 | |
John McCall | 9720514 | 2010-05-04 05:11:27 +0000 | [diff] [blame] | 252 | /// Checks whether one class might instantiate to the other. |
| 253 | static bool MightInstantiateTo(const CXXRecordDecl *From, |
| 254 | const CXXRecordDecl *To) { |
| 255 | // Declaration names are always preserved by instantiation. |
| 256 | if (From->getDeclName() != To->getDeclName()) |
| 257 | return false; |
| 258 | |
| 259 | const DeclContext *FromDC = From->getDeclContext()->getPrimaryContext(); |
| 260 | const DeclContext *ToDC = To->getDeclContext()->getPrimaryContext(); |
| 261 | if (FromDC == ToDC) return true; |
| 262 | if (FromDC->isFileContext() || ToDC->isFileContext()) return false; |
| 263 | |
| 264 | // Be conservative. |
| 265 | return true; |
| 266 | } |
| 267 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 268 | /// Checks whether one class is derived from another, inclusively. |
| 269 | /// Properly indicates when it couldn't be determined due to |
| 270 | /// dependence. |
| 271 | /// |
| 272 | /// This should probably be donated to AST or at least Sema. |
| 273 | static AccessResult IsDerivedFromInclusive(const CXXRecordDecl *Derived, |
| 274 | const CXXRecordDecl *Target) { |
| 275 | assert(Derived->getCanonicalDecl() == Derived); |
| 276 | assert(Target->getCanonicalDecl() == Target); |
John McCall | 69f7586 | 2010-03-24 09:04:37 +0000 | [diff] [blame] | 277 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 278 | if (Derived == Target) return AR_accessible; |
John McCall | 69f7586 | 2010-03-24 09:04:37 +0000 | [diff] [blame] | 279 | |
John McCall | 9720514 | 2010-05-04 05:11:27 +0000 | [diff] [blame] | 280 | bool CheckDependent = Derived->isDependentContext(); |
| 281 | if (CheckDependent && MightInstantiateTo(Derived, Target)) |
| 282 | return AR_dependent; |
| 283 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 284 | AccessResult OnFailure = AR_inaccessible; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 285 | SmallVector<const CXXRecordDecl*, 8> Queue; // actually a stack |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 286 | |
| 287 | while (true) { |
Douglas Gregor | 0201a4c | 2011-11-14 23:00:43 +0000 | [diff] [blame] | 288 | if (Derived->isDependentContext() && !Derived->hasDefinition()) |
| 289 | return AR_dependent; |
| 290 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 291 | for (CXXRecordDecl::base_class_const_iterator |
| 292 | I = Derived->bases_begin(), E = Derived->bases_end(); I != E; ++I) { |
| 293 | |
| 294 | const CXXRecordDecl *RD; |
| 295 | |
| 296 | QualType T = I->getType(); |
| 297 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 298 | RD = cast<CXXRecordDecl>(RT->getDecl()); |
John McCall | 9720514 | 2010-05-04 05:11:27 +0000 | [diff] [blame] | 299 | } else if (const InjectedClassNameType *IT |
| 300 | = T->getAs<InjectedClassNameType>()) { |
| 301 | RD = IT->getDecl(); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 302 | } else { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 303 | assert(T->isDependentType() && "non-dependent base wasn't a record?"); |
| 304 | OnFailure = AR_dependent; |
| 305 | continue; |
| 306 | } |
| 307 | |
| 308 | RD = RD->getCanonicalDecl(); |
| 309 | if (RD == Target) return AR_accessible; |
John McCall | 9720514 | 2010-05-04 05:11:27 +0000 | [diff] [blame] | 310 | if (CheckDependent && MightInstantiateTo(RD, Target)) |
| 311 | OnFailure = AR_dependent; |
| 312 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 313 | Queue.push_back(RD); |
| 314 | } |
| 315 | |
| 316 | if (Queue.empty()) break; |
| 317 | |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 318 | Derived = Queue.pop_back_val(); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | return OnFailure; |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 322 | } |
| 323 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 324 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 325 | static bool MightInstantiateTo(Sema &S, DeclContext *Context, |
| 326 | DeclContext *Friend) { |
| 327 | if (Friend == Context) |
| 328 | return true; |
| 329 | |
| 330 | assert(!Friend->isDependentContext() && |
| 331 | "can't handle friends with dependent contexts here"); |
| 332 | |
| 333 | if (!Context->isDependentContext()) |
| 334 | return false; |
| 335 | |
| 336 | if (Friend->isFileContext()) |
| 337 | return false; |
| 338 | |
| 339 | // TODO: this is very conservative |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | // Asks whether the type in 'context' can ever instantiate to the type |
| 344 | // in 'friend'. |
| 345 | static bool MightInstantiateTo(Sema &S, CanQualType Context, CanQualType Friend) { |
| 346 | if (Friend == Context) |
| 347 | return true; |
| 348 | |
| 349 | if (!Friend->isDependentType() && !Context->isDependentType()) |
| 350 | return false; |
| 351 | |
| 352 | // TODO: this is very conservative. |
| 353 | return true; |
| 354 | } |
| 355 | |
| 356 | static bool MightInstantiateTo(Sema &S, |
| 357 | FunctionDecl *Context, |
| 358 | FunctionDecl *Friend) { |
| 359 | if (Context->getDeclName() != Friend->getDeclName()) |
| 360 | return false; |
| 361 | |
| 362 | if (!MightInstantiateTo(S, |
| 363 | Context->getDeclContext(), |
| 364 | Friend->getDeclContext())) |
| 365 | return false; |
| 366 | |
| 367 | CanQual<FunctionProtoType> FriendTy |
| 368 | = S.Context.getCanonicalType(Friend->getType()) |
| 369 | ->getAs<FunctionProtoType>(); |
| 370 | CanQual<FunctionProtoType> ContextTy |
| 371 | = S.Context.getCanonicalType(Context->getType()) |
| 372 | ->getAs<FunctionProtoType>(); |
| 373 | |
| 374 | // There isn't any way that I know of to add qualifiers |
| 375 | // during instantiation. |
| 376 | if (FriendTy.getQualifiers() != ContextTy.getQualifiers()) |
| 377 | return false; |
| 378 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 379 | if (FriendTy->getNumParams() != ContextTy->getNumParams()) |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 380 | return false; |
| 381 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 382 | if (!MightInstantiateTo(S, ContextTy->getReturnType(), |
| 383 | FriendTy->getReturnType())) |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 384 | return false; |
| 385 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 386 | for (unsigned I = 0, E = FriendTy->getNumParams(); I != E; ++I) |
| 387 | if (!MightInstantiateTo(S, ContextTy->getParamType(I), |
| 388 | FriendTy->getParamType(I))) |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 389 | return false; |
| 390 | |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | static bool MightInstantiateTo(Sema &S, |
| 395 | FunctionTemplateDecl *Context, |
| 396 | FunctionTemplateDecl *Friend) { |
| 397 | return MightInstantiateTo(S, |
| 398 | Context->getTemplatedDecl(), |
| 399 | Friend->getTemplatedDecl()); |
| 400 | } |
| 401 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 402 | static AccessResult MatchesFriend(Sema &S, |
| 403 | const EffectiveContext &EC, |
| 404 | const CXXRecordDecl *Friend) { |
John McCall | 39e8288 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 405 | if (EC.includesClass(Friend)) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 406 | return AR_accessible; |
John McCall | 39e8288 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 407 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 408 | if (EC.isDependent()) { |
| 409 | CanQualType FriendTy |
| 410 | = S.Context.getCanonicalType(S.Context.getTypeDeclType(Friend)); |
| 411 | |
| 412 | for (EffectiveContext::record_iterator |
| 413 | I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) { |
| 414 | CanQualType ContextTy |
| 415 | = S.Context.getCanonicalType(S.Context.getTypeDeclType(*I)); |
| 416 | if (MightInstantiateTo(S, ContextTy, FriendTy)) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 417 | return AR_dependent; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 421 | return AR_inaccessible; |
John McCall | 39e8288 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 422 | } |
| 423 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 424 | static AccessResult MatchesFriend(Sema &S, |
| 425 | const EffectiveContext &EC, |
| 426 | CanQualType Friend) { |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 427 | if (const RecordType *RT = Friend->getAs<RecordType>()) |
| 428 | return MatchesFriend(S, EC, cast<CXXRecordDecl>(RT->getDecl())); |
John McCall | 39e8288 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 429 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 430 | // TODO: we can do better than this |
| 431 | if (Friend->isDependentType()) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 432 | return AR_dependent; |
John McCall | 39e8288 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 433 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 434 | return AR_inaccessible; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /// Determines whether the given friend class template matches |
| 438 | /// anything in the effective context. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 439 | static AccessResult MatchesFriend(Sema &S, |
| 440 | const EffectiveContext &EC, |
| 441 | ClassTemplateDecl *Friend) { |
| 442 | AccessResult OnFailure = AR_inaccessible; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 443 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 444 | // Check whether the friend is the template of a class in the |
| 445 | // context chain. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 446 | for (SmallVectorImpl<CXXRecordDecl*>::const_iterator |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 447 | I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) { |
| 448 | CXXRecordDecl *Record = *I; |
| 449 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 450 | // Figure out whether the current class has a template: |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 451 | ClassTemplateDecl *CTD; |
| 452 | |
| 453 | // A specialization of the template... |
| 454 | if (isa<ClassTemplateSpecializationDecl>(Record)) { |
| 455 | CTD = cast<ClassTemplateSpecializationDecl>(Record) |
| 456 | ->getSpecializedTemplate(); |
| 457 | |
| 458 | // ... or the template pattern itself. |
| 459 | } else { |
| 460 | CTD = Record->getDescribedClassTemplate(); |
| 461 | if (!CTD) continue; |
| 462 | } |
| 463 | |
| 464 | // It's a match. |
| 465 | if (Friend == CTD->getCanonicalDecl()) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 466 | return AR_accessible; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 467 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 468 | // If the context isn't dependent, it can't be a dependent match. |
| 469 | if (!EC.isDependent()) |
| 470 | continue; |
| 471 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 472 | // If the template names don't match, it can't be a dependent |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 473 | // match. |
| 474 | if (CTD->getDeclName() != Friend->getDeclName()) |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 475 | continue; |
| 476 | |
| 477 | // If the class's context can't instantiate to the friend's |
| 478 | // context, it can't be a dependent match. |
| 479 | if (!MightInstantiateTo(S, CTD->getDeclContext(), |
| 480 | Friend->getDeclContext())) |
| 481 | continue; |
| 482 | |
| 483 | // Otherwise, it's a dependent match. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 484 | OnFailure = AR_dependent; |
John McCall | 39e8288 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 485 | } |
| 486 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 487 | return OnFailure; |
| 488 | } |
| 489 | |
| 490 | /// Determines whether the given friend function matches anything in |
| 491 | /// the effective context. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 492 | static AccessResult MatchesFriend(Sema &S, |
| 493 | const EffectiveContext &EC, |
| 494 | FunctionDecl *Friend) { |
| 495 | AccessResult OnFailure = AR_inaccessible; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 496 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 497 | for (SmallVectorImpl<FunctionDecl*>::const_iterator |
John McCall | 3dc81f7 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 498 | I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) { |
| 499 | if (Friend == *I) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 500 | return AR_accessible; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 501 | |
John McCall | 3dc81f7 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 502 | if (EC.isDependent() && MightInstantiateTo(S, *I, Friend)) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 503 | OnFailure = AR_dependent; |
John McCall | 3dc81f7 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 504 | } |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 505 | |
John McCall | 3dc81f7 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 506 | return OnFailure; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /// Determines whether the given friend function template matches |
| 510 | /// anything in the effective context. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 511 | static AccessResult MatchesFriend(Sema &S, |
| 512 | const EffectiveContext &EC, |
| 513 | FunctionTemplateDecl *Friend) { |
| 514 | if (EC.Functions.empty()) return AR_inaccessible; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 515 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 516 | AccessResult OnFailure = AR_inaccessible; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 517 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 518 | for (SmallVectorImpl<FunctionDecl*>::const_iterator |
John McCall | 3dc81f7 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 519 | I = EC.Functions.begin(), E = EC.Functions.end(); I != E; ++I) { |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 520 | |
John McCall | 3dc81f7 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 521 | FunctionTemplateDecl *FTD = (*I)->getPrimaryTemplate(); |
| 522 | if (!FTD) |
| 523 | FTD = (*I)->getDescribedFunctionTemplate(); |
| 524 | if (!FTD) |
| 525 | continue; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 526 | |
John McCall | 3dc81f7 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 527 | FTD = FTD->getCanonicalDecl(); |
| 528 | |
| 529 | if (Friend == FTD) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 530 | return AR_accessible; |
John McCall | 3dc81f7 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 531 | |
| 532 | if (EC.isDependent() && MightInstantiateTo(S, FTD, Friend)) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 533 | OnFailure = AR_dependent; |
John McCall | 3dc81f7 | 2010-03-27 06:55:49 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | return OnFailure; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /// Determines whether the given friend declaration matches anything |
| 540 | /// in the effective context. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 541 | static AccessResult MatchesFriend(Sema &S, |
| 542 | const EffectiveContext &EC, |
| 543 | FriendDecl *FriendD) { |
John McCall | 2c2eb12 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 544 | // Whitelist accesses if there's an invalid or unsupported friend |
| 545 | // declaration. |
| 546 | if (FriendD->isInvalidDecl() || FriendD->isUnsupportedFriend()) |
John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 547 | return AR_accessible; |
| 548 | |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 549 | if (TypeSourceInfo *T = FriendD->getFriendType()) |
| 550 | return MatchesFriend(S, EC, T->getType()->getCanonicalTypeUnqualified()); |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 551 | |
| 552 | NamedDecl *Friend |
| 553 | = cast<NamedDecl>(FriendD->getFriendDecl()->getCanonicalDecl()); |
John McCall | 39e8288 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 554 | |
| 555 | // FIXME: declarations with dependent or templated scope. |
| 556 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 557 | if (isa<ClassTemplateDecl>(Friend)) |
| 558 | return MatchesFriend(S, EC, cast<ClassTemplateDecl>(Friend)); |
John McCall | 39e8288 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 559 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 560 | if (isa<FunctionTemplateDecl>(Friend)) |
| 561 | return MatchesFriend(S, EC, cast<FunctionTemplateDecl>(Friend)); |
John McCall | 39e8288 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 562 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 563 | if (isa<CXXRecordDecl>(Friend)) |
| 564 | return MatchesFriend(S, EC, cast<CXXRecordDecl>(Friend)); |
John McCall | 39e8288 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 565 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 566 | assert(isa<FunctionDecl>(Friend) && "unknown friend decl kind"); |
| 567 | return MatchesFriend(S, EC, cast<FunctionDecl>(Friend)); |
John McCall | 39e8288 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 568 | } |
| 569 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 570 | static AccessResult GetFriendKind(Sema &S, |
| 571 | const EffectiveContext &EC, |
| 572 | const CXXRecordDecl *Class) { |
| 573 | AccessResult OnFailure = AR_inaccessible; |
John McCall | fb803d7 | 2010-03-17 04:58:56 +0000 | [diff] [blame] | 574 | |
John McCall | 16927f6 | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 575 | // Okay, check friends. |
| 576 | for (CXXRecordDecl::friend_iterator I = Class->friend_begin(), |
| 577 | E = Class->friend_end(); I != E; ++I) { |
| 578 | FriendDecl *Friend = *I; |
| 579 | |
John McCall | 39e8288 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 580 | switch (MatchesFriend(S, EC, Friend)) { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 581 | case AR_accessible: |
| 582 | return AR_accessible; |
John McCall | 16927f6 | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 583 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 584 | case AR_inaccessible: |
| 585 | continue; |
| 586 | |
| 587 | case AR_dependent: |
| 588 | OnFailure = AR_dependent; |
John McCall | 39e8288 | 2010-03-17 20:01:29 +0000 | [diff] [blame] | 589 | break; |
John McCall | 16927f6 | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 590 | } |
John McCall | 16927f6 | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | // That's it, give up. |
John McCall | fb803d7 | 2010-03-17 04:58:56 +0000 | [diff] [blame] | 594 | return OnFailure; |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 595 | } |
| 596 | |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 597 | namespace { |
| 598 | |
| 599 | /// A helper class for checking for a friend which will grant access |
| 600 | /// to a protected instance member. |
| 601 | struct ProtectedFriendContext { |
| 602 | Sema &S; |
| 603 | const EffectiveContext &EC; |
| 604 | const CXXRecordDecl *NamingClass; |
| 605 | bool CheckDependent; |
| 606 | bool EverDependent; |
| 607 | |
| 608 | /// The path down to the current base class. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 609 | SmallVector<const CXXRecordDecl*, 20> CurPath; |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 610 | |
| 611 | ProtectedFriendContext(Sema &S, const EffectiveContext &EC, |
| 612 | const CXXRecordDecl *InstanceContext, |
| 613 | const CXXRecordDecl *NamingClass) |
| 614 | : S(S), EC(EC), NamingClass(NamingClass), |
| 615 | CheckDependent(InstanceContext->isDependentContext() || |
| 616 | NamingClass->isDependentContext()), |
| 617 | EverDependent(false) {} |
| 618 | |
John McCall | 1177ff1 | 2010-08-28 08:47:21 +0000 | [diff] [blame] | 619 | /// Check classes in the current path for friendship, starting at |
| 620 | /// the given index. |
| 621 | bool checkFriendshipAlongPath(unsigned I) { |
| 622 | assert(I < CurPath.size()); |
| 623 | for (unsigned E = CurPath.size(); I != E; ++I) { |
| 624 | switch (GetFriendKind(S, EC, CurPath[I])) { |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 625 | case AR_accessible: return true; |
| 626 | case AR_inaccessible: continue; |
| 627 | case AR_dependent: EverDependent = true; continue; |
| 628 | } |
| 629 | } |
| 630 | return false; |
| 631 | } |
| 632 | |
| 633 | /// Perform a search starting at the given class. |
John McCall | 1177ff1 | 2010-08-28 08:47:21 +0000 | [diff] [blame] | 634 | /// |
| 635 | /// PrivateDepth is the index of the last (least derived) class |
| 636 | /// along the current path such that a notional public member of |
| 637 | /// the final class in the path would have access in that class. |
| 638 | bool findFriendship(const CXXRecordDecl *Cur, unsigned PrivateDepth) { |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 639 | // If we ever reach the naming class, check the current path for |
| 640 | // friendship. We can also stop recursing because we obviously |
| 641 | // won't find the naming class there again. |
John McCall | 1177ff1 | 2010-08-28 08:47:21 +0000 | [diff] [blame] | 642 | if (Cur == NamingClass) |
| 643 | return checkFriendshipAlongPath(PrivateDepth); |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 644 | |
| 645 | if (CheckDependent && MightInstantiateTo(Cur, NamingClass)) |
| 646 | EverDependent = true; |
| 647 | |
| 648 | // Recurse into the base classes. |
| 649 | for (CXXRecordDecl::base_class_const_iterator |
| 650 | I = Cur->bases_begin(), E = Cur->bases_end(); I != E; ++I) { |
| 651 | |
John McCall | 1177ff1 | 2010-08-28 08:47:21 +0000 | [diff] [blame] | 652 | // If this is private inheritance, then a public member of the |
| 653 | // base will not have any access in classes derived from Cur. |
| 654 | unsigned BasePrivateDepth = PrivateDepth; |
| 655 | if (I->getAccessSpecifier() == AS_private) |
| 656 | BasePrivateDepth = CurPath.size() - 1; |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 657 | |
| 658 | const CXXRecordDecl *RD; |
| 659 | |
| 660 | QualType T = I->getType(); |
| 661 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 662 | RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 663 | } else if (const InjectedClassNameType *IT |
| 664 | = T->getAs<InjectedClassNameType>()) { |
| 665 | RD = IT->getDecl(); |
| 666 | } else { |
| 667 | assert(T->isDependentType() && "non-dependent base wasn't a record?"); |
| 668 | EverDependent = true; |
| 669 | continue; |
| 670 | } |
| 671 | |
| 672 | // Recurse. We don't need to clean up if this returns true. |
John McCall | 1177ff1 | 2010-08-28 08:47:21 +0000 | [diff] [blame] | 673 | CurPath.push_back(RD); |
| 674 | if (findFriendship(RD->getCanonicalDecl(), BasePrivateDepth)) |
| 675 | return true; |
| 676 | CurPath.pop_back(); |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 677 | } |
| 678 | |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 679 | return false; |
| 680 | } |
John McCall | 1177ff1 | 2010-08-28 08:47:21 +0000 | [diff] [blame] | 681 | |
| 682 | bool findFriendship(const CXXRecordDecl *Cur) { |
| 683 | assert(CurPath.empty()); |
| 684 | CurPath.push_back(Cur); |
| 685 | return findFriendship(Cur, 0); |
| 686 | } |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 687 | }; |
| 688 | } |
| 689 | |
| 690 | /// Search for a class P that EC is a friend of, under the constraint |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 691 | /// InstanceContext <= P |
| 692 | /// if InstanceContext exists, or else |
| 693 | /// NamingClass <= P |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 694 | /// and with the additional restriction that a protected member of |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 695 | /// NamingClass would have some natural access in P, which implicitly |
| 696 | /// imposes the constraint that P <= NamingClass. |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 697 | /// |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 698 | /// This isn't quite the condition laid out in the standard. |
| 699 | /// Instead of saying that a notional protected member of NamingClass |
| 700 | /// would have to have some natural access in P, it says the actual |
| 701 | /// target has to have some natural access in P, which opens up the |
| 702 | /// possibility that the target (which is not necessarily a member |
| 703 | /// of NamingClass) might be more accessible along some path not |
| 704 | /// passing through it. That's really a bad idea, though, because it |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 705 | /// introduces two problems: |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 706 | /// - Most importantly, it breaks encapsulation because you can |
| 707 | /// access a forbidden base class's members by directly subclassing |
| 708 | /// it elsewhere. |
| 709 | /// - It also makes access substantially harder to compute because it |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 710 | /// breaks the hill-climbing algorithm: knowing that the target is |
| 711 | /// accessible in some base class would no longer let you change |
| 712 | /// the question solely to whether the base class is accessible, |
| 713 | /// because the original target might have been more accessible |
| 714 | /// because of crazy subclassing. |
| 715 | /// So we don't implement that. |
| 716 | static AccessResult GetProtectedFriendKind(Sema &S, const EffectiveContext &EC, |
| 717 | const CXXRecordDecl *InstanceContext, |
| 718 | const CXXRecordDecl *NamingClass) { |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 719 | assert(InstanceContext == 0 || |
| 720 | InstanceContext->getCanonicalDecl() == InstanceContext); |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 721 | assert(NamingClass->getCanonicalDecl() == NamingClass); |
| 722 | |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 723 | // If we don't have an instance context, our constraints give us |
| 724 | // that NamingClass <= P <= NamingClass, i.e. P == NamingClass. |
| 725 | // This is just the usual friendship check. |
| 726 | if (!InstanceContext) return GetFriendKind(S, EC, NamingClass); |
| 727 | |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 728 | ProtectedFriendContext PRC(S, EC, InstanceContext, NamingClass); |
| 729 | if (PRC.findFriendship(InstanceContext)) return AR_accessible; |
| 730 | if (PRC.EverDependent) return AR_dependent; |
| 731 | return AR_inaccessible; |
| 732 | } |
| 733 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 734 | static AccessResult HasAccess(Sema &S, |
| 735 | const EffectiveContext &EC, |
| 736 | const CXXRecordDecl *NamingClass, |
| 737 | AccessSpecifier Access, |
| 738 | const AccessTarget &Target) { |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 739 | assert(NamingClass->getCanonicalDecl() == NamingClass && |
| 740 | "declaration should be canonicalized before being passed here"); |
| 741 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 742 | if (Access == AS_public) return AR_accessible; |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 743 | assert(Access == AS_private || Access == AS_protected); |
| 744 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 745 | AccessResult OnFailure = AR_inaccessible; |
| 746 | |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 747 | for (EffectiveContext::record_iterator |
| 748 | I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) { |
| 749 | // All the declarations in EC have been canonicalized, so pointer |
| 750 | // equality from this point on will work fine. |
| 751 | const CXXRecordDecl *ECRecord = *I; |
| 752 | |
| 753 | // [B2] and [M2] |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 754 | if (Access == AS_private) { |
| 755 | if (ECRecord == NamingClass) |
| 756 | return AR_accessible; |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 757 | |
John McCall | 9720514 | 2010-05-04 05:11:27 +0000 | [diff] [blame] | 758 | if (EC.isDependent() && MightInstantiateTo(ECRecord, NamingClass)) |
| 759 | OnFailure = AR_dependent; |
| 760 | |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 761 | // [B3] and [M3] |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 762 | } else { |
| 763 | assert(Access == AS_protected); |
| 764 | switch (IsDerivedFromInclusive(ECRecord, NamingClass)) { |
| 765 | case AR_accessible: break; |
| 766 | case AR_inaccessible: continue; |
| 767 | case AR_dependent: OnFailure = AR_dependent; continue; |
| 768 | } |
| 769 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 770 | // C++ [class.protected]p1: |
| 771 | // An additional access check beyond those described earlier in |
| 772 | // [class.access] is applied when a non-static data member or |
| 773 | // non-static member function is a protected member of its naming |
| 774 | // class. As described earlier, access to a protected member is |
| 775 | // granted because the reference occurs in a friend or member of |
| 776 | // some class C. If the access is to form a pointer to member, |
| 777 | // the nested-name-specifier shall name C or a class derived from |
| 778 | // C. All other accesses involve a (possibly implicit) object |
| 779 | // expression. In this case, the class of the object expression |
| 780 | // shall be C or a class derived from C. |
| 781 | // |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 782 | // We interpret this as a restriction on [M3]. |
| 783 | |
| 784 | // In this part of the code, 'C' is just our context class ECRecord. |
| 785 | |
| 786 | // These rules are different if we don't have an instance context. |
| 787 | if (!Target.hasInstanceContext()) { |
| 788 | // If it's not an instance member, these restrictions don't apply. |
| 789 | if (!Target.isInstanceMember()) return AR_accessible; |
| 790 | |
| 791 | // If it's an instance member, use the pointer-to-member rule |
| 792 | // that the naming class has to be derived from the effective |
| 793 | // context. |
| 794 | |
Francois Pichet | a39371c | 2012-04-17 12:35:05 +0000 | [diff] [blame] | 795 | // Emulate a MSVC bug where the creation of pointer-to-member |
| 796 | // to protected member of base class is allowed but only from |
Francois Pichet | 52d3898 | 2012-04-19 07:48:57 +0000 | [diff] [blame] | 797 | // static member functions. |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 798 | if (S.getLangOpts().MSVCCompat && !EC.Functions.empty()) |
Francois Pichet | 5b061f0 | 2012-04-18 03:24:38 +0000 | [diff] [blame] | 799 | if (CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(EC.Functions.front())) |
| 800 | if (MD->isStatic()) return AR_accessible; |
Francois Pichet | a39371c | 2012-04-17 12:35:05 +0000 | [diff] [blame] | 801 | |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 802 | // Despite the standard's confident wording, there is a case |
| 803 | // where you can have an instance member that's neither in a |
| 804 | // pointer-to-member expression nor in a member access: when |
| 805 | // it names a field in an unevaluated context that can't be an |
| 806 | // implicit member. Pending clarification, we just apply the |
| 807 | // same naming-class restriction here. |
| 808 | // FIXME: we're probably not correctly adding the |
| 809 | // protected-member restriction when we retroactively convert |
| 810 | // an expression to being evaluated. |
| 811 | |
| 812 | // We know that ECRecord derives from NamingClass. The |
| 813 | // restriction says to check whether NamingClass derives from |
| 814 | // ECRecord, but that's not really necessary: two distinct |
| 815 | // classes can't be recursively derived from each other. So |
| 816 | // along this path, we just need to check whether the classes |
| 817 | // are equal. |
| 818 | if (NamingClass == ECRecord) return AR_accessible; |
| 819 | |
| 820 | // Otherwise, this context class tells us nothing; on to the next. |
| 821 | continue; |
| 822 | } |
| 823 | |
| 824 | assert(Target.isInstanceMember()); |
| 825 | |
| 826 | const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S); |
| 827 | if (!InstanceContext) { |
| 828 | OnFailure = AR_dependent; |
| 829 | continue; |
| 830 | } |
| 831 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 832 | switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) { |
| 833 | case AR_accessible: return AR_accessible; |
| 834 | case AR_inaccessible: continue; |
| 835 | case AR_dependent: OnFailure = AR_dependent; continue; |
| 836 | } |
| 837 | } |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 838 | } |
| 839 | |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 840 | // [M3] and [B3] say that, if the target is protected in N, we grant |
| 841 | // access if the access occurs in a friend or member of some class P |
| 842 | // that's a subclass of N and where the target has some natural |
| 843 | // access in P. The 'member' aspect is easy to handle because P |
| 844 | // would necessarily be one of the effective-context records, and we |
| 845 | // address that above. The 'friend' aspect is completely ridiculous |
| 846 | // to implement because there are no restrictions at all on P |
| 847 | // *unless* the [class.protected] restriction applies. If it does, |
| 848 | // however, we should ignore whether the naming class is a friend, |
| 849 | // and instead rely on whether any potential P is a friend. |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 850 | if (Access == AS_protected && Target.isInstanceMember()) { |
| 851 | // Compute the instance context if possible. |
| 852 | const CXXRecordDecl *InstanceContext = 0; |
| 853 | if (Target.hasInstanceContext()) { |
| 854 | InstanceContext = Target.resolveInstanceContext(S); |
| 855 | if (!InstanceContext) return AR_dependent; |
| 856 | } |
| 857 | |
John McCall | 9632967 | 2010-08-28 07:56:00 +0000 | [diff] [blame] | 858 | switch (GetProtectedFriendKind(S, EC, InstanceContext, NamingClass)) { |
| 859 | case AR_accessible: return AR_accessible; |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 860 | case AR_inaccessible: return OnFailure; |
| 861 | case AR_dependent: return AR_dependent; |
| 862 | } |
John McCall | c6af8f4 | 2010-08-28 08:10:32 +0000 | [diff] [blame] | 863 | llvm_unreachable("impossible friendship kind"); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | switch (GetFriendKind(S, EC, NamingClass)) { |
| 867 | case AR_accessible: return AR_accessible; |
| 868 | case AR_inaccessible: return OnFailure; |
| 869 | case AR_dependent: return AR_dependent; |
| 870 | } |
| 871 | |
| 872 | // Silence bogus warnings |
| 873 | llvm_unreachable("impossible friendship kind"); |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 874 | } |
| 875 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 876 | /// Finds the best path from the naming class to the declaring class, |
| 877 | /// taking friend declarations into account. |
| 878 | /// |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 879 | /// C++0x [class.access.base]p5: |
| 880 | /// A member m is accessible at the point R when named in class N if |
| 881 | /// [M1] m as a member of N is public, or |
| 882 | /// [M2] m as a member of N is private, and R occurs in a member or |
| 883 | /// friend of class N, or |
| 884 | /// [M3] m as a member of N is protected, and R occurs in a member or |
| 885 | /// friend of class N, or in a member or friend of a class P |
| 886 | /// derived from N, where m as a member of P is public, private, |
| 887 | /// or protected, or |
| 888 | /// [M4] there exists a base class B of N that is accessible at R, and |
| 889 | /// m is accessible at R when named in class B. |
| 890 | /// |
| 891 | /// C++0x [class.access.base]p4: |
| 892 | /// A base class B of N is accessible at R, if |
| 893 | /// [B1] an invented public member of B would be a public member of N, or |
| 894 | /// [B2] R occurs in a member or friend of class N, and an invented public |
| 895 | /// member of B would be a private or protected member of N, or |
| 896 | /// [B3] R occurs in a member or friend of a class P derived from N, and an |
| 897 | /// invented public member of B would be a private or protected member |
| 898 | /// of P, or |
| 899 | /// [B4] there exists a class S such that B is a base class of S accessible |
| 900 | /// at R and S is a base class of N accessible at R. |
| 901 | /// |
| 902 | /// Along a single inheritance path we can restate both of these |
| 903 | /// iteratively: |
| 904 | /// |
| 905 | /// First, we note that M1-4 are equivalent to B1-4 if the member is |
| 906 | /// treated as a notional base of its declaring class with inheritance |
| 907 | /// access equivalent to the member's access. Therefore we need only |
| 908 | /// ask whether a class B is accessible from a class N in context R. |
| 909 | /// |
| 910 | /// Let B_1 .. B_n be the inheritance path in question (i.e. where |
| 911 | /// B_1 = N, B_n = B, and for all i, B_{i+1} is a direct base class of |
| 912 | /// B_i). For i in 1..n, we will calculate ACAB(i), the access to the |
| 913 | /// closest accessible base in the path: |
| 914 | /// Access(a, b) = (* access on the base specifier from a to b *) |
| 915 | /// Merge(a, forbidden) = forbidden |
| 916 | /// Merge(a, private) = forbidden |
| 917 | /// Merge(a, b) = min(a,b) |
| 918 | /// Accessible(c, forbidden) = false |
| 919 | /// Accessible(c, private) = (R is c) || IsFriend(c, R) |
| 920 | /// Accessible(c, protected) = (R derived from c) || IsFriend(c, R) |
| 921 | /// Accessible(c, public) = true |
| 922 | /// ACAB(n) = public |
| 923 | /// ACAB(i) = |
| 924 | /// let AccessToBase = Merge(Access(B_i, B_{i+1}), ACAB(i+1)) in |
| 925 | /// if Accessible(B_i, AccessToBase) then public else AccessToBase |
| 926 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 927 | /// B is an accessible base of N at R iff ACAB(1) = public. |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 928 | /// |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 929 | /// \param FinalAccess the access of the "final step", or AS_public if |
John McCall | a332b95 | 2010-03-18 23:49:19 +0000 | [diff] [blame] | 930 | /// there is no final step. |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 931 | /// \return null if friendship is dependent |
| 932 | static CXXBasePath *FindBestPath(Sema &S, |
| 933 | const EffectiveContext &EC, |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 934 | AccessTarget &Target, |
John McCall | a332b95 | 2010-03-18 23:49:19 +0000 | [diff] [blame] | 935 | AccessSpecifier FinalAccess, |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 936 | CXXBasePaths &Paths) { |
| 937 | // Derive the paths to the desired base. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 938 | const CXXRecordDecl *Derived = Target.getNamingClass(); |
| 939 | const CXXRecordDecl *Base = Target.getDeclaringClass(); |
| 940 | |
| 941 | // FIXME: fail correctly when there are dependent paths. |
| 942 | bool isDerived = Derived->isDerivedFrom(const_cast<CXXRecordDecl*>(Base), |
| 943 | Paths); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 944 | assert(isDerived && "derived class not actually derived from base"); |
| 945 | (void) isDerived; |
| 946 | |
| 947 | CXXBasePath *BestPath = 0; |
| 948 | |
John McCall | a332b95 | 2010-03-18 23:49:19 +0000 | [diff] [blame] | 949 | assert(FinalAccess != AS_none && "forbidden access after declaring class"); |
| 950 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 951 | bool AnyDependent = false; |
| 952 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 953 | // Derive the friend-modified access along each path. |
| 954 | for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end(); |
| 955 | PI != PE; ++PI) { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 956 | AccessTarget::SavedInstanceContext _ = Target.saveInstanceContext(); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 957 | |
| 958 | // Walk through the path backwards. |
John McCall | a332b95 | 2010-03-18 23:49:19 +0000 | [diff] [blame] | 959 | AccessSpecifier PathAccess = FinalAccess; |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 960 | CXXBasePath::iterator I = PI->end(), E = PI->begin(); |
| 961 | while (I != E) { |
| 962 | --I; |
| 963 | |
John McCall | a332b95 | 2010-03-18 23:49:19 +0000 | [diff] [blame] | 964 | assert(PathAccess != AS_none); |
| 965 | |
| 966 | // If the declaration is a private member of a base class, there |
| 967 | // is no level of friendship in derived classes that can make it |
| 968 | // accessible. |
| 969 | if (PathAccess == AS_private) { |
| 970 | PathAccess = AS_none; |
| 971 | break; |
| 972 | } |
| 973 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 974 | const CXXRecordDecl *NC = I->Class->getCanonicalDecl(); |
| 975 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 976 | AccessSpecifier BaseAccess = I->Base->getAccessSpecifier(); |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 977 | PathAccess = std::max(PathAccess, BaseAccess); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 978 | |
| 979 | switch (HasAccess(S, EC, NC, PathAccess, Target)) { |
| 980 | case AR_inaccessible: break; |
| 981 | case AR_accessible: |
| 982 | PathAccess = AS_public; |
| 983 | |
| 984 | // Future tests are not against members and so do not have |
| 985 | // instance context. |
| 986 | Target.suppressInstanceContext(); |
| 987 | break; |
| 988 | case AR_dependent: |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 989 | AnyDependent = true; |
| 990 | goto Next; |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 991 | } |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | // Note that we modify the path's Access field to the |
| 995 | // friend-modified access. |
| 996 | if (BestPath == 0 || PathAccess < BestPath->Access) { |
| 997 | BestPath = &*PI; |
| 998 | BestPath->Access = PathAccess; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 999 | |
| 1000 | // Short-circuit if we found a public path. |
| 1001 | if (BestPath->Access == AS_public) |
| 1002 | return BestPath; |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1003 | } |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1004 | |
| 1005 | Next: ; |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1008 | assert((!BestPath || BestPath->Access != AS_public) && |
| 1009 | "fell out of loop with public path"); |
| 1010 | |
| 1011 | // We didn't find a public path, but at least one path was subject |
| 1012 | // to dependent friendship, so delay the check. |
| 1013 | if (AnyDependent) |
| 1014 | return 0; |
| 1015 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1016 | return BestPath; |
| 1017 | } |
| 1018 | |
John McCall | 417e744 | 2010-09-03 04:56:05 +0000 | [diff] [blame] | 1019 | /// Given that an entity has protected natural access, check whether |
| 1020 | /// access might be denied because of the protected member access |
| 1021 | /// restriction. |
| 1022 | /// |
| 1023 | /// \return true if a note was emitted |
| 1024 | static bool TryDiagnoseProtectedAccess(Sema &S, const EffectiveContext &EC, |
| 1025 | AccessTarget &Target) { |
| 1026 | // Only applies to instance accesses. |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1027 | if (!Target.isInstanceMember()) |
John McCall | 417e744 | 2010-09-03 04:56:05 +0000 | [diff] [blame] | 1028 | return false; |
John McCall | 417e744 | 2010-09-03 04:56:05 +0000 | [diff] [blame] | 1029 | |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1030 | assert(Target.isMemberAccess()); |
| 1031 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1032 | const CXXRecordDecl *NamingClass = Target.getEffectiveNamingClass(); |
John McCall | 417e744 | 2010-09-03 04:56:05 +0000 | [diff] [blame] | 1033 | |
| 1034 | for (EffectiveContext::record_iterator |
| 1035 | I = EC.Records.begin(), E = EC.Records.end(); I != E; ++I) { |
| 1036 | const CXXRecordDecl *ECRecord = *I; |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1037 | switch (IsDerivedFromInclusive(ECRecord, NamingClass)) { |
John McCall | 417e744 | 2010-09-03 04:56:05 +0000 | [diff] [blame] | 1038 | case AR_accessible: break; |
| 1039 | case AR_inaccessible: continue; |
| 1040 | case AR_dependent: continue; |
| 1041 | } |
| 1042 | |
| 1043 | // The effective context is a subclass of the declaring class. |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1044 | // Check whether the [class.protected] restriction is limiting |
| 1045 | // access. |
John McCall | 417e744 | 2010-09-03 04:56:05 +0000 | [diff] [blame] | 1046 | |
| 1047 | // To get this exactly right, this might need to be checked more |
| 1048 | // holistically; it's not necessarily the case that gaining |
| 1049 | // access here would grant us access overall. |
| 1050 | |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1051 | NamedDecl *D = Target.getTargetDecl(); |
| 1052 | |
| 1053 | // If we don't have an instance context, [class.protected] says the |
| 1054 | // naming class has to equal the context class. |
| 1055 | if (!Target.hasInstanceContext()) { |
| 1056 | // If it does, the restriction doesn't apply. |
| 1057 | if (NamingClass == ECRecord) continue; |
| 1058 | |
| 1059 | // TODO: it would be great to have a fixit here, since this is |
| 1060 | // such an obvious error. |
| 1061 | S.Diag(D->getLocation(), diag::note_access_protected_restricted_noobject) |
| 1062 | << S.Context.getTypeDeclType(ECRecord); |
| 1063 | return true; |
| 1064 | } |
| 1065 | |
John McCall | 417e744 | 2010-09-03 04:56:05 +0000 | [diff] [blame] | 1066 | const CXXRecordDecl *InstanceContext = Target.resolveInstanceContext(S); |
| 1067 | assert(InstanceContext && "diagnosing dependent access"); |
| 1068 | |
| 1069 | switch (IsDerivedFromInclusive(InstanceContext, ECRecord)) { |
| 1070 | case AR_accessible: continue; |
| 1071 | case AR_dependent: continue; |
| 1072 | case AR_inaccessible: |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1073 | break; |
| 1074 | } |
| 1075 | |
| 1076 | // Okay, the restriction seems to be what's limiting us. |
| 1077 | |
| 1078 | // Use a special diagnostic for constructors and destructors. |
| 1079 | if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D) || |
| 1080 | (isa<FunctionTemplateDecl>(D) && |
| 1081 | isa<CXXConstructorDecl>( |
| 1082 | cast<FunctionTemplateDecl>(D)->getTemplatedDecl()))) { |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 1083 | return S.Diag(D->getLocation(), |
| 1084 | diag::note_access_protected_restricted_ctordtor) |
| 1085 | << isa<CXXDestructorDecl>(D->getAsFunction()); |
John McCall | 417e744 | 2010-09-03 04:56:05 +0000 | [diff] [blame] | 1086 | } |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1087 | |
| 1088 | // Otherwise, use the generic diagnostic. |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 1089 | return S.Diag(D->getLocation(), |
| 1090 | diag::note_access_protected_restricted_object) |
| 1091 | << S.Context.getTypeDeclType(ECRecord); |
John McCall | 417e744 | 2010-09-03 04:56:05 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | return false; |
| 1095 | } |
| 1096 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1097 | /// We are unable to access a given declaration due to its direct |
| 1098 | /// access control; diagnose that. |
| 1099 | static void diagnoseBadDirectAccess(Sema &S, |
| 1100 | const EffectiveContext &EC, |
| 1101 | AccessTarget &entity) { |
| 1102 | assert(entity.isMemberAccess()); |
| 1103 | NamedDecl *D = entity.getTargetDecl(); |
| 1104 | |
| 1105 | if (D->getAccess() == AS_protected && |
| 1106 | TryDiagnoseProtectedAccess(S, EC, entity)) |
| 1107 | return; |
| 1108 | |
| 1109 | // Find an original declaration. |
| 1110 | while (D->isOutOfLine()) { |
| 1111 | NamedDecl *PrevDecl = 0; |
| 1112 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 1113 | PrevDecl = VD->getPreviousDecl(); |
| 1114 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 1115 | PrevDecl = FD->getPreviousDecl(); |
| 1116 | else if (TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D)) |
| 1117 | PrevDecl = TND->getPreviousDecl(); |
| 1118 | else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 1119 | if (isa<RecordDecl>(D) && cast<RecordDecl>(D)->isInjectedClassName()) |
| 1120 | break; |
| 1121 | PrevDecl = TD->getPreviousDecl(); |
| 1122 | } |
| 1123 | if (!PrevDecl) break; |
| 1124 | D = PrevDecl; |
| 1125 | } |
| 1126 | |
| 1127 | CXXRecordDecl *DeclaringClass = FindDeclaringClass(D); |
| 1128 | Decl *ImmediateChild; |
| 1129 | if (D->getDeclContext() == DeclaringClass) |
| 1130 | ImmediateChild = D; |
| 1131 | else { |
| 1132 | DeclContext *DC = D->getDeclContext(); |
| 1133 | while (DC->getParent() != DeclaringClass) |
| 1134 | DC = DC->getParent(); |
| 1135 | ImmediateChild = cast<Decl>(DC); |
| 1136 | } |
| 1137 | |
| 1138 | // Check whether there's an AccessSpecDecl preceding this in the |
| 1139 | // chain of the DeclContext. |
| 1140 | bool isImplicit = true; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame^] | 1141 | for (const auto *I : DeclaringClass->decls()) { |
| 1142 | if (I == ImmediateChild) break; |
| 1143 | if (isa<AccessSpecDecl>(I)) { |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1144 | isImplicit = false; |
| 1145 | break; |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | S.Diag(D->getLocation(), diag::note_access_natural) |
| 1150 | << (unsigned) (D->getAccess() == AS_protected) |
| 1151 | << isImplicit; |
| 1152 | } |
| 1153 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1154 | /// Diagnose the path which caused the given declaration or base class |
| 1155 | /// to become inaccessible. |
| 1156 | static void DiagnoseAccessPath(Sema &S, |
| 1157 | const EffectiveContext &EC, |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1158 | AccessTarget &entity) { |
| 1159 | // Save the instance context to preserve invariants. |
| 1160 | AccessTarget::SavedInstanceContext _ = entity.saveInstanceContext(); |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1161 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1162 | // This basically repeats the main algorithm but keeps some more |
| 1163 | // information. |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1164 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1165 | // The natural access so far. |
| 1166 | AccessSpecifier accessSoFar = AS_public; |
John McCall | 417e744 | 2010-09-03 04:56:05 +0000 | [diff] [blame] | 1167 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1168 | // Check whether we have special rights to the declaring class. |
| 1169 | if (entity.isMemberAccess()) { |
| 1170 | NamedDecl *D = entity.getTargetDecl(); |
| 1171 | accessSoFar = D->getAccess(); |
| 1172 | const CXXRecordDecl *declaringClass = entity.getDeclaringClass(); |
John McCall | f551aca | 2010-10-20 08:15:06 +0000 | [diff] [blame] | 1173 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1174 | switch (HasAccess(S, EC, declaringClass, accessSoFar, entity)) { |
| 1175 | // If the declaration is accessible when named in its declaring |
| 1176 | // class, then we must be constrained by the path. |
| 1177 | case AR_accessible: |
| 1178 | accessSoFar = AS_public; |
| 1179 | entity.suppressInstanceContext(); |
| 1180 | break; |
John McCall | f551aca | 2010-10-20 08:15:06 +0000 | [diff] [blame] | 1181 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1182 | case AR_inaccessible: |
| 1183 | if (accessSoFar == AS_private || |
| 1184 | declaringClass == entity.getEffectiveNamingClass()) |
| 1185 | return diagnoseBadDirectAccess(S, EC, entity); |
| 1186 | break; |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1187 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1188 | case AR_dependent: |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1189 | llvm_unreachable("cannot diagnose dependent access"); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1193 | CXXBasePaths paths; |
| 1194 | CXXBasePath &path = *FindBestPath(S, EC, entity, accessSoFar, paths); |
| 1195 | assert(path.Access != AS_public); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1196 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1197 | CXXBasePath::iterator i = path.end(), e = path.begin(); |
| 1198 | CXXBasePath::iterator constrainingBase = i; |
| 1199 | while (i != e) { |
| 1200 | --i; |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1201 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1202 | assert(accessSoFar != AS_none && accessSoFar != AS_private); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1203 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1204 | // Is the entity accessible when named in the deriving class, as |
| 1205 | // modified by the base specifier? |
| 1206 | const CXXRecordDecl *derivingClass = i->Class->getCanonicalDecl(); |
| 1207 | const CXXBaseSpecifier *base = i->Base; |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1208 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1209 | // If the access to this base is worse than the access we have to |
| 1210 | // the declaration, remember it. |
| 1211 | AccessSpecifier baseAccess = base->getAccessSpecifier(); |
| 1212 | if (baseAccess > accessSoFar) { |
| 1213 | constrainingBase = i; |
| 1214 | accessSoFar = baseAccess; |
| 1215 | } |
| 1216 | |
| 1217 | switch (HasAccess(S, EC, derivingClass, accessSoFar, entity)) { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1218 | case AR_inaccessible: break; |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1219 | case AR_accessible: |
| 1220 | accessSoFar = AS_public; |
| 1221 | entity.suppressInstanceContext(); |
| 1222 | constrainingBase = 0; |
| 1223 | break; |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1224 | case AR_dependent: |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1225 | llvm_unreachable("cannot diagnose dependent access"); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1228 | // If this was private inheritance, but we don't have access to |
| 1229 | // the deriving class, we're done. |
| 1230 | if (accessSoFar == AS_private) { |
| 1231 | assert(baseAccess == AS_private); |
| 1232 | assert(constrainingBase == i); |
| 1233 | break; |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1234 | } |
| 1235 | } |
| 1236 | |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1237 | // If we don't have a constraining base, the access failure must be |
| 1238 | // due to the original declaration. |
| 1239 | if (constrainingBase == path.end()) |
| 1240 | return diagnoseBadDirectAccess(S, EC, entity); |
| 1241 | |
| 1242 | // We're constrained by inheritance, but we want to say |
| 1243 | // "declared private here" if we're diagnosing a hierarchy |
| 1244 | // conversion and this is the final step. |
| 1245 | unsigned diagnostic; |
| 1246 | if (entity.isMemberAccess() || |
| 1247 | constrainingBase + 1 != path.end()) { |
| 1248 | diagnostic = diag::note_access_constrained_by_path; |
| 1249 | } else { |
| 1250 | diagnostic = diag::note_access_natural; |
| 1251 | } |
| 1252 | |
| 1253 | const CXXBaseSpecifier *base = constrainingBase->Base; |
| 1254 | |
| 1255 | S.Diag(base->getSourceRange().getBegin(), diagnostic) |
| 1256 | << base->getSourceRange() |
| 1257 | << (base->getAccessSpecifier() == AS_protected) |
| 1258 | << (base->getAccessSpecifierAsWritten() == AS_none); |
| 1259 | |
| 1260 | if (entity.isMemberAccess()) |
| 1261 | S.Diag(entity.getTargetDecl()->getLocation(), diag::note_field_decl); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1264 | static void DiagnoseBadAccess(Sema &S, SourceLocation Loc, |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1265 | const EffectiveContext &EC, |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1266 | AccessTarget &Entity) { |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1267 | const CXXRecordDecl *NamingClass = Entity.getNamingClass(); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1268 | const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass(); |
| 1269 | NamedDecl *D = (Entity.isMemberAccess() ? Entity.getTargetDecl() : 0); |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1270 | |
| 1271 | S.Diag(Loc, Entity.getDiag()) |
| 1272 | << (Entity.getAccess() == AS_protected) |
| 1273 | << (D ? D->getDeclName() : DeclarationName()) |
| 1274 | << S.Context.getTypeDeclType(NamingClass) |
| 1275 | << S.Context.getTypeDeclType(DeclaringClass); |
| 1276 | DiagnoseAccessPath(S, EC, Entity); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Francois Pichet | efb1af9 | 2011-05-23 03:43:44 +0000 | [diff] [blame] | 1279 | /// MSVC has a bug where if during an using declaration name lookup, |
| 1280 | /// the declaration found is unaccessible (private) and that declaration |
| 1281 | /// was bring into scope via another using declaration whose target |
| 1282 | /// declaration is accessible (public) then no error is generated. |
| 1283 | /// Example: |
| 1284 | /// class A { |
| 1285 | /// public: |
| 1286 | /// int f(); |
| 1287 | /// }; |
| 1288 | /// class B : public A { |
| 1289 | /// private: |
| 1290 | /// using A::f; |
| 1291 | /// }; |
| 1292 | /// class C : public B { |
| 1293 | /// private: |
| 1294 | /// using B::f; |
| 1295 | /// }; |
| 1296 | /// |
| 1297 | /// Here, B::f is private so this should fail in Standard C++, but |
| 1298 | /// because B::f refers to A::f which is public MSVC accepts it. |
| 1299 | static bool IsMicrosoftUsingDeclarationAccessBug(Sema& S, |
| 1300 | SourceLocation AccessLoc, |
| 1301 | AccessTarget &Entity) { |
| 1302 | if (UsingShadowDecl *Shadow = |
| 1303 | dyn_cast<UsingShadowDecl>(Entity.getTargetDecl())) { |
| 1304 | const NamedDecl *OrigDecl = Entity.getTargetDecl()->getUnderlyingDecl(); |
| 1305 | if (Entity.getTargetDecl()->getAccess() == AS_private && |
| 1306 | (OrigDecl->getAccess() == AS_public || |
| 1307 | OrigDecl->getAccess() == AS_protected)) { |
Richard Smith | e434590 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 1308 | S.Diag(AccessLoc, diag::ext_ms_using_declaration_inaccessible) |
Francois Pichet | efb1af9 | 2011-05-23 03:43:44 +0000 | [diff] [blame] | 1309 | << Shadow->getUsingDecl()->getQualifiedNameAsString() |
| 1310 | << OrigDecl->getQualifiedNameAsString(); |
| 1311 | return true; |
| 1312 | } |
| 1313 | } |
| 1314 | return false; |
| 1315 | } |
| 1316 | |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1317 | /// Determines whether the accessed entity is accessible. Public members |
| 1318 | /// have been weeded out by this point. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1319 | static AccessResult IsAccessible(Sema &S, |
| 1320 | const EffectiveContext &EC, |
| 1321 | AccessTarget &Entity) { |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1322 | // Determine the actual naming class. |
John McCall | d010ac9 | 2013-02-27 00:08:19 +0000 | [diff] [blame] | 1323 | const CXXRecordDecl *NamingClass = Entity.getEffectiveNamingClass(); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1324 | |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1325 | AccessSpecifier UnprivilegedAccess = Entity.getAccess(); |
| 1326 | assert(UnprivilegedAccess != AS_public && "public access not weeded out"); |
| 1327 | |
| 1328 | // Before we try to recalculate access paths, try to white-list |
| 1329 | // accesses which just trade in on the final step, i.e. accesses |
| 1330 | // which don't require [M4] or [B4]. These are by far the most |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1331 | // common forms of privileged access. |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1332 | if (UnprivilegedAccess != AS_none) { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1333 | switch (HasAccess(S, EC, NamingClass, UnprivilegedAccess, Entity)) { |
| 1334 | case AR_dependent: |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1335 | // This is actually an interesting policy decision. We don't |
| 1336 | // *have* to delay immediately here: we can do the full access |
| 1337 | // calculation in the hope that friendship on some intermediate |
| 1338 | // class will make the declaration accessible non-dependently. |
| 1339 | // But that's not cheap, and odds are very good (note: assertion |
| 1340 | // made without data) that the friend declaration will determine |
| 1341 | // access. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1342 | return AR_dependent; |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1343 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1344 | case AR_accessible: return AR_accessible; |
| 1345 | case AR_inaccessible: break; |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1349 | AccessTarget::SavedInstanceContext _ = Entity.saveInstanceContext(); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1350 | |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1351 | // We lower member accesses to base accesses by pretending that the |
| 1352 | // member is a base class of its declaring class. |
| 1353 | AccessSpecifier FinalAccess; |
| 1354 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1355 | if (Entity.isMemberAccess()) { |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1356 | // Determine if the declaration is accessible from EC when named |
| 1357 | // in its declaring class. |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1358 | NamedDecl *Target = Entity.getTargetDecl(); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1359 | const CXXRecordDecl *DeclaringClass = Entity.getDeclaringClass(); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1360 | |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1361 | FinalAccess = Target->getAccess(); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1362 | switch (HasAccess(S, EC, DeclaringClass, FinalAccess, Entity)) { |
| 1363 | case AR_accessible: |
John McCall | 5149fbf | 2013-02-22 03:52:55 +0000 | [diff] [blame] | 1364 | // Target is accessible at EC when named in its declaring class. |
| 1365 | // We can now hill-climb and simply check whether the declaring |
| 1366 | // class is accessible as a base of the naming class. This is |
| 1367 | // equivalent to checking the access of a notional public |
| 1368 | // member with no instance context. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1369 | FinalAccess = AS_public; |
John McCall | 5149fbf | 2013-02-22 03:52:55 +0000 | [diff] [blame] | 1370 | Entity.suppressInstanceContext(); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1371 | break; |
| 1372 | case AR_inaccessible: break; |
| 1373 | case AR_dependent: return AR_dependent; // see above |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1376 | if (DeclaringClass == NamingClass) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1377 | return (FinalAccess == AS_public ? AR_accessible : AR_inaccessible); |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1378 | } else { |
| 1379 | FinalAccess = AS_public; |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1382 | assert(Entity.getDeclaringClass() != NamingClass); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1383 | |
| 1384 | // Append the declaration's access if applicable. |
| 1385 | CXXBasePaths Paths; |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1386 | CXXBasePath *Path = FindBestPath(S, EC, Entity, FinalAccess, Paths); |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1387 | if (!Path) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1388 | return AR_dependent; |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1389 | |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1390 | assert(Path->Access <= UnprivilegedAccess && |
| 1391 | "access along best path worse than direct?"); |
| 1392 | if (Path->Access == AS_public) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1393 | return AR_accessible; |
| 1394 | return AR_inaccessible; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1397 | static void DelayDependentAccess(Sema &S, |
| 1398 | const EffectiveContext &EC, |
| 1399 | SourceLocation Loc, |
| 1400 | const AccessTarget &Entity) { |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1401 | assert(EC.isDependent() && "delaying non-dependent access"); |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 1402 | DeclContext *DC = EC.getInnerContext(); |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1403 | assert(DC->isDependentContext() && "delaying non-dependent access"); |
| 1404 | DependentDiagnostic::Create(S.Context, DC, DependentDiagnostic::Access, |
| 1405 | Loc, |
| 1406 | Entity.isMemberAccess(), |
| 1407 | Entity.getAccess(), |
| 1408 | Entity.getTargetDecl(), |
| 1409 | Entity.getNamingClass(), |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1410 | Entity.getBaseObjectType(), |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1411 | Entity.getDiag()); |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1414 | /// Checks access to an entity from the given effective context. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1415 | static AccessResult CheckEffectiveAccess(Sema &S, |
| 1416 | const EffectiveContext &EC, |
| 1417 | SourceLocation Loc, |
| 1418 | AccessTarget &Entity) { |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1419 | assert(Entity.getAccess() != AS_public && "called for public access!"); |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1420 | |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1421 | switch (IsAccessible(S, EC, Entity)) { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1422 | case AR_dependent: |
| 1423 | DelayDependentAccess(S, EC, Loc, Entity); |
| 1424 | return AR_dependent; |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1425 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1426 | case AR_inaccessible: |
Reid Kleckner | 42063b0 | 2014-02-08 02:40:20 +0000 | [diff] [blame] | 1427 | if (S.getLangOpts().MSVCCompat && |
| 1428 | IsMicrosoftUsingDeclarationAccessBug(S, Loc, Entity)) |
| 1429 | return AR_accessible; |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1430 | if (!Entity.isQuiet()) |
| 1431 | DiagnoseBadAccess(S, Loc, EC, Entity); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1432 | return AR_inaccessible; |
John McCall | d79b4d8 | 2010-04-02 00:03:43 +0000 | [diff] [blame] | 1433 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1434 | case AR_accessible: |
| 1435 | return AR_accessible; |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1438 | // silence unnecessary warning |
| 1439 | llvm_unreachable("invalid access result"); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1440 | } |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1441 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1442 | static Sema::AccessResult CheckAccess(Sema &S, SourceLocation Loc, |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1443 | AccessTarget &Entity) { |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1444 | // If the access path is public, it's accessible everywhere. |
| 1445 | if (Entity.getAccess() == AS_public) |
| 1446 | return Sema::AR_accessible; |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1447 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 1448 | // If we're currently parsing a declaration, we may need to delay |
| 1449 | // access control checking, because our effective context might be |
| 1450 | // different based on what the declaration comes out as. |
| 1451 | // |
| 1452 | // For example, we might be parsing a declaration with a scope |
| 1453 | // specifier, like this: |
| 1454 | // A::private_type A::foo() { ... } |
| 1455 | // |
| 1456 | // Or we might be parsing something that will turn out to be a friend: |
| 1457 | // void foo(A::private_type); |
| 1458 | // void B::foo(A::private_type); |
| 1459 | if (S.DelayedDiagnostics.shouldDelayDiagnostics()) { |
| 1460 | S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity)); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1461 | return Sema::AR_delayed; |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1464 | EffectiveContext EC(S.CurContext); |
| 1465 | switch (CheckEffectiveAccess(S, EC, Loc, Entity)) { |
| 1466 | case AR_accessible: return Sema::AR_accessible; |
| 1467 | case AR_inaccessible: return Sema::AR_inaccessible; |
| 1468 | case AR_dependent: return Sema::AR_dependent; |
| 1469 | } |
| 1470 | llvm_unreachable("falling off end"); |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Richard Smith | ad5c1ca | 2013-04-29 10:13:55 +0000 | [diff] [blame] | 1473 | void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *D) { |
John McCall | 9743e8d | 2011-02-15 22:51:53 +0000 | [diff] [blame] | 1474 | // Access control for names used in the declarations of functions |
| 1475 | // and function templates should normally be evaluated in the context |
| 1476 | // of the declaration, just in case it's a friend of something. |
| 1477 | // However, this does not apply to local extern declarations. |
| 1478 | |
Richard Smith | ad5c1ca | 2013-04-29 10:13:55 +0000 | [diff] [blame] | 1479 | DeclContext *DC = D->getDeclContext(); |
Richard Smith | 608da01 | 2013-12-11 03:35:27 +0000 | [diff] [blame] | 1480 | if (D->isLocalExternDecl()) { |
| 1481 | DC = D->getLexicalDeclContext(); |
| 1482 | } else if (FunctionDecl *FN = dyn_cast<FunctionDecl>(D)) { |
| 1483 | DC = FN; |
Richard Smith | ad5c1ca | 2013-04-29 10:13:55 +0000 | [diff] [blame] | 1484 | } else if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) { |
| 1485 | DC = cast<DeclContext>(TD->getTemplatedDecl()); |
John McCall | 9743e8d | 2011-02-15 22:51:53 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Chandler Carruth | aad3007 | 2010-04-18 08:23:21 +0000 | [diff] [blame] | 1488 | EffectiveContext EC(DC); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 1489 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1490 | AccessTarget Target(DD.getAccessData()); |
| 1491 | |
| 1492 | if (CheckEffectiveAccess(*this, EC, DD.Loc, Target) == ::AR_inaccessible) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 1493 | DD.Triggered = true; |
| 1494 | } |
| 1495 | |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1496 | void Sema::HandleDependentAccessCheck(const DependentDiagnostic &DD, |
| 1497 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 1498 | SourceLocation Loc = DD.getAccessLoc(); |
| 1499 | AccessSpecifier Access = DD.getAccess(); |
| 1500 | |
| 1501 | Decl *NamingD = FindInstantiatedDecl(Loc, DD.getAccessNamingClass(), |
| 1502 | TemplateArgs); |
| 1503 | if (!NamingD) return; |
| 1504 | Decl *TargetD = FindInstantiatedDecl(Loc, DD.getAccessTarget(), |
| 1505 | TemplateArgs); |
| 1506 | if (!TargetD) return; |
| 1507 | |
| 1508 | if (DD.isAccessToMember()) { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1509 | CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(NamingD); |
| 1510 | NamedDecl *TargetDecl = cast<NamedDecl>(TargetD); |
| 1511 | QualType BaseObjectType = DD.getAccessBaseObjectType(); |
| 1512 | if (!BaseObjectType.isNull()) { |
| 1513 | BaseObjectType = SubstType(BaseObjectType, TemplateArgs, Loc, |
| 1514 | DeclarationName()); |
| 1515 | if (BaseObjectType.isNull()) return; |
| 1516 | } |
| 1517 | |
| 1518 | AccessTarget Entity(Context, |
| 1519 | AccessTarget::Member, |
| 1520 | NamingClass, |
| 1521 | DeclAccessPair::make(TargetDecl, Access), |
| 1522 | BaseObjectType); |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1523 | Entity.setDiag(DD.getDiagnostic()); |
| 1524 | CheckAccess(*this, Loc, Entity); |
| 1525 | } else { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1526 | AccessTarget Entity(Context, |
| 1527 | AccessTarget::Base, |
| 1528 | cast<CXXRecordDecl>(TargetD), |
| 1529 | cast<CXXRecordDecl>(NamingD), |
| 1530 | Access); |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1531 | Entity.setDiag(DD.getDiagnostic()); |
| 1532 | CheckAccess(*this, Loc, Entity); |
| 1533 | } |
| 1534 | } |
| 1535 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1536 | Sema::AccessResult Sema::CheckUnresolvedLookupAccess(UnresolvedLookupExpr *E, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1537 | DeclAccessPair Found) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1538 | if (!getLangOpts().AccessControl || |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1539 | !E->getNamingClass() || |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1540 | Found.getAccess() == AS_public) |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1541 | return AR_accessible; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1542 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1543 | AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(), |
| 1544 | Found, QualType()); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1545 | Entity.setDiag(diag::err_access) << E->getSourceRange(); |
| 1546 | |
| 1547 | return CheckAccess(*this, E->getNameLoc(), Entity); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | /// Perform access-control checking on a previously-unresolved member |
| 1551 | /// access which has now been resolved to a member. |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1552 | Sema::AccessResult Sema::CheckUnresolvedMemberAccess(UnresolvedMemberExpr *E, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1553 | DeclAccessPair Found) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1554 | if (!getLangOpts().AccessControl || |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1555 | Found.getAccess() == AS_public) |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1556 | return AR_accessible; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1557 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1558 | QualType BaseType = E->getBaseType(); |
| 1559 | if (E->isArrow()) |
| 1560 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 1561 | |
| 1562 | AccessTarget Entity(Context, AccessTarget::Member, E->getNamingClass(), |
| 1563 | Found, BaseType); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1564 | Entity.setDiag(diag::err_access) << E->getSourceRange(); |
| 1565 | |
| 1566 | return CheckAccess(*this, E->getMemberLoc(), Entity); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
John McCall | d427421 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 1569 | /// Is the given special member function accessible for the purposes of |
| 1570 | /// deciding whether to define a special member function as deleted? |
| 1571 | bool Sema::isSpecialMemberAccessibleForDeletion(CXXMethodDecl *decl, |
| 1572 | AccessSpecifier access, |
| 1573 | QualType objectType) { |
| 1574 | // Fast path. |
| 1575 | if (access == AS_public || !getLangOpts().AccessControl) return true; |
| 1576 | |
| 1577 | AccessTarget entity(Context, AccessTarget::Member, decl->getParent(), |
| 1578 | DeclAccessPair::make(decl, access), objectType); |
| 1579 | |
| 1580 | // Suppress diagnostics. |
| 1581 | entity.setDiag(PDiag()); |
| 1582 | |
| 1583 | switch (CheckAccess(*this, SourceLocation(), entity)) { |
| 1584 | case AR_accessible: return true; |
| 1585 | case AR_inaccessible: return false; |
| 1586 | case AR_dependent: llvm_unreachable("dependent for =delete computation"); |
| 1587 | case AR_delayed: llvm_unreachable("cannot delay =delete computation"); |
| 1588 | } |
| 1589 | llvm_unreachable("bad access result"); |
| 1590 | } |
| 1591 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1592 | Sema::AccessResult Sema::CheckDestructorAccess(SourceLocation Loc, |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1593 | CXXDestructorDecl *Dtor, |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1594 | const PartialDiagnostic &PDiag, |
| 1595 | QualType ObjectTy) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1596 | if (!getLangOpts().AccessControl) |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1597 | return AR_accessible; |
John McCall | 6781b05 | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 1598 | |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1599 | // There's never a path involved when checking implicit destructor access. |
John McCall | 6781b05 | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 1600 | AccessSpecifier Access = Dtor->getAccess(); |
| 1601 | if (Access == AS_public) |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1602 | return AR_accessible; |
John McCall | 6781b05 | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 1603 | |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1604 | CXXRecordDecl *NamingClass = Dtor->getParent(); |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1605 | if (ObjectTy.isNull()) ObjectTy = Context.getTypeDeclType(NamingClass); |
| 1606 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1607 | AccessTarget Entity(Context, AccessTarget::Member, NamingClass, |
| 1608 | DeclAccessPair::make(Dtor, Access), |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1609 | ObjectTy); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1610 | Entity.setDiag(PDiag); // TODO: avoid copy |
| 1611 | |
| 1612 | return CheckAccess(*this, Loc, Entity); |
John McCall | 6781b05 | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 1615 | /// Checks access to a constructor. |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1616 | Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc, |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 1617 | CXXConstructorDecl *Constructor, |
| 1618 | const InitializedEntity &Entity, |
| 1619 | AccessSpecifier Access, |
| 1620 | bool IsCopyBindingRefToTemp) { |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1621 | if (!getLangOpts().AccessControl || Access == AS_public) |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1622 | return AR_accessible; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 1623 | |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 1624 | PartialDiagnostic PD(PDiag()); |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 1625 | switch (Entity.getKind()) { |
| 1626 | default: |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 1627 | PD = PDiag(IsCopyBindingRefToTemp |
| 1628 | ? diag::ext_rvalue_to_reference_access_ctor |
| 1629 | : diag::err_access_ctor); |
| 1630 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 1631 | break; |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1632 | |
Anders Carlsson | 05bf009 | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 1633 | case InitializedEntity::EK_Base: |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 1634 | PD = PDiag(diag::err_access_base_ctor); |
| 1635 | PD << Entity.isInheritedVirtualBase() |
| 1636 | << Entity.getBaseSpecifier()->getType() << getSpecialMember(Constructor); |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 1637 | break; |
Anders Carlsson | 05bf009 | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 1638 | |
Anders Carlsson | 4bb6e92 | 2010-04-21 20:28:29 +0000 | [diff] [blame] | 1639 | case InitializedEntity::EK_Member: { |
| 1640 | const FieldDecl *Field = cast<FieldDecl>(Entity.getDecl()); |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 1641 | PD = PDiag(diag::err_access_field_ctor); |
| 1642 | PD << Field->getType() << getSpecialMember(Constructor); |
Anders Carlsson | 4bb6e92 | 2010-04-21 20:28:29 +0000 | [diff] [blame] | 1643 | break; |
| 1644 | } |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 1645 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 1646 | case InitializedEntity::EK_LambdaCapture: { |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 1647 | StringRef VarName = Entity.getCapturedVarName(); |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 1648 | PD = PDiag(diag::err_access_lambda_capture); |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 1649 | PD << VarName << Entity.getType() << getSpecialMember(Constructor); |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 1650 | break; |
| 1651 | } |
| 1652 | |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1655 | return CheckConstructorAccess(UseLoc, Constructor, Entity, Access, PD); |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | /// Checks access to a constructor. |
| 1659 | Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc, |
| 1660 | CXXConstructorDecl *Constructor, |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1661 | const InitializedEntity &Entity, |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 1662 | AccessSpecifier Access, |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1663 | const PartialDiagnostic &PD) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1664 | if (!getLangOpts().AccessControl || |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 1665 | Access == AS_public) |
| 1666 | return AR_accessible; |
| 1667 | |
| 1668 | CXXRecordDecl *NamingClass = Constructor->getParent(); |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1669 | |
| 1670 | // Initializing a base sub-object is an instance method call on an |
| 1671 | // object of the derived class. Otherwise, we have an instance method |
| 1672 | // call on an object of the constructed type. |
| 1673 | CXXRecordDecl *ObjectClass; |
| 1674 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 1675 | ObjectClass = cast<CXXConstructorDecl>(CurContext)->getParent(); |
| 1676 | } else { |
| 1677 | ObjectClass = NamingClass; |
| 1678 | } |
| 1679 | |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 1680 | AccessTarget AccessEntity(Context, AccessTarget::Member, NamingClass, |
| 1681 | DeclAccessPair::make(Constructor, Access), |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1682 | Context.getTypeDeclType(ObjectClass)); |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 1683 | AccessEntity.setDiag(PD); |
| 1684 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 1685 | return CheckAccess(*this, UseLoc, AccessEntity); |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1686 | } |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 1687 | |
John McCall | fb6f526 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1688 | /// Checks access to an overloaded operator new or delete. |
| 1689 | Sema::AccessResult Sema::CheckAllocationAccess(SourceLocation OpLoc, |
| 1690 | SourceRange PlacementRange, |
| 1691 | CXXRecordDecl *NamingClass, |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1692 | DeclAccessPair Found, |
| 1693 | bool Diagnose) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1694 | if (!getLangOpts().AccessControl || |
John McCall | fb6f526 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1695 | !NamingClass || |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1696 | Found.getAccess() == AS_public) |
John McCall | fb6f526 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1697 | return AR_accessible; |
| 1698 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1699 | AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found, |
| 1700 | QualType()); |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1701 | if (Diagnose) |
| 1702 | Entity.setDiag(diag::err_access) |
| 1703 | << PlacementRange; |
John McCall | fb6f526 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1704 | |
| 1705 | return CheckAccess(*this, OpLoc, Entity); |
| 1706 | } |
| 1707 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1708 | /// \brief Checks access to a member. |
| 1709 | Sema::AccessResult Sema::CheckMemberAccess(SourceLocation UseLoc, |
| 1710 | CXXRecordDecl *NamingClass, |
Eli Friedman | 3be1a1c | 2013-10-01 02:44:48 +0000 | [diff] [blame] | 1711 | DeclAccessPair Found) { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1712 | if (!getLangOpts().AccessControl || |
| 1713 | !NamingClass || |
Eli Friedman | 3be1a1c | 2013-10-01 02:44:48 +0000 | [diff] [blame] | 1714 | Found.getAccess() == AS_public) |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1715 | return AR_accessible; |
| 1716 | |
| 1717 | AccessTarget Entity(Context, AccessTarget::Member, NamingClass, |
Eli Friedman | 3be1a1c | 2013-10-01 02:44:48 +0000 | [diff] [blame] | 1718 | Found, QualType()); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1719 | |
| 1720 | return CheckAccess(*this, UseLoc, Entity); |
| 1721 | } |
| 1722 | |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 1723 | /// Checks access to an overloaded member operator, including |
| 1724 | /// conversion operators. |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1725 | Sema::AccessResult Sema::CheckMemberOperatorAccess(SourceLocation OpLoc, |
| 1726 | Expr *ObjectExpr, |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1727 | Expr *ArgExpr, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1728 | DeclAccessPair Found) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1729 | if (!getLangOpts().AccessControl || |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1730 | Found.getAccess() == AS_public) |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1731 | return AR_accessible; |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 1732 | |
John McCall | 3090903 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 1733 | const RecordType *RT = ObjectExpr->getType()->castAs<RecordType>(); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 1734 | CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(RT->getDecl()); |
| 1735 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1736 | AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found, |
| 1737 | ObjectExpr->getType()); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1738 | Entity.setDiag(diag::err_access) |
| 1739 | << ObjectExpr->getSourceRange() |
| 1740 | << (ArgExpr ? ArgExpr->getSourceRange() : SourceRange()); |
| 1741 | |
| 1742 | return CheckAccess(*this, OpLoc, Entity); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1743 | } |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 1744 | |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1745 | /// Checks access to the target of a friend declaration. |
| 1746 | Sema::AccessResult Sema::CheckFriendAccess(NamedDecl *target) { |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 1747 | assert(isa<CXXMethodDecl>(target->getAsFunction())); |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1748 | |
| 1749 | // Friendship lookup is a redeclaration lookup, so there's never an |
| 1750 | // inheritance path modifying access. |
| 1751 | AccessSpecifier access = target->getAccess(); |
| 1752 | |
| 1753 | if (!getLangOpts().AccessControl || access == AS_public) |
| 1754 | return AR_accessible; |
| 1755 | |
Alp Toker | 2bd4a28 | 2014-01-22 07:53:08 +0000 | [diff] [blame] | 1756 | CXXMethodDecl *method = cast<CXXMethodDecl>(target->getAsFunction()); |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1757 | assert(method->getQualifier()); |
| 1758 | |
| 1759 | AccessTarget entity(Context, AccessTarget::Member, |
| 1760 | cast<CXXRecordDecl>(target->getDeclContext()), |
| 1761 | DeclAccessPair::make(target, access), |
| 1762 | /*no instance context*/ QualType()); |
| 1763 | entity.setDiag(diag::err_access_friend_function) |
| 1764 | << method->getQualifierLoc().getSourceRange(); |
| 1765 | |
| 1766 | // We need to bypass delayed-diagnostics because we might be called |
| 1767 | // while the ParsingDeclarator is active. |
| 1768 | EffectiveContext EC(CurContext); |
| 1769 | switch (CheckEffectiveAccess(*this, EC, target->getLocation(), entity)) { |
| 1770 | case AR_accessible: return Sema::AR_accessible; |
| 1771 | case AR_inaccessible: return Sema::AR_inaccessible; |
| 1772 | case AR_dependent: return Sema::AR_dependent; |
| 1773 | } |
| 1774 | llvm_unreachable("falling off end"); |
| 1775 | } |
| 1776 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1777 | Sema::AccessResult Sema::CheckAddressOfMemberAccess(Expr *OvlExpr, |
| 1778 | DeclAccessPair Found) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1779 | if (!getLangOpts().AccessControl || |
John McCall | b493d53 | 2010-03-30 22:20:00 +0000 | [diff] [blame] | 1780 | Found.getAccess() == AS_none || |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1781 | Found.getAccess() == AS_public) |
| 1782 | return AR_accessible; |
| 1783 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1784 | OverloadExpr *Ovl = OverloadExpr::find(OvlExpr).Expression; |
John McCall | 8c12dc4 | 2010-04-22 18:44:12 +0000 | [diff] [blame] | 1785 | CXXRecordDecl *NamingClass = Ovl->getNamingClass(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1786 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1787 | AccessTarget Entity(Context, AccessTarget::Member, NamingClass, Found, |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 1788 | /*no instance context*/ QualType()); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1789 | Entity.setDiag(diag::err_access) |
| 1790 | << Ovl->getSourceRange(); |
| 1791 | |
| 1792 | return CheckAccess(*this, Ovl->getNameLoc(), Entity); |
| 1793 | } |
| 1794 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1795 | /// Checks access for a hierarchy conversion. |
| 1796 | /// |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1797 | /// \param ForceCheck true if this check should be performed even if access |
| 1798 | /// control is disabled; some things rely on this for semantics |
| 1799 | /// \param ForceUnprivileged true if this check should proceed as if the |
| 1800 | /// context had no special privileges |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1801 | Sema::AccessResult Sema::CheckBaseClassAccess(SourceLocation AccessLoc, |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1802 | QualType Base, |
| 1803 | QualType Derived, |
| 1804 | const CXXBasePath &Path, |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1805 | unsigned DiagID, |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1806 | bool ForceCheck, |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1807 | bool ForceUnprivileged) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1808 | if (!ForceCheck && !getLangOpts().AccessControl) |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1809 | return AR_accessible; |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 1810 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1811 | if (Path.Access == AS_public) |
| 1812 | return AR_accessible; |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 1813 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1814 | CXXRecordDecl *BaseD, *DerivedD; |
| 1815 | BaseD = cast<CXXRecordDecl>(Base->getAs<RecordType>()->getDecl()); |
| 1816 | DerivedD = cast<CXXRecordDecl>(Derived->getAs<RecordType>()->getDecl()); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1817 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1818 | AccessTarget Entity(Context, AccessTarget::Base, BaseD, DerivedD, |
| 1819 | Path.Access); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1820 | if (DiagID) |
| 1821 | Entity.setDiag(DiagID) << Derived << Base; |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1822 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1823 | if (ForceUnprivileged) { |
| 1824 | switch (CheckEffectiveAccess(*this, EffectiveContext(), |
| 1825 | AccessLoc, Entity)) { |
| 1826 | case ::AR_accessible: return Sema::AR_accessible; |
| 1827 | case ::AR_inaccessible: return Sema::AR_inaccessible; |
| 1828 | case ::AR_dependent: return Sema::AR_dependent; |
| 1829 | } |
| 1830 | llvm_unreachable("unexpected result from CheckEffectiveAccess"); |
| 1831 | } |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1832 | return CheckAccess(*this, AccessLoc, Entity); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1835 | /// Checks access to all the declarations in the given result set. |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1836 | void Sema::CheckLookupAccess(const LookupResult &R) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1837 | assert(getLangOpts().AccessControl |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1838 | && "performing access check without access control"); |
| 1839 | assert(R.getNamingClass() && "performing access check without naming class"); |
| 1840 | |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1841 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 1842 | if (I.getAccess() != AS_public) { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1843 | AccessTarget Entity(Context, AccessedEntity::Member, |
| 1844 | R.getNamingClass(), I.getPair(), |
Erik Verbruggen | 631dfc6 | 2011-09-19 15:10:40 +0000 | [diff] [blame] | 1845 | R.getBaseObjectType()); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1846 | Entity.setDiag(diag::err_access); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1847 | CheckAccess(*this, R.getNameLoc(), Entity); |
| 1848 | } |
| 1849 | } |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 1850 | } |
Chandler Carruth | 2d69ec7 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 1851 | |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1852 | /// Checks access to Decl from the given class. The check will take access |
| 1853 | /// specifiers into account, but no member access expressions and such. |
| 1854 | /// |
| 1855 | /// \param Decl the declaration to check if it can be accessed |
Dmitri Gribenko | dd28e79 | 2012-08-24 00:01:24 +0000 | [diff] [blame] | 1856 | /// \param Ctx the class/context from which to start the search |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1857 | /// \return true if the Decl is accessible from the Class, false otherwise. |
Douglas Gregor | 03ba188 | 2011-11-03 16:51:37 +0000 | [diff] [blame] | 1858 | bool Sema::IsSimplyAccessible(NamedDecl *Decl, DeclContext *Ctx) { |
| 1859 | if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Ctx)) { |
Douglas Gregor | 3b52b69 | 2011-11-03 17:41:55 +0000 | [diff] [blame] | 1860 | if (!Decl->isCXXClassMember()) |
Douglas Gregor | 03ba188 | 2011-11-03 16:51:37 +0000 | [diff] [blame] | 1861 | return true; |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1862 | |
Douglas Gregor | 03ba188 | 2011-11-03 16:51:37 +0000 | [diff] [blame] | 1863 | QualType qType = Class->getTypeForDecl()->getCanonicalTypeInternal(); |
| 1864 | AccessTarget Entity(Context, AccessedEntity::Member, Class, |
| 1865 | DeclAccessPair::make(Decl, Decl->getAccess()), |
| 1866 | qType); |
| 1867 | if (Entity.getAccess() == AS_public) |
| 1868 | return true; |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1869 | |
Douglas Gregor | 03ba188 | 2011-11-03 16:51:37 +0000 | [diff] [blame] | 1870 | EffectiveContext EC(CurContext); |
| 1871 | return ::IsAccessible(*this, EC, Entity) != ::AR_inaccessible; |
| 1872 | } |
| 1873 | |
Douglas Gregor | 21ceb18 | 2011-11-03 19:00:24 +0000 | [diff] [blame] | 1874 | if (ObjCIvarDecl *Ivar = dyn_cast<ObjCIvarDecl>(Decl)) { |
| 1875 | // @public and @package ivars are always accessible. |
| 1876 | if (Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Public || |
| 1877 | Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Package) |
| 1878 | return true; |
Serge Pavlov | 64bc703 | 2013-05-07 16:56:03 +0000 | [diff] [blame] | 1879 | |
Douglas Gregor | 21ceb18 | 2011-11-03 19:00:24 +0000 | [diff] [blame] | 1880 | // If we are inside a class or category implementation, determine the |
| 1881 | // interface we're in. |
| 1882 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 1883 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 1884 | ClassOfMethodDecl = MD->getClassInterface(); |
| 1885 | else if (FunctionDecl *FD = getCurFunctionDecl()) { |
| 1886 | if (ObjCImplDecl *Impl |
| 1887 | = dyn_cast<ObjCImplDecl>(FD->getLexicalDeclContext())) { |
| 1888 | if (ObjCImplementationDecl *IMPD |
| 1889 | = dyn_cast<ObjCImplementationDecl>(Impl)) |
| 1890 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 1891 | else if (ObjCCategoryImplDecl* CatImplClass |
| 1892 | = dyn_cast<ObjCCategoryImplDecl>(Impl)) |
| 1893 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | // If we're not in an interface, this ivar is inaccessible. |
| 1898 | if (!ClassOfMethodDecl) |
| 1899 | return false; |
| 1900 | |
| 1901 | // If we're inside the same interface that owns the ivar, we're fine. |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 1902 | if (declaresSameEntity(ClassOfMethodDecl, Ivar->getContainingInterface())) |
Douglas Gregor | 21ceb18 | 2011-11-03 19:00:24 +0000 | [diff] [blame] | 1903 | return true; |
| 1904 | |
| 1905 | // If the ivar is private, it's inaccessible. |
| 1906 | if (Ivar->getCanonicalAccessControl() == ObjCIvarDecl::Private) |
| 1907 | return false; |
| 1908 | |
| 1909 | return Ivar->getContainingInterface()->isSuperClassOf(ClassOfMethodDecl); |
| 1910 | } |
| 1911 | |
Douglas Gregor | 03ba188 | 2011-11-03 16:51:37 +0000 | [diff] [blame] | 1912 | return true; |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1913 | } |