impl.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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: Substitute the `Self` from the `impl` into the type in the interface
  44. // before checking. Also, this should be a semantic check rather than a
  45. // syntactic one. The functions should be allowed to have different signatures
  46. // as long as we can synthesize a suitable thunk.
  47. if (!CheckFunctionTypeMatches(context, impl_function_decl->function_id,
  48. interface_function_id, substitutions)) {
  49. return SemIR::InstId::BuiltinError;
  50. }
  51. return impl_decl_id;
  52. }
  53. // Builds a witness that the specified impl implements the given interface.
  54. static auto BuildInterfaceWitness(
  55. Context& context, const SemIR::Impl& impl, SemIR::InterfaceId interface_id,
  56. llvm::SmallVectorImpl<SemIR::InstId>& used_decl_ids) -> SemIR::InstId {
  57. const auto& interface = context.interfaces().Get(interface_id);
  58. if (!interface.is_defined()) {
  59. CARBON_DIAGNOSTIC(ImplOfUndefinedInterface, Error,
  60. "Implementation of undefined interface {0}.",
  61. SemIR::NameId);
  62. auto builder = context.emitter().Build(
  63. impl.definition_id, ImplOfUndefinedInterface, interface.name_id);
  64. context.NoteUndefinedInterface(interface_id, builder);
  65. builder.Emit();
  66. return SemIR::InstId::BuiltinError;
  67. }
  68. auto& impl_scope = context.name_scopes().Get(impl.scope_id);
  69. llvm::SmallVector<SemIR::InstId> table;
  70. auto assoc_entities =
  71. context.inst_blocks().Get(interface.associated_entities_id);
  72. table.reserve(assoc_entities.size());
  73. // Substitute `Self` with the impl's self type when associated functions.
  74. Substitution substitutions[1] = {
  75. {.bind_id = interface.self_param_id,
  76. .replacement_id = context.types().GetConstantId(impl.self_id)}};
  77. for (auto decl_id : assoc_entities) {
  78. TryResolveImportRefUnused(context, decl_id);
  79. auto const_id = context.constant_values().Get(decl_id);
  80. CARBON_CHECK(const_id.is_constant()) << "Non-constant associated entity";
  81. auto decl = context.insts().Get(const_id.inst_id());
  82. if (auto fn_decl = decl.TryAs<SemIR::FunctionDecl>()) {
  83. auto& fn = context.functions().Get(fn_decl->function_id);
  84. auto impl_decl_id =
  85. context.LookupNameInExactScope(decl_id, fn.name_id, impl_scope);
  86. if (impl_decl_id.is_valid()) {
  87. used_decl_ids.push_back(impl_decl_id);
  88. table.push_back(CheckAssociatedFunctionImplementation(
  89. context, fn_decl->function_id, impl_decl_id, substitutions));
  90. } else {
  91. CARBON_DIAGNOSTIC(
  92. ImplMissingFunction, Error,
  93. "Missing implementation of {0} in impl of interface {1}.",
  94. SemIR::NameId, SemIR::NameId);
  95. auto builder =
  96. context.emitter().Build(impl.definition_id, ImplMissingFunction,
  97. fn.name_id, interface.name_id);
  98. NoteAssociatedFunction(context, builder, fn_decl->function_id);
  99. builder.Emit();
  100. table.push_back(SemIR::InstId::BuiltinError);
  101. }
  102. } else if (auto const_decl = decl.TryAs<SemIR::AssociatedConstantDecl>()) {
  103. // TODO: Check we have a value for this constant in the constraint.
  104. context.TODO(impl.definition_id,
  105. "impl of interface with associated constant");
  106. return SemIR::InstId::BuiltinError;
  107. } else {
  108. CARBON_FATAL() << "Unexpected kind of associated entity " << decl;
  109. }
  110. }
  111. auto table_id = context.inst_blocks().Add(table);
  112. return context.AddInst(SemIR::InterfaceWitness{
  113. context.GetBuiltinType(SemIR::BuiltinKind::WitnessType), table_id});
  114. }
  115. auto BuildImplWitness(Context& context, SemIR::ImplId impl_id)
  116. -> SemIR::InstId {
  117. auto& impl = context.impls().Get(impl_id);
  118. CARBON_CHECK(impl.is_being_defined());
  119. // TODO: Handle non-interface constraints.
  120. auto interface_type =
  121. context.types().TryGetAs<SemIR::InterfaceType>(impl.constraint_id);
  122. if (!interface_type) {
  123. context.TODO(impl.definition_id, "impl as non-interface");
  124. return SemIR::InstId::BuiltinError;
  125. }
  126. llvm::SmallVector<SemIR::InstId> used_decl_ids;
  127. auto witness_id = BuildInterfaceWitness(
  128. context, impl, interface_type->interface_id, used_decl_ids);
  129. // TODO: Diagnose if any declarations in the impl are not in used_decl_ids.
  130. return witness_id;
  131. }
  132. } // namespace Carbon::Check