impl.cpp 12 KB

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