handle_where.cpp 6.3 KB

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