handle_named_constraint.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_id = context.name_scopes().Add(
  107. decl_inst_id, SemIR::NameId::None, constraint_info.parent_scope_id);
  108. auto self_specific_id =
  109. context.generics().GetSelfSpecific(constraint_info.generic_id);
  110. StartGenericDefinition(context, constraint_info.generic_id);
  111. context.inst_block_stack().Push();
  112. context.require_impls_stack().PushArray();
  113. // Declare and introduce `Self`. We model `Self` as a symbolic binding whose
  114. // type is the named constraint, excluding any other interfaces mentioned by
  115. // `require` declarations. This makes it an empty facet type.
  116. SemIR::TypeId self_type_id =
  117. GetNamedConstraintType(context, named_constraint_id, self_specific_id);
  118. constraint_info.self_param_id = AddSelfGenericParameter(
  119. context, node_id, self_type_id, constraint_info.scope_id, is_template);
  120. // Enter the constraint scope.
  121. context.scope_stack().PushForEntity(decl_inst_id, constraint_info.scope_id,
  122. self_specific_id);
  123. constraint_info.body_block_id = context.inst_block_stack().PeekOrAdd();
  124. context.node_stack().Push(node_id, named_constraint_id);
  125. return true;
  126. }
  127. auto HandleParseNode(Context& context,
  128. Parse::NamedConstraintDefinitionId /*node_id*/) -> bool {
  129. auto named_constraint_id =
  130. context.node_stack()
  131. .Pop<Parse::NodeKind::NamedConstraintDefinitionStart>();
  132. context.inst_block_stack().Pop();
  133. auto require_impls_block_id = context.require_impls_blocks().Add(
  134. context.require_impls_stack().PeekArray());
  135. context.require_impls_stack().PopArray();
  136. auto& constraint_info = context.named_constraints().Get(named_constraint_id);
  137. if (!constraint_info.complete) {
  138. constraint_info.require_impls_block_id = require_impls_block_id;
  139. // TODO: Do something with `alias` statements in the body of the
  140. // constraint.
  141. constraint_info.complete = true;
  142. }
  143. FinishGenericDefinition(context, constraint_info.generic_id);
  144. // The decl_name_stack and scopes are popped by `ProcessNodeIds`.
  145. return true;
  146. }
  147. } // namespace Carbon::Check