handle_where.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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);
  15. // TODO: Validate that `self_type_id` represents a facet type. Only facet
  16. // types may have `where` restrictions.
  17. // Introduce a name scope so that we can remove the `.Self` entry we are
  18. // adding to name lookup at the end of the `where` expression.
  19. context.scope_stack().Push();
  20. // Create a generic region containing `.Self` and the constraints.
  21. StartGenericDecl(context);
  22. // Introduce `.Self` as a symbolic binding. Its type is the value of the
  23. // expression to the left of `where`, so `MyInterface` in the example above.
  24. // Because there is no equivalent non-symbolic value, we use `Invalid` as
  25. // the `value_id` on the `BindSymbolicName`.
  26. auto entity_name_id = context.entity_names().Add(
  27. {.name_id = SemIR::NameId::PeriodSelf,
  28. .parent_scope_id = context.decl_name_stack().PeekParentScopeId(),
  29. .bind_index = context.scope_stack().AddCompileTimeBinding()});
  30. auto inst_id =
  31. context.AddInst(SemIR::LocIdAndInst::NoLoc<SemIR::BindSymbolicName>(
  32. {.type_id = self_type_id,
  33. .entity_name_id = entity_name_id,
  34. .value_id = SemIR::InstId::Invalid}));
  35. context.scope_stack().PushCompileTimeBinding(inst_id);
  36. auto existing =
  37. context.scope_stack().LookupOrAddName(SemIR::NameId::PeriodSelf, inst_id);
  38. // Shouldn't have any names in newly created scope.
  39. CARBON_CHECK(!existing.is_valid());
  40. // Save the `.Self` symbolic binding on the node stack. It will become the
  41. // first argument to the `WhereExpr` instruction.
  42. context.node_stack().Push(node_id, inst_id);
  43. // Going to put each requirement on `args_type_info_stack`, so we can have an
  44. // inst block with the varying number of requirements but keeping other
  45. // instructions on the current inst block from the `inst_block_stack()`.
  46. context.args_type_info_stack().Push();
  47. return true;
  48. }
  49. auto HandleParseNode(Context& context, Parse::RequirementEqualId node_id)
  50. -> bool {
  51. auto rhs = context.node_stack().PopExpr();
  52. auto lhs = context.node_stack().PopExpr();
  53. // TODO: convert rhs to type of lhs
  54. // Build up the list of arguments for the `WhereExpr` inst.
  55. context.args_type_info_stack().AddInstId(
  56. context.AddInstInNoBlock<SemIR::RequirementRewrite>(
  57. node_id, {.lhs_id = lhs, .rhs_id = rhs}));
  58. return true;
  59. }
  60. auto HandleParseNode(Context& context, Parse::RequirementEqualEqualId node_id)
  61. -> bool {
  62. auto rhs = context.node_stack().PopExpr();
  63. auto lhs = context.node_stack().PopExpr();
  64. // TODO: type check lhs and rhs are compatible
  65. // Build up the list of arguments for the `WhereExpr` inst.
  66. context.args_type_info_stack().AddInstId(
  67. context.AddInstInNoBlock<SemIR::RequirementEquivalent>(
  68. node_id, {.lhs_id = lhs, .rhs_id = rhs}));
  69. return true;
  70. }
  71. auto HandleParseNode(Context& context, Parse::RequirementImplsId node_id)
  72. -> bool {
  73. auto rhs = context.node_stack().PopExpr();
  74. auto lhs = context.node_stack().PopExpr();
  75. // TODO: check lhs is a facet and rhs is a facet type
  76. // Build up the list of arguments for the `WhereExpr` inst.
  77. context.args_type_info_stack().AddInstId(
  78. context.AddInstInNoBlock<SemIR::RequirementImpls>(
  79. node_id, {.lhs_id = lhs, .rhs_id = rhs}));
  80. return true;
  81. }
  82. auto HandleParseNode(Context& /*context*/, Parse::RequirementAndId /*node_id*/)
  83. -> bool {
  84. // Nothing to do.
  85. return true;
  86. }
  87. auto HandleParseNode(Context& context, Parse::WhereExprId node_id) -> bool {
  88. // Discard the generic region containing `.Self` and the constraints.
  89. // TODO: Decide if we want to build a `Generic` object for this.
  90. DiscardGenericDecl(context);
  91. // Remove `PeriodSelf` from name lookup, undoing the `Push` done for the
  92. // `WhereOperand`.
  93. context.scope_stack().Pop();
  94. SemIR::InstId period_self_id =
  95. context.node_stack().Pop<Parse::NodeKind::WhereOperand>();
  96. SemIR::InstBlockId requirements_id = context.args_type_info_stack().Pop();
  97. context.AddInstAndPush<SemIR::WhereExpr>(
  98. node_id, {.type_id = SemIR::TypeId::TypeType,
  99. .period_self_id = period_self_id,
  100. .requirements_id = requirements_id});
  101. return true;
  102. }
  103. } // namespace Carbon::Check