interface.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_binding = context.insts().Get(self_binding_id);
  73. auto self_facet_type_id = SemIR::GetTypeInSpecific(
  74. context.sem_ir(), interface_specific_id, self_binding.type_id());
  75. // Create a facet value to be the value of `Self` in the interface.
  76. // TODO: Pass this in instead of creating it here. The caller sometimes
  77. // already has a facet value.
  78. auto type_inst_id = context.types().GetInstId(self_type_id);
  79. auto witnesses_block_id =
  80. context.inst_blocks().AddCanonical({self_witness_id});
  81. auto self_value_const_id =
  82. TryEvalInst(context, SemIR::InstId::None,
  83. 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::TypeId self_type_id,
  112. SemIR::InstId witness_inst_id) -> 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. // Take any trailing argument values from the self specific.
  120. // TODO: If these refer to outer arguments, for example in their types, we may
  121. // need to perform extra substitutions here.
  122. for (auto arg_id : self_specific_args.drop_front(arg_ids.size())) {
  123. arg_ids.push_back(context.constant_values().GetConstantInstId(arg_id));
  124. }
  125. return MakeSpecific(context, loc, generic_id, arg_ids);
  126. }
  127. auto GetTypeForSpecificAssociatedEntity(Context& context, SemIRLoc loc,
  128. SemIR::SpecificId interface_specific_id,
  129. SemIR::InstId decl_id,
  130. SemIR::TypeId self_type_id,
  131. SemIR::InstId self_witness_id)
  132. -> SemIR::TypeId {
  133. auto decl =
  134. context.insts().Get(context.constant_values().GetConstantInstId(decl_id));
  135. if (auto assoc_const = decl.TryAs<SemIR::AssociatedConstantDecl>()) {
  136. // Form a specific for the associated constant, and grab the type from
  137. // there.
  138. auto generic_id = context.associated_constants()
  139. .Get(assoc_const->assoc_const_id)
  140. .generic_id;
  141. auto arg_ids =
  142. GetGenericArgsWithSelfType(context, interface_specific_id, generic_id,
  143. self_type_id, self_witness_id);
  144. auto const_specific_id = MakeSpecific(context, loc, generic_id, arg_ids);
  145. return SemIR::GetTypeInSpecific(context.sem_ir(), const_specific_id,
  146. context.insts().Get(decl_id).type_id());
  147. } else if (auto fn = context.types().TryGetAs<SemIR::FunctionType>(
  148. decl.type_id())) {
  149. // Form the type of the function within the interface, and attach the `Self`
  150. // type.
  151. auto interface_fn_type_id =
  152. SemIR::GetTypeInSpecific(context.sem_ir(), interface_specific_id,
  153. context.insts().Get(decl_id).type_id());
  154. auto self_facet_id =
  155. GetSelfFacet(context, interface_specific_id,
  156. context.functions().Get(fn->function_id).generic_id,
  157. self_type_id, self_witness_id);
  158. return GetFunctionTypeWithSelfType(
  159. context, context.types().GetInstId(interface_fn_type_id),
  160. self_facet_id);
  161. } else {
  162. CARBON_FATAL("Unexpected kind for associated constant {0}", decl);
  163. }
  164. }
  165. } // namespace Carbon::Check