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
-117
View File
@@ -227,123 +227,6 @@ auto Context::Finalize() -> void {
global_init_.Finalize();
}
auto Context::GetTypeIdForTypeConstant(SemIR::ConstantId constant_id)
-> SemIR::TypeId {
CARBON_CHECK(constant_id.is_constant(),
"Canonicalizing non-constant type: {0}", constant_id);
auto type_id =
insts().Get(constant_values().GetInstId(constant_id)).type_id();
CARBON_CHECK(type_id == SemIR::TypeType::SingletonTypeId ||
constant_id == SemIR::ErrorInst::SingletonConstantId,
"Forming type ID for non-type constant of type {0}",
types().GetAsInst(type_id));
return SemIR::TypeId::ForTypeConstant(constant_id);
}
auto Context::FacetTypeFromInterface(SemIR::InterfaceId interface_id,
SemIR::SpecificId specific_id)
-> SemIR::FacetType {
SemIR::FacetTypeId facet_type_id = facet_types().Add(
SemIR::FacetTypeInfo{.impls_constraints = {{interface_id, specific_id}},
.other_requirements = false});
return {.type_id = SemIR::TypeType::SingletonTypeId,
.facet_type_id = facet_type_id};
}
// Gets or forms a type_id for a type, given the instruction kind and arguments.
template <typename InstT, typename... EachArgT>
static auto GetTypeImpl(Context& context, EachArgT... each_arg)
-> SemIR::TypeId {
// TODO: Remove inst_id parameter from TryEvalInst.
InstT inst = {SemIR::TypeType::SingletonTypeId, each_arg...};
return context.GetTypeIdForTypeConstant(
TryEvalInst(context, SemIR::InstId::None, inst));
}
// Gets or forms a type_id for a type, given the instruction kind and arguments,
// and completes the type. This should only be used when type completion cannot
// fail.
template <typename InstT, typename... EachArgT>
static auto GetCompleteTypeImpl(Context& context, EachArgT... each_arg)
-> SemIR::TypeId {
auto type_id = GetTypeImpl<InstT>(context, each_arg...);
CompleteTypeOrCheckFail(context, type_id);
return type_id;
}
auto Context::GetStructType(SemIR::StructTypeFieldsId fields_id)
-> SemIR::TypeId {
return GetTypeImpl<SemIR::StructType>(*this, fields_id);
}
auto Context::GetTupleType(llvm::ArrayRef<SemIR::TypeId> type_ids)
-> SemIR::TypeId {
return GetTypeImpl<SemIR::TupleType>(*this,
type_blocks().AddCanonical(type_ids));
}
auto Context::GetAssociatedEntityType(SemIR::TypeId interface_type_id)
-> SemIR::TypeId {
return GetTypeImpl<SemIR::AssociatedEntityType>(*this, interface_type_id);
}
auto Context::GetSingletonType(SemIR::InstId singleton_id) -> SemIR::TypeId {
CARBON_CHECK(SemIR::IsSingletonInstId(singleton_id));
auto type_id = GetTypeIdForTypeInst(singleton_id);
// To keep client code simpler, complete builtin types before returning them.
CompleteTypeOrCheckFail(*this, type_id);
return type_id;
}
auto Context::GetClassType(SemIR::ClassId class_id,
SemIR::SpecificId specific_id) -> SemIR::TypeId {
return GetTypeImpl<SemIR::ClassType>(*this, class_id, specific_id);
}
auto Context::GetFunctionType(SemIR::FunctionId fn_id,
SemIR::SpecificId specific_id) -> SemIR::TypeId {
return GetCompleteTypeImpl<SemIR::FunctionType>(*this, fn_id, specific_id);
}
auto Context::GetFunctionTypeWithSelfType(
SemIR::InstId interface_function_type_id, SemIR::InstId self_id)
-> SemIR::TypeId {
return GetCompleteTypeImpl<SemIR::FunctionTypeWithSelfType>(
*this, interface_function_type_id, self_id);
}
auto Context::GetGenericClassType(SemIR::ClassId class_id,
SemIR::SpecificId enclosing_specific_id)
-> SemIR::TypeId {
return GetCompleteTypeImpl<SemIR::GenericClassType>(*this, class_id,
enclosing_specific_id);
}
auto Context::GetGenericInterfaceType(SemIR::InterfaceId interface_id,
SemIR::SpecificId enclosing_specific_id)
-> SemIR::TypeId {
return GetCompleteTypeImpl<SemIR::GenericInterfaceType>(
*this, interface_id, enclosing_specific_id);
}
auto Context::GetInterfaceType(SemIR::InterfaceId interface_id,
SemIR::SpecificId specific_id) -> SemIR::TypeId {
return GetTypeImpl<SemIR::FacetType>(
*this, FacetTypeFromInterface(interface_id, specific_id).facet_type_id);
}
auto Context::GetPointerType(SemIR::TypeId pointee_type_id) -> SemIR::TypeId {
return GetTypeImpl<SemIR::PointerType>(*this, pointee_type_id);
}
auto Context::GetUnboundElementType(SemIR::TypeId class_type_id,
SemIR::TypeId element_type_id)
-> SemIR::TypeId {
return GetTypeImpl<SemIR::UnboundElementType>(*this, class_type_id,
element_type_id);
}
auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
output << "Check::Context\n";