handle_interface.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 HandleInterfaceIntroducer(Context& context,
  14. Parse::InterfaceIntroducerId node_id) -> 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.implicit_params_id,
  58. name.params_id),
  59. DeclParams(context.interfaces().Get(
  60. existing_interface_decl->interface_id)))) {
  61. // This is a redeclaration of an existing interface.
  62. interface_decl.interface_id = existing_interface_decl->interface_id;
  63. interface_decl.type_id = existing_interface_decl->type_id;
  64. // TODO: If the new declaration is a definition, keep its parameter
  65. // and implicit parameter lists rather than the ones from the
  66. // previous declaration.
  67. }
  68. } else {
  69. // This is a redeclaration of something other than a interface.
  70. context.DiagnoseDuplicateName(interface_decl_id, existing_id);
  71. }
  72. }
  73. // Create a new interface if this isn't a valid redeclaration.
  74. if (!interface_decl.interface_id.is_valid()) {
  75. auto generic_id = FinishGenericDecl(context, interface_decl_id);
  76. // TODO: If this is an invalid redeclaration of a non-interface entity or
  77. // there was an error in the qualifier, we will have lost track of the
  78. // interface name here. We should keep track of it even if the name is
  79. // invalid.
  80. SemIR::Interface interface_info = {
  81. .name_id = name_context.name_id_for_new_inst(),
  82. .parent_scope_id = name_context.parent_scope_id_for_new_inst(),
  83. .generic_id = generic_id,
  84. .implicit_param_refs_id = name.implicit_params_id,
  85. .param_refs_id = name.params_id,
  86. .decl_id = interface_decl_id};
  87. interface_decl.interface_id = context.interfaces().Add(interface_info);
  88. if (interface_info.is_generic()) {
  89. interface_decl.type_id =
  90. context.GetGenericInterfaceType(interface_decl.interface_id);
  91. }
  92. } else {
  93. FinishGenericRedecl(
  94. context, interface_decl_id,
  95. context.interfaces().Get(interface_decl.interface_id).generic_id);
  96. }
  97. // Write the interface ID into the InterfaceDecl.
  98. context.ReplaceInstBeforeConstantUse(interface_decl_id, interface_decl);
  99. return {interface_decl.interface_id, interface_decl_id};
  100. }
  101. auto HandleInterfaceDecl(Context& context, Parse::InterfaceDeclId node_id)
  102. -> bool {
  103. BuildInterfaceDecl(context, node_id);
  104. context.decl_name_stack().PopScope();
  105. return true;
  106. }
  107. auto HandleInterfaceDefinitionStart(Context& context,
  108. Parse::InterfaceDefinitionStartId node_id)
  109. -> bool {
  110. auto [interface_id, interface_decl_id] = BuildInterfaceDecl(context, node_id);
  111. auto& interface_info = context.interfaces().Get(interface_id);
  112. // Track that this declaration is the definition.
  113. if (interface_info.is_defined()) {
  114. CARBON_DIAGNOSTIC(InterfaceRedefinition, Error,
  115. "Redefinition of interface {0}.", SemIR::NameId);
  116. CARBON_DIAGNOSTIC(InterfacePreviousDefinition, Note,
  117. "Previous definition was here.");
  118. context.emitter()
  119. .Build(node_id, InterfaceRedefinition, interface_info.name_id)
  120. .Note(interface_info.definition_id, InterfacePreviousDefinition)
  121. .Emit();
  122. } else {
  123. interface_info.definition_id = interface_decl_id;
  124. interface_info.scope_id =
  125. context.name_scopes().Add(interface_decl_id, SemIR::NameId::Invalid,
  126. interface_info.parent_scope_id);
  127. }
  128. // Enter the interface scope.
  129. context.scope_stack().Push(interface_decl_id, interface_info.scope_id);
  130. StartGenericDefinition(context, interface_info.generic_id);
  131. context.inst_block_stack().Push();
  132. context.node_stack().Push(node_id, interface_id);
  133. // We use the arg stack to build the witness table type.
  134. context.args_type_info_stack().Push();
  135. // Declare and introduce `Self`.
  136. if (!interface_info.is_defined()) {
  137. SemIR::TypeId self_type_id = SemIR::TypeId::Invalid;
  138. if (interface_info.is_generic()) {
  139. auto instance_id =
  140. MakeGenericSelfInstance(context, interface_info.generic_id);
  141. self_type_id = context.GetTypeIdForTypeConstant(
  142. TryEvalInst(context, SemIR::InstId::Invalid,
  143. SemIR::InterfaceType{.type_id = SemIR::TypeId::TypeType,
  144. .interface_id = interface_id,
  145. .instance_id = instance_id}));
  146. } else {
  147. self_type_id = context.GetTypeIdForTypeInst(interface_decl_id);
  148. }
  149. // We model `Self` as a symbolic binding whose type is the interface.
  150. // Because there is no equivalent non-symbolic value, we use `Invalid` as
  151. // the `value_id` on the `BindSymbolicName`.
  152. auto bind_name_id = context.bind_names().Add(
  153. {.name_id = SemIR::NameId::SelfType,
  154. .parent_scope_id = interface_info.scope_id,
  155. .bind_index = context.scope_stack().AddCompileTimeBinding()});
  156. interface_info.self_param_id = context.AddInst<SemIR::BindSymbolicName>(
  157. SemIR::LocId::Invalid, {.type_id = self_type_id,
  158. .bind_name_id = bind_name_id,
  159. .value_id = SemIR::InstId::Invalid});
  160. context.scope_stack().PushCompileTimeBinding(interface_info.self_param_id);
  161. context.name_scopes().AddRequiredName(interface_info.scope_id,
  162. SemIR::NameId::SelfType,
  163. interface_info.self_param_id);
  164. }
  165. // TODO: Handle the case where there's control flow in the interface body. For
  166. // example:
  167. //
  168. // interface C {
  169. // let v: if true then i32 else f64;
  170. // }
  171. //
  172. // We may need to track a list of instruction blocks here, as we do for a
  173. // function.
  174. interface_info.body_block_id = context.inst_block_stack().PeekOrAdd();
  175. return true;
  176. }
  177. auto HandleInterfaceDefinition(Context& context,
  178. Parse::InterfaceDefinitionId /*node_id*/)
  179. -> bool {
  180. auto interface_id =
  181. context.node_stack().Pop<Parse::NodeKind::InterfaceDefinitionStart>();
  182. context.inst_block_stack().Pop();
  183. auto associated_entities_id = context.args_type_info_stack().Pop();
  184. // The interface type is now fully defined.
  185. auto& interface_info = context.interfaces().Get(interface_id);
  186. if (!interface_info.associated_entities_id.is_valid()) {
  187. interface_info.associated_entities_id = associated_entities_id;
  188. }
  189. FinishGenericDefinition(context, interface_info.generic_id);
  190. // The decl_name_stack and scopes are popped by `ProcessNodeIds`.
  191. return true;
  192. }
  193. } // namespace Carbon::Check