function.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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, int32_t id,
  18. // llvm::SmallVector<PatternBinding, 0> params,
  19. // llvm::SmallVector<NodeRef, 0> return_type,
  20. llvm::SmallVector<NodeRef, 0> body)
  21. : node_(node),
  22. id_(id),
  23. // params_(std::move(params)),
  24. // return_expr_(return_expr),
  25. body_(std::move(body)) {}
  26. void Print(llvm::raw_ostream& out,
  27. std::function<void(NodeRef)> print_node_ref) const {
  28. out << "Function(%" << id_ << ", {";
  29. llvm::ListSeparator sep(", ");
  30. for (auto& node_ref : body_) {
  31. out << sep;
  32. print_node_ref(node_ref);
  33. }
  34. out << "})";
  35. }
  36. auto node() const -> ParseTree::Node { return node_; }
  37. auto id() const -> int32_t { return id_; }
  38. // auto params() const -> llvm::ArrayRef<PatternBinding> { return params_; }
  39. // auto return_expr() const -> llvm::Optional<Statement> { return
  40. // return_expr_; }
  41. auto body() const -> llvm::ArrayRef<NodeRef> { return body_; }
  42. private:
  43. // The FunctionDeclaration node.
  44. ParseTree::Node node_;
  45. // The function's ID.
  46. int32_t id_;
  47. // Regular function parameters.
  48. // llvm::SmallVector<PatternBinding, 0> params_;
  49. // The return type expression.
  50. llvm::SmallVector<NodeRef, 0> return_type_;
  51. llvm::SmallVector<NodeRef, 0> body_;
  52. };
  53. } // namespace Carbon::Semantics
  54. #endif // CARBON_TOOLCHAIN_SEMANTICS_NODES_FUNCTION_H_