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. #include "toolchain/sem_ir/inst.h"
  7. namespace Carbon::Check {
  8. auto HandleIfConditionStart(Context& /*context*/, Parse::NodeId /*parse_node*/)
  9. -> bool {
  10. return true;
  11. }
  12. auto HandleIfCondition(Context& context, Parse::NodeId parse_node) -> bool {
  13. // Convert the condition to `bool`.
  14. auto cond_value_id = context.node_stack().PopExpr();
  15. cond_value_id = ConvertToBoolValue(context, parse_node, 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(parse_node, cond_value_id);
  21. auto else_block_id = context.AddDominatedBlockAndBranch(parse_node);
  22. // Start emitting the `then` block.
  23. context.inst_block_stack().Pop();
  24. context.inst_block_stack().Push(then_block_id);
  25. context.AddCurrentCodeBlockToFunction();
  26. context.node_stack().Push(parse_node, else_block_id);
  27. return true;
  28. }
  29. auto HandleIfStatementElse(Context& context, Parse::NodeId parse_node) -> 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(parse_node);
  35. return true;
  36. }
  37. auto HandleIfStatement(Context& context, Parse::NodeId parse_node) -> bool {
  38. switch (auto kind = context.node_stack().PeekParseNodeKind()) {
  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(SemIR::Branch{parse_node, 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. .PopAndDiscardSoloParseNode<Parse::NodeKind::IfStatementElse>();
  53. context.AddConvergenceBlockAndPush(parse_node, /*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