handle_operator.cpp 9.4 KB

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