semantics_handle_if.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 else block and the then 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 if_block_id = context.node_block_stack().PopForAdd();
  20. auto else_block_id = context.node_block_stack().PushForAdd();
  21. auto then_block_id = context.node_block_stack().PushForAdd();
  22. // Branch to the appropriate block.
  23. context.AddNodeToBlock(
  24. if_block_id,
  25. SemanticsNode::BranchIf::Make(parse_node, then_block_id, cond_value_id));
  26. context.AddNodeToBlock(
  27. if_block_id, SemanticsNode::Branch::Make(parse_node, else_block_id));
  28. context.node_stack().Push(parse_node);
  29. return true;
  30. }
  31. auto SemanticsHandleIfStatementElse(SemanticsContext& context,
  32. ParseTree::Node parse_node) -> bool {
  33. context.node_stack().PopAndDiscardSoloParseNode(ParseNodeKind::IfCondition);
  34. // Switch to emitting the else block.
  35. auto then_block_id = context.node_block_stack().PopForAdd();
  36. context.node_stack().Push(parse_node, then_block_id);
  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. auto resume_block_id = context.node_block_stack().PushForAdd();
  61. context.AddNodeToBlock(then_block_id, SemanticsNode::Branch::Make(
  62. parse_node, resume_block_id));
  63. context.AddNodeToBlock(sub_block_id, SemanticsNode::Branch::Make(
  64. parse_node, resume_block_id));
  65. break;
  66. }
  67. default: {
  68. CARBON_FATAL() << "Unexpected parse node at start of `if`: " << kind;
  69. }
  70. }
  71. return true;
  72. }
  73. } // namespace Carbon