access.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/cpp/access.h"
  5. namespace Carbon::Check {
  6. static auto CalculateEffectiveAccess(clang::DeclAccessPair access_pair)
  7. -> clang::AccessSpecifier {
  8. // Note that we use `.getAccess()` here, not `->getAccess()`, which is
  9. // equivalent to `.getDecl()->getAccess()`, because we want to consider the
  10. // lookup access and not the lexical access.
  11. switch (access_pair.getAccess()) {
  12. // Lookup access takes precedence.
  13. case clang::AS_public:
  14. case clang::AS_protected:
  15. case clang::AS_private:
  16. return access_pair.getAccess();
  17. case clang::AS_none:
  18. // No access specified meaning depends on the declaration. For non class
  19. // members, it means there's no access associated with this function so we
  20. // treat it as public. For class members it means we lost access along the
  21. // inheritance path, and the difference between `none` and `private` only
  22. // matters when the access check is performed within a friend or member of
  23. // the naming class. Because the naming class is a C++ class, and we don't
  24. // yet have a mechanism for a C++ class to befriend a Carbon class, we can
  25. // safely map `none` to `private` for now.
  26. return access_pair->isCXXClassMember() ? clang::AS_private
  27. : clang::AS_public;
  28. }
  29. }
  30. auto MapCppAccess(clang::DeclAccessPair access_pair) -> SemIR::AccessKind {
  31. switch (CalculateEffectiveAccess(access_pair)) {
  32. case clang::AS_public:
  33. return SemIR::AccessKind::Public;
  34. case clang::AS_protected:
  35. return SemIR::AccessKind::Protected;
  36. case clang::AS_private:
  37. return SemIR::AccessKind::Private;
  38. case clang::AS_none:
  39. CARBON_FATAL("Couldn't convert access");
  40. }
  41. }
  42. } // namespace Carbon::Check