handle_interface.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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(
  21. Context& context, Parse::NodeIdOneOf<Parse::InterfaceDeclId,
  22. Parse::InterfaceDefinitionStartId>
  23. parse_node)
  24. -> std::tuple<SemIR::InterfaceId, SemIR::InstId> {
  25. auto name_context = context.decl_name_stack().FinishName();
  26. context.node_stack()
  27. .PopAndDiscardSoloParseNode<Parse::NodeKind::InterfaceIntroducer>();
  28. // Process modifiers.
  29. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Interface);
  30. LimitModifiersOnDecl(context, KeywordModifierSet::Access,
  31. Lex::TokenKind::Interface);
  32. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  33. if (!!(modifiers & KeywordModifierSet::Access)) {
  34. context.TODO(context.decl_state_stack().innermost().saw_access_modifier,
  35. "access modifier");
  36. }
  37. context.decl_state_stack().Pop(DeclState::Interface);
  38. auto decl_block_id = context.inst_block_stack().Pop();
  39. // Add the interface declaration.
  40. auto interface_decl = SemIR::InterfaceDecl{
  41. parse_node, SemIR::InterfaceId::Invalid, decl_block_id};
  42. auto interface_decl_id = context.AddInst(interface_decl);
  43. // Check whether this is a redeclaration.
  44. auto existing_id = context.decl_name_stack().LookupOrAddName(
  45. name_context, interface_decl_id);
  46. if (existing_id.is_valid()) {
  47. if (auto existing_interface_decl =
  48. context.insts().Get(existing_id).TryAs<SemIR::InterfaceDecl>()) {
  49. // This is a redeclaration of an existing interface.
  50. interface_decl.interface_id = existing_interface_decl->interface_id;
  51. // TODO: Check that the generic parameter list agrees with the prior
  52. // declaration.
  53. } else {
  54. // This is a redeclaration of something other than a interface.
  55. context.DiagnoseDuplicateName(name_context.parse_node, existing_id);
  56. }
  57. }
  58. // Create a new interface if this isn't a valid redeclaration.
  59. if (!interface_decl.interface_id.is_valid()) {
  60. // TODO: If this is an invalid redeclaration of a non-interface entity or
  61. // there was an error in the qualifier, we will have lost track of the
  62. // interface name here. We should keep track of it even if the name is
  63. // invalid.
  64. // TODO: should have a `Self` type id member
  65. interface_decl.interface_id = context.interfaces().Add(
  66. {.name_id =
  67. name_context.state == DeclNameStack::NameContext::State::Unresolved
  68. ? name_context.unresolved_name_id
  69. : SemIR::NameId::Invalid,
  70. .decl_id = interface_decl_id});
  71. }
  72. // Write the interface ID into the InterfaceDecl.
  73. context.insts().Set(interface_decl_id, interface_decl);
  74. return {interface_decl.interface_id, interface_decl_id};
  75. }
  76. auto HandleInterfaceDecl(Context& context, Parse::InterfaceDeclId parse_node)
  77. -> bool {
  78. BuildInterfaceDecl(context, parse_node);
  79. context.decl_name_stack().PopScope();
  80. return true;
  81. }
  82. auto HandleInterfaceDefinitionStart(
  83. Context& context, Parse::InterfaceDefinitionStartId parse_node) -> bool {
  84. auto [interface_id, interface_decl_id] =
  85. BuildInterfaceDecl(context, parse_node);
  86. auto& interface_info = context.interfaces().Get(interface_id);
  87. // Track that this declaration is the definition.
  88. if (interface_info.definition_id.is_valid()) {
  89. CARBON_DIAGNOSTIC(InterfaceRedefinition, Error,
  90. "Redefinition of interface {0}.", std::string);
  91. CARBON_DIAGNOSTIC(InterfacePreviousDefinition, Note,
  92. "Previous definition was here.");
  93. context.emitter()
  94. .Build(parse_node, InterfaceRedefinition,
  95. context.names().GetFormatted(interface_info.name_id).str())
  96. .Note(context.insts().Get(interface_info.definition_id).parse_node(),
  97. InterfacePreviousDefinition)
  98. .Emit();
  99. } else {
  100. interface_info.definition_id = interface_decl_id;
  101. interface_info.scope_id = context.name_scopes().Add(interface_decl_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