semantics_handle_if_expression.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. namespace Carbon {
  6. auto SemanticsHandleIfExpressionIf(SemanticsContext& context,
  7. ParseTree::Node if_node) -> bool {
  8. auto cond_value_id = context.node_stack().Pop<SemanticsNodeId>();
  9. context.node_stack().Push(if_node);
  10. // Convert the condition to `bool`.
  11. cond_value_id = context.ImplicitAsRequired(
  12. if_node, cond_value_id,
  13. context.CanonicalizeType(SemanticsNodeId::BuiltinBoolType));
  14. // Stop emitting the current block. We'll add some branch instructions to it
  15. // later, but we don't want it on the stack any more.
  16. auto if_block_id = context.node_block_stack().PeekForAdd();
  17. context.node_block_stack().Pop();
  18. // Create the resumption block, `else` block, and `then` block, and branches
  19. // to them.
  20. context.node_block_stack().Push();
  21. auto else_block_id = context.node_block_stack().PushForAdd();
  22. auto then_block_id = context.node_block_stack().PushForAdd();
  23. context.AddNodeToBlock(
  24. if_block_id,
  25. SemanticsNode::BranchIf::Make(if_node, then_block_id, cond_value_id));
  26. context.AddNodeToBlock(if_block_id,
  27. SemanticsNode::Branch::Make(if_node, else_block_id));
  28. return true;
  29. }
  30. auto SemanticsHandleIfExpressionThen(SemanticsContext& context,
  31. ParseTree::Node then_node) -> bool {
  32. context.node_stack().Push(then_node, context.node_block_stack().Pop());
  33. return true;
  34. }
  35. auto SemanticsHandleIfExpressionElse(SemanticsContext& context,
  36. ParseTree::Node else_node) -> bool {
  37. auto else_value_id = context.node_stack().Pop<SemanticsNodeId>();
  38. auto [then_node, then_end_block_id] =
  39. context.node_stack().PopWithParseNode<SemanticsNodeBlockId>(
  40. ParseNodeKind::IfExpressionThen);
  41. auto then_value_id = context.node_stack().Pop<SemanticsNodeId>();
  42. auto if_node =
  43. context.node_stack().PopForSoloParseNode(ParseNodeKind::IfExpressionIf);
  44. // Convert the `else` value to the `then` value's type, and finish the `else`
  45. // block.
  46. // TODO: Find a common type, and convert both operands to it instead.
  47. auto result_type_id = context.semantics_ir().GetNode(then_value_id).type_id();
  48. else_value_id =
  49. context.ImplicitAsRequired(else_node, else_value_id, result_type_id);
  50. auto else_end_block_id = context.node_block_stack().Pop();
  51. // Create branches to the resumption block.
  52. auto resume_block_id = context.node_block_stack().PeekForAdd();
  53. context.AddNodeToBlock(then_end_block_id,
  54. SemanticsNode::BranchWithArg::Make(
  55. then_node, resume_block_id, then_value_id));
  56. context.AddNodeToBlock(else_end_block_id,
  57. SemanticsNode::BranchWithArg::Make(
  58. else_node, resume_block_id, else_value_id));
  59. // Obtain the value in the resumption block and push it.
  60. context.AddNodeAndPush(
  61. if_node, SemanticsNode::BlockArg::Make(if_node, result_type_id));
  62. return true;
  63. }
  64. } // namespace Carbon