impl.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/inst.h"
  13. #include "toolchain/check/interface.h"
  14. #include "toolchain/check/name_lookup.h"
  15. #include "toolchain/check/type.h"
  16. #include "toolchain/check/type_completion.h"
  17. #include "toolchain/diagnostics/diagnostic_emitter.h"
  18. #include "toolchain/sem_ir/generic.h"
  19. #include "toolchain/sem_ir/ids.h"
  20. #include "toolchain/sem_ir/impl.h"
  21. #include "toolchain/sem_ir/inst.h"
  22. #include "toolchain/sem_ir/typed_insts.h"
  23. namespace Carbon::Check {
  24. // Adds the location of the associated function to a diagnostic.
  25. static auto NoteAssociatedFunction(Context& 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::InstId;
  54. }
  55. auto impl_enclosing_specific_id =
  56. context.types()
  57. .GetAs<SemIR::FunctionType>(impl_function_decl->type_id)
  58. .specific_id;
  59. // Map from the specific for the function type to the specific for the
  60. // function signature. The function signature may have additional generic
  61. // parameters.
  62. auto interface_function_specific_id =
  63. GetSelfSpecificForInterfaceMemberWithSelfType(
  64. context, SemIR::LocId(impl_decl_id),
  65. interface_function_type.specific_id,
  66. context.functions()
  67. .Get(interface_function_type.function_id)
  68. .generic_id,
  69. impl_enclosing_specific_id, self_type_id, witness_inst_id);
  70. if (!CheckFunctionTypeMatches(
  71. context, context.functions().Get(impl_function_decl->function_id),
  72. context.functions().Get(interface_function_type.function_id),
  73. interface_function_specific_id, /*check_syntax=*/false,
  74. /*check_self=*/true)) {
  75. return SemIR::ErrorInst::InstId;
  76. }
  77. return impl_decl_id;
  78. }
  79. // Builds an initial witness from the rewrites in the facet type, if any.
  80. auto ImplWitnessForDeclaration(Context& context, const SemIR::Impl& impl,
  81. bool has_definition) -> SemIR::InstId {
  82. CARBON_CHECK(!impl.has_definition_started());
  83. auto self_type_id = context.types().GetTypeIdForTypeInstId(impl.self_id);
  84. if (self_type_id == SemIR::ErrorInst::TypeId) {
  85. // When 'impl as' is invalid, the self type is an error.
  86. return SemIR::ErrorInst::InstId;
  87. }
  88. return InitialFacetTypeImplWitness(
  89. context, SemIR::LocId(impl.latest_decl_id()), impl.constraint_id,
  90. impl.self_id, impl.interface,
  91. context.generics().GetSelfSpecific(impl.generic_id), has_definition);
  92. }
  93. auto ImplWitnessStartDefinition(Context& context, SemIR::Impl& impl) -> void {
  94. CARBON_CHECK(impl.is_being_defined());
  95. CARBON_CHECK(impl.witness_id.has_value());
  96. if (impl.witness_id == SemIR::ErrorInst::InstId) {
  97. return;
  98. }
  99. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  100. auto witness_table =
  101. context.insts().GetAs<SemIR::ImplWitnessTable>(witness.witness_table_id);
  102. auto witness_block =
  103. context.inst_blocks().GetMutable(witness_table.elements_id);
  104. // `witness_table.elements_id` will be `SemIR::InstBlockId::Empty` when the
  105. // definition is the first declaration and the interface has no members. The
  106. // other case where `witness_block` will be empty is when we are using a
  107. // placeholder witness. This happens when there is a forward declaration of
  108. // the impl and the facet type has no rewrite constraints and so it wasn't
  109. // required to be complete.
  110. if (witness_table.elements_id != SemIR::InstBlockId::Empty &&
  111. witness_block.empty()) {
  112. if (!RequireCompleteFacetTypeForImplDefinition(
  113. context, SemIR::LocId(impl.latest_decl_id()), impl.constraint_id)) {
  114. return;
  115. }
  116. AllocateFacetTypeImplWitness(context, impl.interface.interface_id,
  117. witness_table.elements_id);
  118. witness_block = context.inst_blocks().GetMutable(witness_table.elements_id);
  119. }
  120. const auto& interface = context.interfaces().Get(impl.interface.interface_id);
  121. auto assoc_entities =
  122. context.inst_blocks().Get(interface.associated_entities_id);
  123. CARBON_CHECK(witness_block.size() == assoc_entities.size());
  124. // Check we have a value for all non-function associated constants in the
  125. // witness.
  126. for (auto [assoc_entity, witness_value] :
  127. llvm::zip(assoc_entities, witness_block)) {
  128. auto decl_id = context.constant_values().GetConstantInstId(assoc_entity);
  129. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  130. if (auto decl =
  131. context.insts().TryGetAs<SemIR::AssociatedConstantDecl>(decl_id)) {
  132. if (witness_value == SemIR::ImplWitnessTablePlaceholder::TypeInstId) {
  133. CARBON_DIAGNOSTIC(ImplAssociatedConstantNeedsValue, Error,
  134. "associated constant {0} not given a value in impl "
  135. "of interface {1}",
  136. SemIR::NameId, SemIR::NameId);
  137. CARBON_DIAGNOSTIC(AssociatedConstantHere, Note,
  138. "associated constant declared here");
  139. context.emitter()
  140. .Build(impl.definition_id, ImplAssociatedConstantNeedsValue,
  141. context.associated_constants()
  142. .Get(decl->assoc_const_id)
  143. .name_id,
  144. interface.name_id)
  145. .Note(assoc_entity, AssociatedConstantHere)
  146. .Emit();
  147. witness_value = SemIR::ErrorInst::InstId;
  148. }
  149. }
  150. }
  151. }
  152. // Adds functions to the witness that the specified impl implements the given
  153. // interface.
  154. auto FinishImplWitness(Context& context, SemIR::Impl& impl) -> void {
  155. CARBON_CHECK(impl.is_being_defined());
  156. CARBON_CHECK(impl.witness_id.has_value());
  157. if (impl.witness_id == SemIR::ErrorInst::InstId) {
  158. return;
  159. }
  160. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  161. auto witness_table =
  162. context.insts().GetAs<SemIR::ImplWitnessTable>(witness.witness_table_id);
  163. auto witness_block =
  164. context.inst_blocks().GetMutable(witness_table.elements_id);
  165. auto& impl_scope = context.name_scopes().Get(impl.scope_id);
  166. auto self_type_id = context.types().GetTypeIdForTypeInstId(impl.self_id);
  167. const auto& interface = context.interfaces().Get(impl.interface.interface_id);
  168. auto assoc_entities =
  169. context.inst_blocks().Get(interface.associated_entities_id);
  170. llvm::SmallVector<SemIR::InstId> used_decl_ids;
  171. for (auto [assoc_entity, witness_value] :
  172. llvm::zip(assoc_entities, witness_block)) {
  173. auto decl_id =
  174. context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
  175. context.sem_ir(), impl.interface.specific_id, assoc_entity));
  176. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  177. auto decl = context.insts().Get(decl_id);
  178. CARBON_KIND_SWITCH(decl) {
  179. case CARBON_KIND(SemIR::StructValue struct_value): {
  180. if (struct_value.type_id == SemIR::ErrorInst::TypeId) {
  181. witness_value = SemIR::ErrorInst::InstId;
  182. break;
  183. }
  184. auto type_inst = context.types().GetAsInst(struct_value.type_id);
  185. auto fn_type = type_inst.TryAs<SemIR::FunctionType>();
  186. if (!fn_type) {
  187. CARBON_FATAL("Unexpected type: {0}", type_inst);
  188. }
  189. auto& fn = context.functions().Get(fn_type->function_id);
  190. auto lookup_result =
  191. LookupNameInExactScope(context, SemIR::LocId(decl_id), fn.name_id,
  192. impl.scope_id, impl_scope);
  193. if (lookup_result.is_found()) {
  194. used_decl_ids.push_back(lookup_result.target_inst_id());
  195. witness_value = CheckAssociatedFunctionImplementation(
  196. context, *fn_type, lookup_result.target_inst_id(), self_type_id,
  197. impl.witness_id);
  198. } else {
  199. CARBON_DIAGNOSTIC(
  200. ImplMissingFunction, Error,
  201. "missing implementation of {0} in impl of interface {1}",
  202. SemIR::NameId, SemIR::NameId);
  203. auto builder =
  204. context.emitter().Build(impl.definition_id, ImplMissingFunction,
  205. fn.name_id, interface.name_id);
  206. NoteAssociatedFunction(context, builder, fn_type->function_id);
  207. builder.Emit();
  208. witness_value = SemIR::ErrorInst::InstId;
  209. }
  210. break;
  211. }
  212. case SemIR::AssociatedConstantDecl::Kind: {
  213. // These are set to their final values already.
  214. break;
  215. }
  216. default:
  217. CARBON_CHECK(decl_id == SemIR::ErrorInst::InstId,
  218. "Unexpected kind of associated entity {0}", decl);
  219. witness_value = SemIR::ErrorInst::InstId;
  220. break;
  221. }
  222. }
  223. // TODO: Diagnose if any declarations in the impl are not in used_decl_ids.
  224. }
  225. auto FillImplWitnessWithErrors(Context& context, SemIR::Impl& impl) -> void {
  226. if (impl.witness_id.has_value() &&
  227. impl.witness_id != SemIR::ErrorInst::InstId) {
  228. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  229. auto witness_table = context.insts().GetAs<SemIR::ImplWitnessTable>(
  230. witness.witness_table_id);
  231. auto witness_block =
  232. context.inst_blocks().GetMutable(witness_table.elements_id);
  233. for (auto& elem : witness_block) {
  234. if (elem == SemIR::ImplWitnessTablePlaceholder::TypeInstId) {
  235. elem = SemIR::ErrorInst::InstId;
  236. }
  237. }
  238. }
  239. }
  240. auto AssignImplIdInWitness(Context& context, SemIR::ImplId impl_id,
  241. SemIR::InstId witness_id) -> void {
  242. if (witness_id == SemIR::ErrorInst::InstId) {
  243. return;
  244. }
  245. auto witness = context.insts().GetAs<SemIR::ImplWitness>(witness_id);
  246. auto witness_table =
  247. context.insts().GetAs<SemIR::ImplWitnessTable>(witness.witness_table_id);
  248. witness_table.impl_id = impl_id;
  249. // Note: The `ImplWitnessTable` instruction is `Unique`, so while this marks
  250. // the instruction as being a dependent instruction of a generic impl, it will
  251. // not be substituted into the eval block.
  252. ReplaceInstBeforeConstantUse(context, witness.witness_table_id,
  253. witness_table);
  254. }
  255. auto IsImplEffectivelyFinal(Context& context, const SemIR::Impl& impl) -> bool {
  256. return impl.is_final ||
  257. (context.constant_values().Get(impl.self_id).is_concrete() &&
  258. context.constant_values().Get(impl.constraint_id).is_concrete());
  259. }
  260. } // namespace Carbon::Check