function_definition.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. class Value;
  15. // TODO: expand the kinds of things that can be deduced parameters.
  16. // For now, only generic parameters are supported.
  17. struct GenericBinding {
  18. std::string name;
  19. Nonnull<const Expression*> type;
  20. };
  21. class FunctionDefinition {
  22. public:
  23. FunctionDefinition(SourceLocation source_loc, std::string name,
  24. std::vector<GenericBinding> deduced_params,
  25. Nonnull<TuplePattern*> param_pattern,
  26. Nonnull<Pattern*> return_type, bool is_omitted_return_type,
  27. std::optional<Nonnull<Statement*>> body)
  28. : source_loc_(source_loc),
  29. name_(std::move(name)),
  30. deduced_parameters_(std::move(deduced_params)),
  31. param_pattern_(param_pattern),
  32. return_type_(return_type),
  33. is_omitted_return_type_(is_omitted_return_type),
  34. body_(body) {}
  35. void Print(llvm::raw_ostream& out) const { PrintDepth(-1, out); }
  36. void PrintDepth(int depth, llvm::raw_ostream& out) const;
  37. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  38. auto source_loc() const -> SourceLocation { return source_loc_; }
  39. auto name() const -> const std::string& { return name_; }
  40. auto deduced_parameters() const -> llvm::ArrayRef<GenericBinding> {
  41. return deduced_parameters_;
  42. }
  43. auto param_pattern() const -> const TuplePattern& { return *param_pattern_; }
  44. auto param_pattern() -> TuplePattern& { return *param_pattern_; }
  45. auto return_type() const -> const Pattern& { return *return_type_; }
  46. auto return_type() -> Pattern& { return *return_type_; }
  47. auto is_omitted_return_type() const -> bool {
  48. return is_omitted_return_type_;
  49. }
  50. auto body() const -> std::optional<Nonnull<const Statement*>> {
  51. return body_;
  52. }
  53. auto body() -> std::optional<Nonnull<Statement*>> { return body_; }
  54. // The static type of this function. Cannot be called before typechecking.
  55. auto static_type() const -> const Value& { return **static_type_; }
  56. // Sets the static type of this expression. Can only be called once, during
  57. // typechecking.
  58. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  59. // Returns whether the static type has been set. Should only be called
  60. // during typechecking: before typechecking it's guaranteed to be false,
  61. // and after typechecking it's guaranteed to be true.
  62. auto has_static_type() const -> bool { return static_type_.has_value(); }
  63. private:
  64. SourceLocation source_loc_;
  65. std::string name_;
  66. std::vector<GenericBinding> deduced_parameters_;
  67. Nonnull<TuplePattern*> param_pattern_;
  68. Nonnull<Pattern*> return_type_;
  69. bool is_omitted_return_type_;
  70. std::optional<Nonnull<Statement*>> body_;
  71. std::optional<Nonnull<const Value*>> static_type_;
  72. };
  73. } // namespace Carbon
  74. #endif // EXECUTABLE_SEMANTICS_AST_FUNCTION_DEFINITION_H_