handle_interface.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 node_id) -> bool {
  11. // Create an instruction block to hold the instructions created as part of the
  12. // interface signature, such as generic parameters.
  13. context.inst_block_stack().Push();
  14. // Push the bracketing node.
  15. context.node_stack().Push(node_id);
  16. // Optional modifiers and the name follow.
  17. context.decl_state_stack().Push(DeclState::Interface);
  18. context.decl_name_stack().PushScopeAndStartName();
  19. return true;
  20. }
  21. static auto BuildInterfaceDecl(Context& context,
  22. Parse::AnyInterfaceDeclId node_id)
  23. -> std::tuple<SemIR::InterfaceId, SemIR::InstId> {
  24. if (context.node_stack().PopIf<Parse::NodeKind::TuplePattern>()) {
  25. context.TODO(node_id, "generic interface");
  26. }
  27. if (context.node_stack().PopIf<Parse::NodeKind::ImplicitParamList>()) {
  28. context.TODO(node_id, "generic interface");
  29. }
  30. auto name_context = context.decl_name_stack().FinishName();
  31. context.node_stack()
  32. .PopAndDiscardSoloNodeId<Parse::NodeKind::InterfaceIntroducer>();
  33. // Process modifiers.
  34. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Interface,
  35. name_context.target_scope_id);
  36. LimitModifiersOnDecl(context, KeywordModifierSet::Access,
  37. Lex::TokenKind::Interface);
  38. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  39. if (!!(modifiers & KeywordModifierSet::Access)) {
  40. context.TODO(context.decl_state_stack().innermost().modifier_node_id(
  41. ModifierOrder::Access),
  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({node_id, 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. interface_decl.interface_id = context.interfaces().Add(
  73. {.name_id = name_context.name_id_for_new_inst(),
  74. .enclosing_scope_id = name_context.enclosing_scope_id_for_new_inst(),
  75. .decl_id = interface_decl_id});
  76. }
  77. // Write the interface ID into the InterfaceDecl.
  78. context.ReplaceInstBeforeConstantUse(interface_decl_id, interface_decl);
  79. return {interface_decl.interface_id, interface_decl_id};
  80. }
  81. auto HandleInterfaceDecl(Context& context, Parse::InterfaceDeclId node_id)
  82. -> bool {
  83. BuildInterfaceDecl(context, node_id);
  84. context.decl_name_stack().PopScope();
  85. return true;
  86. }
  87. auto HandleInterfaceDefinitionStart(Context& context,
  88. Parse::InterfaceDefinitionStartId node_id)
  89. -> bool {
  90. auto [interface_id, interface_decl_id] = BuildInterfaceDecl(context, node_id);
  91. auto& interface_info = context.interfaces().Get(interface_id);
  92. // Track that this declaration is the definition.
  93. if (interface_info.is_defined()) {
  94. CARBON_DIAGNOSTIC(InterfaceRedefinition, Error,
  95. "Redefinition of interface {0}.", SemIR::NameId);
  96. CARBON_DIAGNOSTIC(InterfacePreviousDefinition, Note,
  97. "Previous definition was here.");
  98. context.emitter()
  99. .Build(node_id, InterfaceRedefinition, interface_info.name_id)
  100. .Note(interface_info.definition_id, InterfacePreviousDefinition)
  101. .Emit();
  102. } else {
  103. interface_info.definition_id = interface_decl_id;
  104. interface_info.scope_id =
  105. context.name_scopes().Add(interface_decl_id, SemIR::NameId::Invalid,
  106. interface_info.enclosing_scope_id);
  107. }
  108. // Enter the interface scope.
  109. context.scope_stack().Push(interface_decl_id, interface_info.scope_id);
  110. context.inst_block_stack().Push();
  111. context.node_stack().Push(node_id, interface_id);
  112. // We use the arg stack to build the witness table type.
  113. context.args_type_info_stack().Push();
  114. // Declare and introduce `Self`.
  115. if (!interface_info.is_defined()) {
  116. // TODO: Once we support parameterized interfaces, this won't be the right
  117. // type. For `interface X(T:! type)`, the type of `Self` is `X(T)`, whereas
  118. // this will be simply `X`.
  119. auto self_type_id = context.GetTypeIdForTypeInst(interface_decl_id);
  120. // We model `Self` as a symbolic binding whose type is the interface.
  121. // Because there is no equivalent non-symbolic value, we use `Invalid` as
  122. // the `value_id` on the `BindSymbolicName`.
  123. auto bind_name_id = context.bind_names().Add(
  124. {.name_id = SemIR::NameId::SelfType,
  125. .enclosing_scope_id = interface_info.scope_id});
  126. interface_info.self_param_id =
  127. context.AddInst({Parse::NodeId::Invalid,
  128. SemIR::BindSymbolicName{self_type_id, bind_name_id,
  129. SemIR::InstId::Invalid}});
  130. context.name_scopes()
  131. .Get(interface_info.scope_id)
  132. .names.insert({SemIR::NameId::SelfType, interface_info.self_param_id});
  133. }
  134. // TODO: Handle the case where there's control flow in the interface body. For
  135. // example:
  136. //
  137. // interface C {
  138. // let v: if true then i32 else f64;
  139. // }
  140. //
  141. // We may need to track a list of instruction blocks here, as we do for a
  142. // function.
  143. interface_info.body_block_id = context.inst_block_stack().PeekOrAdd();
  144. return true;
  145. }
  146. auto HandleInterfaceDefinition(Context& context,
  147. Parse::InterfaceDefinitionId /*node_id*/)
  148. -> bool {
  149. auto interface_id =
  150. context.node_stack().Pop<Parse::NodeKind::InterfaceDefinitionStart>();
  151. context.inst_block_stack().Pop();
  152. auto associated_entities_id = context.args_type_info_stack().Pop();
  153. // The interface type is now fully defined.
  154. auto& interface_info = context.interfaces().Get(interface_id);
  155. if (!interface_info.associated_entities_id.is_valid()) {
  156. interface_info.associated_entities_id = associated_entities_id;
  157. }
  158. // The decl_name_stack and scopes are popped by `ProcessNodeIds`.
  159. return true;
  160. }
  161. } // namespace Carbon::Check