handle_require.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/base/kind_switch.h"
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/check/convert.h"
  7. #include "toolchain/check/handle.h"
  8. #include "toolchain/check/modifiers.h"
  9. #include "toolchain/check/name_lookup.h"
  10. #include "toolchain/check/subst.h"
  11. #include "toolchain/check/type_completion.h"
  12. #include "toolchain/parse/node_ids.h"
  13. #include "toolchain/sem_ir/named_constraint.h"
  14. #include "toolchain/sem_ir/type_iterator.h"
  15. #include "toolchain/sem_ir/typed_insts.h"
  16. namespace Carbon::Check {
  17. auto HandleParseNode(Context& context, Parse::RequireIntroducerId node_id)
  18. -> bool {
  19. // Create an instruction block to hold the instructions created for the type
  20. // and constraint.
  21. context.inst_block_stack().Push();
  22. // Optional modifiers follow.
  23. context.decl_introducer_state_stack().Push<Lex::TokenKind::Require>();
  24. auto scope_id = context.scope_stack().PeekNameScopeId();
  25. auto scope_inst_id = context.name_scopes().Get(scope_id).inst_id();
  26. auto scope_inst = context.insts().Get(scope_inst_id);
  27. if (!scope_inst.Is<SemIR::InterfaceDecl>() &&
  28. !scope_inst.Is<SemIR::NamedConstraintDecl>()) {
  29. CARBON_DIAGNOSTIC(
  30. RequireInWrongScope, Error,
  31. "`require` can only be used in an `interface` or `constraint`");
  32. context.emitter().Emit(node_id, RequireInWrongScope);
  33. scope_inst_id = SemIR::ErrorInst::InstId;
  34. }
  35. context.node_stack().Push(node_id, scope_inst_id);
  36. return true;
  37. }
  38. auto HandleParseNode(Context& context, Parse::RequireDefaultSelfImplsId node_id)
  39. -> bool {
  40. auto scope_inst_id =
  41. context.node_stack().Peek<Parse::NodeKind::RequireIntroducer>();
  42. if (scope_inst_id == SemIR::ErrorInst::InstId) {
  43. context.node_stack().Push(node_id, SemIR::ErrorInst::TypeInstId);
  44. return true;
  45. }
  46. auto scope_id = context.scope_stack().PeekNameScopeId();
  47. auto lookup_result =
  48. LookupNameInExactScope(context, node_id, SemIR::NameId::SelfType,
  49. scope_id, context.name_scopes().Get(scope_id),
  50. /*is_being_declared=*/false);
  51. CARBON_CHECK(lookup_result.is_found());
  52. auto self_inst_id = lookup_result.target_inst_id();
  53. auto self_type_id = context.insts().Get(self_inst_id).type_id();
  54. CARBON_CHECK(context.types().Is<SemIR::FacetType>(self_type_id));
  55. auto self_facet_as_type = AddTypeInst<SemIR::FacetAccessType>(
  56. context, node_id,
  57. {.type_id = SemIR::TypeType::TypeId,
  58. .facet_value_inst_id = self_inst_id});
  59. context.node_stack().Push(node_id, self_facet_as_type);
  60. return true;
  61. }
  62. auto HandleParseNode(Context& context, Parse::RequireTypeImplsId node_id)
  63. -> bool {
  64. auto [self_node_id, self_inst_id] = context.node_stack().PopExprWithNodeId();
  65. auto self_type = ExprAsType(context, self_node_id, self_inst_id);
  66. context.node_stack().Push(node_id, self_type.inst_id);
  67. return true;
  68. }
  69. static auto TypeStructureReferencesSelf(
  70. Context& context, SemIR::TypeInstId inst_id,
  71. const SemIR::IdentifiedFacetType& identified_facet_type) -> bool {
  72. if (inst_id == SemIR::ErrorInst::TypeInstId) {
  73. // Don't generate more diagnostics.
  74. return true;
  75. }
  76. auto find_self = [&](SemIR::TypeIterator& type_iter) -> bool {
  77. while (true) {
  78. auto step = type_iter.Next();
  79. if (step.Is<SemIR::TypeIterator::Step::Done>()) {
  80. break;
  81. }
  82. CARBON_KIND_SWITCH(step.any) {
  83. case CARBON_KIND(SemIR::TypeIterator::Step::Error _): {
  84. // Don't generate more diagnostics.
  85. return true;
  86. }
  87. case CARBON_KIND(SemIR::TypeIterator::Step::SymbolicBinding bind): {
  88. if (context.entity_names().Get(bind.entity_name_id).name_id ==
  89. SemIR::NameId::SelfType) {
  90. return true;
  91. }
  92. break;
  93. }
  94. default:
  95. break;
  96. }
  97. }
  98. return false;
  99. };
  100. {
  101. SemIR::TypeIterator type_iter(&context.sem_ir());
  102. type_iter.Add(context.constant_values().GetConstantTypeInstId(inst_id));
  103. if (find_self(type_iter)) {
  104. return true;
  105. }
  106. }
  107. if (identified_facet_type.required_interfaces().empty()) {
  108. return false;
  109. }
  110. for (auto specific_interface : identified_facet_type.required_interfaces()) {
  111. SemIR::TypeIterator type_iter(&context.sem_ir());
  112. type_iter.Add(specific_interface);
  113. if (!find_self(type_iter)) {
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. auto HandleParseNode(Context& context, Parse::RequireDeclId node_id) -> bool {
  120. auto [constraint_node_id, constraint_inst_id] =
  121. context.node_stack().PopExprWithNodeId();
  122. auto [self_node_id, self_inst_id] =
  123. context.node_stack().PopWithNodeId<Parse::NodeCategory::RequireImpls>();
  124. [[maybe_unused]] auto decl_block_id = context.inst_block_stack().Pop();
  125. // Process modifiers.
  126. auto introducer =
  127. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Require>();
  128. LimitModifiersOnDecl(context, introducer, KeywordModifierSet::Extend);
  129. auto scope_inst_id =
  130. context.node_stack().Pop<Parse::NodeKind::RequireIntroducer>();
  131. auto constraint_constant_value_inst_id =
  132. context.constant_values().GetConstantInstId(constraint_inst_id);
  133. auto constraint_facet_type = context.insts().TryGetAs<SemIR::FacetType>(
  134. constraint_constant_value_inst_id);
  135. if (!constraint_facet_type) {
  136. if (constraint_constant_value_inst_id != SemIR::ErrorInst::InstId) {
  137. CARBON_DIAGNOSTIC(
  138. RequireImplsMissingFacetType, Error,
  139. "`require` declaration constrained by a non-facet type; "
  140. "expected an `interface` or `constraint` name after `impls`");
  141. context.emitter().Emit(constraint_node_id, RequireImplsMissingFacetType);
  142. }
  143. // Can't continue without a constraint to use.
  144. return true;
  145. }
  146. auto identified_facet_type_id =
  147. RequireIdentifiedFacetType(context, *constraint_facet_type);
  148. const auto& identified =
  149. context.identified_facet_types().Get(identified_facet_type_id);
  150. if (!TypeStructureReferencesSelf(context, self_inst_id, identified)) {
  151. CARBON_DIAGNOSTIC(RequireImplsMissingSelf, Error,
  152. "no `Self` reference found in `require` declaration; "
  153. "`Self` must appear in the self-type or as a generic "
  154. "parameter for each `interface` or `constraint`");
  155. context.emitter().Emit(node_id, RequireImplsMissingSelf);
  156. return true;
  157. }
  158. if (scope_inst_id == SemIR::ErrorInst::InstId) {
  159. // `require` is in the wrong scope.
  160. return true;
  161. }
  162. if (identified.required_interfaces().empty()) {
  163. // A `require T impls type` adds no actual constraints.
  164. return true;
  165. }
  166. // TODO: Add the `require` constraint to the InterfaceDecl or ConstraintDecl
  167. // from `scope_inst_id`.
  168. return true;
  169. }
  170. } // namespace Carbon::Check