facet_type.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 `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, SemIR::LocId loc_id, SemIR::TypeInstId 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_id, 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_id, ImplAsIncompleteFacetTypeRewrites,
  49. facet_type_inst_id);
  50. }
  51. }
  52. auto InitialFacetTypeImplWitness(
  53. Context& context, SemIR::LocId witness_loc_id,
  54. SemIR::TypeInstId facet_type_inst_id, SemIR::TypeInstId 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::TypeId);
  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. auto witness_table_inst_id = AddInst<SemIR::ImplWitnessTable>(
  70. context, witness_loc_id,
  71. {.elements_id = context.inst_blocks().AddPlaceholder(),
  72. .impl_id = SemIR::ImplId::None});
  73. return AddInst<SemIR::ImplWitness>(
  74. context, witness_loc_id,
  75. {.type_id = GetSingletonType(context, SemIR::WitnessType::TypeInstId),
  76. .witness_table_id = witness_table_inst_id,
  77. .specific_id = self_specific_id});
  78. }
  79. if (!RequireCompleteType(context, facet_type_id,
  80. context.insts().GetLocId(facet_type_inst_id), [&] {
  81. return IncompleteFacetTypeDiagnosticBuilder(
  82. context, witness_loc_id, facet_type_inst_id,
  83. is_definition);
  84. })) {
  85. return SemIR::ErrorInst::InstId;
  86. }
  87. const auto& interface =
  88. context.interfaces().Get(interface_to_witness.interface_id);
  89. auto assoc_entities =
  90. context.inst_blocks().Get(interface.associated_entities_id);
  91. // TODO: When this function is used for things other than just impls, may want
  92. // to only load the specific associated entities that are mentioned in rewrite
  93. // rules.
  94. for (auto decl_id : assoc_entities) {
  95. LoadImportRef(context, decl_id);
  96. }
  97. SemIR::InstId witness_inst_id = SemIR::InstId::None;
  98. llvm::MutableArrayRef<SemIR::InstId> table;
  99. {
  100. auto elements_id =
  101. context.inst_blocks().AddUninitialized(assoc_entities.size());
  102. table = context.inst_blocks().GetMutable(elements_id);
  103. for (auto& uninit : table) {
  104. uninit = SemIR::ImplWitnessTablePlaceholder::TypeInstId;
  105. }
  106. auto witness_table_inst_id = AddInst<SemIR::ImplWitnessTable>(
  107. context, witness_loc_id,
  108. {.elements_id = elements_id, .impl_id = SemIR::ImplId::None});
  109. witness_inst_id = AddInst<SemIR::ImplWitness>(
  110. context, witness_loc_id,
  111. {.type_id = GetSingletonType(context, SemIR::WitnessType::TypeInstId),
  112. .witness_table_id = witness_table_inst_id,
  113. .specific_id = self_specific_id});
  114. }
  115. for (auto rewrite : facet_type_info.rewrite_constraints) {
  116. auto access =
  117. context.insts().GetAs<SemIR::ImplWitnessAccess>(rewrite.lhs_id);
  118. if (!WitnessAccessMatchesInterface(context, access.witness_id,
  119. interface_to_witness)) {
  120. continue;
  121. }
  122. auto& table_entry = table[access.index.index];
  123. if (table_entry == SemIR::ErrorInst::InstId) {
  124. // Don't overwrite an error value. This prioritizes not generating
  125. // multiple errors for one associated constant over picking a value
  126. // for it to use to attempt recovery.
  127. continue;
  128. }
  129. auto rewrite_inst_id = rewrite.rhs_id;
  130. if (rewrite_inst_id == SemIR::ErrorInst::InstId) {
  131. table_entry = SemIR::ErrorInst::InstId;
  132. continue;
  133. }
  134. auto decl_id = context.constant_values().GetConstantInstId(
  135. assoc_entities[access.index.index]);
  136. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  137. if (decl_id == SemIR::ErrorInst::InstId) {
  138. table_entry = SemIR::ErrorInst::InstId;
  139. continue;
  140. }
  141. auto assoc_constant_decl =
  142. context.insts().TryGetAs<SemIR::AssociatedConstantDecl>(decl_id);
  143. if (!assoc_constant_decl) {
  144. auto type_id = context.insts().Get(decl_id).type_id();
  145. auto type_inst = context.types().GetAsInst(type_id);
  146. auto fn_type = type_inst.As<SemIR::FunctionType>();
  147. const auto& fn = context.functions().Get(fn_type.function_id);
  148. CARBON_DIAGNOSTIC(RewriteForAssociatedFunction, Error,
  149. "rewrite specified for associated function {0}",
  150. SemIR::NameId);
  151. context.emitter().Emit(facet_type_inst_id, RewriteForAssociatedFunction,
  152. fn.name_id);
  153. table_entry = SemIR::ErrorInst::InstId;
  154. continue;
  155. }
  156. if (table_entry != SemIR::ImplWitnessTablePlaceholder::TypeInstId) {
  157. if (table_entry != rewrite_inst_id) {
  158. // TODO: Figure out how to print the two different values
  159. // `const_id` & `rewrite_inst_id` in the diagnostic
  160. // message.
  161. CARBON_DIAGNOSTIC(
  162. AssociatedConstantWithDifferentValues, Error,
  163. "associated constant {0} given two different values {1} and {2}",
  164. SemIR::NameId, InstIdAsConstant, InstIdAsConstant);
  165. auto& assoc_const = context.associated_constants().Get(
  166. assoc_constant_decl->assoc_const_id);
  167. context.emitter().Emit(
  168. facet_type_inst_id, AssociatedConstantWithDifferentValues,
  169. assoc_const.name_id, table_entry, rewrite_inst_id);
  170. }
  171. table_entry = SemIR::ErrorInst::InstId;
  172. continue;
  173. }
  174. // If the associated constant has a symbolic type, convert the rewrite
  175. // value to that type now we know the value of `Self`.
  176. SemIR::TypeId assoc_const_type_id = assoc_constant_decl->type_id;
  177. if (assoc_const_type_id.is_symbolic()) {
  178. // Get the type of the associated constant in this interface with this
  179. // value for `Self`.
  180. assoc_const_type_id = GetTypeForSpecificAssociatedEntity(
  181. context, facet_type_inst_id, interface_to_witness.specific_id,
  182. decl_id, context.types().GetTypeIdForTypeInstId(self_type_inst_id),
  183. witness_inst_id);
  184. // Perform the conversion of the value to the type. We skipped this when
  185. // forming the facet type because the type of the associated constant
  186. // was symbolic.
  187. auto converted_inst_id = ConvertToValueOfType(
  188. context, context.insts().GetLocId(facet_type_inst_id),
  189. rewrite_inst_id, assoc_const_type_id);
  190. // Canonicalize the converted constant value.
  191. converted_inst_id =
  192. context.constant_values().GetConstantInstId(converted_inst_id);
  193. // The result of conversion can be non-constant even if the original
  194. // value was constant.
  195. if (converted_inst_id.has_value()) {
  196. rewrite_inst_id = converted_inst_id;
  197. } else {
  198. const auto& assoc_const = context.associated_constants().Get(
  199. assoc_constant_decl->assoc_const_id);
  200. CARBON_DIAGNOSTIC(
  201. AssociatedConstantNotConstantAfterConversion, Error,
  202. "associated constant {0} given value {1} that is not constant "
  203. "after conversion to {2}",
  204. SemIR::NameId, InstIdAsConstant, SemIR::TypeId);
  205. context.emitter().Emit(
  206. facet_type_inst_id, AssociatedConstantNotConstantAfterConversion,
  207. assoc_const.name_id, rewrite_inst_id, assoc_const_type_id);
  208. rewrite_inst_id = SemIR::ErrorInst::InstId;
  209. }
  210. }
  211. CARBON_CHECK(rewrite_inst_id == context.constant_values().GetConstantInstId(
  212. rewrite_inst_id),
  213. "Rewritten value for associated constant is not canonical.");
  214. table_entry = AddInst<SemIR::ImplWitnessAssociatedConstant>(
  215. context, witness_loc_id,
  216. {.type_id = context.insts().Get(rewrite_inst_id).type_id(),
  217. .inst_id = rewrite_inst_id});
  218. }
  219. return witness_inst_id;
  220. }
  221. auto RequireCompleteFacetTypeForImplDefinition(
  222. Context& context, SemIR::LocId loc_id, SemIR::TypeInstId facet_type_inst_id)
  223. -> bool {
  224. auto facet_type_id =
  225. context.types().GetTypeIdForTypeInstId(facet_type_inst_id);
  226. return RequireCompleteType(context, facet_type_id,
  227. context.insts().GetLocId(facet_type_inst_id), [&] {
  228. return IncompleteFacetTypeDiagnosticBuilder(
  229. context, loc_id, facet_type_inst_id,
  230. /*is_definition=*/true);
  231. });
  232. }
  233. auto AllocateFacetTypeImplWitness(Context& context,
  234. SemIR::InterfaceId interface_id,
  235. SemIR::InstBlockId witness_id) -> void {
  236. const auto& interface = context.interfaces().Get(interface_id);
  237. CARBON_CHECK(interface.is_complete());
  238. auto assoc_entities =
  239. context.inst_blocks().Get(interface.associated_entities_id);
  240. for (auto decl_id : assoc_entities) {
  241. LoadImportRef(context, decl_id);
  242. }
  243. llvm::SmallVector<SemIR::InstId> empty_table(
  244. assoc_entities.size(), SemIR::ImplWitnessTablePlaceholder::TypeInstId);
  245. context.inst_blocks().ReplacePlaceholder(witness_id, empty_table);
  246. }
  247. } // namespace Carbon::Check