handle_operator.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. auto type_id = context.insts().Get(value_id).type_id();
  175. // Only durable reference expressions can have their address taken.
  176. switch (SemIR::GetExprCategory(context.sem_ir(), value_id)) {
  177. case SemIR::ExprCategory::DurableRef:
  178. case SemIR::ExprCategory::Error:
  179. break;
  180. case SemIR::ExprCategory::EphemeralRef:
  181. CARBON_DIAGNOSTIC(AddrOfEphemeralRef, Error,
  182. "Cannot take the address of a temporary object.");
  183. context.emitter().Emit(TokenOnly(parse_node), AddrOfEphemeralRef);
  184. value_id = SemIR::InstId::BuiltinError;
  185. break;
  186. default:
  187. CARBON_DIAGNOSTIC(AddrOfNonRef, Error,
  188. "Cannot take the address of non-reference expression.");
  189. context.emitter().Emit(TokenOnly(parse_node), AddrOfNonRef);
  190. value_id = SemIR::InstId::BuiltinError;
  191. break;
  192. }
  193. context.AddInstAndPush(
  194. {parse_node, SemIR::AddrOf{context.GetPointerType(type_id), value_id}});
  195. return true;
  196. }
  197. auto HandlePrefixOperatorCaret(Context& context,
  198. Parse::PrefixOperatorCaretId parse_node)
  199. -> bool {
  200. return context.TODO(parse_node, "HandlePrefixOperatorCaret");
  201. }
  202. auto HandlePrefixOperatorConst(Context& context,
  203. Parse::PrefixOperatorConstId parse_node)
  204. -> bool {
  205. auto value_id = context.node_stack().PopExpr();
  206. // `const (const T)` is probably not what the developer intended.
  207. // TODO: Detect `const (const T)*` and suggest moving the `*` inside the
  208. // parentheses.
  209. if (context.insts().Get(value_id).kind() == SemIR::ConstType::Kind) {
  210. CARBON_DIAGNOSTIC(RepeatedConst, Warning,
  211. "`const` applied repeatedly to the same type has no "
  212. "additional effect.");
  213. context.emitter().Emit(parse_node, RepeatedConst);
  214. }
  215. auto inner_type_id = ExprAsType(context, parse_node, value_id);
  216. context.AddInstAndPush(
  217. {parse_node, SemIR::ConstType{SemIR::TypeId::TypeType, inner_type_id}});
  218. return true;
  219. }
  220. auto HandlePrefixOperatorMinus(Context& context,
  221. Parse::PrefixOperatorMinusId parse_node)
  222. -> bool {
  223. return context.TODO(parse_node, "HandlePrefixOperatorMinus");
  224. }
  225. auto HandlePrefixOperatorMinusMinus(
  226. Context& context, Parse::PrefixOperatorMinusMinusId parse_node) -> bool {
  227. return context.TODO(parse_node, "HandlePrefixOperatorMinusMinus");
  228. }
  229. auto HandlePrefixOperatorNot(Context& context,
  230. Parse::PrefixOperatorNotId parse_node) -> bool {
  231. auto value_id = context.node_stack().PopExpr();
  232. value_id = ConvertToBoolValue(context, parse_node, value_id);
  233. context.AddInstAndPush(
  234. {parse_node, SemIR::UnaryOperatorNot{
  235. context.insts().Get(value_id).type_id(), value_id}});
  236. return true;
  237. }
  238. auto HandlePrefixOperatorPlusPlus(Context& context,
  239. Parse::PrefixOperatorPlusPlusId parse_node)
  240. -> bool {
  241. return context.TODO(parse_node, "HandlePrefixOperatorPlusPlus");
  242. }
  243. auto HandlePrefixOperatorStar(Context& context,
  244. Parse::PrefixOperatorStarId parse_node) -> bool {
  245. auto value_id = context.node_stack().PopExpr();
  246. value_id = ConvertToValueExpr(context, value_id);
  247. auto type_id =
  248. context.GetUnqualifiedType(context.insts().Get(value_id).type_id());
  249. auto result_type_id = SemIR::TypeId::Error;
  250. if (auto pointer_type =
  251. context.types().TryGetAs<SemIR::PointerType>(type_id)) {
  252. result_type_id = pointer_type->pointee_id;
  253. } else if (type_id != SemIR::TypeId::Error) {
  254. CARBON_DIAGNOSTIC(DerefOfNonPointer, Error,
  255. "Cannot dereference operand of non-pointer type `{0}`.",
  256. std::string);
  257. auto builder =
  258. context.emitter().Build(TokenOnly(parse_node), DerefOfNonPointer,
  259. context.sem_ir().StringifyType(type_id));
  260. // TODO: Check for any facet here, rather than only a type.
  261. if (type_id == SemIR::TypeId::TypeType) {
  262. CARBON_DIAGNOSTIC(
  263. DerefOfType, Note,
  264. "To form a pointer type, write the `*` after the pointee type.");
  265. builder.Note(TokenOnly(parse_node), DerefOfType);
  266. }
  267. builder.Emit();
  268. }
  269. context.AddInstAndPush({parse_node, SemIR::Deref{result_type_id, value_id}});
  270. return true;
  271. }
  272. // Adds the branch for a short circuit operand.
  273. static auto HandleShortCircuitOperand(Context& context,
  274. Parse::NodeId parse_node, bool is_or)
  275. -> bool {
  276. // Convert the condition to `bool`.
  277. auto cond_value_id = context.node_stack().PopExpr();
  278. cond_value_id = ConvertToBoolValue(context, parse_node, cond_value_id);
  279. auto bool_type_id = context.insts().Get(cond_value_id).type_id();
  280. // Compute the branch value: the condition for `and`, inverted for `or`.
  281. SemIR::InstId branch_value_id =
  282. is_or ? context.AddInst(
  283. {parse_node,
  284. SemIR::UnaryOperatorNot{bool_type_id, cond_value_id}})
  285. : cond_value_id;
  286. auto short_circuit_result_id = context.AddInst(
  287. {parse_node,
  288. SemIR::BoolLiteral{bool_type_id, is_or ? SemIR::BoolValue::True
  289. : SemIR::BoolValue::False}});
  290. // Create a block for the right-hand side and for the continuation.
  291. auto rhs_block_id =
  292. context.AddDominatedBlockAndBranchIf(parse_node, branch_value_id);
  293. auto end_block_id = context.AddDominatedBlockAndBranchWithArg(
  294. parse_node, short_circuit_result_id);
  295. // Push the resumption and the right-hand side blocks, and start emitting the
  296. // right-hand operand.
  297. context.inst_block_stack().Pop();
  298. context.inst_block_stack().Push(end_block_id);
  299. context.inst_block_stack().Push(rhs_block_id);
  300. context.AddCurrentCodeBlockToFunction(parse_node);
  301. // HandleShortCircuitOperator will follow, and doesn't need the operand on the
  302. // node stack.
  303. return true;
  304. }
  305. auto HandleShortCircuitOperandAnd(Context& context,
  306. Parse::ShortCircuitOperandAndId parse_node)
  307. -> bool {
  308. return HandleShortCircuitOperand(context, parse_node, /*is_or=*/false);
  309. }
  310. auto HandleShortCircuitOperandOr(Context& context,
  311. Parse::ShortCircuitOperandOrId parse_node)
  312. -> bool {
  313. return HandleShortCircuitOperand(context, parse_node, /*is_or=*/true);
  314. }
  315. // Short circuit operator handling is uniform because the branching logic
  316. // occurs during operand handling.
  317. static auto HandleShortCircuitOperator(Context& context,
  318. Parse::NodeId parse_node) -> bool {
  319. auto [rhs_node, rhs_id] = context.node_stack().PopExprWithParseNode();
  320. // The first operand is wrapped in a ShortCircuitOperand, which we
  321. // already handled by creating a RHS block and a resumption block, which
  322. // are the current block and its enclosing block.
  323. rhs_id = ConvertToBoolValue(context, parse_node, rhs_id);
  324. // When the second operand is evaluated, the result of `and` and `or` is
  325. // its value.
  326. auto resume_block_id = context.inst_block_stack().PeekOrAdd(/*depth=*/1);
  327. context.AddInst({parse_node, SemIR::BranchWithArg{resume_block_id, rhs_id}});
  328. context.inst_block_stack().Pop();
  329. context.AddCurrentCodeBlockToFunction(parse_node);
  330. // Collect the result from either the first or second operand.
  331. context.AddInstAndPush(
  332. {parse_node, SemIR::BlockArg{context.insts().Get(rhs_id).type_id(),
  333. resume_block_id}});
  334. return true;
  335. }
  336. auto HandleShortCircuitOperatorAnd(Context& context,
  337. Parse::ShortCircuitOperatorAndId parse_node)
  338. -> bool {
  339. return HandleShortCircuitOperator(context, parse_node);
  340. }
  341. auto HandleShortCircuitOperatorOr(Context& context,
  342. Parse::ShortCircuitOperatorOrId parse_node)
  343. -> bool {
  344. return HandleShortCircuitOperator(context, parse_node);
  345. }
  346. } // namespace Carbon::Check