Files
carbon-lang/toolchain/sem_ir/inst_kind.cpp
T
Jon Ross-Perkins 469f1c8e64 Refactor InstKind to move metadata from macros to the type. (#4119)
This adds `DefinitionInfo` for `Define`-based configuration so that
parameters are optional. It also makes it easier to provide the
equivalent functions on both `Definition` and `Define`.

A common pattern used here is to change from a `switch` with in-line
`case`s to instead have `case`s that call an overloaded function. What's
happening here is that the instruction type is used to select an
overload, and if an overload is not defined, a compiler error would
result. Meanwhile, clusters of overloads are being defined using
`requires`-based templating, so that equivalent implementations are not
copied. This addresses a limitation of a vanilla `switch` approach where
it's hard to have redundant cases using conditional logic, while also
getting compiler errors when adding new `InstKind` entries, which had
been a significant part of why we used macros previously.

This starts hitting some odd clang-format edge cases causing
`CARBON_KIND_SWITCH(inst){` (missing space), which I haven't seen
before. Adding `CARBON_KIND_SWITCH` to .clang-format works around it.
2024-07-11 21:39:25 +00:00

35 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_kind.h"
#include "toolchain/sem_ir/typed_insts.h"
namespace Carbon::SemIR {
CARBON_DEFINE_ENUM_CLASS_NAMES(InstKind) = {
#define CARBON_SEM_IR_INST_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name)
#include "toolchain/sem_ir/inst_kind.def"
};
auto InstKind::definition_info(InstKind inst_kind) -> const DefinitionInfo& {
static constexpr InstKind::DefinitionInfo DefinitionInfos[] = {
#define CARBON_SEM_IR_INST_KIND(Name) SemIR::Name::Kind.info_,
#include "toolchain/sem_ir/inst_kind.def"
};
return DefinitionInfos[inst_kind.AsInt()];
}
auto InstKind::value_kind() const -> InstValueKind {
static constexpr InstValueKind Table[] = {
#define CARBON_SEM_IR_INST_KIND(Name) \
Internal::HasTypeIdMember<SemIR::Name> ? InstValueKind::Typed \
: InstValueKind::None,
#include "toolchain/sem_ir/inst_kind.def"
};
return Table[AsInt()];
}
} // namespace Carbon::SemIR