pattern.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "llvm/ADT/ArrayRef.h"
  16. namespace Carbon {
  17. class Value;
  18. // Abstract base class of all AST nodes representing patterns.
  19. //
  20. // Pattern and its derived classes support LLVM-style RTTI, including
  21. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  22. // class derived from Pattern must provide a `classof` operation, and
  23. // every concrete derived class must have a corresponding enumerator
  24. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  25. // details.
  26. class Pattern : public virtual AstNode {
  27. public:
  28. Pattern(const Pattern&) = delete;
  29. auto operator=(const Pattern&) -> Pattern& = delete;
  30. ~Pattern() override = 0;
  31. void Print(llvm::raw_ostream& out) const override;
  32. static auto classof(const AstNode* node) -> bool {
  33. return InheritsFromPattern(node->kind());
  34. }
  35. // Returns the enumerator corresponding to the most-derived type of this
  36. // object.
  37. auto kind() const -> PatternKind {
  38. return static_cast<PatternKind>(root_kind());
  39. }
  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() = default;
  63. private:
  64. std::optional<Nonnull<const Value*>> static_type_;
  65. std::optional<Nonnull<const Value*>> value_;
  66. };
  67. // A pattern consisting of the `auto` keyword.
  68. class AutoPattern : public Pattern {
  69. public:
  70. explicit AutoPattern(SourceLocation source_loc)
  71. : AstNode(AstNodeKind::AutoPattern, source_loc) {}
  72. static auto classof(const AstNode* node) -> bool {
  73. return InheritsFromAutoPattern(node->kind());
  74. }
  75. };
  76. // A pattern that matches a value of a specified type, and optionally binds
  77. // a name to it.
  78. class BindingPattern : public Pattern, public NamedEntity {
  79. public:
  80. BindingPattern(SourceLocation source_loc, std::optional<std::string> name,
  81. Nonnull<Pattern*> type)
  82. : AstNode(AstNodeKind::BindingPattern, source_loc),
  83. name_(std::move(name)),
  84. type_(type) {}
  85. static auto classof(const AstNode* node) -> bool {
  86. return InheritsFromBindingPattern(node->kind());
  87. }
  88. // The name this pattern binds, if any.
  89. auto name() const -> const std::optional<std::string>& { return name_; }
  90. // The pattern specifying the type of values that this pattern matches.
  91. auto type() const -> const Pattern& { return *type_; }
  92. auto type() -> Pattern& { return *type_; }
  93. private:
  94. std::optional<std::string> name_;
  95. Nonnull<Pattern*> type_;
  96. };
  97. // A pattern that matches a tuple value field-wise.
  98. class TuplePattern : public Pattern {
  99. public:
  100. TuplePattern(SourceLocation source_loc, std::vector<Nonnull<Pattern*>> fields)
  101. : AstNode(AstNodeKind::TuplePattern, source_loc),
  102. fields_(std::move(fields)) {}
  103. static auto classof(const AstNode* node) -> bool {
  104. return InheritsFromTuplePattern(node->kind());
  105. }
  106. auto fields() const -> llvm::ArrayRef<Nonnull<const Pattern*>> {
  107. return fields_;
  108. }
  109. auto fields() -> llvm::ArrayRef<Nonnull<Pattern*>> { return fields_; }
  110. private:
  111. std::vector<Nonnull<Pattern*>> fields_;
  112. };
  113. // Converts paren_contents to a Pattern, interpreting the parentheses as
  114. // grouping if their contents permit that interpretation, or as forming a
  115. // tuple otherwise.
  116. auto PatternFromParenContents(Nonnull<Arena*> arena, SourceLocation source_loc,
  117. const ParenContents<Pattern>& paren_contents)
  118. -> Nonnull<Pattern*>;
  119. // Converts paren_contents to a TuplePattern, interpreting the parentheses as
  120. // forming a tuple.
  121. auto TuplePatternFromParenContents(Nonnull<Arena*> arena,
  122. SourceLocation source_loc,
  123. const ParenContents<Pattern>& paren_contents)
  124. -> Nonnull<TuplePattern*>;
  125. // Converts `contents` to ParenContents<Pattern> by replacing each Expression
  126. // with an ExpressionPattern.
  127. auto ParenExpressionToParenPattern(Nonnull<Arena*> arena,
  128. const ParenContents<Expression>& contents)
  129. -> ParenContents<Pattern>;
  130. // A pattern that matches an alternative of a choice type.
  131. class AlternativePattern : public Pattern {
  132. public:
  133. // Constructs an AlternativePattern that matches a value of the type
  134. // specified by choice_type if it represents an alternative named
  135. // alternative_name, and its arguments match `arguments`.
  136. AlternativePattern(SourceLocation source_loc,
  137. Nonnull<Expression*> choice_type,
  138. std::string alternative_name,
  139. Nonnull<TuplePattern*> arguments)
  140. : AstNode(AstNodeKind::AlternativePattern, source_loc),
  141. choice_type_(choice_type),
  142. alternative_name_(std::move(alternative_name)),
  143. arguments_(arguments) {}
  144. // Constructs an AlternativePattern that matches the alternative specified
  145. // by `alternative`, if its arguments match `arguments`.
  146. AlternativePattern(SourceLocation source_loc,
  147. Nonnull<Expression*> alternative,
  148. Nonnull<TuplePattern*> arguments);
  149. static auto classof(const AstNode* node) -> bool {
  150. return InheritsFromAlternativePattern(node->kind());
  151. }
  152. auto choice_type() const -> const Expression& { return *choice_type_; }
  153. auto choice_type() -> Expression& { return *choice_type_; }
  154. auto alternative_name() const -> const std::string& {
  155. return alternative_name_;
  156. }
  157. auto arguments() const -> const TuplePattern& { return *arguments_; }
  158. auto arguments() -> TuplePattern& { return *arguments_; }
  159. private:
  160. Nonnull<Expression*> choice_type_;
  161. std::string alternative_name_;
  162. Nonnull<TuplePattern*> arguments_;
  163. };
  164. // A pattern that matches a value if it is equal to the value of a given
  165. // expression.
  166. class ExpressionPattern : public Pattern {
  167. public:
  168. explicit ExpressionPattern(Nonnull<Expression*> expression)
  169. : AstNode(AstNodeKind::ExpressionPattern, expression->source_loc()),
  170. expression_(expression) {}
  171. static auto classof(const AstNode* node) -> bool {
  172. return InheritsFromExpressionPattern(node->kind());
  173. }
  174. auto expression() const -> const Expression& { return *expression_; }
  175. auto expression() -> Expression& { return *expression_; }
  176. private:
  177. Nonnull<Expression*> expression_;
  178. };
  179. } // namespace Carbon
  180. #endif // EXECUTABLE_SEMANTICS_AST_PATTERN_H_