semantics_handle_if_expression.cpp 3.1 KB

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