interface.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/interface.h"
  5. #include <algorithm>
  6. #include <cstddef>
  7. #include "common/concepts.h"
  8. #include "toolchain/check/context.h"
  9. #include "toolchain/check/eval.h"
  10. #include "toolchain/check/generic.h"
  11. #include "toolchain/check/inst.h"
  12. #include "toolchain/check/merge.h"
  13. #include "toolchain/check/name_lookup.h"
  14. #include "toolchain/check/type.h"
  15. #include "toolchain/sem_ir/entity_with_params_base.h"
  16. #include "toolchain/sem_ir/ids.h"
  17. #include "toolchain/sem_ir/inst.h"
  18. #include "toolchain/sem_ir/typed_insts.h"
  19. namespace Carbon::Check {
  20. auto BuildAssociatedEntity(Context& context, SemIR::InterfaceId interface_id,
  21. SemIR::InstId decl_id) -> SemIR::InstId {
  22. auto& interface_info = context.interfaces().Get(interface_id);
  23. if (!interface_info.is_being_defined()) {
  24. // This should only happen if the interface is erroneously defined more than
  25. // once.
  26. // TODO: Find a way to CHECK this.
  27. return SemIR::ErrorInst::InstId;
  28. }
  29. // This associated entity is being declared as a member of the self specific
  30. // of the interface.
  31. auto interface_specific_id =
  32. context.generics().GetSelfSpecific(interface_info.generic_id);
  33. // Register this declaration as declaring an associated entity.
  34. auto index = SemIR::ElementIndex(
  35. context.args_type_info_stack().PeekCurrentBlockContents().size());
  36. context.args_type_info_stack().AddInstId(decl_id);
  37. // Name lookup for the declaration's name should name the associated entity,
  38. // not the declaration itself.
  39. auto type_id =
  40. GetAssociatedEntityType(context, interface_id, interface_specific_id);
  41. return AddInst<SemIR::AssociatedEntity>(
  42. context, SemIR::LocId(decl_id),
  43. {.type_id = type_id, .index = index, .decl_id = decl_id});
  44. }
  45. // Returns the `Self` binding for an interface, given a specific for the
  46. // interface and a generic for an associated entity within it.
  47. static auto GetSelfBinding(Context& context,
  48. SemIR::SpecificId interface_specific_id,
  49. SemIR::GenericId assoc_entity_generic_id)
  50. -> SemIR::InstId {
  51. const auto& generic = context.generics().Get(assoc_entity_generic_id);
  52. auto bindings = context.inst_blocks().Get(generic.bindings_id);
  53. auto interface_args_id =
  54. context.specifics().GetArgsOrEmpty(interface_specific_id);
  55. auto interface_args = context.inst_blocks().Get(interface_args_id);
  56. // The `Self` binding is the first binding after the interface's arguments.
  57. auto self_binding_id = bindings[interface_args.size()];
  58. // Check that we found the self binding. The binding might be a
  59. // `SymbolicBinding` or an `ImportRef` naming one.
  60. auto self_binding_const_inst_id =
  61. context.constant_values().GetConstantInstId(self_binding_id);
  62. auto bind_name_inst =
  63. context.insts().GetAs<SemIR::SymbolicBinding>(self_binding_const_inst_id);
  64. CARBON_CHECK(
  65. context.entity_names().Get(bind_name_inst.entity_name_id).name_id ==
  66. SemIR::NameId::SelfType,
  67. "Expected a Self binding, found {0}", bind_name_inst);
  68. return self_binding_id;
  69. }
  70. // Given a `Self` type and a witness that it implements an interface, along with
  71. // that interface's `Self` binding, forms and returns a facet that can be used
  72. // as the argument for that `Self` binding.
  73. static auto GetSelfFacet(Context& context,
  74. SemIR::SpecificId interface_specific_id,
  75. SemIR::GenericId generic_id,
  76. SemIR::TypeId self_type_id,
  77. SemIR::InstId self_witness_id) -> SemIR::InstId {
  78. auto self_binding_id =
  79. GetSelfBinding(context, interface_specific_id, generic_id);
  80. auto self_facet_type_id = SemIR::GetTypeOfInstInSpecific(
  81. context.sem_ir(), interface_specific_id, self_binding_id);
  82. // Create a facet value to be the value of `Self` in the interface.
  83. // TODO: Pass this in instead of creating it here. The caller sometimes
  84. // already has a facet value.
  85. auto type_inst_id = context.types().GetInstId(self_type_id);
  86. auto witnesses_block_id =
  87. context.inst_blocks().AddCanonical({self_witness_id});
  88. auto self_value_const_id = TryEvalInst(
  89. context, SemIR::FacetValue{.type_id = self_facet_type_id,
  90. .type_inst_id = type_inst_id,
  91. .witnesses_block_id = witnesses_block_id});
  92. return context.constant_values().GetInstId(self_value_const_id);
  93. }
  94. // Builds and returns the argument list from `interface_specific_id` with a
  95. // value for the `Self` parameter of `generic_id` appended.
  96. static auto GetGenericArgsWithSelfType(Context& context,
  97. SemIR::SpecificId interface_specific_id,
  98. SemIR::GenericId generic_id,
  99. SemIR::TypeId self_type_id,
  100. SemIR::InstId witness_inst_id,
  101. std::size_t reserve_args_size = 0)
  102. -> llvm::SmallVector<SemIR::InstId> {
  103. auto interface_args_id =
  104. context.specifics().GetArgsOrEmpty(interface_specific_id);
  105. auto interface_args = context.inst_blocks().Get(interface_args_id);
  106. llvm::SmallVector<SemIR::InstId> arg_ids;
  107. arg_ids.reserve(std::max(reserve_args_size, interface_args.size() + 1));
  108. // Start with the enclosing arguments from the interface.
  109. arg_ids.assign(interface_args.begin(), interface_args.end());
  110. // Add the `Self` argument.
  111. arg_ids.push_back(GetSelfFacet(context, interface_specific_id, generic_id,
  112. self_type_id, witness_inst_id));
  113. return arg_ids;
  114. }
  115. auto GetSelfSpecificForInterfaceMemberWithSelfType(
  116. Context& context, SemIR::LocId loc_id,
  117. SemIR::SpecificId interface_specific_id, SemIR::GenericId generic_id,
  118. SemIR::SpecificId enclosing_specific_id, SemIR::TypeId self_type_id,
  119. SemIR::InstId witness_inst_id) -> SemIR::SpecificId {
  120. const auto& generic = context.generics().Get(generic_id);
  121. auto self_specific_args = context.inst_blocks().Get(
  122. context.specifics().Get(generic.self_specific_id).args_id);
  123. auto arg_ids = GetGenericArgsWithSelfType(
  124. context, interface_specific_id, generic_id, self_type_id, witness_inst_id,
  125. self_specific_args.size());
  126. // Determine the number of specific arguments that enclose the point where
  127. // this self specific will be used from. In an impl, this will be the number
  128. // of parameters that the impl has.
  129. int num_enclosing_specific_args =
  130. context.inst_blocks()
  131. .Get(context.specifics().GetArgsOrEmpty(enclosing_specific_id))
  132. .size();
  133. // The index of each remaining generic parameter is adjusted to match the
  134. // numbering at the point where the self specific is used.
  135. int index_delta = num_enclosing_specific_args - arg_ids.size();
  136. // Take any trailing argument values from the self specific.
  137. // TODO: If these refer to outer arguments, for example in their types, we may
  138. // need to perform extra substitutions here.
  139. for (auto arg_id : self_specific_args.drop_front(arg_ids.size())) {
  140. auto new_arg_id = context.constant_values().GetConstantInstId(arg_id);
  141. if (index_delta) {
  142. // If this parameter would have a new index in the context described by
  143. // `enclosing_specific_id`, form a new binding with an adjusted index.
  144. auto bind_name = context.insts().GetAs<SemIR::SymbolicBinding>(
  145. context.constant_values().GetConstantInstId(arg_id));
  146. auto entity_name = context.entity_names().Get(bind_name.entity_name_id);
  147. entity_name.bind_index_value += index_delta;
  148. CARBON_CHECK(entity_name.bind_index_value >= 0);
  149. bind_name.entity_name_id =
  150. context.entity_names().AddCanonical(entity_name);
  151. new_arg_id =
  152. context.constant_values().GetInstId(TryEvalInst(context, bind_name));
  153. }
  154. arg_ids.push_back(new_arg_id);
  155. }
  156. return MakeSpecific(context, loc_id, generic_id, arg_ids);
  157. }
  158. auto GetTypeForSpecificAssociatedEntity(Context& context, SemIR::LocId loc_id,
  159. SemIR::SpecificId interface_specific_id,
  160. SemIR::InstId decl_id,
  161. SemIR::TypeId self_type_id,
  162. SemIR::InstId self_witness_id)
  163. -> SemIR::TypeId {
  164. auto decl_constant_inst_id =
  165. context.constant_values().GetConstantInstId(decl_id);
  166. if (decl_constant_inst_id == SemIR::ErrorInst::InstId) {
  167. return SemIR::ErrorInst::TypeId;
  168. }
  169. auto decl = context.insts().Get(decl_constant_inst_id);
  170. if (auto assoc_const = decl.TryAs<SemIR::AssociatedConstantDecl>()) {
  171. // Form a specific for the associated constant, and grab the type from
  172. // there.
  173. auto generic_id = context.associated_constants()
  174. .Get(assoc_const->assoc_const_id)
  175. .generic_id;
  176. auto arg_ids =
  177. GetGenericArgsWithSelfType(context, interface_specific_id, generic_id,
  178. self_type_id, self_witness_id);
  179. auto const_specific_id = MakeSpecific(context, loc_id, generic_id, arg_ids);
  180. return SemIR::GetTypeOfInstInSpecific(context.sem_ir(), const_specific_id,
  181. decl_id);
  182. }
  183. if (auto fn = context.types().TryGetAs<SemIR::FunctionType>(decl.type_id())) {
  184. // Form the type of the function within the interface, and attach the `Self`
  185. // type.
  186. auto interface_fn_type_id = SemIR::GetTypeOfInstInSpecific(
  187. context.sem_ir(), interface_specific_id, decl_id);
  188. auto self_facet_id =
  189. GetSelfFacet(context, interface_specific_id,
  190. context.functions().Get(fn->function_id).generic_id,
  191. self_type_id, self_witness_id);
  192. return GetFunctionTypeWithSelfType(
  193. context, context.types().GetInstId(interface_fn_type_id),
  194. self_facet_id);
  195. }
  196. CARBON_FATAL("Unexpected kind for associated constant {0}", decl);
  197. }
  198. auto AddSelfGenericParameter(Context& context, SemIR::LocId definition_loc_id,
  199. SemIR::TypeId type_id, SemIR::NameScopeId scope_id,
  200. bool is_template) -> SemIR::InstId {
  201. auto entity_name_id = context.entity_names().AddSymbolicBindingName(
  202. SemIR::NameId::SelfType, scope_id,
  203. context.scope_stack().AddCompileTimeBinding(), is_template);
  204. // Because there is no equivalent non-symbolic value, we use `None` as
  205. // the `value_id` on the `SymbolicBinding`.
  206. auto self_param_inst_id =
  207. AddInst<SemIR::SymbolicBinding>(context, definition_loc_id,
  208. {.type_id = type_id,
  209. .entity_name_id = entity_name_id,
  210. .value_id = SemIR::InstId::None});
  211. context.scope_stack().PushCompileTimeBinding(self_param_inst_id);
  212. context.name_scopes().AddRequiredName(scope_id, SemIR::NameId::SelfType,
  213. self_param_inst_id);
  214. return self_param_inst_id;
  215. }
  216. template <typename EntityT>
  217. requires std::same_as<EntityT, SemIR::Interface>
  218. static auto TryGetEntity(Context& context, SemIR::Inst inst)
  219. -> const SemIR::EntityWithParamsBase* {
  220. if (auto decl = inst.TryAs<SemIR::InterfaceDecl>()) {
  221. return &context.interfaces().Get(decl->interface_id);
  222. } else {
  223. return nullptr;
  224. }
  225. }
  226. template <typename EntityT>
  227. requires std::same_as<EntityT, SemIR::NamedConstraint>
  228. static auto TryGetEntity(Context& context, SemIR::Inst inst)
  229. -> const SemIR::EntityWithParamsBase* {
  230. if (auto decl = inst.TryAs<SemIR::NamedConstraintDecl>()) {
  231. return &context.named_constraints().Get(decl->named_constraint_id);
  232. } else {
  233. return nullptr;
  234. }
  235. }
  236. template <typename EntityT>
  237. requires std::same_as<EntityT, SemIR::Interface>
  238. static constexpr auto DeclTokenKind() -> Lex::TokenKind {
  239. return Lex::TokenKind::Interface;
  240. }
  241. template <typename EntityT>
  242. requires std::same_as<EntityT, SemIR::NamedConstraint>
  243. static constexpr auto DeclTokenKind() -> Lex::TokenKind {
  244. return Lex::TokenKind::Constraint;
  245. }
  246. template <typename EntityT>
  247. requires SameAsOneOf<EntityT, SemIR::Interface, SemIR::NamedConstraint>
  248. auto TryGetExistingDecl(Context& context, const NameComponent& name,
  249. SemIR::ScopeLookupResult lookup_result,
  250. const EntityT& entity, bool is_definition)
  251. -> std::optional<SemIR::Inst> {
  252. if (lookup_result.is_poisoned()) {
  253. // This is a declaration of a poisoned name.
  254. DiagnosePoisonedName(context, name.name_id,
  255. lookup_result.poisoning_loc_id(), name.name_loc_id);
  256. return std::nullopt;
  257. }
  258. if (!lookup_result.is_found()) {
  259. return std::nullopt;
  260. }
  261. SemIR::InstId existing_id = lookup_result.target_inst_id();
  262. SemIR::Inst existing_decl_inst = context.insts().Get(existing_id);
  263. const auto* existing_decl_entity =
  264. TryGetEntity<EntityT>(context, existing_decl_inst);
  265. if (!existing_decl_entity) {
  266. // This is a redeclaration with a different entity kind.
  267. DiagnoseDuplicateName(context, name.name_id, name.name_loc_id,
  268. SemIR::LocId(existing_id));
  269. return std::nullopt;
  270. }
  271. if (!CheckRedeclParamsMatch(
  272. context,
  273. DeclParams(SemIR::LocId(entity.latest_decl_id()),
  274. name.first_param_node_id, name.last_param_node_id,
  275. name.implicit_param_patterns_id, name.param_patterns_id),
  276. DeclParams(*existing_decl_entity))) {
  277. // Mismatch is diagnosed already if found.
  278. return std::nullopt;
  279. }
  280. // TODO: This should be refactored a little, particularly for
  281. // prev_import_ir_id. See similar logic for classes and functions, which
  282. // might also be refactored to merge.
  283. DiagnoseIfInvalidRedecl(
  284. context, DeclTokenKind<EntityT>(), existing_decl_entity->name_id,
  285. RedeclInfo(entity, SemIR::LocId(entity.latest_decl_id()), is_definition),
  286. RedeclInfo(*existing_decl_entity,
  287. SemIR::LocId(existing_decl_entity->latest_decl_id()),
  288. existing_decl_entity->has_definition_started()),
  289. /*prev_import_ir_id=*/SemIR::ImportIRId::None);
  290. if (is_definition && existing_decl_entity->has_definition_started()) {
  291. // DiagnoseIfInvalidRedecl would diagnose an error in this case, since we'd
  292. // have two definitions. Given the declaration parts of the definitions
  293. // match, we would be able to use the prior declaration for error recovery,
  294. // except that having two definitions causes larger problems for generics.
  295. // All interfaces (and named constraints) are generic with an implicit Self
  296. // compile time binding.
  297. return std::nullopt;
  298. }
  299. // This is a matching redeclaration of an existing entity of the same type.
  300. return existing_decl_inst;
  301. }
  302. template auto TryGetExistingDecl(Context& context, const NameComponent& name,
  303. SemIR::ScopeLookupResult lookup_result,
  304. const SemIR::Interface& entity,
  305. bool is_definition)
  306. -> std::optional<SemIR::Inst>;
  307. template auto TryGetExistingDecl(Context& context, const NameComponent& name,
  308. SemIR::ScopeLookupResult lookup_result,
  309. const SemIR::NamedConstraint& entity,
  310. bool is_definition)
  311. -> std::optional<SemIR::Inst>;
  312. } // namespace Carbon::Check