handle_if_statement.cpp 2.7 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/convert.h"
  6. namespace Carbon::Check {
  7. auto HandleIfConditionStart(Context& /*context*/,
  8. Parse::IfConditionStartId /*parse_node*/) -> bool {
  9. return true;
  10. }
  11. auto HandleIfCondition(Context& context, Parse::IfConditionId parse_node)
  12. -> 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,
  30. Parse::IfStatementElseId parse_node) -> 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.AddCurrentCodeBlockToFunction();
  35. context.node_stack().Push(parse_node);
  36. return true;
  37. }
  38. auto HandleIfStatement(Context& context, Parse::IfStatementId parse_node)
  39. -> bool {
  40. switch (auto kind = context.node_stack().PeekParseNodeKind()) {
  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({parse_node, SemIR::Branch{else_block_id}});
  47. context.inst_block_stack().Pop();
  48. context.inst_block_stack().Push(else_block_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. .PopAndDiscardSoloParseNode<Parse::NodeKind::IfStatementElse>();
  55. context.AddConvergenceBlockAndPush(parse_node, /*num_blocks=*/2);
  56. break;
  57. }
  58. default: {
  59. CARBON_FATAL() << "Unexpected parse node at start of `if`: " << kind;
  60. }
  61. }
  62. context.AddCurrentCodeBlockToFunction();
  63. return true;
  64. }
  65. } // namespace Carbon::Check