function.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // TODO: StatementBlock has some circularity in its forward declarations that
  14. // needs to be fixed. These includes are a workaround.
  15. #include "toolchain/semantics/nodes/expression_statement.h"
  16. #include "toolchain/semantics/nodes/return.h"
  17. namespace Carbon::Semantics {
  18. // Represents `fn name(params...) [-> return_expr] body`.
  19. class Function {
  20. public:
  21. static constexpr DeclarationKind MetaNodeKind = DeclarationKind::Function;
  22. Function(ParseTree::Node node, DeclaredName name,
  23. llvm::SmallVector<PatternBinding, 0> params,
  24. llvm::Optional<Semantics::Expression> return_expr,
  25. StatementBlock body)
  26. : node_(node),
  27. name_(name),
  28. params_(std::move(params)),
  29. return_expr_(return_expr),
  30. body_(std::move(body)) {}
  31. auto node() const -> ParseTree::Node { return node_; }
  32. auto name() const -> const DeclaredName& { return name_; }
  33. auto params() const -> llvm::ArrayRef<PatternBinding> { return params_; }
  34. auto return_expr() const -> llvm::Optional<Semantics::Expression> {
  35. return return_expr_;
  36. }
  37. auto body() const -> const StatementBlock& { return body_; }
  38. private:
  39. // The FunctionDeclaration node.
  40. ParseTree::Node node_;
  41. // The function's name.
  42. DeclaredName name_;
  43. // Regular function parameters.
  44. llvm::SmallVector<PatternBinding, 0> params_;
  45. // The return expression.
  46. llvm::Optional<Semantics::Expression> return_expr_;
  47. StatementBlock body_;
  48. };
  49. } // namespace Carbon::Semantics
  50. #endif // CARBON_TOOLCHAIN_SEMANTICS_NODES_FUNCTION_H_