handle_operator.cpp 15 KB

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