handle_interface.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/handle.h"
  6. #include "toolchain/check/interface.h"
  7. #include "toolchain/check/modifiers.h"
  8. #include "toolchain/check/name_component.h"
  9. #include "toolchain/sem_ir/typed_insts.h"
  10. namespace Carbon::Check {
  11. auto HandleInterfaceIntroducer(Context& context,
  12. Parse::InterfaceIntroducerId node_id) -> bool {
  13. // Create an instruction block to hold the instructions created as part of the
  14. // interface signature, such as generic parameters.
  15. context.inst_block_stack().Push();
  16. // Push the bracketing node.
  17. context.node_stack().Push(node_id);
  18. // Optional modifiers and the name follow.
  19. context.decl_state_stack().Push(DeclState::Interface);
  20. context.decl_name_stack().PushScopeAndStartName();
  21. return true;
  22. }
  23. static auto BuildInterfaceDecl(Context& context,
  24. Parse::AnyInterfaceDeclId node_id)
  25. -> std::tuple<SemIR::InterfaceId, SemIR::InstId> {
  26. auto name = PopNameComponent(context);
  27. if (name.params_id.is_valid() || name.implicit_params_id.is_valid()) {
  28. context.TODO(node_id, "generic interface");
  29. }
  30. auto name_context = context.decl_name_stack().FinishName(name);
  31. context.node_stack()
  32. .PopAndDiscardSoloNodeId<Parse::NodeKind::InterfaceIntroducer>();
  33. // Process modifiers.
  34. auto [_, parent_scope_inst] =
  35. context.name_scopes().GetInstIfValid(name_context.parent_scope_id);
  36. CheckAccessModifiersOnDecl(context, Lex::TokenKind::Interface,
  37. parent_scope_inst);
  38. LimitModifiersOnDecl(context, KeywordModifierSet::Access,
  39. Lex::TokenKind::Interface);
  40. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  41. if (modifiers.HasAnyOf(KeywordModifierSet::Access)) {
  42. context.TODO(context.decl_state_stack().innermost().modifier_node_id(
  43. ModifierOrder::Access),
  44. "access modifier");
  45. }
  46. context.decl_state_stack().Pop(DeclState::Interface);
  47. auto decl_block_id = context.inst_block_stack().Pop();
  48. // Add the interface declaration.
  49. auto interface_decl = SemIR::InterfaceDecl{
  50. SemIR::TypeId::TypeType, SemIR::InterfaceId::Invalid, decl_block_id};
  51. auto interface_decl_id =
  52. context.AddPlaceholderInst(SemIR::LocIdAndInst(node_id, interface_decl));
  53. // Check whether this is a redeclaration.
  54. auto existing_id = context.decl_name_stack().LookupOrAddName(
  55. name_context, interface_decl_id);
  56. if (existing_id.is_valid()) {
  57. if (auto existing_interface_decl =
  58. context.insts().Get(existing_id).TryAs<SemIR::InterfaceDecl>()) {
  59. // This is a redeclaration of an existing interface.
  60. interface_decl.interface_id = existing_interface_decl->interface_id;
  61. // TODO: Check that the generic parameter list agrees with the prior
  62. // declaration.
  63. } else {
  64. // This is a redeclaration of something other than a interface.
  65. context.DiagnoseDuplicateName(interface_decl_id, existing_id);
  66. }
  67. }
  68. // Create a new interface if this isn't a valid redeclaration.
  69. if (!interface_decl.interface_id.is_valid()) {
  70. // TODO: If this is an invalid redeclaration of a non-interface entity or
  71. // there was an error in the qualifier, we will have lost track of the
  72. // interface name here. We should keep track of it even if the name is
  73. // invalid.
  74. interface_decl.interface_id = context.interfaces().Add(
  75. {.name_id = name_context.name_id_for_new_inst(),
  76. .parent_scope_id = name_context.parent_scope_id_for_new_inst(),
  77. .decl_id = interface_decl_id});
  78. }
  79. // Write the interface ID into the InterfaceDecl.
  80. context.ReplaceInstBeforeConstantUse(interface_decl_id, interface_decl);
  81. return {interface_decl.interface_id, interface_decl_id};
  82. }
  83. auto HandleInterfaceDecl(Context& context, Parse::InterfaceDeclId node_id)
  84. -> bool {
  85. BuildInterfaceDecl(context, node_id);
  86. context.decl_name_stack().PopScope();
  87. return true;
  88. }
  89. auto HandleInterfaceDefinitionStart(Context& context,
  90. Parse::InterfaceDefinitionStartId node_id)
  91. -> bool {
  92. auto [interface_id, interface_decl_id] = BuildInterfaceDecl(context, node_id);
  93. auto& interface_info = context.interfaces().Get(interface_id);
  94. // Track that this declaration is the definition.
  95. if (interface_info.is_defined()) {
  96. CARBON_DIAGNOSTIC(InterfaceRedefinition, Error,
  97. "Redefinition of interface {0}.", SemIR::NameId);
  98. CARBON_DIAGNOSTIC(InterfacePreviousDefinition, Note,
  99. "Previous definition was here.");
  100. context.emitter()
  101. .Build(node_id, InterfaceRedefinition, interface_info.name_id)
  102. .Note(interface_info.definition_id, InterfacePreviousDefinition)
  103. .Emit();
  104. } else {
  105. interface_info.definition_id = interface_decl_id;
  106. interface_info.scope_id =
  107. context.name_scopes().Add(interface_decl_id, SemIR::NameId::Invalid,
  108. interface_info.parent_scope_id);
  109. }
  110. // Enter the interface scope.
  111. context.scope_stack().Push(interface_decl_id, interface_info.scope_id);
  112. context.inst_block_stack().Push();
  113. context.node_stack().Push(node_id, interface_id);
  114. // We use the arg stack to build the witness table type.
  115. context.args_type_info_stack().Push();
  116. // Declare and introduce `Self`.
  117. if (!interface_info.is_defined()) {
  118. // TODO: Once we support parameterized interfaces, this won't be the right
  119. // type. For `interface X(T:! type)`, the type of `Self` is `X(T)`, whereas
  120. // this will be simply `X`.
  121. auto self_type_id = context.GetTypeIdForTypeInst(interface_decl_id);
  122. // We model `Self` as a symbolic binding whose type is the interface.
  123. // Because there is no equivalent non-symbolic value, we use `Invalid` as
  124. // the `value_id` on the `BindSymbolicName`.
  125. auto bind_name_id = context.bind_names().Add(
  126. {.name_id = SemIR::NameId::SelfType,
  127. .parent_scope_id = interface_info.scope_id,
  128. .bind_index = context.scope_stack().AddCompileTimeBinding()});
  129. interface_info.self_param_id = context.AddInst<SemIR::BindSymbolicName>(
  130. SemIR::LocId::Invalid, {.type_id = self_type_id,
  131. .bind_name_id = bind_name_id,
  132. .value_id = SemIR::InstId::Invalid});
  133. context.name_scopes()
  134. .Get(interface_info.scope_id)
  135. .names.insert({SemIR::NameId::SelfType, interface_info.self_param_id});
  136. }
  137. // TODO: Handle the case where there's control flow in the interface body. For
  138. // example:
  139. //
  140. // interface C {
  141. // let v: if true then i32 else f64;
  142. // }
  143. //
  144. // We may need to track a list of instruction blocks here, as we do for a
  145. // function.
  146. interface_info.body_block_id = context.inst_block_stack().PeekOrAdd();
  147. return true;
  148. }
  149. auto HandleInterfaceDefinition(Context& context,
  150. Parse::InterfaceDefinitionId /*node_id*/)
  151. -> bool {
  152. auto interface_id =
  153. context.node_stack().Pop<Parse::NodeKind::InterfaceDefinitionStart>();
  154. context.inst_block_stack().Pop();
  155. auto associated_entities_id = context.args_type_info_stack().Pop();
  156. // The interface type is now fully defined.
  157. auto& interface_info = context.interfaces().Get(interface_id);
  158. if (!interface_info.associated_entities_id.is_valid()) {
  159. interface_info.associated_entities_id = associated_entities_id;
  160. }
  161. // The decl_name_stack and scopes are popped by `ProcessNodeIds`.
  162. return true;
  163. }
  164. } // namespace Carbon::Check