handle_if_expr.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 HandleIfExprIf(Context& context, Parse::IfExprIfId node_id) -> bool {
  8. // Alias node_id for if/then/else consistency.
  9. auto& if_node = node_id;
  10. auto [cond_node, cond_value_id] = context.node_stack().PopExprWithNodeId();
  11. // Convert the condition to `bool`, and branch on it.
  12. cond_value_id = ConvertToBoolValue(context, if_node, cond_value_id);
  13. context.node_stack().Push(cond_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. // Start emitting the `then` block.
  18. context.inst_block_stack().Pop();
  19. context.inst_block_stack().Push(then_block_id);
  20. context.AddCurrentCodeBlockToFunction(node_id);
  21. context.node_stack().Push(if_node, else_block_id);
  22. return true;
  23. }
  24. auto HandleIfExprThen(Context& context, Parse::IfExprThenId node_id) -> bool {
  25. auto then_value_id = context.node_stack().PopExpr();
  26. auto else_block_id = context.node_stack().Peek<Parse::NodeKind::IfExprIf>();
  27. // Convert the first operand to a value.
  28. then_value_id = ConvertToValueExpr(context, then_value_id);
  29. // Start emitting the `else` block.
  30. context.inst_block_stack().Push(else_block_id);
  31. context.AddCurrentCodeBlockToFunction(node_id);
  32. context.node_stack().Push(node_id, then_value_id);
  33. return true;
  34. }
  35. auto HandleIfExprElse(Context& context, Parse::IfExprElseId node_id) -> bool {
  36. // Alias node_id for if/then/else consistency.
  37. auto& else_node = node_id;
  38. auto else_value_id = context.node_stack().PopExpr();
  39. auto then_value_id = context.node_stack().Pop<Parse::NodeKind::IfExprThen>();
  40. auto [if_node, _] =
  41. context.node_stack().PopWithNodeId<Parse::NodeKind::IfExprIf>();
  42. auto cond_value_id = context.node_stack().PopExpr();
  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.insts().Get(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.SetBlockArgResultBeforeConstantUse(chosen_value_id, cond_value_id,
  53. then_value_id, else_value_id);
  54. context.AddCurrentCodeBlockToFunction(node_id);
  55. // Push the result value.
  56. context.node_stack().Push(else_node, chosen_value_id);
  57. return true;
  58. }
  59. } // namespace Carbon::Check