semantics_handle_operator.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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().PopExpression();
  9. auto lhs_id = context.node_stack().PopExpression();
  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. return true;
  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. return true;
  45. }
  46. case TokenKind::Equal: {
  47. // TODO: handle complex assignment expression such as `a += 1`.
  48. // TODO: check if lhs node is assignable.
  49. context.ImplicitAsRequired(
  50. parse_node, rhs_id, context.semantics_ir().GetNode(lhs_id).type_id());
  51. context.AddNodeAndPush(
  52. parse_node, SemanticsNode::Assign::Make(parse_node, lhs_id, rhs_id));
  53. return true;
  54. }
  55. default:
  56. return context.TODO(parse_node, llvm::formatv("Handle {0}", token_kind));
  57. }
  58. }
  59. auto SemanticsHandlePostfixOperator(SemanticsContext& context,
  60. ParseTree::Node parse_node) -> bool {
  61. auto value_id = context.node_stack().PopExpression();
  62. // Figure out the operator for the token.
  63. auto token = context.parse_tree().node_token(parse_node);
  64. switch (auto token_kind = context.tokens().GetKind(token)) {
  65. case TokenKind::Star: {
  66. auto inner_type_id = context.ExpressionAsType(parse_node, value_id);
  67. context.AddNodeAndPush(
  68. parse_node,
  69. SemanticsNode::PointerType::Make(
  70. parse_node, SemanticsTypeId::TypeType, inner_type_id));
  71. return true;
  72. }
  73. default:
  74. CARBON_FATAL() << "Unexpected postfix operator " << token_kind;
  75. }
  76. }
  77. auto SemanticsHandlePrefixOperator(SemanticsContext& context,
  78. ParseTree::Node parse_node) -> bool {
  79. auto value_id = context.node_stack().PopExpression();
  80. // Figure out the operator for the token.
  81. auto token = context.parse_tree().node_token(parse_node);
  82. switch (auto token_kind = context.tokens().GetKind(token)) {
  83. case TokenKind::Not:
  84. value_id = context.ImplicitAsBool(parse_node, value_id);
  85. context.AddNodeAndPush(
  86. parse_node,
  87. SemanticsNode::UnaryOperatorNot::Make(
  88. parse_node, context.semantics_ir().GetNode(value_id).type_id(),
  89. value_id));
  90. return true;
  91. case TokenKind::Const: {
  92. // `const (const T)` is probably not what the developer intended.
  93. // TODO: Detect `const (const T)*` and suggest moving the `*` inside the
  94. // parentheses.
  95. if (context.semantics_ir().GetNode(value_id).kind() ==
  96. SemanticsNodeKind::ConstType) {
  97. CARBON_DIAGNOSTIC(RepeatedConst, Warning,
  98. "`const` applied repeatedly to the same type has no "
  99. "additional effect.");
  100. context.emitter().Emit(parse_node, RepeatedConst);
  101. }
  102. auto inner_type_id = context.ExpressionAsType(parse_node, value_id);
  103. context.AddNodeAndPush(
  104. parse_node,
  105. SemanticsNode::ConstType::Make(parse_node, SemanticsTypeId::TypeType,
  106. inner_type_id));
  107. return true;
  108. }
  109. default:
  110. return context.TODO(parse_node, llvm::formatv("Handle {0}", token_kind));
  111. }
  112. }
  113. auto SemanticsHandleShortCircuitOperand(SemanticsContext& context,
  114. ParseTree::Node parse_node) -> bool {
  115. // Convert the condition to `bool`.
  116. auto cond_value_id = context.node_stack().PopExpression();
  117. cond_value_id = context.ImplicitAsBool(parse_node, cond_value_id);
  118. auto bool_type_id = context.semantics_ir().GetNode(cond_value_id).type_id();
  119. // Compute the branch value: the condition for `and`, inverted for `or`.
  120. auto token = context.parse_tree().node_token(parse_node);
  121. SemanticsNodeId branch_value_id = SemanticsNodeId::Invalid;
  122. auto short_circuit_result_id = SemanticsNodeId::Invalid;
  123. switch (auto token_kind = context.tokens().GetKind(token)) {
  124. case TokenKind::And:
  125. branch_value_id = cond_value_id;
  126. short_circuit_result_id =
  127. context.AddNode(SemanticsNode::BoolLiteral::Make(
  128. parse_node, bool_type_id, SemanticsBoolValue::False));
  129. break;
  130. case TokenKind::Or:
  131. branch_value_id = context.AddNode(SemanticsNode::UnaryOperatorNot::Make(
  132. parse_node, bool_type_id, cond_value_id));
  133. short_circuit_result_id =
  134. context.AddNode(SemanticsNode::BoolLiteral::Make(
  135. parse_node, bool_type_id, SemanticsBoolValue::True));
  136. break;
  137. default:
  138. CARBON_FATAL() << "Unexpected short-circuiting operator " << token_kind;
  139. }
  140. // Create a block for the right-hand side and for the continuation.
  141. auto rhs_block_id =
  142. context.AddDominatedBlockAndBranchIf(parse_node, branch_value_id);
  143. auto end_block_id = context.AddDominatedBlockAndBranchWithArg(
  144. parse_node, short_circuit_result_id);
  145. // Push the resumption and the right-hand side blocks, and start emitting the
  146. // right-hand operand.
  147. context.node_block_stack().Pop();
  148. context.node_block_stack().Push(end_block_id);
  149. context.node_block_stack().Push(rhs_block_id);
  150. context.AddCurrentCodeBlockToFunction();
  151. // Put the condition back on the stack for SemanticsHandleInfixOperator.
  152. context.node_stack().Push(parse_node, cond_value_id);
  153. return true;
  154. }
  155. } // namespace Carbon