kind_switch.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_BASE_KIND_SWITCH_H_
  5. #define CARBON_TOOLCHAIN_BASE_KIND_SWITCH_H_
  6. #include "llvm/ADT/STLExtras.h"
  7. // This library provides switch-like behaviors for Carbon's kind-based types.
  8. //
  9. // An expected use case is to mix regular switch `case` statements and
  10. // `CARBON_KIND`. However, the `switch` must be defined using
  11. // `CARBON_KIND_SWITCH`. For example:
  12. //
  13. // CARBON_KIND_SWITCH(untyped_inst) {
  14. // case CARBON_KIND(SomeInstType inst): {
  15. // return inst.typed_field;
  16. // }
  17. // case OtherType1::Kind:
  18. // case OtherType2::Kind:
  19. // return value;
  20. // default:
  21. // return default_value;
  22. // }
  23. //
  24. // For compatibility, this requires:
  25. //
  26. // - The type passed to `CARBON_KIND_SWITCH` has `.kind()` to switch on, and
  27. // `.As<CaseT>` for `CARBON_KIND` to cast to.
  28. // - Each type passed to `CARBON_KIND` (`CaseT` above) provides
  29. // `CaseT::Kind`, which is passed to the `case` keyword.
  30. // `CaseT::Kind::RawEnumType` is the type returned by `.kind()`.
  31. //
  32. // Note, this is currently used primarily for Inst in toolchain. When more
  33. // use-cases are added, it would be worth considering whether the API
  34. // requirements should change.
  35. namespace Carbon::Internal::Kind {
  36. // Given `CARBON_KIND_SWITCH(value)` this handles calling `value.kind()`.
  37. template <typename T>
  38. auto SwitchOn(T&& switch_value) -> auto {
  39. return switch_value.kind();
  40. }
  41. // Given `CARBON_KIND(CaseT name)` this generates `CaseT::Kind`. It explicitly
  42. // returns `KindT` because that may differ from `CaseT::Kind`, and may not be
  43. // copyable.
  44. template <typename FnT>
  45. consteval auto ForCase() -> auto {
  46. using ArgT = llvm::function_traits<FnT>::template arg_t<0>;
  47. return static_cast<decltype(ArgT::Kind)::RawEnumType>(ArgT::Kind);
  48. }
  49. // Given `CARBON_KIND_SWITCH(value)` and `CARBON_KIND(CaseT name)` this
  50. // generates `value.As<CaseT>()`.
  51. template <typename FnT, typename ValueT>
  52. auto Cast(ValueT&& kind_switch_value) -> auto {
  53. using CaseT = llvm::function_traits<FnT>::template arg_t<0>;
  54. return kind_switch_value.template As<CaseT>();
  55. }
  56. #define CARBON_INTERNAL_KIND_MERGE(Prefix, Line) Prefix##Line
  57. #define CARBON_INTERNAL_KIND_LABEL(Line) \
  58. CARBON_INTERNAL_KIND_MERGE(carbon_internal_kind_case_, Line)
  59. } // namespace Carbon::Internal::Kind
  60. // Produces a switch statement on value.kind().
  61. #define CARBON_KIND_SWITCH(value) \
  62. switch ( \
  63. const auto& carbon_internal_kind_switch_value = value; \
  64. ::Carbon::Internal::Kind::SwitchOn(carbon_internal_kind_switch_value))
  65. // Produces a case-compatible block of code that also instantiates a local typed
  66. // variable. typed_variable_decl looks like `int i`, with a space.
  67. //
  68. // This uses `if` to scope the variable, and provides a dangling `else` in order
  69. // to prevent accidental `else` use. The label allows `:` to follow the macro
  70. // name, making it look more like a typical `case`.
  71. #define CARBON_KIND(typed_variable_decl) \
  72. ::Carbon::Internal::Kind::ForCase< \
  73. decltype([]([[maybe_unused]] typed_variable_decl) {})>() \
  74. : if (typed_variable_decl = ::Carbon::Internal::Kind::Cast< \
  75. decltype([]([[maybe_unused]] typed_variable_decl) {})>( \
  76. carbon_internal_kind_switch_value); \
  77. false) {} \
  78. else [[maybe_unused]] CARBON_INTERNAL_KIND_LABEL(__LINE__)
  79. #endif // CARBON_TOOLCHAIN_BASE_KIND_SWITCH_H_