return_term.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_RETURN_TERM_H_
  5. #define EXECUTABLE_SEMANTICS_AST_RETURN_TERM_H_
  6. #include <optional>
  7. #include <utility>
  8. #include "common/check.h"
  9. #include "common/ostream.h"
  10. #include "executable_semantics/ast/expression.h"
  11. #include "executable_semantics/ast/source_location.h"
  12. #include "executable_semantics/common/nonnull.h"
  13. namespace Carbon {
  14. class Value;
  15. // The syntactic representation of a function declaration's return type.
  16. // This syntax can take one of three forms:
  17. // - An _explicit_ term consists of `->` followed by a type expression.
  18. // - An _auto_ term consists of `-> auto`.
  19. // - An _omitted_ term consists of no tokens at all.
  20. // Each of these forms has a corresponding factory function.
  21. class ReturnTerm {
  22. public:
  23. ReturnTerm(const ReturnTerm&) = default;
  24. auto operator=(const ReturnTerm&) -> ReturnTerm& = default;
  25. // Represents an omitted return term at `source_loc`.
  26. static auto Omitted(SourceLocation source_loc) -> ReturnTerm {
  27. return ReturnTerm(ReturnKind::Omitted, source_loc);
  28. }
  29. // Represents an auto return term at `source_loc`.
  30. static auto Auto(SourceLocation source_loc) -> ReturnTerm {
  31. return ReturnTerm(ReturnKind::Auto, source_loc);
  32. }
  33. // Represents an explicit return term with the given type expression.
  34. static auto Explicit(Nonnull<Expression*> type_expression) -> ReturnTerm {
  35. return ReturnTerm(type_expression);
  36. }
  37. // Returns true if this represents an omitted return term.
  38. auto is_omitted() const -> bool { return kind_ == ReturnKind::Omitted; }
  39. // Returns true if this represents an auto return term.
  40. auto is_auto() const -> bool { return kind_ == ReturnKind::Auto; }
  41. // If this represents an explicit return term, returns the type expression.
  42. // Otherwise, returns nullopt.
  43. auto type_expression() const -> std::optional<Nonnull<const Expression*>> {
  44. return type_expression_;
  45. }
  46. auto type_expression() -> std::optional<Nonnull<Expression*>> {
  47. return type_expression_;
  48. }
  49. // The static return type this term resolves to. Cannot be called before
  50. // typechecking.
  51. auto static_type() const -> const Value& { return **static_type_; }
  52. // Sets the value of static_type(). Can only be called once, during
  53. // typechecking.
  54. void set_static_type(Nonnull<const Value*> type) {
  55. CHECK(!static_type_.has_value());
  56. static_type_ = type;
  57. }
  58. auto source_loc() const -> SourceLocation { return source_loc_; }
  59. void Print(llvm::raw_ostream& out) const;
  60. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  61. private:
  62. enum class ReturnKind { Omitted, Auto, Expression };
  63. explicit ReturnTerm(ReturnKind kind, SourceLocation source_loc)
  64. : kind_(kind), source_loc_(source_loc) {
  65. CHECK(kind != ReturnKind::Expression);
  66. }
  67. explicit ReturnTerm(Nonnull<Expression*> type_expression)
  68. : kind_(ReturnKind::Expression),
  69. type_expression_(type_expression),
  70. source_loc_(type_expression->source_loc()) {}
  71. ReturnKind kind_;
  72. std::optional<Nonnull<Expression*>> type_expression_;
  73. std::optional<Nonnull<const Value*>> static_type_;
  74. SourceLocation source_loc_;
  75. };
  76. } // namespace Carbon
  77. #endif // EXECUTABLE_SEMANTICS_AST_RETURN_TERM_H_