1 // Copyright (C) 2020-2022 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
429 std::unique_ptr
<Pattern
> pattern
;
431 Analysis::NodeMapping mappings
;
434 std::string
as_string () const override
;
436 ReferencePattern (Analysis::NodeMapping mappings
,
437 std::unique_ptr
<Pattern
> pattern
, Mutability reference_mut
,
438 bool ref_has_two_amps
, Location locus
)
439 : has_two_amps (ref_has_two_amps
), mut (reference_mut
),
440 pattern (std::move (pattern
)), locus (locus
), mappings (mappings
)
443 // Copy constructor requires clone
444 ReferencePattern (ReferencePattern
const &other
)
445 : has_two_amps (other
.has_two_amps
), mut (other
.mut
),
446 pattern (other
.pattern
->clone_pattern ()), locus (other
.locus
),
447 mappings (other
.mappings
)
450 // Overload assignment operator to clone
451 ReferencePattern
&operator= (ReferencePattern
const &other
)
453 pattern
= other
.pattern
->clone_pattern ();
455 has_two_amps
= other
.has_two_amps
;
457 mappings
= other
.mappings
;
462 // default move semantics
463 ReferencePattern (ReferencePattern
&&other
) = default;
464 ReferencePattern
&operator= (ReferencePattern
&&other
) = default;
466 bool is_mut () const { return mut
== Mutability::Mut
; }
468 void accept_vis (HIRFullVisitor
&vis
) override
;
469 void accept_vis (HIRPatternVisitor
&vis
) override
;
471 Analysis::NodeMapping
get_pattern_mappings () const override final
476 Location
get_locus () const override final
{ return locus
; }
478 PatternType
get_pattern_type () const override final
480 return PatternType::REFERENCE
;
484 /* Use covariance to implement clone function as returning this object rather
486 ReferencePattern
*clone_pattern_impl () const override
488 return new ReferencePattern (*this);
492 // Base class for a single field in a struct pattern - abstract
493 class StructPatternField
495 AST::AttrVec outer_attrs
;
497 Analysis::NodeMapping mappings
;
507 virtual ~StructPatternField () {}
509 // Unique pointer custom clone function
510 std::unique_ptr
<StructPatternField
> clone_struct_pattern_field () const
512 return std::unique_ptr
<StructPatternField
> (
513 clone_struct_pattern_field_impl ());
516 virtual std::string
as_string () const;
517 virtual void accept_vis (HIRFullVisitor
&vis
) = 0;
518 virtual ItemType
get_item_type () const = 0;
520 Location
get_locus () const { return locus
; }
521 Analysis::NodeMapping
get_mappings () const { return mappings
; };
524 StructPatternField (Analysis::NodeMapping mappings
,
525 AST::AttrVec outer_attribs
, Location locus
)
526 : outer_attrs (std::move (outer_attribs
)), locus (locus
),
530 // Clone function implementation as pure virtual method
531 virtual StructPatternField
*clone_struct_pattern_field_impl () const = 0;
534 // Tuple pattern single field in a struct pattern
535 class StructPatternFieldTuplePat
: public StructPatternField
538 std::unique_ptr
<Pattern
> tuple_pattern
;
541 StructPatternFieldTuplePat (Analysis::NodeMapping mappings
, TupleIndex index
,
542 std::unique_ptr
<Pattern
> tuple_pattern
,
543 AST::AttrVec outer_attribs
, Location locus
)
544 : StructPatternField (mappings
, std::move (outer_attribs
), locus
),
545 index (index
), tuple_pattern (std::move (tuple_pattern
))
548 // Copy constructor requires clone
549 StructPatternFieldTuplePat (StructPatternFieldTuplePat
const &other
)
550 : StructPatternField (other
), index (other
.index
),
551 tuple_pattern (other
.tuple_pattern
->clone_pattern ())
554 // Overload assignment operator to perform clone
555 StructPatternFieldTuplePat
&
556 operator= (StructPatternFieldTuplePat
const &other
)
558 StructPatternField::operator= (other
);
559 tuple_pattern
= other
.tuple_pattern
->clone_pattern ();
561 // outer_attrs = other.outer_attrs;
566 // default move semantics
567 StructPatternFieldTuplePat (StructPatternFieldTuplePat
&&other
) = default;
568 StructPatternFieldTuplePat
&operator= (StructPatternFieldTuplePat
&&other
)
571 std::string
as_string () const override
;
573 void accept_vis (HIRFullVisitor
&vis
) override
;
575 ItemType
get_item_type () const override final
{ return ItemType::TUPLE_PAT
; }
578 /* Use covariance to implement clone function as returning this object rather
580 StructPatternFieldTuplePat
*clone_struct_pattern_field_impl () const override
582 return new StructPatternFieldTuplePat (*this);
586 // Identifier pattern single field in a struct pattern
587 class StructPatternFieldIdentPat
: public StructPatternField
590 std::unique_ptr
<Pattern
> ident_pattern
;
593 StructPatternFieldIdentPat (Analysis::NodeMapping mappings
, Identifier ident
,
594 std::unique_ptr
<Pattern
> ident_pattern
,
595 AST::AttrVec outer_attrs
, Location locus
)
596 : StructPatternField (mappings
, std::move (outer_attrs
), locus
),
597 ident (std::move (ident
)), ident_pattern (std::move (ident_pattern
))
600 // Copy constructor requires clone
601 StructPatternFieldIdentPat (StructPatternFieldIdentPat
const &other
)
602 : StructPatternField (other
), ident (other
.ident
),
603 ident_pattern (other
.ident_pattern
->clone_pattern ())
606 // Overload assignment operator to clone
607 StructPatternFieldIdentPat
&
608 operator= (StructPatternFieldIdentPat
const &other
)
610 StructPatternField::operator= (other
);
612 ident_pattern
= other
.ident_pattern
->clone_pattern ();
613 // outer_attrs = other.outer_attrs;
618 // default move semantics
619 StructPatternFieldIdentPat (StructPatternFieldIdentPat
&&other
) = default;
620 StructPatternFieldIdentPat
&operator= (StructPatternFieldIdentPat
&&other
)
623 std::string
as_string () const override
;
625 void accept_vis (HIRFullVisitor
&vis
) override
;
627 ItemType
get_item_type () const override final
{ return ItemType::IDENT_PAT
; }
630 /* Use covariance to implement clone function as returning this object rather
632 StructPatternFieldIdentPat
*clone_struct_pattern_field_impl () const override
634 return new StructPatternFieldIdentPat (*this);
638 // Identifier only (with no pattern) single field in a struct pattern
639 class StructPatternFieldIdent
: public StructPatternField
646 StructPatternFieldIdent (Analysis::NodeMapping mappings
, Identifier ident
,
647 bool is_ref
, Mutability mut
,
648 AST::AttrVec outer_attrs
, Location locus
)
649 : StructPatternField (mappings
, std::move (outer_attrs
), locus
),
650 has_ref (is_ref
), mut (mut
), ident (std::move (ident
))
653 std::string
as_string () const override
;
655 bool is_mut () const { return mut
== Mutability::Mut
; }
657 void accept_vis (HIRFullVisitor
&vis
) override
;
659 ItemType
get_item_type () const override final
{ return ItemType::IDENT
; }
661 Identifier
get_identifier () const { return ident
; };
664 /* Use covariance to implement clone function as returning this object rather
666 StructPatternFieldIdent
*clone_struct_pattern_field_impl () const override
668 return new StructPatternFieldIdent (*this);
672 // Elements of a struct pattern
673 struct StructPatternElements
676 std::vector
<std::unique_ptr
<StructPatternField
> > fields
;
679 // Returns whether there are any struct pattern fields
680 bool has_struct_pattern_fields () const { return !fields
.empty (); }
682 /* Returns whether the struct pattern elements is entirely empty (no fields,
684 bool is_empty () const { return !has_struct_pattern_fields (); }
686 // Constructor for StructPatternElements with both (potentially)
687 StructPatternElements (
688 std::vector
<std::unique_ptr
<StructPatternField
> > fields
)
689 : fields (std::move (fields
))
692 // Copy constructor with vector clone
693 StructPatternElements (StructPatternElements
const &other
)
695 fields
.reserve (other
.fields
.size ());
696 for (const auto &e
: other
.fields
)
697 fields
.push_back (e
->clone_struct_pattern_field ());
700 // Overloaded assignment operator with vector clone
701 StructPatternElements
&operator= (StructPatternElements
const &other
)
703 fields
.reserve (other
.fields
.size ());
704 for (const auto &e
: other
.fields
)
705 fields
.push_back (e
->clone_struct_pattern_field ());
711 StructPatternElements (StructPatternElements
&&other
) = default;
712 StructPatternElements
&operator= (StructPatternElements
&&other
) = default;
714 // Creates an empty StructPatternElements
715 static StructPatternElements
create_empty ()
717 return StructPatternElements (
718 std::vector
<std::unique_ptr
<StructPatternField
> > ());
721 std::string
as_string () const;
723 std::vector
<std::unique_ptr
<StructPatternField
> > &
724 get_struct_pattern_fields ()
730 // Struct pattern HIR node representation
731 class StructPattern
: public Pattern
733 PathInExpression path
;
734 StructPatternElements elems
;
735 Analysis::NodeMapping mappings
;
738 std::string
as_string () const override
;
740 StructPattern (Analysis::NodeMapping mappings
, PathInExpression struct_path
,
741 StructPatternElements elems
)
742 : path (std::move (struct_path
)), elems (std::move (elems
)),
746 bool has_struct_pattern_elems () const { return !elems
.is_empty (); }
748 Location
get_locus () const override
{ return path
.get_locus (); }
750 void accept_vis (HIRFullVisitor
&vis
) override
;
751 void accept_vis (HIRPatternVisitor
&vis
) override
;
753 PathInExpression
&get_path () { return path
; }
754 StructPatternElements
&get_struct_pattern_elems () { return elems
; }
756 Analysis::NodeMapping
get_pattern_mappings () const override final
761 PatternType
get_pattern_type () const override final
763 return PatternType::STRUCT
;
767 /* Use covariance to implement clone function as returning this object rather
769 StructPattern
*clone_pattern_impl () const override
771 return new StructPattern (*this);
775 // Base abstract class for patterns used in TupleStructPattern
776 class TupleStructItems
785 virtual ~TupleStructItems () {}
787 // TODO: should this store location data?
789 // Unique pointer custom clone function
790 std::unique_ptr
<TupleStructItems
> clone_tuple_struct_items () const
792 return std::unique_ptr
<TupleStructItems
> (clone_tuple_struct_items_impl ());
795 virtual std::string
as_string () const = 0;
797 virtual void accept_vis (HIRFullVisitor
&vis
) = 0;
799 virtual ItemType
get_item_type () const = 0;
802 // pure virtual clone implementation
803 virtual TupleStructItems
*clone_tuple_struct_items_impl () const = 0;
806 // Class for non-ranged tuple struct pattern patterns
807 class TupleStructItemsNoRange
: public TupleStructItems
809 std::vector
<std::unique_ptr
<Pattern
> > patterns
;
812 TupleStructItemsNoRange (std::vector
<std::unique_ptr
<Pattern
> > patterns
)
813 : patterns (std::move (patterns
))
816 // Copy constructor with vector clone
817 TupleStructItemsNoRange (TupleStructItemsNoRange
const &other
)
819 patterns
.reserve (other
.patterns
.size ());
820 for (const auto &e
: other
.patterns
)
821 patterns
.push_back (e
->clone_pattern ());
824 // Overloaded assignment operator with vector clone
825 TupleStructItemsNoRange
&operator= (TupleStructItemsNoRange
const &other
)
827 patterns
.reserve (other
.patterns
.size ());
828 for (const auto &e
: other
.patterns
)
829 patterns
.push_back (e
->clone_pattern ());
835 TupleStructItemsNoRange (TupleStructItemsNoRange
&&other
) = default;
836 TupleStructItemsNoRange
&operator= (TupleStructItemsNoRange
&&other
)
839 std::string
as_string () const override
;
841 void accept_vis (HIRFullVisitor
&vis
) override
;
843 std::vector
<std::unique_ptr
<Pattern
> > &get_patterns () { return patterns
; }
844 const std::vector
<std::unique_ptr
<Pattern
> > &get_patterns () const
849 ItemType
get_item_type () const override final
{ return ItemType::NO_RANGE
; }
852 /* Use covariance to implement clone function as returning this object rather
854 TupleStructItemsNoRange
*clone_tuple_struct_items_impl () const override
856 return new TupleStructItemsNoRange (*this);
860 // Class for ranged tuple struct pattern patterns
861 class TupleStructItemsRange
: public TupleStructItems
863 std::vector
<std::unique_ptr
<Pattern
> > lower_patterns
;
864 std::vector
<std::unique_ptr
<Pattern
> > upper_patterns
;
867 TupleStructItemsRange (std::vector
<std::unique_ptr
<Pattern
> > lower_patterns
,
868 std::vector
<std::unique_ptr
<Pattern
> > upper_patterns
)
869 : lower_patterns (std::move (lower_patterns
)),
870 upper_patterns (std::move (upper_patterns
))
873 // Copy constructor with vector clone
874 TupleStructItemsRange (TupleStructItemsRange
const &other
)
876 lower_patterns
.reserve (other
.lower_patterns
.size ());
877 for (const auto &e
: other
.lower_patterns
)
878 lower_patterns
.push_back (e
->clone_pattern ());
880 upper_patterns
.reserve (other
.upper_patterns
.size ());
881 for (const auto &e
: other
.upper_patterns
)
882 upper_patterns
.push_back (e
->clone_pattern ());
885 // Overloaded assignment operator to clone
886 TupleStructItemsRange
&operator= (TupleStructItemsRange
const &other
)
888 lower_patterns
.reserve (other
.lower_patterns
.size ());
889 for (const auto &e
: other
.lower_patterns
)
890 lower_patterns
.push_back (e
->clone_pattern ());
892 upper_patterns
.reserve (other
.upper_patterns
.size ());
893 for (const auto &e
: other
.upper_patterns
)
894 upper_patterns
.push_back (e
->clone_pattern ());
900 TupleStructItemsRange (TupleStructItemsRange
&&other
) = default;
901 TupleStructItemsRange
&operator= (TupleStructItemsRange
&&other
) = default;
903 std::string
as_string () const override
;
905 void accept_vis (HIRFullVisitor
&vis
) override
;
907 std::vector
<std::unique_ptr
<Pattern
> > &get_lower_patterns ()
909 return lower_patterns
;
911 const std::vector
<std::unique_ptr
<Pattern
> > &get_lower_patterns () const
913 return lower_patterns
;
916 // TODO: seems kinda dodgy. Think of better way.
917 std::vector
<std::unique_ptr
<Pattern
> > &get_upper_patterns ()
919 return upper_patterns
;
921 const std::vector
<std::unique_ptr
<Pattern
> > &get_upper_patterns () const
923 return upper_patterns
;
926 ItemType
get_item_type () const override final
{ return ItemType::RANGE
; }
929 /* Use covariance to implement clone function as returning this object rather
931 TupleStructItemsRange
*clone_tuple_struct_items_impl () const override
933 return new TupleStructItemsRange (*this);
937 // HIR node representing a tuple struct pattern
938 class TupleStructPattern
: public Pattern
940 PathInExpression path
;
941 std::unique_ptr
<TupleStructItems
> items
;
942 Analysis::NodeMapping mappings
;
944 /* TOOD: should this store location data? current accessor uses path location
948 std::string
as_string () const override
;
950 TupleStructPattern (Analysis::NodeMapping mappings
,
951 PathInExpression tuple_struct_path
,
952 std::unique_ptr
<TupleStructItems
> items
)
953 : path (std::move (tuple_struct_path
)), items (std::move (items
)),
957 // Copy constructor required to clone
958 TupleStructPattern (TupleStructPattern
const &other
)
959 : path (other
.path
), items (other
.items
->clone_tuple_struct_items ()),
960 mappings (other
.mappings
)
963 // Operator overload assignment operator to clone
964 TupleStructPattern
&operator= (TupleStructPattern
const &other
)
967 items
= other
.items
->clone_tuple_struct_items ();
968 mappings
= other
.mappings
;
974 TupleStructPattern (TupleStructPattern
&&other
) = default;
975 TupleStructPattern
&operator= (TupleStructPattern
&&other
) = default;
977 Location
get_locus () const override
{ return path
.get_locus (); }
979 void accept_vis (HIRFullVisitor
&vis
) override
;
980 void accept_vis (HIRPatternVisitor
&vis
) override
;
982 PathInExpression
&get_path () { return path
; }
984 std::unique_ptr
<TupleStructItems
> &get_items () { return items
; }
986 Analysis::NodeMapping
get_pattern_mappings () const override final
991 PatternType
get_pattern_type () const override final
993 return PatternType::TUPLE_STRUCT
;
997 /* Use covariance to implement clone function as returning this object rather
999 TupleStructPattern
*clone_pattern_impl () const override
1001 return new TupleStructPattern (*this);
1005 // Base abstract class representing TuplePattern patterns
1006 class TuplePatternItems
1009 enum TuplePatternItemType
1015 virtual ~TuplePatternItems () {}
1017 // TODO: should this store location data?
1019 // Unique pointer custom clone function
1020 std::unique_ptr
<TuplePatternItems
> clone_tuple_pattern_items () const
1022 return std::unique_ptr
<TuplePatternItems
> (
1023 clone_tuple_pattern_items_impl ());
1026 virtual std::string
as_string () const = 0;
1028 virtual void accept_vis (HIRFullVisitor
&vis
) = 0;
1030 virtual TuplePatternItemType
get_pattern_type () const = 0;
1033 // pure virtual clone implementation
1034 virtual TuplePatternItems
*clone_tuple_pattern_items_impl () const = 0;
1037 // Class representing TuplePattern patterns where there are multiple patterns
1038 class TuplePatternItemsMultiple
: public TuplePatternItems
1040 std::vector
<std::unique_ptr
<Pattern
> > patterns
;
1043 TuplePatternItemsMultiple (std::vector
<std::unique_ptr
<Pattern
> > patterns
)
1044 : patterns (std::move (patterns
))
1047 // Copy constructor with vector clone
1048 TuplePatternItemsMultiple (TuplePatternItemsMultiple
const &other
)
1050 patterns
.reserve (other
.patterns
.size ());
1051 for (const auto &e
: other
.patterns
)
1052 patterns
.push_back (e
->clone_pattern ());
1055 // Overloaded assignment operator to vector clone
1056 TuplePatternItemsMultiple
&operator= (TuplePatternItemsMultiple
const &other
)
1058 patterns
.reserve (other
.patterns
.size ());
1059 for (const auto &e
: other
.patterns
)
1060 patterns
.push_back (e
->clone_pattern ());
1065 // move constructors
1066 TuplePatternItemsMultiple (TuplePatternItemsMultiple
&&other
) = default;
1067 TuplePatternItemsMultiple
&operator= (TuplePatternItemsMultiple
&&other
)
1070 std::string
as_string () const override
;
1072 void accept_vis (HIRFullVisitor
&vis
) override
;
1074 TuplePatternItemType
get_pattern_type () const override
1076 return TuplePatternItemType::MULTIPLE
;
1079 std::vector
<std::unique_ptr
<Pattern
> > &get_patterns () { return patterns
; }
1080 const std::vector
<std::unique_ptr
<Pattern
> > &get_patterns () const
1086 /* Use covariance to implement clone function as returning this object rather
1088 TuplePatternItemsMultiple
*clone_tuple_pattern_items_impl () const override
1090 return new TuplePatternItemsMultiple (*this);
1094 // Class representing TuplePattern patterns where there are a range of patterns
1095 class TuplePatternItemsRanged
: public TuplePatternItems
1097 std::vector
<std::unique_ptr
<Pattern
> > lower_patterns
;
1098 std::vector
<std::unique_ptr
<Pattern
> > upper_patterns
;
1101 TuplePatternItemsRanged (
1102 std::vector
<std::unique_ptr
<Pattern
> > lower_patterns
,
1103 std::vector
<std::unique_ptr
<Pattern
> > upper_patterns
)
1104 : lower_patterns (std::move (lower_patterns
)),
1105 upper_patterns (std::move (upper_patterns
))
1108 // Copy constructor with vector clone
1109 TuplePatternItemsRanged (TuplePatternItemsRanged
const &other
)
1111 lower_patterns
.reserve (other
.lower_patterns
.size ());
1112 for (const auto &e
: other
.lower_patterns
)
1113 lower_patterns
.push_back (e
->clone_pattern ());
1115 upper_patterns
.reserve (other
.upper_patterns
.size ());
1116 for (const auto &e
: other
.upper_patterns
)
1117 upper_patterns
.push_back (e
->clone_pattern ());
1120 // Overloaded assignment operator to clone
1121 TuplePatternItemsRanged
&operator= (TuplePatternItemsRanged
const &other
)
1123 lower_patterns
.reserve (other
.lower_patterns
.size ());
1124 for (const auto &e
: other
.lower_patterns
)
1125 lower_patterns
.push_back (e
->clone_pattern ());
1127 upper_patterns
.reserve (other
.upper_patterns
.size ());
1128 for (const auto &e
: other
.upper_patterns
)
1129 upper_patterns
.push_back (e
->clone_pattern ());
1134 // move constructors
1135 TuplePatternItemsRanged (TuplePatternItemsRanged
&&other
) = default;
1136 TuplePatternItemsRanged
&operator= (TuplePatternItemsRanged
&&other
)
1139 std::string
as_string () const override
;
1141 void accept_vis (HIRFullVisitor
&vis
) override
;
1143 TuplePatternItemType
get_pattern_type () const override
1145 return TuplePatternItemType::RANGED
;
1149 /* Use covariance to implement clone function as returning this object rather
1151 TuplePatternItemsRanged
*clone_tuple_pattern_items_impl () const override
1153 return new TuplePatternItemsRanged (*this);
1157 // HIR node representing a tuple pattern
1158 class TuplePattern
: public Pattern
1160 std::unique_ptr
<TuplePatternItems
> items
;
1162 Analysis::NodeMapping mappings
;
1165 std::string
as_string () const override
;
1167 // Returns true if the tuple pattern has items
1168 bool has_tuple_pattern_items () const { return items
!= nullptr; }
1170 TuplePattern (Analysis::NodeMapping mappings
,
1171 std::unique_ptr
<TuplePatternItems
> items
, Location locus
)
1172 : items (std::move (items
)), locus (locus
), mappings (mappings
)
1175 // Copy constructor requires clone
1176 TuplePattern (TuplePattern
const &other
)
1177 : items (other
.items
->clone_tuple_pattern_items ()), locus (other
.locus
),
1178 mappings (other
.mappings
)
1181 // Overload assignment operator to clone
1182 TuplePattern
&operator= (TuplePattern
const &other
)
1184 items
= other
.items
->clone_tuple_pattern_items ();
1185 locus
= other
.locus
;
1186 mappings
= other
.mappings
;
1191 Location
get_locus () const override
{ return locus
; }
1193 void accept_vis (HIRFullVisitor
&vis
) override
;
1194 void accept_vis (HIRPatternVisitor
&vis
) override
;
1196 Analysis::NodeMapping
get_pattern_mappings () const override final
1201 PatternType
get_pattern_type () const override final
1203 return PatternType::TUPLE
;
1206 std::unique_ptr
<TuplePatternItems
> &get_items () { return items
; }
1207 const std::unique_ptr
<TuplePatternItems
> &get_items () const { return items
; }
1210 /* Use covariance to implement clone function as returning this object rather
1212 TuplePattern
*clone_pattern_impl () const override
1214 return new TuplePattern (*this);
1218 // HIR node representing a pattern in parentheses, used to control precedence
1219 class GroupedPattern
: public Pattern
1221 std::unique_ptr
<Pattern
> pattern_in_parens
;
1223 Analysis::NodeMapping mappings
;
1226 std::string
as_string () const override
1228 return "(" + pattern_in_parens
->as_string () + ")";
1231 GroupedPattern (Analysis::NodeMapping mappings
,
1232 std::unique_ptr
<Pattern
> pattern_in_parens
, Location locus
)
1233 : pattern_in_parens (std::move (pattern_in_parens
)), locus (locus
),
1237 // Copy constructor uses clone
1238 GroupedPattern (GroupedPattern
const &other
)
1239 : pattern_in_parens (other
.pattern_in_parens
->clone_pattern ()),
1240 locus (other
.locus
), mappings (other
.mappings
)
1243 // Overload assignment operator to clone
1244 GroupedPattern
&operator= (GroupedPattern
const &other
)
1246 pattern_in_parens
= other
.pattern_in_parens
->clone_pattern ();
1247 locus
= other
.locus
;
1248 mappings
= other
.mappings
;
1253 // default move semantics
1254 GroupedPattern (GroupedPattern
&&other
) = default;
1255 GroupedPattern
&operator= (GroupedPattern
&&other
) = default;
1257 Location
get_locus () const override
{ return locus
; }
1259 void accept_vis (HIRFullVisitor
&vis
) override
;
1260 void accept_vis (HIRPatternVisitor
&vis
) override
;
1262 Analysis::NodeMapping
get_pattern_mappings () const override final
1267 PatternType
get_pattern_type () const override final
1269 return PatternType::GROUPED
;
1272 std::unique_ptr
<Pattern
> &get_item () { return pattern_in_parens
; }
1273 const std::unique_ptr
<Pattern
> &get_item () const
1275 return pattern_in_parens
;
1279 /* Use covariance to implement clone function as returning this object rather
1281 GroupedPattern
*clone_pattern_impl () const override
1283 return new GroupedPattern (*this);
1287 // HIR node representing patterns that can match slices and arrays
1288 class SlicePattern
: public Pattern
1290 std::vector
<std::unique_ptr
<Pattern
> > items
;
1292 Analysis::NodeMapping mappings
;
1295 std::string
as_string () const override
;
1297 SlicePattern (Analysis::NodeMapping mappings
,
1298 std::vector
<std::unique_ptr
<Pattern
> > items
, Location locus
)
1299 : items (std::move (items
)), locus (locus
), mappings (mappings
)
1302 // Copy constructor with vector clone
1303 SlicePattern (SlicePattern
const &other
)
1304 : locus (other
.locus
), mappings (other
.mappings
)
1306 items
.reserve (other
.items
.size ());
1307 for (const auto &e
: other
.items
)
1308 items
.push_back (e
->clone_pattern ());
1311 // Overloaded assignment operator to vector clone
1312 SlicePattern
&operator= (SlicePattern
const &other
)
1314 locus
= other
.locus
;
1315 mappings
= other
.mappings
;
1317 items
.reserve (other
.items
.size ());
1318 for (const auto &e
: other
.items
)
1319 items
.push_back (e
->clone_pattern ());
1324 // move constructors
1325 SlicePattern (SlicePattern
&&other
) = default;
1326 SlicePattern
&operator= (SlicePattern
&&other
) = default;
1328 Location
get_locus () const override
{ return locus
; }
1330 void accept_vis (HIRFullVisitor
&vis
) override
;
1331 void accept_vis (HIRPatternVisitor
&vis
) override
;
1333 Analysis::NodeMapping
get_pattern_mappings () const override final
1338 PatternType
get_pattern_type () const override final
1340 return PatternType::SLICE
;
1344 /* Use covariance to implement clone function as returning this object rather
1346 SlicePattern
*clone_pattern_impl () const override
1348 return new SlicePattern (*this);
1352 // Moved definition to rust-path.h
1355 // Forward decls for paths (defined in rust-path.h)
1356 class PathInExpression
;
1357 class QualifiedPathInExpression
;