From 3cc90f9017422f75040e77f6753e44bdf0a551c0 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Wed, 17 Jul 2024 16:27:14 -0700 Subject: [PATCH] Move `GetTypeInInstance` from `Check` to `SemIR`. (#4144) In preparation for this function being used by other parts of `SemIR` and by lowering. --- toolchain/check/generic.cpp | 12 ------------ toolchain/check/generic.h | 8 -------- toolchain/check/handle_function.cpp | 5 +++-- toolchain/check/handle_name.cpp | 4 ++-- toolchain/check/member_access.cpp | 3 ++- toolchain/sem_ir/generic.cpp | 6 ++++++ toolchain/sem_ir/generic.h | 6 ++++++ 7 files changed, 19 insertions(+), 25 deletions(-) diff --git a/toolchain/check/generic.cpp b/toolchain/check/generic.cpp index e9fbdf4b545b..f0f19a986d31 100644 --- a/toolchain/check/generic.cpp +++ b/toolchain/check/generic.cpp @@ -329,16 +329,4 @@ auto ResolveSpecificDefinition(Context& context, return true; } -auto GetTypeInInstance(Context& context, SemIR::GenericInstanceId instance_id, - SemIR::TypeId type_id) -> SemIR::TypeId { - auto const_id = context.types().GetConstantId(type_id); - auto inst_const_id = - GetConstantInInstance(context.sem_ir(), instance_id, const_id); - if (inst_const_id == const_id) { - // Common case: not an instance constant. - return type_id; - } - return context.GetTypeIdForTypeConstant(inst_const_id); -} - } // namespace Carbon::Check diff --git a/toolchain/check/generic.h b/toolchain/check/generic.h index e52e1c6ba8b3..f5c3ba49374d 100644 --- a/toolchain/check/generic.h +++ b/toolchain/check/generic.h @@ -52,14 +52,6 @@ auto MakeGenericSelfInstance(Context& context, SemIR::GenericId generic_id) auto ResolveSpecificDefinition(Context& context, SemIR::GenericInstanceId specific_id) -> bool; -// Gets the substituted value of a type within a specified instance of a -// generic. Note that this does not perform substitution, and will return -// `Invalid` if the substituted type is not yet known. -// -// TODO: Move this to sem_ir so that lowering can use it. -auto GetTypeInInstance(Context& context, SemIR::GenericInstanceId instance_id, - SemIR::TypeId type_id) -> SemIR::TypeId; - } // namespace Carbon::Check #endif // CARBON_TOOLCHAIN_CHECK_GENERIC_H_ diff --git a/toolchain/check/handle_function.cpp b/toolchain/check/handle_function.cpp index f801043ad049..2d5e6a50abf1 100644 --- a/toolchain/check/handle_function.cpp +++ b/toolchain/check/handle_function.cpp @@ -279,8 +279,9 @@ static auto BuildFunctionDecl(Context& context, // substitution is ready. if (return_storage_id.is_valid()) { auto return_storage = context.insts().Get(return_storage_id); - return_storage.SetType(GetTypeInInstance( - context, SemIR::GenericInstanceId::Invalid, return_storage.type_id())); + return_storage.SetType(SemIR::GetTypeInInstance( + context.sem_ir(), SemIR::GenericInstanceId::Invalid, + return_storage.type_id())); context.sem_ir().insts().Set(return_storage_id, return_storage); } diff --git a/toolchain/check/handle_name.cpp b/toolchain/check/handle_name.cpp index 1b06f6c55910..ae556aca73be 100644 --- a/toolchain/check/handle_name.cpp +++ b/toolchain/check/handle_name.cpp @@ -81,8 +81,8 @@ static auto HandleNameAsExpr(Context& context, Parse::NodeId node_id, SemIR::NameId name_id) -> bool { auto result = context.LookupUnqualifiedName(node_id, name_id); auto value = context.insts().Get(result.inst_id); - auto type_id = - GetTypeInInstance(context, result.instance_id, value.type_id()); + auto type_id = SemIR::GetTypeInInstance(context.sem_ir(), result.instance_id, + value.type_id()); CARBON_CHECK(type_id.is_valid()) << "Missing type for " << value; // If the named entity has a constant value that depends on its generic diff --git a/toolchain/check/member_access.cpp b/toolchain/check/member_access.cpp index d5e4af2603ca..76072fc9c4dd 100644 --- a/toolchain/check/member_access.cpp +++ b/toolchain/check/member_access.cpp @@ -225,7 +225,8 @@ static auto LookupMemberNameInScope(Context& context, Parse::NodeId node_id, // TODO: This duplicates the work that HandleNameAsExpr does. Factor this out. auto inst = context.insts().Get(result.inst_id); - auto type_id = GetTypeInInstance(context, result.instance_id, inst.type_id()); + auto type_id = SemIR::GetTypeInInstance(context.sem_ir(), result.instance_id, + inst.type_id()); CARBON_CHECK(type_id.is_valid()) << "Missing type for member " << inst; // If the named entity has a constant value that depends on its generic diff --git a/toolchain/sem_ir/generic.cpp b/toolchain/sem_ir/generic.cpp index 8c4d1a9816ec..4dbcc10706c8 100644 --- a/toolchain/sem_ir/generic.cpp +++ b/toolchain/sem_ir/generic.cpp @@ -95,4 +95,10 @@ auto GetConstantValueInInstance(const File& sem_ir, sem_ir.constant_values().Get(inst_id)); } +auto GetTypeInInstance(const File& sem_ir, GenericInstanceId instance_id, + TypeId type_id) -> TypeId { + return TypeId::ForTypeConstant(GetConstantInInstance( + sem_ir, instance_id, sem_ir.types().GetConstantId(type_id))); +} + } // namespace Carbon::SemIR diff --git a/toolchain/sem_ir/generic.h b/toolchain/sem_ir/generic.h index 20f54a36c27f..810c7553fa22 100644 --- a/toolchain/sem_ir/generic.h +++ b/toolchain/sem_ir/generic.h @@ -150,6 +150,12 @@ auto GetConstantValueInInstance(const File& sem_ir, GenericInstanceId instance_id, InstId inst_id) -> ConstantId; +// Gets the substituted value of a type within a specified instance of a +// generic. Note that this does not perform substitution, and will return +// `Invalid` if the substituted type is not yet known. +auto GetTypeInInstance(const File& sem_ir, GenericInstanceId instance_id, + TypeId type_id) -> TypeId; + } // namespace Carbon::SemIR #endif // CARBON_TOOLCHAIN_SEM_IR_GENERIC_H_