handle_interface.cpp 6.2 KB

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