mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 12:01:05 +01:00
Move decl-specific merge logic back to respective handlers. (#3937)
I'd split out the decl-specific handlers so that import_ref.cpp could also call them. However, with the current model for imports and constants, we shouldn't use them there. As a consequence, merge them back into individual files for greater consistency with other helper functions (and better visibility when making related code changes).
This commit is contained in:
@@ -61,6 +61,78 @@ static auto DiagnoseModifiers(Context& context, bool is_definition,
|
||||
return context.decl_state_stack().innermost().modifier_set;
|
||||
}
|
||||
|
||||
// Returns the return slot usage for a function given the computed usage for two
|
||||
// different declarations of the function.
|
||||
static auto MergeReturnSlot(SemIR::Function::ReturnSlot a,
|
||||
SemIR::Function::ReturnSlot b)
|
||||
-> SemIR::Function::ReturnSlot {
|
||||
if (a == SemIR::Function::ReturnSlot::NotComputed) {
|
||||
return b;
|
||||
}
|
||||
if (b == SemIR::Function::ReturnSlot::NotComputed) {
|
||||
return a;
|
||||
}
|
||||
if (a == SemIR::Function::ReturnSlot::Error) {
|
||||
return b;
|
||||
}
|
||||
if (b == SemIR::Function::ReturnSlot::Error) {
|
||||
return a;
|
||||
}
|
||||
CARBON_CHECK(a == b)
|
||||
<< "Different return slot usage computed for the same function.";
|
||||
return a;
|
||||
}
|
||||
|
||||
// Tries to merge new_function into prev_function_id. Since new_function won't
|
||||
// have a definition even if one is upcoming, set is_definition to indicate the
|
||||
// planned result.
|
||||
//
|
||||
// If merging is successful, returns true and may update the previous function.
|
||||
// Otherwise, returns false. Prints a diagnostic when appropriate.
|
||||
static auto MergeFunctionRedecl(Context& context, SemIRLoc new_loc,
|
||||
SemIR::Function& new_function,
|
||||
bool new_is_import, bool new_is_definition,
|
||||
SemIR::FunctionId prev_function_id,
|
||||
SemIR::ImportIRId prev_import_ir_id) -> bool {
|
||||
auto& prev_function = context.functions().Get(prev_function_id);
|
||||
|
||||
if (!CheckFunctionTypeMatches(context, new_function, prev_function, {})) {
|
||||
return false;
|
||||
}
|
||||
|
||||
CheckIsAllowedRedecl(context, Lex::TokenKind::Fn, prev_function.name_id,
|
||||
{.loc = new_loc,
|
||||
.is_definition = new_is_definition,
|
||||
.is_extern = new_function.is_extern},
|
||||
{.loc = prev_function.definition_id.is_valid()
|
||||
? prev_function.definition_id
|
||||
: prev_function.decl_id,
|
||||
.is_definition = prev_function.definition_id.is_valid(),
|
||||
.is_extern = prev_function.is_extern},
|
||||
prev_import_ir_id);
|
||||
|
||||
if (new_is_definition) {
|
||||
// Track the signature from the definition, so that IDs in the body
|
||||
// match IDs in the signature.
|
||||
prev_function.definition_id = new_function.definition_id;
|
||||
prev_function.implicit_param_refs_id = new_function.implicit_param_refs_id;
|
||||
prev_function.param_refs_id = new_function.param_refs_id;
|
||||
prev_function.return_type_id = new_function.return_type_id;
|
||||
prev_function.return_storage_id = new_function.return_storage_id;
|
||||
}
|
||||
// The new function might have return slot information if it was imported.
|
||||
prev_function.return_slot =
|
||||
MergeReturnSlot(prev_function.return_slot, new_function.return_slot);
|
||||
if ((prev_import_ir_id.is_valid() && !new_is_import) ||
|
||||
(prev_function.is_extern && !new_function.is_extern)) {
|
||||
prev_function.is_extern = new_function.is_extern;
|
||||
prev_function.decl_id = new_function.decl_id;
|
||||
ReplacePrevInstForMerge(context, prev_function.enclosing_scope_id,
|
||||
prev_function.name_id, new_function.decl_id);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check whether this is a redeclaration, merging if needed.
|
||||
static auto TryMergeRedecl(Context& context, Parse::AnyFunctionDeclId node_id,
|
||||
SemIR::InstId prev_id,
|
||||
|
||||
Reference in New Issue
Block a user