facet_type.cpp 12 KB

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