impl.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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(
  49. context, context.functions().Get(impl_function_decl->function_id),
  50. context.functions().Get(interface_function_id), substitutions,
  51. /*check_syntax=*/false)) {
  52. return SemIR::InstId::BuiltinError;
  53. }
  54. return impl_decl_id;
  55. }
  56. // Builds a witness that the specified impl implements the given interface.
  57. static auto BuildInterfaceWitness(
  58. Context& context, const SemIR::Impl& impl, SemIR::InterfaceId interface_id,
  59. llvm::SmallVectorImpl<SemIR::InstId>& used_decl_ids) -> SemIR::InstId {
  60. const auto& interface = context.interfaces().Get(interface_id);
  61. if (!interface.is_defined()) {
  62. CARBON_DIAGNOSTIC(ImplOfUndefinedInterface, Error,
  63. "Implementation of undefined interface {0}.",
  64. SemIR::NameId);
  65. auto builder = context.emitter().Build(
  66. impl.definition_id, ImplOfUndefinedInterface, interface.name_id);
  67. context.NoteUndefinedInterface(interface_id, builder);
  68. builder.Emit();
  69. return SemIR::InstId::BuiltinError;
  70. }
  71. auto& impl_scope = context.name_scopes().Get(impl.scope_id);
  72. llvm::SmallVector<SemIR::InstId> table;
  73. auto assoc_entities =
  74. context.inst_blocks().Get(interface.associated_entities_id);
  75. table.reserve(assoc_entities.size());
  76. // Substitute `Self` with the impl's self type when associated functions.
  77. auto self_bind =
  78. context.insts().GetAs<SemIR::BindSymbolicName>(interface.self_param_id);
  79. Substitution substitutions[1] = {
  80. {.bind_id =
  81. context.entity_names().Get(self_bind.entity_name_id).bind_index,
  82. .replacement_id = context.types().GetConstantId(impl.self_id)}};
  83. for (auto decl_id : assoc_entities) {
  84. LoadImportRef(context, decl_id);
  85. decl_id = context.constant_values().GetConstantInstId(decl_id);
  86. CARBON_CHECK(decl_id.is_valid()) << "Non-constant associated entity";
  87. auto decl = context.insts().Get(decl_id);
  88. CARBON_KIND_SWITCH(decl) {
  89. case CARBON_KIND(SemIR::StructValue struct_value): {
  90. if (struct_value.type_id == SemIR::TypeId::Error) {
  91. return SemIR::InstId::BuiltinError;
  92. }
  93. auto type_inst = context.types().GetAsInst(struct_value.type_id);
  94. auto fn_type = type_inst.TryAs<SemIR::FunctionType>();
  95. if (!fn_type) {
  96. CARBON_FATAL() << "Unexpected type: " << type_inst;
  97. }
  98. auto& fn = context.functions().Get(fn_type->function_id);
  99. auto impl_decl_id = context.LookupNameInExactScope(
  100. decl_id, fn.name_id, impl.scope_id, impl_scope);
  101. if (impl_decl_id.is_valid()) {
  102. used_decl_ids.push_back(impl_decl_id);
  103. table.push_back(CheckAssociatedFunctionImplementation(
  104. context, fn_type->function_id, impl_decl_id, substitutions));
  105. } else {
  106. CARBON_DIAGNOSTIC(
  107. ImplMissingFunction, Error,
  108. "Missing implementation of {0} in impl of interface {1}.",
  109. SemIR::NameId, SemIR::NameId);
  110. auto builder =
  111. context.emitter().Build(impl.definition_id, ImplMissingFunction,
  112. fn.name_id, interface.name_id);
  113. NoteAssociatedFunction(context, builder, fn_type->function_id);
  114. builder.Emit();
  115. table.push_back(SemIR::InstId::BuiltinError);
  116. }
  117. break;
  118. }
  119. case SemIR::AssociatedConstantDecl::Kind:
  120. // TODO: Check we have a value for this constant in the constraint.
  121. context.TODO(impl.definition_id,
  122. "impl of interface with associated constant");
  123. return SemIR::InstId::BuiltinError;
  124. default:
  125. CARBON_FATAL() << "Unexpected kind of associated entity " << decl;
  126. }
  127. }
  128. auto table_id = context.inst_blocks().Add(table);
  129. return context.AddInst(SemIR::LocIdAndInst::NoLoc<SemIR::InterfaceWitness>(
  130. {.type_id = context.GetBuiltinType(SemIR::BuiltinInstKind::WitnessType),
  131. .elements_id = table_id}));
  132. }
  133. auto BuildImplWitness(Context& context, SemIR::ImplId impl_id)
  134. -> SemIR::InstId {
  135. auto& impl = context.impls().Get(impl_id);
  136. CARBON_CHECK(impl.is_being_defined());
  137. // TODO: Handle non-interface constraints.
  138. auto interface_type =
  139. context.types().TryGetAs<SemIR::InterfaceType>(impl.constraint_id);
  140. if (!interface_type) {
  141. context.TODO(impl.definition_id, "impl as non-interface");
  142. return SemIR::InstId::BuiltinError;
  143. }
  144. llvm::SmallVector<SemIR::InstId> used_decl_ids;
  145. auto witness_id = BuildInterfaceWitness(
  146. context, impl, interface_type->interface_id, used_decl_ids);
  147. // TODO: Diagnose if any declarations in the impl are not in used_decl_ids.
  148. return witness_id;
  149. }
  150. } // namespace Carbon::Check