semantics_handle_statement.cpp 3.1 KB

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