semantics_handle_operator.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/semantics/semantics_context.h"
  5. namespace Carbon {
  6. auto SemanticsHandleInfixOperator(SemanticsContext& context,
  7. ParseTree::Node parse_node) -> bool {
  8. auto rhs_id = context.node_stack().Pop<SemanticsNodeId>();
  9. auto lhs_id = context.node_stack().Pop<SemanticsNodeId>();
  10. // Figure out the operator for the token.
  11. auto token = context.parse_tree().node_token(parse_node);
  12. switch (auto token_kind = context.tokens().GetKind(token)) {
  13. case TokenKind::Plus:
  14. // TODO: This should search for a compatible interface. For now, it's a
  15. // very trivial check of validity on the operation.
  16. lhs_id = context.ImplicitAsRequired(
  17. parse_node, lhs_id, context.semantics_ir().GetNode(rhs_id).type_id());
  18. context.AddNodeAndPush(
  19. parse_node,
  20. SemanticsNode::BinaryOperatorAdd::Make(
  21. parse_node, context.semantics_ir().GetNode(lhs_id).type_id(),
  22. lhs_id, rhs_id));
  23. break;
  24. case TokenKind::And:
  25. case TokenKind::Or: {
  26. // The first operand is wrapped in a ShortCircuitOperand, which we
  27. // already handled by creating a RHS block and a resumption block, which
  28. // are the current block and its enclosing block.
  29. rhs_id = context.ImplicitAsBool(parse_node, rhs_id);
  30. // When the second operand is evaluated, the result of `and` and `or` is
  31. // its value.
  32. auto rhs_block_id = context.node_block_stack().PopForAdd();
  33. auto resume_block_id = context.node_block_stack().PeekForAdd();
  34. context.AddNodeToBlock(rhs_block_id,
  35. SemanticsNode::BranchWithArg::Make(
  36. parse_node, resume_block_id, rhs_id));
  37. context.AddCurrentCodeBlockToFunction();
  38. // Collect the result from either the first or second operand.
  39. context.AddNodeAndPush(
  40. parse_node,
  41. SemanticsNode::BlockArg::Make(
  42. parse_node, context.semantics_ir().GetNode(rhs_id).type_id(),
  43. resume_block_id));
  44. break;
  45. }
  46. default:
  47. return context.TODO(parse_node, llvm::formatv("Handle {0}", token_kind));
  48. }
  49. return true;
  50. }
  51. auto SemanticsHandlePostfixOperator(SemanticsContext& context,
  52. ParseTree::Node parse_node) -> bool {
  53. return context.TODO(parse_node, "HandlePostfixOperator");
  54. }
  55. auto SemanticsHandlePrefixOperator(SemanticsContext& context,
  56. ParseTree::Node parse_node) -> bool {
  57. auto value_id = context.node_stack().Pop<SemanticsNodeId>();
  58. // Figure out the operator for the token.
  59. auto token = context.parse_tree().node_token(parse_node);
  60. switch (auto token_kind = context.tokens().GetKind(token)) {
  61. case TokenKind::Not:
  62. value_id = context.ImplicitAsBool(parse_node, value_id);
  63. context.AddNodeAndPush(
  64. parse_node,
  65. SemanticsNode::UnaryOperatorNot::Make(
  66. parse_node, context.semantics_ir().GetNode(value_id).type_id(),
  67. value_id));
  68. break;
  69. default:
  70. return context.TODO(parse_node, llvm::formatv("Handle {0}", token_kind));
  71. }
  72. return true;
  73. }
  74. auto SemanticsHandleShortCircuitOperand(SemanticsContext& context,
  75. ParseTree::Node parse_node) -> bool {
  76. // Convert the condition to `bool`.
  77. auto cond_value_id = context.node_stack().Pop<SemanticsNodeId>();
  78. cond_value_id = context.ImplicitAsBool(parse_node, cond_value_id);
  79. auto bool_type_id = context.semantics_ir().GetNode(cond_value_id).type_id();
  80. // Compute the branch value: the condition for `and`, inverted for `or`.
  81. auto token = context.parse_tree().node_token(parse_node);
  82. SemanticsNodeId branch_value_id = SemanticsNodeId::Invalid;
  83. auto short_circuit_result_id = SemanticsNodeId::Invalid;
  84. switch (auto token_kind = context.tokens().GetKind(token)) {
  85. case TokenKind::And:
  86. branch_value_id = cond_value_id;
  87. short_circuit_result_id =
  88. context.AddNode(SemanticsNode::BoolLiteral::Make(
  89. parse_node, bool_type_id, SemanticsBoolValue::False));
  90. break;
  91. case TokenKind::Or:
  92. branch_value_id = context.AddNode(SemanticsNode::UnaryOperatorNot::Make(
  93. parse_node, bool_type_id, cond_value_id));
  94. short_circuit_result_id =
  95. context.AddNode(SemanticsNode::BoolLiteral::Make(
  96. parse_node, bool_type_id, SemanticsBoolValue::True));
  97. break;
  98. default:
  99. CARBON_FATAL() << "Unexpected short-circuiting operator " << parse_node;
  100. }
  101. // Create a block for the right-hand side and for the continuation.
  102. auto rhs_block_id =
  103. context.AddDominatedBlockAndBranchIf(parse_node, branch_value_id);
  104. auto end_block_id = context.AddDominatedBlockAndBranchWithArg(
  105. parse_node, short_circuit_result_id);
  106. // Push the resumption and the right-hand side blocks, and start emitting the
  107. // right-hand operand.
  108. context.node_block_stack().Pop();
  109. context.node_block_stack().Push(end_block_id);
  110. context.node_block_stack().Push(rhs_block_id);
  111. context.AddCurrentCodeBlockToFunction();
  112. // Put the condition back on the stack for SemanticsHandleInfixOperator.
  113. context.node_stack().Push(parse_node, cond_value_id);
  114. return true;
  115. }
  116. } // namespace Carbon