semantics_handle_if_expression.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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::Check {
  6. auto HandleIfExpressionIf(Context& context, ParseTree::Node if_node) -> bool {
  7. auto cond_value_id = context.node_stack().PopExpression();
  8. context.node_stack().Push(if_node);
  9. // Convert the condition to `bool`, and branch on it.
  10. cond_value_id = context.ConvertToBoolValue(if_node, cond_value_id);
  11. auto then_block_id =
  12. context.AddDominatedBlockAndBranchIf(if_node, cond_value_id);
  13. auto else_block_id = context.AddDominatedBlockAndBranch(if_node);
  14. // Push the `else` block and `then` block, and start emitting the `then`.
  15. context.node_block_stack().Pop();
  16. context.node_block_stack().Push(else_block_id);
  17. context.node_block_stack().Push(then_block_id);
  18. context.AddCurrentCodeBlockToFunction();
  19. return true;
  20. }
  21. auto HandleIfExpressionThen(Context& context, ParseTree::Node then_node)
  22. -> bool {
  23. // Convert the first operand to a value.
  24. auto [then_value_node, then_value_id] =
  25. context.node_stack().PopExpressionWithParseNode();
  26. context.node_stack().Push(then_value_node,
  27. context.ConvertToValueExpression(then_value_id));
  28. context.node_stack().Push(then_node, context.node_block_stack().Pop());
  29. context.AddCurrentCodeBlockToFunction();
  30. return true;
  31. }
  32. auto HandleIfExpressionElse(Context& context, ParseTree::Node else_node)
  33. -> bool {
  34. auto else_value_id = context.node_stack().PopExpression();
  35. auto [then_node, then_end_block_id] =
  36. context.node_stack().PopWithParseNode<ParseNodeKind::IfExpressionThen>();
  37. auto then_value_id = context.node_stack().PopExpression();
  38. auto if_node =
  39. context.node_stack().PopForSoloParseNode<ParseNodeKind::IfExpressionIf>();
  40. // Convert the `else` value to the `then` value's type, and finish the `else`
  41. // block.
  42. // TODO: Find a common type, and convert both operands to it instead.
  43. auto result_type_id = context.semantics_ir().GetNode(then_value_id).type_id();
  44. else_value_id =
  45. context.ConvertToValueOfType(else_node, else_value_id, result_type_id);
  46. auto else_end_block_id = context.node_block_stack().Pop();
  47. // Create a resumption block and branches to it.
  48. auto chosen_value_id = context.AddConvergenceBlockWithArgAndPush(
  49. if_node,
  50. {{then_end_block_id, then_value_id}, {else_end_block_id, else_value_id}});
  51. context.AddCurrentCodeBlockToFunction();
  52. // Push the result value.
  53. context.node_stack().Push(else_node, chosen_value_id);
  54. return true;
  55. }
  56. } // namespace Carbon::Check