handle_if_expr.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_value_id = context.node_stack().PopExpr();
  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.inst_block_stack().Pop();
  18. context.inst_block_stack().Push(then_block_id);
  19. context.AddCurrentCodeBlockToFunction(node_id);
  20. context.node_stack().Push(if_node, else_block_id);
  21. return true;
  22. }
  23. auto HandleIfExprThen(Context& context, Parse::IfExprThenId node_id) -> bool {
  24. auto then_value_id = context.node_stack().PopExpr();
  25. auto else_block_id = context.node_stack().Peek<Parse::NodeKind::IfExprIf>();
  26. // Convert the first operand to a value.
  27. then_value_id = ConvertToValueExpr(context, then_value_id);
  28. // Start emitting the `else` block.
  29. context.inst_block_stack().Push(else_block_id);
  30. context.AddCurrentCodeBlockToFunction(node_id);
  31. context.node_stack().Push(node_id, then_value_id);
  32. return true;
  33. }
  34. auto HandleIfExprElse(Context& context, Parse::IfExprElseId node_id) -> bool {
  35. // Alias node_id for if/then/else consistency.
  36. auto& else_node = node_id;
  37. auto else_value_id = context.node_stack().PopExpr();
  38. auto then_value_id = context.node_stack().Pop<Parse::NodeKind::IfExprThen>();
  39. auto [if_node, _] =
  40. context.node_stack().PopWithNodeId<Parse::NodeKind::IfExprIf>();
  41. // Convert the `else` value to the `then` value's type, and finish the `else`
  42. // block.
  43. // TODO: Find a common type, and convert both operands to it instead.
  44. auto result_type_id = context.insts().Get(then_value_id).type_id();
  45. else_value_id =
  46. ConvertToValueOfType(context, else_node, else_value_id, result_type_id);
  47. // Create a resumption block and branches to it.
  48. auto chosen_value_id = context.AddConvergenceBlockWithArgAndPush(
  49. if_node, {else_value_id, then_value_id});
  50. context.AddCurrentCodeBlockToFunction(node_id);
  51. // Push the result value.
  52. context.node_stack().Push(else_node, chosen_value_id);
  53. return true;
  54. }
  55. } // namespace Carbon::Check