Lowering support for overloaded operators. (#3798)

Fix a collection of issues that were preventing lowering for overloaded
operators from working.

Instead of creating `import_ref` instructions during name lookup in the
current block, whatever that might be, we now create them in the `file`
block always. This avoids inserting them into blocks that might not be
intended to contain them, such as functions, and avoids the IR generated
for a function depending on which names we happen to have looked up
first.

When importing a class, function, or interface, import its enclosing
scope ID. This is necessary to allow us to distinguish between functions
at interface scope, which shouldn't be lowered, and other functions, and
will also be used in future to provide qualified names for declarations
when printing types. In order to support this:

- Track the constant values of namespaces created during importing so
that we can find them when resolving an import ref. Use those constant
values to convert an enclosing scope ID from the imported IR into a
corresponding ID in the current IR.
- Change how we do two-pass import of classes and namespaces so that we
can do two-pass import even for non-defining declarations, so that we
can import the enclosing scope.

While working on the final point above, I reworked `TryResolveInst` to
return a flag indicating whether another pass is necessary instead of
implicitly encoding this in the `ConstantId`. This permits the handling
of classes to be simplified; now `import_ir_constant_values` is only
accessed in a single place.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
Richard Smith
2024-03-20 23:32:44 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins
parent f97a543395
commit d8be774b8b
35 changed files with 949 additions and 801 deletions
+18 -2
View File
@@ -134,6 +134,23 @@ auto Context::ReplaceInstBeforeConstantUse(
constant_values().Set(inst_id, const_id);
}
auto Context::AddImportRef(SemIR::ImportIRId ir_id, SemIR::InstId inst_id)
-> SemIR::InstId {
auto import_ref_id =
AddPlaceholderInstInNoBlock(SemIR::ImportRefUnused{ir_id, inst_id});
// We can't insert this instruction into whatever block we happen to be in,
// because this function is typically called by name lookup in the middle of
// an otherwise unknown checking step. But we need to add the instruction
// somewhere, because it's referenced by other instructions and needs to be
// visible in textual IR. Adding it to the file block is arbitrary but is the
// best place we have right now.
//
// TODO: Consider adding a dedicated block for import_refs.
inst_block_stack().AddInstIdToFileBlock(import_ref_id);
return import_ref_id;
}
auto Context::DiagnoseDuplicateName(SemIRLocation dup_def,
SemIRLocation prev_def) -> void {
CARBON_DIAGNOSTIC(NameDeclDuplicate, Error,
@@ -301,8 +318,7 @@ static auto LookupInImportIRScopes(Context& context, SemIRLocation loc,
// Name doesn't exist in the import scope.
continue;
}
auto import_inst_id = context.AddPlaceholderInst(
{SemIR::ImportRefUnused{import_ir_id, it->second}});
auto import_inst_id = context.AddImportRef(import_ir_id, it->second);
TryResolveImportRefUnused(context, import_inst_id);
if (result_id.is_valid()) {
// TODO: Add generalized merge functionality (merge_decls.h?).