handle_if_statement.cpp 2.7 KB

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