handle_interface.cpp 5.6 KB

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