facet_type.cpp 11 KB

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