interface.cpp 9.7 KB

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