entity_name.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_ENTITY_NAME_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_ENTITY_NAME_H_
  6. #include "common/hashing.h"
  7. #include "common/set.h"
  8. #include "toolchain/base/value_store.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. namespace Carbon::SemIR {
  11. struct EntityName : public Printable<EntityName> {
  12. auto Print(llvm::raw_ostream& out) const -> void {
  13. out << "{name: " << name_id << ", parent_scope: " << parent_scope_id
  14. << ", index: " << bind_index_value << ", is_template: " << is_template
  15. << "}";
  16. }
  17. friend auto CarbonHashtableEq(const EntityName& lhs, const EntityName& rhs)
  18. -> bool {
  19. return std::memcmp(&lhs, &rhs, sizeof(EntityName)) == 0;
  20. }
  21. // The index of the binding, if this is the name of a symbolic binding, or
  22. // `None` otherwise. This is also `None` for a `.Self` symbolic binding,
  23. // because such a binding is not assigned an index.
  24. auto bind_index() const -> CompileTimeBindIndex {
  25. return CompileTimeBindIndex(bind_index_value);
  26. }
  27. // The name.
  28. NameId name_id;
  29. // The parent scope.
  30. NameScopeId parent_scope_id;
  31. // TODO: The following two fields are only meaningful for a symbolic binding.
  32. // Consider splitting them off into a separate type so that we don't store
  33. // them for other kinds of `EntityName`.
  34. // The bind_index() value, unwrapped so it can be stored in a bit-field.
  35. int32_t bind_index_value : 31 = CompileTimeBindIndex::None.index;
  36. // Whether this binding is a template parameter.
  37. bool is_template : 1 = false;
  38. };
  39. // Hashing for EntityName. See common/hashing.h.
  40. inline auto CarbonHashValue(const EntityName& value, uint64_t seed)
  41. -> HashCode {
  42. Hasher hasher(seed);
  43. hasher.HashRaw(value);
  44. return static_cast<HashCode>(hasher);
  45. }
  46. // Value store for EntityName. In addition to the regular ValueStore
  47. // functionality, this can provide optional canonical IDs for EntityNames.
  48. struct EntityNameStore : public ValueStore<EntityNameId> {
  49. public:
  50. // Adds an entity name for a symbolic binding.
  51. auto AddSymbolicBindingName(NameId name_id, NameScopeId parent_scope_id,
  52. CompileTimeBindIndex bind_index, bool is_template)
  53. -> EntityNameId {
  54. return Add({.name_id = name_id,
  55. .parent_scope_id = parent_scope_id,
  56. .bind_index_value = bind_index.index,
  57. .is_template = is_template});
  58. }
  59. // Convert an `EntityName` to a canonical ID. All calls to this with
  60. // equivalent `EntityName`s will return the same `EntityNameId`. Same as
  61. // `MakeCanonical(Add(name))` except that no new `EntityName` is added if we
  62. // already have a canonical `EntityNameId` for that name.
  63. auto AddCanonical(EntityName name) -> EntityNameId;
  64. // Convert an ID to a canonical ID. All calls to this with equivalent
  65. // `EntityName`s will return the same `EntityNameId`.
  66. auto MakeCanonical(EntityNameId id) -> EntityNameId;
  67. private:
  68. class KeyContext;
  69. Set<EntityNameId, /*SmallSize=*/0, KeyContext> canonical_ids_;
  70. };
  71. class EntityNameStore::KeyContext : public TranslatingKeyContext<KeyContext> {
  72. public:
  73. explicit KeyContext(const EntityNameStore* store) : store_(store) {}
  74. // Note that it is safe to return a `const` reference here as the underlying
  75. // object's lifetime is provided by the `store_`.
  76. auto TranslateKey(EntityNameId id) const -> const EntityName& {
  77. return store_->Get(id);
  78. }
  79. private:
  80. const EntityNameStore* store_;
  81. };
  82. inline auto EntityNameStore::AddCanonical(EntityName name) -> EntityNameId {
  83. return canonical_ids_
  84. .Insert(
  85. name, [&] { return Add(name); }, KeyContext(this))
  86. .key();
  87. }
  88. inline auto EntityNameStore::MakeCanonical(EntityNameId id) -> EntityNameId {
  89. return canonical_ids_.Insert(id, KeyContext(this)).key();
  90. }
  91. } // namespace Carbon::SemIR
  92. #endif // CARBON_TOOLCHAIN_SEM_IR_ENTITY_NAME_H_