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