semantics_handle_statement.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/semantics/semantics_context.h"
  5. #include "toolchain/semantics/semantics_node.h"
  6. namespace Carbon::Check {
  7. auto HandleExpressionStatement(Context& context, Parse::Node /*parse_node*/)
  8. -> bool {
  9. context.HandleDiscardedExpression(context.node_stack().PopExpression());
  10. return true;
  11. }
  12. auto HandleReturnStatement(Context& context, Parse::Node parse_node) -> bool {
  13. CARBON_CHECK(!context.return_scope_stack().empty());
  14. const auto& fn_node =
  15. context.semantics_ir().GetNode(context.return_scope_stack().back());
  16. const auto& callable =
  17. context.semantics_ir().GetFunction(fn_node.GetAsFunctionDeclaration());
  18. if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
  19. Parse::NodeKind::ReturnStatementStart) {
  20. context.node_stack()
  21. .PopAndDiscardSoloParseNode<Parse::NodeKind::ReturnStatementStart>();
  22. if (callable.return_type_id.is_valid()) {
  23. // TODO: Add a note pointing at the return type's parse node.
  24. CARBON_DIAGNOSTIC(ReturnStatementMissingExpression, Error,
  25. "Must return a {0}.", std::string);
  26. context.emitter()
  27. .Build(parse_node, ReturnStatementMissingExpression,
  28. context.semantics_ir().StringifyType(callable.return_type_id))
  29. .Emit();
  30. }
  31. context.AddNode(SemIR::Node::Return::Make(parse_node));
  32. } else {
  33. auto arg = context.node_stack().PopExpression();
  34. context.node_stack()
  35. .PopAndDiscardSoloParseNode<Parse::NodeKind::ReturnStatementStart>();
  36. if (!callable.return_type_id.is_valid()) {
  37. CARBON_DIAGNOSTIC(
  38. ReturnStatementDisallowExpression, Error,
  39. "No return expression should be provided in this context.");
  40. CARBON_DIAGNOSTIC(ReturnStatementImplicitNote, Note,
  41. "There was no return type provided.");
  42. context.emitter()
  43. .Build(parse_node, ReturnStatementDisallowExpression)
  44. .Note(fn_node.parse_node(), ReturnStatementImplicitNote)
  45. .Emit();
  46. } else if (callable.return_slot_id.is_valid()) {
  47. arg = context.Initialize(parse_node, callable.return_slot_id, arg);
  48. } else {
  49. arg = context.ConvertToValueOfType(parse_node, arg,
  50. callable.return_type_id);
  51. }
  52. context.AddNode(SemIR::Node::ReturnExpression::Make(parse_node, arg));
  53. }
  54. // Switch to a new, unreachable, empty node block. This typically won't
  55. // contain any semantics IR, but it can do if there are statements following
  56. // the `return` statement.
  57. context.node_block_stack().Pop();
  58. context.node_block_stack().PushUnreachable();
  59. return true;
  60. }
  61. auto HandleReturnStatementStart(Context& context, Parse::Node parse_node)
  62. -> bool {
  63. // No action, just a bracketing node.
  64. context.node_stack().Push(parse_node);
  65. return true;
  66. }
  67. } // namespace Carbon::Check