Parcourir la source

Add unit tagging to ClassId (#6195)

David Blaikie il y a 6 mois
Parent
commit
60b2b7f8c1

+ 1 - 1
scripts/lldbinit.py

@@ -81,7 +81,7 @@ Example usage:
     # The set of "Make" functions in dump.cpp, and whether the ids are printed
     # The set of "Make" functions in dump.cpp, and whether the ids are printed
     # in decimal or hex.
     # in decimal or hex.
     id_types = {
     id_types = {
-        "class": ("SemIR::MakeClassId", DECIMAL),
+        "class": ("SemIR::MakeClassId", HEX),
         "constant": ("SemIR::MakeConstantId", DECIMAL),
         "constant": ("SemIR::MakeConstantId", DECIMAL),
         "symbolic_constant": ("SemIR::MakeSymbolicConstantId", DECIMAL),
         "symbolic_constant": ("SemIR::MakeSymbolicConstantId", DECIMAL),
         "entity_name": ("SemIR::MakeEntityNameId", DECIMAL),
         "entity_name": ("SemIR::MakeEntityNameId", DECIMAL),

+ 3 - 3
toolchain/check/testdata/basics/raw_sem_ir/cpp_interop.carbon

@@ -61,7 +61,7 @@ fn G(x: Cpp.X) {
 // CHECK:STDOUT:     function3:       {name: name4, parent_scope: name_scope1, call_params_id: inst_block13}
 // CHECK:STDOUT:     function3:       {name: name4, parent_scope: name_scope1, call_params_id: inst_block13}
 // CHECK:STDOUT:     function4:       {name: name6, parent_scope: name_scope1, call_params_id: inst_block18}
 // CHECK:STDOUT:     function4:       {name: name6, parent_scope: name_scope1, call_params_id: inst_block18}
 // CHECK:STDOUT:   classes:
 // CHECK:STDOUT:   classes:
-// CHECK:STDOUT:     class0:          {name: name3, parent_scope: name_scope1, self_type_id: type(inst60000013), inheritance_kind: Base, is_dynamic: 0, scope_id: name_scope2, body_block_id: inst_block10, adapt_id: inst<none>, base_id: inst<none>, complete_type_witness_id: inst60000021, vtable_decl_id: inst<none>}}
+// CHECK:STDOUT:     class60000000:   {name: name3, parent_scope: name_scope1, self_type_id: type(inst60000013), inheritance_kind: Base, is_dynamic: 0, scope_id: name_scope2, body_block_id: inst_block10, adapt_id: inst<none>, base_id: inst<none>, complete_type_witness_id: inst60000021, vtable_decl_id: inst<none>}}
 // CHECK:STDOUT:   generics:        {}
 // CHECK:STDOUT:   generics:        {}
 // CHECK:STDOUT:   specifics:       {}
 // CHECK:STDOUT:   specifics:       {}
 // CHECK:STDOUT:   struct_type_fields:
 // CHECK:STDOUT:   struct_type_fields:
@@ -116,8 +116,8 @@ fn G(x: Cpp.X) {
 // CHECK:STDOUT:     inst6000000F:    {kind: ImportCppDecl}
 // CHECK:STDOUT:     inst6000000F:    {kind: ImportCppDecl}
 // CHECK:STDOUT:     inst60000010:    {kind: Namespace, arg0: name_scope1, arg1: inst6000000F, type: type(inst(NamespaceType))}
 // CHECK:STDOUT:     inst60000010:    {kind: Namespace, arg0: name_scope1, arg1: inst6000000F, type: type(inst(NamespaceType))}
 // CHECK:STDOUT:     inst60000011:    {kind: NameRef, arg0: name0, arg1: inst60000010, type: type(inst(NamespaceType))}
 // CHECK:STDOUT:     inst60000011:    {kind: NameRef, arg0: name0, arg1: inst60000010, type: type(inst(NamespaceType))}
-// CHECK:STDOUT:     inst60000012:    {kind: ClassDecl, arg0: class0, arg1: inst_block<none>, type: type(TypeType)}
-// CHECK:STDOUT:     inst60000013:    {kind: ClassType, arg0: class0, arg1: specific<none>, type: type(TypeType)}
+// CHECK:STDOUT:     inst60000012:    {kind: ClassDecl, arg0: class60000000, arg1: inst_block<none>, type: type(TypeType)}
+// CHECK:STDOUT:     inst60000013:    {kind: ClassType, arg0: class60000000, arg1: specific<none>, type: type(TypeType)}
 // CHECK:STDOUT:     inst60000014:    {kind: NameRef, arg0: name3, arg1: inst60000012, type: type(TypeType)}
 // CHECK:STDOUT:     inst60000014:    {kind: NameRef, arg0: name3, arg1: inst60000012, type: type(TypeType)}
 // CHECK:STDOUT:     inst60000015:    {kind: BindName, arg0: entity_name0, arg1: inst60000019, type: type(inst60000013)}
 // CHECK:STDOUT:     inst60000015:    {kind: BindName, arg0: entity_name0, arg1: inst60000019, type: type(inst60000013)}
 // CHECK:STDOUT:     inst60000016:    {kind: PatternType, arg0: inst60000013, type: type(TypeType)}
 // CHECK:STDOUT:     inst60000016:    {kind: PatternType, arg0: inst60000013, type: type(TypeType)}

+ 1 - 0
toolchain/sem_ir/file.cpp

@@ -35,6 +35,7 @@ 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)),
+      classes_(IdTag(check_ir_id.index, 0)),
       impls_(*this),
       impls_(*this),
       // 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

+ 4 - 0
toolchain/sem_ir/ids.cpp

@@ -50,6 +50,10 @@ auto CheckIRId::Print(llvm::raw_ostream& out) const -> void {
   }
   }
 }
 }
 
 
+auto ClassId::Print(llvm::raw_ostream& out) const -> void {
+  IdBase::PrintHex(out);
+}
+
 auto GenericInstIndex::Print(llvm::raw_ostream& out) const -> void {
 auto GenericInstIndex::Print(llvm::raw_ostream& out) const -> void {
   out << "generic_inst";
   out << "generic_inst";
   if (has_value()) {
   if (has_value()) {

+ 2 - 0
toolchain/sem_ir/ids.h

@@ -304,6 +304,8 @@ struct ClassId : public IdBase<ClassId> {
   static constexpr llvm::StringLiteral Label = "class";
   static constexpr llvm::StringLiteral Label = "class";
 
 
   using IdBase::IdBase;
   using IdBase::IdBase;
+
+  auto Print(llvm::raw_ostream& out) const -> void;
 };
 };
 
 
 // The ID of a `Vtable`.
 // The ID of a `Vtable`.

+ 5 - 1
toolchain/sem_ir/inst_namer.h

@@ -50,8 +50,12 @@ class InstNamer {
   template <typename IdT>
   template <typename IdT>
     requires ScopeIdTypeEnum::Contains<IdT>
     requires ScopeIdTypeEnum::Contains<IdT>
   auto GetScopeFor(IdT id) const -> ScopeId {
   auto GetScopeFor(IdT id) const -> ScopeId {
+    auto index = id.index;
+    if constexpr (std::is_same_v<IdT, ClassId>) {
+      index = sem_ir_->classes().GetRawIndex(id);
+    }
     return static_cast<ScopeId>(GetScopeIdOffset(ScopeIdTypeEnum::For<IdT>) +
     return static_cast<ScopeId>(GetScopeIdOffset(ScopeIdTypeEnum::For<IdT>) +
-                                id.index);
+                                index);
   }
   }
 
 
   // Returns the scope ID corresponding to a generic. A generic object shares
   // Returns the scope ID corresponding to a generic. A generic object shares