impl.cpp 5.9 KB

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