mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 18:41:08 +01:00
This adds support for choice types at a similar level to that of a C enum, where each alternative has a name but no additional data/parameters attached to it. We generate a TODO diagnostic if parameters are specified. Because there's no extra data, the storage is a simple unsigned integer discriminant of the smallest possible size. A choice without any alternatives is not constructible. A choice with a single alternative is, and has an empty tuple in place of a discriminant since it has only one state. The empty tuple is used to make the class non-constructible. This can be improved. Each alternative is turned into a let binding on the choice that is a value of the choice with that alternative set as the active one in the discriminant. This isn't possible to write in user code with a class right now, since the let binding has the same type as the choice (which is a class) it is within. It's possible to generate it in semir however by adding the binding after the class is marked complete.
59 lines
1.9 KiB
C++
59 lines
1.9 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 {
|
|
switch (name_id.index) {
|
|
case NameId::None.index:
|
|
return for_ir ? "" : "<none>";
|
|
case NameId::Base.index:
|
|
return "base";
|
|
case NameId::Core.index:
|
|
return "Core";
|
|
case NameId::PeriodSelf.index:
|
|
return ".Self";
|
|
case NameId::ReturnSlot.index:
|
|
return for_ir ? "return" : "<return slot>";
|
|
case NameId::PackageNamespace.index:
|
|
return "package";
|
|
case NameId::SelfType.index:
|
|
return "Self";
|
|
case NameId::SelfValue.index:
|
|
return "self";
|
|
case NameId::Vptr.index:
|
|
return for_ir ? "vptr" : "<vptr>";
|
|
case NameId::ChoiceDiscriminant.index:
|
|
return "discriminant";
|
|
default:
|
|
CARBON_FATAL("Unknown special name");
|
|
}
|
|
}
|
|
|
|
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
|