semantics_handle_operator.cpp 6.7 KB

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