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().saw_access_modifier,
  42. "access modifier");
  43. }
  44. context.decl_state_stack().Pop(DeclState::Interface);
  45. auto decl_block_id = context.inst_block_stack().Pop();
  46. // Add the interface declaration.
  47. auto interface_decl = SemIR::InterfaceDecl{
  48. SemIR::TypeId::TypeType, SemIR::InterfaceId::Invalid, decl_block_id};
  49. auto interface_decl_id =
  50. context.AddPlaceholderInst({parse_node, interface_decl});
  51. // Check whether this is a redeclaration.
  52. auto existing_id = context.decl_name_stack().LookupOrAddName(
  53. name_context, interface_decl_id);
  54. if (existing_id.is_valid()) {
  55. if (auto existing_interface_decl =
  56. context.insts().Get(existing_id).TryAs<SemIR::InterfaceDecl>()) {
  57. // This is a redeclaration of an existing interface.
  58. interface_decl.interface_id = existing_interface_decl->interface_id;
  59. // TODO: Check that the generic parameter list agrees with the prior
  60. // declaration.
  61. } else {
  62. // This is a redeclaration of something other than a interface.
  63. context.DiagnoseDuplicateName(interface_decl_id, existing_id);
  64. }
  65. }
  66. // Create a new interface if this isn't a valid redeclaration.
  67. if (!interface_decl.interface_id.is_valid()) {
  68. // TODO: If this is an invalid redeclaration of a non-interface entity or
  69. // there was an error in the qualifier, we will have lost track of the
  70. // interface name here. We should keep track of it even if the name is
  71. // invalid.
  72. // TODO: should have a `Self` type id member
  73. interface_decl.interface_id = context.interfaces().Add(
  74. {.name_id = name_context.name_id_for_new_inst(),
  75. .enclosing_scope_id = name_context.enclosing_scope_id_for_new_inst(),
  76. .decl_id = interface_decl_id});
  77. }
  78. // Write the interface ID into the InterfaceDecl.
  79. context.ReplaceInstBeforeConstantUse(interface_decl_id,
  80. {parse_node, interface_decl});
  81. return {interface_decl.interface_id, interface_decl_id};
  82. }
  83. auto HandleInterfaceDecl(Context& context, Parse::InterfaceDeclId parse_node)
  84. -> bool {
  85. BuildInterfaceDecl(context, parse_node);
  86. context.decl_name_stack().PopScope();
  87. return true;
  88. }
  89. auto HandleInterfaceDefinitionStart(
  90. Context& context, Parse::InterfaceDefinitionStartId parse_node) -> bool {
  91. auto [interface_id, interface_decl_id] =
  92. BuildInterfaceDecl(context, parse_node);
  93. auto& interface_info = context.interfaces().Get(interface_id);
  94. // Track that this declaration is the definition.
  95. if (interface_info.is_defined()) {
  96. CARBON_DIAGNOSTIC(InterfaceRedefinition, Error,
  97. "Redefinition of interface {0}.", SemIR::NameId);
  98. CARBON_DIAGNOSTIC(InterfacePreviousDefinition, Note,
  99. "Previous definition was here.");
  100. context.emitter()
  101. .Build(parse_node, InterfaceRedefinition, interface_info.name_id)
  102. .Note(interface_info.definition_id, InterfacePreviousDefinition)
  103. .Emit();
  104. } else {
  105. interface_info.definition_id = interface_decl_id;
  106. interface_info.scope_id =
  107. context.name_scopes().Add(interface_decl_id, SemIR::NameId::Invalid,
  108. interface_info.enclosing_scope_id);
  109. }
  110. // Enter the interface scope.
  111. context.scope_stack().Push(interface_decl_id, interface_info.scope_id);
  112. context.inst_block_stack().Push();
  113. context.node_stack().Push(parse_node, interface_id);
  114. // We use the arg stack to build the witness table type.
  115. context.args_type_info_stack().Push();
  116. // Declare and introduce `Self`.
  117. if (!interface_info.is_defined()) {
  118. // TODO: Once we support parameterized interfaces, this won't be the right
  119. // type. For `interface X(T:! type)`, the type of `Self` is `X(T)`, whereas
  120. // this will be simply `X`.
  121. auto self_type_id = context.GetTypeIdForTypeConstant(
  122. context.constant_values().Get(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