semantics_handle_if_expression.cpp 2.9 KB

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