impl.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // Map from the specific for the function type to the specific for the
  55. // function signature. The function signature may have additional generic
  56. // parameters.
  57. auto interface_function_specific_id =
  58. GetSelfSpecificForInterfaceMemberWithSelfType(
  59. context, impl_decl_id, interface_function_type.specific_id,
  60. context.functions()
  61. .Get(interface_function_type.function_id)
  62. .generic_id,
  63. self_type_id, witness_inst_id);
  64. if (!CheckFunctionTypeMatches(
  65. context, context.functions().Get(impl_function_decl->function_id),
  66. context.functions().Get(interface_function_type.function_id),
  67. interface_function_specific_id, /*check_syntax=*/false,
  68. /*check_self=*/true)) {
  69. return SemIR::ErrorInst::SingletonInstId;
  70. }
  71. return impl_decl_id;
  72. }
  73. // Builds an initial empty witness.
  74. // TODO: Fill the witness with the rewrites from the declaration.
  75. auto ImplWitnessForDeclaration(Context& context, const SemIR::Impl& impl)
  76. -> SemIR::InstId {
  77. CARBON_CHECK(!impl.has_definition_started());
  78. auto self_type_id = context.types().GetTypeIdForTypeInstId(impl.self_id);
  79. if (self_type_id == SemIR::ErrorInst::SingletonTypeId) {
  80. // When 'impl as' is invalid, the self type is an error.
  81. return SemIR::ErrorInst::SingletonInstId;
  82. }
  83. return ResolveFacetTypeImplWitness(
  84. context, context.insts().GetLocId(impl.latest_decl_id()),
  85. impl.constraint_id, impl.self_id, impl.interface,
  86. context.generics().GetSelfSpecific(impl.generic_id));
  87. }
  88. auto ImplWitnessStartDefinition(Context& context, SemIR::Impl& impl) -> void {
  89. CARBON_CHECK(impl.is_being_defined());
  90. CARBON_CHECK(impl.witness_id.has_value());
  91. if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
  92. return;
  93. }
  94. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  95. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  96. const auto& interface = context.interfaces().Get(impl.interface.interface_id);
  97. auto assoc_entities =
  98. context.inst_blocks().Get(interface.associated_entities_id);
  99. CARBON_CHECK(witness_block.size() == assoc_entities.size());
  100. // Check we have a value for all non-function associated constants in the
  101. // witness.
  102. for (auto index : llvm::seq(assoc_entities.size())) {
  103. auto decl_id = assoc_entities[index];
  104. decl_id = context.constant_values().GetConstantInstId(decl_id);
  105. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  106. if (auto decl =
  107. context.insts().TryGetAs<SemIR::AssociatedConstantDecl>(decl_id)) {
  108. auto& witness_value = witness_block[index];
  109. if (!witness_value.has_value()) {
  110. CARBON_DIAGNOSTIC(ImplAssociatedConstantNeedsValue, Error,
  111. "associated constant {0} not given a value in impl "
  112. "of interface {1}",
  113. SemIR::NameId, SemIR::NameId);
  114. CARBON_DIAGNOSTIC(AssociatedConstantHere, Note,
  115. "associated constant declared here");
  116. context.emitter()
  117. .Build(impl.constraint_id, ImplAssociatedConstantNeedsValue,
  118. context.associated_constants()
  119. .Get(decl->assoc_const_id)
  120. .name_id,
  121. interface.name_id)
  122. .Note(assoc_entities[index], AssociatedConstantHere)
  123. .Emit();
  124. witness_value = SemIR::ErrorInst::SingletonInstId;
  125. }
  126. }
  127. }
  128. }
  129. // Adds functions to the witness that the specified impl implements the given
  130. // interface.
  131. auto FinishImplWitness(Context& context, SemIR::Impl& impl) -> void {
  132. CARBON_CHECK(impl.is_being_defined());
  133. CARBON_CHECK(impl.witness_id.has_value());
  134. if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
  135. return;
  136. }
  137. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  138. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  139. auto& impl_scope = context.name_scopes().Get(impl.scope_id);
  140. auto self_type_id = context.types().GetTypeIdForTypeInstId(impl.self_id);
  141. const auto& interface = context.interfaces().Get(impl.interface.interface_id);
  142. auto assoc_entities =
  143. context.inst_blocks().Get(interface.associated_entities_id);
  144. llvm::SmallVector<SemIR::InstId> used_decl_ids;
  145. for (auto index : llvm::seq(assoc_entities.size())) {
  146. auto decl_id = assoc_entities[index];
  147. decl_id =
  148. context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
  149. context.sem_ir(), impl.interface.specific_id, decl_id));
  150. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  151. auto decl = context.insts().Get(decl_id);
  152. CARBON_KIND_SWITCH(decl) {
  153. case CARBON_KIND(SemIR::StructValue struct_value): {
  154. if (struct_value.type_id == SemIR::ErrorInst::SingletonTypeId) {
  155. witness_block[index] = SemIR::ErrorInst::SingletonInstId;
  156. break;
  157. }
  158. auto type_inst = context.types().GetAsInst(struct_value.type_id);
  159. auto fn_type = type_inst.TryAs<SemIR::FunctionType>();
  160. if (!fn_type) {
  161. CARBON_FATAL("Unexpected type: {0}", type_inst);
  162. }
  163. auto& fn = context.functions().Get(fn_type->function_id);
  164. auto lookup_result =
  165. LookupNameInExactScope(context, context.insts().GetLocId(decl_id),
  166. fn.name_id, impl.scope_id, impl_scope);
  167. if (lookup_result.is_found()) {
  168. used_decl_ids.push_back(lookup_result.target_inst_id());
  169. witness_block[index] = CheckAssociatedFunctionImplementation(
  170. context, *fn_type, lookup_result.target_inst_id(), self_type_id,
  171. impl.witness_id);
  172. } else {
  173. CARBON_DIAGNOSTIC(
  174. ImplMissingFunction, Error,
  175. "missing implementation of {0} in impl of interface {1}",
  176. SemIR::NameId, SemIR::NameId);
  177. auto builder =
  178. context.emitter().Build(impl.definition_id, ImplMissingFunction,
  179. fn.name_id, interface.name_id);
  180. NoteAssociatedFunction(context, builder, fn_type->function_id);
  181. builder.Emit();
  182. witness_block[index] = SemIR::ErrorInst::SingletonInstId;
  183. }
  184. break;
  185. }
  186. case SemIR::AssociatedConstantDecl::Kind: {
  187. // These are set to their final values already.
  188. break;
  189. }
  190. default:
  191. CARBON_CHECK(decl_id == SemIR::ErrorInst::SingletonInstId,
  192. "Unexpected kind of associated entity {0}", decl);
  193. witness_block[index] = SemIR::ErrorInst::SingletonInstId;
  194. break;
  195. }
  196. }
  197. // TODO: Diagnose if any declarations in the impl are not in used_decl_ids.
  198. }
  199. auto FillImplWitnessWithErrors(Context& context, SemIR::Impl& impl) -> void {
  200. if (impl.witness_id.has_value() &&
  201. impl.witness_id != SemIR::ErrorInst::SingletonInstId) {
  202. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  203. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  204. for (auto& elem : witness_block) {
  205. if (!elem.has_value()) {
  206. elem = SemIR::ErrorInst::SingletonInstId;
  207. }
  208. }
  209. }
  210. }
  211. } // namespace Carbon::Check