handle_interface.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/modifiers.h"
  6. namespace Carbon::Check {
  7. auto HandleInterfaceIntroducer(Context& context,
  8. Parse::InterfaceIntroducerId parse_node)
  9. -> bool {
  10. // Create an instruction block to hold the instructions created as part of the
  11. // interface signature, such as generic parameters.
  12. context.inst_block_stack().Push();
  13. // Push the bracketing node.
  14. context.node_stack().Push(parse_node);
  15. // Optional modifiers and the name follow.
  16. context.decl_state_stack().Push(DeclState::Interface);
  17. context.decl_name_stack().PushScopeAndStartName();
  18. return true;
  19. }
  20. static auto BuildInterfaceDecl(Context& context,
  21. Parse::AnyInterfaceDeclId parse_node)
  22. -> std::tuple<SemIR::InterfaceId, SemIR::InstId> {
  23. if (context.node_stack().PopIf<Parse::NodeKind::TuplePattern>()) {
  24. context.TODO(parse_node, "generic interface");
  25. }
  26. auto name_context = context.decl_name_stack().FinishName();
  27. context.node_stack()
  28. .PopAndDiscardSoloParseNode<Parse::NodeKind::InterfaceIntroducer>();
  29. // Process modifiers.
  30. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Interface);
  31. LimitModifiersOnDecl(context, KeywordModifierSet::Access,
  32. Lex::TokenKind::Interface);
  33. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  34. if (!!(modifiers & KeywordModifierSet::Access)) {
  35. context.TODO(context.decl_state_stack().innermost().saw_access_modifier,
  36. "access modifier");
  37. }
  38. context.decl_state_stack().Pop(DeclState::Interface);
  39. auto decl_block_id = context.inst_block_stack().Pop();
  40. // Add the interface declaration.
  41. auto interface_decl = SemIR::InterfaceDecl{
  42. parse_node, SemIR::InterfaceId::Invalid, decl_block_id};
  43. auto interface_decl_id = context.AddInst(interface_decl);
  44. // Check whether this is a redeclaration.
  45. auto existing_id = context.decl_name_stack().LookupOrAddName(
  46. name_context, interface_decl_id);
  47. if (existing_id.is_valid()) {
  48. if (auto existing_interface_decl =
  49. context.insts().Get(existing_id).TryAs<SemIR::InterfaceDecl>()) {
  50. // This is a redeclaration of an existing interface.
  51. interface_decl.interface_id = existing_interface_decl->interface_id;
  52. // TODO: Check that the generic parameter list agrees with the prior
  53. // declaration.
  54. } else {
  55. // This is a redeclaration of something other than a interface.
  56. context.DiagnoseDuplicateName(name_context.parse_node, existing_id);
  57. }
  58. }
  59. // Create a new interface if this isn't a valid redeclaration.
  60. if (!interface_decl.interface_id.is_valid()) {
  61. // TODO: If this is an invalid redeclaration of a non-interface entity or
  62. // there was an error in the qualifier, we will have lost track of the
  63. // interface name here. We should keep track of it even if the name is
  64. // invalid.
  65. // TODO: should have a `Self` type id member
  66. interface_decl.interface_id = context.interfaces().Add(
  67. {.name_id = name_context.name_id_for_new_inst(),
  68. .enclosing_scope_id = name_context.enclosing_scope_id_for_new_inst(),
  69. .decl_id = interface_decl_id});
  70. }
  71. // Write the interface ID into the InterfaceDecl.
  72. context.insts().Set(interface_decl_id, interface_decl);
  73. return {interface_decl.interface_id, interface_decl_id};
  74. }
  75. auto HandleInterfaceDecl(Context& context, Parse::InterfaceDeclId parse_node)
  76. -> bool {
  77. BuildInterfaceDecl(context, parse_node);
  78. context.decl_name_stack().PopScope();
  79. return true;
  80. }
  81. auto HandleInterfaceDefinitionStart(
  82. Context& context, Parse::InterfaceDefinitionStartId parse_node) -> bool {
  83. auto [interface_id, interface_decl_id] =
  84. BuildInterfaceDecl(context, parse_node);
  85. auto& interface_info = context.interfaces().Get(interface_id);
  86. // Track that this declaration is the definition.
  87. if (interface_info.definition_id.is_valid()) {
  88. CARBON_DIAGNOSTIC(InterfaceRedefinition, Error,
  89. "Redefinition of interface {0}.", std::string);
  90. CARBON_DIAGNOSTIC(InterfacePreviousDefinition, Note,
  91. "Previous definition was here.");
  92. context.emitter()
  93. .Build(parse_node, InterfaceRedefinition,
  94. context.names().GetFormatted(interface_info.name_id).str())
  95. .Note(context.insts().Get(interface_info.definition_id).parse_node(),
  96. InterfacePreviousDefinition)
  97. .Emit();
  98. } else {
  99. interface_info.definition_id = interface_decl_id;
  100. interface_info.scope_id = context.name_scopes().Add(
  101. interface_decl_id, interface_info.enclosing_scope_id);
  102. }
  103. // Enter the interface scope.
  104. context.PushScope(interface_decl_id, interface_info.scope_id);
  105. // TODO: Introduce `Self`.
  106. context.inst_block_stack().Push();
  107. context.node_stack().Push(parse_node, interface_id);
  108. // TODO: Perhaps use the args_type_info_stack for a witness table.
  109. // TODO: Handle the case where there's control flow in the interface body. For
  110. // example:
  111. //
  112. // interface C {
  113. // let v: if true then i32 else f64;
  114. // }
  115. //
  116. // We may need to track a list of instruction blocks here, as we do for a
  117. // function.
  118. interface_info.body_block_id = context.inst_block_stack().PeekOrAdd();
  119. return true;
  120. }
  121. auto HandleInterfaceDefinition(Context& context,
  122. Parse::InterfaceDefinitionId /*parse_node*/)
  123. -> bool {
  124. auto interface_id =
  125. context.node_stack().Pop<Parse::NodeKind::InterfaceDefinitionStart>();
  126. context.inst_block_stack().Pop();
  127. context.PopScope();
  128. context.decl_name_stack().PopScope();
  129. // The interface type is now fully defined.
  130. auto& interface_info = context.interfaces().Get(interface_id);
  131. interface_info.defined = true;
  132. return true;
  133. }
  134. } // namespace Carbon::Check