function.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_FUNCTION_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_NODES_FUNCTION_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 `fn name(params...) [-> return_expr] body`.
  14. class Function {
  15. public:
  16. static constexpr NodeKind Kind = NodeKind::Function;
  17. Function(ParseTree::Node node, NodeId id, llvm::SmallVector<NodeRef> body)
  18. : node_(node), id_(id), body_(std::move(body)) {}
  19. void Print(
  20. llvm::raw_ostream& out, int indent,
  21. std::function<void(int, llvm::ArrayRef<NodeRef>)> print_block) const {
  22. out << "Function(\n";
  23. int content_intent = indent + 4;
  24. out.indent(content_intent);
  25. out << id_ << ",\n";
  26. out.indent(content_intent);
  27. print_block(content_intent, body_);
  28. out << ")";
  29. }
  30. auto node() const -> ParseTree::Node { return node_; }
  31. auto id() const -> NodeId { return id_; }
  32. auto set_body(llvm::SmallVector<NodeRef, 0> body) { body_ = std::move(body); }
  33. auto body() const -> llvm::ArrayRef<NodeRef> { return body_; }
  34. private:
  35. // The FunctionDeclaration node.
  36. ParseTree::Node node_;
  37. // The function's ID.
  38. NodeId id_;
  39. llvm::SmallVector<NodeRef> body_;
  40. };
  41. } // namespace Carbon::Semantics
  42. #endif // CARBON_TOOLCHAIN_SEMANTICS_NODES_FUNCTION_H_