handle_interface.cpp 5.8 KB

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