handle_where.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 (!context.IsFacetType(self_type_id)) {
  17. CARBON_DIAGNOSTIC(WhereOnNonFacetType, Error,
  18. "left argument of `where` operator must be a facet type");
  19. context.emitter().Emit(self_node, WhereOnNonFacetType);
  20. self_type_id = SemIR::TypeId::Error;
  21. }
  22. // Introduce a name scope so that we can remove the `.Self` entry we are
  23. // adding to name lookup at the end of the `where` expression.
  24. context.scope_stack().Push();
  25. // Create a generic region containing `.Self` and the constraints.
  26. StartGenericDecl(context);
  27. // Introduce `.Self` as a symbolic binding. Its type is the value of the
  28. // expression to the left of `where`, so `MyInterface` in the example above.
  29. // Because there is no equivalent non-symbolic value, we use `Invalid` as
  30. // the `value_id` on the `BindSymbolicName`.
  31. auto entity_name_id = context.entity_names().Add(
  32. {.name_id = SemIR::NameId::PeriodSelf,
  33. .parent_scope_id = context.scope_stack().PeekNameScopeId(),
  34. .bind_index = context.scope_stack().AddCompileTimeBinding()});
  35. auto inst_id =
  36. context.AddInst(SemIR::LocIdAndInst::NoLoc<SemIR::BindSymbolicName>(
  37. {.type_id = self_type_id,
  38. .entity_name_id = entity_name_id,
  39. .value_id = SemIR::InstId::Invalid}));
  40. context.scope_stack().PushCompileTimeBinding(inst_id);
  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.is_valid());
  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 = context.node_stack().PopExpr();
  58. // Convert rhs to type of lhs.
  59. SemIR::InstId rhs_inst_id = ConvertToValueOfType(
  60. context, rhs_node, rhs_id, context.insts().Get(lhs).type_id());
  61. // Build up the list of arguments for the `WhereExpr` inst.
  62. context.args_type_info_stack().AddInstId(
  63. context.AddInstInNoBlock<SemIR::RequirementRewrite>(
  64. node_id, {.lhs_id = lhs, .rhs_id = rhs_inst_id}));
  65. return true;
  66. }
  67. auto HandleParseNode(Context& context, Parse::RequirementEqualEqualId node_id)
  68. -> bool {
  69. auto rhs = context.node_stack().PopExpr();
  70. auto lhs = context.node_stack().PopExpr();
  71. // TODO: type check lhs and rhs are comparable
  72. // TODO: require that at least one side uses a designator
  73. // Build up the list of arguments for the `WhereExpr` inst.
  74. context.args_type_info_stack().AddInstId(
  75. context.AddInstInNoBlock<SemIR::RequirementEquivalent>(
  76. node_id, {.lhs_id = lhs, .rhs_id = rhs}));
  77. return true;
  78. }
  79. auto HandleParseNode(Context& context, Parse::RequirementImplsId node_id)
  80. -> bool {
  81. auto [rhs_node, rhs_id] = context.node_stack().PopExprWithNodeId();
  82. auto [lhs_node, lhs_id] = context.node_stack().PopExprWithNodeId();
  83. // Check lhs is a facet and rhs is a facet type.
  84. auto lhs_as_type = ExprAsType(context, lhs_node, lhs_id);
  85. auto rhs_as_type = ExprAsType(context, rhs_node, rhs_id);
  86. if (rhs_as_type.type_id != SemIR::TypeId::Error &&
  87. !context.IsFacetType(rhs_as_type.type_id)) {
  88. CARBON_DIAGNOSTIC(
  89. ImplsOnNonFacetType, Error,
  90. "right argument of `impls` requirement must be a facet type");
  91. context.emitter().Emit(rhs_node, ImplsOnNonFacetType);
  92. rhs_as_type.inst_id = SemIR::InstId::BuiltinError;
  93. }
  94. // TODO: require that at least one side uses a designator
  95. // Build up the list of arguments for the `WhereExpr` inst.
  96. context.args_type_info_stack().AddInstId(
  97. context.AddInstInNoBlock<SemIR::RequirementImpls>(
  98. node_id,
  99. {.lhs_id = lhs_as_type.inst_id, .rhs_id = rhs_as_type.inst_id}));
  100. return true;
  101. }
  102. auto HandleParseNode(Context& /*context*/, Parse::RequirementAndId /*node_id*/)
  103. -> bool {
  104. // Nothing to do.
  105. return true;
  106. }
  107. auto HandleParseNode(Context& context, Parse::WhereExprId node_id) -> bool {
  108. // Discard the generic region containing `.Self` and the constraints.
  109. // TODO: Decide if we want to build a `Generic` object for this.
  110. DiscardGenericDecl(context);
  111. // Remove `PeriodSelf` from name lookup, undoing the `Push` done for the
  112. // `WhereOperand`.
  113. context.scope_stack().Pop();
  114. SemIR::InstId period_self_id =
  115. context.node_stack().Pop<Parse::NodeKind::WhereOperand>();
  116. SemIR::InstBlockId requirements_id = context.args_type_info_stack().Pop();
  117. context.AddInstAndPush<SemIR::WhereExpr>(
  118. node_id, {.type_id = SemIR::TypeId::TypeType,
  119. .period_self_id = period_self_id,
  120. .requirements_id = requirements_id});
  121. return true;
  122. }
  123. } // namespace Carbon::Check