interface.cpp 8.3 KB

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