bind_name.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_SEM_IR_BIND_NAME_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_BIND_NAME_H_
  6. #include "common/hashing.h"
  7. #include "toolchain/base/value_store.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::SemIR {
  10. struct BindNameInfo : public Printable<BindNameInfo> {
  11. auto Print(llvm::raw_ostream& out) const -> void {
  12. out << "{name: " << name_id << ", parent_scope: " << parent_scope_id
  13. << ", index: " << bind_index << "}";
  14. }
  15. // The name.
  16. NameId name_id;
  17. // The parent scope.
  18. NameScopeId parent_scope_id;
  19. // The index for a compile-time binding. Invalid for a runtime binding.
  20. CompileTimeBindIndex bind_index;
  21. };
  22. // Hashing for BindNameInfo. See common/hashing.h.
  23. inline auto CarbonHashValue(const BindNameInfo& value, uint64_t seed)
  24. -> HashCode {
  25. Hasher hasher(seed);
  26. hasher.HashRaw(value);
  27. return static_cast<HashCode>(hasher);
  28. }
  29. // DenseMapInfo for BindNameInfo.
  30. struct BindNameInfoDenseMapInfo {
  31. static auto getEmptyKey() -> BindNameInfo {
  32. return BindNameInfo{.name_id = NameId::Invalid,
  33. .parent_scope_id = NameScopeId::Invalid,
  34. .bind_index = CompileTimeBindIndex(
  35. CompileTimeBindIndex::InvalidIndex - 1)};
  36. }
  37. static auto getTombstoneKey() -> BindNameInfo {
  38. return BindNameInfo{.name_id = NameId::Invalid,
  39. .parent_scope_id = NameScopeId::Invalid,
  40. .bind_index = CompileTimeBindIndex(
  41. CompileTimeBindIndex::InvalidIndex - 2)};
  42. }
  43. static auto getHashValue(const BindNameInfo& val) -> unsigned {
  44. return static_cast<uint64_t>(HashValue(val));
  45. }
  46. static auto isEqual(const BindNameInfo& lhs, const BindNameInfo& rhs)
  47. -> bool {
  48. return std::memcmp(&lhs, &rhs, sizeof(BindNameInfo)) == 0;
  49. }
  50. };
  51. // Value store for BindNameInfo. In addition to the regular ValueStore
  52. // functionality, this can provide optional canonical IDs for BindNameInfos.
  53. struct BindNameStore : public ValueStore<BindNameId> {
  54. public:
  55. // Convert an ID to a canonical ID. All calls to this with equivalent
  56. // `BindNameInfo`s will return the same `BindNameId`.
  57. auto MakeCanonical(BindNameId id) -> BindNameId {
  58. return canonical_ids_.insert({Get(id), id}).first->second;
  59. }
  60. private:
  61. llvm::DenseMap<BindNameInfo, BindNameId, BindNameInfoDenseMapInfo>
  62. canonical_ids_;
  63. };
  64. } // namespace Carbon::SemIR
  65. #endif // CARBON_TOOLCHAIN_SEM_IR_BIND_NAME_H_