keyword_modifier_set.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #ifndef CARBON_TOOLCHAIN_CHECK_KEYWORD_MODIFIER_SET_H_
  5. #define CARBON_TOOLCHAIN_CHECK_KEYWORD_MODIFIER_SET_H_
  6. #include <optional>
  7. #include "llvm/ADT/BitmaskEnum.h"
  8. #include "toolchain/sem_ir/name_scope.h"
  9. namespace Carbon::Check {
  10. LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
  11. // The order of modifiers. Each of these corresponds to a group on
  12. // KeywordModifierSet, and can be used as an array index.
  13. enum class ModifierOrder : int8_t { Access, Extern, Extend, Decl, Last = Decl };
  14. // Represents a set of keyword modifiers, using a separate bit per modifier.
  15. class KeywordModifierSet {
  16. public:
  17. // Provide values as an enum. This doesn't expose these as KeywordModifierSet
  18. // instances just due to the duplication of declarations that would cause.
  19. //
  20. // We expect this to grow, so are using a bigger size than needed.
  21. enum RawEnumType : uint32_t {
  22. // At most one of these access modifiers allowed for a given declaration,
  23. // and if present it must be first:
  24. Private = 1 << 0,
  25. Protected = 1 << 1,
  26. // Extern is standalone.
  27. Extern = 1 << 2,
  28. // Extend can be combined with Final, but no others in the group below.
  29. Extend = 1 << 3,
  30. // At most one of these declaration modifiers allowed for a given
  31. // declaration:
  32. Abstract = 1 << 4,
  33. Base = 1 << 5,
  34. Default = 1 << 6,
  35. Export = 1 << 7,
  36. Final = 1 << 8,
  37. Impl = 1 << 9,
  38. Virtual = 1 << 10,
  39. Returned = 1 << 11,
  40. // Sets of modifiers:
  41. Access = Private | Protected,
  42. Class = Abstract | Base,
  43. Method = Abstract | Impl | Virtual,
  44. ImplDecl = Extend | Final,
  45. Interface = Default | Final,
  46. Decl = Class | Method | Interface | Export | Returned,
  47. None = 0,
  48. LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/Returned)
  49. };
  50. // Default construct to empty.
  51. explicit KeywordModifierSet() : set_(None) {}
  52. // Support implicit conversion so that the difference with the member enum is
  53. // opaque.
  54. explicit(false) constexpr KeywordModifierSet(RawEnumType set) : set_(set) {}
  55. // Adds entries to the set.
  56. auto Add(KeywordModifierSet set) -> void { set_ |= set.set_; }
  57. // Removes entries from the set.
  58. auto Remove(KeywordModifierSet set) -> void { set_ &= ~set.set_; }
  59. // Returns true if there's a non-empty set intersection.
  60. constexpr auto HasAnyOf(KeywordModifierSet other) const -> bool {
  61. return set_ & other.set_;
  62. }
  63. // Return a builder that returns the new enumeration type once a series of
  64. // mapping `Case`s and a final `Default` are provided. For example:
  65. // ```
  66. // auto e = set.ToEnum<SomeEnum>()
  67. // .Case(KeywordModifierSet::A, SomeEnum::A)
  68. // .Case(KeywordModifierSet::B, SomeEnum::B)
  69. // .Default(SomeEnum::DefaultValue);
  70. // ```
  71. template <typename T>
  72. auto ToEnum() const -> auto {
  73. class Converter {
  74. public:
  75. explicit Converter(const KeywordModifierSet& set) : set_(set) {}
  76. auto Case(RawEnumType raw_enumerator, T result) -> Converter& {
  77. if (set_.HasAnyOf(raw_enumerator)) {
  78. result_ = result;
  79. }
  80. return *this;
  81. }
  82. auto Default(T default_value) -> T {
  83. if (result_) {
  84. return *result_;
  85. }
  86. return default_value;
  87. }
  88. private:
  89. const KeywordModifierSet& set_;
  90. std::optional<T> result_;
  91. };
  92. return Converter(*this);
  93. }
  94. // Returns the access kind from modifiers.
  95. auto GetAccessKind() const -> SemIR::AccessKind {
  96. if (HasAnyOf(KeywordModifierSet::Protected)) {
  97. return SemIR::AccessKind::Protected;
  98. }
  99. if (HasAnyOf(KeywordModifierSet::Private)) {
  100. return SemIR::AccessKind::Private;
  101. }
  102. return SemIR::AccessKind::Public;
  103. }
  104. // Returns true if empty.
  105. constexpr auto empty() const -> bool { return !set_; }
  106. // Returns the set intersection.
  107. constexpr auto operator&(KeywordModifierSet other) const
  108. -> KeywordModifierSet {
  109. return set_ & other.set_;
  110. }
  111. // Returns the set inverse.
  112. auto operator~() const -> KeywordModifierSet { return ~set_; }
  113. private:
  114. RawEnumType set_;
  115. };
  116. static_assert(!KeywordModifierSet(KeywordModifierSet::Access)
  117. .HasAnyOf(KeywordModifierSet::Extern) &&
  118. !KeywordModifierSet(KeywordModifierSet::Access |
  119. KeywordModifierSet::Extern |
  120. KeywordModifierSet::Extend)
  121. .HasAnyOf(KeywordModifierSet::Decl),
  122. "Order-related sets must not overlap");
  123. static_assert(~KeywordModifierSet::None ==
  124. (KeywordModifierSet::Access | KeywordModifierSet::Extern |
  125. KeywordModifierSet::Extend | KeywordModifierSet::Decl),
  126. "Modifier missing from all modifier sets");
  127. } // namespace Carbon::Check
  128. #endif // CARBON_TOOLCHAIN_CHECK_KEYWORD_MODIFIER_SET_H_