Unwrap FormatInst templating (#5505)

Now that `FormatInstLhs` and `FormatInstRhs` are no longer templated,
remove `FormatInst` templating.
This commit is contained in:
Jon Ross-Perkins
2025-05-21 16:21:13 +00:00
committed by GitHub
parent 852d0191a9
commit 0091c699a9
2 changed files with 57 additions and 91 deletions
+52 -54
View File
@@ -745,59 +745,6 @@ 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_) {
Indent();
}
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 Formatter::FormatInst(InstId inst_id) -> void {
if (!inst_id.has_value()) {
Indent();
@@ -805,7 +752,58 @@ auto Formatter::FormatInst(InstId inst_id) -> void {
return;
}
FormatInst(inst_id, sem_ir_->insts().GetWithAttachedType(inst_id));
if (!in_terminator_sequence_) {
Indent();
}
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;
}
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;
}
}
}
auto Formatter::FormatPendingImportedFrom(AddSpace space_where) -> void {
+5 -37
View File
@@ -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> {