pattern.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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/ast_node.h"
  11. #include "executable_semantics/ast/ast_rtti.h"
  12. #include "executable_semantics/ast/expression.h"
  13. #include "executable_semantics/ast/source_location.h"
  14. #include "executable_semantics/ast/static_scope.h"
  15. #include "executable_semantics/ast/value_category.h"
  16. #include "llvm/ADT/ArrayRef.h"
  17. namespace Carbon {
  18. class Value;
  19. // Abstract base class of all AST nodes representing patterns.
  20. //
  21. // Pattern and its derived classes support LLVM-style RTTI, including
  22. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  23. // class derived from Pattern must provide a `classof` operation, and
  24. // every concrete derived class must have a corresponding enumerator
  25. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  26. // details.
  27. class Pattern : public AstNode {
  28. public:
  29. Pattern(const Pattern&) = delete;
  30. auto operator=(const Pattern&) -> Pattern& = delete;
  31. ~Pattern() override = 0;
  32. void Print(llvm::raw_ostream& out) const override;
  33. static auto classof(const AstNode* node) -> bool {
  34. return InheritsFromPattern(node->kind());
  35. }
  36. // Returns the enumerator corresponding to the most-derived type of this
  37. // object.
  38. auto kind() const -> PatternKind {
  39. return static_cast<PatternKind>(root_kind());
  40. }
  41. // The static type of this pattern. Cannot be called before typechecking.
  42. auto static_type() const -> const Value& { return **static_type_; }
  43. // Sets the static type of this expression. Can only be called once, during
  44. // typechecking.
  45. void set_static_type(Nonnull<const Value*> type) {
  46. CHECK(!static_type_.has_value());
  47. static_type_ = type;
  48. }
  49. // The value of this pattern. Cannot be called before typechecking.
  50. // TODO rename to avoid confusion with BindingPattern::constant_value
  51. auto value() const -> const Value& { return **value_; }
  52. // Sets the value of this pattern. Can only be called once, during
  53. // typechecking.
  54. void set_value(Nonnull<const Value*> value) { value_ = value; }
  55. // Returns whether the value has been set. Should only be called
  56. // during typechecking: before typechecking it's guaranteed to be false,
  57. // and after typechecking it's guaranteed to be true.
  58. auto has_value() const -> bool { return value_.has_value(); }
  59. protected:
  60. // Constructs a Pattern representing syntax at the given line number.
  61. // `kind` must be the enumerator corresponding to the most-derived type being
  62. // constructed.
  63. Pattern(AstNodeKind kind, SourceLocation source_loc)
  64. : AstNode(kind, source_loc) {}
  65. private:
  66. std::optional<Nonnull<const Value*>> static_type_;
  67. std::optional<Nonnull<const Value*>> value_;
  68. };
  69. class BindingPattern;
  70. // Returns all `BindingPattern`s in the AST subtree rooted at `pattern`.
  71. auto GetBindings(const Pattern& pattern)
  72. -> std::vector<Nonnull<const BindingPattern*>>;
  73. // A pattern consisting of the `auto` keyword.
  74. class AutoPattern : public Pattern {
  75. public:
  76. explicit AutoPattern(SourceLocation source_loc)
  77. : Pattern(AstNodeKind::AutoPattern, source_loc) {}
  78. static auto classof(const AstNode* node) -> bool {
  79. return InheritsFromAutoPattern(node->kind());
  80. }
  81. };
  82. class VarPattern : public Pattern {
  83. public:
  84. explicit VarPattern(SourceLocation source_loc, Nonnull<Pattern*> pattern)
  85. : Pattern(AstNodeKind::VarPattern, source_loc), pattern_(pattern) {}
  86. static auto classof(const AstNode* node) -> bool {
  87. return InheritsFromVarPattern(node->kind());
  88. }
  89. auto pattern() const -> const Pattern& { return *pattern_; }
  90. auto pattern() -> Pattern& { return *pattern_; }
  91. auto value_category() const -> ValueCategory { return ValueCategory::Var; }
  92. private:
  93. Nonnull<Pattern*> pattern_;
  94. };
  95. // A pattern that matches a value of a specified type, and optionally binds
  96. // a name to it.
  97. class BindingPattern : public Pattern {
  98. public:
  99. using ImplementsCarbonValueNode = void;
  100. BindingPattern(SourceLocation source_loc, std::string name,
  101. Nonnull<Pattern*> type,
  102. std::optional<ValueCategory> value_category)
  103. : Pattern(AstNodeKind::BindingPattern, source_loc),
  104. name_(std::move(name)),
  105. type_(type),
  106. value_category_(value_category) {}
  107. static auto classof(const AstNode* node) -> bool {
  108. return InheritsFromBindingPattern(node->kind());
  109. }
  110. // The name this pattern binds, if any. If equal to AnonymousName, indicates
  111. // that this BindingPattern does not bind a name, which in turn means it
  112. // should not be used as a ValueNode.
  113. auto name() const -> const std::string& { return name_; }
  114. // The pattern specifying the type of values that this pattern matches.
  115. auto type() const -> const Pattern& { return *type_; }
  116. auto type() -> Pattern& { return *type_; }
  117. // Returns the value category of this pattern. Can only be called after
  118. // typechecking.
  119. auto value_category() const -> ValueCategory {
  120. return value_category_.value();
  121. }
  122. // Returns whether the value category has been set. Should only be called
  123. // during typechecking.
  124. auto has_value_category() const -> bool {
  125. return value_category_.has_value();
  126. }
  127. // Sets the value category of the variable being bound. Can only be called
  128. // once during typechecking
  129. void set_value_category(ValueCategory vc) {
  130. CHECK(!value_category_.has_value());
  131. value_category_ = vc;
  132. }
  133. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  134. return std::nullopt;
  135. }
  136. private:
  137. std::string name_;
  138. Nonnull<Pattern*> type_;
  139. std::optional<ValueCategory> value_category_;
  140. };
  141. // A pattern that matches a tuple value field-wise.
  142. class TuplePattern : public Pattern {
  143. public:
  144. TuplePattern(SourceLocation source_loc, std::vector<Nonnull<Pattern*>> fields)
  145. : Pattern(AstNodeKind::TuplePattern, source_loc),
  146. fields_(std::move(fields)) {}
  147. static auto classof(const AstNode* node) -> bool {
  148. return InheritsFromTuplePattern(node->kind());
  149. }
  150. auto fields() const -> llvm::ArrayRef<Nonnull<const Pattern*>> {
  151. return fields_;
  152. }
  153. auto fields() -> llvm::ArrayRef<Nonnull<Pattern*>> { return fields_; }
  154. private:
  155. std::vector<Nonnull<Pattern*>> fields_;
  156. };
  157. // Converts paren_contents to a Pattern, interpreting the parentheses as
  158. // grouping if their contents permit that interpretation, or as forming a
  159. // tuple otherwise.
  160. auto PatternFromParenContents(Nonnull<Arena*> arena, SourceLocation source_loc,
  161. const ParenContents<Pattern>& paren_contents)
  162. -> Nonnull<Pattern*>;
  163. // Converts paren_contents to a TuplePattern, interpreting the parentheses as
  164. // forming a tuple.
  165. auto TuplePatternFromParenContents(Nonnull<Arena*> arena,
  166. SourceLocation source_loc,
  167. const ParenContents<Pattern>& paren_contents)
  168. -> Nonnull<TuplePattern*>;
  169. // Converts `contents` to ParenContents<Pattern> by replacing each Expression
  170. // with an ExpressionPattern.
  171. auto ParenExpressionToParenPattern(Nonnull<Arena*> arena,
  172. const ParenContents<Expression>& contents)
  173. -> ParenContents<Pattern>;
  174. // A pattern that matches an alternative of a choice type.
  175. class AlternativePattern : public Pattern {
  176. public:
  177. // Constructs an AlternativePattern that matches the alternative specified
  178. // by `alternative`, if its arguments match `arguments`.
  179. static auto Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  180. Nonnull<Expression*> alternative,
  181. Nonnull<TuplePattern*> arguments)
  182. -> ErrorOr<Nonnull<AlternativePattern*>> {
  183. ASSIGN_OR_RETURN(Nonnull<FieldAccessExpression*> field_access,
  184. RequireFieldAccess(alternative));
  185. return arena->New<AlternativePattern>(source_loc,
  186. &field_access->aggregate(),
  187. field_access->field(), arguments);
  188. }
  189. // Constructs an AlternativePattern that matches a value of the type
  190. // specified by choice_type if it represents an alternative named
  191. // alternative_name, and its arguments match `arguments`.
  192. AlternativePattern(SourceLocation source_loc,
  193. Nonnull<Expression*> choice_type,
  194. std::string alternative_name,
  195. Nonnull<TuplePattern*> arguments)
  196. : Pattern(AstNodeKind::AlternativePattern, source_loc),
  197. choice_type_(choice_type),
  198. alternative_name_(std::move(alternative_name)),
  199. arguments_(arguments) {}
  200. static auto classof(const AstNode* node) -> bool {
  201. return InheritsFromAlternativePattern(node->kind());
  202. }
  203. auto choice_type() const -> const Expression& { return *choice_type_; }
  204. auto choice_type() -> Expression& { return *choice_type_; }
  205. auto alternative_name() const -> const std::string& {
  206. return alternative_name_;
  207. }
  208. auto arguments() const -> const TuplePattern& { return *arguments_; }
  209. auto arguments() -> TuplePattern& { return *arguments_; }
  210. private:
  211. static auto RequireFieldAccess(Nonnull<Expression*> alternative)
  212. -> ErrorOr<Nonnull<FieldAccessExpression*>>;
  213. Nonnull<Expression*> choice_type_;
  214. std::string alternative_name_;
  215. Nonnull<TuplePattern*> arguments_;
  216. };
  217. // A pattern that matches a value if it is equal to the value of a given
  218. // expression.
  219. class ExpressionPattern : public Pattern {
  220. public:
  221. explicit ExpressionPattern(Nonnull<Expression*> expression)
  222. : Pattern(AstNodeKind::ExpressionPattern, expression->source_loc()),
  223. expression_(expression) {}
  224. static auto classof(const AstNode* node) -> bool {
  225. return InheritsFromExpressionPattern(node->kind());
  226. }
  227. auto expression() const -> const Expression& { return *expression_; }
  228. auto expression() -> Expression& { return *expression_; }
  229. private:
  230. Nonnull<Expression*> expression_;
  231. };
  232. } // namespace Carbon
  233. #endif // EXECUTABLE_SEMANTICS_AST_PATTERN_H_