impl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 witness from the rewrites in the facet type, if any.
  78. auto ImplWitnessForDeclaration(Context& context, const SemIR::Impl& impl,
  79. bool has_definition) -> SemIR::InstId {
  80. CARBON_CHECK(!impl.has_definition_started());
  81. auto self_type_id = context.types().GetTypeIdForTypeInstId(impl.self_id);
  82. if (self_type_id == SemIR::ErrorInst::SingletonTypeId) {
  83. // When 'impl as' is invalid, the self type is an error.
  84. return SemIR::ErrorInst::SingletonInstId;
  85. }
  86. return InitialFacetTypeImplWitness(
  87. context, context.insts().GetLocId(impl.latest_decl_id()),
  88. impl.constraint_id, impl.self_id, impl.interface,
  89. context.generics().GetSelfSpecific(impl.generic_id), has_definition);
  90. }
  91. auto ImplWitnessStartDefinition(Context& context, SemIR::Impl& impl) -> void {
  92. CARBON_CHECK(impl.is_being_defined());
  93. CARBON_CHECK(impl.witness_id.has_value());
  94. if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
  95. return;
  96. }
  97. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  98. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  99. // `witness.elements_id` will be `SemIR::InstBlockId::Empty` when the
  100. // definition is the first declaration and the interface has no members. The
  101. // other case where `witness_block` will be empty is when we are using a
  102. // placeholder witness. This happens when there is a forward declaration of
  103. // the impl and the facet type has no rewrite constraints and so it wasn't
  104. // required to be complete.
  105. if (witness.elements_id != SemIR::InstBlockId::Empty &&
  106. witness_block.empty()) {
  107. if (!RequireCompleteFacetTypeForImplDefinition(
  108. context, impl.latest_decl_id(), impl.constraint_id)) {
  109. return;
  110. }
  111. AllocateFacetTypeImplWitness(context, impl.interface.interface_id,
  112. witness.elements_id);
  113. witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  114. }
  115. const auto& interface = context.interfaces().Get(impl.interface.interface_id);
  116. auto assoc_entities =
  117. context.inst_blocks().Get(interface.associated_entities_id);
  118. CARBON_CHECK(witness_block.size() == assoc_entities.size());
  119. // Check we have a value for all non-function associated constants in the
  120. // witness.
  121. for (auto [assoc_entity, witness_value] :
  122. llvm::zip(assoc_entities, witness_block)) {
  123. auto decl_id = context.constant_values().GetConstantInstId(assoc_entity);
  124. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  125. if (auto decl =
  126. context.insts().TryGetAs<SemIR::AssociatedConstantDecl>(decl_id)) {
  127. if (witness_value ==
  128. SemIR::ImplWitnessTablePlaceholder::SingletonInstId) {
  129. CARBON_DIAGNOSTIC(ImplAssociatedConstantNeedsValue, Error,
  130. "associated constant {0} not given a value in impl "
  131. "of interface {1}",
  132. SemIR::NameId, SemIR::NameId);
  133. CARBON_DIAGNOSTIC(AssociatedConstantHere, Note,
  134. "associated constant declared here");
  135. context.emitter()
  136. .Build(impl.definition_id, ImplAssociatedConstantNeedsValue,
  137. context.associated_constants()
  138. .Get(decl->assoc_const_id)
  139. .name_id,
  140. interface.name_id)
  141. .Note(assoc_entity, AssociatedConstantHere)
  142. .Emit();
  143. witness_value = SemIR::ErrorInst::SingletonInstId;
  144. }
  145. }
  146. }
  147. }
  148. // Adds functions to the witness that the specified impl implements the given
  149. // interface.
  150. auto FinishImplWitness(Context& context, SemIR::Impl& impl) -> void {
  151. CARBON_CHECK(impl.is_being_defined());
  152. CARBON_CHECK(impl.witness_id.has_value());
  153. if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
  154. return;
  155. }
  156. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  157. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  158. auto& impl_scope = context.name_scopes().Get(impl.scope_id);
  159. auto self_type_id = context.types().GetTypeIdForTypeInstId(impl.self_id);
  160. const auto& interface = context.interfaces().Get(impl.interface.interface_id);
  161. auto assoc_entities =
  162. context.inst_blocks().Get(interface.associated_entities_id);
  163. llvm::SmallVector<SemIR::InstId> used_decl_ids;
  164. for (auto [assoc_entity, witness_value] :
  165. llvm::zip(assoc_entities, witness_block)) {
  166. auto decl_id =
  167. context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
  168. context.sem_ir(), impl.interface.specific_id, assoc_entity));
  169. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  170. auto decl = context.insts().Get(decl_id);
  171. CARBON_KIND_SWITCH(decl) {
  172. case CARBON_KIND(SemIR::StructValue struct_value): {
  173. if (struct_value.type_id == SemIR::ErrorInst::SingletonTypeId) {
  174. witness_value = SemIR::ErrorInst::SingletonInstId;
  175. break;
  176. }
  177. auto type_inst = context.types().GetAsInst(struct_value.type_id);
  178. auto fn_type = type_inst.TryAs<SemIR::FunctionType>();
  179. if (!fn_type) {
  180. CARBON_FATAL("Unexpected type: {0}", type_inst);
  181. }
  182. auto& fn = context.functions().Get(fn_type->function_id);
  183. auto lookup_result =
  184. LookupNameInExactScope(context, context.insts().GetLocId(decl_id),
  185. fn.name_id, impl.scope_id, impl_scope);
  186. if (lookup_result.is_found()) {
  187. used_decl_ids.push_back(lookup_result.target_inst_id());
  188. witness_value = CheckAssociatedFunctionImplementation(
  189. context, *fn_type, lookup_result.target_inst_id(), self_type_id,
  190. impl.witness_id);
  191. } else {
  192. CARBON_DIAGNOSTIC(
  193. ImplMissingFunction, Error,
  194. "missing implementation of {0} in impl of interface {1}",
  195. SemIR::NameId, SemIR::NameId);
  196. auto builder =
  197. context.emitter().Build(impl.definition_id, ImplMissingFunction,
  198. fn.name_id, interface.name_id);
  199. NoteAssociatedFunction(context, builder, fn_type->function_id);
  200. builder.Emit();
  201. witness_value = SemIR::ErrorInst::SingletonInstId;
  202. }
  203. break;
  204. }
  205. case SemIR::AssociatedConstantDecl::Kind: {
  206. // These are set to their final values already.
  207. break;
  208. }
  209. default:
  210. CARBON_CHECK(decl_id == SemIR::ErrorInst::SingletonInstId,
  211. "Unexpected kind of associated entity {0}", decl);
  212. witness_value = SemIR::ErrorInst::SingletonInstId;
  213. break;
  214. }
  215. }
  216. // TODO: Diagnose if any declarations in the impl are not in used_decl_ids.
  217. }
  218. auto FillImplWitnessWithErrors(Context& context, SemIR::Impl& impl) -> void {
  219. if (impl.witness_id.has_value() &&
  220. impl.witness_id != SemIR::ErrorInst::SingletonInstId) {
  221. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  222. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  223. for (auto& elem : witness_block) {
  224. if (elem == SemIR::ImplWitnessTablePlaceholder::SingletonInstId) {
  225. elem = SemIR::ErrorInst::SingletonInstId;
  226. }
  227. }
  228. }
  229. }
  230. auto IsImplEffectivelyFinal(Context& context, const SemIR::Impl& impl) -> bool {
  231. return impl.is_final ||
  232. (context.constant_values().Get(impl.self_id).is_concrete() &&
  233. context.constant_values().Get(impl.constraint_id).is_concrete());
  234. }
  235. } // namespace Carbon::Check