function.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 "toolchain/parser/parse_tree.h"
  9. #include "toolchain/semantics/meta_node.h"
  10. #include "toolchain/semantics/meta_node_block.h"
  11. #include "toolchain/semantics/nodes/declared_name.h"
  12. #include "toolchain/semantics/nodes/pattern_binding.h"
  13. namespace Carbon::Semantics {
  14. // Represents `fn name(params...) [-> return_expr] body`.
  15. class Function {
  16. public:
  17. static constexpr DeclarationKind MetaNodeKind = DeclarationKind::Function;
  18. Function(ParseTree::Node node, DeclaredName name,
  19. llvm::SmallVector<PatternBinding, 0> params,
  20. llvm::Optional<Semantics::Expression> return_expr,
  21. StatementBlock body)
  22. : node_(node),
  23. name_(name),
  24. params_(std::move(params)),
  25. return_expr_(return_expr),
  26. body_(std::move(body)) {}
  27. auto node() const -> ParseTree::Node { return node_; }
  28. auto name() const -> const DeclaredName& { return name_; }
  29. auto params() const -> llvm::ArrayRef<PatternBinding> { return params_; }
  30. auto return_expr() const -> llvm::Optional<Semantics::Expression> {
  31. return return_expr_;
  32. }
  33. auto body() const -> const StatementBlock& { return body_; }
  34. private:
  35. // The FunctionDeclaration node.
  36. ParseTree::Node node_;
  37. // The function's name.
  38. DeclaredName name_;
  39. // Regular function parameters.
  40. llvm::SmallVector<PatternBinding, 0> params_;
  41. // The return expression.
  42. llvm::Optional<Semantics::Expression> return_expr_;
  43. StatementBlock body_;
  44. };
  45. } // namespace Carbon::Semantics
  46. #endif // CARBON_TOOLCHAIN_SEMANTICS_NODES_FUNCTION_H_