handle_return_statement.cpp 2.0 KB

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