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" |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
| 17 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
Anders Carlsson | 29f006b | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 20 | /// SetMemberAccessSpecifier - Set the access specifier of a member. |
| 21 | /// Returns true on error (when the previous member decl access specifier |
| 22 | /// is different from the new member decl access specifier). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 23 | bool Sema::SetMemberAccessSpecifier(NamedDecl *MemberDecl, |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 24 | NamedDecl *PrevMemberDecl, |
| 25 | AccessSpecifier LexicalAS) { |
| 26 | if (!PrevMemberDecl) { |
| 27 | // Use the lexical access specifier. |
| 28 | MemberDecl->setAccess(LexicalAS); |
| 29 | return false; |
| 30 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 31 | |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 32 | // C++ [class.access.spec]p3: When a member is redeclared its access |
| 33 | // specifier must be same as its initial declaration. |
| 34 | if (LexicalAS != AS_none && LexicalAS != PrevMemberDecl->getAccess()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | Diag(MemberDecl->getLocation(), |
| 36 | diag::err_class_redeclared_with_different_access) |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 37 | << MemberDecl << LexicalAS; |
| 38 | Diag(PrevMemberDecl->getLocation(), diag::note_previous_access_declaration) |
| 39 | << PrevMemberDecl << PrevMemberDecl->getAccess(); |
| 40 | return true; |
| 41 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Anders Carlsson | c60e888 | 2009-03-27 04:54:36 +0000 | [diff] [blame] | 43 | MemberDecl->setAccess(PrevMemberDecl->getAccess()); |
| 44 | return false; |
| 45 | } |
Anders Carlsson | 29f006b | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 46 | |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 47 | /// Find a class on the derivation path between Derived and Base that is |
| 48 | /// inaccessible. If @p NoPrivileges is true, special access rights (members |
| 49 | /// and friends) are not considered. |
| 50 | const CXXBaseSpecifier *Sema::FindInaccessibleBase( |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 51 | QualType Derived, QualType Base, CXXBasePaths &Paths, bool NoPrivileges) { |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 52 | Base = Context.getCanonicalType(Base).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | assert(!Paths.isAmbiguous(Base) && |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 54 | "Can't check base class access if set of paths is ambiguous"); |
| 55 | assert(Paths.isRecordingPaths() && |
| 56 | "Can't check base class access without recorded paths"); |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 57 | |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 58 | |
| 59 | const CXXBaseSpecifier *InaccessibleBase = 0; |
| 60 | |
| 61 | const CXXRecordDecl *CurrentClassDecl = 0; |
Anders Carlsson | f8080a3 | 2009-03-27 19:01:12 +0000 | [diff] [blame] | 62 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(getCurFunctionDecl())) |
| 63 | CurrentClassDecl = MD->getParent(); |
| 64 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 65 | for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathsEnd = Paths.end(); |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 66 | Path != PathsEnd; ++Path) { |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 67 | |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 68 | bool FoundInaccessibleBase = false; |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 70 | for (CXXBasePath::const_iterator Element = Path->begin(), |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 71 | ElementEnd = Path->end(); Element != ElementEnd; ++Element) { |
| 72 | const CXXBaseSpecifier *Base = Element->Base; |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 73 | |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 74 | switch (Base->getAccessSpecifier()) { |
| 75 | default: |
| 76 | assert(0 && "invalid access specifier"); |
| 77 | case AS_public: |
| 78 | // Nothing to do. |
| 79 | break; |
| 80 | case AS_private: |
Anders Carlsson | f8080a3 | 2009-03-27 19:01:12 +0000 | [diff] [blame] | 81 | // FIXME: Check if the current function/class is a friend. |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 82 | if (NoPrivileges || CurrentClassDecl != Element->Class) |
Anders Carlsson | f8080a3 | 2009-03-27 19:01:12 +0000 | [diff] [blame] | 83 | FoundInaccessibleBase = true; |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 84 | break; |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 85 | case AS_protected: |
Anders Carlsson | 14734f7 | 2009-03-28 04:17:27 +0000 | [diff] [blame] | 86 | // FIXME: Implement |
| 87 | break; |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 88 | } |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 89 | |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 90 | if (FoundInaccessibleBase) { |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 91 | InaccessibleBase = Base; |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 92 | break; |
| 93 | } |
| 94 | } |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 95 | |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 96 | if (!FoundInaccessibleBase) { |
| 97 | // We found a path to the base, our work here is done. |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 98 | return 0; |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 102 | assert(InaccessibleBase && "no path found, but no inaccessible base"); |
| 103 | return InaccessibleBase; |
| 104 | } |
| 105 | |
| 106 | /// CheckBaseClassAccess - Check that a derived class can access its base class |
| 107 | /// and report an error if it can't. [class.access.base] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | bool Sema::CheckBaseClassAccess(QualType Derived, QualType Base, |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 109 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 110 | CXXBasePaths &Paths, SourceLocation AccessLoc, |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 111 | DeclarationName Name) { |
| 112 | |
| 113 | if (!getLangOptions().AccessControl) |
| 114 | return false; |
| 115 | const CXXBaseSpecifier *InaccessibleBase = FindInaccessibleBase( |
| 116 | Derived, Base, Paths); |
| 117 | |
| 118 | if (InaccessibleBase) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | Diag(AccessLoc, InaccessibleBaseID) |
Anders Carlsson | d8f9cb0 | 2009-05-13 21:11:42 +0000 | [diff] [blame] | 120 | << Derived << Base << Name; |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 121 | |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 122 | AccessSpecifier AS = InaccessibleBase->getAccessSpecifierAsWritten(); |
| 123 | |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 124 | // If there's no written access specifier, then the inheritance specifier |
| 125 | // is implicitly private. |
| 126 | if (AS == AS_none) |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 127 | Diag(InaccessibleBase->getSourceRange().getBegin(), |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 128 | diag::note_inheritance_implicitly_private_here); |
| 129 | else |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 130 | Diag(InaccessibleBase->getSourceRange().getBegin(), |
Anders Carlsson | c4f1e87 | 2009-03-27 06:03:27 +0000 | [diff] [blame] | 131 | diag::note_inheritance_specifier_here) << AS; |
| 132 | |
| 133 | return true; |
| 134 | } |
Sebastian Redl | 726212f | 2009-07-18 14:32:15 +0000 | [diff] [blame] | 135 | |
Anders Carlsson | 29f006b | 2009-03-27 05:05:05 +0000 | [diff] [blame] | 136 | return false; |
| 137 | } |