Files
carbon-lang/toolchain/sem_ir/name.cpp
T
Jon Ross-Perkins 9d85b23b4b Use an x-macro for special NameId values. (#5018)
Doing an in-file X-macro, though maybe we'll want to move it out to a
#include if we keep piling on more. I know there's `destroy` to add, and
possibly `copy` and `move`, but I don't know what threshold we'll want
for a separate file.

Note this does set up for an advantage where we can `switch` instead of
repeated `if` for special names, shifting to compile errors for new
values.

I also considered a simpler enum approach (with an implicit conversion
to `NameId`) but that runs into issues with things like
`Id::Kind::For<...>` as a consequence of calls like `name_id ==
special_name_id` (just requires another `operator==`) and
`context.node_stack().Push(node_id, SemIR::NameId::SelfType);` (a little
more complex how we'd want to handle it).
2025-02-26 02:23:22 +00:00

61 lines
2.0 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/name.h"
#include "llvm/ADT/StringSwitch.h"
namespace Carbon::SemIR {
// Get the spelling to use for a special name.
static auto GetSpecialName(NameId name_id, bool for_ir) -> llvm::StringRef {
if (name_id == NameId::None) {
return for_ir ? "" : "<none>";
}
auto special_name_id = name_id.AsSpecialNameId();
CARBON_CHECK(special_name_id, "Not a special name");
switch (*special_name_id) {
case NameId::SpecialNameId::Base:
return "base";
case NameId::SpecialNameId::ChoiceDiscriminant:
return "discriminant";
case NameId::SpecialNameId::Core:
return "Core";
case NameId::SpecialNameId::PackageNamespace:
return "package";
case NameId::SpecialNameId::PeriodSelf:
return ".Self";
case NameId::SpecialNameId::ReturnSlot:
return for_ir ? "return" : "<return slot>";
case NameId::SpecialNameId::SelfType:
return "Self";
case NameId::SpecialNameId::SelfValue:
return "self";
case NameId::SpecialNameId::Vptr:
return for_ir ? "vptr" : "<vptr>";
}
}
auto NameStoreWrapper::GetFormatted(NameId name_id) const -> llvm::StringRef {
// If the name is an identifier name with a keyword spelling, format it with
// an `r#` prefix. Format any other identifier name as just the identifier.
if (auto string_name = GetAsStringIfIdentifier(name_id)) {
return llvm::StringSwitch<llvm::StringRef>(*string_name)
#define CARBON_KEYWORD_TOKEN(Name, Spelling) .Case(Spelling, "r#" Spelling)
#include "toolchain/lex/token_kind.def"
.Default(*string_name);
}
return GetSpecialName(name_id, /*for_ir=*/false);
}
auto NameStoreWrapper::GetIRBaseName(NameId name_id) const -> llvm::StringRef {
if (auto string_name = GetAsStringIfIdentifier(name_id)) {
return *string_name;
}
return GetSpecialName(name_id, /*for_ir=*/true);
}
} // namespace Carbon::SemIR