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/convert.h"
  6. #include "toolchain/sem_ir/inst.h"
  7. namespace Carbon::Check {
  8. auto HandleIfConditionStart(Context& /*context*/,
  9. Parse::IfConditionStartId /*parse_node*/) -> bool {
  10. return true;
  11. }
  12. auto HandleIfCondition(Context& context, Parse::IfConditionId parse_node)
  13. -> bool {
  14. // Convert the condition to `bool`.
  15. auto cond_value_id = context.node_stack().PopExpr();
  16. cond_value_id = ConvertToBoolValue(context, parse_node, 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. context.AddDominatedBlockAndBranchIf(parse_node, cond_value_id);
  22. auto else_block_id = context.AddDominatedBlockAndBranch(parse_node);
  23. // Start emitting the `then` block.
  24. context.inst_block_stack().Pop();
  25. context.inst_block_stack().Push(then_block_id);
  26. context.AddCurrentCodeBlockToFunction();
  27. context.node_stack().Push(parse_node, else_block_id);
  28. return true;
  29. }
  30. auto HandleIfStatementElse(Context& context,
  31. Parse::IfStatementElseId parse_node) -> 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.AddCurrentCodeBlockToFunction();
  36. context.node_stack().Push(parse_node);
  37. return true;
  38. }
  39. auto HandleIfStatement(Context& context, Parse::IfStatementId parse_node)
  40. -> bool {
  41. switch (auto kind = context.node_stack().PeekParseNodeKind()) {
  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. context.AddInst(SemIR::Branch{parse_node, else_block_id});
  48. context.inst_block_stack().Pop();
  49. context.inst_block_stack().Push(else_block_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. .PopAndDiscardSoloParseNode<Parse::NodeKind::IfStatementElse>();
  56. context.AddConvergenceBlockAndPush(parse_node, /*num_blocks=*/2);
  57. break;
  58. }
  59. default: {
  60. CARBON_FATAL() << "Unexpected parse node at start of `if`: " << kind;
  61. }
  62. }
  63. context.AddCurrentCodeBlockToFunction();
  64. return true;
  65. }
  66. } // namespace Carbon::Check