handle_if_expr.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/check/context.h"
  5. #include "toolchain/check/convert.h"
  6. #include "toolchain/check/handle.h"
  7. namespace Carbon::Check {
  8. auto HandleIfExprIf(Context& context, Parse::IfExprIfId node_id) -> bool {
  9. // Alias node_id for if/then/else consistency.
  10. auto& if_node = node_id;
  11. auto [cond_node, cond_value_id] = context.node_stack().PopExprWithNodeId();
  12. // Convert the condition to `bool`, and branch on it.
  13. cond_value_id = ConvertToBoolValue(context, if_node, cond_value_id);
  14. context.node_stack().Push(cond_node, cond_value_id);
  15. auto then_block_id =
  16. context.AddDominatedBlockAndBranchIf(if_node, cond_value_id);
  17. auto else_block_id = context.AddDominatedBlockAndBranch(if_node);
  18. // Start emitting the `then` block.
  19. context.inst_block_stack().Pop();
  20. context.inst_block_stack().Push(then_block_id);
  21. context.AddCurrentCodeBlockToFunction(node_id);
  22. context.node_stack().Push(if_node, else_block_id);
  23. return true;
  24. }
  25. auto HandleIfExprThen(Context& context, Parse::IfExprThenId node_id) -> bool {
  26. auto then_value_id = context.node_stack().PopExpr();
  27. auto else_block_id = context.node_stack().Peek<Parse::NodeKind::IfExprIf>();
  28. // Convert the first operand to a value.
  29. then_value_id = ConvertToValueExpr(context, then_value_id);
  30. // Start emitting the `else` block.
  31. context.inst_block_stack().Push(else_block_id);
  32. context.AddCurrentCodeBlockToFunction(node_id);
  33. context.node_stack().Push(node_id, then_value_id);
  34. return true;
  35. }
  36. auto HandleIfExprElse(Context& context, Parse::IfExprElseId node_id) -> bool {
  37. // Alias node_id for if/then/else consistency.
  38. auto& else_node = node_id;
  39. auto else_value_id = context.node_stack().PopExpr();
  40. auto then_value_id = context.node_stack().Pop<Parse::NodeKind::IfExprThen>();
  41. auto [if_node, _] =
  42. context.node_stack().PopWithNodeId<Parse::NodeKind::IfExprIf>();
  43. auto cond_value_id = context.node_stack().PopExpr();
  44. // Convert the `else` value to the `then` value's type, and finish the `else`
  45. // block.
  46. // TODO: Find a common type, and convert both operands to it instead.
  47. auto result_type_id = context.insts().Get(then_value_id).type_id();
  48. else_value_id =
  49. ConvertToValueOfType(context, else_node, else_value_id, result_type_id);
  50. // Create a resumption block and branches to it.
  51. auto chosen_value_id = context.AddConvergenceBlockWithArgAndPush(
  52. if_node, {else_value_id, then_value_id});
  53. context.SetBlockArgResultBeforeConstantUse(chosen_value_id, cond_value_id,
  54. then_value_id, else_value_id);
  55. context.AddCurrentCodeBlockToFunction(node_id);
  56. // Push the result value.
  57. context.node_stack().Push(else_node, chosen_value_id);
  58. return true;
  59. }
  60. } // namespace Carbon::Check