handle_if_statement.cpp 2.7 KB

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