Jelajahi Sumber

Omit a trailing `SpecificId` by default when formatting instruction arguments. (#4315)

Richard Smith 1 tahun lalu
induk
melakukan
5d481877f1
1 mengubah file dengan 10 tambahan dan 40 penghapusan
  1. 10 40
      toolchain/sem_ir/formatter.cpp

+ 10 - 40
toolchain/sem_ir/formatter.cpp

@@ -661,6 +661,16 @@ class FormatterImpl {
     // By default, an instruction has a comma-separated argument list.
     using Info = Internal::InstLikeTypeInfo<InstT>;
     if constexpr (Info::NumArgs == 2) {
+      // Several instructions have a second operand that's a specific ID. We
+      // don't include it in the argument list if there is no corresponding
+      // specific, that is, when we're not in a generic context.
+      if constexpr (std::is_same_v<typename Info::template ArgType<1>,
+                                   SemIR::SpecificId>) {
+        if (!Info::template Get<1>(inst).is_valid()) {
+          FormatArgs(Info::template Get<0>(inst));
+          return;
+        }
+      }
       FormatArgs(Info::template Get<0>(inst), Info::template Get<1>(inst));
     } else if constexpr (Info::NumArgs == 1) {
       FormatArgs(Info::template Get<0>(inst));
@@ -790,35 +800,11 @@ class FormatterImpl {
     FormatTrailingBlock(inst.decl_block_id);
   }
 
-  auto FormatInstRHS(FunctionType inst) -> void {
-    if (inst.specific_id.is_valid()) {
-      FormatArgs(inst.function_id, inst.specific_id);
-    } else {
-      FormatArgs(inst.function_id);
-    }
-  }
-
   auto FormatInstRHS(ClassDecl inst) -> void {
     FormatArgs(inst.class_id);
     FormatTrailingBlock(inst.decl_block_id);
   }
 
-  auto FormatInstRHS(ClassType inst) -> void {
-    if (inst.specific_id.is_valid()) {
-      FormatArgs(inst.class_id, inst.specific_id);
-    } else {
-      FormatArgs(inst.class_id);
-    }
-  }
-
-  auto FormatInstRHS(GenericClassType inst) -> void {
-    if (inst.enclosing_specific_id.is_valid()) {
-      FormatArgs(inst.class_id, inst.enclosing_specific_id);
-    } else {
-      FormatArgs(inst.class_id);
-    }
-  }
-
   auto FormatInstRHS(ImplDecl inst) -> void {
     FormatArgs(inst.impl_id);
     FormatTrailingBlock(inst.decl_block_id);
@@ -829,22 +815,6 @@ class FormatterImpl {
     FormatTrailingBlock(inst.decl_block_id);
   }
 
-  auto FormatInstRHS(InterfaceType inst) -> void {
-    if (inst.specific_id.is_valid()) {
-      FormatArgs(inst.interface_id, inst.specific_id);
-    } else {
-      FormatArgs(inst.interface_id);
-    }
-  }
-
-  auto FormatInstRHS(GenericInterfaceType inst) -> void {
-    if (inst.enclosing_specific_id.is_valid()) {
-      FormatArgs(inst.interface_id, inst.enclosing_specific_id);
-    } else {
-      FormatArgs(inst.interface_id);
-    }
-  }
-
   auto FormatInstRHS(IntLiteral inst) -> void {
     out_ << " ";
     sem_ir_.ints()