impl.cpp 7.1 KB

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