impl.cpp 6.3 KB

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