interface.cpp 9.7 KB

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