Formalize Cpp as a PackageNameId (#6306)

This turns `Cpp` into a keyword, and makes it map to `NameId::Cpp` and
`PackageNameId::Cpp`.

Per discussion with zygoloid, the keyword versus identifier question is
deliberately kept open by #4846. This PR switches to a keyword because
mapping to a specific `PackageNameId` works best with a special `NameId`
not backed by an `IdentifierId`. We could in theory make it work using
`IdentifierId` or a runtime-tracked `PackageNameId` for `Cpp` (e.g.
stored on `SemIR::File`), but this approach is consistent with `Core`
and so seemed like a good starting point.

---------

Co-authored-by: Dana Jansens <danakj@orodu.net>
This commit is contained in:
Jon Ross-Perkins
2025-11-05 17:21:41 +00:00
committed by GitHub
co-authored by Dana Jansens
parent ae50e0f623
commit 8166f9a7cf
22 changed files with 162 additions and 100 deletions
@@ -48,7 +48,7 @@ static auto HandleDeclContent(Context& context, Context::State state,
// This is either `library ...` or `import library ...`, so no package name
// is expected.
} else {
// We require a package name. This is either an identifier or the `Core`
// We require a package name. This is either an identifier or package name
// keyword.
auto package_name_position = *context.position();
if (auto ident = context.ConsumeIf(Lex::TokenKind::Identifier)) {
@@ -56,9 +56,11 @@ static auto HandleDeclContent(Context& context, Context::State state,
PackageNameId::ForIdentifier(context.tokens().GetIdentifier(*ident));
context.AddLeafNode(NodeKind::IdentifierPackageName, *ident);
} else if (auto core = context.ConsumeIf(Lex::TokenKind::Core)) {
// TODO: Model `Cpp` as a keyword too.
names.package_id = PackageNameId::Core;
context.AddLeafNode(NodeKind::CorePackageName, *core);
} else if (auto cpp = context.ConsumeIf(Lex::TokenKind::Cpp)) {
names.package_id = PackageNameId::Cpp;
context.AddLeafNode(NodeKind::CppPackageName, *cpp);
} else {
CARBON_DIAGNOSTIC(ExpectedIdentifierAfterPackage, Error,
"expected identifier after `package`");