Files
carbon-lang/toolchain/sem_ir/inst.cpp
T
Richard SmithandDana Jansens 4f5d11a28b Build generic eval blocks incrementally (#5313)
Instead of building an eval block as a separate pass at the end of a
generic, build the eval block incrementally.

The larger change here is that asking for the type or constant value of
an instruction now always returns an unattached type or constant value,
in order to preserve the behavior that we previously achieved by doing
the rewrite to attached types and constant values at the end of handling
the generic.

This also incidentally fixes some subtle issues where attached types and
constant values would leak out into check and cause it to get confused
about differences between attached and unattached values. Check should
no longer see attached values except where it explicitly asks for them.

---------

Co-authored-by: Dana Jansens <danakj@orodu.net>
2025-05-01 20:24:15 +00:00

62 lines
1.8 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"
#include <utility>
#include "toolchain/sem_ir/file.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_);
}
};
switch (kind()) {
#define CARBON_SEM_IR_INST_KIND(Name) \
case Name::Kind: \
print_args(Internal::InstLikeTypeInfo<Name>()); \
break;
#include "toolchain/sem_ir/inst_kind.def"
}
if (type_id_.has_value()) {
out << ", type: " << type_id_;
}
out << "}";
}
// Returns the IdKind of an instruction's argument, or None if there is no
// argument with that index.
template <typename InstKind, int ArgIndex>
static constexpr auto IdKindFor() -> IdKind {
using Info = Internal::InstLikeTypeInfo<InstKind>;
if constexpr (ArgIndex < Info::NumArgs) {
return IdKind::For<typename Info::template ArgType<ArgIndex>>;
} else {
return IdKind::None;
}
}
const std::pair<IdKind, IdKind> Inst::ArgKindTable[] = {
#define CARBON_SEM_IR_INST_KIND(Name) \
{IdKindFor<Name, 0>(), IdKindFor<Name, 1>()},
#include "toolchain/sem_ir/inst_kind.def"
};
auto InstStore::GetUnattachedType(TypeId type_id) const -> TypeId {
return file_->types().GetUnattachedType(type_id);
}
} // namespace Carbon::SemIR