handle_statement.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "toolchain/sem_ir/inst.h"
  7. namespace Carbon::Check {
  8. // TODO: Find a better home for this. We'll likely need it for more than just
  9. // expression statements.
  10. static auto HandleDiscardedExpr(Context& context, SemIR::InstId expr_id)
  11. -> void {
  12. // If we discard an initializing expression, convert it to a value or
  13. // reference so that it has something to initialize.
  14. auto expr = context.insts().Get(expr_id);
  15. Convert(context, expr.parse_node(), expr_id,
  16. {.kind = ConversionTarget::Discarded, .type_id = expr.type_id()});
  17. // TODO: This will eventually need to do some "do not discard" analysis.
  18. }
  19. auto HandleExprStatement(Context& context, Parse::Node /*parse_node*/) -> bool {
  20. HandleDiscardedExpr(context, context.node_stack().PopExpr());
  21. return true;
  22. }
  23. auto HandleReturnStatement(Context& context, Parse::Node parse_node) -> bool {
  24. CARBON_CHECK(!context.return_scope_stack().empty());
  25. auto fn_inst = context.insts().GetAs<SemIR::FunctionDecl>(
  26. context.return_scope_stack().back());
  27. const auto& callable = context.functions().Get(fn_inst.function_id);
  28. if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
  29. Parse::NodeKind::ReturnStatementStart) {
  30. context.node_stack()
  31. .PopAndDiscardSoloParseNode<Parse::NodeKind::ReturnStatementStart>();
  32. if (callable.return_type_id.is_valid()) {
  33. // TODO: Add a note pointing at the return type's parse node.
  34. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error, "Must return a {0}.",
  35. std::string);
  36. context.emitter()
  37. .Build(parse_node, ReturnStatementMissingExpr,
  38. context.sem_ir().StringifyType(callable.return_type_id))
  39. .Emit();
  40. }
  41. context.AddInst(SemIR::Return{parse_node});
  42. } else {
  43. auto arg = context.node_stack().PopExpr();
  44. context.node_stack()
  45. .PopAndDiscardSoloParseNode<Parse::NodeKind::ReturnStatementStart>();
  46. if (!callable.return_type_id.is_valid()) {
  47. CARBON_DIAGNOSTIC(
  48. ReturnStatementDisallowExpr, Error,
  49. "No return expression should be provided in this context.");
  50. CARBON_DIAGNOSTIC(ReturnStatementImplicitNote, Note,
  51. "There was no return type provided.");
  52. context.emitter()
  53. .Build(parse_node, ReturnStatementDisallowExpr)
  54. .Note(fn_inst.parse_node, ReturnStatementImplicitNote)
  55. .Emit();
  56. } else if (callable.return_slot_id.is_valid()) {
  57. arg = Initialize(context, parse_node, callable.return_slot_id, arg);
  58. } else {
  59. arg = ConvertToValueOfType(context, parse_node, arg,
  60. callable.return_type_id);
  61. }
  62. context.AddInst(SemIR::ReturnExpr{parse_node, arg});
  63. }
  64. // Switch to a new, unreachable, empty instruction block. This typically won't
  65. // contain any semantics IR, but it can do if there are statements following
  66. // the `return` statement.
  67. context.inst_block_stack().Pop();
  68. context.inst_block_stack().PushUnreachable();
  69. return true;
  70. }
  71. auto HandleReturnStatementStart(Context& context, Parse::Node parse_node)
  72. -> bool {
  73. // No action, just a bracketing node.
  74. context.node_stack().Push(parse_node);
  75. return true;
  76. }
  77. } // namespace Carbon::Check