semantics_handle_operator.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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_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 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. case TokenKind::Equal: {
  47. // TODO: handle complex assignment expression such as `a += 1`.
  48. if (GetSemanticsExpressionCategory(context.semantics_ir(), lhs_id) !=
  49. SemanticsExpressionCategory::DurableReference) {
  50. CARBON_DIAGNOSTIC(AssignmentToNonAssignable, Error,
  51. "Expression is not assignable.");
  52. context.emitter().Emit(lhs_node, AssignmentToNonAssignable);
  53. }
  54. context.ImplicitAsRequired(
  55. parse_node, rhs_id, context.semantics_ir().GetNode(lhs_id).type_id());
  56. context.AddNodeAndPush(
  57. parse_node, SemanticsNode::Assign::Make(parse_node, lhs_id, rhs_id));
  58. return true;
  59. }
  60. default:
  61. return context.TODO(parse_node, llvm::formatv("Handle {0}", token_kind));
  62. }
  63. }
  64. auto SemanticsHandlePostfixOperator(SemanticsContext& context,
  65. ParseTree::Node parse_node) -> bool {
  66. auto value_id = context.node_stack().PopExpression();
  67. // Figure out the operator for the token.
  68. auto token = context.parse_tree().node_token(parse_node);
  69. switch (auto token_kind = context.tokens().GetKind(token)) {
  70. case TokenKind::Star: {
  71. auto inner_type_id = context.ExpressionAsType(parse_node, value_id);
  72. context.AddNodeAndPush(
  73. parse_node,
  74. SemanticsNode::PointerType::Make(
  75. parse_node, SemanticsTypeId::TypeType, inner_type_id));
  76. return true;
  77. }
  78. default:
  79. CARBON_FATAL() << "Unexpected postfix operator " << token_kind;
  80. }
  81. }
  82. auto SemanticsHandlePrefixOperator(SemanticsContext& context,
  83. ParseTree::Node parse_node) -> bool {
  84. auto value_id = context.node_stack().PopExpression();
  85. // Figure out the operator for the token.
  86. auto token = context.parse_tree().node_token(parse_node);
  87. switch (auto token_kind = context.tokens().GetKind(token)) {
  88. case TokenKind::Amp: {
  89. // Only durable reference expressions can have their address taken.
  90. switch (
  91. GetSemanticsExpressionCategory(context.semantics_ir(), value_id)) {
  92. case SemanticsExpressionCategory::DurableReference:
  93. break;
  94. case SemanticsExpressionCategory::EphemeralReference:
  95. CARBON_DIAGNOSTIC(AddressOfEphemeralReference, Error,
  96. "Cannot take the address of a temporary object.");
  97. context.emitter().Emit(parse_node, AddressOfEphemeralReference);
  98. break;
  99. default:
  100. CARBON_DIAGNOSTIC(
  101. AddressOfNonReference, Error,
  102. "Cannot take the address of non-reference expression.");
  103. context.emitter().Emit(parse_node, AddressOfNonReference);
  104. break;
  105. }
  106. context.AddNodeAndPush(
  107. parse_node,
  108. SemanticsNode::AddressOf::Make(
  109. parse_node,
  110. context.GetPointerType(
  111. parse_node,
  112. context.semantics_ir().GetNode(value_id).type_id()),
  113. value_id));
  114. return true;
  115. }
  116. case 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.semantics_ir().GetNode(value_id).kind() ==
  121. SemanticsNodeKind::ConstType) {
  122. CARBON_DIAGNOSTIC(RepeatedConst, Warning,
  123. "`const` applied repeatedly to the same type has no "
  124. "additional effect.");
  125. context.emitter().Emit(parse_node, RepeatedConst);
  126. }
  127. auto inner_type_id = context.ExpressionAsType(parse_node, value_id);
  128. context.AddNodeAndPush(
  129. parse_node,
  130. SemanticsNode::ConstType::Make(parse_node, SemanticsTypeId::TypeType,
  131. inner_type_id));
  132. return true;
  133. }
  134. case TokenKind::Not:
  135. value_id = context.ImplicitAsBool(parse_node, value_id);
  136. context.AddNodeAndPush(
  137. parse_node,
  138. SemanticsNode::UnaryOperatorNot::Make(
  139. parse_node, context.semantics_ir().GetNode(value_id).type_id(),
  140. value_id));
  141. return true;
  142. case TokenKind::Star: {
  143. auto type_id = context.GetUnqualifiedType(
  144. context.semantics_ir().GetNode(value_id).type_id());
  145. auto type_node = context.semantics_ir().GetNode(
  146. context.semantics_ir().GetTypeAllowBuiltinTypes(type_id));
  147. auto result_type_id = SemanticsTypeId::Error;
  148. if (type_node.kind() == SemanticsNodeKind::PointerType) {
  149. result_type_id = type_node.GetAsPointerType();
  150. } else {
  151. CARBON_DIAGNOSTIC(
  152. DereferenceOfNonPointer, Error,
  153. "Cannot dereference operand of non-pointer type `{0}`.",
  154. std::string);
  155. auto builder = context.emitter().Build(
  156. parse_node, DereferenceOfNonPointer,
  157. context.semantics_ir().StringifyType(type_id));
  158. // TODO: Check for any facet here, rather than only a type.
  159. if (type_id == SemanticsTypeId::TypeType) {
  160. CARBON_DIAGNOSTIC(
  161. DereferenceOfType, Note,
  162. "To form a pointer type, write the `*` after the pointee type.");
  163. builder.Note(parse_node, DereferenceOfType);
  164. }
  165. builder.Emit();
  166. }
  167. context.AddNodeAndPush(parse_node,
  168. SemanticsNode::Dereference::Make(
  169. parse_node, result_type_id, value_id));
  170. return true;
  171. }
  172. default:
  173. return context.TODO(parse_node, llvm::formatv("Handle {0}", token_kind));
  174. }
  175. }
  176. auto SemanticsHandleShortCircuitOperand(SemanticsContext& context,
  177. ParseTree::Node parse_node) -> bool {
  178. // Convert the condition to `bool`.
  179. auto cond_value_id = context.node_stack().PopExpression();
  180. cond_value_id = context.ImplicitAsBool(parse_node, cond_value_id);
  181. auto bool_type_id = context.semantics_ir().GetNode(cond_value_id).type_id();
  182. // Compute the branch value: the condition for `and`, inverted for `or`.
  183. auto token = context.parse_tree().node_token(parse_node);
  184. SemanticsNodeId branch_value_id = SemanticsNodeId::Invalid;
  185. auto short_circuit_result_id = SemanticsNodeId::Invalid;
  186. switch (auto token_kind = context.tokens().GetKind(token)) {
  187. case TokenKind::And:
  188. branch_value_id = cond_value_id;
  189. short_circuit_result_id =
  190. context.AddNode(SemanticsNode::BoolLiteral::Make(
  191. parse_node, bool_type_id, SemanticsBoolValue::False));
  192. break;
  193. case TokenKind::Or:
  194. branch_value_id = context.AddNode(SemanticsNode::UnaryOperatorNot::Make(
  195. parse_node, bool_type_id, cond_value_id));
  196. short_circuit_result_id =
  197. context.AddNode(SemanticsNode::BoolLiteral::Make(
  198. parse_node, bool_type_id, SemanticsBoolValue::True));
  199. break;
  200. default:
  201. CARBON_FATAL() << "Unexpected short-circuiting operator " << token_kind;
  202. }
  203. // Create a block for the right-hand side and for the continuation.
  204. auto rhs_block_id =
  205. context.AddDominatedBlockAndBranchIf(parse_node, branch_value_id);
  206. auto end_block_id = context.AddDominatedBlockAndBranchWithArg(
  207. parse_node, short_circuit_result_id);
  208. // Push the resumption and the right-hand side blocks, and start emitting the
  209. // right-hand operand.
  210. context.node_block_stack().Pop();
  211. context.node_block_stack().Push(end_block_id);
  212. context.node_block_stack().Push(rhs_block_id);
  213. context.AddCurrentCodeBlockToFunction();
  214. // Put the condition back on the stack for SemanticsHandleInfixOperator.
  215. context.node_stack().Push(parse_node, cond_value_id);
  216. return true;
  217. }
  218. } // namespace Carbon