handle_operator.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/convert.h"
  6. namespace Carbon::Check {
  7. auto HandleInfixOperator(Context& context, Parse::Node parse_node) -> bool {
  8. auto rhs_id = context.node_stack().PopExpression();
  9. auto [lhs_node, lhs_id] = context.node_stack().PopExpressionWithParseNode();
  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 Lex::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 = ConvertToValueOfType(
  17. context, parse_node, lhs_id,
  18. context.semantics_ir().GetNode(rhs_id).type_id());
  19. rhs_id = ConvertToValueExpression(context, rhs_id);
  20. context.AddNodeAndPush(
  21. parse_node,
  22. SemIR::BinaryOperatorAdd(
  23. parse_node, context.semantics_ir().GetNode(lhs_id).type_id(),
  24. lhs_id, rhs_id));
  25. return true;
  26. case Lex::TokenKind::And:
  27. case Lex::TokenKind::Or: {
  28. // The first operand is wrapped in a ShortCircuitOperand, which we
  29. // already handled by creating a RHS block and a resumption block, which
  30. // are the current block and its enclosing block.
  31. rhs_id = ConvertToBoolValue(context, parse_node, rhs_id);
  32. // When the second operand is evaluated, the result of `and` and `or` is
  33. // its value.
  34. auto resume_block_id = context.node_block_stack().PeekOrAdd(/*depth=*/1);
  35. context.AddNode(
  36. SemIR::BranchWithArg(parse_node, resume_block_id, rhs_id));
  37. context.node_block_stack().Pop();
  38. context.AddCurrentCodeBlockToFunction();
  39. // Collect the result from either the first or second operand.
  40. context.AddNodeAndPush(
  41. parse_node,
  42. SemIR::BlockArg(parse_node,
  43. context.semantics_ir().GetNode(rhs_id).type_id(),
  44. resume_block_id));
  45. return true;
  46. }
  47. case Lex::TokenKind::Equal: {
  48. // TODO: handle complex assignment expression such as `a += 1`.
  49. if (SemIR::GetExpressionCategory(context.semantics_ir(), lhs_id) !=
  50. SemIR::ExpressionCategory::DurableReference) {
  51. CARBON_DIAGNOSTIC(AssignmentToNonAssignable, Error,
  52. "Expression is not assignable.");
  53. context.emitter().Emit(lhs_node, AssignmentToNonAssignable);
  54. }
  55. // TODO: Destroy the old value before reinitializing. This will require
  56. // building the destruction code before we build the RHS subexpression.
  57. rhs_id = Initialize(context, parse_node, lhs_id, rhs_id);
  58. context.AddNode(SemIR::Assign(parse_node, lhs_id, rhs_id));
  59. // We model assignment as an expression, so we need to push a value for
  60. // it, even though it doesn't produce a value.
  61. // TODO: Consider changing our parse tree to model assignment as a
  62. // different kind of statement than an expression statement.
  63. context.node_stack().Push(parse_node, lhs_id);
  64. return true;
  65. }
  66. default:
  67. return context.TODO(parse_node, llvm::formatv("Handle {0}", token_kind));
  68. }
  69. }
  70. auto HandlePostfixOperator(Context& context, Parse::Node parse_node) -> bool {
  71. auto value_id = context.node_stack().PopExpression();
  72. // Figure out the operator for the token.
  73. auto token = context.parse_tree().node_token(parse_node);
  74. switch (auto token_kind = context.tokens().GetKind(token)) {
  75. case Lex::TokenKind::Star: {
  76. auto inner_type_id = ExpressionAsType(context, parse_node, value_id);
  77. context.AddNodeAndPush(
  78. parse_node, SemIR::PointerType(parse_node, SemIR::TypeId::TypeType,
  79. inner_type_id));
  80. return true;
  81. }
  82. default:
  83. CARBON_FATAL() << "Unexpected postfix operator " << token_kind;
  84. }
  85. }
  86. auto HandlePrefixOperator(Context& context, Parse::Node parse_node) -> bool {
  87. auto value_id = context.node_stack().PopExpression();
  88. // Figure out the operator for the token.
  89. auto token = context.parse_tree().node_token(parse_node);
  90. switch (auto token_kind = context.tokens().GetKind(token)) {
  91. case Lex::TokenKind::Amp: {
  92. // Only durable reference expressions can have their address taken.
  93. switch (SemIR::GetExpressionCategory(context.semantics_ir(), value_id)) {
  94. case SemIR::ExpressionCategory::DurableReference:
  95. break;
  96. case SemIR::ExpressionCategory::EphemeralReference:
  97. CARBON_DIAGNOSTIC(AddressOfEphemeralReference, Error,
  98. "Cannot take the address of a temporary object.");
  99. context.emitter().Emit(parse_node, AddressOfEphemeralReference);
  100. break;
  101. default:
  102. CARBON_DIAGNOSTIC(
  103. AddressOfNonReference, Error,
  104. "Cannot take the address of non-reference expression.");
  105. context.emitter().Emit(parse_node, AddressOfNonReference);
  106. break;
  107. }
  108. context.AddNodeAndPush(
  109. parse_node,
  110. SemIR::AddressOf(
  111. parse_node,
  112. context.GetPointerType(
  113. parse_node,
  114. context.semantics_ir().GetNode(value_id).type_id()),
  115. value_id));
  116. return true;
  117. }
  118. case Lex::TokenKind::Const: {
  119. // `const (const T)` is probably not what the developer intended.
  120. // TODO: Detect `const (const T)*` and suggest moving the `*` inside the
  121. // parentheses.
  122. if (context.semantics_ir().GetNode(value_id).kind() ==
  123. SemIR::NodeKind::ConstType) {
  124. CARBON_DIAGNOSTIC(RepeatedConst, Warning,
  125. "`const` applied repeatedly to the same type has no "
  126. "additional effect.");
  127. context.emitter().Emit(parse_node, RepeatedConst);
  128. }
  129. auto inner_type_id = ExpressionAsType(context, parse_node, value_id);
  130. context.AddNodeAndPush(
  131. parse_node,
  132. SemIR::ConstType(parse_node, SemIR::TypeId::TypeType, inner_type_id));
  133. return true;
  134. }
  135. case Lex::TokenKind::Not:
  136. value_id = ConvertToBoolValue(context, parse_node, value_id);
  137. context.AddNodeAndPush(
  138. parse_node,
  139. SemIR::UnaryOperatorNot(
  140. parse_node, context.semantics_ir().GetNode(value_id).type_id(),
  141. value_id));
  142. return true;
  143. case Lex::TokenKind::Star: {
  144. auto type_id = context.GetUnqualifiedType(
  145. context.semantics_ir().GetNode(value_id).type_id());
  146. auto type_node = context.semantics_ir().GetNode(
  147. context.semantics_ir().GetTypeAllowBuiltinTypes(type_id));
  148. auto result_type_id = SemIR::TypeId::Error;
  149. if (auto pointer_type = type_node.TryAs<SemIR::PointerType>()) {
  150. result_type_id = pointer_type->pointee_id;
  151. } else {
  152. CARBON_DIAGNOSTIC(
  153. DereferenceOfNonPointer, Error,
  154. "Cannot dereference operand of non-pointer type `{0}`.",
  155. std::string);
  156. auto builder = context.emitter().Build(
  157. parse_node, DereferenceOfNonPointer,
  158. context.semantics_ir().StringifyType(type_id));
  159. // TODO: Check for any facet here, rather than only a type.
  160. if (type_id == SemIR::TypeId::TypeType) {
  161. CARBON_DIAGNOSTIC(
  162. DereferenceOfType, Note,
  163. "To form a pointer type, write the `*` after the pointee type.");
  164. builder.Note(parse_node, DereferenceOfType);
  165. }
  166. builder.Emit();
  167. }
  168. value_id = ConvertToValueExpression(context, value_id);
  169. context.AddNodeAndPush(
  170. parse_node, SemIR::Dereference(parse_node, result_type_id, value_id));
  171. return true;
  172. }
  173. default:
  174. return context.TODO(parse_node, llvm::formatv("Handle {0}", token_kind));
  175. }
  176. }
  177. auto HandleShortCircuitOperand(Context& context, Parse::Node parse_node)
  178. -> bool {
  179. // Convert the condition to `bool`.
  180. auto cond_value_id = context.node_stack().PopExpression();
  181. cond_value_id = ConvertToBoolValue(context, parse_node, cond_value_id);
  182. auto bool_type_id = context.semantics_ir().GetNode(cond_value_id).type_id();
  183. // Compute the branch value: the condition for `and`, inverted for `or`.
  184. auto token = context.parse_tree().node_token(parse_node);
  185. SemIR::NodeId branch_value_id = SemIR::NodeId::Invalid;
  186. auto short_circuit_result_id = SemIR::NodeId::Invalid;
  187. switch (auto token_kind = context.tokens().GetKind(token)) {
  188. case Lex::TokenKind::And:
  189. branch_value_id = cond_value_id;
  190. short_circuit_result_id = context.AddNode(SemIR::BoolLiteral(
  191. parse_node, bool_type_id, SemIR::BoolValue::False));
  192. break;
  193. case Lex::TokenKind::Or:
  194. branch_value_id = context.AddNode(
  195. SemIR::UnaryOperatorNot(parse_node, bool_type_id, cond_value_id));
  196. short_circuit_result_id = context.AddNode(
  197. SemIR::BoolLiteral(parse_node, bool_type_id, SemIR::BoolValue::True));
  198. break;
  199. default:
  200. CARBON_FATAL() << "Unexpected short-circuiting operator " << token_kind;
  201. }
  202. // Create a block for the right-hand side and for the continuation.
  203. auto rhs_block_id =
  204. context.AddDominatedBlockAndBranchIf(parse_node, branch_value_id);
  205. auto end_block_id = context.AddDominatedBlockAndBranchWithArg(
  206. parse_node, short_circuit_result_id);
  207. // Push the resumption and the right-hand side blocks, and start emitting the
  208. // right-hand operand.
  209. context.node_block_stack().Pop();
  210. context.node_block_stack().Push(end_block_id);
  211. context.node_block_stack().Push(rhs_block_id);
  212. context.AddCurrentCodeBlockToFunction();
  213. // Put the condition back on the stack for HandleInfixOperator.
  214. context.node_stack().Push(parse_node, cond_value_id);
  215. return true;
  216. }
  217. } // namespace Carbon::Check