Branch data Line data Source code
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 : 1462 : 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 : 177 : LiteralPattern (Analysis::NodeMapping mappings, Literal lit, location_t locus)
41 : 177 : : lit (std::move (lit)), locus (locus), mappings (mappings)
42 : 177 : {}
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 : 1051 : 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 : 1092 : const Analysis::NodeMapping &get_mappings () const override final
56 : : {
57 : 1092 : return mappings;
58 : : }
59 : :
60 : 134 : PatternType get_pattern_type () const override final
61 : : {
62 : 134 : return PatternType::LITERAL;
63 : : }
64 : :
65 : 348 : 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 : 731 : virtual LiteralPattern *clone_pattern_impl () const override
72 : : {
73 : 731 : 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 : 78511 : bool has_pattern_to_bind () const { return to_bind != nullptr; }
92 : :
93 : : // Constructor
94 : 35209 : 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 : 70418 : : variable_ident (std::move (ident)), is_ref (is_ref), mut (mut),
99 : 35209 : to_bind (std::move (to_bind)), locus (locus), mappings (mappings)
100 : 35209 : {}
101 : :
102 : : // Copy constructor with clone
103 : 27882 : IdentifierPattern (IdentifierPattern const &other)
104 : 55764 : : variable_ident (other.variable_ident), is_ref (other.is_ref),
105 : 27882 : mut (other.mut), locus (other.locus), mappings (other.mappings)
106 : : {
107 : : // fix to get prevent null pointer dereference
108 : 27882 : if (other.to_bind != nullptr)
109 : 0 : to_bind = other.to_bind->clone_pattern ();
110 : 27882 : }
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 : 16132 : IdentifierPattern (IdentifierPattern &&other) = default;
130 : : IdentifierPattern &operator= (IdentifierPattern &&other) = default;
131 : :
132 : 37599 : location_t get_locus () const override { return locus; }
133 : :
134 : 98693 : bool is_mut () const { return mut == Mutability::Mut; }
135 : 22825 : bool get_is_ref () const { return is_ref; }
136 : 0 : Pattern &get_to_bind () { return *to_bind; }
137 : :
138 : : void accept_vis (HIRFullVisitor &vis) override;
139 : : void accept_vis (HIRPatternVisitor &vis) override;
140 : :
141 : 132211 : const Analysis::NodeMapping &get_mappings () const override final
142 : : {
143 : 132211 : return mappings;
144 : : }
145 : :
146 : 20180 : Identifier get_identifier () const { return variable_ident; }
147 : :
148 : 360 : PatternType get_pattern_type () const override final
149 : : {
150 : 360 : return PatternType::IDENTIFIER;
151 : : }
152 : :
153 : : protected:
154 : : /* Use covariance to implement clone function as returning this object rather
155 : : * than base */
156 : 27882 : IdentifierPattern *clone_pattern_impl () const override
157 : : {
158 : 27882 : return new IdentifierPattern (*this);
159 : : }
160 : : };
161 : :
162 : : // HIR node for using the '_' wildcard "match any value" pattern
163 : 1050 : class WildcardPattern : public Pattern
164 : : {
165 : : location_t locus;
166 : : Analysis::NodeMapping mappings;
167 : :
168 : : public:
169 : 390 : std::string as_string () const override { return std::string (1, '_'); }
170 : :
171 : 628 : WildcardPattern (Analysis::NodeMapping mappings, location_t locus)
172 : 628 : : locus (locus), mappings (mappings)
173 : : {}
174 : :
175 : 1357 : 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 : 3841 : const Analysis::NodeMapping &get_mappings () const override final
181 : : {
182 : 3841 : return mappings;
183 : : }
184 : :
185 : 219 : PatternType get_pattern_type () const override final
186 : : {
187 : 219 : return PatternType::WILDCARD;
188 : : }
189 : :
190 : : protected:
191 : : /* Use covariance to implement clone function as returning this object rather
192 : : * than base */
193 : 1050 : WildcardPattern *clone_pattern_impl () const override
194 : : {
195 : 1050 : return new WildcardPattern (*this);
196 : : }
197 : : };
198 : :
199 : : // Base range pattern bound (lower or upper limit) - abstract
200 : 294 : 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 : 252 : std::unique_ptr<RangePatternBound> clone_range_pattern_bound () const
214 : : {
215 : 126 : return std::unique_ptr<RangePatternBound> (
216 : 126 : 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 : 252 : 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 : 21 : RangePatternBoundLiteral (Literal literal, location_t locus,
245 : : bool has_minus = false)
246 : 21 : : 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 : 42 : Literal get_literal () const { return literal; }
254 : 0 : bool get_has_minus () const { return has_minus; }
255 : :
256 : : void accept_vis (HIRFullVisitor &vis) override;
257 : :
258 : 42 : RangePatternBoundType get_bound_type () const override
259 : : {
260 : 42 : return RangePatternBoundType::LITERAL;
261 : : }
262 : :
263 : : protected:
264 : : /* Use covariance to implement clone function as returning this object rather
265 : : * than base */
266 : 126 : RangePatternBoundLiteral *clone_range_pattern_bound_impl () const override
267 : : {
268 : 126 : return new RangePatternBoundLiteral (*this);
269 : : }
270 : : };
271 : :
272 : : // Path-based pattern bound
273 : 126 : 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 : 21 : RangePatternBoundPath (PathInExpression path) : path (std::move (path)) {}
282 : :
283 : 0 : std::string as_string () const override { return path.as_string (); }
284 : :
285 : : location_t get_locus () const { return path.get_locus (); }
286 : :
287 : 21 : PathInExpression &get_path () { return path; }
288 : : const PathInExpression &get_path () const { return path; }
289 : :
290 : : void accept_vis (HIRFullVisitor &vis) override;
291 : :
292 : 42 : RangePatternBoundType get_bound_type () const override
293 : : {
294 : 42 : return RangePatternBoundType::PATH;
295 : : }
296 : :
297 : : protected:
298 : : /* Use covariance to implement clone function as returning this object rather
299 : : * than base */
300 : 126 : RangePatternBoundPath *clone_range_pattern_bound_impl () const override
301 : : {
302 : 126 : return new RangePatternBoundPath (*this);
303 : : }
304 : : };
305 : :
306 : : // Qualified path-based pattern bound
307 : 0 : 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 : 0 : RangePatternBoundQualPath (QualifiedPathInExpression path)
316 : 0 : : path (std::move (path))
317 : : {}
318 : :
319 : 0 : 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 : 0 : QualifiedPathInExpression &get_qualified_path () { return path; }
326 : : const QualifiedPathInExpression &get_qualified_path () const { return path; }
327 : :
328 : 0 : RangePatternBoundType get_bound_type () const override
329 : : {
330 : 0 : return RangePatternBoundType::QUALPATH;
331 : : }
332 : :
333 : : protected:
334 : : /* Use covariance to implement clone function as returning this object rather
335 : : * than base */
336 : 0 : RangePatternBoundQualPath *clone_range_pattern_bound_impl () const override
337 : : {
338 : 0 : 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 : 21 : 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 : 21 : : lower (std::move (lower)), upper (std::move (upper)),
364 : 21 : has_ellipsis_syntax (has_ellipsis_syntax), locus (locus),
365 : 21 : mappings (mappings)
366 : : {}
367 : :
368 : : // Copy constructor with clone
369 : 126 : RangePattern (RangePattern const &other)
370 : 252 : : lower (other.lower->clone_range_pattern_bound ()),
371 : 126 : upper (other.upper->clone_range_pattern_bound ()),
372 : 126 : has_ellipsis_syntax (other.has_ellipsis_syntax), locus (other.locus),
373 : 126 : mappings (other.mappings)
374 : 126 : {}
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 : 231 : 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 : 0 : bool get_has_ellipsis_syntax () { return has_ellipsis_syntax; };
398 : :
399 : 126 : const Analysis::NodeMapping &get_mappings () const override final
400 : : {
401 : 126 : return mappings;
402 : : }
403 : :
404 : 21 : PatternType get_pattern_type () const override final
405 : : {
406 : 21 : return PatternType::RANGE;
407 : : }
408 : :
409 : 42 : RangePatternBound &get_lower_bound () { return *lower; }
410 : :
411 : 42 : 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 : 126 : RangePattern *clone_pattern_impl () const override
417 : : {
418 : 126 : 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 : 81 : ReferencePattern (Analysis::NodeMapping mappings,
434 : : std::unique_ptr<Pattern> pattern, Mutability reference_mut,
435 : : location_t locus)
436 : 81 : : mut (reference_mut), pattern (std::move (pattern)), locus (locus),
437 : 81 : mappings (mappings)
438 : : {}
439 : :
440 : : // Copy constructor requires clone
441 : 78 : ReferencePattern (ReferencePattern const &other)
442 : 156 : : mut (other.mut), pattern (other.pattern->clone_pattern ()),
443 : 78 : locus (other.locus), mappings (other.mappings)
444 : 78 : {}
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 : 115 : bool is_mut () const { return mut == Mutability::Mut; }
462 : :
463 : 2 : Mutability get_mutability () const { return mut; }
464 : :
465 : : void accept_vis (HIRFullVisitor &vis) override;
466 : : void accept_vis (HIRPatternVisitor &vis) override;
467 : :
468 : 387 : const Analysis::NodeMapping &get_mappings () const override final
469 : : {
470 : 387 : return mappings;
471 : : }
472 : :
473 : 238 : location_t get_locus () const override final { return locus; }
474 : :
475 : 41 : PatternType get_pattern_type () const override final
476 : : {
477 : 41 : return PatternType::REFERENCE;
478 : : }
479 : :
480 : 187 : 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 : 78 : ReferencePattern *clone_pattern_impl () const override
486 : : {
487 : 78 : return new ReferencePattern (*this);
488 : : }
489 : : };
490 : :
491 : : // Base class for a single field in a struct pattern - abstract
492 : 68 : 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 : 477 : std::unique_ptr<StructPatternField> clone_struct_pattern_field () const
510 : : {
511 : 477 : return std::unique_ptr<StructPatternField> (
512 : 477 : 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 : 256 : location_t get_locus () const { return locus; }
520 : 362 : Analysis::NodeMapping get_mappings () const { return mappings; };
521 : 0 : AST::AttrVec get_outer_attrs () { return outer_attrs; }
522 : :
523 : : protected:
524 : 160 : StructPatternField (Analysis::NodeMapping mappings,
525 : : AST::AttrVec outer_attribs, location_t locus)
526 : 160 : : outer_attrs (std::move (outer_attribs)), locus (locus),
527 : 160 : 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 : 2 : StructPatternFieldTuplePat (Analysis::NodeMapping mappings, TupleIndex index,
542 : : std::unique_ptr<Pattern> tuple_pattern,
543 : : AST::AttrVec outer_attribs, location_t locus)
544 : 2 : : StructPatternField (mappings, std::move (outer_attribs), locus),
545 : 2 : index (index), tuple_pattern (std::move (tuple_pattern))
546 : 2 : {}
547 : :
548 : : // Copy constructor requires clone
549 : 0 : StructPatternFieldTuplePat (StructPatternFieldTuplePat const &other)
550 : 0 : : StructPatternField (other), index (other.index),
551 : 0 : tuple_pattern (other.tuple_pattern->clone_pattern ())
552 : 0 : {}
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 : 0 : TupleIndex get_index () { return index; }
576 : 0 : Pattern &get_tuple_pattern () { return *tuple_pattern; }
577 : :
578 : 0 : 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 : 0 : StructPatternFieldTuplePat *clone_struct_pattern_field_impl () const override
584 : : {
585 : 0 : 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 : 44 : StructPatternFieldIdentPat (Analysis::NodeMapping mappings, Identifier ident,
597 : : std::unique_ptr<Pattern> ident_pattern,
598 : : AST::AttrVec outer_attrs, location_t locus)
599 : 44 : : StructPatternField (mappings, std::move (outer_attrs), locus),
600 : 44 : ident (std::move (ident)), ident_pattern (std::move (ident_pattern))
601 : 44 : {}
602 : :
603 : : // Copy constructor requires clone
604 : 34 : StructPatternFieldIdentPat (StructPatternFieldIdentPat const &other)
605 : 68 : : StructPatternField (other), ident (other.ident),
606 : 34 : ident_pattern (other.ident_pattern->clone_pattern ())
607 : 34 : {}
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 : 82 : ItemType get_item_type () const override final { return ItemType::IDENT_PAT; }
631 : :
632 : 122 : Identifier get_identifier () const { return ident; }
633 : :
634 : 78 : 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 : 34 : StructPatternFieldIdentPat *clone_struct_pattern_field_impl () const override
640 : : {
641 : 34 : 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 : 114 : StructPatternFieldIdent (Analysis::NodeMapping mappings, Identifier ident,
654 : : bool is_ref, Mutability mut,
655 : : AST::AttrVec outer_attrs, location_t locus)
656 : 114 : : StructPatternField (mappings, std::move (outer_attrs), locus),
657 : 114 : has_ref (is_ref), mut (mut), ident (std::move (ident))
658 : 114 : {}
659 : :
660 : : std::string as_string () const override;
661 : :
662 : 0 : bool is_mut () const { return mut == Mutability::Mut; }
663 : :
664 : : void accept_vis (HIRFullVisitor &vis) override;
665 : :
666 : 379 : ItemType get_item_type () const override final { return ItemType::IDENT; }
667 : 0 : bool get_has_ref () const { return has_ref; }
668 : 393 : 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 : 443 : StructPatternFieldIdent *clone_struct_pattern_field_impl () const override
674 : : {
675 : 443 : return new StructPatternFieldIdent (*this);
676 : : }
677 : : };
678 : :
679 : : // Elements of a struct pattern
680 : 161 : class StructPatternElements
681 : : {
682 : : std::vector<std::unique_ptr<StructPatternField>> fields;
683 : :
684 : : public:
685 : : // Returns whether there are any struct pattern fields
686 : 0 : 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 : 0 : bool is_empty () const { return !has_struct_pattern_fields (); }
691 : :
692 : : // Constructor for StructPatternElements with both (potentially)
693 : 100 : StructPatternElements (
694 : : std::vector<std::unique_ptr<StructPatternField>> fields)
695 : 100 : : fields (std::move (fields))
696 : : {}
697 : :
698 : : // Copy constructor with vector clone
699 : 257 : StructPatternElements (StructPatternElements const &other)
700 : 257 : {
701 : 257 : fields.reserve (other.fields.size ());
702 : 734 : for (const auto &e : other.fields)
703 : 477 : fields.push_back (e->clone_struct_pattern_field ());
704 : 257 : }
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 : 100 : 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 : 6 : std::vector<std::unique_ptr<StructPatternField>> &get_struct_pattern_fields ()
731 : : {
732 : 208 : 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 : 100 : StructPattern (Analysis::NodeMapping mappings, PathInExpression struct_path,
747 : : StructPatternElements elems)
748 : 200 : : path (std::move (struct_path)), elems (std::move (elems)),
749 : 100 : mappings (mappings)
750 : 100 : {}
751 : :
752 : 0 : bool has_struct_pattern_elems () const { return !elems.is_empty (); }
753 : :
754 : 339 : 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 : 2 : PathInExpression &get_path () { return path; }
760 : 273 : StructPatternElements &get_struct_pattern_elems () { return elems; }
761 : :
762 : 398 : const Analysis::NodeMapping &get_mappings () const override final
763 : : {
764 : 398 : return mappings;
765 : : }
766 : :
767 : 61 : PatternType get_pattern_type () const override final
768 : : {
769 : 61 : return PatternType::STRUCT;
770 : : }
771 : :
772 : : protected:
773 : : /* Use covariance to implement clone function as returning this object rather
774 : : * than base */
775 : 196 : StructPattern *clone_pattern_impl () const override
776 : : {
777 : 196 : return new StructPattern (*this);
778 : : }
779 : : };
780 : :
781 : : // Base abstract class for TupleStructItems and TuplePatternItems
782 : 1198 : 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 : 742 : class TupleStructItems : public TupleItems
812 : : {
813 : : public:
814 : : // Unique pointer custom clone function
815 : 392 : std::unique_ptr<TupleStructItems> clone_tuple_struct_items () const
816 : : {
817 : 392 : 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 : 350 : TupleStructItemsNoRange (std::vector<std::unique_ptr<Pattern>> patterns)
832 : 350 : : patterns (std::move (patterns))
833 : : {}
834 : :
835 : : // Copy constructor with vector clone
836 : 392 : TupleStructItemsNoRange (TupleStructItemsNoRange const &other)
837 : 392 : {
838 : 392 : patterns.reserve (other.patterns.size ());
839 : 762 : for (const auto &e : other.patterns)
840 : 370 : patterns.push_back (e->clone_pattern ());
841 : 392 : }
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 : 1113 : 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 : 1155 : 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 : 392 : TupleStructItemsNoRange *clone_tuple_items_impl () const override
875 : : {
876 : 392 : 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 : 0 : TupleStructItemsRange (TupleStructItemsRange const &other)
895 : 0 : {
896 : 0 : lower_patterns.reserve (other.lower_patterns.size ());
897 : 0 : for (const auto &e : other.lower_patterns)
898 : 0 : lower_patterns.push_back (e->clone_pattern ());
899 : :
900 : 0 : upper_patterns.reserve (other.upper_patterns.size ());
901 : 0 : for (const auto &e : other.upper_patterns)
902 : 0 : upper_patterns.push_back (e->clone_pattern ());
903 : 0 : }
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 : 0 : std::vector<std::unique_ptr<Pattern>> &get_lower_patterns ()
930 : : {
931 : 0 : 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 : 0 : std::vector<std::unique_ptr<Pattern>> &get_upper_patterns ()
940 : : {
941 : 0 : return upper_patterns;
942 : : }
943 : : const std::vector<std::unique_ptr<Pattern>> &get_upper_patterns () const
944 : : {
945 : : return upper_patterns;
946 : : }
947 : :
948 : 0 : 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 : 0 : TupleStructItemsRange *clone_tuple_items_impl () const override
954 : : {
955 : 0 : 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 : 350 : TupleStructPattern (Analysis::NodeMapping mappings,
973 : : PathInExpression tuple_struct_path,
974 : : std::unique_ptr<TupleStructItems> items)
975 : 700 : : path (std::move (tuple_struct_path)), items (std::move (items)),
976 : 350 : mappings (mappings)
977 : 350 : {}
978 : :
979 : : // Copy constructor required to clone
980 : 392 : TupleStructPattern (TupleStructPattern const &other)
981 : 392 : : path (other.path), items (other.items->clone_tuple_struct_items ()),
982 : 392 : mappings (other.mappings)
983 : 392 : {}
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 : 824 : 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 : 0 : PathInExpression &get_path () { return path; }
1005 : :
1006 : 1155 : TupleStructItems &get_items () { return *items; }
1007 : :
1008 : 1405 : const Analysis::NodeMapping &get_mappings () const override final
1009 : : {
1010 : 1405 : return mappings;
1011 : : }
1012 : :
1013 : 293 : PatternType get_pattern_type () const override final
1014 : : {
1015 : 293 : return PatternType::TUPLE_STRUCT;
1016 : : }
1017 : :
1018 : : protected:
1019 : : /* Use covariance to implement clone function as returning this object rather
1020 : : * than base */
1021 : 392 : TupleStructPattern *clone_pattern_impl () const override
1022 : : {
1023 : 392 : return new TupleStructPattern (*this);
1024 : : }
1025 : : };
1026 : :
1027 : : // Base abstract class representing TuplePattern patterns
1028 : 456 : class TuplePatternItems : public TupleItems
1029 : : {
1030 : : public:
1031 : : // Unique pointer custom clone function
1032 : 96 : std::unique_ptr<TuplePatternItems> clone_tuple_pattern_items () const
1033 : : {
1034 : 96 : 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 : 360 : TuplePatternItemsMultiple (std::vector<std::unique_ptr<Pattern>> patterns)
1049 : 360 : : patterns (std::move (patterns))
1050 : : {}
1051 : :
1052 : : // Copy constructor with vector clone
1053 : 96 : TuplePatternItemsMultiple (TuplePatternItemsMultiple const &other)
1054 : 96 : {
1055 : 96 : patterns.reserve (other.patterns.size ());
1056 : 290 : for (const auto &e : other.patterns)
1057 : 194 : patterns.push_back (e->clone_pattern ());
1058 : 96 : }
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 : 1044 : ItemType get_item_type () const override { return ItemType::MULTIPLE; }
1081 : :
1082 : 1040 : 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 : 96 : TuplePatternItemsMultiple *clone_tuple_items_impl () const override
1092 : : {
1093 : 96 : 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 : 0 : TuplePatternItemsRanged (std::vector<std::unique_ptr<Pattern>> lower_patterns,
1105 : : std::vector<std::unique_ptr<Pattern>> upper_patterns)
1106 : 0 : : lower_patterns (std::move (lower_patterns)),
1107 : 0 : upper_patterns (std::move (upper_patterns))
1108 : : {}
1109 : :
1110 : : // Copy constructor with vector clone
1111 : 0 : TuplePatternItemsRanged (TuplePatternItemsRanged const &other)
1112 : 0 : {
1113 : 0 : lower_patterns.reserve (other.lower_patterns.size ());
1114 : 0 : for (const auto &e : other.lower_patterns)
1115 : 0 : lower_patterns.push_back (e->clone_pattern ());
1116 : :
1117 : 0 : upper_patterns.reserve (other.upper_patterns.size ());
1118 : 0 : for (const auto &e : other.upper_patterns)
1119 : 0 : upper_patterns.push_back (e->clone_pattern ());
1120 : 0 : }
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 : 0 : ItemType get_item_type () const override { return ItemType::RANGED; }
1148 : :
1149 : 0 : std::vector<std::unique_ptr<Pattern>> &get_lower_patterns ()
1150 : : {
1151 : 0 : return lower_patterns;
1152 : : }
1153 : : const std::vector<std::unique_ptr<Pattern>> &get_lower_patterns () const
1154 : : {
1155 : : return lower_patterns;
1156 : : }
1157 : :
1158 : 0 : std::vector<std::unique_ptr<Pattern>> &get_upper_patterns ()
1159 : : {
1160 : 0 : 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 : 0 : TuplePatternItemsRanged *clone_tuple_items_impl () const override
1171 : : {
1172 : 0 : 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 : 342 : bool has_tuple_pattern_items () const { return items != nullptr; }
1188 : :
1189 : 360 : TuplePattern (Analysis::NodeMapping mappings,
1190 : : std::unique_ptr<TuplePatternItems> items, location_t locus)
1191 : 360 : : items (std::move (items)), locus (locus), mappings (mappings)
1192 : : {}
1193 : :
1194 : : // Copy constructor requires clone
1195 : 96 : TuplePattern (TuplePattern const &other)
1196 : 192 : : items (other.items->clone_tuple_pattern_items ()), locus (other.locus),
1197 : 96 : mappings (other.mappings)
1198 : 96 : {}
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 : 1388 : 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 : 2467 : const Analysis::NodeMapping &get_mappings () const override final
1216 : : {
1217 : 2467 : return mappings;
1218 : : }
1219 : :
1220 : 25 : PatternType get_pattern_type () const override final
1221 : : {
1222 : 25 : return PatternType::TUPLE;
1223 : : }
1224 : :
1225 : 2088 : 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 : 96 : TuplePattern *clone_pattern_impl () const override
1232 : : {
1233 : 96 : 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 : 0 : SlicePattern (Analysis::NodeMapping mappings,
1248 : : std::vector<std::unique_ptr<Pattern>> items, location_t locus)
1249 : 0 : : items (std::move (items)), locus (locus), mappings (mappings)
1250 : 0 : {}
1251 : :
1252 : : // Copy constructor with vector clone
1253 : 0 : SlicePattern (SlicePattern const &other)
1254 : 0 : : locus (other.locus), mappings (other.mappings)
1255 : : {
1256 : 0 : items.reserve (other.items.size ());
1257 : 0 : for (const auto &e : other.items)
1258 : 0 : items.push_back (e->clone_pattern ());
1259 : 0 : }
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 : 0 : 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 : 0 : 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 : 0 : const Analysis::NodeMapping &get_mappings () const override final
1291 : : {
1292 : 0 : return mappings;
1293 : : }
1294 : :
1295 : 0 : PatternType get_pattern_type () const override final
1296 : : {
1297 : 0 : return PatternType::SLICE;
1298 : : }
1299 : :
1300 : : protected:
1301 : : /* Use covariance to implement clone function as returning this object rather
1302 : : * than base */
1303 : 0 : SlicePattern *clone_pattern_impl () const override
1304 : : {
1305 : 0 : 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 : 16 : AltPattern (Analysis::NodeMapping mappings,
1320 : : std::vector<std::unique_ptr<Pattern>> alts, location_t locus)
1321 : 16 : : alts (std::move (alts)), locus (locus), mappings (mappings)
1322 : 16 : {}
1323 : :
1324 : : // Copy constructor with vector clone
1325 : 8 : AltPattern (AltPattern const &other)
1326 : 8 : : locus (other.locus), mappings (other.mappings)
1327 : : {
1328 : 8 : alts.reserve (other.alts.size ());
1329 : 28 : for (const auto &e : other.alts)
1330 : 20 : alts.push_back (e->clone_pattern ());
1331 : 8 : }
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 : 14 : 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 : 54 : 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 : 89 : const Analysis::NodeMapping &get_mappings () const override final
1363 : : {
1364 : 89 : return mappings;
1365 : : }
1366 : :
1367 : 6 : PatternType get_pattern_type () const override final
1368 : : {
1369 : 6 : return PatternType::ALT;
1370 : : }
1371 : :
1372 : : protected:
1373 : : /* Use covariance to implement clone function as returning this object rather
1374 : : * than base */
1375 : 8 : AltPattern *clone_pattern_impl () const override
1376 : : {
1377 : 8 : 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
|