entity_name.h 4.2 KB

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