mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
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:
co-authored by
Jon Ross-Perkins
parent
f97a543395
commit
d8be774b8b
@@ -159,6 +159,15 @@ struct UnitInfo {
|
||||
// Add imports to the root block.
|
||||
static auto InitPackageScopeAndImports(Context& context, UnitInfo& unit_info)
|
||||
-> void {
|
||||
// First create the constant values map for all imported IRs. We'll populate
|
||||
// these with mappings for namespaces as we go.
|
||||
size_t num_irs = context.import_irs().size();
|
||||
for (auto& [_, package_imports] : unit_info.package_imports_map) {
|
||||
num_irs += package_imports.imports.size();
|
||||
}
|
||||
context.import_ir_constant_values().resize(
|
||||
num_irs, SemIR::ConstantValueStore(SemIR::ConstantId::Invalid));
|
||||
|
||||
// Importing makes many namespaces, so only canonicalize the type once.
|
||||
auto namespace_type_id =
|
||||
context.GetBuiltinType(SemIR::BuiltinKind::NamespaceType);
|
||||
@@ -217,9 +226,8 @@ static auto InitPackageScopeAndImports(Context& context, UnitInfo& unit_info)
|
||||
sem_irs, package_imports.has_load_error);
|
||||
}
|
||||
|
||||
context.import_ir_constant_values().resize(
|
||||
context.import_irs().size(),
|
||||
SemIR::ConstantValueStore(SemIR::ConstantId::Invalid));
|
||||
CARBON_CHECK(context.import_irs().size() == num_irs)
|
||||
<< "Created an unexpected number of IRs";
|
||||
}
|
||||
|
||||
// Loops over all nodes in the tree. On some errors, this may return early,
|
||||
|
||||
Reference in New Issue
Block a user