blob: 31d8a3f5ab0750075d432b38535df1e0c5f781d1 [file] [log] [blame]
Anders Carlsson29f006b2009-03-27 05:05:05 +00001//===---- SemaAccess.cpp - C++ Access Control -------------------*- C++ -*-===//
Anders Carlsson60d6b0d2009-03-27 04:43:36 +00002//
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 Carlssonc60e8882009-03-27 04:54:36 +000013
Anders Carlssonc4f1e872009-03-27 06:03:27 +000014#include "SemaInherit.h"
Anders Carlssonc60e8882009-03-27 04:54:36 +000015#include "Sema.h"
Anders Carlssonc4f1e872009-03-27 06:03:27 +000016#include "clang/AST/ASTContext.h"
Anders Carlssonc60e8882009-03-27 04:54:36 +000017using namespace clang;
18
Anders Carlsson29f006b2009-03-27 05:05:05 +000019/// SetMemberAccessSpecifier - Set the access specifier of a member.
20/// Returns true on error (when the previous member decl access specifier
21/// is different from the new member decl access specifier).
Anders Carlssonc60e8882009-03-27 04:54:36 +000022bool Sema::SetMemberAccessSpecifier(NamedDecl *MemberDecl,
23 NamedDecl *PrevMemberDecl,
24 AccessSpecifier LexicalAS) {
25 if (!PrevMemberDecl) {
26 // Use the lexical access specifier.
27 MemberDecl->setAccess(LexicalAS);
28 return false;
29 }
30
31 // C++ [class.access.spec]p3: When a member is redeclared its access
32 // specifier must be same as its initial declaration.
33 if (LexicalAS != AS_none && LexicalAS != PrevMemberDecl->getAccess()) {
34 Diag(MemberDecl->getLocation(),
35 diag::err_class_redeclared_with_different_access)
36 << MemberDecl << LexicalAS;
37 Diag(PrevMemberDecl->getLocation(), diag::note_previous_access_declaration)
38 << PrevMemberDecl << PrevMemberDecl->getAccess();
39 return true;
40 }
41
42 MemberDecl->setAccess(PrevMemberDecl->getAccess());
43 return false;
44}
Anders Carlsson29f006b2009-03-27 05:05:05 +000045
Sebastian Redl726212f2009-07-18 14:32:15 +000046/// Find a class on the derivation path between Derived and Base that is
47/// inaccessible. If @p NoPrivileges is true, special access rights (members
48/// and friends) are not considered.
49const CXXBaseSpecifier *Sema::FindInaccessibleBase(
50 QualType Derived, QualType Base, BasePaths &Paths, bool NoPrivileges)
51{
Anders Carlssonc4f1e872009-03-27 06:03:27 +000052 Base = Context.getCanonicalType(Base).getUnqualifiedType();
53 assert(!Paths.isAmbiguous(Base) &&
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 Carlssonc4f1e872009-03-27 06:03:27 +000057
Sebastian Redl726212f2009-07-18 14:32:15 +000058
59 const CXXBaseSpecifier *InaccessibleBase = 0;
60
61 const CXXRecordDecl *CurrentClassDecl = 0;
Anders Carlssonf8080a32009-03-27 19:01:12 +000062 if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(getCurFunctionDecl()))
63 CurrentClassDecl = MD->getParent();
64
Sebastian Redl726212f2009-07-18 14:32:15 +000065 for (BasePaths::paths_iterator Path = Paths.begin(), PathsEnd = Paths.end();
Anders Carlssonc4f1e872009-03-27 06:03:27 +000066 Path != PathsEnd; ++Path) {
Sebastian Redl726212f2009-07-18 14:32:15 +000067
Anders Carlssonc4f1e872009-03-27 06:03:27 +000068 bool FoundInaccessibleBase = false;
Sebastian Redl726212f2009-07-18 14:32:15 +000069
70 for (BasePath::const_iterator Element = Path->begin(),
Anders Carlssonc4f1e872009-03-27 06:03:27 +000071 ElementEnd = Path->end(); Element != ElementEnd; ++Element) {
72 const CXXBaseSpecifier *Base = Element->Base;
Sebastian Redl726212f2009-07-18 14:32:15 +000073
Anders Carlssonc4f1e872009-03-27 06:03:27 +000074 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 Carlssonf8080a32009-03-27 19:01:12 +000081 // FIXME: Check if the current function/class is a friend.
Sebastian Redl726212f2009-07-18 14:32:15 +000082 if (NoPrivileges || CurrentClassDecl != Element->Class)
Anders Carlssonf8080a32009-03-27 19:01:12 +000083 FoundInaccessibleBase = true;
Anders Carlssonc4f1e872009-03-27 06:03:27 +000084 break;
Sebastian Redl726212f2009-07-18 14:32:15 +000085 case AS_protected:
Anders Carlsson14734f72009-03-28 04:17:27 +000086 // FIXME: Implement
87 break;
Anders Carlssonc4f1e872009-03-27 06:03:27 +000088 }
Sebastian Redl726212f2009-07-18 14:32:15 +000089
Anders Carlssonc4f1e872009-03-27 06:03:27 +000090 if (FoundInaccessibleBase) {
Sebastian Redl726212f2009-07-18 14:32:15 +000091 InaccessibleBase = Base;
Anders Carlssonc4f1e872009-03-27 06:03:27 +000092 break;
93 }
94 }
Sebastian Redl726212f2009-07-18 14:32:15 +000095
Anders Carlssonc4f1e872009-03-27 06:03:27 +000096 if (!FoundInaccessibleBase) {
97 // We found a path to the base, our work here is done.
Sebastian Redl726212f2009-07-18 14:32:15 +000098 return 0;
Anders Carlssonc4f1e872009-03-27 06:03:27 +000099 }
100 }
101
Sebastian Redl726212f2009-07-18 14:32:15 +0000102 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]
108bool Sema::CheckBaseClassAccess(QualType Derived, QualType Base,
109 unsigned InaccessibleBaseID,
110 BasePaths &Paths, SourceLocation AccessLoc,
111 DeclarationName Name) {
112
113 if (!getLangOptions().AccessControl)
114 return false;
115 const CXXBaseSpecifier *InaccessibleBase = FindInaccessibleBase(
116 Derived, Base, Paths);
117
118 if (InaccessibleBase) {
Anders Carlssond8f9cb02009-05-13 21:11:42 +0000119 Diag(AccessLoc, InaccessibleBaseID)
120 << Derived << Base << Name;
Anders Carlssonc4f1e872009-03-27 06:03:27 +0000121
Sebastian Redl726212f2009-07-18 14:32:15 +0000122 AccessSpecifier AS = InaccessibleBase->getAccessSpecifierAsWritten();
123
Anders Carlssonc4f1e872009-03-27 06:03:27 +0000124 // If there's no written access specifier, then the inheritance specifier
125 // is implicitly private.
126 if (AS == AS_none)
Sebastian Redl726212f2009-07-18 14:32:15 +0000127 Diag(InaccessibleBase->getSourceRange().getBegin(),
Anders Carlssonc4f1e872009-03-27 06:03:27 +0000128 diag::note_inheritance_implicitly_private_here);
129 else
Sebastian Redl726212f2009-07-18 14:32:15 +0000130 Diag(InaccessibleBase->getSourceRange().getBegin(),
Anders Carlssonc4f1e872009-03-27 06:03:27 +0000131 diag::note_inheritance_specifier_here) << AS;
132
133 return true;
134 }
Sebastian Redl726212f2009-07-18 14:32:15 +0000135
Anders Carlsson29f006b2009-03-27 05:05:05 +0000136 return false;
137}