impl.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/function.h"
  8. #include "toolchain/check/import_ref.h"
  9. #include "toolchain/check/subst.h"
  10. #include "toolchain/diagnostics/diagnostic_emitter.h"
  11. #include "toolchain/sem_ir/ids.h"
  12. #include "toolchain/sem_ir/impl.h"
  13. #include "toolchain/sem_ir/inst.h"
  14. #include "toolchain/sem_ir/typed_insts.h"
  15. namespace Carbon::Check {
  16. // Adds the location of the associated function to a diagnostic.
  17. static auto NoteAssociatedFunction(Context& context,
  18. Context::DiagnosticBuilder& builder,
  19. SemIR::FunctionId function_id) -> void {
  20. CARBON_DIAGNOSTIC(ImplAssociatedFunctionHere, Note,
  21. "Associated function {0} declared here.", SemIR::NameId);
  22. const auto& function = context.functions().Get(function_id);
  23. builder.Note(function.decl_id, ImplAssociatedFunctionHere, function.name_id);
  24. }
  25. // Checks that `impl_function_id` is a valid implementation of the function
  26. // described in the interface as `interface_function_id`. Returns the value to
  27. // put into the corresponding slot in the witness table, which can be
  28. // `BuiltinError` if the function is not usable.
  29. static auto CheckAssociatedFunctionImplementation(
  30. Context& context, SemIR::FunctionId interface_function_id,
  31. SemIR::InstId impl_decl_id, Substitutions substitutions) -> SemIR::InstId {
  32. auto impl_function_decl =
  33. context.insts().TryGetAs<SemIR::FunctionDecl>(impl_decl_id);
  34. if (!impl_function_decl) {
  35. CARBON_DIAGNOSTIC(ImplFunctionWithNonFunction, Error,
  36. "Associated function {0} implemented by non-function.",
  37. SemIR::NameId);
  38. auto builder = context.emitter().Build(
  39. impl_decl_id, ImplFunctionWithNonFunction,
  40. context.functions().Get(interface_function_id).name_id);
  41. NoteAssociatedFunction(context, builder, interface_function_id);
  42. builder.Emit();
  43. return SemIR::InstId::BuiltinError;
  44. }
  45. // TODO: This should be a semantic check rather than a syntactic one. The
  46. // functions should be allowed to have different signatures as long as we can
  47. // synthesize a suitable thunk.
  48. if (!CheckFunctionTypeMatches(context, impl_function_decl->function_id,
  49. interface_function_id, substitutions)) {
  50. return SemIR::InstId::BuiltinError;
  51. }
  52. return impl_decl_id;
  53. }
  54. // Builds a witness that the specified impl implements the given interface.
  55. static auto BuildInterfaceWitness(
  56. Context& context, const SemIR::Impl& impl, SemIR::InterfaceId interface_id,
  57. llvm::SmallVectorImpl<SemIR::InstId>& used_decl_ids) -> SemIR::InstId {
  58. const auto& interface = context.interfaces().Get(interface_id);
  59. if (!interface.is_defined()) {
  60. CARBON_DIAGNOSTIC(ImplOfUndefinedInterface, Error,
  61. "Implementation of undefined interface {0}.",
  62. SemIR::NameId);
  63. auto builder = context.emitter().Build(
  64. impl.definition_id, ImplOfUndefinedInterface, interface.name_id);
  65. context.NoteUndefinedInterface(interface_id, builder);
  66. builder.Emit();
  67. return SemIR::InstId::BuiltinError;
  68. }
  69. auto& impl_scope = context.name_scopes().Get(impl.scope_id);
  70. llvm::SmallVector<SemIR::InstId> table;
  71. auto assoc_entities =
  72. context.inst_blocks().Get(interface.associated_entities_id);
  73. table.reserve(assoc_entities.size());
  74. // Substitute `Self` with the impl's self type when associated functions.
  75. auto self_bind =
  76. context.insts().GetAs<SemIR::BindSymbolicName>(interface.self_param_id);
  77. Substitution substitutions[1] = {
  78. {.bind_id = context.bind_names().Get(self_bind.bind_name_id).bind_index,
  79. .replacement_id = context.types().GetConstantId(impl.self_id)}};
  80. for (auto decl_id : assoc_entities) {
  81. LoadImportRef(context, decl_id, impl.definition_id);
  82. auto const_id = context.constant_values().Get(decl_id);
  83. CARBON_CHECK(const_id.is_constant()) << "Non-constant associated entity";
  84. auto decl = context.insts().Get(const_id.inst_id());
  85. CARBON_KIND_SWITCH(decl) {
  86. case CARBON_KIND(SemIR::StructValue struct_value): {
  87. if (struct_value.type_id == SemIR::TypeId::Error) {
  88. return SemIR::InstId::BuiltinError;
  89. }
  90. auto type_inst = context.types().GetAsInst(struct_value.type_id);
  91. auto fn_type = type_inst.TryAs<SemIR::FunctionType>();
  92. if (!fn_type) {
  93. CARBON_FATAL() << "Unexpected type: " << type_inst;
  94. }
  95. auto& fn = context.functions().Get(fn_type->function_id);
  96. auto impl_decl_id = context.LookupNameInExactScope(
  97. decl_id, fn.name_id, impl_scope, /*mark_imports_used=*/true);
  98. if (impl_decl_id.is_valid()) {
  99. used_decl_ids.push_back(impl_decl_id);
  100. table.push_back(CheckAssociatedFunctionImplementation(
  101. context, fn_type->function_id, impl_decl_id, substitutions));
  102. } else {
  103. CARBON_DIAGNOSTIC(
  104. ImplMissingFunction, Error,
  105. "Missing implementation of {0} in impl of interface {1}.",
  106. SemIR::NameId, SemIR::NameId);
  107. auto builder =
  108. context.emitter().Build(impl.definition_id, ImplMissingFunction,
  109. fn.name_id, interface.name_id);
  110. NoteAssociatedFunction(context, builder, fn_type->function_id);
  111. builder.Emit();
  112. table.push_back(SemIR::InstId::BuiltinError);
  113. }
  114. break;
  115. }
  116. case SemIR::AssociatedConstantDecl::Kind:
  117. // TODO: Check we have a value for this constant in the constraint.
  118. context.TODO(impl.definition_id,
  119. "impl of interface with associated constant");
  120. return SemIR::InstId::BuiltinError;
  121. default:
  122. CARBON_FATAL() << "Unexpected kind of associated entity " << decl;
  123. }
  124. }
  125. auto table_id = context.inst_blocks().Add(table);
  126. return context.AddInst(SemIR::LocIdAndInst::NoLoc(SemIR::InterfaceWitness{
  127. context.GetBuiltinType(SemIR::BuiltinKind::WitnessType), table_id}));
  128. }
  129. auto BuildImplWitness(Context& context, SemIR::ImplId impl_id)
  130. -> SemIR::InstId {
  131. auto& impl = context.impls().Get(impl_id);
  132. CARBON_CHECK(impl.is_being_defined());
  133. // TODO: Handle non-interface constraints.
  134. auto interface_type =
  135. context.types().TryGetAs<SemIR::InterfaceType>(impl.constraint_id);
  136. if (!interface_type) {
  137. context.TODO(impl.definition_id, "impl as non-interface");
  138. return SemIR::InstId::BuiltinError;
  139. }
  140. llvm::SmallVector<SemIR::InstId> used_decl_ids;
  141. auto witness_id = BuildInterfaceWitness(
  142. context, impl, interface_type->interface_id, used_decl_ids);
  143. // TODO: Diagnose if any declarations in the impl are not in used_decl_ids.
  144. return witness_id;
  145. }
  146. } // namespace Carbon::Check