return.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. #ifndef CARBON_TOOLCHAIN_SEMANTICS_NODES_RETURN_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_NODES_RETURN_H_
  6. #include "common/ostream.h"
  7. #include "llvm/ADT/SmallVector.h"
  8. #include "llvm/ADT/StringExtras.h"
  9. #include "toolchain/parser/parse_tree.h"
  10. #include "toolchain/semantics/node_kind.h"
  11. #include "toolchain/semantics/node_ref.h"
  12. namespace Carbon::Semantics {
  13. // Represents `return [expr];`
  14. class Return {
  15. public:
  16. static constexpr NodeKind Kind = NodeKind::Return;
  17. Return(ParseTree::Node node, llvm::Optional<int32_t> target_id)
  18. : node_(node), target_id_(target_id) {}
  19. void Print(llvm::raw_ostream& out) const {
  20. out << "Return(";
  21. if (target_id_) {
  22. out << "%" << *target_id_;
  23. } else {
  24. out << "None";
  25. }
  26. out << ")";
  27. }
  28. auto node() const -> ParseTree::Node { return node_; }
  29. auto target_id() const -> const llvm::Optional<int32_t>& {
  30. return target_id_;
  31. }
  32. private:
  33. ParseTree::Node node_;
  34. llvm::Optional<int32_t> target_id_;
  35. };
  36. } // namespace Carbon::Semantics
  37. #endif // CARBON_TOOLCHAIN_SEMANTICS_NODES_RETURN_H_