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