decompose.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. //
  5. // Utilities for types that support the `Decompose` API.
  6. #ifndef CARBON_EXPLORER_BASE_DECOMPOSE_H_
  7. #define CARBON_EXPLORER_BASE_DECOMPOSE_H_
  8. #include "llvm/ADT/ArrayRef.h"
  9. #include "llvm/ADT/Hashing.h"
  10. namespace Carbon {
  11. // CRTP base class which extends `Base` to support hashing and equality
  12. // comparison. `Base` must support the `Decompose` API.
  13. template <typename Base>
  14. class HashFromDecompose {
  15. public:
  16. friend auto operator==(const HashFromDecompose& lhs,
  17. const HashFromDecompose& rhs) -> bool {
  18. return static_cast<const Base*>(&lhs)->Decompose(
  19. [&](auto&&... lhs_elements) {
  20. return static_cast<const Base*>(&rhs)->Decompose(
  21. [&](auto&&... rhs_elements) {
  22. return ((lhs_elements == rhs_elements) && ...);
  23. });
  24. });
  25. }
  26. friend auto hash_value(const HashFromDecompose& self) -> llvm::hash_code {
  27. return static_cast<const Base*>(&self)->Decompose(
  28. [&](auto&&... lhs_elements) {
  29. return llvm::hash_combine(WrapForHash(lhs_elements)...);
  30. });
  31. }
  32. private:
  33. // Wraps T in a form that supports `hash_value`. Used for adapting types that
  34. // we can't extend directly.
  35. template <typename T>
  36. static auto WrapForHash(const T& t) -> const T& {
  37. return t;
  38. }
  39. template <typename T>
  40. static auto WrapForHash(const std::vector<T>& vec) -> llvm::ArrayRef<T> {
  41. return vec;
  42. }
  43. };
  44. } // namespace Carbon
  45. #endif // CARBON_EXPLORER_BASE_DECOMPOSE_H_