pattern.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. protected:
  50. // Constructs a Pattern representing syntax at the given line number.
  51. // `kind` must be the enumerator corresponding to the most-derived type being
  52. // constructed.
  53. Pattern(Kind kind, SourceLocation source_loc)
  54. : kind_(kind), source_loc_(source_loc) {}
  55. private:
  56. const Kind kind_;
  57. SourceLocation source_loc_;
  58. std::optional<Nonnull<const Value*>> static_type_;
  59. };
  60. // A pattern consisting of the `auto` keyword.
  61. class AutoPattern : public Pattern {
  62. public:
  63. explicit AutoPattern(SourceLocation source_loc)
  64. : Pattern(Kind::AutoPattern, source_loc) {}
  65. static auto classof(const Pattern* pattern) -> bool {
  66. return pattern->kind() == Kind::AutoPattern;
  67. }
  68. };
  69. // A pattern that matches a value of a specified type, and optionally binds
  70. // a name to it.
  71. class BindingPattern : public Pattern {
  72. public:
  73. BindingPattern(SourceLocation source_loc, std::optional<std::string> name,
  74. Nonnull<Pattern*> type)
  75. : Pattern(Kind::BindingPattern, source_loc),
  76. name_(std::move(name)),
  77. type_(type) {}
  78. static auto classof(const Pattern* pattern) -> bool {
  79. return pattern->kind() == Kind::BindingPattern;
  80. }
  81. // The name this pattern binds, if any.
  82. auto name() const -> const std::optional<std::string>& { return name_; }
  83. // The pattern specifying the type of values that this pattern matches.
  84. auto type() const -> const Pattern& { return *type_; }
  85. auto type() -> Pattern& { return *type_; }
  86. private:
  87. std::optional<std::string> name_;
  88. Nonnull<Pattern*> type_;
  89. };
  90. // A pattern that matches a tuple value field-wise.
  91. class TuplePattern : public Pattern {
  92. public:
  93. TuplePattern(SourceLocation source_loc, std::vector<Nonnull<Pattern*>> fields)
  94. : Pattern(Kind::TuplePattern, source_loc), fields_(std::move(fields)) {}
  95. static auto classof(const Pattern* pattern) -> bool {
  96. return pattern->kind() == Kind::TuplePattern;
  97. }
  98. auto fields() const -> llvm::ArrayRef<Nonnull<const Pattern*>> {
  99. return fields_;
  100. }
  101. auto fields() -> llvm::ArrayRef<Nonnull<Pattern*>> { return fields_; }
  102. private:
  103. std::vector<Nonnull<Pattern*>> fields_;
  104. };
  105. // Converts paren_contents to a Pattern, interpreting the parentheses as
  106. // grouping if their contents permit that interpretation, or as forming a
  107. // tuple otherwise.
  108. auto PatternFromParenContents(Nonnull<Arena*> arena, SourceLocation source_loc,
  109. const ParenContents<Pattern>& paren_contents)
  110. -> Nonnull<Pattern*>;
  111. // Converts paren_contents to a TuplePattern, interpreting the parentheses as
  112. // forming a tuple.
  113. auto TuplePatternFromParenContents(Nonnull<Arena*> arena,
  114. SourceLocation source_loc,
  115. const ParenContents<Pattern>& paren_contents)
  116. -> Nonnull<TuplePattern*>;
  117. // Converts `contents` to ParenContents<Pattern> by replacing each Expression
  118. // with an ExpressionPattern.
  119. auto ParenExpressionToParenPattern(Nonnull<Arena*> arena,
  120. const ParenContents<Expression>& contents)
  121. -> ParenContents<Pattern>;
  122. // A pattern that matches an alternative of a choice type.
  123. class AlternativePattern : public Pattern {
  124. public:
  125. // Constructs an AlternativePattern that matches a value of the type
  126. // specified by choice_type if it represents an alternative named
  127. // alternative_name, and its arguments match `arguments`.
  128. AlternativePattern(SourceLocation source_loc,
  129. Nonnull<Expression*> choice_type,
  130. std::string alternative_name,
  131. Nonnull<TuplePattern*> arguments)
  132. : Pattern(Kind::AlternativePattern, source_loc),
  133. choice_type_(choice_type),
  134. alternative_name_(std::move(alternative_name)),
  135. arguments_(arguments) {}
  136. // Constructs an AlternativePattern that matches the alternative specified
  137. // by `alternative`, if its arguments match `arguments`.
  138. AlternativePattern(SourceLocation source_loc,
  139. Nonnull<Expression*> alternative,
  140. Nonnull<TuplePattern*> arguments);
  141. static auto classof(const Pattern* pattern) -> bool {
  142. return pattern->kind() == Kind::AlternativePattern;
  143. }
  144. auto choice_type() const -> const Expression& { return *choice_type_; }
  145. auto choice_type() -> Expression& { return *choice_type_; }
  146. auto alternative_name() const -> const std::string& {
  147. return alternative_name_;
  148. }
  149. auto arguments() const -> const TuplePattern& { return *arguments_; }
  150. auto arguments() -> TuplePattern& { return *arguments_; }
  151. private:
  152. Nonnull<Expression*> choice_type_;
  153. std::string alternative_name_;
  154. Nonnull<TuplePattern*> arguments_;
  155. };
  156. // A pattern that matches a value if it is equal to the value of a given
  157. // expression.
  158. class ExpressionPattern : public Pattern {
  159. public:
  160. ExpressionPattern(Nonnull<Expression*> expression)
  161. : Pattern(Kind::ExpressionPattern, expression->source_loc()),
  162. expression_(expression) {}
  163. static auto classof(const Pattern* pattern) -> bool {
  164. return pattern->kind() == Kind::ExpressionPattern;
  165. }
  166. auto expression() const -> const Expression& { return *expression_; }
  167. auto expression() -> Expression& { return *expression_; }
  168. private:
  169. Nonnull<Expression*> expression_;
  170. };
  171. } // namespace Carbon
  172. #endif // EXECUTABLE_SEMANTICS_AST_PATTERN_H_