facet_type.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/facet_type.h"
  5. #include "toolchain/check/convert.h"
  6. #include "toolchain/check/import_ref.h"
  7. #include "toolchain/check/inst.h"
  8. #include "toolchain/check/interface.h"
  9. #include "toolchain/check/type.h"
  10. namespace Carbon::Check {
  11. auto FacetTypeFromInterface(Context& context, SemIR::InterfaceId interface_id,
  12. SemIR::SpecificId specific_id) -> SemIR::FacetType {
  13. SemIR::FacetTypeId facet_type_id = context.facet_types().Add(
  14. SemIR::FacetTypeInfo{.impls_constraints = {{interface_id, specific_id}},
  15. .other_requirements = false});
  16. return {.type_id = SemIR::TypeType::SingletonTypeId,
  17. .facet_type_id = facet_type_id};
  18. }
  19. // Returns `true` if the `FacetAccessWitness` of `witness_id` matches
  20. // `interface`.
  21. static auto WitnessAccessMatchesInterface(
  22. Context& context, SemIR::InstId witness_id,
  23. const SemIR::SpecificInterface& interface) -> bool {
  24. auto access = context.insts().GetAs<SemIR::FacetAccessWitness>(witness_id);
  25. auto type_id = context.insts().Get(access.facet_value_inst_id).type_id();
  26. auto facet_type = context.types().GetAs<SemIR::FacetType>(type_id);
  27. const auto& facet_info = context.facet_types().Get(facet_type.facet_type_id);
  28. if (auto impls = facet_info.TryAsSingleInterface()) {
  29. return impls->interface_id == interface.interface_id &&
  30. impls->specific_id == interface.specific_id;
  31. }
  32. return false;
  33. }
  34. auto ResolveFacetTypeImplWitness(
  35. Context& context, SemIR::LocId witness_loc_id,
  36. SemIR::InstId facet_type_inst_id, SemIR::InstId self_type_inst_id,
  37. const SemIR::SpecificInterface& interface_to_witness,
  38. SemIR::SpecificId self_specific_id) -> SemIR::InstId {
  39. // TODO: Finish facet type resolution. This code currently only handles
  40. // rewrite constraints that set associated constants to a concrete value.
  41. // Need logic to topologically sort rewrites to respect dependencies, and
  42. // afterwards reject duplicates that are not identical.
  43. const auto& interface =
  44. context.interfaces().Get(interface_to_witness.interface_id);
  45. auto assoc_entities =
  46. context.inst_blocks().Get(interface.associated_entities_id);
  47. // TODO: When this function is used for things other than just impls, may want
  48. // to only load the specific associated entities that are mentioned in rewrite
  49. // rules.
  50. for (auto decl_id : assoc_entities) {
  51. LoadImportRef(context, decl_id);
  52. }
  53. SemIR::InstId witness_inst_id = SemIR::InstId::None;
  54. llvm::MutableArrayRef<SemIR::InstId> table;
  55. {
  56. llvm::SmallVector<SemIR::InstId> empty_table(assoc_entities.size(),
  57. SemIR::InstId::None);
  58. auto table_id = context.inst_blocks().Add(empty_table);
  59. table = context.inst_blocks().GetMutable(table_id);
  60. witness_inst_id = AddInst<SemIR::ImplWitness>(
  61. context, witness_loc_id,
  62. {.type_id =
  63. GetSingletonType(context, SemIR::WitnessType::SingletonInstId),
  64. .elements_id = table_id,
  65. .specific_id = self_specific_id});
  66. }
  67. auto facet_type_id =
  68. context.types().GetTypeIdForTypeInstId(facet_type_inst_id);
  69. CARBON_CHECK(facet_type_id != SemIR::ErrorInst::SingletonTypeId);
  70. auto facet_type = context.types().GetAs<SemIR::FacetType>(facet_type_id);
  71. // TODO: This is currently a copy because I'm not sure whether anything could
  72. // cause the facet type store to resize before we are done with it.
  73. auto facet_type_info = context.facet_types().Get(facet_type.facet_type_id);
  74. for (auto rewrite : facet_type_info.rewrite_constraints) {
  75. auto inst_id = context.constant_values().GetInstId(rewrite.lhs_const_id);
  76. auto access = context.insts().GetAs<SemIR::ImplWitnessAccess>(inst_id);
  77. if (!WitnessAccessMatchesInterface(context, access.witness_id,
  78. interface_to_witness)) {
  79. continue;
  80. }
  81. auto& table_entry = table[access.index.index];
  82. if (table_entry == SemIR::ErrorInst::SingletonInstId) {
  83. // Don't overwrite an error value. This prioritizes not generating
  84. // multiple errors for one associated constant over picking a value
  85. // for it to use to attempt recovery.
  86. continue;
  87. }
  88. auto rewrite_value = rewrite.rhs_const_id;
  89. if (table_entry.has_value()) {
  90. auto const_id = context.constant_values().Get(table_entry);
  91. if (const_id != rewrite_value &&
  92. rewrite_value != SemIR::ErrorInst::SingletonConstantId) {
  93. table_entry = SemIR::ErrorInst::SingletonInstId;
  94. // TODO: Figure out how to print the two different values
  95. // `const_id` & `rewrite_value` in the diagnostic
  96. // message.
  97. CARBON_DIAGNOSTIC(AssociatedConstantWithDifferentValues, Error,
  98. "associated constant {0} given two different values",
  99. SemIR::NameId);
  100. auto decl_id = assoc_entities[access.index.index];
  101. SemIR::NameId name_id = SemIR::NameId::None;
  102. if (auto decl = context.insts().TryGetAs<SemIR::AssociatedConstantDecl>(
  103. decl_id)) {
  104. auto& assoc_const =
  105. context.associated_constants().Get(decl->assoc_const_id);
  106. name_id = assoc_const.name_id;
  107. } else {
  108. auto import_ref = context.insts().GetAs<SemIR::AnyImportRef>(decl_id);
  109. const auto& entity_name =
  110. context.entity_names().Get(import_ref.entity_name_id);
  111. name_id = entity_name.name_id;
  112. }
  113. context.emitter().Emit(facet_type_inst_id,
  114. AssociatedConstantWithDifferentValues, name_id);
  115. }
  116. continue;
  117. }
  118. auto decl_id = context.constant_values().GetConstantInstId(
  119. assoc_entities[access.index.index]);
  120. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  121. if (auto decl =
  122. context.insts().TryGetAs<SemIR::AssociatedConstantDecl>(decl_id)) {
  123. // If the associated constant has a symbolic type, convert the rewrite
  124. // value to that type now we know the value of `Self`.
  125. SemIR::TypeId assoc_const_type_id = decl->type_id;
  126. if (context.types().GetConstantId(assoc_const_type_id).is_symbolic()) {
  127. // Get the type of the associated constant in this interface with this
  128. // value for `Self`.
  129. assoc_const_type_id = GetTypeForSpecificAssociatedEntity(
  130. context, facet_type_inst_id, interface_to_witness.specific_id,
  131. decl_id, context.types().GetTypeIdForTypeInstId(self_type_inst_id),
  132. witness_inst_id);
  133. // Perform the conversion of the value to the type. We skipped this when
  134. // forming the facet type because the type of the associated constant
  135. // was symbolic.
  136. auto converted_inst_id = ConvertToValueOfType(
  137. context, context.insts().GetLocId(facet_type_inst_id),
  138. context.constant_values().GetInstId(rewrite_value),
  139. assoc_const_type_id);
  140. rewrite_value = context.constant_values().Get(converted_inst_id);
  141. // The result of conversion can be non-constant even if the original
  142. // value was constant.
  143. if (!rewrite_value.is_constant() &&
  144. rewrite_value != SemIR::ErrorInst::SingletonConstantId) {
  145. const auto& assoc_const =
  146. context.associated_constants().Get(decl->assoc_const_id);
  147. CARBON_DIAGNOSTIC(
  148. AssociatedConstantNotConstantAfterConversion, Error,
  149. "associated constant {0} given value that is not constant "
  150. "after conversion to {1}",
  151. SemIR::NameId, SemIR::TypeId);
  152. context.emitter().Emit(facet_type_inst_id,
  153. AssociatedConstantNotConstantAfterConversion,
  154. assoc_const.name_id, assoc_const_type_id);
  155. rewrite_value = SemIR::ErrorInst::SingletonConstantId;
  156. }
  157. }
  158. } else {
  159. if (decl_id != SemIR::ErrorInst::SingletonInstId) {
  160. auto type_id = context.insts().Get(decl_id).type_id();
  161. auto type_inst = context.types().GetAsInst(type_id);
  162. auto fn_type = type_inst.As<SemIR::FunctionType>();
  163. const auto& fn = context.functions().Get(fn_type.function_id);
  164. CARBON_DIAGNOSTIC(RewriteForAssociatedFunction, Error,
  165. "rewrite specified for associated function {0}",
  166. SemIR::NameId);
  167. context.emitter().Emit(facet_type_inst_id, RewriteForAssociatedFunction,
  168. fn.name_id);
  169. }
  170. continue;
  171. }
  172. table_entry = context.constant_values().GetInstId(rewrite_value);
  173. }
  174. return witness_inst_id;
  175. }
  176. } // namespace Carbon::Check