pattern.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. namespace Carbon {
  12. // Abstract base class of all AST nodes representing patterns.
  13. //
  14. // Pattern and its derived classes support LLVM-style RTTI, including
  15. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  16. // class derived from Pattern must provide a `classof` operation, and
  17. // every concrete derived class must have a corresponding enumerator
  18. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  19. // details.
  20. class Pattern {
  21. public:
  22. enum class Kind {
  23. AutoPattern,
  24. BindingPattern,
  25. TuplePattern,
  26. AlternativePattern,
  27. ExpressionPattern,
  28. };
  29. Pattern(const Pattern&) = delete;
  30. Pattern& operator=(const Pattern&) = delete;
  31. // Returns the enumerator corresponding to the most-derived type of this
  32. // object.
  33. auto Tag() const -> Kind { return tag; }
  34. auto LineNumber() const -> int { return line_num; }
  35. void Print(llvm::raw_ostream& out) const;
  36. protected:
  37. // Constructs a Pattern representing syntax at the given line number.
  38. // `tag` must be the enumerator corresponding to the most-derived type being
  39. // constructed.
  40. Pattern(Kind tag, int line_num) : tag(tag), line_num(line_num) {}
  41. private:
  42. const Kind tag;
  43. int line_num;
  44. };
  45. // A pattern consisting of the `auto` keyword.
  46. class AutoPattern : public Pattern {
  47. public:
  48. explicit AutoPattern(int line_num) : Pattern(Kind::AutoPattern, line_num) {}
  49. static auto classof(const Pattern* pattern) -> bool {
  50. return pattern->Tag() == Kind::AutoPattern;
  51. }
  52. };
  53. // A pattern that matches a value of a specified type, and optionally binds
  54. // a name to it.
  55. class BindingPattern : public Pattern {
  56. public:
  57. BindingPattern(int line_num, std::optional<std::string> name,
  58. const Pattern* type)
  59. : Pattern(Kind::BindingPattern, line_num),
  60. name(std::move(name)),
  61. type(type) {}
  62. static auto classof(const Pattern* pattern) -> bool {
  63. return pattern->Tag() == Kind::BindingPattern;
  64. }
  65. // The name this pattern binds, if any.
  66. auto Name() const -> const std::optional<std::string>& { return name; }
  67. // The pattern specifying the type of values that this pattern matches.
  68. auto Type() const -> const Pattern* { return type; }
  69. private:
  70. std::optional<std::string> name;
  71. const Pattern* type;
  72. };
  73. // A pattern that matches a tuple value field-wise.
  74. class TuplePattern : public Pattern {
  75. public:
  76. // Represents a portion of a tuple pattern corresponding to a single field.
  77. struct Field {
  78. Field(std::string name, const Pattern* pattern)
  79. : name(std::move(name)), pattern(pattern) {}
  80. // The field name. Cannot be empty
  81. std::string name;
  82. // The pattern the field must match.
  83. const Pattern* pattern;
  84. };
  85. TuplePattern(int line_num, std::vector<Field> fields)
  86. : Pattern(Kind::TuplePattern, line_num), fields(std::move(fields)) {}
  87. // Converts tuple_literal to a TuplePattern, by wrapping each field in an
  88. // ExpressionPattern.
  89. //
  90. // REQUIRES: tuple_literal->Tag() == ExpressionKind::TupleLiteral
  91. explicit TuplePattern(const Expression* tuple_literal);
  92. static auto classof(const Pattern* pattern) -> bool {
  93. return pattern->Tag() == Kind::TuplePattern;
  94. }
  95. auto Fields() const -> const std::vector<Field>& { return fields; }
  96. private:
  97. std::vector<Field> fields;
  98. };
  99. // Converts paren_contents to a Pattern, interpreting the parentheses as
  100. // grouping if their contents permit that interpretation, or as forming a
  101. // tuple otherwise.
  102. auto PatternFromParenContents(int line_num,
  103. const ParenContents<Pattern>& paren_contents)
  104. -> const Pattern*;
  105. // Converts paren_contents to a TuplePattern, interpreting the parentheses as
  106. // forming a tuple.
  107. auto TuplePatternFromParenContents(int line_num,
  108. const ParenContents<Pattern>& paren_contents)
  109. -> const TuplePattern*;
  110. // Converts `contents` to ParenContents<Pattern> by replacing each Expression
  111. // with an ExpressionPattern.
  112. auto ParenExpressionToParenPattern(const ParenContents<Expression>& contents)
  113. -> ParenContents<Pattern>;
  114. // A pattern that matches an alternative of a choice type.
  115. class AlternativePattern : public Pattern {
  116. public:
  117. // Constructs an AlternativePattern that matches a value of the type
  118. // specified by choice_type if it represents an alternative named
  119. // alternative_name, and its arguments match `arguments`.
  120. AlternativePattern(int line_num, const Expression* choice_type,
  121. std::string alternative_name,
  122. const TuplePattern* arguments)
  123. : Pattern(Kind::AlternativePattern, line_num),
  124. choice_type(choice_type),
  125. alternative_name(std::move(alternative_name)),
  126. arguments(arguments) {}
  127. // Constructs an AlternativePattern that matches the alternative specified
  128. // by `alternative`, if its arguments match `arguments`.
  129. AlternativePattern(int line_num, const Expression* alternative,
  130. const TuplePattern* arguments);
  131. static auto classof(const Pattern* pattern) -> bool {
  132. return pattern->Tag() == Kind::AlternativePattern;
  133. }
  134. auto ChoiceType() const -> const Expression* { return choice_type; }
  135. auto AlternativeName() const -> const std::string& {
  136. return alternative_name;
  137. }
  138. auto Arguments() const -> const TuplePattern* { return arguments; }
  139. private:
  140. const Expression* choice_type;
  141. std::string alternative_name;
  142. const TuplePattern* arguments;
  143. };
  144. // A pattern that matches a value if it is equal to the value of a given
  145. // expression.
  146. class ExpressionPattern : public Pattern {
  147. public:
  148. ExpressionPattern(const Expression* expression)
  149. : Pattern(Kind::ExpressionPattern, expression->line_num),
  150. expression(expression) {}
  151. static auto classof(const Pattern* pattern) -> bool {
  152. return pattern->Tag() == Kind::ExpressionPattern;
  153. }
  154. auto Expression() const -> const Expression* { return expression; }
  155. private:
  156. const Carbon::Expression* expression;
  157. };
  158. } // namespace Carbon
  159. #endif // EXECUTABLE_SEMANTICS_AST_PATTERN_H_