handle_interface.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 <tuple>
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/check/eval.h"
  7. #include "toolchain/check/facet_type.h"
  8. #include "toolchain/check/generic.h"
  9. #include "toolchain/check/handle.h"
  10. #include "toolchain/check/inst.h"
  11. #include "toolchain/check/interface.h"
  12. #include "toolchain/check/merge.h"
  13. #include "toolchain/check/modifiers.h"
  14. #include "toolchain/check/name_component.h"
  15. #include "toolchain/check/name_lookup.h"
  16. #include "toolchain/check/type.h"
  17. #include "toolchain/sem_ir/entity_with_params_base.h"
  18. #include "toolchain/sem_ir/ids.h"
  19. #include "toolchain/sem_ir/interface.h"
  20. #include "toolchain/sem_ir/typed_insts.h"
  21. namespace Carbon::Check {
  22. auto HandleParseNode(Context& context, Parse::InterfaceIntroducerId node_id)
  23. -> bool {
  24. // This interface is potentially generic.
  25. StartGenericDecl(context);
  26. // Create an instruction block to hold the instructions created as part of the
  27. // interface signature, such as generic parameters.
  28. context.inst_block_stack().Push();
  29. // Optional modifiers and the name follow.
  30. context.decl_introducer_state_stack().Push<Lex::TokenKind::Interface>();
  31. context.decl_name_stack().PushScopeAndStartName();
  32. // Push the bracketing node.
  33. context.node_stack().Push(node_id);
  34. return true;
  35. }
  36. static auto BuildInterfaceDecl(Context& context,
  37. Parse::AnyInterfaceDeclId node_id,
  38. bool is_definition)
  39. -> std::tuple<SemIR::InterfaceId, SemIR::InstId> {
  40. auto name = PopNameComponent(context);
  41. auto name_context = context.decl_name_stack().FinishName(name);
  42. context.node_stack()
  43. .PopAndDiscardSoloNodeId<Parse::NodeKind::InterfaceIntroducer>();
  44. // Process modifiers.
  45. auto [_, parent_scope_inst] =
  46. context.name_scopes().GetInstIfValid(name_context.parent_scope_id);
  47. auto introducer =
  48. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Interface>();
  49. CheckAccessModifiersOnDecl(context, introducer, parent_scope_inst);
  50. LimitModifiersOnDecl(context, introducer, KeywordModifierSet::Access);
  51. auto decl_block_id = context.inst_block_stack().Pop();
  52. // Add the interface declaration.
  53. auto interface_decl = SemIR::InterfaceDecl{
  54. SemIR::TypeType::TypeId, SemIR::InterfaceId::None, decl_block_id};
  55. auto decl_inst_id = AddPlaceholderInst(context, node_id, interface_decl);
  56. SemIR::Interface interface_info = {name_context.MakeEntityWithParamsBase(
  57. name, decl_inst_id, /*is_extern=*/false, SemIR::LibraryNameId::None)};
  58. DiagnoseIfGenericMissingExplicitParameters(context, interface_info);
  59. // Check whether this is a redeclaration.
  60. SemIR::ScopeLookupResult lookup_result =
  61. context.decl_name_stack().LookupOrAddName(
  62. name_context, decl_inst_id, introducer.modifier_set.GetAccessKind());
  63. if (auto existing_decl = TryGetExistingDecl(context, name, lookup_result,
  64. interface_info, is_definition)) {
  65. auto existing_interface_decl = existing_decl->As<SemIR::InterfaceDecl>();
  66. interface_decl.interface_id = existing_interface_decl.interface_id;
  67. interface_decl.type_id = existing_interface_decl.type_id;
  68. // TODO: If the new declaration is a definition, keep its parameter
  69. // and implicit parameter lists rather than the ones from the
  70. // previous declaration.
  71. auto prev_decl_generic_id =
  72. context.interfaces().Get(interface_decl.interface_id).generic_id;
  73. FinishGenericRedecl(context, prev_decl_generic_id);
  74. } else {
  75. // Create a new interface if this isn't a valid redeclaration.
  76. interface_info.generic_id = BuildGenericDecl(context, decl_inst_id);
  77. interface_decl.interface_id = context.interfaces().Add(interface_info);
  78. if (interface_info.has_parameters()) {
  79. interface_decl.type_id =
  80. GetGenericInterfaceType(context, interface_decl.interface_id,
  81. context.scope_stack().PeekSpecificId());
  82. }
  83. }
  84. // Write the interface ID into the InterfaceDecl.
  85. ReplaceInstBeforeConstantUse(context, decl_inst_id, interface_decl);
  86. return {interface_decl.interface_id, decl_inst_id};
  87. }
  88. auto HandleParseNode(Context& context, Parse::InterfaceDeclId node_id) -> bool {
  89. BuildInterfaceDecl(context, node_id, /*is_definition=*/false);
  90. context.decl_name_stack().PopScope();
  91. return true;
  92. }
  93. auto HandleParseNode(Context& context,
  94. Parse::InterfaceDefinitionStartId node_id) -> bool {
  95. auto [interface_id, decl_inst_id] =
  96. BuildInterfaceDecl(context, node_id, /*is_definition=*/true);
  97. auto& interface_info = context.interfaces().Get(interface_id);
  98. // Track that this declaration is the definition.
  99. CARBON_CHECK(!interface_info.has_definition_started(),
  100. "Can't merge with defined interfaces.");
  101. interface_info.definition_id = decl_inst_id;
  102. interface_info.scope_without_self_id = context.name_scopes().Add(
  103. decl_inst_id, SemIR::NameId::None, interface_info.parent_scope_id);
  104. // Start the definition of interface-without-self.
  105. StartGenericDefinition(context, interface_info.generic_id);
  106. context.inst_block_stack().Push();
  107. // Enter the interface-without-self scope, which is used for the Self
  108. // instruction, since it needs to reference the interface (without-self)
  109. // generic. Self can't reference the interface-with-self generic since it's a
  110. // parameter to the generic.
  111. context.scope_stack().PushForEntity(
  112. decl_inst_id, interface_info.scope_without_self_id,
  113. context.generics().GetSelfSpecific(interface_info.generic_id));
  114. // Declare and introduce `Self`. We model `Self` as a symbolic binding whose
  115. // type is the interface, excluding any other interfaces mentioned by
  116. // `require` declarations.
  117. //
  118. // This is an instruction in the interface-without-self so that we can apply a
  119. // SpecificInterface to it, and get the inner `Self` as modified by any
  120. // enclosing specific.
  121. SemIR::TypeId self_type_id = GetInterfaceType(
  122. context, interface_id,
  123. context.generics().GetSelfSpecific(interface_info.generic_id));
  124. interface_info.self_param_id = AddSelfSymbolicBindingToScope(
  125. context, node_id, self_type_id, interface_info.scope_without_self_id,
  126. /*is_template=*/false);
  127. // Start the declaration of interface-with-self.
  128. StartGenericDecl(context);
  129. // Push `Self` as a parameter of the interface-with-self.
  130. context.scope_stack().PushCompileTimeBinding(interface_info.self_param_id);
  131. // Add the interface-with-self declaration and build the generic for it. This
  132. // captures the `interface_info.self_param_id` as a parameter of the generic.
  133. auto interface_with_self_decl =
  134. SemIR::InterfaceWithSelfDecl{.interface_id = interface_id};
  135. auto decl_with_self_inst_id =
  136. AddPlaceholderInst(context, node_id, interface_with_self_decl);
  137. auto generic_with_self_id = BuildGenericDecl(context, decl_with_self_inst_id);
  138. interface_info.generic_with_self_id = generic_with_self_id;
  139. ReplaceInstBeforeConstantUse(context, decl_with_self_inst_id,
  140. interface_with_self_decl);
  141. interface_info.scope_with_self_id =
  142. context.name_scopes().Add(decl_with_self_inst_id, SemIR::NameId::None,
  143. interface_info.scope_without_self_id);
  144. // Set on the name scope that `M` is replaced by `Self.M`.
  145. context.name_scopes()
  146. .Get(interface_info.scope_with_self_id)
  147. .set_is_interface_definition();
  148. // Start the definition of interface-with-self.
  149. StartGenericDefinition(context, interface_info.generic_with_self_id);
  150. // Enter a scope for the interace-with-self.
  151. context.scope_stack().PushForEntity(
  152. decl_with_self_inst_id, interface_info.scope_with_self_id,
  153. context.generics().GetSelfSpecific(interface_info.generic_with_self_id));
  154. interface_info.body_block_without_self_id =
  155. context.inst_block_stack().PeekOrAdd();
  156. context.inst_block_stack().Push();
  157. context.require_impls_stack().Push(interface_id);
  158. // We use the arg stack to build the witness table type.
  159. context.args_type_info_stack().Push();
  160. // TODO: Handle the case where there's control flow in the interface body. For
  161. // example:
  162. //
  163. // interface C {
  164. // let v: if true then i32 else f64;
  165. // }
  166. //
  167. // We may need to track a list of instruction blocks here, as we do for a
  168. // function.
  169. interface_info.body_block_with_self_id =
  170. context.inst_block_stack().PeekOrAdd();
  171. context.node_stack().Push(node_id, interface_id);
  172. return true;
  173. }
  174. auto HandleParseNode(Context& context, Parse::InterfaceDefinitionId /*node_id*/)
  175. -> bool {
  176. auto interface_id =
  177. context.node_stack().Pop<Parse::NodeKind::InterfaceDefinitionStart>();
  178. // Pop the body_block_with_self.
  179. context.inst_block_stack().Pop();
  180. auto associated_entities_id = context.args_type_info_stack().Pop();
  181. auto require_impls_block_id = context.require_impls_blocks().Add(
  182. context.require_impls_stack().PeekTop());
  183. context.require_impls_stack().Pop();
  184. auto& interface_info = context.interfaces().Get(interface_id);
  185. if (!interface_info.associated_entities_id.has_value()) {
  186. interface_info.require_impls_block_id = require_impls_block_id;
  187. // This marks the interface type as fully defined.
  188. interface_info.associated_entities_id = associated_entities_id;
  189. }
  190. // Finish the definition of interface-with-self.
  191. FinishGenericDefinition(context, interface_info.generic_with_self_id);
  192. // Pop the body_block_without_self.
  193. context.inst_block_stack().Pop();
  194. // Finish the definition of interface-without-self.
  195. FinishGenericDefinition(context, interface_info.generic_id);
  196. // The decl_name_stack and scopes are popped by `ProcessNodeIds`.
  197. return true;
  198. }
  199. } // namespace Carbon::Check