handle_interface.cpp 7.4 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/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,
  79. {node_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. interface_info.self_param_id =
  128. context.AddInst({Parse::NodeId::Invalid,
  129. SemIR::BindSymbolicName{self_type_id, bind_name_id,
  130. SemIR::InstId::Invalid}});
  131. context.name_scopes()
  132. .Get(interface_info.scope_id)
  133. .names.insert({SemIR::NameId::SelfType, interface_info.self_param_id});
  134. }
  135. // TODO: Handle the case where there's control flow in the interface body. For
  136. // example:
  137. //
  138. // interface C {
  139. // let v: if true then i32 else f64;
  140. // }
  141. //
  142. // We may need to track a list of instruction blocks here, as we do for a
  143. // function.
  144. interface_info.body_block_id = context.inst_block_stack().PeekOrAdd();
  145. return true;
  146. }
  147. auto HandleInterfaceDefinition(Context& context,
  148. Parse::InterfaceDefinitionId /*node_id*/)
  149. -> bool {
  150. auto interface_id =
  151. context.node_stack().Pop<Parse::NodeKind::InterfaceDefinitionStart>();
  152. context.inst_block_stack().Pop();
  153. context.scope_stack().Pop();
  154. context.decl_name_stack().PopScope();
  155. auto associated_entities_id = context.args_type_info_stack().Pop();
  156. // The interface type is now fully defined.
  157. auto& interface_info = context.interfaces().Get(interface_id);
  158. if (!interface_info.associated_entities_id.is_valid()) {
  159. interface_info.associated_entities_id = associated_entities_id;
  160. }
  161. return true;
  162. }
  163. } // namespace Carbon::Check