handle_interface.cpp 7.5 KB

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