handle_if_statement.cpp 2.8 KB

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