Move type functions off Context (#4951)

This creates a new check/type.h for most logic, and also moves some
functions to TypeStore in sem_ir/type.h. My approach for TypeStore is to
focus on moving the read-only functions there.
This commit is contained in:
Jon Ross-Perkins
2025-02-13 23:02:38 +00:00
committed by GitHub
parent 23e5677c8e
commit dc8f47e6ad
39 changed files with 506 additions and 393 deletions
+96 -66
View File
@@ -10,6 +10,7 @@
#include "toolchain/check/eval.h"
#include "toolchain/check/generic.h"
#include "toolchain/check/name_lookup.h"
#include "toolchain/check/type.h"
#include "toolchain/parse/node_ids.h"
#include "toolchain/sem_ir/constant.h"
#include "toolchain/sem_ir/file.h"
@@ -520,9 +521,9 @@ class ImportRefResolver : public ImportContext {
import_ir().constant_values().GetInstId(import_type_const_id);
SemIR::IsSingletonInstId(import_type_inst_id)) {
// Builtins don't require constant resolution; we can use them directly.
return local_context().GetSingletonType(import_type_inst_id);
return GetSingletonType(local_context(), import_type_inst_id);
} else {
return local_context().GetTypeIdForTypeConstant(
return local_context().types().GetTypeIdForTypeConstantId(
ResolveConstant(import_type_id.AsConstantId()));
}
}
@@ -1023,7 +1024,7 @@ static auto GetLocalParamPatternsId(ImportContext& context,
auto entity_name =
context.import_entity_names().Get(binding.entity_name_id);
auto name_id = GetLocalNameId(context, entity_name.name_id);
auto type_id = context.local_context().GetTypeIdForTypeConstant(
auto type_id = context.local_context().types().GetTypeIdForTypeConstantId(
GetLocalConstantIdChecked(context, binding.type_id));
auto new_param_id = SemIR::InstId::None;
@@ -1062,7 +1063,7 @@ static auto GetLocalParamPatternsId(ImportContext& context,
.subpattern_id = new_param_id,
.runtime_index = param_pattern.runtime_index}));
if (addr_inst) {
type_id = context.local_context().GetTypeIdForTypeConstant(
type_id = context.local_context().types().GetTypeIdForTypeConstantId(
GetLocalConstantIdChecked(context, addr_inst->type_id));
new_param_id = context.local_context().AddInstInNoBlock(
context.local_context().MakeImportedLocAndInst<SemIR::AddrPattern>(
@@ -1093,7 +1094,7 @@ static auto GetLocalReturnSlotPatternId(
auto return_slot_pattern =
context.import_insts().GetAs<SemIR::ReturnSlotPattern>(
param_pattern.subpattern_id);
auto type_id = context.local_context().GetTypeIdForTypeConstant(
auto type_id = context.local_context().types().GetTypeIdForTypeConstantId(
GetLocalConstantIdChecked(context, return_slot_pattern.type_id));
auto new_return_slot_pattern_id = context.local_context().AddInstInNoBlock(
@@ -1357,7 +1358,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
}
auto element_type_id =
resolver.local_context().GetTypeIdForTypeConstant(element_type_const_id);
resolver.local_context().types().GetTypeIdForTypeConstantId(
element_type_const_id);
return ResolveAs<SemIR::ArrayType>(
resolver, {.type_id = SemIR::TypeType::SingletonTypeId,
.bound_id = bound_id,
@@ -1412,8 +1414,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
}
// In the second phase, create the associated constant and its declaration.
auto type_id =
resolver.local_context().GetTypeIdForTypeConstant(type_const_id);
auto type_id = resolver.local_context().types().GetTypeIdForTypeConstantId(
type_const_id);
std::tie(assoc_const_id, const_id) =
MakeAssociatedConstant(resolver, import_assoc_const, type_id);
} else {
@@ -1456,10 +1458,11 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
auto decl_id = AddImportRef(resolver, inst.decl_id);
return ResolveAs<SemIR::AssociatedEntity>(
resolver, {.type_id = resolver.local_context().GetTypeIdForTypeConstant(
type_const_id),
.index = inst.index,
.decl_id = decl_id});
resolver,
{.type_id = resolver.local_context().types().GetTypeIdForTypeConstantId(
type_const_id),
.index = inst.index,
.decl_id = decl_id});
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
@@ -1475,8 +1478,9 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
return ResolveAs<SemIR::AssociatedEntityType>(
resolver,
{.type_id = SemIR::TypeType::SingletonTypeId,
.interface_type_id = resolver.local_context().GetTypeIdForTypeConstant(
interface_inst_id)});
.interface_type_id =
resolver.local_context().types().GetTypeIdForTypeConstantId(
interface_inst_id)});
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
@@ -1498,7 +1502,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
resolver.local_context().MakeImportedLocAndInst<SemIR::BaseDecl>(
AddImportIRInst(resolver, import_inst_id),
{.type_id =
resolver.local_context().GetTypeIdForTypeConstant(type_const_id),
resolver.local_context().types().GetTypeIdForTypeConstantId(
type_const_id),
.base_type_inst_id = base_type_inst_id,
.index = inst.index}));
return ResolveResult::Done(resolver.local_constant_values().Get(inst_id),
@@ -1517,9 +1522,10 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver, SemIR::Vtable inst)
auto virtual_functions_id = GetLocalCanonicalInstBlockId(
resolver, inst.virtual_functions_id, virtual_functions);
return ResolveAs<SemIR::Vtable>(
resolver, {.type_id = resolver.local_context().GetTypeIdForTypeConstant(
type_const_id),
.virtual_functions_id = virtual_functions_id});
resolver,
{.type_id = resolver.local_context().types().GetTypeIdForTypeConstantId(
type_const_id),
.virtual_functions_id = virtual_functions_id});
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
@@ -1545,7 +1551,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
import_entity_name.is_template);
return ResolveAs<SemIR::BindSymbolicName>(
resolver,
{.type_id = resolver.local_context().GetTypeIdForTypeConstant(type_id),
{.type_id =
resolver.local_context().types().GetTypeIdForTypeConstantId(type_id),
.entity_name_id = entity_name_id,
.value_id = SemIR::InstId::None});
}
@@ -1568,7 +1575,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
import_entity_name.is_template);
return ResolveAs<SemIR::SymbolicBindingPattern>(
resolver,
{.type_id = resolver.local_context().GetTypeIdForTypeConstant(type_id),
{.type_id =
resolver.local_context().types().GetTypeIdForTypeConstantId(type_id),
.entity_name_id = entity_name_id});
}
@@ -1585,10 +1593,11 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
}
return ResolveAs<SemIR::BoundMethod>(
resolver, {.type_id = resolver.local_context().GetSingletonType(
SemIR::BoundMethodType::SingletonInstId),
.object_id = object_id,
.function_decl_id = function_decl_id});
resolver,
{.type_id = GetSingletonType(resolver.local_context(),
SemIR::BoundMethodType::SingletonInstId),
.object_id = object_id,
.function_decl_id = function_decl_id});
}
static auto TryResolveTypedInst(ImportRefResolver& resolver, SemIR::Call inst)
@@ -1603,7 +1612,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver, SemIR::Call inst)
return ResolveAs<SemIR::Call>(
resolver,
{.type_id = resolver.local_context().GetTypeIdForTypeConstant(type_id),
{.type_id =
resolver.local_context().types().GetTypeIdForTypeConstantId(type_id),
.callee_id = callee_id,
.args_id = GetLocalCanonicalInstBlockId(resolver, inst.args_id, args)});
}
@@ -1630,8 +1640,8 @@ static auto MakeIncompleteClass(ImportContext& context,
.is_dynamic = import_class.is_dynamic}});
if (import_class.has_parameters()) {
class_decl.type_id = context.local_context().GetGenericClassType(
class_decl.class_id, enclosing_specific_id);
class_decl.type_id = GetGenericClassType(
context.local_context(), class_decl.class_id, enclosing_specific_id);
}
// Write the class ID into the ClassDecl.
@@ -1756,13 +1766,14 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
SetGenericData(resolver, import_class.generic_id, new_class.generic_id,
generic_data);
new_class.self_type_id =
resolver.local_context().GetTypeIdForTypeConstant(self_const_id);
resolver.local_context().types().GetTypeIdForTypeConstantId(
self_const_id);
if (import_class.is_defined()) {
auto complete_type_witness_id = AddLoadedImportRef(
resolver,
resolver.local_context().GetSingletonType(
SemIR::WitnessType::SingletonInstId),
GetSingletonType(resolver.local_context(),
SemIR::WitnessType::SingletonInstId),
import_class.complete_type_witness_id, complete_type_witness_const_id);
AddClassDefinition(resolver, import_class, new_class,
complete_type_witness_id, base_id, adapt_id, vtable_id);
@@ -1812,11 +1823,13 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
return ResolveResult::Retry();
}
auto object_repr_id =
resolver.local_context().GetTypeIdForTypeConstant(object_repr_const_id);
resolver.local_context().types().GetTypeIdForTypeConstantId(
object_repr_const_id);
return ResolveAs<SemIR::CompleteTypeWitness>(
resolver, {.type_id = resolver.local_context().GetSingletonType(
SemIR::WitnessType::SingletonInstId),
.object_repr_id = object_repr_id});
resolver,
{.type_id = GetSingletonType(resolver.local_context(),
SemIR::WitnessType::SingletonInstId),
.object_repr_id = object_repr_id});
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
@@ -1827,7 +1840,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
return ResolveResult::Retry();
}
auto inner_type_id =
resolver.local_context().GetTypeIdForTypeConstant(inner_const_id);
resolver.local_context().types().GetTypeIdForTypeConstantId(
inner_const_id);
return ResolveAs<SemIR::ConstType>(
resolver,
{.type_id = SemIR::TypeType::SingletonTypeId, .inner_id = inner_type_id});
@@ -1850,7 +1864,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
resolver.local_context().MakeImportedLocAndInst<SemIR::FieldDecl>(
AddImportIRInst(resolver, import_inst_id),
{.type_id =
resolver.local_context().GetTypeIdForTypeConstant(const_id),
resolver.local_context().types().GetTypeIdForTypeConstantId(
const_id),
.name_id = GetLocalNameId(resolver, inst.name_id),
.index = inst.index}));
return ResolveResult::Done(resolver.local_constant_values().Get(inst_id),
@@ -1878,8 +1893,8 @@ static auto MakeFunctionDecl(ImportContext& context,
{.return_slot_pattern_id = SemIR::InstId::None,
.builtin_function_kind = import_function.builtin_function_kind}});
function_decl.type_id = context.local_context().GetFunctionType(
function_decl.function_id, specific_id);
function_decl.type_id = GetFunctionType(
context.local_context(), function_decl.function_id, specific_id);
// Write the function ID and type into the FunctionDecl.
context.local_context().ReplaceInstBeforeConstantUse(function_decl_id,
@@ -2199,8 +2214,9 @@ static auto MakeInterfaceDecl(ImportContext& context,
{}});
if (import_interface.has_parameters()) {
interface_decl.type_id = context.local_context().GetGenericInterfaceType(
interface_decl.interface_id, enclosing_specific_id);
interface_decl.type_id = GetGenericInterfaceType(
context.local_context(), interface_decl.interface_id,
enclosing_specific_id);
}
// Write the interface ID into the InterfaceDecl.
@@ -2341,9 +2357,10 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
}
return ResolveAs<SemIR::FacetAccessWitness>(
resolver, {.type_id = resolver.local_context().GetSingletonType(
SemIR::WitnessType::SingletonInstId),
.facet_value_inst_id = facet_value_inst_id});
resolver,
{.type_id = GetSingletonType(resolver.local_context(),
SemIR::WitnessType::SingletonInstId),
.facet_value_inst_id = facet_value_inst_id});
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
@@ -2423,7 +2440,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
return ResolveAs<SemIR::FacetValue>(
resolver,
{.type_id = resolver.local_context().GetTypeIdForTypeConstant(type_id),
{.type_id =
resolver.local_context().types().GetTypeIdForTypeConstantId(type_id),
.type_inst_id = type_inst_id,
.witness_inst_id = witness_inst_id});
}
@@ -2439,10 +2457,11 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
auto specific_id =
GetOrAddLocalSpecific(resolver, inst.specific_id, specific_data);
return ResolveAs<SemIR::ImplWitness>(
resolver, {.type_id = resolver.local_context().GetSingletonType(
SemIR::WitnessType::SingletonInstId),
.elements_id = elements_id,
.specific_id = specific_id});
resolver,
{.type_id = GetSingletonType(resolver.local_context(),
SemIR::WitnessType::SingletonInstId),
.elements_id = elements_id,
.specific_id = specific_id});
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
@@ -2456,7 +2475,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
return ResolveAs<SemIR::ImplWitnessAccess>(
resolver,
{.type_id = resolver.local_context().GetTypeIdForTypeConstant(type_id),
{.type_id =
resolver.local_context().types().GetTypeIdForTypeConstantId(type_id),
.witness_id = witness_id,
.index = inst.index});
}
@@ -2477,7 +2497,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
return ResolveAs<SemIR::IntValue>(
resolver,
{.type_id = resolver.local_context().GetTypeIdForTypeConstant(type_id),
{.type_id =
resolver.local_context().types().GetTypeIdForTypeConstantId(type_id),
.int_id = int_id});
}
@@ -2511,8 +2532,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
return ResolveResult::Retry();
}
auto namespace_type_id = resolver.local_context().GetSingletonType(
SemIR::NamespaceType::SingletonInstId);
auto namespace_type_id = GetSingletonType(
resolver.local_context(), SemIR::NamespaceType::SingletonInstId);
auto namespace_decl =
SemIR::Namespace{.type_id = namespace_type_id,
.name_scope_id = SemIR::NameScopeId::None,
@@ -2543,7 +2564,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
}
auto pointee_type_id =
resolver.local_context().GetTypeIdForTypeConstant(pointee_const_id);
resolver.local_context().types().GetTypeIdForTypeConstantId(
pointee_const_id);
return ResolveAs<SemIR::PointerType>(
resolver, {.type_id = SemIR::TypeType::SingletonTypeId,
.pointee_id = pointee_type_id});
@@ -2562,11 +2584,13 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
}
auto complete_type_id =
resolver.local_context().GetTypeIdForTypeConstant(complete_type_const_id);
resolver.local_context().types().GetTypeIdForTypeConstantId(
complete_type_const_id);
return ResolveAs<SemIR::RequireCompleteType>(
resolver, {.type_id = resolver.local_context().GetSingletonType(
SemIR::WitnessType::SingletonInstId),
.complete_type_id = complete_type_id});
resolver,
{.type_id = GetSingletonType(resolver.local_context(),
SemIR::WitnessType::SingletonInstId),
.complete_type_id = complete_type_id});
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
@@ -2578,8 +2602,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
return ResolveResult::Retry();
}
auto type_id =
resolver.local_context().GetTypeIdForTypeConstant(type_const_id);
auto type_id = resolver.local_context().types().GetTypeIdForTypeConstantId(
type_const_id);
auto specific_id =
GetOrAddLocalSpecific(resolver, inst.specific_id, specific_data);
return ResolveAs<SemIR::SpecificFunction>(
@@ -2607,7 +2631,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
llvm::zip(orig_fields, field_const_ids)) {
auto name_id = GetLocalNameId(resolver, orig_field.name_id);
auto field_type_id =
resolver.local_context().GetTypeIdForTypeConstant(field_const_id);
resolver.local_context().types().GetTypeIdForTypeConstantId(
field_const_id);
new_fields.push_back({.name_id = name_id, .type_id = field_type_id});
}
@@ -2627,7 +2652,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
return ResolveAs<SemIR::StructValue>(
resolver,
{.type_id = resolver.local_context().GetTypeIdForTypeConstant(type_id),
{.type_id =
resolver.local_context().types().GetTypeIdForTypeConstantId(type_id),
.elements_id =
GetLocalCanonicalInstBlockId(resolver, inst.elements_id, elems)});
}
@@ -2651,11 +2677,12 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
elem_type_ids.reserve(orig_elem_type_ids.size());
for (auto elem_const_id : elem_const_ids) {
elem_type_ids.push_back(
resolver.local_context().GetTypeIdForTypeConstant(elem_const_id));
resolver.local_context().types().GetTypeIdForTypeConstantId(
elem_const_id));
}
return ResolveResult::Done(resolver.local_types().GetConstantId(
resolver.local_context().GetTupleType(elem_type_ids)));
GetTupleType(resolver.local_context(), elem_type_ids)));
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
@@ -2668,7 +2695,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
return ResolveAs<SemIR::TupleValue>(
resolver,
{.type_id = resolver.local_context().GetTypeIdForTypeConstant(type_id),
{.type_id =
resolver.local_context().types().GetTypeIdForTypeConstantId(type_id),
.elements_id =
GetLocalCanonicalInstBlockId(resolver, inst.elements_id, elems)});
}
@@ -2687,9 +2715,11 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
resolver,
{.type_id = SemIR::TypeType::SingletonTypeId,
.class_type_id =
resolver.local_context().GetTypeIdForTypeConstant(class_const_id),
resolver.local_context().types().GetTypeIdForTypeConstantId(
class_const_id),
.element_type_id =
resolver.local_context().GetTypeIdForTypeConstant(elem_const_id)});
resolver.local_context().types().GetTypeIdForTypeConstantId(
elem_const_id)});
}
// Tries to resolve the InstId, returning a canonical constant when ready, or