impl.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/thunk.h"
  16. #include "toolchain/check/type.h"
  17. #include "toolchain/check/type_completion.h"
  18. #include "toolchain/diagnostics/diagnostic_emitter.h"
  19. #include "toolchain/sem_ir/generic.h"
  20. #include "toolchain/sem_ir/ids.h"
  21. #include "toolchain/sem_ir/impl.h"
  22. #include "toolchain/sem_ir/inst.h"
  23. #include "toolchain/sem_ir/typed_insts.h"
  24. namespace Carbon::Check {
  25. // Adds the location of the associated function to a diagnostic.
  26. static auto NoteAssociatedFunction(Context& context, DiagnosticBuilder& builder,
  27. SemIR::FunctionId function_id) -> void {
  28. CARBON_DIAGNOSTIC(AssociatedFunctionHere, Note,
  29. "associated function {0} declared here", SemIR::NameId);
  30. const auto& function = context.functions().Get(function_id);
  31. builder.Note(function.latest_decl_id(), AssociatedFunctionHere,
  32. function.name_id);
  33. }
  34. // Checks that `impl_function_id` is a valid implementation of the function
  35. // described in the interface as `interface_function_id`. Returns the value to
  36. // put into the corresponding slot in the witness table, which can be
  37. // `BuiltinErrorInst` if the function is not usable.
  38. static auto CheckAssociatedFunctionImplementation(
  39. Context& context, SemIR::FunctionType interface_function_type,
  40. SemIR::InstId impl_decl_id, SemIR::TypeId self_type_id,
  41. SemIR::InstId witness_inst_id) -> SemIR::InstId {
  42. auto impl_function_decl =
  43. context.insts().TryGetAs<SemIR::FunctionDecl>(impl_decl_id);
  44. if (!impl_function_decl) {
  45. CARBON_DIAGNOSTIC(ImplFunctionWithNonFunction, Error,
  46. "associated function {0} implemented by non-function",
  47. SemIR::NameId);
  48. auto builder = context.emitter().Build(
  49. impl_decl_id, ImplFunctionWithNonFunction,
  50. context.functions().Get(interface_function_type.function_id).name_id);
  51. NoteAssociatedFunction(context, builder,
  52. interface_function_type.function_id);
  53. builder.Emit();
  54. return SemIR::ErrorInst::InstId;
  55. }
  56. auto impl_enclosing_specific_id =
  57. context.types()
  58. .GetAs<SemIR::FunctionType>(impl_function_decl->type_id)
  59. .specific_id;
  60. // Map from the specific for the function type to the specific for the
  61. // function signature. The function signature may have additional generic
  62. // parameters.
  63. auto interface_function_specific_id =
  64. GetSelfSpecificForInterfaceMemberWithSelfType(
  65. context, SemIR::LocId(impl_decl_id),
  66. interface_function_type.specific_id,
  67. context.functions()
  68. .Get(interface_function_type.function_id)
  69. .generic_id,
  70. impl_enclosing_specific_id, self_type_id, witness_inst_id);
  71. return BuildThunk(context, interface_function_type.function_id,
  72. interface_function_specific_id, impl_decl_id);
  73. }
  74. // Builds an initial witness from the rewrites in the facet type, if any.
  75. auto ImplWitnessForDeclaration(Context& context, const SemIR::Impl& impl,
  76. bool has_definition) -> 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::TypeId) {
  80. // When 'impl as' is invalid, the self type is an error.
  81. return SemIR::ErrorInst::InstId;
  82. }
  83. return InitialFacetTypeImplWitness(
  84. context, SemIR::LocId(impl.latest_decl_id()), impl.constraint_id,
  85. impl.self_id, impl.interface,
  86. context.generics().GetSelfSpecific(impl.generic_id), has_definition);
  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::InstId) {
  92. return;
  93. }
  94. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  95. auto witness_table =
  96. context.insts().GetAs<SemIR::ImplWitnessTable>(witness.witness_table_id);
  97. auto witness_block =
  98. context.inst_blocks().GetMutable(witness_table.elements_id);
  99. // `witness_table.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_table.elements_id != SemIR::InstBlockId::Empty &&
  106. witness_block.empty()) {
  107. if (!RequireCompleteFacetTypeForImplDefinition(
  108. context, SemIR::LocId(impl.latest_decl_id()), impl.constraint_id)) {
  109. return;
  110. }
  111. AllocateFacetTypeImplWitness(context, impl.interface.interface_id,
  112. witness_table.elements_id);
  113. witness_block = context.inst_blocks().GetMutable(witness_table.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 == SemIR::ImplWitnessTablePlaceholder::TypeInstId) {
  128. CARBON_DIAGNOSTIC(ImplAssociatedConstantNeedsValue, Error,
  129. "associated constant {0} not given a value in impl "
  130. "of interface {1}",
  131. SemIR::NameId, SemIR::NameId);
  132. CARBON_DIAGNOSTIC(AssociatedConstantHere, Note,
  133. "associated constant declared here");
  134. context.emitter()
  135. .Build(impl.definition_id, ImplAssociatedConstantNeedsValue,
  136. context.associated_constants()
  137. .Get(decl->assoc_const_id)
  138. .name_id,
  139. interface.name_id)
  140. .Note(assoc_entity, AssociatedConstantHere)
  141. .Emit();
  142. witness_value = SemIR::ErrorInst::InstId;
  143. }
  144. }
  145. }
  146. }
  147. // Adds functions to the witness that the specified impl implements the given
  148. // interface.
  149. auto FinishImplWitness(Context& context, SemIR::ImplId impl_id) -> void {
  150. const auto& impl = context.impls().Get(impl_id);
  151. CARBON_CHECK(impl.is_being_defined());
  152. CARBON_CHECK(impl.witness_id.has_value());
  153. if (impl.witness_id == SemIR::ErrorInst::InstId) {
  154. return;
  155. }
  156. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  157. auto witness_table =
  158. context.insts().GetAs<SemIR::ImplWitnessTable>(witness.witness_table_id);
  159. auto witness_block =
  160. context.inst_blocks().GetMutable(witness_table.elements_id);
  161. auto& impl_scope = context.name_scopes().Get(impl.scope_id);
  162. auto self_type_id = context.types().GetTypeIdForTypeInstId(impl.self_id);
  163. const auto& interface = context.interfaces().Get(impl.interface.interface_id);
  164. auto assoc_entities =
  165. context.inst_blocks().Get(interface.associated_entities_id);
  166. llvm::SmallVector<SemIR::InstId> used_decl_ids;
  167. for (auto [assoc_entity, witness_value] :
  168. llvm::zip(assoc_entities, witness_block)) {
  169. auto decl_id =
  170. context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
  171. context.sem_ir(), impl.interface.specific_id, assoc_entity));
  172. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  173. auto decl = context.insts().Get(decl_id);
  174. CARBON_KIND_SWITCH(decl) {
  175. case CARBON_KIND(SemIR::StructValue struct_value): {
  176. if (struct_value.type_id == SemIR::ErrorInst::TypeId) {
  177. witness_value = SemIR::ErrorInst::InstId;
  178. break;
  179. }
  180. auto type_inst = context.types().GetAsInst(struct_value.type_id);
  181. auto fn_type = type_inst.TryAs<SemIR::FunctionType>();
  182. if (!fn_type) {
  183. CARBON_FATAL("Unexpected type: {0}", type_inst);
  184. }
  185. auto& fn = context.functions().Get(fn_type->function_id);
  186. auto lookup_result =
  187. LookupNameInExactScope(context, SemIR::LocId(decl_id), fn.name_id,
  188. impl.scope_id, impl_scope);
  189. if (lookup_result.is_found()) {
  190. used_decl_ids.push_back(lookup_result.target_inst_id());
  191. witness_value = CheckAssociatedFunctionImplementation(
  192. context, *fn_type, lookup_result.target_inst_id(), self_type_id,
  193. impl.witness_id);
  194. } else {
  195. CARBON_DIAGNOSTIC(
  196. ImplMissingFunction, Error,
  197. "missing implementation of {0} in impl of interface {1}",
  198. SemIR::NameId, SemIR::NameId);
  199. auto builder =
  200. context.emitter().Build(impl.definition_id, ImplMissingFunction,
  201. fn.name_id, interface.name_id);
  202. NoteAssociatedFunction(context, builder, fn_type->function_id);
  203. builder.Emit();
  204. witness_value = SemIR::ErrorInst::InstId;
  205. }
  206. break;
  207. }
  208. case SemIR::AssociatedConstantDecl::Kind: {
  209. // These are set to their final values already.
  210. break;
  211. }
  212. default:
  213. CARBON_CHECK(decl_id == SemIR::ErrorInst::InstId,
  214. "Unexpected kind of associated entity {0}", decl);
  215. witness_value = SemIR::ErrorInst::InstId;
  216. break;
  217. }
  218. }
  219. // TODO: Diagnose if any declarations in the impl are not in used_decl_ids.
  220. }
  221. auto FillImplWitnessWithErrors(Context& context, SemIR::Impl& impl) -> void {
  222. if (impl.witness_id == SemIR::ErrorInst::InstId) {
  223. return;
  224. }
  225. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  226. auto witness_table =
  227. context.insts().GetAs<SemIR::ImplWitnessTable>(witness.witness_table_id);
  228. auto witness_block =
  229. context.inst_blocks().GetMutable(witness_table.elements_id);
  230. for (auto& elem : witness_block) {
  231. if (elem == SemIR::ImplWitnessTablePlaceholder::TypeInstId) {
  232. elem = SemIR::ErrorInst::InstId;
  233. }
  234. }
  235. impl.witness_id = SemIR::ErrorInst::InstId;
  236. }
  237. auto AssignImplIdInWitness(Context& context, SemIR::ImplId impl_id,
  238. SemIR::InstId witness_id) -> void {
  239. if (witness_id == SemIR::ErrorInst::InstId) {
  240. return;
  241. }
  242. auto witness = context.insts().GetAs<SemIR::ImplWitness>(witness_id);
  243. auto witness_table =
  244. context.insts().GetAs<SemIR::ImplWitnessTable>(witness.witness_table_id);
  245. witness_table.impl_id = impl_id;
  246. // Note: The `ImplWitnessTable` instruction is `Unique`, so while this marks
  247. // the instruction as being a dependent instruction of a generic impl, it will
  248. // not be substituted into the eval block.
  249. ReplaceInstBeforeConstantUse(context, witness.witness_table_id,
  250. witness_table);
  251. }
  252. auto IsImplEffectivelyFinal(Context& context, const SemIR::Impl& impl) -> bool {
  253. return impl.is_final ||
  254. (context.constant_values().Get(impl.self_id).is_concrete() &&
  255. context.constant_values().Get(impl.constraint_id).is_concrete());
  256. }
  257. } // namespace Carbon::Check