interface.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/interface.h"
  5. #include <algorithm>
  6. #include <cstddef>
  7. #include "toolchain/check/context.h"
  8. #include "toolchain/check/eval.h"
  9. #include "toolchain/check/generic.h"
  10. #include "toolchain/check/inst.h"
  11. #include "toolchain/check/type.h"
  12. #include "toolchain/sem_ir/ids.h"
  13. #include "toolchain/sem_ir/inst.h"
  14. #include "toolchain/sem_ir/typed_insts.h"
  15. namespace Carbon::Check {
  16. auto BuildAssociatedEntity(Context& context, SemIR::InterfaceId interface_id,
  17. SemIR::InstId decl_id) -> SemIR::InstId {
  18. auto& interface_info = context.interfaces().Get(interface_id);
  19. if (!interface_info.is_being_defined()) {
  20. // This should only happen if the interface is erroneously defined more than
  21. // once.
  22. // TODO: Find a way to CHECK this.
  23. return SemIR::ErrorInst::InstId;
  24. }
  25. // This associated entity is being declared as a member of the self specific
  26. // of the interface.
  27. auto interface_specific_id =
  28. context.generics().GetSelfSpecific(interface_info.generic_id);
  29. // Register this declaration as declaring an associated entity.
  30. auto index = SemIR::ElementIndex(
  31. context.args_type_info_stack().PeekCurrentBlockContents().size());
  32. context.args_type_info_stack().AddInstId(decl_id);
  33. // Name lookup for the declaration's name should name the associated entity,
  34. // not the declaration itself.
  35. auto type_id =
  36. GetAssociatedEntityType(context, interface_id, interface_specific_id);
  37. return AddInst<SemIR::AssociatedEntity>(
  38. context, SemIR::LocId(decl_id),
  39. {.type_id = type_id, .index = index, .decl_id = decl_id});
  40. }
  41. // Returns the `Self` binding for an interface, given a specific for the
  42. // interface and a generic for an associated entity within it.
  43. static auto GetSelfBinding(Context& context,
  44. SemIR::SpecificId interface_specific_id,
  45. SemIR::GenericId assoc_entity_generic_id)
  46. -> SemIR::InstId {
  47. const auto& generic = context.generics().Get(assoc_entity_generic_id);
  48. auto bindings = context.inst_blocks().Get(generic.bindings_id);
  49. auto interface_args_id =
  50. context.specifics().GetArgsOrEmpty(interface_specific_id);
  51. auto interface_args = context.inst_blocks().Get(interface_args_id);
  52. // The `Self` binding is the first binding after the interface's arguments.
  53. auto self_binding_id = bindings[interface_args.size()];
  54. // Check that we found the self binding. The binding might be a
  55. // `BindSymbolicName` or an `ImportRef` naming one.
  56. auto self_binding_const_inst_id =
  57. context.constant_values().GetConstantInstId(self_binding_id);
  58. auto bind_name_inst = context.insts().GetAs<SemIR::BindSymbolicName>(
  59. self_binding_const_inst_id);
  60. CARBON_CHECK(
  61. context.entity_names().Get(bind_name_inst.entity_name_id).name_id ==
  62. SemIR::NameId::SelfType,
  63. "Expected a Self binding, found {0}", bind_name_inst);
  64. return self_binding_id;
  65. }
  66. // Given a `Self` type and a witness that it implements an interface, along with
  67. // that interface's `Self` binding, forms and returns a facet that can be used
  68. // as the argument for that `Self` binding.
  69. static auto GetSelfFacet(Context& context,
  70. SemIR::SpecificId interface_specific_id,
  71. SemIR::GenericId generic_id,
  72. SemIR::TypeId self_type_id,
  73. SemIR::InstId self_witness_id) -> SemIR::InstId {
  74. auto self_binding_id =
  75. GetSelfBinding(context, interface_specific_id, generic_id);
  76. auto self_facet_type_id = SemIR::GetTypeOfInstInSpecific(
  77. context.sem_ir(), interface_specific_id, self_binding_id);
  78. // Create a facet value to be the value of `Self` in the interface.
  79. // TODO: Pass this in instead of creating it here. The caller sometimes
  80. // already has a facet value.
  81. auto type_inst_id = context.types().GetInstId(self_type_id);
  82. auto witnesses_block_id =
  83. context.inst_blocks().AddCanonical({self_witness_id});
  84. auto self_value_const_id = TryEvalInst(
  85. context, SemIR::FacetValue{.type_id = self_facet_type_id,
  86. .type_inst_id = type_inst_id,
  87. .witnesses_block_id = witnesses_block_id});
  88. return context.constant_values().GetInstId(self_value_const_id);
  89. }
  90. // Builds and returns the argument list from `interface_specific_id` with a
  91. // value for the `Self` parameter of `generic_id` appended.
  92. static auto GetGenericArgsWithSelfType(Context& context,
  93. SemIR::SpecificId interface_specific_id,
  94. SemIR::GenericId generic_id,
  95. SemIR::TypeId self_type_id,
  96. SemIR::InstId witness_inst_id,
  97. std::size_t reserve_args_size = 0)
  98. -> llvm::SmallVector<SemIR::InstId> {
  99. auto interface_args_id =
  100. context.specifics().GetArgsOrEmpty(interface_specific_id);
  101. auto interface_args = context.inst_blocks().Get(interface_args_id);
  102. llvm::SmallVector<SemIR::InstId> arg_ids;
  103. arg_ids.reserve(std::max(reserve_args_size, interface_args.size() + 1));
  104. // Start with the enclosing arguments from the interface.
  105. arg_ids.assign(interface_args.begin(), interface_args.end());
  106. // Add the `Self` argument.
  107. arg_ids.push_back(GetSelfFacet(context, interface_specific_id, generic_id,
  108. self_type_id, witness_inst_id));
  109. return arg_ids;
  110. }
  111. auto GetSelfSpecificForInterfaceMemberWithSelfType(
  112. Context& context, SemIR::LocId loc_id,
  113. SemIR::SpecificId interface_specific_id, SemIR::GenericId generic_id,
  114. SemIR::SpecificId enclosing_specific_id, SemIR::TypeId self_type_id,
  115. SemIR::InstId witness_inst_id) -> SemIR::SpecificId {
  116. const auto& generic = context.generics().Get(generic_id);
  117. auto self_specific_args = context.inst_blocks().Get(
  118. context.specifics().Get(generic.self_specific_id).args_id);
  119. auto arg_ids = GetGenericArgsWithSelfType(
  120. context, interface_specific_id, generic_id, self_type_id, witness_inst_id,
  121. self_specific_args.size());
  122. // Determine the number of specific arguments that enclose the point where
  123. // this self specific will be used from. In an impl, this will be the number
  124. // of parameters that the impl has.
  125. int num_enclosing_specific_args =
  126. context.inst_blocks()
  127. .Get(context.specifics().GetArgsOrEmpty(enclosing_specific_id))
  128. .size();
  129. // The index of each remaining generic parameter is adjusted to match the
  130. // numbering at the point where the self specific is used.
  131. int index_delta = num_enclosing_specific_args - arg_ids.size();
  132. // Take any trailing argument values from the self specific.
  133. // TODO: If these refer to outer arguments, for example in their types, we may
  134. // need to perform extra substitutions here.
  135. for (auto arg_id : self_specific_args.drop_front(arg_ids.size())) {
  136. auto new_arg_id = context.constant_values().GetConstantInstId(arg_id);
  137. if (index_delta) {
  138. // If this parameter would have a new index in the context described by
  139. // `enclosing_specific_id`, form a new binding with an adjusted index.
  140. auto bind_name = context.insts().GetAs<SemIR::BindSymbolicName>(
  141. context.constant_values().GetConstantInstId(arg_id));
  142. auto entity_name = context.entity_names().Get(bind_name.entity_name_id);
  143. entity_name.bind_index_value += index_delta;
  144. CARBON_CHECK(entity_name.bind_index_value >= 0);
  145. bind_name.entity_name_id =
  146. context.entity_names().AddCanonical(entity_name);
  147. new_arg_id =
  148. context.constant_values().GetInstId(TryEvalInst(context, bind_name));
  149. }
  150. arg_ids.push_back(new_arg_id);
  151. }
  152. return MakeSpecific(context, loc_id, generic_id, arg_ids);
  153. }
  154. auto GetTypeForSpecificAssociatedEntity(Context& context, SemIR::LocId loc_id,
  155. SemIR::SpecificId interface_specific_id,
  156. SemIR::InstId decl_id,
  157. SemIR::TypeId self_type_id,
  158. SemIR::InstId self_witness_id)
  159. -> SemIR::TypeId {
  160. auto decl =
  161. context.insts().Get(context.constant_values().GetConstantInstId(decl_id));
  162. if (auto assoc_const = decl.TryAs<SemIR::AssociatedConstantDecl>()) {
  163. // Form a specific for the associated constant, and grab the type from
  164. // there.
  165. auto generic_id = context.associated_constants()
  166. .Get(assoc_const->assoc_const_id)
  167. .generic_id;
  168. auto arg_ids =
  169. GetGenericArgsWithSelfType(context, interface_specific_id, generic_id,
  170. self_type_id, self_witness_id);
  171. auto const_specific_id = MakeSpecific(context, loc_id, generic_id, arg_ids);
  172. return SemIR::GetTypeOfInstInSpecific(context.sem_ir(), const_specific_id,
  173. decl_id);
  174. } else if (auto fn = context.types().TryGetAs<SemIR::FunctionType>(
  175. decl.type_id())) {
  176. // Form the type of the function within the interface, and attach the `Self`
  177. // type.
  178. auto interface_fn_type_id = SemIR::GetTypeOfInstInSpecific(
  179. context.sem_ir(), interface_specific_id, decl_id);
  180. auto self_facet_id =
  181. GetSelfFacet(context, interface_specific_id,
  182. context.functions().Get(fn->function_id).generic_id,
  183. self_type_id, self_witness_id);
  184. return GetFunctionTypeWithSelfType(
  185. context, context.types().GetInstId(interface_fn_type_id),
  186. self_facet_id);
  187. } else {
  188. CARBON_FATAL("Unexpected kind for associated constant {0}", decl);
  189. }
  190. }
  191. } // namespace Carbon::Check