| 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" |
| Anders Carlsson | 1794112 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
| Anders Carlsson | 4742a9c | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 21 | /// SetMemberAccessSpecifier - Set the access specifier of a member. |
| 22 | /// Returns true on error (when the previous member decl access specifier |
| 23 | /// is different from the new member decl access specifier). |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 24 | bool Sema::SetMemberAccessSpecifier(NamedDecl *MemberDecl, |
| Anders Carlsson | 1794112 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 25 | NamedDecl *PrevMemberDecl, |
| 26 | AccessSpecifier LexicalAS) { |
| 27 | if (!PrevMemberDecl) { |
| 28 | // Use the lexical access specifier. |
| 29 | MemberDecl->setAccess(LexicalAS); |
| 30 | return false; |
| 31 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | |
| Anders Carlsson | 1794112 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 33 | // C++ [class.access.spec]p3: When a member is redeclared its access |
| 34 | // specifier must be same as its initial declaration. |
| 35 | if (LexicalAS != AS_none && LexicalAS != PrevMemberDecl->getAccess()) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | Diag(MemberDecl->getLocation(), |
| 37 | diag::err_class_redeclared_with_different_access) |
| Anders Carlsson | 1794112 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 38 | << MemberDecl << LexicalAS; |
| 39 | Diag(PrevMemberDecl->getLocation(), diag::note_previous_access_declaration) |
| 40 | << PrevMemberDecl << PrevMemberDecl->getAccess(); |
| John McCall | 0a4bb26 | 2009-12-23 00:37:40 +0000 | [diff] [blame] | 41 | |
| 42 | MemberDecl->setAccess(LexicalAS); |
| Anders Carlsson | 1794112 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 43 | return true; |
| 44 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
| Anders Carlsson | 1794112 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 46 | MemberDecl->setAccess(PrevMemberDecl->getAccess()); |
| 47 | return false; |
| 48 | } |
| Anders Carlsson | 4742a9c | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 49 | |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 50 | /// Find a class on the derivation path between Derived and Base that is |
| 51 | /// inaccessible. If @p NoPrivileges is true, special access rights (members |
| 52 | /// and friends) are not considered. |
| 53 | const CXXBaseSpecifier *Sema::FindInaccessibleBase( |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 54 | QualType Derived, QualType Base, CXXBasePaths &Paths, bool NoPrivileges) { |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 55 | Base = Context.getCanonicalType(Base).getUnqualifiedType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | assert(!Paths.isAmbiguous(Base) && |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 57 | "Can't check base class access if set of paths is ambiguous"); |
| 58 | assert(Paths.isRecordingPaths() && |
| 59 | "Can't check base class access without recorded paths"); |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 60 | |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 61 | |
| 62 | const CXXBaseSpecifier *InaccessibleBase = 0; |
| 63 | |
| 64 | const CXXRecordDecl *CurrentClassDecl = 0; |
| Anders Carlsson | af06b97 | 2009-03-27 19:01:12 +0000 | [diff] [blame] | 65 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(getCurFunctionDecl())) |
| 66 | CurrentClassDecl = MD->getParent(); |
| 67 | |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 68 | for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathsEnd = Paths.end(); |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 69 | Path != PathsEnd; ++Path) { |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 70 | |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 71 | bool FoundInaccessibleBase = false; |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 72 | |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 73 | for (CXXBasePath::const_iterator Element = Path->begin(), |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 74 | ElementEnd = Path->end(); Element != ElementEnd; ++Element) { |
| 75 | const CXXBaseSpecifier *Base = Element->Base; |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 76 | |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 77 | switch (Base->getAccessSpecifier()) { |
| 78 | default: |
| 79 | assert(0 && "invalid access specifier"); |
| 80 | case AS_public: |
| 81 | // Nothing to do. |
| 82 | break; |
| 83 | case AS_private: |
| Anders Carlsson | af06b97 | 2009-03-27 19:01:12 +0000 | [diff] [blame] | 84 | // FIXME: Check if the current function/class is a friend. |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 85 | if (NoPrivileges || CurrentClassDecl != Element->Class) |
| Anders Carlsson | af06b97 | 2009-03-27 19:01:12 +0000 | [diff] [blame] | 86 | FoundInaccessibleBase = true; |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 87 | break; |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 88 | case AS_protected: |
| Anders Carlsson | 72f307a | 2009-03-28 04:17:27 +0000 | [diff] [blame] | 89 | // FIXME: Implement |
| 90 | break; |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 91 | } |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 92 | |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 93 | if (FoundInaccessibleBase) { |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 94 | InaccessibleBase = Base; |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 95 | break; |
| 96 | } |
| 97 | } |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 98 | |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 99 | if (!FoundInaccessibleBase) { |
| 100 | // We found a path to the base, our work here is done. |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 101 | return 0; |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 105 | assert(InaccessibleBase && "no path found, but no inaccessible base"); |
| 106 | return InaccessibleBase; |
| 107 | } |
| 108 | |
| 109 | /// CheckBaseClassAccess - Check that a derived class can access its base class |
| 110 | /// and report an error if it can't. [class.access.base] |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | bool Sema::CheckBaseClassAccess(QualType Derived, QualType Base, |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 112 | unsigned InaccessibleBaseID, |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 113 | CXXBasePaths &Paths, SourceLocation AccessLoc, |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 114 | DeclarationName Name) { |
| 115 | |
| 116 | if (!getLangOptions().AccessControl) |
| 117 | return false; |
| 118 | const CXXBaseSpecifier *InaccessibleBase = FindInaccessibleBase( |
| 119 | Derived, Base, Paths); |
| 120 | |
| 121 | if (InaccessibleBase) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | Diag(AccessLoc, InaccessibleBaseID) |
| Anders Carlsson | 49d216d | 2009-05-13 21:11:42 +0000 | [diff] [blame] | 123 | << Derived << Base << Name; |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 124 | |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 125 | AccessSpecifier AS = InaccessibleBase->getAccessSpecifierAsWritten(); |
| 126 | |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 127 | // If there's no written access specifier, then the inheritance specifier |
| 128 | // is implicitly private. |
| 129 | if (AS == AS_none) |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 130 | Diag(InaccessibleBase->getSourceRange().getBegin(), |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 131 | diag::note_inheritance_implicitly_private_here); |
| 132 | else |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 133 | Diag(InaccessibleBase->getSourceRange().getBegin(), |
| Anders Carlsson | 733d77f | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 134 | diag::note_inheritance_specifier_here) << AS; |
| 135 | |
| 136 | return true; |
| 137 | } |
| Sebastian Redl | e644e19 | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 138 | |
| Anders Carlsson | 4742a9c | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 139 | return false; |
| 140 | } |
| John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame^] | 141 | |
| 142 | /// Diagnose the path which caused the given declaration to become |
| 143 | /// inaccessible. |
| 144 | static void DiagnoseAccessPath(Sema &S, const LookupResult &R, NamedDecl *D, |
| 145 | AccessSpecifier Access) { |
| 146 | // Easy case: the decl's natural access determined its path access. |
| 147 | if (Access == D->getAccess() || D->getAccess() == AS_private) { |
| 148 | S.Diag(D->getLocation(), diag::note_access_natural) |
| 149 | << (unsigned) (Access == AS_protected); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | // TODO: flesh this out |
| 154 | S.Diag(D->getLocation(), diag::note_access_constrained_by_path) |
| 155 | << (unsigned) (Access == AS_protected); |
| 156 | } |
| 157 | |
| 158 | /// Checks access to the given declaration in the current context. |
| 159 | /// |
| 160 | /// \param R the means via which the access was made; must have a naming |
| 161 | /// class set |
| 162 | /// \param D the declaration accessed |
| 163 | /// \param Access the best access along any inheritance path from the |
| 164 | /// naming class to the declaration. AS_none means the path is impossible |
| 165 | bool Sema::CheckAccess(const LookupResult &R, NamedDecl *D, |
| 166 | AccessSpecifier Access) { |
| 167 | assert(R.getNamingClass() && "performing access check without naming class"); |
| 168 | |
| 169 | // If the access path is public, it's accessible everywhere. |
| 170 | if (Access == AS_public) |
| 171 | return false; |
| 172 | |
| 173 | // Otherwise, derive the current class context. |
| 174 | DeclContext *DC = CurContext; |
| 175 | while (isa<CXXRecordDecl>(DC) && |
| 176 | cast<CXXRecordDecl>(DC)->isAnonymousStructOrUnion()) |
| 177 | DC = DC->getParent(); |
| 178 | |
| 179 | CXXRecordDecl *CurRecord; |
| 180 | if (isa<CXXRecordDecl>(DC)) |
| 181 | CurRecord = cast<CXXRecordDecl>(DC); |
| 182 | else if (isa<CXXMethodDecl>(DC)) |
| 183 | CurRecord = cast<CXXMethodDecl>(DC)->getParent(); |
| 184 | else { |
| 185 | Diag(R.getNameLoc(), diag::err_access_outside_class) |
| 186 | << (Access == AS_protected); |
| 187 | DiagnoseAccessPath(*this, R, D, Access); |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | CXXRecordDecl *NamingClass = R.getNamingClass(); |
| 192 | while (NamingClass->isAnonymousStructOrUnion()) |
| 193 | // This should be guaranteed by the fact that the decl has |
| 194 | // non-public access. If not, we should make it guaranteed! |
| 195 | NamingClass = cast<CXXRecordDecl>(NamingClass); |
| 196 | |
| 197 | // White-list accesses from within the declaring class. |
| 198 | if (Access != AS_none && |
| 199 | CurRecord->getCanonicalDecl() == NamingClass->getCanonicalDecl()) |
| 200 | return false; |
| 201 | |
| 202 | // Protected access. |
| 203 | if (Access == AS_protected) { |
| 204 | // FIXME: implement [class.protected]p1 |
| 205 | if (CurRecord->isDerivedFrom(NamingClass)) |
| 206 | return false; |
| 207 | |
| 208 | // FIXME: dependent classes |
| 209 | } |
| 210 | |
| 211 | // FIXME: friends |
| 212 | |
| 213 | // Okay, it's a bad access, reject it. |
| 214 | |
| 215 | |
| 216 | CXXRecordDecl *DeclaringClass = cast<CXXRecordDecl>(D->getDeclContext()); |
| 217 | |
| 218 | if (Access == AS_protected) { |
| 219 | Diag(R.getNameLoc(), diag::err_access_protected) |
| 220 | << Context.getTypeDeclType(DeclaringClass) |
| 221 | << Context.getTypeDeclType(CurRecord); |
| 222 | DiagnoseAccessPath(*this, R, D, Access); |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | assert(Access == AS_private || Access == AS_none); |
| 227 | Diag(R.getNameLoc(), diag::err_access_private) |
| 228 | << Context.getTypeDeclType(DeclaringClass) |
| 229 | << Context.getTypeDeclType(CurRecord); |
| 230 | DiagnoseAccessPath(*this, R, D, Access); |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | /// Checks access to all the declarations in the given result set. |
| 235 | void Sema::CheckAccess(const LookupResult &R) { |
| 236 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 237 | CheckAccess(R, *I, I.getAccess()); |
| 238 | } |