mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:10:12 +01:00
Construct LocId from InstId directly (explicitly) instead of doing lookups when possible (#5355)
Remove calls to `InstStore::GetLocId()` to build a LocId from an InstId now that they can be constructed directly from the InstId. Most uses of LocId are just plumbing, so this does not affect them. However places that want to look inside the LocId do not want to work with the InstId form. In these places, introduce `InstStore::GetResolvedLocId()` which converts a LocId (or an InstId as an optimization) into a LocId which is not backed by an InstId. These locations can be printed (they have a line and column when they are a NodeId), they can have flags added to them (`ToImplicit`, `ToTokenOnly`), they can be converted to an underlying ImportIRInstId, or they may be `None`. `Dump()` is made to print a resolved location instead of printing the InstId in the location, since (at least in my experience) the resolved location is what is interesting in debugging, and this saves manual `MakeInstId` steps in the debugger every time a location is of interest. The LocId constructor from InstId is made `explicit` to add clarity to function calls passing an `inst_id` now directly instead of calling `context.insts().GetLocId(inst_id)`. To avoid needing to construct `SemIR::LocId(...)` explicitly in all cases though, the diagnostics code in Check uses `DiagnosticLocId` as its template parameter which accepts InstId as well and does the construction of LocId from it. Because LocId now requires an explicit construction from InstId, any callers to `AddInst()` functions will have to explicitly convert to LocId if they had an InstId, but not if they pass a NodeId. To make this difference clear to callers, we `requires` that the input type can be converted to LocId. This ensures that passing an InstId results in an error at the callsite where the InstId is passed, instead of generating a compiler error when trying to construct `LocIdAndInst` inside `AddInst()`, which is less clear about what went wrong and doesn't seem entirely intentional. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
co-authored by
Jon Ross-Perkins
parent
fafb655d39
commit
315e206ff1
@@ -165,7 +165,7 @@ static auto MergeFunctionRedecl(Context& context,
|
||||
DiagnoseIfInvalidRedecl(
|
||||
context, Lex::TokenKind::Fn, prev_function.name_id,
|
||||
RedeclInfo(new_function, node_id, new_is_definition),
|
||||
RedeclInfo(prev_function, prev_function.latest_decl_id(),
|
||||
RedeclInfo(prev_function, SemIR::LocId(prev_function.latest_decl_id()),
|
||||
prev_function.has_definition_started()),
|
||||
prev_import_ir_id);
|
||||
if (new_is_definition && prev_function.has_definition_started()) {
|
||||
@@ -246,7 +246,7 @@ static auto TryMergeRedecl(Context& context, Parse::AnyFunctionDeclId node_id,
|
||||
|
||||
if (!prev_function_id.has_value()) {
|
||||
DiagnoseDuplicateName(context, name_context.name_id, name_context.loc_id,
|
||||
prev_id);
|
||||
SemIR::LocId(prev_id));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -553,7 +553,8 @@ static auto BuildFunctionDecl(Context& context,
|
||||
SemIR::Function::VirtualModifier::Abstract) {
|
||||
CARBON_DIAGNOSTIC(DefinedAbstractFunction, Error,
|
||||
"definition of `abstract` function");
|
||||
context.emitter().Emit(TokenOnly(node_id), DefinedAbstractFunction);
|
||||
context.emitter().Emit(SemIR::LocId(node_id).ToTokenOnly(),
|
||||
DefinedAbstractFunction);
|
||||
}
|
||||
|
||||
// Add to name lookup if needed, now that the decl is built.
|
||||
@@ -584,9 +585,9 @@ static auto CheckFunctionDefinitionSignature(Context& context,
|
||||
|
||||
// Check the return type is complete.
|
||||
if (function.return_slot_pattern_id.has_value()) {
|
||||
CheckFunctionReturnType(
|
||||
context, context.insts().GetLocId(function.return_slot_pattern_id),
|
||||
function, SemIR::SpecificId::None);
|
||||
CheckFunctionReturnType(context,
|
||||
SemIR::LocId(function.return_slot_pattern_id),
|
||||
function, SemIR::SpecificId::None);
|
||||
params_to_complete = params_to_complete.drop_back();
|
||||
}
|
||||
|
||||
@@ -599,7 +600,7 @@ static auto CheckFunctionDefinitionSignature(Context& context,
|
||||
// The parameter types need to be complete.
|
||||
RequireCompleteType(
|
||||
context, context.insts().GetAs<SemIR::AnyParam>(param_ref_id).type_id,
|
||||
context.insts().GetLocId(param_ref_id), [&] {
|
||||
SemIR::LocId(param_ref_id), [&] {
|
||||
CARBON_DIAGNOSTIC(
|
||||
IncompleteTypeInFunctionParam, Error,
|
||||
"parameter has incomplete type {0} in function definition",
|
||||
@@ -672,7 +673,8 @@ auto HandleParseNode(Context& context, Parse::FunctionDefinitionId node_id)
|
||||
CARBON_DIAGNOSTIC(
|
||||
MissingReturnStatement, Error,
|
||||
"missing `return` at end of function with declared return type");
|
||||
context.emitter().Emit(TokenOnly(node_id), MissingReturnStatement);
|
||||
context.emitter().Emit(SemIR::LocId(node_id).ToTokenOnly(),
|
||||
MissingReturnStatement);
|
||||
} else {
|
||||
AddReturnCleanupBlock(context, node_id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user