handle_named_constraint.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/generic.h"
  6. #include "toolchain/check/handle.h"
  7. #include "toolchain/check/inst.h"
  8. #include "toolchain/check/interface.h"
  9. #include "toolchain/check/modifiers.h"
  10. #include "toolchain/check/type.h"
  11. #include "toolchain/sem_ir/ids.h"
  12. #include "toolchain/sem_ir/named_constraint.h"
  13. #include "toolchain/sem_ir/typed_insts.h"
  14. namespace Carbon::Check {
  15. auto HandleParseNode(Context& context,
  16. Parse::NamedConstraintIntroducerId node_id) -> bool {
  17. // This named constraint is potentially generic.
  18. StartGenericDecl(context);
  19. // Create an instruction block to hold the instructions created as part of the
  20. // named constraint signature, such as generic parameters.
  21. context.inst_block_stack().Push();
  22. // Optional modifiers and the name follow.
  23. context.decl_introducer_state_stack().Push<Lex::TokenKind::Constraint>();
  24. context.decl_name_stack().PushScopeAndStartName();
  25. // Push the bracketing node.
  26. context.node_stack().Push(node_id);
  27. return true;
  28. }
  29. static auto BuildNamedConstraintDecl(Context& context,
  30. Parse::AnyNamedConstraintDeclId node_id,
  31. bool is_definition)
  32. -> std::tuple<SemIR::NamedConstraintId, SemIR::InstId> {
  33. auto name = PopNameComponent(context);
  34. auto name_context = context.decl_name_stack().FinishName(name);
  35. context.node_stack()
  36. .PopAndDiscardSoloNodeId<Parse::NodeKind::NamedConstraintIntroducer>();
  37. // TODO: PopSoloNodeId(`template`) if it's present, and track that in the
  38. // NamedConstraint. Or maybe it should be a modifier, like `abstract class`?
  39. // Process modifiers.
  40. auto [_, parent_scope_inst] =
  41. context.name_scopes().GetInstIfValid(name_context.parent_scope_id);
  42. auto introducer =
  43. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Constraint>();
  44. CheckAccessModifiersOnDecl(context, introducer, parent_scope_inst);
  45. LimitModifiersOnDecl(context, introducer, KeywordModifierSet::Access);
  46. auto decl_block_id = context.inst_block_stack().Pop();
  47. // Add the constraint declaration.
  48. auto constraint_decl = SemIR::NamedConstraintDecl{
  49. SemIR::TypeType::TypeId, SemIR::NamedConstraintId::None, decl_block_id};
  50. auto decl_inst_id = AddPlaceholderInst(context, node_id, constraint_decl);
  51. SemIR::NamedConstraint constraint_info = {
  52. name_context.MakeEntityWithParamsBase(name, decl_inst_id,
  53. /*is_extern=*/false,
  54. SemIR::LibraryNameId::None)};
  55. DiagnoseIfGenericMissingExplicitParameters(context, constraint_info);
  56. // Check whether this is a redeclaration.
  57. SemIR::ScopeLookupResult lookup_result =
  58. context.decl_name_stack().LookupOrAddName(
  59. name_context, decl_inst_id, introducer.modifier_set.GetAccessKind());
  60. if (auto existing_decl = TryGetExistingDecl(context, name, lookup_result,
  61. constraint_info, is_definition)) {
  62. auto existing_constraint_decl =
  63. existing_decl->As<SemIR::NamedConstraintDecl>();
  64. constraint_decl.named_constraint_id =
  65. existing_constraint_decl.named_constraint_id;
  66. constraint_decl.type_id = existing_constraint_decl.type_id;
  67. // TODO: If the new declaration is a definition, keep its parameter
  68. // and implicit parameter lists rather than the ones from the
  69. // previous declaration.
  70. auto prev_decl_generic_id = context.named_constraints()
  71. .Get(constraint_decl.named_constraint_id)
  72. .generic_id;
  73. FinishGenericRedecl(context, prev_decl_generic_id);
  74. } else {
  75. // Create a new named constraint if this isn't a valid redeclaration.
  76. constraint_info.generic_id = BuildGenericDecl(context, decl_inst_id);
  77. constraint_decl.named_constraint_id =
  78. context.named_constraints().Add(constraint_info);
  79. if (constraint_info.has_parameters()) {
  80. constraint_decl.type_id = GetGenericNamedConstraintType(
  81. context, constraint_decl.named_constraint_id,
  82. context.scope_stack().PeekSpecificId());
  83. }
  84. }
  85. // Write the completed NamedConstraintDecl instruction.
  86. ReplaceInstBeforeConstantUse(context, decl_inst_id, constraint_decl);
  87. return {constraint_decl.named_constraint_id, decl_inst_id};
  88. }
  89. auto HandleParseNode(Context& context, Parse::NamedConstraintDeclId node_id)
  90. -> bool {
  91. BuildNamedConstraintDecl(context, node_id, /*is_definition=*/false);
  92. context.decl_name_stack().PopScope();
  93. return true;
  94. }
  95. auto HandleParseNode(Context& context,
  96. Parse::NamedConstraintDefinitionStartId node_id) -> bool {
  97. auto [named_constraint_id, decl_inst_id] =
  98. BuildNamedConstraintDecl(context, node_id, /*is_definition=*/true);
  99. auto& constraint_info = context.named_constraints().Get(named_constraint_id);
  100. // TODO: Support for `template constraint`.
  101. bool is_template = false;
  102. // Track that this declaration is the definition.
  103. CARBON_CHECK(!constraint_info.has_definition_started(),
  104. "Can't merge with defined named constraints.");
  105. constraint_info.definition_id = decl_inst_id;
  106. constraint_info.scope_without_self_id = context.name_scopes().Add(
  107. decl_inst_id, SemIR::NameId::None, constraint_info.parent_scope_id);
  108. StartGenericDefinition(context, constraint_info.generic_id);
  109. context.inst_block_stack().Push();
  110. // Enter the constraint-without-self scope, which is used for the Self
  111. // instruction, since it needs to reference the constraint (without-self)
  112. // generic. Self can't reference the constraint-with-self generic since it's a
  113. // parameter to the generic.
  114. context.scope_stack().PushForEntity(
  115. decl_inst_id, constraint_info.scope_without_self_id,
  116. context.generics().GetSelfSpecific(constraint_info.generic_id));
  117. // Declare and introduce `Self`. We model `Self` as a symbolic binding whose
  118. // type is the named constraint, excluding any other interfaces mentioned by
  119. // `require` declarations. This makes it an empty facet type.
  120. SemIR::TypeId self_type_id = GetNamedConstraintType(
  121. context, named_constraint_id,
  122. context.generics().GetSelfSpecific(constraint_info.generic_id));
  123. constraint_info.self_param_id = AddSelfSymbolicBindingToScope(
  124. context, node_id, self_type_id, constraint_info.scope_without_self_id,
  125. is_template);
  126. // Start the declaration of constraint-with-self.
  127. StartGenericDecl(context);
  128. // Push `Self` as a parameter of the constraint-with-self.
  129. context.scope_stack().PushCompileTimeBinding(constraint_info.self_param_id);
  130. // Add the interface-with-self declaration and build the generic for it. This
  131. // captures the `interface_info.self_param_id` as a parameter of the generic.
  132. auto constraint_with_self_decl = SemIR::NamedConstraintWithSelfDecl{
  133. .named_constraint_id = named_constraint_id};
  134. auto decl_with_self_inst_id =
  135. AddPlaceholderInst(context, node_id, constraint_with_self_decl);
  136. auto generic_with_self_id = BuildGenericDecl(context, decl_with_self_inst_id);
  137. constraint_info.generic_with_self_id = generic_with_self_id;
  138. ReplaceInstBeforeConstantUse(context, decl_with_self_inst_id,
  139. constraint_with_self_decl);
  140. constraint_info.scope_with_self_id =
  141. context.name_scopes().Add(decl_with_self_inst_id, SemIR::NameId::None,
  142. constraint_info.scope_without_self_id);
  143. // Start the definition of constraint-with-self.
  144. StartGenericDefinition(context, constraint_info.generic_with_self_id);
  145. // Enter a scope for the constraint-with-self.
  146. context.scope_stack().PushForEntity(
  147. decl_with_self_inst_id, constraint_info.scope_with_self_id,
  148. context.generics().GetSelfSpecific(constraint_info.generic_with_self_id));
  149. constraint_info.body_block_without_self_id =
  150. context.inst_block_stack().PeekOrAdd();
  151. context.inst_block_stack().Push();
  152. constraint_info.body_block_with_self_id =
  153. context.inst_block_stack().PeekOrAdd();
  154. context.require_impls_stack().Push(named_constraint_id);
  155. context.node_stack().Push(node_id, named_constraint_id);
  156. return true;
  157. }
  158. auto HandleParseNode(Context& context,
  159. Parse::NamedConstraintDefinitionId /*node_id*/) -> bool {
  160. auto named_constraint_id =
  161. context.node_stack()
  162. .Pop<Parse::NodeKind::NamedConstraintDefinitionStart>();
  163. // Pop the body_block_with_self.
  164. context.inst_block_stack().Pop();
  165. auto require_impls_block_id = context.require_impls_blocks().Add(
  166. context.require_impls_stack().PeekTop());
  167. context.require_impls_stack().Pop();
  168. auto& constraint_info = context.named_constraints().Get(named_constraint_id);
  169. if (!constraint_info.complete) {
  170. constraint_info.require_impls_block_id = require_impls_block_id;
  171. // TODO: Do something with `alias` statements in the body of the
  172. // constraint.
  173. constraint_info.complete = true;
  174. }
  175. // Finish the definition of constraint-with-self.
  176. FinishGenericDefinition(context, constraint_info.generic_with_self_id);
  177. // Pop the body_block_without_self.
  178. context.inst_block_stack().Pop();
  179. // Finish the definition of interfconstraintace-without-self.
  180. FinishGenericDefinition(context, constraint_info.generic_id);
  181. // The decl_name_stack and scopes are popped by `ProcessNodeIds`.
  182. return true;
  183. }
  184. } // namespace Carbon::Check