facet_type.cpp 12 KB

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