handle_interface.cpp 8.3 KB

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