handle_interface.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/check/name_component.h"
  8. #include "toolchain/sem_ir/typed_insts.h"
  9. namespace Carbon::Check {
  10. auto HandleInterfaceIntroducer(Context& context,
  11. Parse::InterfaceIntroducerId node_id) -> 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(node_id);
  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 node_id)
  24. -> std::tuple<SemIR::InterfaceId, SemIR::InstId> {
  25. auto name = PopNameComponent(context);
  26. if (name.params_id.is_valid() || name.implicit_params_id.is_valid()) {
  27. context.TODO(node_id, "generic interface");
  28. }
  29. auto name_context = context.decl_name_stack().FinishName(name);
  30. context.node_stack()
  31. .PopAndDiscardSoloNodeId<Parse::NodeKind::InterfaceIntroducer>();
  32. // Process modifiers.
  33. auto [_, enclosing_scope_inst] =
  34. context.name_scopes().GetInstIfValid(name_context.enclosing_scope_id);
  35. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Interface,
  36. enclosing_scope_inst);
  37. LimitModifiersOnDecl(context, KeywordModifierSet::Access,
  38. Lex::TokenKind::Interface);
  39. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  40. if (modifiers.HasAnyOf(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({node_id, 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. 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, interface_decl);
  80. return {interface_decl.interface_id, interface_decl_id};
  81. }
  82. auto HandleInterfaceDecl(Context& context, Parse::InterfaceDeclId node_id)
  83. -> bool {
  84. BuildInterfaceDecl(context, node_id);
  85. context.decl_name_stack().PopScope();
  86. return true;
  87. }
  88. auto HandleInterfaceDefinitionStart(Context& context,
  89. Parse::InterfaceDefinitionStartId node_id)
  90. -> bool {
  91. auto [interface_id, interface_decl_id] = BuildInterfaceDecl(context, node_id);
  92. auto& interface_info = context.interfaces().Get(interface_id);
  93. // Track that this declaration is the definition.
  94. if (interface_info.is_defined()) {
  95. CARBON_DIAGNOSTIC(InterfaceRedefinition, Error,
  96. "Redefinition of interface {0}.", SemIR::NameId);
  97. CARBON_DIAGNOSTIC(InterfacePreviousDefinition, Note,
  98. "Previous definition was here.");
  99. context.emitter()
  100. .Build(node_id, InterfaceRedefinition, interface_info.name_id)
  101. .Note(interface_info.definition_id, InterfacePreviousDefinition)
  102. .Emit();
  103. } else {
  104. interface_info.definition_id = interface_decl_id;
  105. interface_info.scope_id =
  106. context.name_scopes().Add(interface_decl_id, SemIR::NameId::Invalid,
  107. interface_info.enclosing_scope_id);
  108. }
  109. // Enter the interface scope.
  110. context.scope_stack().Push(interface_decl_id, interface_info.scope_id);
  111. context.inst_block_stack().Push();
  112. context.node_stack().Push(node_id, interface_id);
  113. // We use the arg stack to build the witness table type.
  114. context.args_type_info_stack().Push();
  115. // Declare and introduce `Self`.
  116. if (!interface_info.is_defined()) {
  117. // TODO: Once we support parameterized interfaces, this won't be the right
  118. // type. For `interface X(T:! type)`, the type of `Self` is `X(T)`, whereas
  119. // this will be simply `X`.
  120. auto self_type_id = context.GetTypeIdForTypeInst(interface_decl_id);
  121. // We model `Self` as a symbolic binding whose type is the interface.
  122. // Because there is no equivalent non-symbolic value, we use `Invalid` as
  123. // the `value_id` on the `BindSymbolicName`.
  124. auto bind_name_id = context.bind_names().Add(
  125. {.name_id = SemIR::NameId::SelfType,
  126. .enclosing_scope_id = interface_info.scope_id,
  127. .bind_index = context.scope_stack().AddCompileTimeBinding()});
  128. interface_info.self_param_id =
  129. context.AddInst({Parse::NodeId::Invalid,
  130. SemIR::BindSymbolicName{self_type_id, bind_name_id,
  131. SemIR::InstId::Invalid}});
  132. context.name_scopes()
  133. .Get(interface_info.scope_id)
  134. .names.insert({SemIR::NameId::SelfType, interface_info.self_param_id});
  135. }
  136. // TODO: Handle the case where there's control flow in the interface body. For
  137. // example:
  138. //
  139. // interface C {
  140. // let v: if true then i32 else f64;
  141. // }
  142. //
  143. // We may need to track a list of instruction blocks here, as we do for a
  144. // function.
  145. interface_info.body_block_id = context.inst_block_stack().PeekOrAdd();
  146. return true;
  147. }
  148. auto HandleInterfaceDefinition(Context& context,
  149. Parse::InterfaceDefinitionId /*node_id*/)
  150. -> bool {
  151. auto interface_id =
  152. context.node_stack().Pop<Parse::NodeKind::InterfaceDefinitionStart>();
  153. context.inst_block_stack().Pop();
  154. auto associated_entities_id = context.args_type_info_stack().Pop();
  155. // The interface type is now fully defined.
  156. auto& interface_info = context.interfaces().Get(interface_id);
  157. if (!interface_info.associated_entities_id.is_valid()) {
  158. interface_info.associated_entities_id = associated_entities_id;
  159. }
  160. // The decl_name_stack and scopes are popped by `ProcessNodeIds`.
  161. return true;
  162. }
  163. } // namespace Carbon::Check