semantics_handle_operator.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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::Check {
  6. auto HandleInfixOperator(Context& context, Parse::Node parse_node) -> bool {
  7. auto rhs_id = context.node_stack().PopExpression();
  8. auto [lhs_node, lhs_id] = context.node_stack().PopExpressionWithParseNode();
  9. // Figure out the operator for the token.
  10. auto token = context.parse_tree().node_token(parse_node);
  11. switch (auto token_kind = context.tokens().GetKind(token)) {
  12. case TokenKind::Plus:
  13. // TODO: This should search for a compatible interface. For now, it's a
  14. // very trivial check of validity on the operation.
  15. lhs_id = context.ConvertToValueOfType(
  16. parse_node, lhs_id, context.semantics_ir().GetNode(rhs_id).type_id());
  17. rhs_id = context.ConvertToValueExpression(rhs_id);
  18. context.AddNodeAndPush(
  19. parse_node,
  20. SemIR::Node::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.ConvertToBoolValue(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. SemIR::Node::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. SemIR::Node::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. if (SemIR::GetExpressionCategory(context.semantics_ir(), lhs_id) !=
  49. SemIR::ExpressionCategory::DurableReference) {
  50. CARBON_DIAGNOSTIC(AssignmentToNonAssignable, Error,
  51. "Expression is not assignable.");
  52. context.emitter().Emit(lhs_node, AssignmentToNonAssignable);
  53. }
  54. context.Initialize(parse_node, lhs_id, rhs_id);
  55. // We model assignment as an expression, so we need to push a value for
  56. // it, even though it doesn't produce a value.
  57. // TODO: Consider changing our parse tree to model assignment as a
  58. // different kind of statement than an expression statement.
  59. context.node_stack().Push(parse_node, lhs_id);
  60. return true;
  61. }
  62. default:
  63. return context.TODO(parse_node, llvm::formatv("Handle {0}", token_kind));
  64. }
  65. }
  66. auto HandlePostfixOperator(Context& context, Parse::Node parse_node) -> bool {
  67. auto value_id = context.node_stack().PopExpression();
  68. // Figure out the operator for the token.
  69. auto token = context.parse_tree().node_token(parse_node);
  70. switch (auto token_kind = context.tokens().GetKind(token)) {
  71. case TokenKind::Star: {
  72. auto inner_type_id = context.ExpressionAsType(parse_node, value_id);
  73. context.AddNodeAndPush(
  74. parse_node, SemIR::Node::PointerType::Make(
  75. parse_node, SemIR::TypeId::TypeType, inner_type_id));
  76. return true;
  77. }
  78. default:
  79. CARBON_FATAL() << "Unexpected postfix operator " << token_kind;
  80. }
  81. }
  82. auto HandlePrefixOperator(Context& context, Parse::Node parse_node) -> bool {
  83. auto value_id = context.node_stack().PopExpression();
  84. // Figure out the operator for the token.
  85. auto token = context.parse_tree().node_token(parse_node);
  86. switch (auto token_kind = context.tokens().GetKind(token)) {
  87. case TokenKind::Amp: {
  88. // Only durable reference expressions can have their address taken.
  89. switch (SemIR::GetExpressionCategory(context.semantics_ir(), value_id)) {
  90. case SemIR::ExpressionCategory::DurableReference:
  91. break;
  92. case SemIR::ExpressionCategory::EphemeralReference:
  93. CARBON_DIAGNOSTIC(AddressOfEphemeralReference, Error,
  94. "Cannot take the address of a temporary object.");
  95. context.emitter().Emit(parse_node, AddressOfEphemeralReference);
  96. break;
  97. default:
  98. CARBON_DIAGNOSTIC(
  99. AddressOfNonReference, Error,
  100. "Cannot take the address of non-reference expression.");
  101. context.emitter().Emit(parse_node, AddressOfNonReference);
  102. break;
  103. }
  104. context.AddNodeAndPush(
  105. parse_node,
  106. SemIR::Node::AddressOf::Make(
  107. parse_node,
  108. context.GetPointerType(
  109. parse_node,
  110. context.semantics_ir().GetNode(value_id).type_id()),
  111. value_id));
  112. return true;
  113. }
  114. case TokenKind::Const: {
  115. // `const (const T)` is probably not what the developer intended.
  116. // TODO: Detect `const (const T)*` and suggest moving the `*` inside the
  117. // parentheses.
  118. if (context.semantics_ir().GetNode(value_id).kind() ==
  119. SemIR::NodeKind::ConstType) {
  120. CARBON_DIAGNOSTIC(RepeatedConst, Warning,
  121. "`const` applied repeatedly to the same type has no "
  122. "additional effect.");
  123. context.emitter().Emit(parse_node, RepeatedConst);
  124. }
  125. auto inner_type_id = context.ExpressionAsType(parse_node, value_id);
  126. context.AddNodeAndPush(
  127. parse_node, SemIR::Node::ConstType::Make(
  128. parse_node, SemIR::TypeId::TypeType, inner_type_id));
  129. return true;
  130. }
  131. case TokenKind::Not:
  132. value_id = context.ConvertToBoolValue(parse_node, value_id);
  133. context.AddNodeAndPush(
  134. parse_node,
  135. SemIR::Node::UnaryOperatorNot::Make(
  136. parse_node, context.semantics_ir().GetNode(value_id).type_id(),
  137. value_id));
  138. return true;
  139. case TokenKind::Star: {
  140. auto type_id = context.GetUnqualifiedType(
  141. context.semantics_ir().GetNode(value_id).type_id());
  142. auto type_node = context.semantics_ir().GetNode(
  143. context.semantics_ir().GetTypeAllowBuiltinTypes(type_id));
  144. auto result_type_id = SemIR::TypeId::Error;
  145. if (type_node.kind() == SemIR::NodeKind::PointerType) {
  146. result_type_id = type_node.GetAsPointerType();
  147. } else {
  148. CARBON_DIAGNOSTIC(
  149. DereferenceOfNonPointer, Error,
  150. "Cannot dereference operand of non-pointer type `{0}`.",
  151. std::string);
  152. auto builder = context.emitter().Build(
  153. parse_node, DereferenceOfNonPointer,
  154. context.semantics_ir().StringifyType(type_id));
  155. // TODO: Check for any facet here, rather than only a type.
  156. if (type_id == SemIR::TypeId::TypeType) {
  157. CARBON_DIAGNOSTIC(
  158. DereferenceOfType, Note,
  159. "To form a pointer type, write the `*` after the pointee type.");
  160. builder.Note(parse_node, DereferenceOfType);
  161. }
  162. builder.Emit();
  163. }
  164. value_id = context.ConvertToValueExpression(value_id);
  165. context.AddNodeAndPush(
  166. parse_node,
  167. SemIR::Node::Dereference::Make(parse_node, result_type_id, value_id));
  168. return true;
  169. }
  170. default:
  171. return context.TODO(parse_node, llvm::formatv("Handle {0}", token_kind));
  172. }
  173. }
  174. auto HandleShortCircuitOperand(Context& context, Parse::Node parse_node)
  175. -> bool {
  176. // Convert the condition to `bool`.
  177. auto cond_value_id = context.node_stack().PopExpression();
  178. cond_value_id = context.ConvertToBoolValue(parse_node, cond_value_id);
  179. auto bool_type_id = context.semantics_ir().GetNode(cond_value_id).type_id();
  180. // Compute the branch value: the condition for `and`, inverted for `or`.
  181. auto token = context.parse_tree().node_token(parse_node);
  182. SemIR::NodeId branch_value_id = SemIR::NodeId::Invalid;
  183. auto short_circuit_result_id = SemIR::NodeId::Invalid;
  184. switch (auto token_kind = context.tokens().GetKind(token)) {
  185. case TokenKind::And:
  186. branch_value_id = cond_value_id;
  187. short_circuit_result_id = context.AddNode(SemIR::Node::BoolLiteral::Make(
  188. parse_node, bool_type_id, SemIR::BoolValue::False));
  189. break;
  190. case TokenKind::Or:
  191. branch_value_id = context.AddNode(SemIR::Node::UnaryOperatorNot::Make(
  192. parse_node, bool_type_id, cond_value_id));
  193. short_circuit_result_id = context.AddNode(SemIR::Node::BoolLiteral::Make(
  194. parse_node, bool_type_id, SemIR::BoolValue::True));
  195. break;
  196. default:
  197. CARBON_FATAL() << "Unexpected short-circuiting operator " << token_kind;
  198. }
  199. // Create a block for the right-hand side and for the continuation.
  200. auto rhs_block_id =
  201. context.AddDominatedBlockAndBranchIf(parse_node, branch_value_id);
  202. auto end_block_id = context.AddDominatedBlockAndBranchWithArg(
  203. parse_node, short_circuit_result_id);
  204. // Push the resumption and the right-hand side blocks, and start emitting the
  205. // right-hand operand.
  206. context.node_block_stack().Pop();
  207. context.node_block_stack().Push(end_block_id);
  208. context.node_block_stack().Push(rhs_block_id);
  209. context.AddCurrentCodeBlockToFunction();
  210. // Put the condition back on the stack for HandleInfixOperator.
  211. context.node_stack().Push(parse_node, cond_value_id);
  212. return true;
  213. }
  214. } // namespace Carbon::Check