Implement #4864: Core is a keyword (#4909)

Change representation of package names from `IdentifierId` to
`PackageNameId`, and add a special value `PackageNameId::Core` for the
Core package. Add a `Core` expression to name the Core package, and
support for parsing the `Core` keyword in `package` and `import`
declarations.

For now, I've made no changes to instruction fingerprinting or name
mangling. This means that fingerprints and mangled names will collide
between names in the `Core` package and names in a `r#Core` package. See
#4908.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
Richard Smith
2025-02-11 21:33:57 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins
parent 50b3c825e4
commit 8eb4e24cb6
62 changed files with 935 additions and 487 deletions
+17 -11
View File
@@ -113,7 +113,7 @@ auto CheckUnit::InitPackageScopeAndImports() -> void {
const auto& names = context_.parse_tree().packaging_decl()->names;
auto import_decl_id = context_.AddInst<SemIR::ImportDecl>(
names.node_id,
{.package_id = SemIR::NameId::ForIdentifier(names.package_id)});
{.package_id = SemIR::NameId::ForPackageName(names.package_id)});
SetApiImportIR(context_,
{.decl_id = import_decl_id,
.is_export = false,
@@ -128,7 +128,7 @@ auto CheckUnit::InitPackageScopeAndImports() -> void {
for (auto& package_imports : unit_and_imports_->package_imports) {
CARBON_CHECK(!package_imports.import_decl_id.has_value());
package_imports.import_decl_id = context_.AddInst<SemIR::ImportDecl>(
package_imports.node_id, {.package_id = SemIR::NameId::ForIdentifier(
package_imports.node_id, {.package_id = SemIR::NameId::ForPackageName(
package_imports.package_id)});
}
@@ -223,7 +223,7 @@ auto CheckUnit::ImportCurrentPackage(SemIR::InstId package_inst_id,
SemIR::TypeId namespace_type_id) -> void {
// Add imports from the current package.
auto import_map_lookup =
unit_and_imports_->package_imports_map.Lookup(IdentifierId::None);
unit_and_imports_->package_imports_map.Lookup(PackageNameId::None);
if (!import_map_lookup) {
// Push the scope; there are no names to add.
context_.scope_stack().Push(package_inst_id, SemIR::NameScopeId::Package);
@@ -252,12 +252,12 @@ auto CheckUnit::ImportOtherPackages(SemIR::TypeId namespace_type_id) -> void {
// when processing an implementation file, in order to combine the API file
// imports.
//
// For packages imported by the API file, the IdentifierId is the package name
// and the index is into the API's import list. Otherwise, the initial
// For packages imported by the API file, the PackageNameId is the package
// name and the index is into the API's import list. Otherwise, the initial
// {None, -1} state remains.
llvm::SmallVector<std::pair<IdentifierId, int32_t>> api_imports_list;
llvm::SmallVector<std::pair<PackageNameId, int32_t>> api_imports_list;
api_imports_list.resize(unit_and_imports_->package_imports.size(),
{IdentifierId::None, -1});
{PackageNameId::None, -1});
// When there's an API file, add the mapping to api_imports_list.
if (unit_and_imports_->api_for_impl) {
@@ -272,9 +272,15 @@ auto CheckUnit::ImportOtherPackages(SemIR::TypeId namespace_type_id) -> void {
if (!api_imports.package_id.has_value()) {
continue;
}
// Translate the package ID from the API file to the implementation file.
auto impl_package_id =
impl_identifiers.Add(api_identifiers.Get(api_imports.package_id));
auto impl_package_id = api_imports.package_id;
if (auto package_identifier_id = impl_package_id.AsIdentifierId();
package_identifier_id.has_value()) {
impl_package_id = PackageNameId::ForIdentifier(
impl_identifiers.Add(api_identifiers.Get(package_identifier_id)));
}
if (auto lookup =
unit_and_imports_->package_imports_map.Lookup(impl_package_id)) {
// On a hit, replace the entry to unify the API and implementation
@@ -290,7 +296,7 @@ auto CheckUnit::ImportOtherPackages(SemIR::TypeId namespace_type_id) -> void {
for (auto [i, api_imports_entry] : llvm::enumerate(api_imports_list)) {
// These variables are updated after figuring out which imports are present.
auto import_decl_id = SemIR::InstId::None;
IdentifierId package_id = IdentifierId::None;
PackageNameId package_id = PackageNameId::None;
bool has_load_error = false;
// Identify the local package imports if present.
@@ -321,7 +327,7 @@ auto CheckUnit::ImportOtherPackages(SemIR::TypeId namespace_type_id) -> void {
.inst_id = api_imports->import_decl_id});
import_decl_id =
context_.AddInst(context_.MakeImportedLocAndInst<SemIR::ImportDecl>(
import_ir_inst_id, {.package_id = SemIR::NameId::ForIdentifier(
import_ir_inst_id, {.package_id = SemIR::NameId::ForPackageName(
api_imports_entry.first)}));
package_id = api_imports_entry.first;
}