mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 14:40:11 +01:00
Add a mechanism to define instruction categories, to support inspecting the common representation of similar kinds of instruction. Use that mechanism to make formatting of branch instructions slightly more type-safe. The idea here is to use the existing `Inst` mechanism for converting to and from structs, extended to operate on a struct representing multiple different kinds of instruction. In this case, the concrete kind of instruction is stored in the struct in a `kind` field, rather than being implied by the type. Factored out of #3555 where this mechanism is used to provide a common interface for runtime and symbolic name bindings.
38 lines
1.1 KiB
C++
38 lines
1.1 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/sem_ir/inst.h"
|
|
|
|
namespace Carbon::SemIR {
|
|
|
|
auto Inst::Print(llvm::raw_ostream& out) const -> void {
|
|
out << "{kind: " << kind_;
|
|
|
|
auto print_args = [&](auto info) {
|
|
using Info = decltype(info);
|
|
if constexpr (Info::NumArgs > 0) {
|
|
out << ", arg0: " << FromRaw<typename Info::template ArgType<0>>(arg0_);
|
|
}
|
|
if constexpr (Info::NumArgs > 1) {
|
|
out << ", arg1: " << FromRaw<typename Info::template ArgType<1>>(arg1_);
|
|
}
|
|
};
|
|
|
|
// clang warns on unhandled enum values; clang-tidy is incorrect here.
|
|
// NOLINTNEXTLINE(bugprone-switch-missing-default-case)
|
|
switch (kind_) {
|
|
#define CARBON_SEM_IR_INST_KIND(Name) \
|
|
case Name::Kind: \
|
|
print_args(InstLikeTypeInfo<Name>()); \
|
|
break;
|
|
#include "toolchain/sem_ir/inst_kind.def"
|
|
}
|
|
if (type_id_.is_valid()) {
|
|
out << ", type: " << type_id_;
|
|
}
|
|
out << "}";
|
|
}
|
|
|
|
} // namespace Carbon::SemIR
|