handle_operator.cpp 16 KB

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