handle_interface.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/context.h"
  5. #include "toolchain/check/interface.h"
  6. #include "toolchain/check/modifiers.h"
  7. #include "toolchain/sem_ir/typed_insts.h"
  8. namespace Carbon::Check {
  9. auto HandleInterfaceIntroducer(Context& context,
  10. Parse::InterfaceIntroducerId parse_node)
  11. -> bool {
  12. // Create an instruction block to hold the instructions created as part of the
  13. // interface signature, such as generic parameters.
  14. context.inst_block_stack().Push();
  15. // Push the bracketing node.
  16. context.node_stack().Push(parse_node);
  17. // Optional modifiers and the name follow.
  18. context.decl_state_stack().Push(DeclState::Interface);
  19. context.decl_name_stack().PushScopeAndStartName();
  20. return true;
  21. }
  22. static auto BuildInterfaceDecl(Context& context,
  23. Parse::AnyInterfaceDeclId parse_node)
  24. -> std::tuple<SemIR::InterfaceId, SemIR::InstId> {
  25. if (context.node_stack().PopIf<Parse::NodeKind::TuplePattern>()) {
  26. context.TODO(parse_node, "generic interface");
  27. }
  28. if (context.node_stack().PopIf<Parse::NodeKind::ImplicitParamList>()) {
  29. context.TODO(parse_node, "generic interface");
  30. }
  31. auto name_context = context.decl_name_stack().FinishName();
  32. context.node_stack()
  33. .PopAndDiscardSoloParseNode<Parse::NodeKind::InterfaceIntroducer>();
  34. // Process modifiers.
  35. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Interface,
  36. name_context.target_scope_id);
  37. LimitModifiersOnDecl(context, KeywordModifierSet::Access,
  38. Lex::TokenKind::Interface);
  39. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  40. if (!!(modifiers & KeywordModifierSet::Access)) {
  41. context.TODO(context.decl_state_stack().innermost().modifier_node_id(
  42. ModifierOrder::Access),
  43. "access modifier");
  44. }
  45. context.decl_state_stack().Pop(DeclState::Interface);
  46. auto decl_block_id = context.inst_block_stack().Pop();
  47. // Add the interface declaration.
  48. auto interface_decl = SemIR::InterfaceDecl{
  49. SemIR::TypeId::TypeType, SemIR::InterfaceId::Invalid, decl_block_id};
  50. auto interface_decl_id =
  51. context.AddPlaceholderInst({parse_node, interface_decl});
  52. // Check whether this is a redeclaration.
  53. auto existing_id = context.decl_name_stack().LookupOrAddName(
  54. name_context, interface_decl_id);
  55. if (existing_id.is_valid()) {
  56. if (auto existing_interface_decl =
  57. context.insts().Get(existing_id).TryAs<SemIR::InterfaceDecl>()) {
  58. // This is a redeclaration of an existing interface.
  59. interface_decl.interface_id = existing_interface_decl->interface_id;
  60. // TODO: Check that the generic parameter list agrees with the prior
  61. // declaration.
  62. } else {
  63. // This is a redeclaration of something other than a interface.
  64. context.DiagnoseDuplicateName(interface_decl_id, existing_id);
  65. }
  66. }
  67. // Create a new interface if this isn't a valid redeclaration.
  68. if (!interface_decl.interface_id.is_valid()) {
  69. // TODO: If this is an invalid redeclaration of a non-interface entity or
  70. // there was an error in the qualifier, we will have lost track of the
  71. // interface name here. We should keep track of it even if the name is
  72. // invalid.
  73. // TODO: should have a `Self` type id member
  74. interface_decl.interface_id = context.interfaces().Add(
  75. {.name_id = name_context.name_id_for_new_inst(),
  76. .enclosing_scope_id = name_context.enclosing_scope_id_for_new_inst(),
  77. .decl_id = interface_decl_id});
  78. }
  79. // Write the interface ID into the InterfaceDecl.
  80. context.ReplaceInstBeforeConstantUse(interface_decl_id,
  81. {parse_node, interface_decl});
  82. return {interface_decl.interface_id, interface_decl_id};
  83. }
  84. auto HandleInterfaceDecl(Context& context, Parse::InterfaceDeclId parse_node)
  85. -> bool {
  86. BuildInterfaceDecl(context, parse_node);
  87. context.decl_name_stack().PopScope();
  88. return true;
  89. }
  90. auto HandleInterfaceDefinitionStart(
  91. Context& context, Parse::InterfaceDefinitionStartId parse_node) -> bool {
  92. auto [interface_id, interface_decl_id] =
  93. BuildInterfaceDecl(context, parse_node);
  94. auto& interface_info = context.interfaces().Get(interface_id);
  95. // Track that this declaration is the definition.
  96. if (interface_info.is_defined()) {
  97. CARBON_DIAGNOSTIC(InterfaceRedefinition, Error,
  98. "Redefinition of interface {0}.", SemIR::NameId);
  99. CARBON_DIAGNOSTIC(InterfacePreviousDefinition, Note,
  100. "Previous definition was here.");
  101. context.emitter()
  102. .Build(parse_node, InterfaceRedefinition, interface_info.name_id)
  103. .Note(interface_info.definition_id, InterfacePreviousDefinition)
  104. .Emit();
  105. } else {
  106. interface_info.definition_id = interface_decl_id;
  107. interface_info.scope_id =
  108. context.name_scopes().Add(interface_decl_id, SemIR::NameId::Invalid,
  109. interface_info.enclosing_scope_id);
  110. }
  111. // Enter the interface scope.
  112. context.scope_stack().Push(interface_decl_id, interface_info.scope_id);
  113. context.inst_block_stack().Push();
  114. context.node_stack().Push(parse_node, interface_id);
  115. // We use the arg stack to build the witness table type.
  116. context.args_type_info_stack().Push();
  117. // Declare and introduce `Self`.
  118. if (!interface_info.is_defined()) {
  119. // TODO: Once we support parameterized interfaces, this won't be the right
  120. // type. For `interface X(T:! type)`, the type of `Self` is `X(T)`, whereas
  121. // this will be simply `X`.
  122. auto self_type_id = context.GetTypeIdForTypeInst(interface_decl_id);
  123. // We model `Self` as a symbolic binding whose type is the interface.
  124. // Because there is no equivalent non-symbolic value, we use `Invalid` as
  125. // the `value_id` on the `BindSymbolicName`.
  126. auto bind_name_id = context.bind_names().Add(
  127. {.name_id = SemIR::NameId::SelfType,
  128. .enclosing_scope_id = interface_info.scope_id});
  129. interface_info.self_param_id =
  130. context.AddInst({Parse::NodeId::Invalid,
  131. SemIR::BindSymbolicName{self_type_id, bind_name_id,
  132. SemIR::InstId::Invalid}});
  133. context.name_scopes()
  134. .Get(interface_info.scope_id)
  135. .names.insert({SemIR::NameId::SelfType, interface_info.self_param_id});
  136. }
  137. // TODO: Handle the case where there's control flow in the interface body. For
  138. // example:
  139. //
  140. // interface C {
  141. // let v: if true then i32 else f64;
  142. // }
  143. //
  144. // We may need to track a list of instruction blocks here, as we do for a
  145. // function.
  146. interface_info.body_block_id = context.inst_block_stack().PeekOrAdd();
  147. return true;
  148. }
  149. auto HandleInterfaceDefinition(Context& context,
  150. Parse::InterfaceDefinitionId /*parse_node*/)
  151. -> bool {
  152. auto interface_id =
  153. context.node_stack().Pop<Parse::NodeKind::InterfaceDefinitionStart>();
  154. context.inst_block_stack().Pop();
  155. context.scope_stack().Pop();
  156. context.decl_name_stack().PopScope();
  157. auto associated_entities_id = context.args_type_info_stack().Pop();
  158. // The interface type is now fully defined.
  159. auto& interface_info = context.interfaces().Get(interface_id);
  160. if (!interface_info.associated_entities_id.is_valid()) {
  161. interface_info.associated_entities_id = associated_entities_id;
  162. }
  163. return true;
  164. }
  165. } // namespace Carbon::Check