handle_operator.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 HandleInfixOperatorAmp(Context& context, Parse::NodeId parse_node)
  8. -> bool {
  9. return context.TODO(parse_node, "HandleInfixOperatorAmp");
  10. }
  11. auto HandleInfixOperatorAmpEqual(Context& context, Parse::NodeId parse_node)
  12. -> bool {
  13. return context.TODO(parse_node, "HandleInfixOperatorAmpEqual");
  14. }
  15. auto HandleInfixOperatorAs(Context& context, Parse::NodeId parse_node) -> bool {
  16. auto [rhs_node, rhs_id] = context.node_stack().PopExprWithParseNode();
  17. auto [lhs_node, lhs_id] = context.node_stack().PopExprWithParseNode();
  18. auto rhs_type_id = ExprAsType(context, rhs_node, rhs_id);
  19. context.node_stack().Push(
  20. parse_node,
  21. ConvertForExplicitAs(context, parse_node, lhs_id, rhs_type_id));
  22. return true;
  23. }
  24. auto HandleInfixOperatorCaret(Context& context, Parse::NodeId parse_node)
  25. -> bool {
  26. return context.TODO(parse_node, "HandleInfixOperatorCaret");
  27. }
  28. auto HandleInfixOperatorCaretEqual(Context& context, Parse::NodeId parse_node)
  29. -> bool {
  30. return context.TODO(parse_node, "HandleInfixOperatorCaretEqual");
  31. }
  32. auto HandleInfixOperatorEqual(Context& context, Parse::NodeId parse_node)
  33. -> bool {
  34. auto [rhs_node, rhs_id] = context.node_stack().PopExprWithParseNode();
  35. auto [lhs_node, lhs_id] = context.node_stack().PopExprWithParseNode();
  36. // TODO: handle complex assignment expression such as `a += 1`.
  37. if (auto lhs_cat = SemIR::GetExprCategory(context.sem_ir(), lhs_id);
  38. lhs_cat != SemIR::ExprCategory::DurableRef &&
  39. lhs_cat != SemIR::ExprCategory::Error) {
  40. CARBON_DIAGNOSTIC(AssignmentToNonAssignable, Error,
  41. "Expression is not assignable.");
  42. context.emitter().Emit(lhs_node, AssignmentToNonAssignable);
  43. }
  44. // TODO: Destroy the old value before reinitializing. This will require
  45. // building the destruction code before we build the RHS subexpression.
  46. rhs_id = Initialize(context, parse_node, lhs_id, rhs_id);
  47. context.AddInst(SemIR::Assign{parse_node, lhs_id, rhs_id});
  48. // We model assignment as an expression, so we need to push a value for
  49. // it, even though it doesn't produce a value.
  50. // TODO: Consider changing our parse tree to model assignment as a
  51. // different kind of statement than an expression statement.
  52. context.node_stack().Push(parse_node, lhs_id);
  53. return true;
  54. }
  55. auto HandleInfixOperatorEqualEqual(Context& context, Parse::NodeId parse_node)
  56. -> bool {
  57. return context.TODO(parse_node, "HandleInfixOperatorEqualEqual");
  58. }
  59. auto HandleInfixOperatorExclaimEqual(Context& context, Parse::NodeId parse_node)
  60. -> bool {
  61. return context.TODO(parse_node, "HandleInfixOperatorExclaimEqual");
  62. }
  63. auto HandleInfixOperatorGreater(Context& context, Parse::NodeId parse_node)
  64. -> bool {
  65. return context.TODO(parse_node, "HandleInfixOperatorGreater");
  66. }
  67. auto HandleInfixOperatorGreaterEqual(Context& context, Parse::NodeId parse_node)
  68. -> bool {
  69. return context.TODO(parse_node, "HandleInfixOperatorGreaterEqual");
  70. }
  71. auto HandleInfixOperatorGreaterGreater(Context& context,
  72. Parse::NodeId parse_node) -> bool {
  73. return context.TODO(parse_node, "HandleInfixOperatorGreaterGreater");
  74. }
  75. auto HandleInfixOperatorGreaterGreaterEqual(Context& context,
  76. Parse::NodeId parse_node) -> bool {
  77. return context.TODO(parse_node, "HandleInfixOperatorGreaterGreaterEqual");
  78. }
  79. auto HandleInfixOperatorLess(Context& context, Parse::NodeId parse_node)
  80. -> bool {
  81. return context.TODO(parse_node, "HandleInfixOperatorLess");
  82. }
  83. auto HandleInfixOperatorLessEqual(Context& context, Parse::NodeId parse_node)
  84. -> bool {
  85. return context.TODO(parse_node, "HandleInfixOperatorLessEqual");
  86. }
  87. auto HandleInfixOperatorLessEqualGreater(Context& context,
  88. Parse::NodeId parse_node) -> bool {
  89. return context.TODO(parse_node, "HandleInfixOperatorLessEqualGreater");
  90. }
  91. auto HandleInfixOperatorLessLess(Context& context, Parse::NodeId parse_node)
  92. -> bool {
  93. return context.TODO(parse_node, "HandleInfixOperatorLessLess");
  94. }
  95. auto HandleInfixOperatorLessLessEqual(Context& context,
  96. Parse::NodeId parse_node) -> bool {
  97. return context.TODO(parse_node, "HandleInfixOperatorLessLessEqual");
  98. }
  99. auto HandleInfixOperatorMinus(Context& context, Parse::NodeId parse_node)
  100. -> bool {
  101. return context.TODO(parse_node, "HandleInfixOperatorMinus");
  102. }
  103. auto HandleInfixOperatorMinusEqual(Context& context, Parse::NodeId parse_node)
  104. -> bool {
  105. return context.TODO(parse_node, "HandleInfixOperatorMinusEqual");
  106. }
  107. auto HandleInfixOperatorPercent(Context& context, Parse::NodeId parse_node)
  108. -> bool {
  109. return context.TODO(parse_node, "HandleInfixOperatorPercent");
  110. }
  111. auto HandleInfixOperatorPercentEqual(Context& context, Parse::NodeId parse_node)
  112. -> bool {
  113. return context.TODO(parse_node, "HandleInfixOperatorPercentEqual");
  114. }
  115. auto HandleInfixOperatorPipe(Context& context, Parse::NodeId parse_node)
  116. -> bool {
  117. return context.TODO(parse_node, "HandleInfixOperatorPipe");
  118. }
  119. auto HandleInfixOperatorPipeEqual(Context& context, Parse::NodeId parse_node)
  120. -> bool {
  121. return context.TODO(parse_node, "HandleInfixOperatorPipeEqual");
  122. }
  123. auto HandleInfixOperatorPlus(Context& context, Parse::NodeId parse_node)
  124. -> bool {
  125. return context.TODO(parse_node, "HandleInfixOperatorPlus");
  126. }
  127. auto HandleInfixOperatorPlusEqual(Context& context, Parse::NodeId parse_node)
  128. -> bool {
  129. return context.TODO(parse_node, "HandleInfixOperatorPlusEqual");
  130. }
  131. auto HandleInfixOperatorSlash(Context& context, Parse::NodeId parse_node)
  132. -> bool {
  133. return context.TODO(parse_node, "HandleInfixOperatorSlash");
  134. }
  135. auto HandleInfixOperatorSlashEqual(Context& context, Parse::NodeId parse_node)
  136. -> bool {
  137. return context.TODO(parse_node, "HandleInfixOperatorSlashEqual");
  138. }
  139. auto HandleInfixOperatorStar(Context& context, Parse::NodeId parse_node)
  140. -> bool {
  141. return context.TODO(parse_node, "HandleInfixOperatorStar");
  142. }
  143. auto HandleInfixOperatorStarEqual(Context& context, Parse::NodeId parse_node)
  144. -> bool {
  145. return context.TODO(parse_node, "HandleInfixOperatorStarEqual");
  146. }
  147. auto HandlePostfixOperatorStar(Context& context, Parse::NodeId parse_node)
  148. -> bool {
  149. auto value_id = context.node_stack().PopExpr();
  150. auto inner_type_id = ExprAsType(context, parse_node, value_id);
  151. context.AddInstAndPush(
  152. parse_node,
  153. SemIR::PointerType{parse_node, SemIR::TypeId::TypeType, inner_type_id});
  154. return true;
  155. }
  156. auto HandlePrefixOperatorAmp(Context& context, Parse::NodeId parse_node)
  157. -> bool {
  158. auto value_id = context.node_stack().PopExpr();
  159. // Only durable reference expressions can have their address taken.
  160. switch (SemIR::GetExprCategory(context.sem_ir(), value_id)) {
  161. case SemIR::ExprCategory::DurableRef:
  162. case SemIR::ExprCategory::Error:
  163. break;
  164. case SemIR::ExprCategory::EphemeralRef:
  165. CARBON_DIAGNOSTIC(AddressOfEphemeralRef, Error,
  166. "Cannot take the address of a temporary object.");
  167. context.emitter().Emit(TokenOnly(parse_node), AddressOfEphemeralRef);
  168. break;
  169. default:
  170. CARBON_DIAGNOSTIC(AddressOfNonRef, Error,
  171. "Cannot take the address of non-reference expression.");
  172. context.emitter().Emit(TokenOnly(parse_node), AddressOfNonRef);
  173. break;
  174. }
  175. context.AddInstAndPush(
  176. parse_node,
  177. SemIR::AddressOf{parse_node,
  178. context.GetPointerType(
  179. parse_node, context.insts().Get(value_id).type_id()),
  180. value_id});
  181. return true;
  182. }
  183. auto HandlePrefixOperatorCaret(Context& context, Parse::NodeId parse_node)
  184. -> bool {
  185. return context.TODO(parse_node, "HandlePrefixOperatorCaret");
  186. }
  187. auto HandlePrefixOperatorConst(Context& context, Parse::NodeId parse_node)
  188. -> bool {
  189. auto value_id = context.node_stack().PopExpr();
  190. // `const (const T)` is probably not what the developer intended.
  191. // TODO: Detect `const (const T)*` and suggest moving the `*` inside the
  192. // parentheses.
  193. if (context.insts().Get(value_id).kind() == SemIR::ConstType::Kind) {
  194. CARBON_DIAGNOSTIC(RepeatedConst, Warning,
  195. "`const` applied repeatedly to the same type has no "
  196. "additional effect.");
  197. context.emitter().Emit(parse_node, RepeatedConst);
  198. }
  199. auto inner_type_id = ExprAsType(context, parse_node, value_id);
  200. context.AddInstAndPush(
  201. parse_node,
  202. SemIR::ConstType{parse_node, SemIR::TypeId::TypeType, inner_type_id});
  203. return true;
  204. }
  205. auto HandlePrefixOperatorMinus(Context& context, Parse::NodeId parse_node)
  206. -> bool {
  207. return context.TODO(parse_node, "HandlePrefixOperatorMinus");
  208. }
  209. auto HandlePrefixOperatorMinusMinus(Context& context, Parse::NodeId parse_node)
  210. -> bool {
  211. return context.TODO(parse_node, "HandlePrefixOperatorMinusMinus");
  212. }
  213. auto HandlePrefixOperatorNot(Context& context, Parse::NodeId parse_node)
  214. -> bool {
  215. auto value_id = context.node_stack().PopExpr();
  216. value_id = ConvertToBoolValue(context, parse_node, value_id);
  217. context.AddInstAndPush(
  218. parse_node,
  219. SemIR::UnaryOperatorNot{
  220. parse_node, context.insts().Get(value_id).type_id(), value_id});
  221. return true;
  222. }
  223. auto HandlePrefixOperatorPlus(Context& context, Parse::NodeId parse_node)
  224. -> bool {
  225. return context.TODO(parse_node, "HandlePrefixOperatorPlus");
  226. }
  227. auto HandlePrefixOperatorPlusPlus(Context& context, Parse::NodeId parse_node)
  228. -> bool {
  229. return context.TODO(parse_node, "HandlePrefixOperatorPlusPlus");
  230. }
  231. auto HandlePrefixOperatorStar(Context& context, Parse::NodeId parse_node)
  232. -> bool {
  233. auto value_id = context.node_stack().PopExpr();
  234. value_id = ConvertToValueExpr(context, value_id);
  235. auto type_id =
  236. context.GetUnqualifiedType(context.insts().Get(value_id).type_id());
  237. auto result_type_id = SemIR::TypeId::Error;
  238. if (auto pointer_type =
  239. context.types().TryGetAs<SemIR::PointerType>(type_id)) {
  240. result_type_id = pointer_type->pointee_id;
  241. } else if (type_id != SemIR::TypeId::Error) {
  242. CARBON_DIAGNOSTIC(DerefOfNonPointer, Error,
  243. "Cannot dereference operand of non-pointer type `{0}`.",
  244. std::string);
  245. auto builder =
  246. context.emitter().Build(TokenOnly(parse_node), DerefOfNonPointer,
  247. context.sem_ir().StringifyType(type_id));
  248. // TODO: Check for any facet here, rather than only a type.
  249. if (type_id == SemIR::TypeId::TypeType) {
  250. CARBON_DIAGNOSTIC(
  251. DerefOfType, Note,
  252. "To form a pointer type, write the `*` after the pointee type.");
  253. builder.Note(TokenOnly(parse_node), DerefOfType);
  254. }
  255. builder.Emit();
  256. }
  257. context.AddInstAndPush(parse_node,
  258. SemIR::Deref{parse_node, result_type_id, value_id});
  259. return true;
  260. }
  261. // Adds the branch for a short circuit operand.
  262. static auto HandleShortCircuitOperand(Context& context,
  263. Parse::NodeId parse_node, bool is_or)
  264. -> bool {
  265. // Convert the condition to `bool`.
  266. auto cond_value_id = context.node_stack().PopExpr();
  267. cond_value_id = ConvertToBoolValue(context, parse_node, cond_value_id);
  268. auto bool_type_id = context.insts().Get(cond_value_id).type_id();
  269. // Compute the branch value: the condition for `and`, inverted for `or`.
  270. SemIR::InstId branch_value_id =
  271. is_or ? context.AddInst(SemIR::UnaryOperatorNot{parse_node, bool_type_id,
  272. cond_value_id})
  273. : cond_value_id;
  274. auto short_circuit_result_id = context.AddInst(SemIR::BoolLiteral{
  275. parse_node, bool_type_id,
  276. is_or ? SemIR::BoolValue::True : SemIR::BoolValue::False});
  277. // Create a block for the right-hand side and for the continuation.
  278. auto rhs_block_id =
  279. context.AddDominatedBlockAndBranchIf(parse_node, branch_value_id);
  280. auto end_block_id = context.AddDominatedBlockAndBranchWithArg(
  281. parse_node, short_circuit_result_id);
  282. // Push the resumption and the right-hand side blocks, and start emitting the
  283. // right-hand operand.
  284. context.inst_block_stack().Pop();
  285. context.inst_block_stack().Push(end_block_id);
  286. context.inst_block_stack().Push(rhs_block_id);
  287. context.AddCurrentCodeBlockToFunction();
  288. // HandleShortCircuitOperator will follow, and doesn't need the operand on the
  289. // node stack.
  290. return true;
  291. }
  292. auto HandleShortCircuitOperandAnd(Context& context, Parse::NodeId parse_node)
  293. -> bool {
  294. return HandleShortCircuitOperand(context, parse_node, /*is_or=*/false);
  295. }
  296. auto HandleShortCircuitOperandOr(Context& context, Parse::NodeId parse_node)
  297. -> bool {
  298. return HandleShortCircuitOperand(context, parse_node, /*is_or=*/true);
  299. }
  300. // Short circuit operator handling is uniform because the branching logic
  301. // occurs during operand handling.
  302. static auto HandleShortCircuitOperator(Context& context,
  303. Parse::NodeId parse_node) -> bool {
  304. auto [rhs_node, rhs_id] = context.node_stack().PopExprWithParseNode();
  305. // The first operand is wrapped in a ShortCircuitOperand, which we
  306. // already handled by creating a RHS block and a resumption block, which
  307. // are the current block and its enclosing block.
  308. rhs_id = ConvertToBoolValue(context, parse_node, rhs_id);
  309. // When the second operand is evaluated, the result of `and` and `or` is
  310. // its value.
  311. auto resume_block_id = context.inst_block_stack().PeekOrAdd(/*depth=*/1);
  312. context.AddInst(SemIR::BranchWithArg{parse_node, resume_block_id, rhs_id});
  313. context.inst_block_stack().Pop();
  314. context.AddCurrentCodeBlockToFunction();
  315. // Collect the result from either the first or second operand.
  316. context.AddInstAndPush(
  317. parse_node,
  318. SemIR::BlockArg{parse_node, context.insts().Get(rhs_id).type_id(),
  319. resume_block_id});
  320. return true;
  321. }
  322. auto HandleShortCircuitOperatorAnd(Context& context, Parse::NodeId parse_node)
  323. -> bool {
  324. return HandleShortCircuitOperator(context, parse_node);
  325. }
  326. auto HandleShortCircuitOperatorOr(Context& context, Parse::NodeId parse_node)
  327. -> bool {
  328. return HandleShortCircuitOperator(context, parse_node);
  329. }
  330. } // namespace Carbon::Check