1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 // This file is part of GCC.
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
19 #ifndef RUST_HIR_PATTERN_H
20 #define RUST_HIR_PATTERN_H
22 #include "rust-common.h"
28 // Literal pattern HIR node (comparing to a literal)
29 class LiteralPattern
: public Pattern
33 Analysis::NodeMapping mappings
;
36 std::string
as_string () const override
;
38 // Constructor for a literal pattern
39 LiteralPattern (Analysis::NodeMapping mappings
, Literal lit
, Location locus
)
40 : lit (std::move (lit
)), locus (locus
), mappings (mappings
)
43 LiteralPattern (Analysis::NodeMapping mappings
, std::string val
,
44 Literal::LitType type
, Location locus
)
45 : lit (Literal (std::move (val
), type
, PrimitiveCoreType::CORETYPE_STR
)),
46 locus (locus
), mappings (mappings
)
49 Location
get_locus () const override
{ return locus
; }
51 void accept_vis (HIRFullVisitor
&vis
) override
;
52 void accept_vis (HIRPatternVisitor
&vis
) override
;
54 Analysis::NodeMapping
get_pattern_mappings () const override final
59 PatternType
get_pattern_type () const override final
61 return PatternType::LITERAL
;
64 Literal
&get_literal () { return lit
; }
65 const Literal
&get_literal () const { return lit
; }
68 /* Use covariance to implement clone function as returning this object rather
70 virtual LiteralPattern
*clone_pattern_impl () const override
72 return new LiteralPattern (*this);
76 // Identifier pattern HIR node (bind value matched to a variable)
77 class IdentifierPattern
: public Pattern
79 Identifier variable_ident
;
82 std::unique_ptr
<Pattern
> to_bind
;
84 Analysis::NodeMapping mappings
;
87 std::string
as_string () const override
;
89 // Returns whether the IdentifierPattern has a pattern to bind.
90 bool has_pattern_to_bind () const { return to_bind
!= nullptr; }
93 IdentifierPattern (Analysis::NodeMapping mappings
, Identifier ident
,
94 Location locus
, bool is_ref
= false,
95 Mutability mut
= Mutability::Imm
,
96 std::unique_ptr
<Pattern
> to_bind
= nullptr)
97 : variable_ident (std::move (ident
)), is_ref (is_ref
), mut (mut
),
98 to_bind (std::move (to_bind
)), locus (locus
), mappings (mappings
)
101 // Copy constructor with clone
102 IdentifierPattern (IdentifierPattern
const &other
)
103 : variable_ident (other
.variable_ident
), is_ref (other
.is_ref
),
104 mut (other
.mut
), locus (other
.locus
), mappings (other
.mappings
)
106 // fix to get prevent null pointer dereference
107 if (other
.to_bind
!= nullptr)
108 to_bind
= other
.to_bind
->clone_pattern ();
111 // Overload assignment operator to use clone
112 IdentifierPattern
&operator= (IdentifierPattern
const &other
)
114 variable_ident
= other
.variable_ident
;
115 is_ref
= other
.is_ref
;
118 mappings
= other
.mappings
;
120 // fix to get prevent null pointer dereference
121 if (other
.to_bind
!= nullptr)
122 to_bind
= other
.to_bind
->clone_pattern ();
127 // default move semantics
128 IdentifierPattern (IdentifierPattern
&&other
) = default;
129 IdentifierPattern
&operator= (IdentifierPattern
&&other
) = default;
131 Location
get_locus () const override
{ return locus
; }
133 bool is_mut () const { return mut
== Mutability::Mut
; }
135 void accept_vis (HIRFullVisitor
&vis
) override
;
136 void accept_vis (HIRPatternVisitor
&vis
) override
;
138 Analysis::NodeMapping
get_pattern_mappings () const override final
143 Identifier
get_identifier () const { return variable_ident
; }
145 PatternType
get_pattern_type () const override final
147 return PatternType::IDENTIFIER
;
151 /* Use covariance to implement clone function as returning this object rather
153 IdentifierPattern
*clone_pattern_impl () const override
155 return new IdentifierPattern (*this);
159 // HIR node for using the '_' wildcard "match any value" pattern
160 class WildcardPattern
: public Pattern
163 Analysis::NodeMapping mappings
;
166 std::string
as_string () const override
{ return std::string (1, '_'); }
168 WildcardPattern (Analysis::NodeMapping mappings
, Location locus
)
169 : locus (locus
), mappings (mappings
)
172 Location
get_locus () const override
{ return locus
; }
174 void accept_vis (HIRFullVisitor
&vis
) override
;
175 void accept_vis (HIRPatternVisitor
&vis
) override
;
177 Analysis::NodeMapping
get_pattern_mappings () const override final
182 PatternType
get_pattern_type () const override final
184 return PatternType::WILDCARD
;
188 /* Use covariance to implement clone function as returning this object rather
190 WildcardPattern
*clone_pattern_impl () const override
192 return new WildcardPattern (*this);
196 // Base range pattern bound (lower or upper limit) - abstract
197 class RangePatternBound
200 enum RangePatternBoundType
207 virtual ~RangePatternBound () {}
209 // Unique pointer custom clone function
210 std::unique_ptr
<RangePatternBound
> clone_range_pattern_bound () const
212 return std::unique_ptr
<RangePatternBound
> (
213 clone_range_pattern_bound_impl ());
216 virtual std::string
as_string () const = 0;
218 virtual void accept_vis (HIRFullVisitor
&vis
) = 0;
220 virtual RangePatternBoundType
get_bound_type () const = 0;
223 // pure virtual as RangePatternBound is abstract
224 virtual RangePatternBound
*clone_range_pattern_bound_impl () const = 0;
227 // Literal-based pattern bound
228 class RangePatternBoundLiteral
: public RangePatternBound
231 /* Can only be a char, byte, int, or float literal - same impl here as
234 // Minus prefixed to literal (if integer or floating-point)
241 RangePatternBoundLiteral (Literal literal
, Location locus
,
242 bool has_minus
= false)
243 : literal (literal
), has_minus (has_minus
), locus (locus
)
246 std::string
as_string () const override
;
248 Location
get_locus () const { return locus
; }
250 Literal
get_literal () const { return literal
; }
252 void accept_vis (HIRFullVisitor
&vis
) override
;
254 RangePatternBoundType
get_bound_type () const override
256 return RangePatternBoundType::LITERAL
;
260 /* Use covariance to implement clone function as returning this object rather
262 RangePatternBoundLiteral
*clone_range_pattern_bound_impl () const override
264 return new RangePatternBoundLiteral (*this);
268 // Path-based pattern bound
269 class RangePatternBoundPath
: public RangePatternBound
271 PathInExpression path
;
273 /* TODO: should this be refactored so that PathInExpression is a subclass of
274 * RangePatternBound? */
277 RangePatternBoundPath (PathInExpression path
) : path (std::move (path
)) {}
279 std::string
as_string () const override
{ return path
.as_string (); }
281 Location
get_locus () const { return path
.get_locus (); }
283 PathInExpression
&get_path () { return path
; }
284 const PathInExpression
&get_path () const { return path
; }
286 void accept_vis (HIRFullVisitor
&vis
) override
;
288 RangePatternBoundType
get_bound_type () const override
290 return RangePatternBoundType::PATH
;
294 /* Use covariance to implement clone function as returning this object rather
296 RangePatternBoundPath
*clone_range_pattern_bound_impl () const override
298 return new RangePatternBoundPath (*this);
302 // Qualified path-based pattern bound
303 class RangePatternBoundQualPath
: public RangePatternBound
305 QualifiedPathInExpression path
;
307 /* TODO: should this be refactored so that QualifiedPathInExpression is a
308 * subclass of RangePatternBound? */
311 RangePatternBoundQualPath (QualifiedPathInExpression path
)
312 : path (std::move (path
))
315 std::string
as_string () const override
{ return path
.as_string (); }
317 Location
get_locus () const { return path
.get_locus (); }
319 void accept_vis (HIRFullVisitor
&vis
) override
;
321 QualifiedPathInExpression
&get_qualified_path () { return path
; }
322 const QualifiedPathInExpression
&get_qualified_path () const { return path
; }
324 RangePatternBoundType
get_bound_type () const override
326 return RangePatternBoundType::QUALPATH
;
330 /* Use covariance to implement clone function as returning this object rather
332 RangePatternBoundQualPath
*clone_range_pattern_bound_impl () const override
334 return new RangePatternBoundQualPath (*this);
338 // HIR node for matching within a certain range (range pattern)
339 class RangePattern
: public Pattern
341 std::unique_ptr
<RangePatternBound
> lower
;
342 std::unique_ptr
<RangePatternBound
> upper
;
344 bool has_ellipsis_syntax
;
346 /* location only stored to avoid a dereference - lower pattern should give
347 * correct location so maybe change in future */
349 Analysis::NodeMapping mappings
;
352 std::string
as_string () const override
;
355 RangePattern (Analysis::NodeMapping mappings
,
356 std::unique_ptr
<RangePatternBound
> lower
,
357 std::unique_ptr
<RangePatternBound
> upper
, Location locus
,
358 bool has_ellipsis_syntax
= false)
359 : lower (std::move (lower
)), upper (std::move (upper
)),
360 has_ellipsis_syntax (has_ellipsis_syntax
), locus (locus
),
364 // Copy constructor with clone
365 RangePattern (RangePattern
const &other
)
366 : lower (other
.lower
->clone_range_pattern_bound ()),
367 upper (other
.upper
->clone_range_pattern_bound ()),
368 has_ellipsis_syntax (other
.has_ellipsis_syntax
), locus (other
.locus
),
369 mappings (other
.mappings
)
372 // Overloaded assignment operator to clone
373 RangePattern
&operator= (RangePattern
const &other
)
375 lower
= other
.lower
->clone_range_pattern_bound ();
376 upper
= other
.upper
->clone_range_pattern_bound ();
377 has_ellipsis_syntax
= other
.has_ellipsis_syntax
;
379 mappings
= other
.mappings
;
384 // default move semantics
385 RangePattern (RangePattern
&&other
) = default;
386 RangePattern
&operator= (RangePattern
&&other
) = default;
388 Location
get_locus () const override
{ return locus
; }
390 void accept_vis (HIRFullVisitor
&vis
) override
;
391 void accept_vis (HIRPatternVisitor
&vis
) override
;
393 Analysis::NodeMapping
get_pattern_mappings () const override final
398 PatternType
get_pattern_type () const override final
400 return PatternType::RANGE
;
403 std::unique_ptr
<RangePatternBound
> &get_lower_bound ()
405 rust_assert (lower
!= nullptr);
409 std::unique_ptr
<RangePatternBound
> &get_upper_bound ()
411 rust_assert (upper
!= nullptr);
416 /* Use covariance to implement clone function as returning this object rather
418 RangePattern
*clone_pattern_impl () const override
420 return new RangePattern (*this);
424 // HIR node for pattern based on dereferencing the pointers given
425 class ReferencePattern
: public Pattern
428 std::unique_ptr
<Pattern
> pattern
;
430 Analysis::NodeMapping mappings
;
433 std::string
as_string () const override
;
435 ReferencePattern (Analysis::NodeMapping mappings
,
436 std::unique_ptr
<Pattern
> pattern
, Mutability reference_mut
,
438 : mut (reference_mut
), pattern (std::move (pattern
)), locus (locus
),
442 // Copy constructor requires clone
443 ReferencePattern (ReferencePattern
const &other
)
444 : mut (other
.mut
), pattern (other
.pattern
->clone_pattern ()),
445 locus (other
.locus
), mappings (other
.mappings
)
448 // Overload assignment operator to clone
449 ReferencePattern
&operator= (ReferencePattern
const &other
)
451 pattern
= other
.pattern
->clone_pattern ();
454 mappings
= other
.mappings
;
459 // default move semantics
460 ReferencePattern (ReferencePattern
&&other
) = default;
461 ReferencePattern
&operator= (ReferencePattern
&&other
) = default;
463 bool is_mut () const { return mut
== Mutability::Mut
; }
465 void accept_vis (HIRFullVisitor
&vis
) override
;
466 void accept_vis (HIRPatternVisitor
&vis
) override
;
468 Analysis::NodeMapping
get_pattern_mappings () const override final
473 Location
get_locus () const override final
{ return locus
; }
475 PatternType
get_pattern_type () const override final
477 return PatternType::REFERENCE
;
481 /* Use covariance to implement clone function as returning this object rather
483 ReferencePattern
*clone_pattern_impl () const override
485 return new ReferencePattern (*this);
489 // Base class for a single field in a struct pattern - abstract
490 class StructPatternField
492 AST::AttrVec outer_attrs
;
494 Analysis::NodeMapping mappings
;
504 virtual ~StructPatternField () {}
506 // Unique pointer custom clone function
507 std::unique_ptr
<StructPatternField
> clone_struct_pattern_field () const
509 return std::unique_ptr
<StructPatternField
> (
510 clone_struct_pattern_field_impl ());
513 virtual std::string
as_string () const;
514 virtual void accept_vis (HIRFullVisitor
&vis
) = 0;
515 virtual ItemType
get_item_type () const = 0;
517 Location
get_locus () const { return locus
; }
518 Analysis::NodeMapping
get_mappings () const { return mappings
; };
521 StructPatternField (Analysis::NodeMapping mappings
,
522 AST::AttrVec outer_attribs
, Location locus
)
523 : outer_attrs (std::move (outer_attribs
)), locus (locus
),
527 // Clone function implementation as pure virtual method
528 virtual StructPatternField
*clone_struct_pattern_field_impl () const = 0;
531 // Tuple pattern single field in a struct pattern
532 class StructPatternFieldTuplePat
: public StructPatternField
535 std::unique_ptr
<Pattern
> tuple_pattern
;
538 StructPatternFieldTuplePat (Analysis::NodeMapping mappings
, TupleIndex index
,
539 std::unique_ptr
<Pattern
> tuple_pattern
,
540 AST::AttrVec outer_attribs
, Location locus
)
541 : StructPatternField (mappings
, std::move (outer_attribs
), locus
),
542 index (index
), tuple_pattern (std::move (tuple_pattern
))
545 // Copy constructor requires clone
546 StructPatternFieldTuplePat (StructPatternFieldTuplePat
const &other
)
547 : StructPatternField (other
), index (other
.index
),
548 tuple_pattern (other
.tuple_pattern
->clone_pattern ())
551 // Overload assignment operator to perform clone
552 StructPatternFieldTuplePat
&
553 operator= (StructPatternFieldTuplePat
const &other
)
555 StructPatternField::operator= (other
);
556 tuple_pattern
= other
.tuple_pattern
->clone_pattern ();
558 // outer_attrs = other.outer_attrs;
563 // default move semantics
564 StructPatternFieldTuplePat (StructPatternFieldTuplePat
&&other
) = default;
565 StructPatternFieldTuplePat
&operator= (StructPatternFieldTuplePat
&&other
)
568 std::string
as_string () const override
;
570 void accept_vis (HIRFullVisitor
&vis
) override
;
572 ItemType
get_item_type () const override final
{ return ItemType::TUPLE_PAT
; }
575 /* Use covariance to implement clone function as returning this object rather
577 StructPatternFieldTuplePat
*clone_struct_pattern_field_impl () const override
579 return new StructPatternFieldTuplePat (*this);
583 // Identifier pattern single field in a struct pattern
584 class StructPatternFieldIdentPat
: public StructPatternField
587 std::unique_ptr
<Pattern
> ident_pattern
;
590 StructPatternFieldIdentPat (Analysis::NodeMapping mappings
, Identifier ident
,
591 std::unique_ptr
<Pattern
> ident_pattern
,
592 AST::AttrVec outer_attrs
, Location locus
)
593 : StructPatternField (mappings
, std::move (outer_attrs
), locus
),
594 ident (std::move (ident
)), ident_pattern (std::move (ident_pattern
))
597 // Copy constructor requires clone
598 StructPatternFieldIdentPat (StructPatternFieldIdentPat
const &other
)
599 : StructPatternField (other
), ident (other
.ident
),
600 ident_pattern (other
.ident_pattern
->clone_pattern ())
603 // Overload assignment operator to clone
604 StructPatternFieldIdentPat
&
605 operator= (StructPatternFieldIdentPat
const &other
)
607 StructPatternField::operator= (other
);
609 ident_pattern
= other
.ident_pattern
->clone_pattern ();
610 // outer_attrs = other.outer_attrs;
615 // default move semantics
616 StructPatternFieldIdentPat (StructPatternFieldIdentPat
&&other
) = default;
617 StructPatternFieldIdentPat
&operator= (StructPatternFieldIdentPat
&&other
)
620 std::string
as_string () const override
;
622 void accept_vis (HIRFullVisitor
&vis
) override
;
624 ItemType
get_item_type () const override final
{ return ItemType::IDENT_PAT
; }
627 /* Use covariance to implement clone function as returning this object rather
629 StructPatternFieldIdentPat
*clone_struct_pattern_field_impl () const override
631 return new StructPatternFieldIdentPat (*this);
635 // Identifier only (with no pattern) single field in a struct pattern
636 class StructPatternFieldIdent
: public StructPatternField
643 StructPatternFieldIdent (Analysis::NodeMapping mappings
, Identifier ident
,
644 bool is_ref
, Mutability mut
,
645 AST::AttrVec outer_attrs
, Location locus
)
646 : StructPatternField (mappings
, std::move (outer_attrs
), locus
),
647 has_ref (is_ref
), mut (mut
), ident (std::move (ident
))
650 std::string
as_string () const override
;
652 bool is_mut () const { return mut
== Mutability::Mut
; }
654 void accept_vis (HIRFullVisitor
&vis
) override
;
656 ItemType
get_item_type () const override final
{ return ItemType::IDENT
; }
658 Identifier
get_identifier () const { return ident
; };
661 /* Use covariance to implement clone function as returning this object rather
663 StructPatternFieldIdent
*clone_struct_pattern_field_impl () const override
665 return new StructPatternFieldIdent (*this);
669 // Elements of a struct pattern
670 struct StructPatternElements
673 std::vector
<std::unique_ptr
<StructPatternField
> > fields
;
676 // Returns whether there are any struct pattern fields
677 bool has_struct_pattern_fields () const { return !fields
.empty (); }
679 /* Returns whether the struct pattern elements is entirely empty (no fields,
681 bool is_empty () const { return !has_struct_pattern_fields (); }
683 // Constructor for StructPatternElements with both (potentially)
684 StructPatternElements (
685 std::vector
<std::unique_ptr
<StructPatternField
> > fields
)
686 : fields (std::move (fields
))
689 // Copy constructor with vector clone
690 StructPatternElements (StructPatternElements
const &other
)
692 fields
.reserve (other
.fields
.size ());
693 for (const auto &e
: other
.fields
)
694 fields
.push_back (e
->clone_struct_pattern_field ());
697 // Overloaded assignment operator with vector clone
698 StructPatternElements
&operator= (StructPatternElements
const &other
)
700 fields
.reserve (other
.fields
.size ());
701 for (const auto &e
: other
.fields
)
702 fields
.push_back (e
->clone_struct_pattern_field ());
708 StructPatternElements (StructPatternElements
&&other
) = default;
709 StructPatternElements
&operator= (StructPatternElements
&&other
) = default;
711 // Creates an empty StructPatternElements
712 static StructPatternElements
create_empty ()
714 return StructPatternElements (
715 std::vector
<std::unique_ptr
<StructPatternField
> > ());
718 std::string
as_string () const;
720 std::vector
<std::unique_ptr
<StructPatternField
> > &
721 get_struct_pattern_fields ()
727 // Struct pattern HIR node representation
728 class StructPattern
: public Pattern
730 PathInExpression path
;
731 StructPatternElements elems
;
732 Analysis::NodeMapping mappings
;
735 std::string
as_string () const override
;
737 StructPattern (Analysis::NodeMapping mappings
, PathInExpression struct_path
,
738 StructPatternElements elems
)
739 : path (std::move (struct_path
)), elems (std::move (elems
)),
743 bool has_struct_pattern_elems () const { return !elems
.is_empty (); }
745 Location
get_locus () const override
{ return path
.get_locus (); }
747 void accept_vis (HIRFullVisitor
&vis
) override
;
748 void accept_vis (HIRPatternVisitor
&vis
) override
;
750 PathInExpression
&get_path () { return path
; }
751 StructPatternElements
&get_struct_pattern_elems () { return elems
; }
753 Analysis::NodeMapping
get_pattern_mappings () const override final
758 PatternType
get_pattern_type () const override final
760 return PatternType::STRUCT
;
764 /* Use covariance to implement clone function as returning this object rather
766 StructPattern
*clone_pattern_impl () const override
768 return new StructPattern (*this);
772 // Base abstract class for patterns used in TupleStructPattern
773 class TupleStructItems
782 virtual ~TupleStructItems () {}
784 // TODO: should this store location data?
786 // Unique pointer custom clone function
787 std::unique_ptr
<TupleStructItems
> clone_tuple_struct_items () const
789 return std::unique_ptr
<TupleStructItems
> (clone_tuple_struct_items_impl ());
792 virtual std::string
as_string () const = 0;
794 virtual void accept_vis (HIRFullVisitor
&vis
) = 0;
796 virtual ItemType
get_item_type () const = 0;
799 // pure virtual clone implementation
800 virtual TupleStructItems
*clone_tuple_struct_items_impl () const = 0;
803 // Class for non-ranged tuple struct pattern patterns
804 class TupleStructItemsNoRange
: public TupleStructItems
806 std::vector
<std::unique_ptr
<Pattern
> > patterns
;
809 TupleStructItemsNoRange (std::vector
<std::unique_ptr
<Pattern
> > patterns
)
810 : patterns (std::move (patterns
))
813 // Copy constructor with vector clone
814 TupleStructItemsNoRange (TupleStructItemsNoRange
const &other
)
816 patterns
.reserve (other
.patterns
.size ());
817 for (const auto &e
: other
.patterns
)
818 patterns
.push_back (e
->clone_pattern ());
821 // Overloaded assignment operator with vector clone
822 TupleStructItemsNoRange
&operator= (TupleStructItemsNoRange
const &other
)
824 patterns
.reserve (other
.patterns
.size ());
825 for (const auto &e
: other
.patterns
)
826 patterns
.push_back (e
->clone_pattern ());
832 TupleStructItemsNoRange (TupleStructItemsNoRange
&&other
) = default;
833 TupleStructItemsNoRange
&operator= (TupleStructItemsNoRange
&&other
)
836 std::string
as_string () const override
;
838 void accept_vis (HIRFullVisitor
&vis
) override
;
840 std::vector
<std::unique_ptr
<Pattern
> > &get_patterns () { return patterns
; }
841 const std::vector
<std::unique_ptr
<Pattern
> > &get_patterns () const
846 ItemType
get_item_type () const override final
{ return ItemType::NO_RANGE
; }
849 /* Use covariance to implement clone function as returning this object rather
851 TupleStructItemsNoRange
*clone_tuple_struct_items_impl () const override
853 return new TupleStructItemsNoRange (*this);
857 // Class for ranged tuple struct pattern patterns
858 class TupleStructItemsRange
: public TupleStructItems
860 std::vector
<std::unique_ptr
<Pattern
> > lower_patterns
;
861 std::vector
<std::unique_ptr
<Pattern
> > upper_patterns
;
864 TupleStructItemsRange (std::vector
<std::unique_ptr
<Pattern
> > lower_patterns
,
865 std::vector
<std::unique_ptr
<Pattern
> > upper_patterns
)
866 : lower_patterns (std::move (lower_patterns
)),
867 upper_patterns (std::move (upper_patterns
))
870 // Copy constructor with vector clone
871 TupleStructItemsRange (TupleStructItemsRange
const &other
)
873 lower_patterns
.reserve (other
.lower_patterns
.size ());
874 for (const auto &e
: other
.lower_patterns
)
875 lower_patterns
.push_back (e
->clone_pattern ());
877 upper_patterns
.reserve (other
.upper_patterns
.size ());
878 for (const auto &e
: other
.upper_patterns
)
879 upper_patterns
.push_back (e
->clone_pattern ());
882 // Overloaded assignment operator to clone
883 TupleStructItemsRange
&operator= (TupleStructItemsRange
const &other
)
885 lower_patterns
.reserve (other
.lower_patterns
.size ());
886 for (const auto &e
: other
.lower_patterns
)
887 lower_patterns
.push_back (e
->clone_pattern ());
889 upper_patterns
.reserve (other
.upper_patterns
.size ());
890 for (const auto &e
: other
.upper_patterns
)
891 upper_patterns
.push_back (e
->clone_pattern ());
897 TupleStructItemsRange (TupleStructItemsRange
&&other
) = default;
898 TupleStructItemsRange
&operator= (TupleStructItemsRange
&&other
) = default;
900 std::string
as_string () const override
;
902 void accept_vis (HIRFullVisitor
&vis
) override
;
904 std::vector
<std::unique_ptr
<Pattern
> > &get_lower_patterns ()
906 return lower_patterns
;
908 const std::vector
<std::unique_ptr
<Pattern
> > &get_lower_patterns () const
910 return lower_patterns
;
913 // TODO: seems kinda dodgy. Think of better way.
914 std::vector
<std::unique_ptr
<Pattern
> > &get_upper_patterns ()
916 return upper_patterns
;
918 const std::vector
<std::unique_ptr
<Pattern
> > &get_upper_patterns () const
920 return upper_patterns
;
923 ItemType
get_item_type () const override final
{ return ItemType::RANGE
; }
926 /* Use covariance to implement clone function as returning this object rather
928 TupleStructItemsRange
*clone_tuple_struct_items_impl () const override
930 return new TupleStructItemsRange (*this);
934 // HIR node representing a tuple struct pattern
935 class TupleStructPattern
: public Pattern
937 PathInExpression path
;
938 std::unique_ptr
<TupleStructItems
> items
;
939 Analysis::NodeMapping mappings
;
941 /* TOOD: should this store location data? current accessor uses path location
945 std::string
as_string () const override
;
947 TupleStructPattern (Analysis::NodeMapping mappings
,
948 PathInExpression tuple_struct_path
,
949 std::unique_ptr
<TupleStructItems
> items
)
950 : path (std::move (tuple_struct_path
)), items (std::move (items
)),
954 // Copy constructor required to clone
955 TupleStructPattern (TupleStructPattern
const &other
)
956 : path (other
.path
), items (other
.items
->clone_tuple_struct_items ()),
957 mappings (other
.mappings
)
960 // Operator overload assignment operator to clone
961 TupleStructPattern
&operator= (TupleStructPattern
const &other
)
964 items
= other
.items
->clone_tuple_struct_items ();
965 mappings
= other
.mappings
;
971 TupleStructPattern (TupleStructPattern
&&other
) = default;
972 TupleStructPattern
&operator= (TupleStructPattern
&&other
) = default;
974 Location
get_locus () const override
{ return path
.get_locus (); }
976 void accept_vis (HIRFullVisitor
&vis
) override
;
977 void accept_vis (HIRPatternVisitor
&vis
) override
;
979 PathInExpression
&get_path () { return path
; }
981 std::unique_ptr
<TupleStructItems
> &get_items () { return items
; }
983 Analysis::NodeMapping
get_pattern_mappings () const override final
988 PatternType
get_pattern_type () const override final
990 return PatternType::TUPLE_STRUCT
;
994 /* Use covariance to implement clone function as returning this object rather
996 TupleStructPattern
*clone_pattern_impl () const override
998 return new TupleStructPattern (*this);
1002 // Base abstract class representing TuplePattern patterns
1003 class TuplePatternItems
1006 enum TuplePatternItemType
1012 virtual ~TuplePatternItems () {}
1014 // TODO: should this store location data?
1016 // Unique pointer custom clone function
1017 std::unique_ptr
<TuplePatternItems
> clone_tuple_pattern_items () const
1019 return std::unique_ptr
<TuplePatternItems
> (
1020 clone_tuple_pattern_items_impl ());
1023 virtual std::string
as_string () const = 0;
1025 virtual void accept_vis (HIRFullVisitor
&vis
) = 0;
1027 virtual TuplePatternItemType
get_pattern_type () const = 0;
1030 // pure virtual clone implementation
1031 virtual TuplePatternItems
*clone_tuple_pattern_items_impl () const = 0;
1034 // Class representing TuplePattern patterns where there are multiple patterns
1035 class TuplePatternItemsMultiple
: public TuplePatternItems
1037 std::vector
<std::unique_ptr
<Pattern
> > patterns
;
1040 TuplePatternItemsMultiple (std::vector
<std::unique_ptr
<Pattern
> > patterns
)
1041 : patterns (std::move (patterns
))
1044 // Copy constructor with vector clone
1045 TuplePatternItemsMultiple (TuplePatternItemsMultiple
const &other
)
1047 patterns
.reserve (other
.patterns
.size ());
1048 for (const auto &e
: other
.patterns
)
1049 patterns
.push_back (e
->clone_pattern ());
1052 // Overloaded assignment operator to vector clone
1053 TuplePatternItemsMultiple
&operator= (TuplePatternItemsMultiple
const &other
)
1055 patterns
.reserve (other
.patterns
.size ());
1056 for (const auto &e
: other
.patterns
)
1057 patterns
.push_back (e
->clone_pattern ());
1062 // move constructors
1063 TuplePatternItemsMultiple (TuplePatternItemsMultiple
&&other
) = default;
1064 TuplePatternItemsMultiple
&operator= (TuplePatternItemsMultiple
&&other
)
1067 std::string
as_string () const override
;
1069 void accept_vis (HIRFullVisitor
&vis
) override
;
1071 TuplePatternItemType
get_pattern_type () const override
1073 return TuplePatternItemType::MULTIPLE
;
1076 std::vector
<std::unique_ptr
<Pattern
> > &get_patterns () { return patterns
; }
1077 const std::vector
<std::unique_ptr
<Pattern
> > &get_patterns () const
1083 /* Use covariance to implement clone function as returning this object rather
1085 TuplePatternItemsMultiple
*clone_tuple_pattern_items_impl () const override
1087 return new TuplePatternItemsMultiple (*this);
1091 // Class representing TuplePattern patterns where there are a range of patterns
1092 class TuplePatternItemsRanged
: public TuplePatternItems
1094 std::vector
<std::unique_ptr
<Pattern
> > lower_patterns
;
1095 std::vector
<std::unique_ptr
<Pattern
> > upper_patterns
;
1098 TuplePatternItemsRanged (
1099 std::vector
<std::unique_ptr
<Pattern
> > lower_patterns
,
1100 std::vector
<std::unique_ptr
<Pattern
> > upper_patterns
)
1101 : lower_patterns (std::move (lower_patterns
)),
1102 upper_patterns (std::move (upper_patterns
))
1105 // Copy constructor with vector clone
1106 TuplePatternItemsRanged (TuplePatternItemsRanged
const &other
)
1108 lower_patterns
.reserve (other
.lower_patterns
.size ());
1109 for (const auto &e
: other
.lower_patterns
)
1110 lower_patterns
.push_back (e
->clone_pattern ());
1112 upper_patterns
.reserve (other
.upper_patterns
.size ());
1113 for (const auto &e
: other
.upper_patterns
)
1114 upper_patterns
.push_back (e
->clone_pattern ());
1117 // Overloaded assignment operator to clone
1118 TuplePatternItemsRanged
&operator= (TuplePatternItemsRanged
const &other
)
1120 lower_patterns
.reserve (other
.lower_patterns
.size ());
1121 for (const auto &e
: other
.lower_patterns
)
1122 lower_patterns
.push_back (e
->clone_pattern ());
1124 upper_patterns
.reserve (other
.upper_patterns
.size ());
1125 for (const auto &e
: other
.upper_patterns
)
1126 upper_patterns
.push_back (e
->clone_pattern ());
1131 // move constructors
1132 TuplePatternItemsRanged (TuplePatternItemsRanged
&&other
) = default;
1133 TuplePatternItemsRanged
&operator= (TuplePatternItemsRanged
&&other
)
1136 std::string
as_string () const override
;
1138 void accept_vis (HIRFullVisitor
&vis
) override
;
1140 TuplePatternItemType
get_pattern_type () const override
1142 return TuplePatternItemType::RANGED
;
1145 std::vector
<std::unique_ptr
<Pattern
> > &get_lower_patterns ()
1147 return lower_patterns
;
1149 const std::vector
<std::unique_ptr
<Pattern
> > &get_lower_patterns () const
1151 return lower_patterns
;
1154 std::vector
<std::unique_ptr
<Pattern
> > &get_upper_patterns ()
1156 return upper_patterns
;
1158 const std::vector
<std::unique_ptr
<Pattern
> > &get_upper_patterns () const
1160 return upper_patterns
;
1164 /* Use covariance to implement clone function as returning this object rather
1166 TuplePatternItemsRanged
*clone_tuple_pattern_items_impl () const override
1168 return new TuplePatternItemsRanged (*this);
1172 // HIR node representing a tuple pattern
1173 class TuplePattern
: public Pattern
1175 std::unique_ptr
<TuplePatternItems
> items
;
1177 Analysis::NodeMapping mappings
;
1180 std::string
as_string () const override
;
1182 // Returns true if the tuple pattern has items
1183 bool has_tuple_pattern_items () const { return items
!= nullptr; }
1185 TuplePattern (Analysis::NodeMapping mappings
,
1186 std::unique_ptr
<TuplePatternItems
> items
, Location locus
)
1187 : items (std::move (items
)), locus (locus
), mappings (mappings
)
1190 // Copy constructor requires clone
1191 TuplePattern (TuplePattern
const &other
)
1192 : items (other
.items
->clone_tuple_pattern_items ()), locus (other
.locus
),
1193 mappings (other
.mappings
)
1196 // Overload assignment operator to clone
1197 TuplePattern
&operator= (TuplePattern
const &other
)
1199 items
= other
.items
->clone_tuple_pattern_items ();
1200 locus
= other
.locus
;
1201 mappings
= other
.mappings
;
1206 Location
get_locus () const override
{ return locus
; }
1208 void accept_vis (HIRFullVisitor
&vis
) override
;
1209 void accept_vis (HIRPatternVisitor
&vis
) override
;
1211 Analysis::NodeMapping
get_pattern_mappings () const override final
1216 PatternType
get_pattern_type () const override final
1218 return PatternType::TUPLE
;
1221 std::unique_ptr
<TuplePatternItems
> &get_items () { return items
; }
1222 const std::unique_ptr
<TuplePatternItems
> &get_items () const { return items
; }
1225 /* Use covariance to implement clone function as returning this object rather
1227 TuplePattern
*clone_pattern_impl () const override
1229 return new TuplePattern (*this);
1233 // HIR node representing patterns that can match slices and arrays
1234 class SlicePattern
: public Pattern
1236 std::vector
<std::unique_ptr
<Pattern
> > items
;
1238 Analysis::NodeMapping mappings
;
1241 std::string
as_string () const override
;
1243 SlicePattern (Analysis::NodeMapping mappings
,
1244 std::vector
<std::unique_ptr
<Pattern
> > items
, Location locus
)
1245 : items (std::move (items
)), locus (locus
), mappings (mappings
)
1248 // Copy constructor with vector clone
1249 SlicePattern (SlicePattern
const &other
)
1250 : locus (other
.locus
), mappings (other
.mappings
)
1252 items
.reserve (other
.items
.size ());
1253 for (const auto &e
: other
.items
)
1254 items
.push_back (e
->clone_pattern ());
1257 // Overloaded assignment operator to vector clone
1258 SlicePattern
&operator= (SlicePattern
const &other
)
1260 locus
= other
.locus
;
1261 mappings
= other
.mappings
;
1263 items
.reserve (other
.items
.size ());
1264 for (const auto &e
: other
.items
)
1265 items
.push_back (e
->clone_pattern ());
1270 // move constructors
1271 SlicePattern (SlicePattern
&&other
) = default;
1272 SlicePattern
&operator= (SlicePattern
&&other
) = default;
1274 Location
get_locus () const override
{ return locus
; }
1276 void accept_vis (HIRFullVisitor
&vis
) override
;
1277 void accept_vis (HIRPatternVisitor
&vis
) override
;
1279 Analysis::NodeMapping
get_pattern_mappings () const override final
1284 PatternType
get_pattern_type () const override final
1286 return PatternType::SLICE
;
1290 /* Use covariance to implement clone function as returning this object rather
1292 SlicePattern
*clone_pattern_impl () const override
1294 return new SlicePattern (*this);
1298 // Moved definition to rust-path.h
1301 // Forward decls for paths (defined in rust-path.h)
1302 class PathInExpression
;
1303 class QualifiedPathInExpression
;