pattern.h 7.0 KB

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