handle_return_statement.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/return.h"
  6. namespace Carbon::Check {
  7. auto HandleReturnStatementStart(Context& context, Parse::NodeId parse_node)
  8. -> bool {
  9. // No action, just a bracketing node.
  10. context.node_stack().Push(parse_node);
  11. return true;
  12. }
  13. auto HandleReturnVarModifier(Context& context, Parse::NodeId parse_node)
  14. -> bool {
  15. // No action, just a bracketing node.
  16. context.node_stack().Push(parse_node);
  17. return true;
  18. }
  19. auto HandleReturnStatement(Context& context, Parse::NodeId parse_node) -> bool {
  20. switch (context.node_stack().PeekParseNodeKind()) {
  21. case Parse::NodeKind::ReturnStatementStart:
  22. // This is a `return;` statement.
  23. context.node_stack()
  24. .PopAndDiscardSoloParseNode<Parse::NodeKind::ReturnStatementStart>();
  25. BuildReturnWithNoExpr(context, parse_node);
  26. break;
  27. case Parse::NodeKind::ReturnVarModifier:
  28. // This is a `return var;` statement.
  29. context.node_stack()
  30. .PopAndDiscardSoloParseNode<Parse::NodeKind::ReturnVarModifier>();
  31. context.node_stack()
  32. .PopAndDiscardSoloParseNode<Parse::NodeKind::ReturnStatementStart>();
  33. BuildReturnVar(context, parse_node);
  34. break;
  35. default:
  36. // This is a `return <expression>;` statement.
  37. auto expr_id = context.node_stack().PopExpr();
  38. context.node_stack()
  39. .PopAndDiscardSoloParseNode<Parse::NodeKind::ReturnStatementStart>();
  40. BuildReturnWithExpr(context, parse_node, expr_id);
  41. break;
  42. }
  43. // Switch to a new, unreachable, empty instruction block. This typically won't
  44. // contain any semantics IR, but it can do if there are statements following
  45. // the `return` statement.
  46. context.inst_block_stack().Pop();
  47. context.inst_block_stack().PushUnreachable();
  48. return true;
  49. }
  50. } // namespace Carbon::Check