handle_operator.cpp 9.8 KB

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