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