Replace NodeId with a hybrid LocationId in SemIR diagnostics. (#3810)

The purpose of this change is to allow something such as a FunctionDecl
instruction to note an imported instruction as the "loc_id". Note that
doesn't occur here: this change is already very sweeping in edits. There
is no testdata affected, intended to show equivalent behavior.

We might want to consolidate NodeId references towards LocationId, but
if that's preferred, I'd still like to split it out. A lot of this just
piping through LocationId where it's a build error otherwise, enough
that imports should be able to start using it for diagnostics.
ValueStores are added but still unused -- just flushing out structure
for review.

Restructuring SemIRLocation is necessary to use LocationId this way. For
TokenOnly, it's not getting used in Parse, so I migrated it to Check and
it's now specific to SemIRLocation.

I also considered making LocationId reference an InstId (which would
need to be an ImportRef) instead of an ImportIRInstId. However, that
would've required import.cpp to add instructions for decls which are
reached during resolution -- we typically don't have an inst ready for
use. An extra inst is essentially 16 bytes in InstId's ValueStore + 4
bytes in LocationId's ValueStore, whereas this is 8 bytes per.
This commit is contained in:
Jon Ross-Perkins
2024-03-27 22:55:22 +00:00
committed by GitHub
parent e8cc089b2b
commit b079acd86f
27 changed files with 395 additions and 262 deletions
+18 -18
View File
@@ -15,10 +15,10 @@ auto DeclNameStack::MakeEmptyNameContext() -> NameContext {
.target_scope_id = context_->scope_stack().PeekNameScopeId()};
}
auto DeclNameStack::MakeUnqualifiedName(Parse::NodeId node_id,
auto DeclNameStack::MakeUnqualifiedName(SemIR::LocationId loc_id,
SemIR::NameId name_id) -> NameContext {
NameContext context = MakeEmptyNameContext();
ApplyNameQualifierTo(context, node_id, name_id, /*is_unqualified=*/true);
ApplyNameQualifierTo(context, loc_id, name_id, /*is_unqualified=*/true);
return context;
}
@@ -38,8 +38,8 @@ auto DeclNameStack::FinishName() -> NameContext {
// into the name.
} else {
// The name had no qualifiers, so we need to process the node now.
auto [node_id, name_id] = context_->node_stack().PopNameWithNodeId();
ApplyNameQualifier(node_id, name_id);
auto [loc_id, name_id] = context_->node_stack().PopNameWithNodeId();
ApplyNameQualifier(loc_id, name_id);
}
NameContext result = decl_name_stack_.back();
@@ -92,7 +92,7 @@ auto DeclNameStack::LookupOrAddName(NameContext name_context,
QualifiedDeclOutsideScopeEntity, Error,
"Out-of-line declaration requires a declaration in "
"scoped entity.");
context_->emitter().Emit(name_context.node_id,
context_->emitter().Emit(name_context.loc_id,
QualifiedDeclOutsideScopeEntity);
}
}
@@ -125,20 +125,20 @@ auto DeclNameStack::AddNameToLookup(NameContext name_context,
}
}
auto DeclNameStack::ApplyNameQualifier(Parse::NodeId node_id,
auto DeclNameStack::ApplyNameQualifier(SemIR::LocationId loc_id,
SemIR::NameId name_id) -> void {
ApplyNameQualifierTo(decl_name_stack_.back(), node_id, name_id,
ApplyNameQualifierTo(decl_name_stack_.back(), loc_id, name_id,
/*is_unqualified=*/false);
}
auto DeclNameStack::ApplyNameQualifierTo(NameContext& name_context,
Parse::NodeId node_id,
SemIR::LocationId loc_id,
SemIR::NameId name_id,
bool is_unqualified) -> void {
if (TryResolveQualifier(name_context, node_id)) {
if (TryResolveQualifier(name_context, loc_id)) {
// For identifier nodes, we need to perform a lookup on the identifier.
auto resolved_inst_id = context_->LookupNameInDecl(
name_context.node_id, name_id, name_context.target_scope_id);
name_context.loc_id, name_id, name_context.target_scope_id);
if (!resolved_inst_id.is_valid()) {
// Invalid indicates an unresolved name. Store it and return.
name_context.state = NameContext::State::Unresolved;
@@ -221,7 +221,7 @@ auto DeclNameStack::UpdateScopeIfNeeded(NameContext& name_context,
CARBON_DIAGNOSTIC(QualifiedDeclOutsidePackageSource, Note,
"Package imported here.");
context_->emitter()
.Build(name_context.node_id, QualifiedDeclOutsidePackage)
.Build(name_context.loc_id, QualifiedDeclOutsidePackage)
.Note(scope.inst_id, QualifiedDeclOutsidePackageSource)
.Emit();
// Only error once per package.
@@ -241,7 +241,7 @@ auto DeclNameStack::UpdateScopeIfNeeded(NameContext& name_context,
}
auto DeclNameStack::TryResolveQualifier(NameContext& name_context,
Parse::NodeId node_id) -> bool {
SemIR::LocationId loc_id) -> bool {
// Update has_qualifiers based on the state before any possible changes. If
// this is the first qualifier, it may just be the name.
name_context.has_qualifiers = name_context.state != NameContext::State::Empty;
@@ -255,7 +255,7 @@ auto DeclNameStack::TryResolveQualifier(NameContext& name_context,
// Because more qualifiers were found, we diagnose that the earlier
// qualifier failed to resolve.
name_context.state = NameContext::State::Error;
context_->DiagnoseNameNotFound(name_context.node_id,
context_->DiagnoseNameNotFound(name_context.loc_id,
name_context.unresolved_name_id);
return false;
@@ -268,7 +268,7 @@ auto DeclNameStack::TryResolveQualifier(NameContext& name_context,
"Cannot declare a member of incomplete class `{0}`.",
SemIR::TypeId);
auto builder = context_->emitter().Build(
name_context.node_id, QualifiedDeclInIncompleteClassScope,
name_context.loc_id, QualifiedDeclInIncompleteClassScope,
context_->classes().Get(class_decl->class_id).self_type_id);
context_->NoteIncompleteClass(class_decl->class_id, builder);
builder.Emit();
@@ -280,7 +280,7 @@ auto DeclNameStack::TryResolveQualifier(NameContext& name_context,
"Cannot declare a member of undefined interface `{0}`.",
std::string);
auto builder = context_->emitter().Build(
name_context.node_id, QualifiedDeclInUndefinedInterfaceScope,
name_context.loc_id, QualifiedDeclInUndefinedInterfaceScope,
context_->sem_ir().StringifyTypeExpr(
context_->sem_ir()
.constant_values()
@@ -295,8 +295,8 @@ auto DeclNameStack::TryResolveQualifier(NameContext& name_context,
CARBON_DIAGNOSTIC(QualifiedNameNonScopeEntity, Note,
"Non-scope entity referenced here.");
context_->emitter()
.Build(node_id, QualifiedNameInNonScope)
.Note(name_context.node_id, QualifiedNameNonScopeEntity)
.Build(loc_id, QualifiedNameInNonScope)
.Note(name_context.loc_id, QualifiedNameNonScopeEntity)
.Emit();
}
name_context.state = NameContext::State::Error;
@@ -305,7 +305,7 @@ auto DeclNameStack::TryResolveQualifier(NameContext& name_context,
case NameContext::State::Empty:
case NameContext::State::Resolved: {
name_context.node_id = node_id;
name_context.loc_id = loc_id;
return true;
}