handle_interface.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_id = context.name_scopes().Add(
  103. decl_inst_id, SemIR::NameId::None, interface_info.parent_scope_id);
  104. context.name_scopes()
  105. .Get(interface_info.scope_id)
  106. .set_is_interface_definition();
  107. auto self_specific_id =
  108. context.generics().GetSelfSpecific(interface_info.generic_id);
  109. StartGenericDefinition(context, interface_info.generic_id);
  110. context.inst_block_stack().Push();
  111. context.require_impls_stack().PushArray();
  112. // We use the arg stack to build the witness table type.
  113. context.args_type_info_stack().Push();
  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. SemIR::TypeId self_type_id =
  118. GetInterfaceType(context, interface_id, self_specific_id);
  119. interface_info.self_param_id =
  120. AddSelfGenericParameter(context, node_id, self_type_id,
  121. interface_info.scope_id, /*is_template=*/false);
  122. // Enter the interface scope.
  123. context.scope_stack().PushForEntity(decl_inst_id, interface_info.scope_id,
  124. self_specific_id);
  125. // TODO: Handle the case where there's control flow in the interface body. For
  126. // example:
  127. //
  128. // interface C {
  129. // let v: if true then i32 else f64;
  130. // }
  131. //
  132. // We may need to track a list of instruction blocks here, as we do for a
  133. // function.
  134. interface_info.body_block_id = context.inst_block_stack().PeekOrAdd();
  135. context.node_stack().Push(node_id, interface_id);
  136. return true;
  137. }
  138. auto HandleParseNode(Context& context, Parse::InterfaceDefinitionId /*node_id*/)
  139. -> bool {
  140. auto interface_id =
  141. context.node_stack().Pop<Parse::NodeKind::InterfaceDefinitionStart>();
  142. context.inst_block_stack().Pop();
  143. auto associated_entities_id = context.args_type_info_stack().Pop();
  144. auto require_impls_block_id = context.require_impls_blocks().Add(
  145. context.require_impls_stack().PeekArray());
  146. context.require_impls_stack().PopArray();
  147. auto& interface_info = context.interfaces().Get(interface_id);
  148. if (!interface_info.associated_entities_id.has_value()) {
  149. interface_info.require_impls_block_id = require_impls_block_id;
  150. // This marks the interface type as fully defined.
  151. interface_info.associated_entities_id = associated_entities_id;
  152. }
  153. FinishGenericDefinition(context, interface_info.generic_id);
  154. // The decl_name_stack and scopes are popped by `ProcessNodeIds`.
  155. return true;
  156. }
  157. } // namespace Carbon::Check