handle_interface.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. if (introducer.modifier_set.HasAnyOf(KeywordModifierSet::Access)) {
  39. context.TODO(introducer.modifier_node_id(ModifierOrder::Access),
  40. "access modifier");
  41. }
  42. auto decl_block_id = context.inst_block_stack().Pop();
  43. // Add the interface declaration.
  44. auto interface_decl = SemIR::InterfaceDecl{
  45. SemIR::TypeId::TypeType, SemIR::InterfaceId::Invalid, decl_block_id};
  46. auto interface_decl_id =
  47. context.AddPlaceholderInst(SemIR::LocIdAndInst(node_id, interface_decl));
  48. // Check whether this is a redeclaration.
  49. auto existing_id = context.decl_name_stack().LookupOrAddName(
  50. name_context, interface_decl_id);
  51. if (existing_id.is_valid()) {
  52. if (auto existing_interface_decl =
  53. context.insts().Get(existing_id).TryAs<SemIR::InterfaceDecl>()) {
  54. // TODO: Implement full redeclaration checking. See `MergeClassDecl`. For
  55. // now we just check the generic parameters match.
  56. if (CheckRedeclParamsMatch(
  57. context,
  58. DeclParams(interface_decl_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. // 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. // TODO: If this is an invalid redeclaration of a non-interface entity or
  76. // there was an error in the qualifier, we will have lost track of the
  77. // interface name here. We should keep track of it even if the name is
  78. // invalid.
  79. interface_decl.interface_id = context.interfaces().Add(
  80. {.name_id = name_context.name_id_for_new_inst(),
  81. .parent_scope_id = name_context.parent_scope_id_for_new_inst(),
  82. .implicit_param_refs_id = name.implicit_params_id,
  83. .param_refs_id = name.params_id,
  84. .decl_id = interface_decl_id});
  85. }
  86. // TODO: For a generic interface declaration, set the `type_id` to a suitable
  87. // generic interface type rather than `type`.
  88. // Write the interface ID into the InterfaceDecl.
  89. context.ReplaceInstBeforeConstantUse(interface_decl_id, interface_decl);
  90. return {interface_decl.interface_id, interface_decl_id};
  91. }
  92. auto HandleInterfaceDecl(Context& context, Parse::InterfaceDeclId node_id)
  93. -> bool {
  94. BuildInterfaceDecl(context, node_id);
  95. context.decl_name_stack().PopScope();
  96. return true;
  97. }
  98. auto HandleInterfaceDefinitionStart(Context& context,
  99. Parse::InterfaceDefinitionStartId node_id)
  100. -> bool {
  101. auto [interface_id, interface_decl_id] = BuildInterfaceDecl(context, node_id);
  102. auto& interface_info = context.interfaces().Get(interface_id);
  103. // Track that this declaration is the definition.
  104. if (interface_info.is_defined()) {
  105. CARBON_DIAGNOSTIC(InterfaceRedefinition, Error,
  106. "Redefinition of interface {0}.", SemIR::NameId);
  107. CARBON_DIAGNOSTIC(InterfacePreviousDefinition, Note,
  108. "Previous definition was here.");
  109. context.emitter()
  110. .Build(node_id, InterfaceRedefinition, interface_info.name_id)
  111. .Note(interface_info.definition_id, InterfacePreviousDefinition)
  112. .Emit();
  113. } else {
  114. interface_info.definition_id = interface_decl_id;
  115. interface_info.scope_id =
  116. context.name_scopes().Add(interface_decl_id, SemIR::NameId::Invalid,
  117. interface_info.parent_scope_id);
  118. }
  119. // Enter the interface scope.
  120. context.scope_stack().Push(interface_decl_id, interface_info.scope_id);
  121. context.inst_block_stack().Push();
  122. context.node_stack().Push(node_id, interface_id);
  123. // We use the arg stack to build the witness table type.
  124. context.args_type_info_stack().Push();
  125. // Declare and introduce `Self`.
  126. if (!interface_info.is_defined()) {
  127. // TODO: Once we support parameterized interfaces, this won't be the right
  128. // type. For `interface X(T:! type)`, the type of `Self` is `X(T)`, whereas
  129. // this will be simply `X`.
  130. auto self_type_id = context.GetTypeIdForTypeInst(interface_decl_id);
  131. // We model `Self` as a symbolic binding whose type is the interface.
  132. // Because there is no equivalent non-symbolic value, we use `Invalid` as
  133. // the `value_id` on the `BindSymbolicName`.
  134. auto bind_name_id = context.bind_names().Add(
  135. {.name_id = SemIR::NameId::SelfType,
  136. .parent_scope_id = interface_info.scope_id,
  137. .bind_index = context.scope_stack().AddCompileTimeBinding()});
  138. interface_info.self_param_id = context.AddInst<SemIR::BindSymbolicName>(
  139. SemIR::LocId::Invalid, {.type_id = self_type_id,
  140. .bind_name_id = bind_name_id,
  141. .value_id = SemIR::InstId::Invalid});
  142. context.name_scopes()
  143. .Get(interface_info.scope_id)
  144. .names.insert({SemIR::NameId::SelfType, interface_info.self_param_id});
  145. }
  146. // TODO: Handle the case where there's control flow in the interface body. For
  147. // example:
  148. //
  149. // interface C {
  150. // let v: if true then i32 else f64;
  151. // }
  152. //
  153. // We may need to track a list of instruction blocks here, as we do for a
  154. // function.
  155. interface_info.body_block_id = context.inst_block_stack().PeekOrAdd();
  156. return true;
  157. }
  158. auto HandleInterfaceDefinition(Context& context,
  159. Parse::InterfaceDefinitionId /*node_id*/)
  160. -> bool {
  161. auto interface_id =
  162. context.node_stack().Pop<Parse::NodeKind::InterfaceDefinitionStart>();
  163. context.inst_block_stack().Pop();
  164. auto associated_entities_id = context.args_type_info_stack().Pop();
  165. // The interface type is now fully defined.
  166. auto& interface_info = context.interfaces().Get(interface_id);
  167. if (!interface_info.associated_entities_id.is_valid()) {
  168. interface_info.associated_entities_id = associated_entities_id;
  169. }
  170. // The decl_name_stack and scopes are popped by `ProcessNodeIds`.
  171. return true;
  172. }
  173. } // namespace Carbon::Check