semantics_handle_if_statement.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/semantics/semantics_context.h"
  5. #include "toolchain/semantics/semantics_node.h"
  6. namespace Carbon {
  7. auto SemanticsHandleIfConditionStart(SemanticsContext& /*context*/,
  8. ParseTree::Node /*parse_node*/) -> bool {
  9. return true;
  10. }
  11. auto SemanticsHandleIfCondition(SemanticsContext& context,
  12. ParseTree::Node parse_node) -> bool {
  13. // Convert the condition to `bool`.
  14. auto cond_value_id = context.node_stack().Pop<SemanticsNodeId>();
  15. cond_value_id = context.ImplicitAsBool(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. // Push the else and then blocks, and start emitting code in the then block.
  23. context.node_block_stack().Pop();
  24. context.node_block_stack().Push(else_block_id);
  25. context.node_block_stack().Push(then_block_id);
  26. context.AddCurrentCodeBlockToFunction();
  27. context.node_stack().Push(parse_node);
  28. return true;
  29. }
  30. auto SemanticsHandleIfStatementElse(SemanticsContext& context,
  31. ParseTree::Node parse_node) -> bool {
  32. context.node_stack().PopAndDiscardSoloParseNode(ParseNodeKind::IfCondition);
  33. // Switch to emitting the else block.
  34. auto then_block_id = context.node_block_stack().PopForAdd();
  35. context.node_stack().Push(parse_node, then_block_id);
  36. context.AddCurrentCodeBlockToFunction();
  37. return true;
  38. }
  39. auto SemanticsHandleIfStatement(SemanticsContext& context,
  40. ParseTree::Node parse_node) -> bool {
  41. // Either the then or else block, depending on whether there's an `else` node
  42. // on the top of the node stack.
  43. auto sub_block_id = context.node_block_stack().PopForAdd();
  44. switch (auto kind = context.parse_tree().node_kind(
  45. context.node_stack().PeekParseNode())) {
  46. case ParseNodeKind::IfCondition: {
  47. // Branch from then block to else block.
  48. context.node_stack().PopAndDiscardSoloParseNode(
  49. ParseNodeKind::IfCondition);
  50. context.AddNodeToBlock(
  51. sub_block_id,
  52. SemanticsNode::Branch::Make(parse_node,
  53. context.node_block_stack().PeekForAdd()));
  54. break;
  55. }
  56. case ParseNodeKind::IfStatementElse: {
  57. // Branch from the then and else blocks to a new resumption block.
  58. auto then_block_id = context.node_stack().Pop<SemanticsNodeBlockId>(
  59. ParseNodeKind::IfStatementElse);
  60. context.AddConvergenceBlockAndPush(parse_node,
  61. {then_block_id, sub_block_id});
  62. break;
  63. }
  64. default: {
  65. CARBON_FATAL() << "Unexpected parse node at start of `if`: " << kind;
  66. }
  67. }
  68. context.AddCurrentCodeBlockToFunction();
  69. return true;
  70. }
  71. } // namespace Carbon