handle_if_expression.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/check/context.h"
  5. #include "toolchain/check/convert.h"
  6. namespace Carbon::Check {
  7. auto HandleIfExpressionIf(Context& context, Parse::Node parse_node) -> 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. // Convert the condition to `bool`, and branch on it.
  12. cond_value_id = ConvertToBoolValue(context, 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. // Start emitting the `then` block.
  17. context.node_block_stack().Pop();
  18. context.node_block_stack().Push(then_block_id);
  19. context.AddCurrentCodeBlockToFunction();
  20. context.node_stack().Push(if_node, else_block_id);
  21. return true;
  22. }
  23. auto HandleIfExpressionThen(Context& context, Parse::Node parse_node) -> bool {
  24. auto then_value_id = context.node_stack().PopExpression();
  25. auto else_block_id =
  26. context.node_stack().Peek<Parse::NodeKind::IfExpressionIf>();
  27. // Convert the first operand to a value.
  28. then_value_id = ConvertToValueExpression(context, then_value_id);
  29. // Start emitting the `else` block.
  30. context.node_block_stack().Push(else_block_id);
  31. context.AddCurrentCodeBlockToFunction();
  32. context.node_stack().Push(parse_node, then_value_id);
  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_value_id =
  40. context.node_stack().Pop<Parse::NodeKind::IfExpressionThen>();
  41. auto [if_node, _] =
  42. context.node_stack().PopWithParseNode<Parse::NodeKind::IfExpressionIf>();
  43. // Convert the `else` value to the `then` value's type, and finish the `else`
  44. // block.
  45. // TODO: Find a common type, and convert both operands to it instead.
  46. auto result_type_id = context.semantics_ir().GetNode(then_value_id).type_id();
  47. else_value_id =
  48. ConvertToValueOfType(context, else_node, else_value_id, result_type_id);
  49. // Create a resumption block and branches to it.
  50. auto chosen_value_id = context.AddConvergenceBlockWithArgAndPush(
  51. if_node, {else_value_id, then_value_id});
  52. context.AddCurrentCodeBlockToFunction();
  53. // Push the result value.
  54. context.node_stack().Push(else_node, chosen_value_id);
  55. return true;
  56. }
  57. } // namespace Carbon::Check