handle_if_expr.cpp 3.8 KB

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