keyword_modifier_set.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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, 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. // NOLINTNEXTLINE(performance-enum-size)
  22. enum RawEnumType : uint32_t {
  23. // At most one of these access modifiers allowed for a given declaration,
  24. // and if present it must be first:
  25. Private = 1 << 0,
  26. Protected = 1 << 1,
  27. // Extern is standalone.
  28. Extern = 1 << 2,
  29. // At most one of these declaration modifiers allowed for a given
  30. // declaration:
  31. Abstract = 1 << 3,
  32. Base = 1 << 4,
  33. Default = 1 << 5,
  34. Export = 1 << 6,
  35. Extend = 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 | ImplDecl | 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. // NOLINTNEXTLINE(google-explicit-constructor)
  55. constexpr KeywordModifierSet(RawEnumType set) : set_(set) {}
  56. // Adds entries to the set.
  57. auto Add(KeywordModifierSet set) -> void { set_ |= set.set_; }
  58. // Removes entries from the set.
  59. auto Remove(KeywordModifierSet set) -> void { set_ &= ~set.set_; }
  60. // Returns true if there's a non-empty set intersection.
  61. constexpr auto HasAnyOf(KeywordModifierSet other) const -> bool {
  62. return set_ & other.set_;
  63. }
  64. // Return a builder that returns the new enumeration type once a series of
  65. // mapping `Case`s and a final `Default` are provided. For example:
  66. // ```
  67. // auto e = set.ToEnum<SomeEnum>()
  68. // .Case(KeywordModifierSet::A, SomeEnum::A)
  69. // .Case(KeywordModifierSet::B, SomeEnum::B)
  70. // .Default(SomeEnum::DefaultValue);
  71. // ```
  72. template <typename T>
  73. auto ToEnum() const -> auto {
  74. class Converter {
  75. public:
  76. explicit Converter(const KeywordModifierSet& set) : set_(set) {}
  77. auto Case(RawEnumType raw_enumerator, T result) -> Converter& {
  78. if (set_.HasAnyOf(raw_enumerator)) {
  79. result_ = result;
  80. }
  81. return *this;
  82. }
  83. auto Default(T default_value) -> T {
  84. if (result_) {
  85. return *result_;
  86. }
  87. return default_value;
  88. }
  89. private:
  90. const KeywordModifierSet& set_;
  91. std::optional<T> result_;
  92. };
  93. return Converter(*this);
  94. }
  95. // Returns the access kind from modifiers.
  96. auto GetAccessKind() const -> SemIR::AccessKind {
  97. if (HasAnyOf(KeywordModifierSet::Protected)) {
  98. return SemIR::AccessKind::Protected;
  99. }
  100. if (HasAnyOf(KeywordModifierSet::Private)) {
  101. return SemIR::AccessKind::Private;
  102. }
  103. return SemIR::AccessKind::Public;
  104. }
  105. // Returns true if empty.
  106. constexpr auto empty() const -> bool { return !set_; }
  107. // Returns the set intersection.
  108. constexpr auto operator&(KeywordModifierSet other) const
  109. -> KeywordModifierSet {
  110. return set_ & other.set_;
  111. }
  112. // Returns the set inverse.
  113. auto operator~() const -> KeywordModifierSet { return ~set_; }
  114. private:
  115. RawEnumType set_;
  116. };
  117. static_assert(!KeywordModifierSet(KeywordModifierSet::Access)
  118. .HasAnyOf(KeywordModifierSet::Extern) &&
  119. !KeywordModifierSet(KeywordModifierSet::Access |
  120. KeywordModifierSet::Extern)
  121. .HasAnyOf(KeywordModifierSet::Decl),
  122. "Order-related sets must not overlap");
  123. static_assert(~KeywordModifierSet::None ==
  124. (KeywordModifierSet::Access | KeywordModifierSet::Extern |
  125. KeywordModifierSet::Decl),
  126. "Modifier missing from all modifier sets");
  127. } // namespace Carbon::Check
  128. #endif // CARBON_TOOLCHAIN_CHECK_KEYWORD_MODIFIER_SET_H_