handle_interface.cpp 9.0 KB

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