handle_if_statement.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/control_flow.h"
  6. #include "toolchain/check/convert.h"
  7. #include "toolchain/check/handle.h"
  8. #include "toolchain/check/inst.h"
  9. namespace Carbon::Check {
  10. auto HandleParseNode(Context& /*context*/,
  11. Parse::IfConditionStartId /*node_id*/) -> bool {
  12. return true;
  13. }
  14. auto HandleParseNode(Context& context, Parse::IfConditionId node_id) -> bool {
  15. // Convert the condition to `bool`.
  16. auto cond_value_id = context.node_stack().PopExpr();
  17. cond_value_id = ConvertToBoolValue(context, node_id, cond_value_id);
  18. // Create the then block and the else block, and branch to the right one. If
  19. // there is no `else`, the then block will terminate with a branch to the
  20. // else block, which will be reused as the resumption block.
  21. auto then_block_id =
  22. AddDominatedBlockAndBranchIf(context, node_id, cond_value_id);
  23. auto else_block_id = AddDominatedBlockAndBranch(context, node_id);
  24. // Start emitting the `then` block.
  25. context.inst_block_stack().Pop();
  26. context.inst_block_stack().Push(then_block_id);
  27. context.region_stack().AddToRegion(then_block_id, node_id);
  28. context.node_stack().Push(node_id, else_block_id);
  29. return true;
  30. }
  31. auto HandleParseNode(Context& context, Parse::IfStatementElseId node_id)
  32. -> bool {
  33. auto else_block_id = context.node_stack().Pop<Parse::NodeKind::IfCondition>();
  34. // Switch to emitting the `else` block.
  35. context.inst_block_stack().Push(else_block_id);
  36. context.region_stack().AddToRegion(else_block_id, node_id);
  37. context.node_stack().Push(node_id);
  38. return true;
  39. }
  40. auto HandleParseNode(Context& context, Parse::IfStatementId node_id) -> bool {
  41. switch (auto kind = context.node_stack().PeekNodeKind()) {
  42. case Parse::NodeKind::IfCondition: {
  43. // Branch from then block to else block, and start emitting the else
  44. // block.
  45. auto else_block_id =
  46. context.node_stack().Pop<Parse::NodeKind::IfCondition>();
  47. AddInst<SemIR::Branch>(context, node_id, {.target_id = else_block_id});
  48. context.inst_block_stack().Pop();
  49. context.inst_block_stack().Push(else_block_id);
  50. context.region_stack().AddToRegion(else_block_id, node_id);
  51. break;
  52. }
  53. case Parse::NodeKind::IfStatementElse: {
  54. // Branch from the then and else blocks to a new resumption block.
  55. context.node_stack()
  56. .PopAndDiscardSoloNodeId<Parse::NodeKind::IfStatementElse>();
  57. AddConvergenceBlockAndPush(context, node_id, /*num_blocks=*/2);
  58. break;
  59. }
  60. default: {
  61. CARBON_FATAL("Unexpected parse node at start of `if`: {0}", kind);
  62. }
  63. }
  64. return true;
  65. }
  66. } // namespace Carbon::Check