pattern.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_PATTERN_H_
  5. #define EXECUTABLE_SEMANTICS_AST_PATTERN_H_
  6. #include <optional>
  7. #include <string>
  8. #include <vector>
  9. #include "common/ostream.h"
  10. #include "executable_semantics/ast/expression.h"
  11. #include "executable_semantics/ast/source_location.h"
  12. #include "llvm/ADT/ArrayRef.h"
  13. namespace Carbon {
  14. class Value;
  15. // Abstract base class of all AST nodes representing patterns.
  16. //
  17. // Pattern and its derived classes support LLVM-style RTTI, including
  18. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  19. // class derived from Pattern must provide a `classof` operation, and
  20. // every concrete derived class must have a corresponding enumerator
  21. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  22. // details.
  23. class Pattern {
  24. public:
  25. enum class Kind {
  26. AutoPattern,
  27. BindingPattern,
  28. TuplePattern,
  29. AlternativePattern,
  30. ExpressionPattern,
  31. };
  32. Pattern(const Pattern&) = delete;
  33. Pattern& operator=(const Pattern&) = delete;
  34. void Print(llvm::raw_ostream& out) const;
  35. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  36. // Returns the enumerator corresponding to the most-derived type of this
  37. // object.
  38. auto kind() const -> Kind { return kind_; }
  39. auto source_loc() const -> SourceLocation { return source_loc_; }
  40. // The static type of this pattern. Cannot be called before typechecking.
  41. auto static_type() const -> const Value& { return **static_type_; }
  42. // Sets the static type of this expression. Can only be called once, during
  43. // typechecking.
  44. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  45. // Returns whether the static type has been set. Should only be called
  46. // during typechecking: before typechecking it's guaranteed to be false,
  47. // and after typechecking it's guaranteed to be true.
  48. auto has_static_type() const -> bool { return static_type_.has_value(); }
  49. // The value of this pattern. Cannot be called before typechecking.
  50. auto value() const -> const Value& { return **value_; }
  51. // Sets the value of this pattern. Can only be called once, during
  52. // typechecking.
  53. void set_value(Nonnull<const Value*> value) { value_ = value; }
  54. // Returns whether the value has been set. Should only be called
  55. // during typechecking: before typechecking it's guaranteed to be false,
  56. // and after typechecking it's guaranteed to be true.
  57. auto has_value() const -> bool { return value_.has_value(); }
  58. protected:
  59. // Constructs a Pattern representing syntax at the given line number.
  60. // `kind` must be the enumerator corresponding to the most-derived type being
  61. // constructed.
  62. Pattern(Kind kind, SourceLocation source_loc)
  63. : kind_(kind), source_loc_(source_loc) {}
  64. private:
  65. const Kind kind_;
  66. SourceLocation source_loc_;
  67. std::optional<Nonnull<const Value*>> static_type_;
  68. std::optional<Nonnull<const Value*>> value_;
  69. };
  70. // A pattern consisting of the `auto` keyword.
  71. class AutoPattern : public Pattern {
  72. public:
  73. explicit AutoPattern(SourceLocation source_loc)
  74. : Pattern(Kind::AutoPattern, source_loc) {}
  75. static auto classof(const Pattern* pattern) -> bool {
  76. return pattern->kind() == Kind::AutoPattern;
  77. }
  78. };
  79. // A pattern that matches a value of a specified type, and optionally binds
  80. // a name to it.
  81. class BindingPattern : public Pattern {
  82. public:
  83. BindingPattern(SourceLocation source_loc, std::optional<std::string> name,
  84. Nonnull<Pattern*> type)
  85. : Pattern(Kind::BindingPattern, source_loc),
  86. name_(std::move(name)),
  87. type_(type) {}
  88. static auto classof(const Pattern* pattern) -> bool {
  89. return pattern->kind() == Kind::BindingPattern;
  90. }
  91. // The name this pattern binds, if any.
  92. auto name() const -> const std::optional<std::string>& { return name_; }
  93. // The pattern specifying the type of values that this pattern matches.
  94. auto type() const -> const Pattern& { return *type_; }
  95. auto type() -> Pattern& { return *type_; }
  96. private:
  97. std::optional<std::string> name_;
  98. Nonnull<Pattern*> type_;
  99. };
  100. // A pattern that matches a tuple value field-wise.
  101. class TuplePattern : public Pattern {
  102. public:
  103. TuplePattern(SourceLocation source_loc, std::vector<Nonnull<Pattern*>> fields)
  104. : Pattern(Kind::TuplePattern, source_loc), fields_(std::move(fields)) {}
  105. static auto classof(const Pattern* pattern) -> bool {
  106. return pattern->kind() == Kind::TuplePattern;
  107. }
  108. auto fields() const -> llvm::ArrayRef<Nonnull<const Pattern*>> {
  109. return fields_;
  110. }
  111. auto fields() -> llvm::ArrayRef<Nonnull<Pattern*>> { return fields_; }
  112. private:
  113. std::vector<Nonnull<Pattern*>> fields_;
  114. };
  115. // Converts paren_contents to a Pattern, interpreting the parentheses as
  116. // grouping if their contents permit that interpretation, or as forming a
  117. // tuple otherwise.
  118. auto PatternFromParenContents(Nonnull<Arena*> arena, SourceLocation source_loc,
  119. const ParenContents<Pattern>& paren_contents)
  120. -> Nonnull<Pattern*>;
  121. // Converts paren_contents to a TuplePattern, interpreting the parentheses as
  122. // forming a tuple.
  123. auto TuplePatternFromParenContents(Nonnull<Arena*> arena,
  124. SourceLocation source_loc,
  125. const ParenContents<Pattern>& paren_contents)
  126. -> Nonnull<TuplePattern*>;
  127. // Converts `contents` to ParenContents<Pattern> by replacing each Expression
  128. // with an ExpressionPattern.
  129. auto ParenExpressionToParenPattern(Nonnull<Arena*> arena,
  130. const ParenContents<Expression>& contents)
  131. -> ParenContents<Pattern>;
  132. // A pattern that matches an alternative of a choice type.
  133. class AlternativePattern : public Pattern {
  134. public:
  135. // Constructs an AlternativePattern that matches a value of the type
  136. // specified by choice_type if it represents an alternative named
  137. // alternative_name, and its arguments match `arguments`.
  138. AlternativePattern(SourceLocation source_loc,
  139. Nonnull<Expression*> choice_type,
  140. std::string alternative_name,
  141. Nonnull<TuplePattern*> arguments)
  142. : Pattern(Kind::AlternativePattern, source_loc),
  143. choice_type_(choice_type),
  144. alternative_name_(std::move(alternative_name)),
  145. arguments_(arguments) {}
  146. // Constructs an AlternativePattern that matches the alternative specified
  147. // by `alternative`, if its arguments match `arguments`.
  148. AlternativePattern(SourceLocation source_loc,
  149. Nonnull<Expression*> alternative,
  150. Nonnull<TuplePattern*> arguments);
  151. static auto classof(const Pattern* pattern) -> bool {
  152. return pattern->kind() == Kind::AlternativePattern;
  153. }
  154. auto choice_type() const -> const Expression& { return *choice_type_; }
  155. auto choice_type() -> Expression& { return *choice_type_; }
  156. auto alternative_name() const -> const std::string& {
  157. return alternative_name_;
  158. }
  159. auto arguments() const -> const TuplePattern& { return *arguments_; }
  160. auto arguments() -> TuplePattern& { return *arguments_; }
  161. private:
  162. Nonnull<Expression*> choice_type_;
  163. std::string alternative_name_;
  164. Nonnull<TuplePattern*> arguments_;
  165. };
  166. // A pattern that matches a value if it is equal to the value of a given
  167. // expression.
  168. class ExpressionPattern : public Pattern {
  169. public:
  170. ExpressionPattern(Nonnull<Expression*> expression)
  171. : Pattern(Kind::ExpressionPattern, expression->source_loc()),
  172. expression_(expression) {}
  173. static auto classof(const Pattern* pattern) -> bool {
  174. return pattern->kind() == Kind::ExpressionPattern;
  175. }
  176. auto expression() const -> const Expression& { return *expression_; }
  177. auto expression() -> Expression& { return *expression_; }
  178. private:
  179. Nonnull<Expression*> expression_;
  180. };
  181. } // namespace Carbon
  182. #endif // EXECUTABLE_SEMANTICS_AST_PATTERN_H_