handle_if_expr.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #include "toolchain/check/literal.h"
  8. namespace Carbon::Check {
  9. auto HandleParseNode(Context& context, Parse::IfExprIfId node_id) -> bool {
  10. // Alias node_id for if/then/else consistency.
  11. auto& if_node = node_id;
  12. auto [cond_node, cond_value_id] = context.node_stack().PopExprWithNodeId();
  13. // Convert the condition to `bool`, and branch on it.
  14. cond_value_id = ConvertToBoolValue(context, if_node, cond_value_id);
  15. context.node_stack().Push(cond_node, cond_value_id);
  16. auto then_block_id =
  17. context.AddDominatedBlockAndBranchIf(if_node, cond_value_id);
  18. auto else_block_id = context.AddDominatedBlockAndBranch(if_node);
  19. // Start emitting the `then` block.
  20. context.inst_block_stack().Pop();
  21. context.inst_block_stack().Push(then_block_id);
  22. context.AddToRegion(then_block_id, node_id);
  23. context.node_stack().Push(if_node, else_block_id);
  24. return true;
  25. }
  26. // If the operand is an `IntLiteral`, convert it to a suitably-sized `Int` type.
  27. // TODO: For now we always pick `i32`.
  28. static auto DecayIntLiteralToSizedInt(Context& context, Parse::NodeId node_id,
  29. SemIR::InstId operand_id)
  30. -> SemIR::InstId {
  31. if (context.types().GetInstId(context.insts().Get(operand_id).type_id()) ==
  32. SemIR::IntLiteralType::SingletonInstId) {
  33. operand_id = ConvertToValueOfType(
  34. context, node_id, operand_id,
  35. MakeIntType(context, node_id, SemIR::IntKind::Signed,
  36. context.ints().Add(32)));
  37. }
  38. return operand_id;
  39. }
  40. auto HandleParseNode(Context& context, Parse::IfExprThenId node_id) -> bool {
  41. auto then_value_id = context.node_stack().PopExpr();
  42. auto else_block_id = context.node_stack().Peek<Parse::NodeKind::IfExprIf>();
  43. // Convert the first operand to a value.
  44. then_value_id = ConvertToValueExpr(context, then_value_id);
  45. then_value_id = DecayIntLiteralToSizedInt(context, node_id, then_value_id);
  46. // Start emitting the `else` block.
  47. context.inst_block_stack().Push(else_block_id);
  48. context.AddToRegion(else_block_id, node_id);
  49. context.node_stack().Push(node_id, then_value_id);
  50. return true;
  51. }
  52. auto HandleParseNode(Context& context, Parse::IfExprElseId node_id) -> bool {
  53. if (context.return_scope_stack().empty()) {
  54. context.TODO(node_id,
  55. "Control flow expressions are currently only supported inside "
  56. "functions.");
  57. }
  58. // Alias node_id for if/then/else consistency.
  59. auto& else_node = node_id;
  60. auto else_value_id = context.node_stack().PopExpr();
  61. auto then_value_id = context.node_stack().Pop<Parse::NodeKind::IfExprThen>();
  62. auto [if_node, _] =
  63. context.node_stack().PopWithNodeId<Parse::NodeKind::IfExprIf>();
  64. auto cond_value_id = context.node_stack().PopExpr();
  65. // Convert the `else` value to the `then` value's type, and finish the `else`
  66. // block.
  67. // TODO: Find a common type, and convert both operands to it instead.
  68. auto result_type_id = context.insts().Get(then_value_id).type_id();
  69. else_value_id =
  70. ConvertToValueOfType(context, else_node, else_value_id, result_type_id);
  71. // Create a resumption block and branches to it.
  72. auto chosen_value_id = context.AddConvergenceBlockWithArgAndPush(
  73. if_node, {else_value_id, then_value_id});
  74. context.SetBlockArgResultBeforeConstantUse(chosen_value_id, cond_value_id,
  75. then_value_id, else_value_id);
  76. // Push the result value.
  77. context.node_stack().Push(else_node, chosen_value_id);
  78. return true;
  79. }
  80. } // namespace Carbon::Check