impl.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/impl.h"
  5. #include "toolchain/base/kind_switch.h"
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/eval.h"
  8. #include "toolchain/check/facet_type.h"
  9. #include "toolchain/check/function.h"
  10. #include "toolchain/check/generic.h"
  11. #include "toolchain/check/import_ref.h"
  12. #include "toolchain/check/interface.h"
  13. #include "toolchain/check/name_lookup.h"
  14. #include "toolchain/check/type.h"
  15. #include "toolchain/check/type_completion.h"
  16. #include "toolchain/diagnostics/diagnostic_emitter.h"
  17. #include "toolchain/sem_ir/generic.h"
  18. #include "toolchain/sem_ir/ids.h"
  19. #include "toolchain/sem_ir/impl.h"
  20. #include "toolchain/sem_ir/inst.h"
  21. #include "toolchain/sem_ir/typed_insts.h"
  22. namespace Carbon::Check {
  23. // Adds the location of the associated function to a diagnostic.
  24. static auto NoteAssociatedFunction(Context& context, DiagnosticBuilder& builder,
  25. SemIR::FunctionId function_id) -> void {
  26. CARBON_DIAGNOSTIC(AssociatedFunctionHere, Note,
  27. "associated function {0} declared here", SemIR::NameId);
  28. const auto& function = context.functions().Get(function_id);
  29. builder.Note(function.latest_decl_id(), AssociatedFunctionHere,
  30. function.name_id);
  31. }
  32. // Checks that `impl_function_id` is a valid implementation of the function
  33. // described in the interface as `interface_function_id`. Returns the value to
  34. // put into the corresponding slot in the witness table, which can be
  35. // `BuiltinErrorInst` if the function is not usable.
  36. static auto CheckAssociatedFunctionImplementation(
  37. Context& context, SemIR::FunctionType interface_function_type,
  38. SemIR::InstId impl_decl_id, SemIR::TypeId self_type_id,
  39. SemIR::InstId witness_inst_id) -> SemIR::InstId {
  40. auto impl_function_decl =
  41. context.insts().TryGetAs<SemIR::FunctionDecl>(impl_decl_id);
  42. if (!impl_function_decl) {
  43. CARBON_DIAGNOSTIC(ImplFunctionWithNonFunction, Error,
  44. "associated function {0} implemented by non-function",
  45. SemIR::NameId);
  46. auto builder = context.emitter().Build(
  47. impl_decl_id, ImplFunctionWithNonFunction,
  48. context.functions().Get(interface_function_type.function_id).name_id);
  49. NoteAssociatedFunction(context, builder,
  50. interface_function_type.function_id);
  51. builder.Emit();
  52. return SemIR::ErrorInst::SingletonInstId;
  53. }
  54. auto impl_enclosing_specific_id =
  55. context.types()
  56. .GetAs<SemIR::FunctionType>(impl_function_decl->type_id)
  57. .specific_id;
  58. // Map from the specific for the function type to the specific for the
  59. // function signature. The function signature may have additional generic
  60. // parameters.
  61. auto interface_function_specific_id =
  62. GetSelfSpecificForInterfaceMemberWithSelfType(
  63. context, impl_decl_id, interface_function_type.specific_id,
  64. context.functions()
  65. .Get(interface_function_type.function_id)
  66. .generic_id,
  67. impl_enclosing_specific_id, self_type_id, witness_inst_id);
  68. if (!CheckFunctionTypeMatches(
  69. context, context.functions().Get(impl_function_decl->function_id),
  70. context.functions().Get(interface_function_type.function_id),
  71. interface_function_specific_id, /*check_syntax=*/false,
  72. /*check_self=*/true)) {
  73. return SemIR::ErrorInst::SingletonInstId;
  74. }
  75. return impl_decl_id;
  76. }
  77. // Builds an initial empty witness.
  78. // TODO: Fill the witness with the rewrites from the declaration.
  79. auto ImplWitnessForDeclaration(Context& context, const SemIR::Impl& impl)
  80. -> SemIR::InstId {
  81. CARBON_CHECK(!impl.has_definition_started());
  82. auto self_type_id = context.types().GetTypeIdForTypeInstId(impl.self_id);
  83. if (self_type_id == SemIR::ErrorInst::SingletonTypeId) {
  84. // When 'impl as' is invalid, the self type is an error.
  85. return SemIR::ErrorInst::SingletonInstId;
  86. }
  87. return ResolveFacetTypeImplWitness(
  88. context, context.insts().GetLocId(impl.latest_decl_id()),
  89. impl.constraint_id, impl.self_id, impl.interface,
  90. context.generics().GetSelfSpecific(impl.generic_id));
  91. }
  92. auto ImplWitnessStartDefinition(Context& context, SemIR::Impl& impl) -> void {
  93. CARBON_CHECK(impl.is_being_defined());
  94. CARBON_CHECK(impl.witness_id.has_value());
  95. if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
  96. return;
  97. }
  98. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  99. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  100. const auto& interface = context.interfaces().Get(impl.interface.interface_id);
  101. auto assoc_entities =
  102. context.inst_blocks().Get(interface.associated_entities_id);
  103. CARBON_CHECK(witness_block.size() == assoc_entities.size());
  104. // Check we have a value for all non-function associated constants in the
  105. // witness.
  106. for (auto index : llvm::seq(assoc_entities.size())) {
  107. auto decl_id = assoc_entities[index];
  108. decl_id = context.constant_values().GetConstantInstId(decl_id);
  109. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  110. if (auto decl =
  111. context.insts().TryGetAs<SemIR::AssociatedConstantDecl>(decl_id)) {
  112. auto& witness_value = witness_block[index];
  113. if (!witness_value.has_value()) {
  114. CARBON_DIAGNOSTIC(ImplAssociatedConstantNeedsValue, Error,
  115. "associated constant {0} not given a value in impl "
  116. "of interface {1}",
  117. SemIR::NameId, SemIR::NameId);
  118. CARBON_DIAGNOSTIC(AssociatedConstantHere, Note,
  119. "associated constant declared here");
  120. context.emitter()
  121. .Build(impl.constraint_id, ImplAssociatedConstantNeedsValue,
  122. context.associated_constants()
  123. .Get(decl->assoc_const_id)
  124. .name_id,
  125. interface.name_id)
  126. .Note(assoc_entities[index], AssociatedConstantHere)
  127. .Emit();
  128. witness_value = SemIR::ErrorInst::SingletonInstId;
  129. }
  130. }
  131. }
  132. }
  133. // Adds functions to the witness that the specified impl implements the given
  134. // interface.
  135. auto FinishImplWitness(Context& context, SemIR::Impl& impl) -> void {
  136. CARBON_CHECK(impl.is_being_defined());
  137. CARBON_CHECK(impl.witness_id.has_value());
  138. if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
  139. return;
  140. }
  141. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  142. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  143. auto& impl_scope = context.name_scopes().Get(impl.scope_id);
  144. auto self_type_id = context.types().GetTypeIdForTypeInstId(impl.self_id);
  145. const auto& interface = context.interfaces().Get(impl.interface.interface_id);
  146. auto assoc_entities =
  147. context.inst_blocks().Get(interface.associated_entities_id);
  148. llvm::SmallVector<SemIR::InstId> used_decl_ids;
  149. for (auto index : llvm::seq(assoc_entities.size())) {
  150. auto decl_id = assoc_entities[index];
  151. decl_id =
  152. context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
  153. context.sem_ir(), impl.interface.specific_id, decl_id));
  154. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  155. auto decl = context.insts().Get(decl_id);
  156. CARBON_KIND_SWITCH(decl) {
  157. case CARBON_KIND(SemIR::StructValue struct_value): {
  158. if (struct_value.type_id == SemIR::ErrorInst::SingletonTypeId) {
  159. witness_block[index] = SemIR::ErrorInst::SingletonInstId;
  160. break;
  161. }
  162. auto type_inst = context.types().GetAsInst(struct_value.type_id);
  163. auto fn_type = type_inst.TryAs<SemIR::FunctionType>();
  164. if (!fn_type) {
  165. CARBON_FATAL("Unexpected type: {0}", type_inst);
  166. }
  167. auto& fn = context.functions().Get(fn_type->function_id);
  168. auto lookup_result =
  169. LookupNameInExactScope(context, context.insts().GetLocId(decl_id),
  170. fn.name_id, impl.scope_id, impl_scope);
  171. if (lookup_result.is_found()) {
  172. used_decl_ids.push_back(lookup_result.target_inst_id());
  173. witness_block[index] = CheckAssociatedFunctionImplementation(
  174. context, *fn_type, lookup_result.target_inst_id(), self_type_id,
  175. impl.witness_id);
  176. } else {
  177. CARBON_DIAGNOSTIC(
  178. ImplMissingFunction, Error,
  179. "missing implementation of {0} in impl of interface {1}",
  180. SemIR::NameId, SemIR::NameId);
  181. auto builder =
  182. context.emitter().Build(impl.definition_id, ImplMissingFunction,
  183. fn.name_id, interface.name_id);
  184. NoteAssociatedFunction(context, builder, fn_type->function_id);
  185. builder.Emit();
  186. witness_block[index] = SemIR::ErrorInst::SingletonInstId;
  187. }
  188. break;
  189. }
  190. case SemIR::AssociatedConstantDecl::Kind: {
  191. // These are set to their final values already.
  192. break;
  193. }
  194. default:
  195. CARBON_CHECK(decl_id == SemIR::ErrorInst::SingletonInstId,
  196. "Unexpected kind of associated entity {0}", decl);
  197. witness_block[index] = SemIR::ErrorInst::SingletonInstId;
  198. break;
  199. }
  200. }
  201. // TODO: Diagnose if any declarations in the impl are not in used_decl_ids.
  202. }
  203. auto FillImplWitnessWithErrors(Context& context, SemIR::Impl& impl) -> void {
  204. if (impl.witness_id.has_value() &&
  205. impl.witness_id != SemIR::ErrorInst::SingletonInstId) {
  206. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  207. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  208. for (auto& elem : witness_block) {
  209. if (!elem.has_value()) {
  210. elem = SemIR::ErrorInst::SingletonInstId;
  211. }
  212. }
  213. }
  214. }
  215. } // namespace Carbon::Check