Files
carbon-lang/toolchain/sem_ir/ids.cpp
T
Jon Ross-Perkins 4923445e3a Drop Singleton from ErrorInst::SingletonInstId and similar (#5304)
We frequently want to operate on singletons. Per discussion, drop
`Singleton` to make the code shorter.

This started off as wanting to write `inst_id.is_error()`, but the
dependency relationship between ids.h and singleton_insts.h would
require some kind of delayed evaluation to allow the implementation to
remain in headers (which I suspect is helpful to have for inlining). I
could have added something like `IsErrorInst`, forward declared in ids.h
and defined in singleton_insts.h (which would always be included by
typed_insts.h), but the template approach felt like a decent balance
between (a) removing the boilerplate `::SingletonInstId`, (b)
understandability, (c) still visually mirroring if we immediately return
a singleton, and (d) flexibility for more than just `ErrorInst`. But TBH
I'd probably still have written `is_error()` if it didn't require
addressing the cross-header cycle.

Then I tried `SemIR::InstId::Is<SemIR::ErrorInst>`, which generally
worked with types but generated the complaint that it didn't shorten
*all* singleton uses. So pulling back on `::Is`, and instead just
dropping `Singleton`.
2025-04-15 22:40:29 +00:00

186 lines
4.7 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/ids.h"
#include "toolchain/base/value_ids.h"
#include "toolchain/sem_ir/singleton_insts.h"
#include "toolchain/sem_ir/typed_insts.h"
namespace Carbon::SemIR {
auto InstId::Print(llvm::raw_ostream& out) const -> void {
if (IsSingletonInstId(*this)) {
out << Label << "(" << SingletonInstKinds[index] << ")";
} else {
IdBase::Print(out);
}
}
auto ConstantId::Print(llvm::raw_ostream& out, bool disambiguate) const
-> void {
if (!has_value()) {
IdBase::Print(out);
return;
}
if (is_concrete()) {
if (disambiguate) {
out << "concrete_constant(";
}
out << concrete_inst_id();
if (disambiguate) {
out << ")";
}
} else if (is_symbolic()) {
out << "symbolic_constant" << symbolic_index();
} else {
CARBON_CHECK(!is_constant());
out << "runtime";
}
}
auto GenericInstIndex::Print(llvm::raw_ostream& out) const -> void {
out << "generic_inst";
if (has_value()) {
out << (region() == Declaration ? "_in_decl" : "_in_def") << index();
} else {
out << "<none>";
}
}
auto BoolValue::Print(llvm::raw_ostream& out) const -> void {
if (*this == False) {
out << "false";
} else if (*this == True) {
out << "true";
} else {
CARBON_FATAL("Invalid bool value {0}", index);
}
}
auto IntKind::Print(llvm::raw_ostream& out) const -> void {
if (*this == Unsigned) {
out << "unsigned";
} else if (*this == Signed) {
out << "signed";
} else {
CARBON_FATAL("Invalid int kind value {0}", index);
}
}
// Double-check the special value mapping and constexpr evaluation.
static_assert(NameId::SpecialNameId::Vptr == *NameId::Vptr.AsSpecialNameId());
auto NameId::ForIdentifier(IdentifierId id) -> NameId {
if (id.index >= 0) {
return NameId(id.index);
} else if (!id.has_value()) {
return NameId::None;
} else {
CARBON_FATAL("Unexpected identifier ID {0}", id);
}
}
auto NameId::ForPackageName(PackageNameId id) -> NameId {
if (auto identifier_id = id.AsIdentifierId(); identifier_id.has_value()) {
return ForIdentifier(identifier_id);
} else if (id == PackageNameId::Core) {
return NameId::Core;
} else if (!id.has_value()) {
return NameId::None;
} else {
CARBON_FATAL("Unexpected package ID {0}", id);
}
}
auto NameId::Print(llvm::raw_ostream& out) const -> void {
if (!has_value() || index >= 0) {
IdBase::Print(out);
return;
}
out << Label << "(";
auto special_name_id = AsSpecialNameId();
CARBON_CHECK(special_name_id, "Unknown index {0}", index);
switch (*special_name_id) {
#define CARBON_SPECIAL_NAME_ID_FOR_PRINT(Name) \
case SpecialNameId::Name: \
out << #Name; \
break;
CARBON_SPECIAL_NAME_ID(CARBON_SPECIAL_NAME_ID_FOR_PRINT)
#undef CARBON_SPECIAL_NAME_ID_FOR_PRINT
}
out << ")";
}
auto InstBlockId::Print(llvm::raw_ostream& out) const -> void {
if (*this == Unreachable) {
out << "unreachable";
} else if (*this == Empty) {
out << Label << "_empty";
} else if (*this == Exports) {
out << "exports";
} else if (*this == ImportRefs) {
out << "import_refs";
} else if (*this == GlobalInit) {
out << "global_init";
} else {
IdBase::Print(out);
}
}
auto TypeId::Print(llvm::raw_ostream& out) const -> void {
out << Label << "(";
if (*this == TypeType::TypeId) {
out << "TypeType";
} else if (*this == AutoType::TypeId) {
out << "AutoType";
} else if (*this == ErrorInst::TypeId) {
out << "Error";
} else {
AsConstantId().Print(out, /*disambiguate=*/false);
}
out << ")";
}
auto LibraryNameId::ForStringLiteralValueId(StringLiteralValueId id)
-> LibraryNameId {
CARBON_CHECK(id.index >= NoneIndex, "Unexpected library name ID {0}", id);
if (id == StringLiteralValueId::None) {
// Prior to SemIR, we use `None` to indicate `default`.
return LibraryNameId::Default;
} else {
return LibraryNameId(id.index);
}
}
auto LibraryNameId::Print(llvm::raw_ostream& out) const -> void {
if (*this == Default) {
out << Label << "Default";
} else if (*this == Error) {
out << Label << "<error>";
} else {
IdBase::Print(out);
}
}
auto LocId::Print(llvm::raw_ostream& out) const -> void {
switch (kind()) {
case Kind::None:
IdBase::Print(out);
break;
case Kind::ImportIRInstId:
out << Label << "_" << import_ir_inst_id();
break;
case Kind::InstId:
out << Label << "_" << inst_id();
break;
case Kind::NodeId:
out << Label << "_" << node_id();
break;
}
}
} // namespace Carbon::SemIR