pattern.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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;
  32. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  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) { static_type_ = type; }
  46. // Returns whether the static type has been set. Should only be called
  47. // during typechecking: before typechecking it's guaranteed to be false,
  48. // and after typechecking it's guaranteed to be true.
  49. auto has_static_type() const -> bool { return static_type_.has_value(); }
  50. // The value of this pattern. Cannot be called before typechecking.
  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() = default;
  64. private:
  65. std::optional<Nonnull<const Value*>> static_type_;
  66. std::optional<Nonnull<const Value*>> value_;
  67. };
  68. // A pattern consisting of the `auto` keyword.
  69. class AutoPattern : public Pattern {
  70. public:
  71. explicit AutoPattern(SourceLocation source_loc)
  72. : AstNode(AstNodeKind::AutoPattern, source_loc) {}
  73. static auto classof(const AstNode* node) -> bool {
  74. return InheritsFromAutoPattern(node->kind());
  75. }
  76. };
  77. // A pattern that matches a value of a specified type, and optionally binds
  78. // a name to it.
  79. class BindingPattern : public Pattern, public NamedEntity {
  80. public:
  81. BindingPattern(SourceLocation source_loc, std::optional<std::string> name,
  82. Nonnull<Pattern*> type)
  83. : AstNode(AstNodeKind::BindingPattern, source_loc),
  84. name_(std::move(name)),
  85. type_(type) {}
  86. static auto classof(const AstNode* node) -> bool {
  87. return InheritsFromBindingPattern(node->kind());
  88. }
  89. // The name this pattern binds, if any.
  90. auto name() const -> const std::optional<std::string>& { return name_; }
  91. // The pattern specifying the type of values that this pattern matches.
  92. auto type() const -> const Pattern& { return *type_; }
  93. auto type() -> Pattern& { return *type_; }
  94. private:
  95. std::optional<std::string> name_;
  96. Nonnull<Pattern*> type_;
  97. };
  98. // A pattern that matches a tuple value field-wise.
  99. class TuplePattern : public Pattern {
  100. public:
  101. TuplePattern(SourceLocation source_loc, std::vector<Nonnull<Pattern*>> fields)
  102. : AstNode(AstNodeKind::TuplePattern, source_loc),
  103. fields_(std::move(fields)) {}
  104. static auto classof(const AstNode* node) -> bool {
  105. return InheritsFromTuplePattern(node->kind());
  106. }
  107. auto fields() const -> llvm::ArrayRef<Nonnull<const Pattern*>> {
  108. return fields_;
  109. }
  110. auto fields() -> llvm::ArrayRef<Nonnull<Pattern*>> { return fields_; }
  111. private:
  112. std::vector<Nonnull<Pattern*>> fields_;
  113. };
  114. // Converts paren_contents to a Pattern, interpreting the parentheses as
  115. // grouping if their contents permit that interpretation, or as forming a
  116. // tuple otherwise.
  117. auto PatternFromParenContents(Nonnull<Arena*> arena, SourceLocation source_loc,
  118. const ParenContents<Pattern>& paren_contents)
  119. -> Nonnull<Pattern*>;
  120. // Converts paren_contents to a TuplePattern, interpreting the parentheses as
  121. // forming a tuple.
  122. auto TuplePatternFromParenContents(Nonnull<Arena*> arena,
  123. SourceLocation source_loc,
  124. const ParenContents<Pattern>& paren_contents)
  125. -> Nonnull<TuplePattern*>;
  126. // Converts `contents` to ParenContents<Pattern> by replacing each Expression
  127. // with an ExpressionPattern.
  128. auto ParenExpressionToParenPattern(Nonnull<Arena*> arena,
  129. const ParenContents<Expression>& contents)
  130. -> ParenContents<Pattern>;
  131. // A pattern that matches an alternative of a choice type.
  132. class AlternativePattern : public Pattern {
  133. public:
  134. // Constructs an AlternativePattern that matches a value of the type
  135. // specified by choice_type if it represents an alternative named
  136. // alternative_name, and its arguments match `arguments`.
  137. AlternativePattern(SourceLocation source_loc,
  138. Nonnull<Expression*> choice_type,
  139. std::string alternative_name,
  140. Nonnull<TuplePattern*> arguments)
  141. : AstNode(AstNodeKind::AlternativePattern, source_loc),
  142. choice_type_(choice_type),
  143. alternative_name_(std::move(alternative_name)),
  144. arguments_(arguments) {}
  145. // Constructs an AlternativePattern that matches the alternative specified
  146. // by `alternative`, if its arguments match `arguments`.
  147. AlternativePattern(SourceLocation source_loc,
  148. Nonnull<Expression*> alternative,
  149. Nonnull<TuplePattern*> arguments);
  150. static auto classof(const AstNode* node) -> bool {
  151. return InheritsFromAlternativePattern(node->kind());
  152. }
  153. auto choice_type() const -> const Expression& { return *choice_type_; }
  154. auto choice_type() -> Expression& { return *choice_type_; }
  155. auto alternative_name() const -> const std::string& {
  156. return alternative_name_;
  157. }
  158. auto arguments() const -> const TuplePattern& { return *arguments_; }
  159. auto arguments() -> TuplePattern& { return *arguments_; }
  160. private:
  161. Nonnull<Expression*> choice_type_;
  162. std::string alternative_name_;
  163. Nonnull<TuplePattern*> arguments_;
  164. };
  165. // A pattern that matches a value if it is equal to the value of a given
  166. // expression.
  167. class ExpressionPattern : public Pattern {
  168. public:
  169. explicit ExpressionPattern(Nonnull<Expression*> expression)
  170. : AstNode(AstNodeKind::ExpressionPattern, expression->source_loc()),
  171. expression_(expression) {}
  172. static auto classof(const AstNode* node) -> bool {
  173. return InheritsFromExpressionPattern(node->kind());
  174. }
  175. auto expression() const -> const Expression& { return *expression_; }
  176. auto expression() -> Expression& { return *expression_; }
  177. private:
  178. Nonnull<Expression*> expression_;
  179. };
  180. } // namespace Carbon
  181. #endif // EXECUTABLE_SEMANTICS_AST_PATTERN_H_