handle_where.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/convert.h"
  6. #include "toolchain/check/generic.h"
  7. #include "toolchain/check/handle.h"
  8. #include "toolchain/check/inst.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. #include "toolchain/sem_ir/typed_insts.h"
  11. namespace Carbon::Check {
  12. auto HandleParseNode(Context& context, Parse::WhereOperandId node_id) -> bool {
  13. // The expression at the top of the stack represents a constraint type that
  14. // is being modified by the `where` operator. It would be `MyInterface` in
  15. // `MyInterface where .Member = i32`.
  16. auto [self_node, self_id] = context.node_stack().PopExprWithNodeId();
  17. auto self_type_id = ExprAsType(context, self_node, self_id).type_id;
  18. // Only facet types may have `where` restrictions.
  19. if (self_type_id != SemIR::ErrorInst::TypeId &&
  20. !context.types().IsFacetType(self_type_id)) {
  21. CARBON_DIAGNOSTIC(WhereOnNonFacetType, Error,
  22. "left argument of `where` operator must be a facet type");
  23. context.emitter().Emit(self_node, WhereOnNonFacetType);
  24. self_type_id = SemIR::ErrorInst::TypeId;
  25. }
  26. // Introduce a name scope so that we can remove the `.Self` entry we are
  27. // adding to name lookup at the end of the `where` expression.
  28. context.scope_stack().PushForSameRegion();
  29. // Introduce `.Self` as a symbolic binding. Its type is the value of the
  30. // expression to the left of `where`, so `MyInterface` in the example above.
  31. auto entity_name_id = context.entity_names().AddCanonical(
  32. {.name_id = SemIR::NameId::PeriodSelf,
  33. .parent_scope_id = context.scope_stack().PeekNameScopeId()});
  34. auto inst_id = AddInst(
  35. context, SemIR::LocIdAndInst::NoLoc<SemIR::BindSymbolicName>({
  36. .type_id = self_type_id,
  37. .entity_name_id = entity_name_id,
  38. // `None` because there is no equivalent non-symbolic value.
  39. .value_id = SemIR::InstId::None,
  40. }));
  41. auto existing =
  42. context.scope_stack().LookupOrAddName(SemIR::NameId::PeriodSelf, inst_id);
  43. // Shouldn't have any names in newly created scope.
  44. CARBON_CHECK(!existing.has_value());
  45. // Save the `.Self` symbolic binding on the node stack. It will become the
  46. // first argument to the `WhereExpr` instruction.
  47. context.node_stack().Push(node_id, inst_id);
  48. // Going to put each requirement on `args_type_info_stack`, so we can have an
  49. // inst block with the varying number of requirements but keeping other
  50. // instructions on the current inst block from the `inst_block_stack()`.
  51. context.args_type_info_stack().Push();
  52. return true;
  53. }
  54. auto HandleParseNode(Context& context, Parse::RequirementEqualId node_id)
  55. -> bool {
  56. auto [rhs_node, rhs_id] = context.node_stack().PopExprWithNodeId();
  57. auto lhs_id = context.node_stack().PopExpr();
  58. // Convert rhs to type of lhs.
  59. auto lhs_type_id = context.insts().Get(lhs_id).type_id();
  60. if (lhs_type_id.is_symbolic()) {
  61. // If the type of the associated constant is symbolic, we defer conversion
  62. // until the constraint is resolved, in case it depends on `Self` (which
  63. // will now be a reference to `.Self`).
  64. // For now we convert to a value expression eagerly because otherwise we'll
  65. // often be unable to constant-evaluate the enclosing `where` expression.
  66. // TODO: Perform the conversion symbolically and add an implicit constraint
  67. // that this conversion is valid and produces a constant.
  68. rhs_id = ConvertToValueExpr(context, rhs_id);
  69. } else {
  70. rhs_id = ConvertToValueOfType(context, rhs_node, rhs_id,
  71. context.insts().Get(lhs_id).type_id());
  72. }
  73. // Build up the list of arguments for the `WhereExpr` inst.
  74. context.args_type_info_stack().AddInstId(
  75. AddInstInNoBlock<SemIR::RequirementRewrite>(
  76. context, node_id, {.lhs_id = lhs_id, .rhs_id = rhs_id}));
  77. return true;
  78. }
  79. auto HandleParseNode(Context& context, Parse::RequirementEqualEqualId node_id)
  80. -> bool {
  81. auto rhs = context.node_stack().PopExpr();
  82. auto lhs = context.node_stack().PopExpr();
  83. // TODO: Type check lhs and rhs are comparable.
  84. // TODO: Require that at least one side uses a designator.
  85. // Build up the list of arguments for the `WhereExpr` inst.
  86. context.args_type_info_stack().AddInstId(
  87. AddInstInNoBlock<SemIR::RequirementEquivalent>(
  88. context, node_id, {.lhs_id = lhs, .rhs_id = rhs}));
  89. return true;
  90. }
  91. auto HandleParseNode(Context& context, Parse::RequirementImplsId node_id)
  92. -> bool {
  93. auto [rhs_node, rhs_id] = context.node_stack().PopExprWithNodeId();
  94. auto [lhs_node, lhs_id] = context.node_stack().PopExprWithNodeId();
  95. // Check lhs is a facet and rhs is a facet type.
  96. auto lhs_as_type = ExprAsType(context, lhs_node, lhs_id);
  97. auto rhs_as_type = ExprAsType(context, rhs_node, rhs_id);
  98. if (rhs_as_type.type_id != SemIR::ErrorInst::TypeId &&
  99. !context.types().IsFacetType(rhs_as_type.type_id)) {
  100. CARBON_DIAGNOSTIC(
  101. ImplsOnNonFacetType, Error,
  102. "right argument of `impls` requirement must be a facet type");
  103. context.emitter().Emit(rhs_node, ImplsOnNonFacetType);
  104. rhs_as_type.inst_id = SemIR::ErrorInst::TypeInstId;
  105. }
  106. // TODO: Require that at least one side uses a designator.
  107. // TODO: For things like `HashSet(.T) as type`, add an implied constraint
  108. // that `.T impls Hash`.
  109. // Build up the list of arguments for the `WhereExpr` inst.
  110. context.args_type_info_stack().AddInstId(
  111. AddInstInNoBlock<SemIR::RequirementImpls>(
  112. context, node_id,
  113. {.lhs_id = lhs_as_type.inst_id, .rhs_id = rhs_as_type.inst_id}));
  114. return true;
  115. }
  116. auto HandleParseNode(Context& /*context*/, Parse::RequirementAndId /*node_id*/)
  117. -> bool {
  118. // Nothing to do.
  119. return true;
  120. }
  121. auto HandleParseNode(Context& context, Parse::WhereExprId node_id) -> bool {
  122. // Remove `PeriodSelf` from name lookup, undoing the `Push` done for the
  123. // `WhereOperand`.
  124. context.scope_stack().Pop();
  125. SemIR::InstId period_self_id =
  126. context.node_stack().Pop<Parse::NodeKind::WhereOperand>();
  127. SemIR::InstBlockId requirements_id = context.args_type_info_stack().Pop();
  128. AddInstAndPush<SemIR::WhereExpr>(context, node_id,
  129. {.type_id = SemIR::TypeType::TypeId,
  130. .period_self_id = period_self_id,
  131. .requirements_id = requirements_id});
  132. return true;
  133. }
  134. } // namespace Carbon::Check