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/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. // Make a copy of the impl. We're going to reference it a lot, and `impl`s
  151. // could get invalidated by some of the things we do.
  152. const auto impl = context.impls().Get(impl_id);
  153. CARBON_CHECK(impl.is_being_defined());
  154. CARBON_CHECK(impl.witness_id.has_value());
  155. if (impl.witness_id == SemIR::ErrorInst::InstId) {
  156. return;
  157. }
  158. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  159. auto witness_table =
  160. context.insts().GetAs<SemIR::ImplWitnessTable>(witness.witness_table_id);
  161. auto witness_block =
  162. context.inst_blocks().GetMutable(witness_table.elements_id);
  163. auto& impl_scope = context.name_scopes().Get(impl.scope_id);
  164. auto self_type_id = context.types().GetTypeIdForTypeInstId(impl.self_id);
  165. const auto& interface = context.interfaces().Get(impl.interface.interface_id);
  166. auto assoc_entities =
  167. context.inst_blocks().Get(interface.associated_entities_id);
  168. llvm::SmallVector<SemIR::InstId> used_decl_ids;
  169. for (auto [assoc_entity, witness_value] :
  170. llvm::zip(assoc_entities, witness_block)) {
  171. auto decl_id =
  172. context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
  173. context.sem_ir(), impl.interface.specific_id, assoc_entity));
  174. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  175. auto decl = context.insts().Get(decl_id);
  176. CARBON_KIND_SWITCH(decl) {
  177. case CARBON_KIND(SemIR::StructValue struct_value): {
  178. if (struct_value.type_id == SemIR::ErrorInst::TypeId) {
  179. witness_value = SemIR::ErrorInst::InstId;
  180. break;
  181. }
  182. auto type_inst = context.types().GetAsInst(struct_value.type_id);
  183. auto fn_type = type_inst.TryAs<SemIR::FunctionType>();
  184. if (!fn_type) {
  185. CARBON_FATAL("Unexpected type: {0}", type_inst);
  186. }
  187. auto& fn = context.functions().Get(fn_type->function_id);
  188. auto lookup_result =
  189. LookupNameInExactScope(context, SemIR::LocId(decl_id), fn.name_id,
  190. impl.scope_id, impl_scope);
  191. if (lookup_result.is_found()) {
  192. used_decl_ids.push_back(lookup_result.target_inst_id());
  193. witness_value = CheckAssociatedFunctionImplementation(
  194. context, *fn_type, lookup_result.target_inst_id(), self_type_id,
  195. impl.witness_id);
  196. } else {
  197. CARBON_DIAGNOSTIC(
  198. ImplMissingFunction, Error,
  199. "missing implementation of {0} in impl of interface {1}",
  200. SemIR::NameId, SemIR::NameId);
  201. auto builder =
  202. context.emitter().Build(impl.definition_id, ImplMissingFunction,
  203. fn.name_id, interface.name_id);
  204. NoteAssociatedFunction(context, builder, fn_type->function_id);
  205. builder.Emit();
  206. witness_value = SemIR::ErrorInst::InstId;
  207. }
  208. break;
  209. }
  210. case SemIR::AssociatedConstantDecl::Kind: {
  211. // These are set to their final values already.
  212. break;
  213. }
  214. default:
  215. CARBON_CHECK(decl_id == SemIR::ErrorInst::InstId,
  216. "Unexpected kind of associated entity {0}", decl);
  217. witness_value = SemIR::ErrorInst::InstId;
  218. break;
  219. }
  220. }
  221. // TODO: Diagnose if any declarations in the impl are not in used_decl_ids.
  222. }
  223. auto FillImplWitnessWithErrors(Context& context, SemIR::Impl& impl) -> void {
  224. if (impl.witness_id == SemIR::ErrorInst::InstId) {
  225. return;
  226. }
  227. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  228. auto witness_table =
  229. context.insts().GetAs<SemIR::ImplWitnessTable>(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. impl.witness_id = SemIR::ErrorInst::InstId;
  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