impl.cpp 9.7 KB

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