blob: 85e1c2ed883b497464ca47fcf7125dc53c173378 [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
46/// CheckBaseClassAccess - Check that a derived class can access its base class
47/// and report an error if it can't. [class.access.base]
48bool Sema::CheckBaseClassAccess(QualType Derived, QualType Base,
49 BasePaths& Paths, SourceLocation AccessLoc) {
Anders Carlssonc4f1e872009-03-27 06:03:27 +000050 Base = Context.getCanonicalType(Base).getUnqualifiedType();
51 assert(!Paths.isAmbiguous(Base) &&
52 "Can't check base class access if set of paths is ambiguous");
53 assert(Paths.isRecordingPaths() &&
54 "Can't check base class access without recorded paths");
55
56 const CXXBaseSpecifier *InacessibleBase = 0;
57
58 for (BasePaths::paths_iterator Path = Paths.begin(), PathsEnd = Paths.end();
59 Path != PathsEnd; ++Path) {
60
61 bool FoundInaccessibleBase = false;
62
63 for (BasePath::const_iterator Element = Path->begin(),
64 ElementEnd = Path->end(); Element != ElementEnd; ++Element) {
65 const CXXBaseSpecifier *Base = Element->Base;
66
67 switch (Base->getAccessSpecifier()) {
68 default:
69 assert(0 && "invalid access specifier");
70 case AS_public:
71 // Nothing to do.
72 break;
73 case AS_private:
74 // FIXME: Check if the current function is a member or friend.
75 FoundInaccessibleBase = true;
76 break;
77 case AS_protected:
78 // FIXME: Implement
79 break;
80 }
81
82 if (FoundInaccessibleBase) {
83 InacessibleBase = Base;
84 break;
85 }
86 }
87
88 if (!FoundInaccessibleBase) {
89 // We found a path to the base, our work here is done.
90 InacessibleBase = 0;
91 break;
92 }
93 }
94
95 if (InacessibleBase) {
96 Diag(AccessLoc, diag::err_conv_to_inaccessible_base)
97 << Derived << Base;
98
99 AccessSpecifier AS = InacessibleBase->getAccessSpecifierAsWritten();
100
101 // If there's no written access specifier, then the inheritance specifier
102 // is implicitly private.
103 if (AS == AS_none)
104 Diag(InacessibleBase->getSourceRange().getBegin(),
105 diag::note_inheritance_implicitly_private_here);
106 else
107 Diag(InacessibleBase->getSourceRange().getBegin(),
108 diag::note_inheritance_specifier_here) << AS;
109
110 return true;
111 }
112
Anders Carlsson29f006b2009-03-27 05:05:05 +0000113 return false;
114}