handle_interface.cpp 5.7 KB

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