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/sem_ir/node.h"
  7. namespace Carbon::Check {
  8. auto HandleIfConditionStart(Context& /*context*/, Parse::Node /*parse_node*/)
  9. -> bool {
  10. return true;
  11. }
  12. auto HandleIfCondition(Context& context, Parse::Node parse_node) -> bool {
  13. // Convert the condition to `bool`.
  14. auto cond_value_id = context.node_stack().PopExpression();
  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.node_block_stack().Pop();
  24. context.node_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::Node parse_node) -> bool {
  30. auto else_block_id = context.node_stack().Pop<Parse::NodeKind::IfCondition>();
  31. // Switch to emitting the `else` block.
  32. context.node_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::Node parse_node) -> bool {
  38. switch (auto kind = context.parse_tree().node_kind(
  39. context.node_stack().PeekParseNode())) {
  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.AddNode(SemIR::Branch(parse_node, else_block_id));
  46. context.node_block_stack().Pop();
  47. context.node_block_stack().Push(else_block_id);
  48. break;
  49. }
  50. case Parse::NodeKind::IfStatementElse: {
  51. // Branch from the then and else blocks to a new resumption block.
  52. context.node_stack()
  53. .PopAndDiscardSoloParseNode<Parse::NodeKind::IfStatementElse>();
  54. context.AddConvergenceBlockAndPush(parse_node, /*num_blocks=*/2);
  55. break;
  56. }
  57. default: {
  58. CARBON_FATAL() << "Unexpected parse node at start of `if`: " << kind;
  59. }
  60. }
  61. context.AddCurrentCodeBlockToFunction();
  62. return true;
  63. }
  64. } // namespace Carbon::Check