Просмотр исходного кода

Unwrap FormatInst templating (#5505)

Now that `FormatInstLhs` and `FormatInstRhs` are no longer templated,
remove `FormatInst` templating.
Jon Ross-Perkins 11 месяцев назад
Родитель
Сommit
0091c699a9
2 измененных файлов с 55 добавлено и 89 удалено
  1. 50 52
      toolchain/sem_ir/formatter.cpp
  2. 5 37
      toolchain/sem_ir/formatter.h

+ 50 - 52
toolchain/sem_ir/formatter.cpp

@@ -745,67 +745,65 @@ auto Formatter::FormatNameScope(NameScopeId id, llvm::StringRef label) -> void {
   }
 }
 
-auto Formatter::FormatInst(InstId inst_id, Inst inst) -> void {
-  CARBON_KIND_SWITCH(inst) {
-#define CARBON_SEM_IR_INST_KIND(InstT)  \
-  case CARBON_KIND(InstT typed_inst): { \
-    FormatInst(inst_id, typed_inst);    \
-    break;                              \
-  }
-#include "toolchain/sem_ir/inst_kind.def"
-  }
-}
-
-auto Formatter::FormatInst(InstId /*inst_id*/, Branch inst) -> void {
-  if (!in_terminator_sequence_) {
-    Indent();
-  }
-  out_ << Branch::Kind.ir_name() << " ";
-  FormatLabel(inst.target_id);
-  out_ << "\n";
-  in_terminator_sequence_ = false;
-}
-
-auto Formatter::FormatInst(InstId /*inst_id*/, BranchIf inst) -> void {
-  if (!in_terminator_sequence_) {
+auto Formatter::FormatInst(InstId inst_id) -> void {
+  if (!inst_id.has_value()) {
     Indent();
+    out_ << "none\n";
+    return;
   }
-  out_ << "if ";
-  FormatName(inst.cond_id);
-  out_ << " " << Branch::Kind.ir_name() << " ";
-  FormatLabel(inst.target_id);
-  out_ << " else ";
-  in_terminator_sequence_ = true;
-}
 
-auto Formatter::FormatInst(InstId /*inst_id*/, BranchWithArg inst) -> void {
   if (!in_terminator_sequence_) {
     Indent();
   }
-  out_ << BranchWithArg::Kind.ir_name() << " ";
-  FormatLabel(inst.target_id);
-  out_ << "(";
-  FormatName(inst.arg_id);
-  out_ << ")\n";
-  in_terminator_sequence_ = false;
-}
 
-auto Formatter::FormatInst(InstId inst_id, ImportRefUnloaded inst) -> void {
-  Indent();
-  FormatInstLhs(inst_id, inst);
-  out_ << ImportRefUnloaded::Kind.ir_name();
-  FormatInstRhs(inst);
-  out_ << "\n";
-}
+  auto inst = sem_ir_->insts().GetWithAttachedType(inst_id);
+  CARBON_KIND_SWITCH(inst) {
+    case CARBON_KIND(Branch branch): {
+      out_ << Branch::Kind.ir_name() << " ";
+      FormatLabel(branch.target_id);
+      out_ << "\n";
+      in_terminator_sequence_ = false;
+      return;
+    }
+    case CARBON_KIND(BranchIf branch_if): {
+      out_ << "if ";
+      FormatName(branch_if.cond_id);
+      out_ << " " << Branch::Kind.ir_name() << " ";
+      FormatLabel(branch_if.target_id);
+      out_ << " else ";
+      in_terminator_sequence_ = true;
+      return;
+    }
+    case CARBON_KIND(BranchWithArg branch_with_arg): {
+      out_ << BranchWithArg::Kind.ir_name() << " ";
+      FormatLabel(branch_with_arg.target_id);
+      out_ << "(";
+      FormatName(branch_with_arg.arg_id);
+      out_ << ")\n";
+      in_terminator_sequence_ = false;
+      return;
+    }
+    default: {
+      FormatInstLhs(inst_id, inst);
+      out_ << inst.kind().ir_name();
+
+      // Add constants for everything except `ImportRefUnloaded`.
+      if (!inst.Is<ImportRefUnloaded>()) {
+        pending_constant_value_ =
+            sem_ir_->constant_values().GetAttached(inst_id);
+        pending_constant_value_is_self_ =
+            sem_ir_->constant_values().GetInstIdIfValid(
+                pending_constant_value_) == inst_id;
+      }
 
-auto Formatter::FormatInst(InstId inst_id) -> void {
-  if (!inst_id.has_value()) {
-    Indent();
-    out_ << "none\n";
-    return;
+      FormatInstRhs(inst);
+      // This usually prints the constant, but when `FormatInstRhs` prints it
+      // first (or for `ImportRefUnloaded`), this does nothing.
+      FormatPendingConstantValue(AddSpace::Before);
+      out_ << "\n";
+      return;
+    }
   }
-
-  FormatInst(inst_id, sem_ir_->insts().GetWithAttachedType(inst_id));
 }
 
 auto Formatter::FormatPendingImportedFrom(AddSpace space_where) -> void {

+ 5 - 37
toolchain/sem_ir/formatter.h

@@ -203,33 +203,14 @@ class Formatter {
   // Prints the contents of a name scope, with an optional label.
   auto FormatNameScope(NameScopeId id, llvm::StringRef label = "") -> void;
 
-  auto FormatInst(InstId inst_id, Inst inst) -> void;
-
-  // Don't print a constant for ImportRefUnloaded.
-  auto FormatInst(InstId inst_id, ImportRefUnloaded inst) -> void;
-
-  // Formats as "branch <target>".
-  auto FormatInst(InstId inst_id, Branch inst) -> void;
-
-  // Formats as "if <cond> branch_if <target> else ".
-  auto FormatInst(InstId inst_id, BranchIf inst) -> void;
-
-  // Formats as "branch_with_arg <target>(arg)".
-  auto FormatInst(InstId inst_id, BranchWithArg inst) -> void;
-
-  // Prints a single instruction. This typically dispatches to one of the
-  // `FormatInst` overloads, based on a specific instruction type.
+  // Prints a single instruction. This typically formats as:
+  //   `FormatInstLhs()` `<ir_name>` `FormatInstRhs()` `<constant>`
   //
-  // While there is default formatting behavior, we do have overloads when
-  // special behavior is required, although typically of functions called by
-  // `FormatInst` rather than `FormatInst` itself. For example, `FormatInstRhs`
-  // is frequently overloaded because the default argument formatting often
-  // isn't what we want for instructions.
+  // Some instruction kinds are special-cased here. However, it's more common to
+  // provide special-casing of `FormatInstRhs`, for custom argument
+  // formatting.
   auto FormatInst(InstId inst_id) -> void;
 
-  template <typename InstT>
-  auto FormatInst(InstId inst_id, InstT inst) -> void;
-
   // If there is a pending library name that the current instruction was
   // imported from, print it now and clear it out.
   auto FormatPendingImportedFrom(AddSpace space_where) -> void;
@@ -449,19 +430,6 @@ auto Formatter::FormatEntityStart(llvm::StringRef entity_kind,
                     entity_id);
 }
 
-template <typename InstT>
-auto Formatter::FormatInst(InstId inst_id, InstT inst) -> void {
-  Indent();
-  FormatInstLhs(inst_id, inst);
-  out_ << InstT::Kind.ir_name();
-  pending_constant_value_ = sem_ir_->constant_values().GetAttached(inst_id);
-  pending_constant_value_is_self_ = sem_ir_->constant_values().GetInstIdIfValid(
-                                        pending_constant_value_) == inst_id;
-  FormatInstRhs(inst);
-  FormatPendingConstantValue(AddSpace::Before);
-  out_ << "\n";
-}
-
 template <typename... Types>
 constexpr auto Formatter::MakeFormatArgFnTable(TypeEnum<Types...>* /*id_kind*/)
     -> std::array<FormatArgFnT*, SemIR::IdKind::NumValues> {