facet_type.cpp 12 KB

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