Jelajahi Sumber

Add ValueStore ctor template for Id to use for IdTag (#6226)

Based on review feedback on
https://github.com/carbon-language/carbon-lang/pull/6215#discussion_r2430177644

There are some intermediate commits with alternatives, finding other
ways (non-templates) to address the layering boundaries between
`ValueStore` construction and `CheckIRId` tagging. But, yeah, template
seems like the way to go - certainly in terms of terseness and probably
in terms of extensibility to other Id tagging as/when needed.
David Blaikie 6 bulan lalu
induk
melakukan
4fdc08582a

+ 3 - 1
toolchain/base/canonical_value_store.h

@@ -37,7 +37,9 @@ class CanonicalValueStore {
   using ConstRefType = ValueStoreTypes<ValueT>::ConstRefType;
   using ConstRefType = ValueStoreTypes<ValueT>::ConstRefType;
 
 
   CanonicalValueStore() = default;
   CanonicalValueStore() = default;
-  explicit CanonicalValueStore(IdTag tag) : values_(tag) {}
+  template <typename Id>
+  explicit CanonicalValueStore(Id id, int32_t initial_reserved_ids = 0)
+      : values_(id, initial_reserved_ids) {}
 
 
   // Stores a canonical copy of the value and returns an ID to reference it. If
   // Stores a canonical copy of the value and returns an ID to reference it. If
   // the value is already in the store, returns the ID of the existing value.
   // the value is already in the store, returns the ID of the existing value.

+ 3 - 0
toolchain/base/value_store.h

@@ -177,6 +177,9 @@ class ValueStore
 
 
   ValueStore() = default;
   ValueStore() = default;
   explicit ValueStore(IdTag tag) : tag_(tag) {}
   explicit ValueStore(IdTag tag) : tag_(tag) {}
+  template <typename Id>
+  explicit ValueStore(Id id, int32_t initial_reserved_ids = 0)
+      : tag_(id.index, initial_reserved_ids) {}
 
 
   // Stores the value and returns an ID to reference it.
   // Stores the value and returns an ID to reference it.
   auto Add(ValueType value) -> IdType {
   auto Add(ValueType value) -> IdType {

+ 5 - 5
toolchain/sem_ir/file.cpp

@@ -35,12 +35,12 @@ File::File(const Parse::Tree* parse_tree, CheckIRId check_ir_id,
                                  : LibraryNameId::Default),
                                  : LibraryNameId::Default),
       value_stores_(&value_stores),
       value_stores_(&value_stores),
       filename_(std::move(filename)),
       filename_(std::move(filename)),
-      functions_(IdTag(check_ir_id.index, 0)),
-      cpp_overload_sets_(IdTag(check_ir_id.index, 0)),
-      classes_(IdTag(check_ir_id.index, 0)),
-      associated_constants_(IdTag(check_ir_id.index, 0)),
+      functions_(check_ir_id),
+      cpp_overload_sets_(check_ir_id),
+      classes_(check_ir_id),
+      associated_constants_(check_ir_id),
       impls_(*this),
       impls_(*this),
-      specific_interfaces_(IdTag(check_ir_id.index, 0)),
+      specific_interfaces_(check_ir_id_),
       // The `+1` prevents adding a tag to the global `NameSpace::PackageInstId`
       // The `+1` prevents adding a tag to the global `NameSpace::PackageInstId`
       // instruction. It's not a "singleton" instruction, but it's a unique
       // instruction. It's not a "singleton" instruction, but it's a unique
       // instruction id that comes right after the singletons.
       // instruction id that comes right after the singletons.

+ 1 - 1
toolchain/sem_ir/impl.cpp

@@ -9,7 +9,7 @@
 namespace Carbon::SemIR {
 namespace Carbon::SemIR {
 
 
 ImplStore::ImplStore(File& sem_ir)
 ImplStore::ImplStore(File& sem_ir)
-    : sem_ir_(sem_ir), values_(IdTag(sem_ir.check_ir_id().index, 0)) {}
+    : sem_ir_(sem_ir), values_(sem_ir.check_ir_id()) {}
 
 
 auto ImplStore::GetOrAddLookupBucket(const Impl& impl) -> LookupBucketRef {
 auto ImplStore::GetOrAddLookupBucket(const Impl& impl) -> LookupBucketRef {
   auto self_id = sem_ir_.constant_values().GetConstantInstId(impl.self_id);
   auto self_id = sem_ir_.constant_values().GetConstantInstId(impl.self_id);

+ 1 - 2
toolchain/sem_ir/inst.cpp

@@ -55,8 +55,7 @@ const std::pair<IdKind, IdKind> Inst::ArgKindTable[] = {
 };
 };
 
 
 InstStore::InstStore(File* file, int32_t reserved_inst_ids)
 InstStore::InstStore(File* file, int32_t reserved_inst_ids)
-    : file_(file),
-      values_(IdTag(file->check_ir_id().index, reserved_inst_ids)) {}
+    : file_(file), values_(file->check_ir_id(), reserved_inst_ids) {}
 
 
 auto InstStore::GetUnattachedType(TypeId type_id) const -> TypeId {
 auto InstStore::GetUnattachedType(TypeId type_id) const -> TypeId {
   return file_->types().GetUnattachedType(type_id);
   return file_->types().GetUnattachedType(type_id);