interface.cpp 8.4 KB

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