Debian Codegraph @

gcc/rust/hir/tree/rust-hir-pattern.h

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2
3 // This file is part of GCC.
4
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
8 // version.
9
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
13 // for more details.
14
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/>.
18
19 #ifndef RUST_HIR_PATTERN_H
20 #define RUST_HIR_PATTERN_H
21
22 #include "rust-hir-pattern-abstract.h"
23 #include "rust-common.h"
24 #include "rust-hir-literal.h"
25 #include "rust-hir-path.h"
26
27 namespace Rust {
28 namespace HIR {
29 // Literal pattern HIR node (comparing to a literal)
30 class LiteralPattern : public Pattern
31 {
32 Literal lit;
33 location_t locus;
34 Analysis::NodeMapping mappings;
35
36 public:
37 std::string as_string () const override;
38
39 // Constructor for a literal pattern
40 LiteralPattern (Analysis::NodeMapping mappings, Literal lit, location_t locus)
41 : lit (std::move (lit)), locus (locus), mappings (mappings)
42 {}
43
44 LiteralPattern (Analysis::NodeMapping mappings, std::string val,
45 Literal::LitType type, location_t locus)
46 : lit (Literal (std::move (val), type, PrimitiveCoreType::CORETYPE_STR)),
47 locus (locus), mappings (mappings)
48 {}
49
50 location_t get_locus () const override { return locus; }
51
52 void accept_vis (HIRFullVisitor &vis) override;
53 void accept_vis (HIRPatternVisitor &vis) override;
54
55 const Analysis::NodeMapping &get_mappings () const override final
56 {
57 return mappings;
58 }
59
60 PatternType get_pattern_type () const override final
61 {
62 return PatternType::LITERAL;
63 }
64
65 Literal &get_literal () { return lit; }
66 const Literal &get_literal () const { return lit; }
67
68 protected:
69 /* Use covariance to implement clone function as returning this object rather
70 * than base */
71 virtual LiteralPattern *clone_pattern_impl () const override
72 {
73 return new LiteralPattern (*this);
74 }
75 };
76
77 // Identifier pattern HIR node (bind value matched to a variable)
78 class IdentifierPattern : public Pattern
79 {
80 Identifier variable_ident;
81 bool is_ref;
82 Mutability mut;
83 std::unique_ptr<Pattern> to_bind;
84 location_t locus;
85 Analysis::NodeMapping mappings;
86
87 public:
88 std::string as_string () const override;
89
90 // Returns whether the IdentifierPattern has a pattern to bind.
91 bool has_pattern_to_bind () const { return to_bind != nullptr; }
92
93 // Constructor
94 IdentifierPattern (Analysis::NodeMapping mappings, Identifier ident,
95 location_t locus, bool is_ref = false,
96 Mutability mut = Mutability::Imm,
97 std::unique_ptr<Pattern> to_bind = nullptr)
98 : variable_ident (std::move (ident)), is_ref (is_ref), mut (mut),
99 to_bind (std::move (to_bind)), locus (locus), mappings (mappings)
100 {}
101
102 // Copy constructor with clone
103 IdentifierPattern (IdentifierPattern const &other)
104 : variable_ident (other.variable_ident), is_ref (other.is_ref),
105 mut (other.mut), locus (other.locus), mappings (other.mappings)
106 {
107 // fix to get prevent null pointer dereference
108 if (other.to_bind != nullptr)
109 to_bind = other.to_bind->clone_pattern ();
110 }
111
112 // Overload assignment operator to use clone
113 IdentifierPattern &operator= (IdentifierPattern const &other)
114 {
115 variable_ident = other.variable_ident;
116 is_ref = other.is_ref;
117 mut = other.mut;
118 locus = other.locus;
119 mappings = other.mappings;
120
121 // fix to get prevent null pointer dereference
122 if (other.to_bind != nullptr)
123 to_bind = other.to_bind->clone_pattern ();
124
125 return *this;
126 }
127
128 // default move semantics
129 IdentifierPattern (IdentifierPattern &&other) = default;
130 IdentifierPattern &operator= (IdentifierPattern &&other) = default;
131
132 location_t get_locus () const override { return locus; }
133
134 bool is_mut () const { return mut == Mutability::Mut; }
135 bool get_is_ref () const { return is_ref; }
136 Pattern &get_to_bind () { return *to_bind; }
137
138 void accept_vis (HIRFullVisitor &vis) override;
139 void accept_vis (HIRPatternVisitor &vis) override;
140
141 const Analysis::NodeMapping &get_mappings () const override final
142 {
143 return mappings;
144 }
145
146 Identifier get_identifier () const { return variable_ident; }
147
148 PatternType get_pattern_type () const override final
149 {
150 return PatternType::IDENTIFIER;
151 }
152
153 protected:
154 /* Use covariance to implement clone function as returning this object rather
155 * than base */
156 IdentifierPattern *clone_pattern_impl () const override
157 {
158 return new IdentifierPattern (*this);
159 }
160 };
161
162 // HIR node for using the '_' wildcard "match any value" pattern
163 class WildcardPattern : public Pattern
164 {
165 location_t locus;
166 Analysis::NodeMapping mappings;
167
168 public:
169 std::string as_string () const override { return std::string (1, '_'); }
170
171 WildcardPattern (Analysis::NodeMapping mappings, location_t locus)
172 : locus (locus), mappings (mappings)
173 {}
174
175 location_t get_locus () const override { return locus; }
176
177 void accept_vis (HIRFullVisitor &vis) override;
178 void accept_vis (HIRPatternVisitor &vis) override;
179
180 const Analysis::NodeMapping &get_mappings () const override final
181 {
182 return mappings;
183 }
184
185 PatternType get_pattern_type () const override final
186 {
187 return PatternType::WILDCARD;
188 }
189
190 protected:
191 /* Use covariance to implement clone function as returning this object rather
192 * than base */
193 WildcardPattern *clone_pattern_impl () const override
194 {
195 return new WildcardPattern (*this);
196 }
197 };
198
199 // Base range pattern bound (lower or upper limit) - abstract
200 class RangePatternBound
201 {
202 public:
203 enum RangePatternBoundType
204 {
205 LITERAL,
206 PATH,
207 QUALPATH
208 };
209
210 virtual ~RangePatternBound () {}
211
212 // Unique pointer custom clone function
213 std::unique_ptr<RangePatternBound> clone_range_pattern_bound () const
214 {
215 return std::unique_ptr<RangePatternBound> (
216 clone_range_pattern_bound_impl ());
217 }
218
219 virtual std::string as_string () const = 0;
220
221 virtual void accept_vis (HIRFullVisitor &vis) = 0;
222
223 virtual RangePatternBoundType get_bound_type () const = 0;
224
225 protected:
226 // pure virtual as RangePatternBound is abstract
227 virtual RangePatternBound *clone_range_pattern_bound_impl () const = 0;
228 };
229
230 // Literal-based pattern bound
231 class RangePatternBoundLiteral : public RangePatternBound
232 {
233 Literal literal;
234 /* Can only be a char, byte, int, or float literal - same impl here as
235 * previously */
236
237 // Minus prefixed to literal (if integer or floating-point)
238 bool has_minus;
239
240 location_t locus;
241
242 public:
243 // Constructor
244 RangePatternBoundLiteral (Literal literal, location_t locus,
245 bool has_minus = false)
246 : literal (literal), has_minus (has_minus), locus (locus)
247 {}
248
249 std::string as_string () const override;
250
251 location_t get_locus () const { return locus; }
252
253 Literal get_literal () const { return literal; }
254 bool get_has_minus () const { return has_minus; }
255
256 void accept_vis (HIRFullVisitor &vis) override;
257
258 RangePatternBoundType get_bound_type () const override
259 {
260 return RangePatternBoundType::LITERAL;
261 }
262
263 protected:
264 /* Use covariance to implement clone function as returning this object rather
265 * than base */
266 RangePatternBoundLiteral *clone_range_pattern_bound_impl () const override
267 {
268 return new RangePatternBoundLiteral (*this);
269 }
270 };
271
272 // Path-based pattern bound
273 class RangePatternBoundPath : public RangePatternBound
274 {
275 PathInExpression path;
276
277 /* TODO: should this be refactored so that PathInExpression is a subclass of
278 * RangePatternBound? */
279
280 public:
281 RangePatternBoundPath (PathInExpression path) : path (std::move (path)) {}
282
283 std::string as_string () const override { return path.as_string (); }
284
285 location_t get_locus () const { return path.get_locus (); }
286
287 PathInExpression &get_path () { return path; }
288 const PathInExpression &get_path () const { return path; }
289
290 void accept_vis (HIRFullVisitor &vis) override;
291
292 RangePatternBoundType get_bound_type () const override
293 {
294 return RangePatternBoundType::PATH;
295 }
296
297 protected:
298 /* Use covariance to implement clone function as returning this object rather
299 * than base */
300 RangePatternBoundPath *clone_range_pattern_bound_impl () const override
301 {
302 return new RangePatternBoundPath (*this);
303 }
304 };
305
306 // Qualified path-based pattern bound
307 class RangePatternBoundQualPath : public RangePatternBound
308 {
309 QualifiedPathInExpression path;
310
311 /* TODO: should this be refactored so that QualifiedPathInExpression is a
312 * subclass of RangePatternBound? */
313
314 public:
315 RangePatternBoundQualPath (QualifiedPathInExpression path)
316 : path (std::move (path))
317 {}
318
319 std::string as_string () const override { return path.as_string (); }
320
321 location_t get_locus () const { return path.get_locus (); }
322
323 void accept_vis (HIRFullVisitor &vis) override;
324
325 QualifiedPathInExpression &get_qualified_path () { return path; }
326 const QualifiedPathInExpression &get_qualified_path () const { return path; }
327
328 RangePatternBoundType get_bound_type () const override
329 {
330 return RangePatternBoundType::QUALPATH;
331 }
332
333 protected:
334 /* Use covariance to implement clone function as returning this object rather
335 * than base */
336 RangePatternBoundQualPath *clone_range_pattern_bound_impl () const override
337 {
338 return new RangePatternBoundQualPath (*this);
339 }
340 };
341
342 // HIR node for matching within a certain range (range pattern)
343 class RangePattern : public Pattern
344 {
345 std::unique_ptr<RangePatternBound> lower;
346 std::unique_ptr<RangePatternBound> upper;
347
348 bool has_ellipsis_syntax;
349
350 /* location only stored to avoid a dereference - lower pattern should give
351 * correct location so maybe change in future */
352 location_t locus;
353 Analysis::NodeMapping mappings;
354
355 public:
356 std::string as_string () const override;
357
358 // Constructor
359 RangePattern (Analysis::NodeMapping mappings,
360 std::unique_ptr<RangePatternBound> lower,
361 std::unique_ptr<RangePatternBound> upper, location_t locus,
362 bool has_ellipsis_syntax = false)
363 : lower (std::move (lower)), upper (std::move (upper)),
364 has_ellipsis_syntax (has_ellipsis_syntax), locus (locus),
365 mappings (mappings)
366 {}
367
368 // Copy constructor with clone
369 RangePattern (RangePattern const &other)
370 : lower (other.lower->clone_range_pattern_bound ()),
371 upper (other.upper->clone_range_pattern_bound ()),
372 has_ellipsis_syntax (other.has_ellipsis_syntax), locus (other.locus),
373 mappings (other.mappings)
374 {}
375
376 // Overloaded assignment operator to clone
377 RangePattern &operator= (RangePattern const &other)
378 {
379 lower = other.lower->clone_range_pattern_bound ();
380 upper = other.upper->clone_range_pattern_bound ();
381 has_ellipsis_syntax = other.has_ellipsis_syntax;
382 locus = other.locus;
383 mappings = other.mappings;
384
385 return *this;
386 }
387
388 // default move semantics
389 RangePattern (RangePattern &&other) = default;
390 RangePattern &operator= (RangePattern &&other) = default;
391
392 location_t get_locus () const override { return locus; }
393
394 void accept_vis (HIRFullVisitor &vis) override;
395 void accept_vis (HIRPatternVisitor &vis) override;
396
397 bool get_has_ellipsis_syntax () { return has_ellipsis_syntax; };
398
399 const Analysis::NodeMapping &get_mappings () const override final
400 {
401 return mappings;
402 }
403
404 PatternType get_pattern_type () const override final
405 {
406 return PatternType::RANGE;
407 }
408
409 RangePatternBound &get_lower_bound () { return *lower; }
410
411 RangePatternBound &get_upper_bound () { return *upper; }
412
413 protected:
414 /* Use covariance to implement clone function as returning this object rather
415 * than base */
416 RangePattern *clone_pattern_impl () const override
417 {
418 return new RangePattern (*this);
419 }
420 };
421
422 // HIR node for pattern based on dereferencing the pointers given
423 class ReferencePattern : public Pattern
424 {
425 Mutability mut;
426 std::unique_ptr<Pattern> pattern;
427 location_t locus;
428 Analysis::NodeMapping mappings;
429
430 public:
431 std::string as_string () const override;
432
433 ReferencePattern (Analysis::NodeMapping mappings,
434 std::unique_ptr<Pattern> pattern, Mutability reference_mut,
435 location_t locus)
436 : mut (reference_mut), pattern (std::move (pattern)), locus (locus),
437 mappings (mappings)
438 {}
439
440 // Copy constructor requires clone
441 ReferencePattern (ReferencePattern const &other)
442 : mut (other.mut), pattern (other.pattern->clone_pattern ()),
443 locus (other.locus), mappings (other.mappings)
444 {}
445
446 // Overload assignment operator to clone
447 ReferencePattern &operator= (ReferencePattern const &other)
448 {
449 pattern = other.pattern->clone_pattern ();
450 mut = other.mut;
451 locus = other.locus;
452 mappings = other.mappings;
453
454 return *this;
455 }
456
457 // default move semantics
458 ReferencePattern (ReferencePattern &&other) = default;
459 ReferencePattern &operator= (ReferencePattern &&other) = default;
460
461 bool is_mut () const { return mut == Mutability::Mut; }
462
463 Mutability get_mutability () const { return mut; }
464
465 void accept_vis (HIRFullVisitor &vis) override;
466 void accept_vis (HIRPatternVisitor &vis) override;
467
468 const Analysis::NodeMapping &get_mappings () const override final
469 {
470 return mappings;
471 }
472
473 location_t get_locus () const override final { return locus; }
474
475 PatternType get_pattern_type () const override final
476 {
477 return PatternType::REFERENCE;
478 }
479
480 Pattern &get_referenced_pattern () { return *pattern; }
481
482 protected:
483 /* Use covariance to implement clone function as returning this object rather
484 * than base */
485 ReferencePattern *clone_pattern_impl () const override
486 {
487 return new ReferencePattern (*this);
488 }
489 };
490
491 // Base class for a single field in a struct pattern - abstract
492 class StructPatternField
493 {
494 AST::AttrVec outer_attrs;
495 location_t locus;
496 Analysis::NodeMapping mappings;
497
498 public:
499 enum ItemType
500 {
501 TUPLE_PAT,
502 IDENT_PAT,
503 IDENT
504 };
505
506 virtual ~StructPatternField () {}
507
508 // Unique pointer custom clone function
509 std::unique_ptr<StructPatternField> clone_struct_pattern_field () const
510 {
511 return std::unique_ptr<StructPatternField> (
512 clone_struct_pattern_field_impl ());
513 }
514
515 virtual std::string as_string () const;
516 virtual void accept_vis (HIRFullVisitor &vis) = 0;
517 virtual ItemType get_item_type () const = 0;
518
519 location_t get_locus () const { return locus; }
520 Analysis::NodeMapping get_mappings () const { return mappings; };
521 AST::AttrVec get_outer_attrs () { return outer_attrs; }
522
523 protected:
524 StructPatternField (Analysis::NodeMapping mappings,
525 AST::AttrVec outer_attribs, location_t locus)
526 : outer_attrs (std::move (outer_attribs)), locus (locus),
527 mappings (mappings)
528 {}
529
530 // Clone function implementation as pure virtual method
531 virtual StructPatternField *clone_struct_pattern_field_impl () const = 0;
532 };
533
534 // Tuple pattern single field in a struct pattern
535 class StructPatternFieldTuplePat : public StructPatternField
536 {
537 TupleIndex index;
538 std::unique_ptr<Pattern> tuple_pattern;
539
540 public:
541 StructPatternFieldTuplePat (Analysis::NodeMapping mappings, TupleIndex index,
542 std::unique_ptr<Pattern> tuple_pattern,
543 AST::AttrVec outer_attribs, location_t locus)
544 : StructPatternField (mappings, std::move (outer_attribs), locus),
545 index (index), tuple_pattern (std::move (tuple_pattern))
546 {}
547
548 // Copy constructor requires clone
549 StructPatternFieldTuplePat (StructPatternFieldTuplePat const &other)
550 : StructPatternField (other), index (other.index),
551 tuple_pattern (other.tuple_pattern->clone_pattern ())
552 {}
553
554 // Overload assignment operator to perform clone
555 StructPatternFieldTuplePat &
556 operator= (StructPatternFieldTuplePat const &other)
557 {
558 StructPatternField::operator= (other);
559 tuple_pattern = other.tuple_pattern->clone_pattern ();
560 index = other.index;
561 // outer_attrs = other.outer_attrs;
562
563 return *this;
564 }
565
566 // default move semantics
567 StructPatternFieldTuplePat (StructPatternFieldTuplePat &&other) = default;
568 StructPatternFieldTuplePat &operator= (StructPatternFieldTuplePat &&other)
569 = default;
570
571 std::string as_string () const override;
572
573 void accept_vis (HIRFullVisitor &vis) override;
574
575 TupleIndex get_index () { return index; }
576 Pattern &get_tuple_pattern () { return *tuple_pattern; }
577
578 ItemType get_item_type () const override final { return ItemType::TUPLE_PAT; }
579
580 protected:
581 /* Use covariance to implement clone function as returning this object rather
582 * than base */
583 StructPatternFieldTuplePat *clone_struct_pattern_field_impl () const override
584 {
585 return new StructPatternFieldTuplePat (*this);
586 }
587 };
588
589 // Identifier pattern single field in a struct pattern
590 class StructPatternFieldIdentPat : public StructPatternField
591 {
592 Identifier ident;
593 std::unique_ptr<Pattern> ident_pattern;
594
595 public:
596 StructPatternFieldIdentPat (Analysis::NodeMapping mappings, Identifier ident,
597 std::unique_ptr<Pattern> ident_pattern,
598 AST::AttrVec outer_attrs, location_t locus)
599 : StructPatternField (mappings, std::move (outer_attrs), locus),
600 ident (std::move (ident)), ident_pattern (std::move (ident_pattern))
601 {}
602
603 // Copy constructor requires clone
604 StructPatternFieldIdentPat (StructPatternFieldIdentPat const &other)
605 : StructPatternField (other), ident (other.ident),
606 ident_pattern (other.ident_pattern->clone_pattern ())
607 {}
608
609 // Overload assignment operator to clone
610 StructPatternFieldIdentPat &
611 operator= (StructPatternFieldIdentPat const &other)
612 {
613 StructPatternField::operator= (other);
614 ident = other.ident;
615 ident_pattern = other.ident_pattern->clone_pattern ();
616 // outer_attrs = other.outer_attrs;
617
618 return *this;
619 }
620
621 // default move semantics
622 StructPatternFieldIdentPat (StructPatternFieldIdentPat &&other) = default;
623 StructPatternFieldIdentPat &operator= (StructPatternFieldIdentPat &&other)
624 = default;
625
626 std::string as_string () const override;
627
628 void accept_vis (HIRFullVisitor &vis) override;
629
630 ItemType get_item_type () const override final { return ItemType::IDENT_PAT; }
631
632 Identifier get_identifier () const { return ident; }
633
634 Pattern &get_pattern () { return *ident_pattern; }
635
636 protected:
637 /* Use covariance to implement clone function as returning this object rather
638 * than base */
639 StructPatternFieldIdentPat *clone_struct_pattern_field_impl () const override
640 {
641 return new StructPatternFieldIdentPat (*this);
642 }
643 };
644
645 // Identifier only (with no pattern) single field in a struct pattern
646 class StructPatternFieldIdent : public StructPatternField
647 {
648 bool has_ref;
649 Mutability mut;
650 Identifier ident;
651
652 public:
653 StructPatternFieldIdent (Analysis::NodeMapping mappings, Identifier ident,
654 bool is_ref, Mutability mut,
655 AST::AttrVec outer_attrs, location_t locus)
656 : StructPatternField (mappings, std::move (outer_attrs), locus),
657 has_ref (is_ref), mut (mut), ident (std::move (ident))
658 {}
659
660 std::string as_string () const override;
661
662 bool is_mut () const { return mut == Mutability::Mut; }
663
664 void accept_vis (HIRFullVisitor &vis) override;
665
666 ItemType get_item_type () const override final { return ItemType::IDENT; }
667 bool get_has_ref () const { return has_ref; }
668 Identifier get_identifier () const { return ident; };
669
670 protected:
671 /* Use covariance to implement clone function as returning this object rather
672 * than base */
673 StructPatternFieldIdent *clone_struct_pattern_field_impl () const override
674 {
675 return new StructPatternFieldIdent (*this);
676 }
677 };
678
679 // Elements of a struct pattern
680 class StructPatternElements
681 {
682 std::vector<std::unique_ptr<StructPatternField>> fields;
683
684 public:
685 // Returns whether there are any struct pattern fields
686 bool has_struct_pattern_fields () const { return !fields.empty (); }
687
688 /* Returns whether the struct pattern elements is entirely empty (no fields,
689 * no etc). */
690 bool is_empty () const { return !has_struct_pattern_fields (); }
691
692 // Constructor for StructPatternElements with both (potentially)
693 StructPatternElements (
694 std::vector<std::unique_ptr<StructPatternField>> fields)
695 : fields (std::move (fields))
696 {}
697
698 // Copy constructor with vector clone
699 StructPatternElements (StructPatternElements const &other)
700 {
701 fields.reserve (other.fields.size ());
702 for (const auto &e : other.fields)
703 fields.push_back (e->clone_struct_pattern_field ());
704 }
705
706 // Overloaded assignment operator with vector clone
707 StructPatternElements &operator= (StructPatternElements const &other)
708 {
709 fields.clear ();
710 fields.reserve (other.fields.size ());
711 for (const auto &e : other.fields)
712 fields.push_back (e->clone_struct_pattern_field ());
713
714 return *this;
715 }
716
717 // move constructors
718 StructPatternElements (StructPatternElements &&other) = default;
719 StructPatternElements &operator= (StructPatternElements &&other) = default;
720
721 // Creates an empty StructPatternElements
722 static StructPatternElements create_empty ()
723 {
724 return StructPatternElements (
725 std::vector<std::unique_ptr<StructPatternField>> ());
726 }
727
728 std::string as_string () const;
729
730 std::vector<std::unique_ptr<StructPatternField>> &get_struct_pattern_fields ()
731 {
732 return fields;
733 }
734 };
735
736 // Struct pattern HIR node representation
737 class StructPattern : public Pattern
738 {
739 PathInExpression path;
740 StructPatternElements elems;
741 Analysis::NodeMapping mappings;
742
743 public:
744 std::string as_string () const override;
745
746 StructPattern (Analysis::NodeMapping mappings, PathInExpression struct_path,
747 StructPatternElements elems)
748 : path (std::move (struct_path)), elems (std::move (elems)),
749 mappings (mappings)
750 {}
751
752 bool has_struct_pattern_elems () const { return !elems.is_empty (); }
753
754 location_t get_locus () const override { return path.get_locus (); }
755
756 void accept_vis (HIRFullVisitor &vis) override;
757 void accept_vis (HIRPatternVisitor &vis) override;
758
759 PathInExpression &get_path () { return path; }
760 StructPatternElements &get_struct_pattern_elems () { return elems; }
761
762 const Analysis::NodeMapping &get_mappings () const override final
763 {
764 return mappings;
765 }
766
767 PatternType get_pattern_type () const override final
768 {
769 return PatternType::STRUCT;
770 }
771
772 protected:
773 /* Use covariance to implement clone function as returning this object rather
774 * than base */
775 StructPattern *clone_pattern_impl () const override
776 {
777 return new StructPattern (*this);
778 }
779 };
780
781 // Base abstract class for TupleStructItems and TuplePatternItems
782 class TupleItems : public FullVisitable
783 {
784 public:
785 enum ItemType
786 {
787 MULTIPLE,
788 RANGED,
789 };
790
791 virtual ~TupleItems () {}
792
793 // TODO: should this store location data?
794
795 // Unique pointer custom clone function
796 std::unique_ptr<TupleItems> clone_tuple_items () const
797 {
798 return std::unique_ptr<TupleItems> (clone_tuple_items_impl ());
799 }
800
801 virtual ItemType get_item_type () const = 0;
802
803 virtual std::string as_string () const = 0;
804
805 protected:
806 // pure virtual clone implementation
807 virtual TupleItems *clone_tuple_items_impl () const = 0;
808 };
809
810 // Base abstract class for patterns used in TupleStructPattern
811 class TupleStructItems : public TupleItems
812 {
813 public:
814 // Unique pointer custom clone function
815 std::unique_ptr<TupleStructItems> clone_tuple_struct_items () const
816 {
817 return std::unique_ptr<TupleStructItems> (clone_tuple_items_impl ());
818 }
819
820 protected:
821 // pure virtual clone implementation
822 virtual TupleStructItems *clone_tuple_items_impl () const override = 0;
823 };
824
825 // Class for non-ranged tuple struct pattern patterns
826 class TupleStructItemsNoRange : public TupleStructItems
827 {
828 std::vector<std::unique_ptr<Pattern>> patterns;
829
830 public:
831 TupleStructItemsNoRange (std::vector<std::unique_ptr<Pattern>> patterns)
832 : patterns (std::move (patterns))
833 {}
834
835 // Copy constructor with vector clone
836 TupleStructItemsNoRange (TupleStructItemsNoRange const &other)
837 {
838 patterns.reserve (other.patterns.size ());
839 for (const auto &e : other.patterns)
840 patterns.push_back (e->clone_pattern ());
841 }
842
843 // Overloaded assignment operator with vector clone
844 TupleStructItemsNoRange &operator= (TupleStructItemsNoRange const &other)
845 {
846 patterns.clear ();
847 patterns.reserve (other.patterns.size ());
848 for (const auto &e : other.patterns)
849 patterns.push_back (e->clone_pattern ());
850
851 return *this;
852 }
853
854 // move constructors
855 TupleStructItemsNoRange (TupleStructItemsNoRange &&other) = default;
856 TupleStructItemsNoRange &operator= (TupleStructItemsNoRange &&other)
857 = default;
858
859 std::string as_string () const override;
860
861 void accept_vis (HIRFullVisitor &vis) override;
862
863 std::vector<std::unique_ptr<Pattern>> &get_patterns () { return patterns; }
864 const std::vector<std::unique_ptr<Pattern>> &get_patterns () const
865 {
866 return patterns;
867 }
868
869 ItemType get_item_type () const override final { return ItemType::MULTIPLE; }
870
871 protected:
872 /* Use covariance to implement clone function as returning this object rather
873 * than base */
874 TupleStructItemsNoRange *clone_tuple_items_impl () const override
875 {
876 return new TupleStructItemsNoRange (*this);
877 }
878 };
879
880 // Class for ranged tuple struct pattern patterns
881 class TupleStructItemsRange : public TupleStructItems
882 {
883 std::vector<std::unique_ptr<Pattern>> lower_patterns;
884 std::vector<std::unique_ptr<Pattern>> upper_patterns;
885
886 public:
887 TupleStructItemsRange (std::vector<std::unique_ptr<Pattern>> lower_patterns,
888 std::vector<std::unique_ptr<Pattern>> upper_patterns)
889 : lower_patterns (std::move (lower_patterns)),
890 upper_patterns (std::move (upper_patterns))
891 {}
892
893 // Copy constructor with vector clone
894 TupleStructItemsRange (TupleStructItemsRange const &other)
895 {
896 lower_patterns.reserve (other.lower_patterns.size ());
897 for (const auto &e : other.lower_patterns)
898 lower_patterns.push_back (e->clone_pattern ());
899
900 upper_patterns.reserve (other.upper_patterns.size ());
901 for (const auto &e : other.upper_patterns)
902 upper_patterns.push_back (e->clone_pattern ());
903 }
904
905 // Overloaded assignment operator to clone
906 TupleStructItemsRange &operator= (TupleStructItemsRange const &other)
907 {
908 lower_patterns.clear ();
909 lower_patterns.reserve (other.lower_patterns.size ());
910 for (const auto &e : other.lower_patterns)
911 lower_patterns.push_back (e->clone_pattern ());
912
913 upper_patterns.clear ();
914 upper_patterns.reserve (other.upper_patterns.size ());
915 for (const auto &e : other.upper_patterns)
916 upper_patterns.push_back (e->clone_pattern ());
917
918 return *this;
919 }
920
921 // move constructors
922 TupleStructItemsRange (TupleStructItemsRange &&other) = default;
923 TupleStructItemsRange &operator= (TupleStructItemsRange &&other) = default;
924
925 std::string as_string () const override;
926
927 void accept_vis (HIRFullVisitor &vis) override;
928
929 std::vector<std::unique_ptr<Pattern>> &get_lower_patterns ()
930 {
931 return lower_patterns;
932 }
933 const std::vector<std::unique_ptr<Pattern>> &get_lower_patterns () const
934 {
935 return lower_patterns;
936 }
937
938 // TODO: seems kinda dodgy. Think of better way.
939 std::vector<std::unique_ptr<Pattern>> &get_upper_patterns ()
940 {
941 return upper_patterns;
942 }
943 const std::vector<std::unique_ptr<Pattern>> &get_upper_patterns () const
944 {
945 return upper_patterns;
946 }
947
948 ItemType get_item_type () const override final { return ItemType::RANGED; }
949
950 protected:
951 /* Use covariance to implement clone function as returning this object rather
952 * than base */
953 TupleStructItemsRange *clone_tuple_items_impl () const override
954 {
955 return new TupleStructItemsRange (*this);
956 }
957 };
958
959 // HIR node representing a tuple struct pattern
960 class TupleStructPattern : public Pattern
961 {
962 PathInExpression path;
963 std::unique_ptr<TupleStructItems> items;
964 Analysis::NodeMapping mappings;
965
966 /* TOOD: should this store location data? current accessor uses path location
967 * data */
968
969 public:
970 std::string as_string () const override;
971
972 TupleStructPattern (Analysis::NodeMapping mappings,
973 PathInExpression tuple_struct_path,
974 std::unique_ptr<TupleStructItems> items)
975 : path (std::move (tuple_struct_path)), items (std::move (items)),
976 mappings (mappings)
977 {}
978
979 // Copy constructor required to clone
980 TupleStructPattern (TupleStructPattern const &other)
981 : path (other.path), items (other.items->clone_tuple_struct_items ()),
982 mappings (other.mappings)
983 {}
984
985 // Operator overload assignment operator to clone
986 TupleStructPattern &operator= (TupleStructPattern const &other)
987 {
988 path = other.path;
989 items = other.items->clone_tuple_struct_items ();
990 mappings = other.mappings;
991
992 return *this;
993 }
994
995 // move constructors
996 TupleStructPattern (TupleStructPattern &&other) = default;
997 TupleStructPattern &operator= (TupleStructPattern &&other) = default;
998
999 location_t get_locus () const override { return path.get_locus (); }
1000
1001 void accept_vis (HIRFullVisitor &vis) override;
1002 void accept_vis (HIRPatternVisitor &vis) override;
1003
1004 PathInExpression &get_path () { return path; }
1005
1006 TupleStructItems &get_items () { return *items; }
1007
1008 const Analysis::NodeMapping &get_mappings () const override final
1009 {
1010 return mappings;
1011 }
1012
1013 PatternType get_pattern_type () const override final
1014 {
1015 return PatternType::TUPLE_STRUCT;
1016 }
1017
1018 protected:
1019 /* Use covariance to implement clone function as returning this object rather
1020 * than base */
1021 TupleStructPattern *clone_pattern_impl () const override
1022 {
1023 return new TupleStructPattern (*this);
1024 }
1025 };
1026
1027 // Base abstract class representing TuplePattern patterns
1028 class TuplePatternItems : public TupleItems
1029 {
1030 public:
1031 // Unique pointer custom clone function
1032 std::unique_ptr<TuplePatternItems> clone_tuple_pattern_items () const
1033 {
1034 return std::unique_ptr<TuplePatternItems> (clone_tuple_items_impl ());
1035 }
1036
1037 protected:
1038 // pure virtual clone implementation
1039 virtual TuplePatternItems *clone_tuple_items_impl () const override = 0;
1040 };
1041
1042 // Class representing TuplePattern patterns where there are multiple patterns
1043 class TuplePatternItemsMultiple : public TuplePatternItems
1044 {
1045 std::vector<std::unique_ptr<Pattern>> patterns;
1046
1047 public:
1048 TuplePatternItemsMultiple (std::vector<std::unique_ptr<Pattern>> patterns)
1049 : patterns (std::move (patterns))
1050 {}
1051
1052 // Copy constructor with vector clone
1053 TuplePatternItemsMultiple (TuplePatternItemsMultiple const &other)
1054 {
1055 patterns.reserve (other.patterns.size ());
1056 for (const auto &e : other.patterns)
1057 patterns.push_back (e->clone_pattern ());
1058 }
1059
1060 // Overloaded assignment operator to vector clone
1061 TuplePatternItemsMultiple &operator= (TuplePatternItemsMultiple const &other)
1062 {
1063 patterns.clear ();
1064 patterns.reserve (other.patterns.size ());
1065 for (const auto &e : other.patterns)
1066 patterns.push_back (e->clone_pattern ());
1067
1068 return *this;
1069 }
1070
1071 // move constructors
1072 TuplePatternItemsMultiple (TuplePatternItemsMultiple &&other) = default;
1073 TuplePatternItemsMultiple &operator= (TuplePatternItemsMultiple &&other)
1074 = default;
1075
1076 std::string as_string () const override;
1077
1078 void accept_vis (HIRFullVisitor &vis) override;
1079
1080 ItemType get_item_type () const override { return ItemType::MULTIPLE; }
1081
1082 std::vector<std::unique_ptr<Pattern>> &get_patterns () { return patterns; }
1083 const std::vector<std::unique_ptr<Pattern>> &get_patterns () const
1084 {
1085 return patterns;
1086 }
1087
1088 protected:
1089 /* Use covariance to implement clone function as returning this object rather
1090 * than base */
1091 TuplePatternItemsMultiple *clone_tuple_items_impl () const override
1092 {
1093 return new TuplePatternItemsMultiple (*this);
1094 }
1095 };
1096
1097 // Class representing TuplePattern patterns where there are a range of patterns
1098 class TuplePatternItemsRanged : public TuplePatternItems
1099 {
1100 std::vector<std::unique_ptr<Pattern>> lower_patterns;
1101 std::vector<std::unique_ptr<Pattern>> upper_patterns;
1102
1103 public:
1104 TuplePatternItemsRanged (std::vector<std::unique_ptr<Pattern>> lower_patterns,
1105 std::vector<std::unique_ptr<Pattern>> upper_patterns)
1106 : lower_patterns (std::move (lower_patterns)),
1107 upper_patterns (std::move (upper_patterns))
1108 {}
1109
1110 // Copy constructor with vector clone
1111 TuplePatternItemsRanged (TuplePatternItemsRanged const &other)
1112 {
1113 lower_patterns.reserve (other.lower_patterns.size ());
1114 for (const auto &e : other.lower_patterns)
1115 lower_patterns.push_back (e->clone_pattern ());
1116
1117 upper_patterns.reserve (other.upper_patterns.size ());
1118 for (const auto &e : other.upper_patterns)
1119 upper_patterns.push_back (e->clone_pattern ());
1120 }
1121
1122 // Overloaded assignment operator to clone
1123 TuplePatternItemsRanged &operator= (TuplePatternItemsRanged const &other)
1124 {
1125 lower_patterns.clear ();
1126 lower_patterns.reserve (other.lower_patterns.size ());
1127 for (const auto &e : other.lower_patterns)
1128 lower_patterns.push_back (e->clone_pattern ());
1129
1130 lower_patterns.clear ();
1131 upper_patterns.reserve (other.upper_patterns.size ());
1132 for (const auto &e : other.upper_patterns)
1133 upper_patterns.push_back (e->clone_pattern ());
1134
1135 return *this;
1136 }
1137
1138 // move constructors
1139 TuplePatternItemsRanged (TuplePatternItemsRanged &&other) = default;
1140 TuplePatternItemsRanged &operator= (TuplePatternItemsRanged &&other)
1141 = default;
1142
1143 std::string as_string () const override;
1144
1145 void accept_vis (HIRFullVisitor &vis) override;
1146
1147 ItemType get_item_type () const override { return ItemType::RANGED; }
1148
1149 std::vector<std::unique_ptr<Pattern>> &get_lower_patterns ()
1150 {
1151 return lower_patterns;
1152 }
1153 const std::vector<std::unique_ptr<Pattern>> &get_lower_patterns () const
1154 {
1155 return lower_patterns;
1156 }
1157
1158 std::vector<std::unique_ptr<Pattern>> &get_upper_patterns ()
1159 {
1160 return upper_patterns;
1161 }
1162 const std::vector<std::unique_ptr<Pattern>> &get_upper_patterns () const
1163 {
1164 return upper_patterns;
1165 }
1166
1167 protected:
1168 /* Use covariance to implement clone function as returning this object rather
1169 * than base */
1170 TuplePatternItemsRanged *clone_tuple_items_impl () const override
1171 {
1172 return new TuplePatternItemsRanged (*this);
1173 }
1174 };
1175
1176 // HIR node representing a tuple pattern
1177 class TuplePattern : public Pattern
1178 {
1179 std::unique_ptr<TuplePatternItems> items;
1180 location_t locus;
1181 Analysis::NodeMapping mappings;
1182
1183 public:
1184 std::string as_string () const override;
1185
1186 // Returns true if the tuple pattern has items
1187 bool has_tuple_pattern_items () const { return items != nullptr; }
1188
1189 TuplePattern (Analysis::NodeMapping mappings,
1190 std::unique_ptr<TuplePatternItems> items, location_t locus)
1191 : items (std::move (items)), locus (locus), mappings (mappings)
1192 {}
1193
1194 // Copy constructor requires clone
1195 TuplePattern (TuplePattern const &other)
1196 : items (other.items->clone_tuple_pattern_items ()), locus (other.locus),
1197 mappings (other.mappings)
1198 {}
1199
1200 // Overload assignment operator to clone
1201 TuplePattern &operator= (TuplePattern const &other)
1202 {
1203 items = other.items->clone_tuple_pattern_items ();
1204 locus = other.locus;
1205 mappings = other.mappings;
1206
1207 return *this;
1208 }
1209
1210 location_t get_locus () const override { return locus; }
1211
1212 void accept_vis (HIRFullVisitor &vis) override;
1213 void accept_vis (HIRPatternVisitor &vis) override;
1214
1215 const Analysis::NodeMapping &get_mappings () const override final
1216 {
1217 return mappings;
1218 }
1219
1220 PatternType get_pattern_type () const override final
1221 {
1222 return PatternType::TUPLE;
1223 }
1224
1225 TuplePatternItems &get_items () { return *items; }
1226 const TuplePatternItems &get_items () const { return *items; }
1227
1228 protected:
1229 /* Use covariance to implement clone function as returning this object rather
1230 * than base */
1231 TuplePattern *clone_pattern_impl () const override
1232 {
1233 return new TuplePattern (*this);
1234 }
1235 };
1236
1237 // HIR node representing patterns that can match slices and arrays
1238 class SlicePattern : public Pattern
1239 {
1240 std::vector<std::unique_ptr<Pattern>> items;
1241 location_t locus;
1242 Analysis::NodeMapping mappings;
1243
1244 public:
1245 std::string as_string () const override;
1246
1247 SlicePattern (Analysis::NodeMapping mappings,
1248 std::vector<std::unique_ptr<Pattern>> items, location_t locus)
1249 : items (std::move (items)), locus (locus), mappings (mappings)
1250 {}
1251
1252 // Copy constructor with vector clone
1253 SlicePattern (SlicePattern const &other)
1254 : locus (other.locus), mappings (other.mappings)
1255 {
1256 items.reserve (other.items.size ());
1257 for (const auto &e : other.items)
1258 items.push_back (e->clone_pattern ());
1259 }
1260
1261 // Overloaded assignment operator to vector clone
1262 SlicePattern &operator= (SlicePattern const &other)
1263 {
1264 locus = other.locus;
1265 mappings = other.mappings;
1266
1267 items.clear ();
1268 items.reserve (other.items.size ());
1269 for (const auto &e : other.items)
1270 items.push_back (e->clone_pattern ());
1271
1272 return *this;
1273 }
1274
1275 // move constructors
1276 SlicePattern (SlicePattern &&other) = default;
1277 SlicePattern &operator= (SlicePattern &&other) = default;
1278
1279 std::vector<std::unique_ptr<Pattern>> &get_items () { return items; }
1280 const std::vector<std::unique_ptr<Pattern>> &get_items () const
1281 {
1282 return items;
1283 }
1284
1285 location_t get_locus () const override { return locus; }
1286
1287 void accept_vis (HIRFullVisitor &vis) override;
1288 void accept_vis (HIRPatternVisitor &vis) override;
1289
1290 const Analysis::NodeMapping &get_mappings () const override final
1291 {
1292 return mappings;
1293 }
1294
1295 PatternType get_pattern_type () const override final
1296 {
1297 return PatternType::SLICE;
1298 }
1299
1300 protected:
1301 /* Use covariance to implement clone function as returning this object rather
1302 * than base */
1303 SlicePattern *clone_pattern_impl () const override
1304 {
1305 return new SlicePattern (*this);
1306 }
1307 };
1308
1309 // HIR node for alternative patterns
1310 class AltPattern : public Pattern
1311 {
1312 std::vector<std::unique_ptr<Pattern>> alts;
1313 location_t locus;
1314 Analysis::NodeMapping mappings;
1315
1316 public:
1317 std::string as_string () const override;
1318
1319 AltPattern (Analysis::NodeMapping mappings,
1320 std::vector<std::unique_ptr<Pattern>> alts, location_t locus)
1321 : alts (std::move (alts)), locus (locus), mappings (mappings)
1322 {}
1323
1324 // Copy constructor with vector clone
1325 AltPattern (AltPattern const &other)
1326 : locus (other.locus), mappings (other.mappings)
1327 {
1328 alts.reserve (other.alts.size ());
1329 for (const auto &e : other.alts)
1330 alts.push_back (e->clone_pattern ());
1331 }
1332
1333 // Overloaded assignment operator to vector clone
1334 AltPattern &operator= (AltPattern const &other)
1335 {
1336 locus = other.locus;
1337 mappings = other.mappings;
1338
1339 alts.clear ();
1340 alts.reserve (other.alts.size ());
1341 for (const auto &e : other.alts)
1342 alts.push_back (e->clone_pattern ());
1343
1344 return *this;
1345 }
1346
1347 // move constructors
1348 AltPattern (AltPattern &&other) = default;
1349 AltPattern &operator= (AltPattern &&other) = default;
1350
1351 std::vector<std::unique_ptr<Pattern>> &get_alts () { return alts; }
1352 const std::vector<std::unique_ptr<Pattern>> &get_alts () const
1353 {
1354 return alts;
1355 }
1356
1357 location_t get_locus () const override { return locus; }
1358
1359 void accept_vis (HIRFullVisitor &vis) override;
1360 void accept_vis (HIRPatternVisitor &vis) override;
1361
1362 const Analysis::NodeMapping &get_mappings () const override final
1363 {
1364 return mappings;
1365 }
1366
1367 PatternType get_pattern_type () const override final
1368 {
1369 return PatternType::ALT;
1370 }
1371
1372 protected:
1373 /* Use covariance to implement clone function as returning this object rather
1374 * than base */
1375 AltPattern *clone_pattern_impl () const override
1376 {
1377 return new AltPattern (*this);
1378 }
1379 };
1380
1381 // Moved definition to rust-path.h
1382 class PathPattern;
1383
1384 // Forward decls for paths (defined in rust-path.h)
1385 class PathInExpression;
1386 class QualifiedPathInExpression;
1387
1388 } // namespace HIR
1389 } // namespace Rust
1390
1391 #endif