function_definition.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 EXECUTABLE_SEMANTICS_AST_FUNCTION_DEFINITION_H_
  5. #define EXECUTABLE_SEMANTICS_AST_FUNCTION_DEFINITION_H_
  6. #include "common/ostream.h"
  7. #include "executable_semantics/ast/expression.h"
  8. #include "executable_semantics/ast/pattern.h"
  9. #include "executable_semantics/ast/source_location.h"
  10. #include "executable_semantics/ast/statement.h"
  11. #include "llvm/ADT/ArrayRef.h"
  12. #include "llvm/Support/Compiler.h"
  13. namespace Carbon {
  14. // TODO: expand the kinds of things that can be deduced parameters.
  15. // For now, only generic parameters are supported.
  16. struct GenericBinding {
  17. std::string name;
  18. Nonnull<const Expression*> type;
  19. };
  20. class FunctionDefinition {
  21. public:
  22. FunctionDefinition(SourceLocation source_loc, std::string name,
  23. std::vector<GenericBinding> deduced_params,
  24. Nonnull<TuplePattern*> param_pattern,
  25. Nonnull<Pattern*> return_type, bool is_omitted_return_type,
  26. std::optional<Nonnull<Statement*>> body)
  27. : source_loc_(source_loc),
  28. name_(std::move(name)),
  29. deduced_parameters_(std::move(deduced_params)),
  30. param_pattern_(param_pattern),
  31. return_type_(return_type),
  32. is_omitted_return_type_(is_omitted_return_type),
  33. body_(body) {}
  34. void Print(llvm::raw_ostream& out) const { PrintDepth(-1, out); }
  35. void PrintDepth(int depth, llvm::raw_ostream& out) const;
  36. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  37. auto source_loc() const -> SourceLocation { return source_loc_; }
  38. auto name() const -> const std::string& { return name_; }
  39. auto deduced_parameters() const -> llvm::ArrayRef<GenericBinding> {
  40. return deduced_parameters_;
  41. }
  42. auto param_pattern() const -> const TuplePattern& { return *param_pattern_; }
  43. auto param_pattern() -> TuplePattern& { return *param_pattern_; }
  44. auto return_type() const -> const Pattern& { return *return_type_; }
  45. auto is_omitted_return_type() const -> bool {
  46. return is_omitted_return_type_;
  47. }
  48. auto body() const -> std::optional<Nonnull<const Statement*>> {
  49. return body_;
  50. }
  51. auto body() -> std::optional<Nonnull<Statement*>> { return body_; }
  52. private:
  53. SourceLocation source_loc_;
  54. std::string name_;
  55. std::vector<GenericBinding> deduced_parameters_;
  56. Nonnull<TuplePattern*> param_pattern_;
  57. Nonnull<Pattern*> return_type_;
  58. bool is_omitted_return_type_;
  59. std::optional<Nonnull<Statement*>> body_;
  60. };
  61. } // namespace Carbon
  62. #endif // EXECUTABLE_SEMANTICS_AST_FUNCTION_DEFINITION_H_