Change TypeId to be a thin wrapper around ConstantId. (#4140)

This better follows the principle that types are simply constants of
type `type`, and allows more uniform treatment of types as just another
kind of constant from generics handling.

Use a hash table to map from `TypeId` to information about the complete
type. This makes basic operations on types a bit simpler, and operations
that actually need to access the complete class information a bit more
complex.
This commit is contained in:
Richard Smith
2024-07-17 22:46:03 +00:00
committed by GitHub
parent 65d6e3e221
commit dde0bd0ffe
17 changed files with 355 additions and 277 deletions
+10 -4
View File
@@ -734,7 +734,7 @@ class TypeCompleter {
case Phase::BuildValueRepr: {
auto value_rep = BuildValueRepr(type_id, inst);
context_.sem_ir().CompleteType(type_id, value_rep);
context_.types().SetValueRepr(type_id, value_rep);
CARBON_CHECK(old_work_list_size == work_list_.size())
<< "BuildValueRepr should not change work items";
work_list_.pop_back();
@@ -1107,10 +1107,16 @@ auto Context::GetTypeIdForTypeConstant(SemIR::ConstantId constant_id)
-> SemIR::TypeId {
CARBON_CHECK(constant_id.is_constant())
<< "Canonicalizing non-constant type: " << constant_id;
auto type_id =
insts().Get(constant_values().GetInstId(constant_id)).type_id();
// TODO: For now, we allow values of facet type to be used as types.
CARBON_CHECK(type_id == SemIR::TypeId::TypeType ||
types().Is<SemIR::InterfaceType>(type_id) ||
constant_id == SemIR::ConstantId::Error)
<< "Forming type ID for non-type constant of type "
<< types().GetAsInst(type_id);
auto result = type_ids_for_type_constants_.Insert(
constant_id, [&]() { return types().Add({.constant_id = constant_id}); });
return result.value();
return SemIR::TypeId::ForTypeConstant(constant_id);
}
// Gets or forms a type_id for a type, given the instruction kind and arguments.