Anders Carlsson | 29f006b | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 1 | //===---- SemaAccess.cpp - C++ Access Control -------------------*- C++ -*-===// |
Anders Carlsson | 60d6b0d | 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 | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 13 | |
| 14 | #include "Sema.h" |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 15 | #include "Lookup.h" |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
| 18 | #include "clang/AST/DeclCXX.h" |
John McCall | d60e22e | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclFriend.h" |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
| 21 | |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Anders Carlsson | 29f006b | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 24 | /// SetMemberAccessSpecifier - Set the access specifier of a member. |
| 25 | /// Returns true on error (when the previous member decl access specifier |
| 26 | /// is different from the new member decl access specifier). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 27 | bool Sema::SetMemberAccessSpecifier(NamedDecl *MemberDecl, |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 28 | NamedDecl *PrevMemberDecl, |
| 29 | AccessSpecifier LexicalAS) { |
| 30 | if (!PrevMemberDecl) { |
| 31 | // Use the lexical access specifier. |
| 32 | MemberDecl->setAccess(LexicalAS); |
| 33 | return false; |
| 34 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 36 | // C++ [class.access.spec]p3: When a member is redeclared its access |
| 37 | // specifier must be same as its initial declaration. |
| 38 | if (LexicalAS != AS_none && LexicalAS != PrevMemberDecl->getAccess()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | Diag(MemberDecl->getLocation(), |
| 40 | diag::err_class_redeclared_with_different_access) |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 41 | << MemberDecl << LexicalAS; |
| 42 | Diag(PrevMemberDecl->getLocation(), diag::note_previous_access_declaration) |
| 43 | << PrevMemberDecl << PrevMemberDecl->getAccess(); |
John McCall | 44e067b | 2009-12-23 00:37:40 +0000 | [diff] [blame] | 44 | |
| 45 | MemberDecl->setAccess(LexicalAS); |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 46 | return true; |
| 47 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 49 | MemberDecl->setAccess(PrevMemberDecl->getAccess()); |
| 50 | return false; |
| 51 | } |
Anders Carlsson | 29f006b | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 52 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 53 | namespace { |
| 54 | struct EffectiveContext { |
| 55 | EffectiveContext() : Record(0), Function(0) {} |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 56 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 57 | explicit EffectiveContext(DeclContext *DC) { |
| 58 | if (isa<FunctionDecl>(DC)) { |
John McCall | d60e22e | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 59 | Function = cast<FunctionDecl>(DC)->getCanonicalDecl(); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 60 | DC = Function->getDeclContext(); |
| 61 | } else |
| 62 | Function = 0; |
| 63 | |
| 64 | if (isa<CXXRecordDecl>(DC)) |
| 65 | Record = cast<CXXRecordDecl>(DC)->getCanonicalDecl(); |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 66 | else |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 67 | Record = 0; |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 68 | } |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 69 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 70 | bool isClass(const CXXRecordDecl *R) const { |
| 71 | return R->getCanonicalDecl() == Record; |
| 72 | } |
| 73 | |
| 74 | CXXRecordDecl *Record; |
| 75 | FunctionDecl *Function; |
| 76 | }; |
Anders Carlsson | 29f006b | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 77 | } |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 78 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 79 | static CXXRecordDecl *FindDeclaringClass(NamedDecl *D) { |
| 80 | CXXRecordDecl *DeclaringClass = cast<CXXRecordDecl>(D->getDeclContext()); |
| 81 | while (DeclaringClass->isAnonymousStructOrUnion()) |
| 82 | DeclaringClass = cast<CXXRecordDecl>(DeclaringClass->getDeclContext()); |
| 83 | return DeclaringClass; |
| 84 | } |
| 85 | |
| 86 | static Sema::AccessResult GetFriendKind(Sema &S, |
| 87 | const EffectiveContext &EC, |
| 88 | const CXXRecordDecl *Class) { |
John McCall | d60e22e | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 89 | // A class always has access to its own members. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 90 | if (EC.isClass(Class)) |
| 91 | return Sema::AR_accessible; |
| 92 | |
John McCall | d60e22e | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 93 | // Okay, check friends. |
| 94 | for (CXXRecordDecl::friend_iterator I = Class->friend_begin(), |
| 95 | E = Class->friend_end(); I != E; ++I) { |
| 96 | FriendDecl *Friend = *I; |
| 97 | |
| 98 | if (Type *T = Friend->getFriendType()) { |
| 99 | if (EC.Record && |
| 100 | S.Context.hasSameType(QualType(T, 0), |
| 101 | S.Context.getTypeDeclType(EC.Record))) |
| 102 | return Sema::AR_accessible; |
| 103 | } else { |
| 104 | NamedDecl *D |
| 105 | = cast<NamedDecl>(Friend->getFriendDecl()->getCanonicalDecl()); |
| 106 | |
| 107 | // The decl pointers in EC have been canonicalized, so pointer |
| 108 | // equality is sufficient. |
| 109 | if (D == EC.Function || D == EC.Record) |
| 110 | return Sema::AR_accessible; |
| 111 | } |
| 112 | |
| 113 | // FIXME: templates! templated contexts! dependent delay! |
| 114 | } |
| 115 | |
| 116 | // That's it, give up. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 117 | return Sema::AR_inaccessible; |
| 118 | } |
| 119 | |
| 120 | /// Finds the best path from the naming class to the declaring class, |
| 121 | /// taking friend declarations into account. |
| 122 | /// |
| 123 | /// \return null if friendship is dependent |
| 124 | static CXXBasePath *FindBestPath(Sema &S, |
| 125 | const EffectiveContext &EC, |
| 126 | CXXRecordDecl *Derived, |
| 127 | CXXRecordDecl *Base, |
| 128 | CXXBasePaths &Paths) { |
| 129 | // Derive the paths to the desired base. |
| 130 | bool isDerived = Derived->isDerivedFrom(Base, Paths); |
| 131 | assert(isDerived && "derived class not actually derived from base"); |
| 132 | (void) isDerived; |
| 133 | |
| 134 | CXXBasePath *BestPath = 0; |
| 135 | |
| 136 | // Derive the friend-modified access along each path. |
| 137 | for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end(); |
| 138 | PI != PE; ++PI) { |
| 139 | |
| 140 | // Walk through the path backwards. |
| 141 | AccessSpecifier PathAccess = AS_public; |
| 142 | CXXBasePath::iterator I = PI->end(), E = PI->begin(); |
| 143 | while (I != E) { |
| 144 | --I; |
| 145 | |
| 146 | AccessSpecifier BaseAccess = I->Base->getAccessSpecifier(); |
| 147 | if (BaseAccess != AS_public) { |
| 148 | switch (GetFriendKind(S, EC, I->Class)) { |
| 149 | case Sema::AR_inaccessible: break; |
| 150 | case Sema::AR_accessible: BaseAccess = AS_public; break; |
| 151 | case Sema::AR_dependent: return 0; |
| 152 | case Sema::AR_delayed: |
| 153 | llvm_unreachable("friend resolution is never delayed"); break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | PathAccess = CXXRecordDecl::MergeAccess(BaseAccess, PathAccess); |
| 158 | } |
| 159 | |
| 160 | // Note that we modify the path's Access field to the |
| 161 | // friend-modified access. |
| 162 | if (BestPath == 0 || PathAccess < BestPath->Access) { |
| 163 | BestPath = &*PI; |
| 164 | BestPath->Access = PathAccess; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return BestPath; |
| 169 | } |
| 170 | |
| 171 | /// Diagnose the path which caused the given declaration or base class |
| 172 | /// to become inaccessible. |
| 173 | static void DiagnoseAccessPath(Sema &S, |
| 174 | const EffectiveContext &EC, |
| 175 | CXXRecordDecl *NamingClass, |
| 176 | CXXRecordDecl *DeclaringClass, |
| 177 | NamedDecl *D, AccessSpecifier Access) { |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 178 | // Easy case: the decl's natural access determined its path access. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 179 | // We have to check against AS_private here in case Access is AS_none, |
| 180 | // indicating a non-public member of a private base class. |
| 181 | // |
| 182 | // DependentFriend should be impossible here. |
| 183 | if (D && (Access == D->getAccess() || D->getAccess() == AS_private)) { |
| 184 | switch (GetFriendKind(S, EC, DeclaringClass)) { |
| 185 | case Sema::AR_inaccessible: { |
| 186 | S.Diag(D->getLocation(), diag::note_access_natural) |
| 187 | << (unsigned) (Access == AS_protected) |
| 188 | << /*FIXME: not implicitly*/ 0; |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | case Sema::AR_accessible: break; |
| 193 | |
| 194 | case Sema::AR_dependent: |
| 195 | case Sema::AR_delayed: |
| 196 | llvm_unreachable("dependent/delayed not allowed"); |
| 197 | return; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | CXXBasePaths Paths; |
| 202 | CXXBasePath &Path = *FindBestPath(S, EC, NamingClass, DeclaringClass, Paths); |
| 203 | |
| 204 | CXXBasePath::iterator I = Path.end(), E = Path.begin(); |
| 205 | while (I != E) { |
| 206 | --I; |
| 207 | |
| 208 | const CXXBaseSpecifier *BS = I->Base; |
| 209 | AccessSpecifier BaseAccess = BS->getAccessSpecifier(); |
| 210 | |
| 211 | // If this is public inheritance, or the derived class is a friend, |
| 212 | // skip this step. |
| 213 | if (BaseAccess == AS_public) |
| 214 | continue; |
| 215 | |
| 216 | switch (GetFriendKind(S, EC, I->Class)) { |
| 217 | case Sema::AR_accessible: continue; |
| 218 | case Sema::AR_inaccessible: break; |
| 219 | |
| 220 | case Sema::AR_dependent: |
| 221 | case Sema::AR_delayed: |
| 222 | llvm_unreachable("dependent friendship, should not be diagnosing"); |
| 223 | } |
| 224 | |
| 225 | // Check whether this base specifier is the tighest point |
| 226 | // constraining access. We have to check against AS_private for |
| 227 | // the same reasons as above. |
| 228 | if (BaseAccess == AS_private || BaseAccess >= Access) { |
| 229 | |
| 230 | // We're constrained by inheritance, but we want to say |
| 231 | // "declared private here" if we're diagnosing a hierarchy |
| 232 | // conversion and this is the final step. |
| 233 | unsigned diagnostic; |
| 234 | if (D) diagnostic = diag::note_access_constrained_by_path; |
| 235 | else if (I + 1 == Path.end()) diagnostic = diag::note_access_natural; |
| 236 | else diagnostic = diag::note_access_constrained_by_path; |
| 237 | |
| 238 | S.Diag(BS->getSourceRange().getBegin(), diagnostic) |
| 239 | << BS->getSourceRange() |
| 240 | << (BaseAccess == AS_protected) |
| 241 | << (BS->getAccessSpecifierAsWritten() == AS_none); |
| 242 | return; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | llvm_unreachable("access not apparently constrained by path"); |
| 247 | } |
| 248 | |
| 249 | /// Diagnose an inaccessible class member. |
| 250 | static void DiagnoseInaccessibleMember(Sema &S, SourceLocation Loc, |
| 251 | const EffectiveContext &EC, |
| 252 | CXXRecordDecl *NamingClass, |
| 253 | AccessSpecifier Access, |
| 254 | const Sema::AccessedEntity &Entity) { |
| 255 | NamedDecl *D = Entity.getTargetDecl(); |
| 256 | CXXRecordDecl *DeclaringClass = FindDeclaringClass(D); |
| 257 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 258 | S.Diag(Loc, Entity.getDiag()) |
| 259 | << (Access == AS_protected) |
| 260 | << D->getDeclName() |
| 261 | << S.Context.getTypeDeclType(NamingClass) |
| 262 | << S.Context.getTypeDeclType(DeclaringClass); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 263 | DiagnoseAccessPath(S, EC, NamingClass, DeclaringClass, D, Access); |
| 264 | } |
| 265 | |
| 266 | /// Diagnose an inaccessible hierarchy conversion. |
| 267 | static void DiagnoseInaccessibleBase(Sema &S, SourceLocation Loc, |
| 268 | const EffectiveContext &EC, |
| 269 | AccessSpecifier Access, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 270 | const Sema::AccessedEntity &Entity) { |
| 271 | S.Diag(Loc, Entity.getDiag()) |
| 272 | << (Access == AS_protected) |
| 273 | << DeclarationName() |
| 274 | << S.Context.getTypeDeclType(Entity.getDerivedClass()) |
| 275 | << S.Context.getTypeDeclType(Entity.getBaseClass()); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 276 | DiagnoseAccessPath(S, EC, Entity.getDerivedClass(), |
| 277 | Entity.getBaseClass(), 0, Access); |
| 278 | } |
| 279 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 280 | static void DiagnoseBadAccess(Sema &S, SourceLocation Loc, |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 281 | const EffectiveContext &EC, |
| 282 | CXXRecordDecl *NamingClass, |
| 283 | AccessSpecifier Access, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 284 | const Sema::AccessedEntity &Entity) { |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 285 | if (Entity.isMemberAccess()) |
| 286 | DiagnoseInaccessibleMember(S, Loc, EC, NamingClass, Access, Entity); |
| 287 | else |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 288 | DiagnoseInaccessibleBase(S, Loc, EC, Access, Entity); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | |
| 292 | /// Try to elevate access using friend declarations. This is |
| 293 | /// potentially quite expensive. |
| 294 | static void TryElevateAccess(Sema &S, |
| 295 | const EffectiveContext &EC, |
| 296 | const Sema::AccessedEntity &Entity, |
| 297 | AccessSpecifier &Access) { |
| 298 | CXXRecordDecl *DeclaringClass; |
| 299 | if (Entity.isMemberAccess()) { |
| 300 | DeclaringClass = FindDeclaringClass(Entity.getTargetDecl()); |
| 301 | } else { |
| 302 | DeclaringClass = Entity.getBaseClass(); |
| 303 | } |
| 304 | CXXRecordDecl *NamingClass = Entity.getNamingClass(); |
| 305 | |
| 306 | // Adjust the declaration of the referred entity. |
| 307 | AccessSpecifier DeclAccess = AS_none; |
| 308 | if (Entity.isMemberAccess()) { |
| 309 | NamedDecl *Target = Entity.getTargetDecl(); |
| 310 | |
| 311 | DeclAccess = Target->getAccess(); |
| 312 | if (DeclAccess != AS_public) { |
| 313 | switch (GetFriendKind(S, EC, DeclaringClass)) { |
| 314 | case Sema::AR_accessible: DeclAccess = AS_public; break; |
| 315 | case Sema::AR_inaccessible: break; |
| 316 | case Sema::AR_dependent: /* FIXME: delay dependent friendship */ return; |
| 317 | case Sema::AR_delayed: llvm_unreachable("friend status is never delayed"); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | if (DeclaringClass == NamingClass) { |
| 322 | Access = DeclAccess; |
| 323 | return; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | assert(DeclaringClass != NamingClass); |
| 328 | |
| 329 | // Append the declaration's access if applicable. |
| 330 | CXXBasePaths Paths; |
| 331 | CXXBasePath *Path = FindBestPath(S, EC, Entity.getNamingClass(), |
| 332 | DeclaringClass, Paths); |
| 333 | if (!Path) { |
| 334 | // FIXME: delay dependent friendship |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 338 | // Grab the access along the best path. |
| 339 | AccessSpecifier NewAccess = Path->Access; |
| 340 | if (Entity.isMemberAccess()) |
| 341 | NewAccess = CXXRecordDecl::MergeAccess(NewAccess, DeclAccess); |
| 342 | |
| 343 | assert(NewAccess <= Access && "access along best path worse than direct?"); |
| 344 | Access = NewAccess; |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 345 | } |
| 346 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 347 | /// Checks access to an entity from the given effective context. |
| 348 | static Sema::AccessResult CheckEffectiveAccess(Sema &S, |
| 349 | const EffectiveContext &EC, |
| 350 | SourceLocation Loc, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 351 | Sema::AccessedEntity const &Entity) { |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 352 | AccessSpecifier Access = Entity.getAccess(); |
| 353 | assert(Access != AS_public); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 354 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 355 | CXXRecordDecl *NamingClass = Entity.getNamingClass(); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 356 | while (NamingClass->isAnonymousStructOrUnion()) |
| 357 | // This should be guaranteed by the fact that the decl has |
| 358 | // non-public access. If not, we should make it guaranteed! |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 359 | NamingClass = cast<CXXRecordDecl>(NamingClass->getParent()); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 360 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 361 | if (!EC.Record) { |
| 362 | TryElevateAccess(S, EC, Entity, Access); |
| 363 | if (Access == AS_public) return Sema::AR_accessible; |
| 364 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 365 | if (!Entity.isQuiet()) |
| 366 | DiagnoseBadAccess(S, Loc, EC, NamingClass, Access, Entity); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 367 | return Sema::AR_inaccessible; |
| 368 | } |
| 369 | |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 370 | // White-list accesses from within the declaring class. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 371 | if (Access != AS_none && EC.isClass(NamingClass)) |
| 372 | return Sema::AR_accessible; |
| 373 | |
| 374 | // If the access is worse than 'protected', try to promote to it using |
| 375 | // friend declarations. |
| 376 | bool TriedElevation = false; |
| 377 | if (Access != AS_protected) { |
| 378 | TryElevateAccess(S, EC, Entity, Access); |
| 379 | if (Access == AS_public) return Sema::AR_accessible; |
| 380 | TriedElevation = true; |
| 381 | } |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 382 | |
| 383 | // Protected access. |
| 384 | if (Access == AS_protected) { |
| 385 | // FIXME: implement [class.protected]p1 |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 386 | if (EC.Record->isDerivedFrom(NamingClass)) |
| 387 | return Sema::AR_accessible; |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 388 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 389 | // FIXME: delay dependent classes |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 390 | } |
| 391 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 392 | // We're about to reject; one last chance to promote access. |
| 393 | if (!TriedElevation) { |
| 394 | TryElevateAccess(S, EC, Entity, Access); |
| 395 | if (Access == AS_public) return Sema::AR_accessible; |
| 396 | } |
| 397 | |
| 398 | // Okay, that's it, reject it. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 399 | if (!Entity.isQuiet()) |
| 400 | DiagnoseBadAccess(S, Loc, EC, NamingClass, Access, Entity); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 401 | return Sema::AR_inaccessible; |
| 402 | } |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 403 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 404 | static Sema::AccessResult CheckAccess(Sema &S, SourceLocation Loc, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 405 | const Sema::AccessedEntity &Entity) { |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 406 | // If the access path is public, it's accessible everywhere. |
| 407 | if (Entity.getAccess() == AS_public) |
| 408 | return Sema::AR_accessible; |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 409 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 410 | // If we're currently parsing a top-level declaration, delay |
| 411 | // diagnostics. This is the only case where parsing a declaration |
| 412 | // can actually change our effective context for the purposes of |
| 413 | // access control. |
| 414 | if (S.CurContext->isFileContext() && S.ParsingDeclDepth) { |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 415 | S.DelayedDiagnostics.push_back( |
| 416 | Sema::DelayedDiagnostic::makeAccess(Loc, Entity)); |
| 417 | return Sema::AR_delayed; |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 418 | } |
| 419 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 420 | return CheckEffectiveAccess(S, EffectiveContext(S.CurContext), |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 421 | Loc, Entity); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 422 | } |
| 423 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 424 | void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *Ctx) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 425 | // Pretend we did this from the context of the newly-parsed |
| 426 | // declaration. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 427 | EffectiveContext EC(Ctx->getDeclContext()); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 428 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 429 | if (CheckEffectiveAccess(*this, EC, DD.Loc, DD.getAccessData())) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 430 | DD.Triggered = true; |
| 431 | } |
| 432 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 433 | Sema::AccessResult Sema::CheckUnresolvedLookupAccess(UnresolvedLookupExpr *E, |
| 434 | NamedDecl *D, |
| 435 | AccessSpecifier Access) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 436 | if (!getLangOptions().AccessControl || |
| 437 | !E->getNamingClass() || |
| 438 | Access == AS_public) |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 439 | return AR_accessible; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 440 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 441 | AccessedEntity Entity(AccessedEntity::Member, |
| 442 | E->getNamingClass(), Access, D); |
| 443 | Entity.setDiag(diag::err_access) << E->getSourceRange(); |
| 444 | |
| 445 | return CheckAccess(*this, E->getNameLoc(), Entity); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /// Perform access-control checking on a previously-unresolved member |
| 449 | /// access which has now been resolved to a member. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 450 | Sema::AccessResult Sema::CheckUnresolvedMemberAccess(UnresolvedMemberExpr *E, |
| 451 | NamedDecl *D, |
| 452 | AccessSpecifier Access) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 453 | if (!getLangOptions().AccessControl || |
| 454 | Access == AS_public) |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 455 | return AR_accessible; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 456 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 457 | AccessedEntity Entity(AccessedEntity::Member, |
| 458 | E->getNamingClass(), Access, D); |
| 459 | Entity.setDiag(diag::err_access) << E->getSourceRange(); |
| 460 | |
| 461 | return CheckAccess(*this, E->getMemberLoc(), Entity); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 462 | } |
| 463 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 464 | Sema::AccessResult Sema::CheckDestructorAccess(SourceLocation Loc, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 465 | CXXDestructorDecl *Dtor, |
| 466 | const PartialDiagnostic &PDiag) { |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 467 | if (!getLangOptions().AccessControl) |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 468 | return AR_accessible; |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 469 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 470 | // There's never a path involved when checking implicit destructor access. |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 471 | AccessSpecifier Access = Dtor->getAccess(); |
| 472 | if (Access == AS_public) |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 473 | return AR_accessible; |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 474 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 475 | CXXRecordDecl *NamingClass = Dtor->getParent(); |
| 476 | AccessedEntity Entity(AccessedEntity::Member, NamingClass, Access, Dtor); |
| 477 | Entity.setDiag(PDiag); // TODO: avoid copy |
| 478 | |
| 479 | return CheckAccess(*this, Loc, Entity); |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 480 | } |
| 481 | |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 482 | /// Checks access to a constructor. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 483 | Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc, |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 484 | CXXConstructorDecl *Constructor, |
| 485 | AccessSpecifier Access) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 486 | if (!getLangOptions().AccessControl || |
| 487 | Access == AS_public) |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 488 | return AR_accessible; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 489 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 490 | CXXRecordDecl *NamingClass = Constructor->getParent(); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 491 | AccessedEntity Entity(AccessedEntity::Member, |
| 492 | NamingClass, Access, Constructor); |
| 493 | Entity.setDiag(diag::err_access_ctor); |
| 494 | |
| 495 | return CheckAccess(*this, UseLoc, Entity); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | /// Checks access to an overloaded member operator, including |
| 499 | /// conversion operators. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 500 | Sema::AccessResult Sema::CheckMemberOperatorAccess(SourceLocation OpLoc, |
| 501 | Expr *ObjectExpr, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 502 | Expr *ArgExpr, |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 503 | NamedDecl *MemberOperator, |
| 504 | AccessSpecifier Access) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 505 | if (!getLangOptions().AccessControl || |
| 506 | Access == AS_public) |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 507 | return AR_accessible; |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 508 | |
| 509 | const RecordType *RT = ObjectExpr->getType()->getAs<RecordType>(); |
| 510 | assert(RT && "found member operator but object expr not of record type"); |
| 511 | CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(RT->getDecl()); |
| 512 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 513 | AccessedEntity Entity(AccessedEntity::Member, |
| 514 | NamingClass, Access, MemberOperator); |
| 515 | Entity.setDiag(diag::err_access) |
| 516 | << ObjectExpr->getSourceRange() |
| 517 | << (ArgExpr ? ArgExpr->getSourceRange() : SourceRange()); |
| 518 | |
| 519 | return CheckAccess(*this, OpLoc, Entity); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 520 | } |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 521 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 522 | /// Checks access for a hierarchy conversion. |
| 523 | /// |
| 524 | /// \param IsBaseToDerived whether this is a base-to-derived conversion (true) |
| 525 | /// or a derived-to-base conversion (false) |
| 526 | /// \param ForceCheck true if this check should be performed even if access |
| 527 | /// control is disabled; some things rely on this for semantics |
| 528 | /// \param ForceUnprivileged true if this check should proceed as if the |
| 529 | /// context had no special privileges |
| 530 | /// \param ADK controls the kind of diagnostics that are used |
| 531 | Sema::AccessResult Sema::CheckBaseClassAccess(SourceLocation AccessLoc, |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 532 | QualType Base, |
| 533 | QualType Derived, |
| 534 | const CXXBasePath &Path, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 535 | unsigned DiagID, |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 536 | bool ForceCheck, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 537 | bool ForceUnprivileged) { |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 538 | if (!ForceCheck && !getLangOptions().AccessControl) |
| 539 | return AR_accessible; |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 540 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 541 | if (Path.Access == AS_public) |
| 542 | return AR_accessible; |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 543 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 544 | CXXRecordDecl *BaseD, *DerivedD; |
| 545 | BaseD = cast<CXXRecordDecl>(Base->getAs<RecordType>()->getDecl()); |
| 546 | DerivedD = cast<CXXRecordDecl>(Derived->getAs<RecordType>()->getDecl()); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 547 | |
| 548 | AccessedEntity Entity(AccessedEntity::Base, BaseD, DerivedD, Path.Access); |
| 549 | if (DiagID) |
| 550 | Entity.setDiag(DiagID) << Derived << Base; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 551 | |
| 552 | if (ForceUnprivileged) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 553 | return CheckEffectiveAccess(*this, EffectiveContext(), AccessLoc, Entity); |
| 554 | return CheckAccess(*this, AccessLoc, Entity); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 555 | } |
| 556 | |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 557 | /// Checks access to all the declarations in the given result set. |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 558 | void Sema::CheckLookupAccess(const LookupResult &R) { |
| 559 | assert(getLangOptions().AccessControl |
| 560 | && "performing access check without access control"); |
| 561 | assert(R.getNamingClass() && "performing access check without naming class"); |
| 562 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 563 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 564 | if (I.getAccess() != AS_public) { |
| 565 | AccessedEntity Entity(AccessedEntity::Member, |
| 566 | R.getNamingClass(), I.getAccess(), *I); |
| 567 | Entity.setDiag(diag::err_access); |
| 568 | |
| 569 | CheckAccess(*this, R.getNameLoc(), Entity); |
| 570 | } |
| 571 | } |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 572 | } |