handle_interface.cpp 6.1 KB

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