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_AST_PATTERN_H
20 : : #define RUST_AST_PATTERN_H
21 : :
22 : : #include "rust-ast.h"
23 : :
24 : : namespace Rust {
25 : : namespace AST {
26 : : // Literal pattern AST node (comparing to a literal)
27 : 144 : class LiteralPattern : public Pattern
28 : : {
29 : : Literal lit;
30 : : location_t locus;
31 : : NodeId node_id;
32 : :
33 : : public:
34 : : std::string as_string () const override;
35 : :
36 : : // Constructor for a literal pattern
37 : : LiteralPattern (Literal lit, location_t locus)
38 : : : lit (std::move (lit)), locus (locus),
39 : : node_id (Analysis::Mappings::get ().get_next_node_id ())
40 : : {}
41 : :
42 : 301 : LiteralPattern (std::string val, Literal::LitType type, location_t locus,
43 : : PrimitiveCoreType type_hint)
44 : 301 : : lit (Literal (std::move (val), type, type_hint)), locus (locus),
45 : 602 : node_id (Analysis::Mappings::get ().get_next_node_id ())
46 : 301 : {}
47 : :
48 : 586 : location_t get_locus () const override final { return locus; }
49 : :
50 : : void accept_vis (ASTVisitor &vis) override;
51 : :
52 : 283 : NodeId get_node_id () const override { return node_id; }
53 : :
54 : 303 : Literal &get_literal () { return lit; }
55 : :
56 : : const Literal &get_literal () const { return lit; }
57 : :
58 : 0 : Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Literal; }
59 : :
60 : : protected:
61 : : /* Use covariance to implement clone function as returning this object rather
62 : : * than base */
63 : 72 : virtual LiteralPattern *clone_pattern_impl () const override
64 : : {
65 : 72 : return new LiteralPattern (*this);
66 : : }
67 : : };
68 : :
69 : : // Identifier pattern AST node (bind value matched to a variable)
70 : : class IdentifierPattern : public Pattern
71 : : {
72 : : Identifier variable_ident;
73 : : bool is_ref;
74 : : bool is_mut;
75 : :
76 : : // bool has_pattern;
77 : : std::unique_ptr<Pattern> subpattern;
78 : : location_t locus;
79 : : NodeId node_id;
80 : :
81 : : public:
82 : : std::string as_string () const override;
83 : :
84 : : // Returns whether the IdentifierPattern has a pattern to bind.
85 : 464154 : bool has_subpattern () const { return subpattern != nullptr; }
86 : :
87 : : // Constructor
88 : 24257 : IdentifierPattern (Identifier ident, location_t locus, bool is_ref = false,
89 : : bool is_mut = false,
90 : : std::unique_ptr<Pattern> subpattern = nullptr)
91 : 48514 : : Pattern (), variable_ident (std::move (ident)), is_ref (is_ref),
92 : 24257 : is_mut (is_mut), subpattern (std::move (subpattern)), locus (locus),
93 : 24257 : node_id (Analysis::Mappings::get ().get_next_node_id ())
94 : 24257 : {}
95 : :
96 : 0 : IdentifierPattern (NodeId node_id, Identifier ident, location_t locus,
97 : : bool is_ref = false, bool is_mut = false,
98 : : std::unique_ptr<Pattern> subpattern = nullptr)
99 : 0 : : Pattern (), variable_ident (std::move (ident)), is_ref (is_ref),
100 : 0 : is_mut (is_mut), subpattern (std::move (subpattern)), locus (locus),
101 : 0 : node_id (node_id)
102 : : {}
103 : :
104 : : // Copy constructor with clone
105 : 19159 : IdentifierPattern (IdentifierPattern const &other)
106 : 38318 : : variable_ident (other.variable_ident), is_ref (other.is_ref),
107 : 19159 : is_mut (other.is_mut), locus (other.locus), node_id (other.node_id)
108 : : {
109 : : // fix to get prevent null pointer dereference
110 : 19159 : if (other.subpattern != nullptr)
111 : 0 : subpattern = other.subpattern->clone_pattern ();
112 : 19159 : }
113 : :
114 : : // Overload assignment operator to use clone
115 : : IdentifierPattern &operator= (IdentifierPattern const &other)
116 : : {
117 : : variable_ident = other.variable_ident;
118 : : is_ref = other.is_ref;
119 : : is_mut = other.is_mut;
120 : : locus = other.locus;
121 : : node_id = other.node_id;
122 : :
123 : : // fix to prevent null pointer dereference
124 : : if (other.subpattern != nullptr)
125 : : subpattern = other.subpattern->clone_pattern ();
126 : : else
127 : : subpattern = nullptr;
128 : :
129 : : return *this;
130 : : }
131 : :
132 : : // default move semantics
133 : : IdentifierPattern (IdentifierPattern &&other) = default;
134 : : IdentifierPattern &operator= (IdentifierPattern &&other) = default;
135 : :
136 : 65661 : location_t get_locus () const override final { return locus; }
137 : :
138 : : void accept_vis (ASTVisitor &vis) override;
139 : :
140 : 185 : Pattern &get_subpattern ()
141 : : {
142 : 185 : rust_assert (has_subpattern ());
143 : 185 : return *subpattern;
144 : : }
145 : :
146 : 49309 : Identifier get_ident () const { return variable_ident; }
147 : :
148 : 46724 : bool get_is_mut () const { return is_mut; }
149 : 46724 : bool get_is_ref () const { return is_ref; }
150 : :
151 : 44685 : NodeId get_node_id () const override { return node_id; }
152 : :
153 : 5168 : Pattern::Kind get_pattern_kind () override
154 : : {
155 : 5168 : return Pattern::Kind::Identifier;
156 : : }
157 : :
158 : : protected:
159 : : /* Use covariance to implement clone function as returning this object rather
160 : : * than base */
161 : 19159 : IdentifierPattern *clone_pattern_impl () const override
162 : : {
163 : 19159 : return new IdentifierPattern (*this);
164 : : }
165 : : };
166 : :
167 : : // AST node for using the '_' wildcard "match any value" pattern
168 : 272 : class WildcardPattern : public Pattern
169 : : {
170 : : location_t locus;
171 : : NodeId node_id;
172 : :
173 : : public:
174 : 0 : std::string as_string () const override { return std::string (1, '_'); }
175 : :
176 : 1087 : WildcardPattern (location_t locus)
177 : 1087 : : locus (locus), node_id (Analysis::Mappings::get ().get_next_node_id ())
178 : 1087 : {}
179 : :
180 : 2044 : location_t get_locus () const override final { return locus; }
181 : :
182 : : void accept_vis (ASTVisitor &vis) override;
183 : :
184 : 965 : NodeId get_node_id () const override { return node_id; }
185 : :
186 : 132 : Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Wildcard; }
187 : :
188 : : protected:
189 : : /* Use covariance to implement clone function as returning this object rather
190 : : * than base */
191 : 272 : WildcardPattern *clone_pattern_impl () const override
192 : : {
193 : 272 : return new WildcardPattern (*this);
194 : : }
195 : : };
196 : :
197 : 0 : class RestPattern : public Pattern
198 : : {
199 : : location_t locus;
200 : : NodeId node_id;
201 : :
202 : : public:
203 : 0 : std::string as_string () const override { return ".."; }
204 : :
205 : 0 : RestPattern (location_t locus)
206 : 0 : : locus (locus), node_id (Analysis::Mappings::get ().get_next_node_id ())
207 : 0 : {}
208 : :
209 : 0 : location_t get_locus () const override final { return locus; }
210 : :
211 : : void accept_vis (ASTVisitor &vis) override;
212 : :
213 : 0 : NodeId get_node_id () const override final { return node_id; }
214 : :
215 : 0 : Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Rest; }
216 : :
217 : : protected:
218 : 0 : RestPattern *clone_pattern_impl () const override
219 : : {
220 : 0 : return new RestPattern (*this);
221 : : }
222 : : };
223 : :
224 : : // Base range pattern bound (lower or upper limit) - abstract
225 : 52 : class RangePatternBound
226 : : {
227 : : public:
228 : : enum RangePatternBoundType
229 : : {
230 : : LITERAL,
231 : : PATH,
232 : : QUALPATH
233 : : };
234 : :
235 : : virtual ~RangePatternBound () {}
236 : :
237 : : // Unique pointer custom clone function
238 : 0 : std::unique_ptr<RangePatternBound> clone_range_pattern_bound () const
239 : : {
240 : 0 : return std::unique_ptr<RangePatternBound> (
241 : 0 : clone_range_pattern_bound_impl ());
242 : : }
243 : :
244 : : virtual std::string as_string () const = 0;
245 : :
246 : : virtual void accept_vis (ASTVisitor &vis) = 0;
247 : :
248 : : virtual RangePatternBoundType get_bound_type () const = 0;
249 : :
250 : : protected:
251 : : // pure virtual as RangePatternBound is abstract
252 : : virtual RangePatternBound *clone_range_pattern_bound_impl () const = 0;
253 : : };
254 : :
255 : : // Literal-based pattern bound
256 : 0 : class RangePatternBoundLiteral : public RangePatternBound
257 : : {
258 : : Literal literal;
259 : : /* Can only be a char, byte, int, or float literal - same impl here as
260 : : * previously */
261 : :
262 : : // Minus prefixed to literal (if integer or floating-point)
263 : : bool has_minus;
264 : :
265 : : location_t locus;
266 : :
267 : : public:
268 : : // Constructor
269 : 29 : RangePatternBoundLiteral (Literal literal, location_t locus,
270 : : bool has_minus = false)
271 : 29 : : literal (literal), has_minus (has_minus), locus (locus)
272 : : {}
273 : :
274 : : std::string as_string () const override;
275 : :
276 : 21 : Literal get_literal () const { return literal; }
277 : :
278 : 21 : bool get_has_minus () const { return has_minus; }
279 : :
280 : 21 : location_t get_locus () const { return locus; }
281 : :
282 : : void accept_vis (ASTVisitor &vis) override;
283 : :
284 : 21 : RangePatternBoundType get_bound_type () const override
285 : : {
286 : 21 : return RangePatternBoundType::LITERAL;
287 : : }
288 : :
289 : : protected:
290 : : /* Use covariance to implement clone function as returning this object rather
291 : : * than base */
292 : 0 : RangePatternBoundLiteral *clone_range_pattern_bound_impl () const override
293 : : {
294 : 0 : return new RangePatternBoundLiteral (*this);
295 : : }
296 : : };
297 : :
298 : : // Path-based pattern bound
299 : 0 : class RangePatternBoundPath : public RangePatternBound
300 : : {
301 : : PathInExpression path;
302 : :
303 : : /* TODO: should this be refactored so that PathInExpression is a subclass of
304 : : * RangePatternBound? */
305 : :
306 : : public:
307 : 23 : RangePatternBoundPath (PathInExpression path) : path (std::move (path)) {}
308 : :
309 : 0 : std::string as_string () const override { return path.as_string (); }
310 : :
311 : : location_t get_locus () const { return path.get_locus (); }
312 : :
313 : : void accept_vis (ASTVisitor &vis) override;
314 : :
315 : : // TODO: this mutable getter seems kinda dodgy
316 : 294 : PathInExpression &get_path () { return path; }
317 : : const PathInExpression &get_path () const { return path; }
318 : :
319 : 21 : RangePatternBoundType get_bound_type () const override
320 : : {
321 : 21 : return RangePatternBoundType::PATH;
322 : : }
323 : :
324 : : protected:
325 : : /* Use covariance to implement clone function as returning this object rather
326 : : * than base */
327 : 0 : RangePatternBoundPath *clone_range_pattern_bound_impl () const override
328 : : {
329 : 0 : return new RangePatternBoundPath (*this);
330 : : }
331 : : };
332 : :
333 : : // Qualified path-based pattern bound
334 : 0 : class RangePatternBoundQualPath : public RangePatternBound
335 : : {
336 : : QualifiedPathInExpression path;
337 : :
338 : : /* TODO: should this be refactored so that QualifiedPathInExpression is a
339 : : * subclass of RangePatternBound? */
340 : :
341 : : public:
342 : 0 : RangePatternBoundQualPath (QualifiedPathInExpression path)
343 : 0 : : path (std::move (path))
344 : : {}
345 : :
346 : 0 : std::string as_string () const override { return path.as_string (); }
347 : :
348 : : location_t get_locus () const { return path.get_locus (); }
349 : :
350 : : void accept_vis (ASTVisitor &vis) override;
351 : :
352 : : // TODO: this mutable getter seems kinda dodgy
353 : 0 : QualifiedPathInExpression &get_qualified_path () { return path; }
354 : : const QualifiedPathInExpression &get_qualified_path () const { return path; }
355 : :
356 : 0 : RangePatternBoundType get_bound_type () const override
357 : : {
358 : 0 : return RangePatternBoundType::QUALPATH;
359 : : }
360 : :
361 : : protected:
362 : : /* Use covariance to implement clone function as returning this object rather
363 : : * than base */
364 : 0 : RangePatternBoundQualPath *clone_range_pattern_bound_impl () const override
365 : : {
366 : 0 : return new RangePatternBoundQualPath (*this);
367 : : }
368 : : };
369 : :
370 : : enum class RangeKind
371 : : {
372 : : INCLUDED,
373 : : ELLIPSIS,
374 : : EXCLUDED,
375 : : };
376 : :
377 : : RangeKind tokenid_to_rangekind (TokenId id);
378 : : // AST node for matching within a certain range (range pattern)
379 : : class RangePattern : public Pattern
380 : : {
381 : : std::unique_ptr<RangePatternBound> lower;
382 : : std::unique_ptr<RangePatternBound> upper;
383 : :
384 : : RangeKind range_kind;
385 : :
386 : : /* location only stored to avoid a dereference - lower pattern should give
387 : : * correct location so maybe change in future */
388 : : location_t locus;
389 : : NodeId node_id;
390 : :
391 : : public:
392 : : std::string as_string () const override;
393 : :
394 : : // Constructor
395 : 26 : RangePattern (std::unique_ptr<RangePatternBound> lower,
396 : : std::unique_ptr<RangePatternBound> upper, RangeKind range_kind,
397 : : location_t locus)
398 : 26 : : lower (std::move (lower)), upper (std::move (upper)),
399 : 26 : range_kind (range_kind), locus (locus),
400 : 26 : node_id (Analysis::Mappings::get ().get_next_node_id ())
401 : 26 : {}
402 : :
403 : : // Copy constructor with clone
404 : 0 : RangePattern (RangePattern const &other)
405 : 0 : : lower (other.lower->clone_range_pattern_bound ()),
406 : 0 : upper (other.upper->clone_range_pattern_bound ()),
407 : 0 : range_kind (other.range_kind), locus (other.locus),
408 : 0 : node_id (other.node_id)
409 : 0 : {}
410 : :
411 : : // Overloaded assignment operator to clone
412 : : RangePattern &operator= (RangePattern const &other)
413 : : {
414 : : lower = other.lower->clone_range_pattern_bound ();
415 : : upper = other.upper->clone_range_pattern_bound ();
416 : : range_kind = other.range_kind;
417 : : locus = other.locus;
418 : : node_id = other.node_id;
419 : :
420 : : return *this;
421 : : }
422 : :
423 : : // default move semantics
424 : : RangePattern (RangePattern &&other) = default;
425 : : RangePattern &operator= (RangePattern &&other) = default;
426 : :
427 : 43 : location_t get_locus () const override final { return locus; }
428 : :
429 : 0 : bool get_has_ellipsis_syntax () const
430 : : {
431 : 0 : return range_kind == RangeKind::ELLIPSIS;
432 : : }
433 : :
434 : 43 : RangeKind get_range_kind () const { return range_kind; }
435 : :
436 : 286 : bool get_has_lower_bound () const { return lower != nullptr; }
437 : :
438 : 286 : bool get_has_upper_bound () const { return upper != nullptr; }
439 : :
440 : : void accept_vis (ASTVisitor &vis) override;
441 : :
442 : : // TODO: is this better? or is a "vis_bound" better?
443 : 307 : RangePatternBound &get_lower_bound ()
444 : : {
445 : 307 : rust_assert (lower != nullptr);
446 : 307 : return *lower;
447 : : }
448 : :
449 : 307 : RangePatternBound &get_upper_bound ()
450 : : {
451 : 307 : rust_assert (upper != nullptr);
452 : 307 : return *upper;
453 : : }
454 : :
455 : 21 : NodeId get_node_id () const override { return node_id; }
456 : :
457 : 0 : Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Range; }
458 : :
459 : : protected:
460 : : /* Use covariance to implement clone function as returning this object rather
461 : : * than base */
462 : 0 : RangePattern *clone_pattern_impl () const override
463 : : {
464 : 0 : return new RangePattern (*this);
465 : : }
466 : : };
467 : :
468 : : // AST node for pattern based on dereferencing the pointers given
469 : : class ReferencePattern : public Pattern
470 : : {
471 : : bool has_two_amps;
472 : : bool is_mut;
473 : : std::unique_ptr<Pattern> pattern;
474 : : location_t locus;
475 : : NodeId node_id;
476 : :
477 : : public:
478 : : std::string as_string () const override;
479 : :
480 : 195 : ReferencePattern (std::unique_ptr<Pattern> pattern, bool is_mut_reference,
481 : : bool ref_has_two_amps, location_t locus)
482 : 195 : : has_two_amps (ref_has_two_amps), is_mut (is_mut_reference),
483 : 195 : pattern (std::move (pattern)), locus (locus),
484 : 195 : node_id (Analysis::Mappings::get ().get_next_node_id ())
485 : 195 : {}
486 : :
487 : : // Copy constructor requires clone
488 : 10 : ReferencePattern (ReferencePattern const &other)
489 : 10 : : has_two_amps (other.has_two_amps), is_mut (other.is_mut),
490 : 10 : pattern (other.pattern->clone_pattern ()), locus (other.locus),
491 : 10 : node_id (other.node_id)
492 : 10 : {}
493 : :
494 : : // Overload assignment operator to clone
495 : : ReferencePattern &operator= (ReferencePattern const &other)
496 : : {
497 : : pattern = other.pattern->clone_pattern ();
498 : : is_mut = other.is_mut;
499 : : has_two_amps = other.has_two_amps;
500 : : locus = other.locus;
501 : : node_id = other.node_id;
502 : :
503 : : return *this;
504 : : }
505 : :
506 : : // default move semantics
507 : : ReferencePattern (ReferencePattern &&other) = default;
508 : : ReferencePattern &operator= (ReferencePattern &&other) = default;
509 : :
510 : 218 : location_t get_locus () const override final { return locus; }
511 : :
512 : : void accept_vis (ASTVisitor &vis) override;
513 : :
514 : : // TODO: is this better? Or is a "vis_pattern" better?
515 : 4077 : Pattern &get_referenced_pattern ()
516 : : {
517 : 4077 : rust_assert (pattern != nullptr);
518 : 4077 : return *pattern;
519 : : }
520 : :
521 : 193 : bool is_double_reference () const { return has_two_amps; }
522 : :
523 : 193 : bool get_is_mut () const { return is_mut; }
524 : :
525 : 199 : NodeId get_node_id () const override { return node_id; }
526 : :
527 : 0 : Pattern::Kind get_pattern_kind () override
528 : : {
529 : 0 : return Pattern::Kind::Reference;
530 : : }
531 : :
532 : : protected:
533 : : /* Use covariance to implement clone function as returning this object rather
534 : : * than base */
535 : 10 : ReferencePattern *clone_pattern_impl () const override
536 : : {
537 : 10 : return new ReferencePattern (*this);
538 : : }
539 : : };
540 : :
541 : : #if 0
542 : : // aka StructPatternEtCetera; potential element in struct pattern
543 : : struct StructPatternEtc
544 : : {
545 : : private:
546 : : std::vector<Attribute> outer_attrs;
547 : :
548 : : // should this store location data?
549 : :
550 : : public:
551 : : StructPatternEtc (std::vector<Attribute> outer_attribs)
552 : : : outer_attrs (std::move (outer_attribs))
553 : : {}
554 : :
555 : : // Creates an empty StructPatternEtc
556 : : static StructPatternEtc create_empty ()
557 : : {
558 : : return StructPatternEtc (std::vector<Attribute> ());
559 : : }
560 : : };
561 : : #endif
562 : :
563 : : // Base class for a single field in a struct pattern - abstract
564 : 4 : class StructPatternField
565 : : {
566 : : std::vector<Attribute> outer_attrs;
567 : : location_t locus;
568 : :
569 : : protected:
570 : : NodeId node_id;
571 : :
572 : : public:
573 : : enum ItemType
574 : : {
575 : : TUPLE_PAT,
576 : : IDENT_PAT,
577 : : IDENT
578 : : };
579 : :
580 : : virtual ~StructPatternField () {}
581 : :
582 : : // Unique pointer custom clone function
583 : 14 : std::unique_ptr<StructPatternField> clone_struct_pattern_field () const
584 : : {
585 : 14 : return std::unique_ptr<StructPatternField> (
586 : 14 : clone_struct_pattern_field_impl ());
587 : : }
588 : :
589 : : virtual std::string as_string () const;
590 : :
591 : 249 : location_t get_locus () const { return locus; }
592 : :
593 : : virtual void accept_vis (ASTVisitor &vis) = 0;
594 : :
595 : : virtual void mark_for_strip () = 0;
596 : : virtual bool is_marked_for_strip () const = 0;
597 : : virtual ItemType get_item_type () const = 0;
598 : :
599 : 348 : NodeId get_node_id () const { return node_id; }
600 : :
601 : : // TODO: seems kinda dodgy. Think of better way.
602 : 3327 : std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
603 : : const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
604 : :
605 : : protected:
606 : 250 : StructPatternField (std::vector<Attribute> outer_attribs, location_t locus,
607 : : NodeId node_id)
608 : 250 : : outer_attrs (std::move (outer_attribs)), locus (locus), node_id (node_id)
609 : : {}
610 : :
611 : : // Clone function implementation as pure virtual method
612 : : virtual StructPatternField *clone_struct_pattern_field_impl () const = 0;
613 : : };
614 : :
615 : : // Tuple pattern single field in a struct pattern
616 : : class StructPatternFieldTuplePat : public StructPatternField
617 : : {
618 : : TupleIndex index;
619 : : std::unique_ptr<Pattern> tuple_pattern;
620 : :
621 : : public:
622 : 3 : StructPatternFieldTuplePat (TupleIndex index,
623 : : std::unique_ptr<Pattern> tuple_pattern,
624 : : std::vector<Attribute> outer_attribs,
625 : : location_t locus)
626 : 3 : : StructPatternField (std::move (outer_attribs), locus,
627 : 3 : Analysis::Mappings::get ().get_next_node_id ()),
628 : 3 : index (index), tuple_pattern (std::move (tuple_pattern))
629 : 3 : {}
630 : :
631 : : // Copy constructor requires clone
632 : 1 : StructPatternFieldTuplePat (StructPatternFieldTuplePat const &other)
633 : 1 : : StructPatternField (other), index (other.index)
634 : : {
635 : : // guard to prevent null dereference (only required if error state)
636 : 1 : node_id = other.get_node_id ();
637 : 1 : if (other.tuple_pattern != nullptr)
638 : 1 : tuple_pattern = other.tuple_pattern->clone_pattern ();
639 : 1 : }
640 : :
641 : : // Overload assignment operator to perform clone
642 : : StructPatternFieldTuplePat &
643 : : operator= (StructPatternFieldTuplePat const &other)
644 : : {
645 : : StructPatternField::operator= (other);
646 : : index = other.index;
647 : : // outer_attrs = other.outer_attrs;
648 : : node_id = other.get_node_id ();
649 : :
650 : : // guard to prevent null dereference (only required if error state)
651 : : if (other.tuple_pattern != nullptr)
652 : : tuple_pattern = other.tuple_pattern->clone_pattern ();
653 : : else
654 : : tuple_pattern = nullptr;
655 : :
656 : : return *this;
657 : : }
658 : :
659 : : // default move semantics
660 : : StructPatternFieldTuplePat (StructPatternFieldTuplePat &&other) = default;
661 : : StructPatternFieldTuplePat &operator= (StructPatternFieldTuplePat &&other)
662 : : = default;
663 : :
664 : : std::string as_string () const override;
665 : :
666 : : void accept_vis (ASTVisitor &vis) override;
667 : :
668 : : // based on idea of tuple pattern no longer existing
669 : 0 : void mark_for_strip () override { tuple_pattern = nullptr; }
670 : 4 : bool is_marked_for_strip () const override
671 : : {
672 : 4 : return tuple_pattern == nullptr;
673 : : }
674 : :
675 : 1 : TupleIndex get_index () { return index; }
676 : :
677 : : // TODO: is this better? Or is a "vis_pattern" better?
678 : 19 : Pattern &get_index_pattern ()
679 : : {
680 : 19 : rust_assert (tuple_pattern != nullptr);
681 : 19 : return *tuple_pattern;
682 : : }
683 : :
684 : 1 : ItemType get_item_type () const override final { return ItemType::TUPLE_PAT; }
685 : :
686 : : protected:
687 : : /* Use covariance to implement clone function as returning this object rather
688 : : * than base */
689 : 1 : StructPatternFieldTuplePat *clone_struct_pattern_field_impl () const override
690 : : {
691 : 1 : return new StructPatternFieldTuplePat (*this);
692 : : }
693 : : };
694 : :
695 : : // Identifier pattern single field in a struct pattern
696 : : class StructPatternFieldIdentPat : public StructPatternField
697 : : {
698 : : Identifier ident;
699 : : std::unique_ptr<Pattern> ident_pattern;
700 : :
701 : : public:
702 : 149 : StructPatternFieldIdentPat (Identifier ident,
703 : : std::unique_ptr<Pattern> ident_pattern,
704 : : std::vector<Attribute> outer_attrs,
705 : : location_t locus)
706 : 149 : : StructPatternField (std::move (outer_attrs), locus,
707 : 149 : Analysis::Mappings::get ().get_next_node_id ()),
708 : 149 : ident (std::move (ident)), ident_pattern (std::move (ident_pattern))
709 : 149 : {}
710 : :
711 : : // Copy constructor requires clone
712 : 1 : StructPatternFieldIdentPat (StructPatternFieldIdentPat const &other)
713 : 1 : : StructPatternField (other), ident (other.ident)
714 : : {
715 : : // guard to prevent null dereference (only required if error state)
716 : 1 : node_id = other.get_node_id ();
717 : 1 : if (other.ident_pattern != nullptr)
718 : 1 : ident_pattern = other.ident_pattern->clone_pattern ();
719 : 1 : }
720 : :
721 : : // Overload assignment operator to clone
722 : : StructPatternFieldIdentPat &
723 : : operator= (StructPatternFieldIdentPat const &other)
724 : : {
725 : : StructPatternField::operator= (other);
726 : : ident = other.ident;
727 : : // outer_attrs = other.outer_attrs;
728 : : node_id = other.get_node_id ();
729 : :
730 : : // guard to prevent null dereference (only required if error state)
731 : : if (other.ident_pattern != nullptr)
732 : : ident_pattern = other.ident_pattern->clone_pattern ();
733 : : else
734 : : ident_pattern = nullptr;
735 : :
736 : : return *this;
737 : : }
738 : :
739 : : // default move semantics
740 : : StructPatternFieldIdentPat (StructPatternFieldIdentPat &&other) = default;
741 : : StructPatternFieldIdentPat &operator= (StructPatternFieldIdentPat &&other)
742 : : = default;
743 : :
744 : : std::string as_string () const override;
745 : :
746 : : void accept_vis (ASTVisitor &vis) override;
747 : :
748 : : // based on idea of identifier pattern no longer existing
749 : 0 : void mark_for_strip () override { ident_pattern = nullptr; }
750 : 512 : bool is_marked_for_strip () const override
751 : : {
752 : 512 : return ident_pattern == nullptr;
753 : : }
754 : :
755 : 149 : const Identifier &get_identifier () const { return ident; }
756 : :
757 : : // TODO: is this better? Or is a "vis_pattern" better?
758 : 2962 : Pattern &get_ident_pattern ()
759 : : {
760 : 2962 : rust_assert (ident_pattern != nullptr);
761 : 2962 : return *ident_pattern;
762 : : }
763 : :
764 : 149 : ItemType get_item_type () const override final { return ItemType::IDENT_PAT; }
765 : :
766 : : protected:
767 : : /* Use covariance to implement clone function as returning this object rather
768 : : * than base */
769 : 1 : StructPatternFieldIdentPat *clone_struct_pattern_field_impl () const override
770 : : {
771 : 1 : return new StructPatternFieldIdentPat (*this);
772 : : }
773 : : };
774 : :
775 : : // Identifier only (with no pattern) single field in a struct pattern
776 : : class StructPatternFieldIdent : public StructPatternField
777 : : {
778 : : bool has_ref;
779 : : bool has_mut;
780 : : Identifier ident;
781 : :
782 : : public:
783 : 98 : StructPatternFieldIdent (Identifier ident, bool is_ref, bool is_mut,
784 : : std::vector<Attribute> outer_attrs, location_t locus)
785 : 98 : : StructPatternField (std::move (outer_attrs), locus,
786 : 98 : Analysis::Mappings::get ().get_next_node_id ()),
787 : 98 : has_ref (is_ref), has_mut (is_mut), ident (std::move (ident))
788 : 98 : {}
789 : :
790 : : std::string as_string () const override;
791 : :
792 : : void accept_vis (ASTVisitor &vis) override;
793 : :
794 : : // based on idea of identifier no longer existing
795 : 0 : void mark_for_strip () override { ident = {""}; }
796 : 248 : bool is_marked_for_strip () const override { return ident.empty (); }
797 : :
798 : 196 : const Identifier &get_identifier () const { return ident; }
799 : :
800 : 98 : ItemType get_item_type () const override final { return ItemType::IDENT; }
801 : :
802 : 196 : bool is_ref () const { return has_ref; }
803 : :
804 : 196 : bool is_mut () const { return has_mut; }
805 : :
806 : : protected:
807 : : /* Use covariance to implement clone function as returning this object rather
808 : : * than base */
809 : 12 : StructPatternFieldIdent *clone_struct_pattern_field_impl () const override
810 : : {
811 : 12 : return new StructPatternFieldIdent (*this);
812 : : }
813 : : };
814 : :
815 : : // Elements of a struct pattern
816 : 183 : class StructPatternElements
817 : : {
818 : : // bool has_struct_pattern_fields;
819 : : std::vector<std::unique_ptr<StructPatternField>> fields;
820 : :
821 : : bool has_struct_pattern_etc;
822 : : std::vector<Attribute> struct_pattern_etc_attrs;
823 : : // StructPatternEtc etc;
824 : :
825 : : // must have at least one of the two and maybe both
826 : :
827 : : // should this store location data?
828 : :
829 : : public:
830 : : // Returns whether there are any struct pattern fields
831 : 455 : bool has_struct_pattern_fields () const { return !fields.empty (); }
832 : :
833 : : /* Returns whether the struct pattern elements is entirely empty (no fields,
834 : : * no etc). */
835 : 454 : bool is_empty () const
836 : : {
837 : 2 : return !has_struct_pattern_fields () && !has_struct_pattern_etc;
838 : : }
839 : :
840 : 601 : bool has_etc () const { return has_struct_pattern_etc; }
841 : :
842 : : // Constructor for StructPatternElements with both (potentially)
843 : 0 : StructPatternElements (
844 : : std::vector<std::unique_ptr<StructPatternField>> fields,
845 : : std::vector<Attribute> etc_attrs)
846 : 0 : : fields (std::move (fields)), has_struct_pattern_etc (true),
847 : 0 : struct_pattern_etc_attrs (std::move (etc_attrs))
848 : : {}
849 : :
850 : : // Constructor for StructPatternElements with no StructPatternEtc
851 : 149 : StructPatternElements (
852 : : std::vector<std::unique_ptr<StructPatternField>> fields)
853 : 56 : : fields (std::move (fields)), has_struct_pattern_etc (false),
854 : 126 : struct_pattern_etc_attrs ()
855 : : {}
856 : :
857 : : // Copy constructor with vector clone
858 : 12 : StructPatternElements (StructPatternElements const &other)
859 : 12 : : has_struct_pattern_etc (other.has_struct_pattern_etc),
860 : 12 : struct_pattern_etc_attrs (other.struct_pattern_etc_attrs)
861 : : {
862 : 12 : fields.reserve (other.fields.size ());
863 : 26 : for (const auto &e : other.fields)
864 : 14 : fields.push_back (e->clone_struct_pattern_field ());
865 : 12 : }
866 : :
867 : : // Overloaded assignment operator with vector clone
868 : : StructPatternElements &operator= (StructPatternElements const &other)
869 : : {
870 : : struct_pattern_etc_attrs = other.struct_pattern_etc_attrs;
871 : : has_struct_pattern_etc = other.has_struct_pattern_etc;
872 : :
873 : : fields.clear ();
874 : : fields.reserve (other.fields.size ());
875 : : for (const auto &e : other.fields)
876 : : fields.push_back (e->clone_struct_pattern_field ());
877 : :
878 : : return *this;
879 : : }
880 : :
881 : : // move constructors
882 : 288 : StructPatternElements (StructPatternElements &&other) = default;
883 : : StructPatternElements &operator= (StructPatternElements &&other) = default;
884 : :
885 : : // Creates an empty StructPatternElements
886 : 0 : static StructPatternElements create_empty ()
887 : : {
888 : 0 : return StructPatternElements (
889 : 0 : std::vector<std::unique_ptr<StructPatternField>> ());
890 : : }
891 : :
892 : : std::string as_string () const;
893 : :
894 : : // TODO: seems kinda dodgy. Think of better way.
895 : 453 : std::vector<std::unique_ptr<StructPatternField>> &get_struct_pattern_fields ()
896 : : {
897 : 2396 : return fields;
898 : : }
899 : : const std::vector<std::unique_ptr<StructPatternField>> &
900 : : get_struct_pattern_fields () const
901 : : {
902 : : return fields;
903 : : }
904 : :
905 : 0 : std::vector<Attribute> &get_etc_outer_attrs ()
906 : : {
907 : 1795 : return struct_pattern_etc_attrs;
908 : : }
909 : : const std::vector<Attribute> &get_etc_outer_attrs () const
910 : : {
911 : : return struct_pattern_etc_attrs;
912 : : }
913 : :
914 : 0 : void strip_etc ()
915 : : {
916 : 0 : has_struct_pattern_etc = false;
917 : 0 : struct_pattern_etc_attrs.clear ();
918 : 0 : struct_pattern_etc_attrs.shrink_to_fit ();
919 : : }
920 : : };
921 : :
922 : : // Struct pattern AST node representation
923 : : class StructPattern : public Pattern
924 : : {
925 : : PathInExpression path;
926 : :
927 : : // bool has_struct_pattern_elements;
928 : : StructPatternElements elems;
929 : :
930 : : NodeId node_id;
931 : : location_t locus;
932 : :
933 : : public:
934 : : std::string as_string () const override;
935 : :
936 : : // Constructs a struct pattern from specified StructPatternElements
937 : 149 : StructPattern (PathInExpression struct_path, location_t locus,
938 : : StructPatternElements elems
939 : : = StructPatternElements::create_empty ())
940 : 149 : : path (std::move (struct_path)), elems (std::move (elems)),
941 : 298 : node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus)
942 : 149 : {}
943 : :
944 : : /* TODO: constructor to construct via elements included in
945 : : * StructPatternElements */
946 : :
947 : : /* Returns whether struct pattern has any struct pattern elements (if not, it
948 : : * is empty). */
949 : 454 : bool has_struct_pattern_elems () const { return !elems.is_empty (); }
950 : :
951 : 149 : location_t get_locus () const override { return locus; }
952 : :
953 : : void accept_vis (ASTVisitor &vis) override;
954 : :
955 : : // TODO: seems kinda dodgy. Think of better way.
956 : 2396 : StructPatternElements &get_struct_pattern_elems () { return elems; }
957 : : const StructPatternElements &get_struct_pattern_elems () const
958 : : {
959 : : return elems;
960 : : }
961 : :
962 : 2398 : PathInExpression &get_path () { return path; }
963 : : const PathInExpression &get_path () const { return path; }
964 : :
965 : 148 : NodeId get_node_id () const override { return node_id; }
966 : :
967 : 0 : Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Struct; }
968 : :
969 : : protected:
970 : : /* Use covariance to implement clone function as returning this object rather
971 : : * than base */
972 : 1 : StructPattern *clone_pattern_impl () const override
973 : : {
974 : 1 : return new StructPattern (*this);
975 : : }
976 : : };
977 : :
978 : : // Base abstract class for patterns used in TupleStructPattern
979 : 1373 : class TupleStructItems
980 : : {
981 : : public:
982 : : enum ItemType
983 : : {
984 : : RANGE,
985 : : NO_RANGE
986 : : };
987 : :
988 : : virtual ~TupleStructItems () {}
989 : :
990 : : // TODO: should this store location data?
991 : :
992 : : // Unique pointer custom clone function
993 : 324 : std::unique_ptr<TupleStructItems> clone_tuple_struct_items () const
994 : : {
995 : 324 : return std::unique_ptr<TupleStructItems> (clone_tuple_struct_items_impl ());
996 : : }
997 : :
998 : : virtual std::string as_string () const = 0;
999 : :
1000 : : virtual void accept_vis (ASTVisitor &vis) = 0;
1001 : :
1002 : : virtual ItemType get_item_type () const = 0;
1003 : :
1004 : : protected:
1005 : : // pure virtual clone implementation
1006 : : virtual TupleStructItems *clone_tuple_struct_items_impl () const = 0;
1007 : : };
1008 : :
1009 : : // Class for non-ranged tuple struct pattern patterns
1010 : : class TupleStructItemsNoRange : public TupleStructItems
1011 : : {
1012 : : std::vector<std::unique_ptr<Pattern>> patterns;
1013 : :
1014 : : public:
1015 : 1049 : TupleStructItemsNoRange (std::vector<std::unique_ptr<Pattern>> patterns)
1016 : 1049 : : patterns (std::move (patterns))
1017 : : {}
1018 : :
1019 : : // Copy constructor with vector clone
1020 : 324 : TupleStructItemsNoRange (TupleStructItemsNoRange const &other)
1021 : 324 : {
1022 : 324 : patterns.reserve (other.patterns.size ());
1023 : 648 : for (const auto &e : other.patterns)
1024 : 324 : patterns.push_back (e->clone_pattern ());
1025 : 324 : }
1026 : :
1027 : : // Overloaded assignment operator with vector clone
1028 : : TupleStructItemsNoRange &operator= (TupleStructItemsNoRange const &other)
1029 : : {
1030 : : patterns.clear ();
1031 : : patterns.reserve (other.patterns.size ());
1032 : : for (const auto &e : other.patterns)
1033 : : patterns.push_back (e->clone_pattern ());
1034 : :
1035 : : return *this;
1036 : : }
1037 : :
1038 : : // move constructors
1039 : : TupleStructItemsNoRange (TupleStructItemsNoRange &&other) = default;
1040 : : TupleStructItemsNoRange &operator= (TupleStructItemsNoRange &&other)
1041 : : = default;
1042 : :
1043 : : std::string as_string () const override;
1044 : :
1045 : : void accept_vis (ASTVisitor &vis) override;
1046 : :
1047 : : // TODO: seems kinda dodgy. Think of better way.
1048 : 23856 : std::vector<std::unique_ptr<Pattern>> &get_patterns () { return patterns; }
1049 : : const std::vector<std::unique_ptr<Pattern>> &get_patterns () const
1050 : : {
1051 : : return patterns;
1052 : : }
1053 : :
1054 : 962 : ItemType get_item_type () const override final { return ItemType::NO_RANGE; }
1055 : :
1056 : : protected:
1057 : : /* Use covariance to implement clone function as returning this object rather
1058 : : * than base */
1059 : 324 : TupleStructItemsNoRange *clone_tuple_struct_items_impl () const override
1060 : : {
1061 : 324 : return new TupleStructItemsNoRange (*this);
1062 : : }
1063 : : };
1064 : :
1065 : : // Class for ranged tuple struct pattern patterns
1066 : : class TupleStructItemsRange : public TupleStructItems
1067 : : {
1068 : : std::vector<std::unique_ptr<Pattern>> lower_patterns;
1069 : : std::vector<std::unique_ptr<Pattern>> upper_patterns;
1070 : :
1071 : : public:
1072 : 0 : TupleStructItemsRange (std::vector<std::unique_ptr<Pattern>> lower_patterns,
1073 : : std::vector<std::unique_ptr<Pattern>> upper_patterns)
1074 : 0 : : lower_patterns (std::move (lower_patterns)),
1075 : 0 : upper_patterns (std::move (upper_patterns))
1076 : : {}
1077 : :
1078 : : // Copy constructor with vector clone
1079 : 0 : TupleStructItemsRange (TupleStructItemsRange const &other)
1080 : 0 : {
1081 : 0 : lower_patterns.reserve (other.lower_patterns.size ());
1082 : 0 : for (const auto &e : other.lower_patterns)
1083 : 0 : lower_patterns.push_back (e->clone_pattern ());
1084 : :
1085 : 0 : upper_patterns.reserve (other.upper_patterns.size ());
1086 : 0 : for (const auto &e : other.upper_patterns)
1087 : 0 : upper_patterns.push_back (e->clone_pattern ());
1088 : 0 : }
1089 : :
1090 : : // Overloaded assignment operator to clone
1091 : : TupleStructItemsRange &operator= (TupleStructItemsRange const &other)
1092 : : {
1093 : : lower_patterns.clear ();
1094 : : lower_patterns.reserve (other.lower_patterns.size ());
1095 : : for (const auto &e : other.lower_patterns)
1096 : : lower_patterns.push_back (e->clone_pattern ());
1097 : :
1098 : : upper_patterns.clear ();
1099 : : upper_patterns.reserve (other.upper_patterns.size ());
1100 : : for (const auto &e : other.upper_patterns)
1101 : : upper_patterns.push_back (e->clone_pattern ());
1102 : :
1103 : : return *this;
1104 : : }
1105 : :
1106 : : // move constructors
1107 : : TupleStructItemsRange (TupleStructItemsRange &&other) = default;
1108 : : TupleStructItemsRange &operator= (TupleStructItemsRange &&other) = default;
1109 : :
1110 : : std::string as_string () const override;
1111 : :
1112 : : void accept_vis (ASTVisitor &vis) override;
1113 : :
1114 : : // TODO: seems kinda dodgy. Think of better way.
1115 : : std::vector<std::unique_ptr<Pattern>> &get_lower_patterns ()
1116 : : {
1117 : 0 : return lower_patterns;
1118 : : }
1119 : : const std::vector<std::unique_ptr<Pattern>> &get_lower_patterns () const
1120 : : {
1121 : : return lower_patterns;
1122 : : }
1123 : :
1124 : : // TODO: seems kinda dodgy. Think of better way.
1125 : : std::vector<std::unique_ptr<Pattern>> &get_upper_patterns ()
1126 : : {
1127 : 0 : return upper_patterns;
1128 : : }
1129 : : const std::vector<std::unique_ptr<Pattern>> &get_upper_patterns () const
1130 : : {
1131 : : return upper_patterns;
1132 : : }
1133 : :
1134 : 0 : ItemType get_item_type () const override final { return ItemType::RANGE; }
1135 : :
1136 : : protected:
1137 : : /* Use covariance to implement clone function as returning this object rather
1138 : : * than base */
1139 : 0 : TupleStructItemsRange *clone_tuple_struct_items_impl () const override
1140 : : {
1141 : 0 : return new TupleStructItemsRange (*this);
1142 : : }
1143 : : };
1144 : :
1145 : : // AST node representing a tuple struct pattern
1146 : : class TupleStructPattern : public Pattern
1147 : : {
1148 : : PathInExpression path;
1149 : : std::unique_ptr<TupleStructItems> items;
1150 : : NodeId node_id;
1151 : :
1152 : : /* TOOD: should this store location data? current accessor uses path location
1153 : : * data */
1154 : :
1155 : : public:
1156 : : std::string as_string () const override;
1157 : :
1158 : 1049 : TupleStructPattern (PathInExpression tuple_struct_path,
1159 : : std::unique_ptr<TupleStructItems> items)
1160 : 1049 : : path (std::move (tuple_struct_path)), items (std::move (items)),
1161 : 1049 : node_id (Analysis::Mappings::get ().get_next_node_id ())
1162 : : {
1163 : 1049 : rust_assert (this->items != nullptr);
1164 : 1049 : }
1165 : :
1166 : : // Copy constructor required to clone
1167 : 324 : TupleStructPattern (TupleStructPattern const &other) : path (other.path)
1168 : : {
1169 : : // guard to protect from null dereference
1170 : 324 : rust_assert (other.items != nullptr);
1171 : :
1172 : 324 : node_id = other.node_id;
1173 : 324 : items = other.items->clone_tuple_struct_items ();
1174 : 324 : }
1175 : :
1176 : : // Operator overload assignment operator to clone
1177 : : TupleStructPattern &operator= (TupleStructPattern const &other)
1178 : : {
1179 : : path = other.path;
1180 : : node_id = other.node_id;
1181 : :
1182 : : // guard to protect from null dereference
1183 : : rust_assert (other.items != nullptr);
1184 : :
1185 : : items = other.items->clone_tuple_struct_items ();
1186 : :
1187 : : return *this;
1188 : : }
1189 : :
1190 : : // move constructors
1191 : : TupleStructPattern (TupleStructPattern &&other) = default;
1192 : : TupleStructPattern &operator= (TupleStructPattern &&other) = default;
1193 : :
1194 : 997 : location_t get_locus () const override { return path.get_locus (); }
1195 : :
1196 : : void accept_vis (ASTVisitor &vis) override;
1197 : :
1198 : 19079 : TupleStructItems &get_items ()
1199 : : {
1200 : 19079 : rust_assert (items != nullptr);
1201 : 19079 : return *items;
1202 : : }
1203 : :
1204 : 23856 : PathInExpression &get_path () { return path; }
1205 : : const PathInExpression &get_path () const { return path; }
1206 : :
1207 : 962 : NodeId get_node_id () const override { return node_id; }
1208 : :
1209 : 0 : Pattern::Kind get_pattern_kind () override
1210 : : {
1211 : 0 : return Pattern::Kind::TupleStruct;
1212 : : }
1213 : :
1214 : : protected:
1215 : : /* Use covariance to implement clone function as returning this object rather
1216 : : * than base */
1217 : 324 : TupleStructPattern *clone_pattern_impl () const override
1218 : : {
1219 : 324 : return new TupleStructPattern (*this);
1220 : : }
1221 : : };
1222 : :
1223 : : // Base abstract class representing TuplePattern patterns
1224 : 1412 : class TuplePatternItems
1225 : : {
1226 : : public:
1227 : : enum TuplePatternItemType
1228 : : {
1229 : : MULTIPLE,
1230 : : RANGED,
1231 : : };
1232 : :
1233 : : virtual ~TuplePatternItems () {}
1234 : :
1235 : : // TODO: should this store location data?
1236 : :
1237 : : // Unique pointer custom clone function
1238 : 986 : std::unique_ptr<TuplePatternItems> clone_tuple_pattern_items () const
1239 : : {
1240 : 986 : return std::unique_ptr<TuplePatternItems> (
1241 : 986 : clone_tuple_pattern_items_impl ());
1242 : : }
1243 : :
1244 : : virtual std::string as_string () const = 0;
1245 : :
1246 : : virtual void accept_vis (ASTVisitor &vis) = 0;
1247 : :
1248 : : virtual TuplePatternItemType get_pattern_type () const = 0;
1249 : :
1250 : : protected:
1251 : : // pure virtual clone implementation
1252 : : virtual TuplePatternItems *clone_tuple_pattern_items_impl () const = 0;
1253 : : };
1254 : :
1255 : : // Class representing TuplePattern patterns where there are multiple patterns
1256 : : class TuplePatternItemsMultiple : public TuplePatternItems
1257 : : {
1258 : : std::vector<std::unique_ptr<Pattern>> patterns;
1259 : :
1260 : : public:
1261 : 402 : TuplePatternItemsMultiple (std::vector<std::unique_ptr<Pattern>> patterns)
1262 : 402 : : patterns (std::move (patterns))
1263 : : {}
1264 : :
1265 : : // Copy constructor with vector clone
1266 : 986 : TuplePatternItemsMultiple (TuplePatternItemsMultiple const &other)
1267 : 986 : {
1268 : 986 : patterns.reserve (other.patterns.size ());
1269 : 2934 : for (const auto &e : other.patterns)
1270 : 1948 : patterns.push_back (e->clone_pattern ());
1271 : 986 : }
1272 : :
1273 : : // Overloaded assignment operator to vector clone
1274 : : TuplePatternItemsMultiple &operator= (TuplePatternItemsMultiple const &other)
1275 : : {
1276 : : patterns.clear ();
1277 : : patterns.reserve (other.patterns.size ());
1278 : : for (const auto &e : other.patterns)
1279 : : patterns.push_back (e->clone_pattern ());
1280 : :
1281 : : return *this;
1282 : : }
1283 : :
1284 : : // move constructors
1285 : : TuplePatternItemsMultiple (TuplePatternItemsMultiple &&other) = default;
1286 : : TuplePatternItemsMultiple &operator= (TuplePatternItemsMultiple &&other)
1287 : : = default;
1288 : :
1289 : : std::string as_string () const override;
1290 : :
1291 : : void accept_vis (ASTVisitor &vis) override;
1292 : :
1293 : : // TODO: seems kinda dodgy. Think of better way.
1294 : 12980 : std::vector<std::unique_ptr<Pattern>> &get_patterns () { return patterns; }
1295 : : const std::vector<std::unique_ptr<Pattern>> &get_patterns () const
1296 : : {
1297 : : return patterns;
1298 : : }
1299 : :
1300 : 377 : TuplePatternItemType get_pattern_type () const override
1301 : : {
1302 : 377 : return TuplePatternItemType::MULTIPLE;
1303 : : }
1304 : :
1305 : : protected:
1306 : : /* Use covariance to implement clone function as returning this object rather
1307 : : * than base */
1308 : 986 : TuplePatternItemsMultiple *clone_tuple_pattern_items_impl () const override
1309 : : {
1310 : 986 : return new TuplePatternItemsMultiple (*this);
1311 : : }
1312 : : };
1313 : :
1314 : : // Class representing TuplePattern patterns where there are a range of patterns
1315 : : class TuplePatternItemsRanged : public TuplePatternItems
1316 : : {
1317 : : std::vector<std::unique_ptr<Pattern>> lower_patterns;
1318 : : std::vector<std::unique_ptr<Pattern>> upper_patterns;
1319 : :
1320 : : public:
1321 : 24 : TuplePatternItemsRanged (std::vector<std::unique_ptr<Pattern>> lower_patterns,
1322 : : std::vector<std::unique_ptr<Pattern>> upper_patterns)
1323 : 24 : : lower_patterns (std::move (lower_patterns)),
1324 : 24 : upper_patterns (std::move (upper_patterns))
1325 : : {}
1326 : :
1327 : : // Copy constructor with vector clone
1328 : 0 : TuplePatternItemsRanged (TuplePatternItemsRanged const &other)
1329 : 0 : {
1330 : 0 : lower_patterns.reserve (other.lower_patterns.size ());
1331 : 0 : for (const auto &e : other.lower_patterns)
1332 : 0 : lower_patterns.push_back (e->clone_pattern ());
1333 : :
1334 : 0 : upper_patterns.reserve (other.upper_patterns.size ());
1335 : 0 : for (const auto &e : other.upper_patterns)
1336 : 0 : upper_patterns.push_back (e->clone_pattern ());
1337 : 0 : }
1338 : :
1339 : : // Overloaded assignment operator to clone
1340 : : TuplePatternItemsRanged &operator= (TuplePatternItemsRanged const &other)
1341 : : {
1342 : : lower_patterns.clear ();
1343 : : lower_patterns.reserve (other.lower_patterns.size ());
1344 : : for (const auto &e : other.lower_patterns)
1345 : : lower_patterns.push_back (e->clone_pattern ());
1346 : :
1347 : : upper_patterns.clear ();
1348 : : upper_patterns.reserve (other.upper_patterns.size ());
1349 : : for (const auto &e : other.upper_patterns)
1350 : : upper_patterns.push_back (e->clone_pattern ());
1351 : :
1352 : : return *this;
1353 : : }
1354 : :
1355 : : // move constructors
1356 : : TuplePatternItemsRanged (TuplePatternItemsRanged &&other) = default;
1357 : : TuplePatternItemsRanged &operator= (TuplePatternItemsRanged &&other)
1358 : : = default;
1359 : :
1360 : : std::string as_string () const override;
1361 : :
1362 : : void accept_vis (ASTVisitor &vis) override;
1363 : :
1364 : : // TODO: seems kinda dodgy. Think of better way.
1365 : : std::vector<std::unique_ptr<Pattern>> &get_lower_patterns ()
1366 : : {
1367 : 496 : return lower_patterns;
1368 : : }
1369 : : const std::vector<std::unique_ptr<Pattern>> &get_lower_patterns () const
1370 : : {
1371 : : return lower_patterns;
1372 : : }
1373 : :
1374 : : // TODO: seems kinda dodgy. Think of better way.
1375 : : std::vector<std::unique_ptr<Pattern>> &get_upper_patterns ()
1376 : : {
1377 : 496 : return upper_patterns;
1378 : : }
1379 : : const std::vector<std::unique_ptr<Pattern>> &get_upper_patterns () const
1380 : : {
1381 : : return upper_patterns;
1382 : : }
1383 : :
1384 : 24 : TuplePatternItemType get_pattern_type () const override
1385 : : {
1386 : 24 : return TuplePatternItemType::RANGED;
1387 : : }
1388 : :
1389 : : protected:
1390 : : /* Use covariance to implement clone function as returning this object rather
1391 : : * than base */
1392 : 0 : TuplePatternItemsRanged *clone_tuple_pattern_items_impl () const override
1393 : : {
1394 : 0 : return new TuplePatternItemsRanged (*this);
1395 : : }
1396 : : };
1397 : :
1398 : : // AST node representing a tuple pattern
1399 : : class TuplePattern : public Pattern
1400 : : {
1401 : : std::unique_ptr<TuplePatternItems> items;
1402 : : location_t locus;
1403 : : NodeId node_id;
1404 : :
1405 : : public:
1406 : : std::string as_string () const override;
1407 : :
1408 : 426 : TuplePattern (std::unique_ptr<TuplePatternItems> items, location_t locus)
1409 : 426 : : items (std::move (items)), locus (locus),
1410 : 426 : node_id (Analysis::Mappings::get ().get_next_node_id ())
1411 : : {
1412 : 426 : rust_assert (this->items != nullptr);
1413 : 426 : }
1414 : :
1415 : : // Copy constructor requires clone
1416 : 986 : TuplePattern (TuplePattern const &other) : locus (other.locus)
1417 : : {
1418 : : // guard to prevent null dereference
1419 : 986 : rust_assert (other.items != nullptr);
1420 : :
1421 : 986 : node_id = other.node_id;
1422 : 986 : items = other.items->clone_tuple_pattern_items ();
1423 : 986 : }
1424 : :
1425 : : // Overload assignment operator to clone
1426 : : TuplePattern &operator= (TuplePattern const &other)
1427 : : {
1428 : : locus = other.locus;
1429 : : node_id = other.node_id;
1430 : :
1431 : : // guard to prevent null dereference
1432 : : rust_assert (other.items != nullptr);
1433 : :
1434 : : items = other.items->clone_tuple_pattern_items ();
1435 : : return *this;
1436 : : }
1437 : :
1438 : 807 : location_t get_locus () const override final { return locus; }
1439 : :
1440 : : void accept_vis (ASTVisitor &vis) override;
1441 : :
1442 : : // TODO: seems kinda dodgy. Think of better way.
1443 : 10107 : TuplePatternItems &get_items ()
1444 : : {
1445 : 10107 : rust_assert (items != nullptr);
1446 : 10107 : return *items;
1447 : : }
1448 : :
1449 : 401 : NodeId get_node_id () const override { return node_id; }
1450 : :
1451 : 0 : Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Tuple; }
1452 : :
1453 : : protected:
1454 : : /* Use covariance to implement clone function as returning this object rather
1455 : : * than base */
1456 : 986 : TuplePattern *clone_pattern_impl () const override
1457 : : {
1458 : 986 : return new TuplePattern (*this);
1459 : : }
1460 : : };
1461 : :
1462 : : // AST node representing a pattern in parentheses, used to control precedence
1463 : : class GroupedPattern : public Pattern
1464 : : {
1465 : : std::unique_ptr<Pattern> pattern_in_parens;
1466 : : location_t locus;
1467 : : NodeId node_id;
1468 : :
1469 : : public:
1470 : 0 : std::string as_string () const override
1471 : : {
1472 : 0 : return "(" + pattern_in_parens->as_string () + ")";
1473 : : }
1474 : :
1475 : 43 : GroupedPattern (std::unique_ptr<Pattern> pattern_in_parens, location_t locus)
1476 : 43 : : pattern_in_parens (std::move (pattern_in_parens)), locus (locus),
1477 : 43 : node_id (Analysis::Mappings::get ().get_next_node_id ())
1478 : 43 : {}
1479 : :
1480 : : // Copy constructor uses clone
1481 : 0 : GroupedPattern (GroupedPattern const &other)
1482 : 0 : : pattern_in_parens (other.pattern_in_parens->clone_pattern ()),
1483 : 0 : locus (other.locus), node_id (other.node_id)
1484 : 0 : {}
1485 : :
1486 : : // Overload assignment operator to clone
1487 : : GroupedPattern &operator= (GroupedPattern const &other)
1488 : : {
1489 : : pattern_in_parens = other.pattern_in_parens->clone_pattern ();
1490 : : locus = other.locus;
1491 : : node_id = other.node_id;
1492 : :
1493 : : return *this;
1494 : : }
1495 : :
1496 : : // default move semantics
1497 : : GroupedPattern (GroupedPattern &&other) = default;
1498 : : GroupedPattern &operator= (GroupedPattern &&other) = default;
1499 : :
1500 : 1 : location_t get_locus () const override final { return locus; }
1501 : :
1502 : : void accept_vis (ASTVisitor &vis) override;
1503 : :
1504 : : // TODO: seems kinda dodgy. Think of better way.
1505 : 777 : Pattern &get_pattern_in_parens ()
1506 : : {
1507 : 777 : rust_assert (pattern_in_parens != nullptr);
1508 : 777 : return *pattern_in_parens;
1509 : : }
1510 : :
1511 : 0 : NodeId get_node_id () const override { return node_id; }
1512 : :
1513 : 0 : Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Grouped; }
1514 : :
1515 : : protected:
1516 : : /* Use covariance to implement clone function as returning this object rather
1517 : : * than base */
1518 : 0 : GroupedPattern *clone_pattern_impl () const override
1519 : : {
1520 : 0 : return new GroupedPattern (*this);
1521 : : }
1522 : : };
1523 : :
1524 : : // Base abstract class representing patterns in a SlicePattern
1525 : 35 : class SlicePatternItems
1526 : : {
1527 : : public:
1528 : : enum SlicePatternItemType
1529 : : {
1530 : : NO_REST,
1531 : : HAS_REST,
1532 : : };
1533 : :
1534 : : virtual ~SlicePatternItems () {}
1535 : :
1536 : : // TODO: should this store location data?
1537 : :
1538 : : // Unique pointer custom clone function
1539 : 0 : std::unique_ptr<SlicePatternItems> clone_slice_pattern_items () const
1540 : : {
1541 : 0 : return std::unique_ptr<SlicePatternItems> (
1542 : 0 : clone_slice_pattern_items_impl ());
1543 : : }
1544 : :
1545 : : virtual std::string as_string () const = 0;
1546 : :
1547 : : virtual void accept_vis (ASTVisitor &vis) = 0;
1548 : :
1549 : : virtual SlicePatternItemType get_pattern_type () const = 0;
1550 : :
1551 : : protected:
1552 : : // pure virtual clone implementation
1553 : : virtual SlicePatternItems *clone_slice_pattern_items_impl () const = 0;
1554 : : };
1555 : :
1556 : : // Class representing the patterns in a SlicePattern without `..`
1557 : : class SlicePatternItemsNoRest : public SlicePatternItems
1558 : : {
1559 : : std::vector<std::unique_ptr<Pattern>> patterns;
1560 : :
1561 : : public:
1562 : 33 : SlicePatternItemsNoRest (std::vector<std::unique_ptr<Pattern>> patterns)
1563 : 33 : : patterns (std::move (patterns))
1564 : : {}
1565 : :
1566 : : // Copy constructor with vector clone
1567 : 0 : SlicePatternItemsNoRest (SlicePatternItemsNoRest const &other)
1568 : 0 : {
1569 : 0 : patterns.reserve (other.patterns.size ());
1570 : 0 : for (const auto &e : other.patterns)
1571 : 0 : patterns.push_back (e->clone_pattern ());
1572 : 0 : }
1573 : :
1574 : : // Overloaded assignment operator to vector clone
1575 : : SlicePatternItemsNoRest &operator= (SlicePatternItemsNoRest const &other)
1576 : : {
1577 : : patterns.clear ();
1578 : : patterns.reserve (other.patterns.size ());
1579 : : for (const auto &e : other.patterns)
1580 : : patterns.push_back (e->clone_pattern ());
1581 : :
1582 : : return *this;
1583 : : }
1584 : :
1585 : : // move constructors
1586 : : SlicePatternItemsNoRest (SlicePatternItemsNoRest &&other) = default;
1587 : : SlicePatternItemsNoRest &operator= (SlicePatternItemsNoRest &&other)
1588 : : = default;
1589 : :
1590 : : std::string as_string () const override;
1591 : :
1592 : : void accept_vis (ASTVisitor &vis) override;
1593 : :
1594 : : // TODO: seems kinda dodgy. Think of better way.
1595 : 656 : std::vector<std::unique_ptr<Pattern>> &get_patterns () { return patterns; }
1596 : : const std::vector<std::unique_ptr<Pattern>> &get_patterns () const
1597 : : {
1598 : : return patterns;
1599 : : }
1600 : :
1601 : 32 : SlicePatternItemType get_pattern_type () const override
1602 : : {
1603 : 32 : return SlicePatternItemType::NO_REST;
1604 : : }
1605 : :
1606 : : protected:
1607 : : /* Use covariance to implement clone function as returning this object rather
1608 : : * than base */
1609 : 0 : SlicePatternItemsNoRest *clone_slice_pattern_items_impl () const override
1610 : : {
1611 : 0 : return new SlicePatternItemsNoRest (*this);
1612 : : }
1613 : : };
1614 : :
1615 : : // Class representing the patterns in a SlicePattern that contains a `..`
1616 : : class SlicePatternItemsHasRest : public SlicePatternItems
1617 : : {
1618 : : std::vector<std::unique_ptr<Pattern>> lower_patterns;
1619 : : std::vector<std::unique_ptr<Pattern>> upper_patterns;
1620 : :
1621 : : public:
1622 : 2 : SlicePatternItemsHasRest (
1623 : : std::vector<std::unique_ptr<Pattern>> lower_patterns,
1624 : : std::vector<std::unique_ptr<Pattern>> upper_patterns)
1625 : 2 : : lower_patterns (std::move (lower_patterns)),
1626 : 2 : upper_patterns (std::move (upper_patterns))
1627 : : {}
1628 : :
1629 : : // Copy constructor with vector clone
1630 : 0 : SlicePatternItemsHasRest (SlicePatternItemsHasRest const &other)
1631 : 0 : {
1632 : 0 : lower_patterns.reserve (other.lower_patterns.size ());
1633 : 0 : for (const auto &e : other.lower_patterns)
1634 : 0 : lower_patterns.push_back (e->clone_pattern ());
1635 : :
1636 : 0 : upper_patterns.reserve (other.upper_patterns.size ());
1637 : 0 : for (const auto &e : other.upper_patterns)
1638 : 0 : upper_patterns.push_back (e->clone_pattern ());
1639 : 0 : }
1640 : :
1641 : : // Overloaded assignment operator to clone
1642 : : SlicePatternItemsHasRest &operator= (SlicePatternItemsHasRest const &other)
1643 : : {
1644 : : lower_patterns.clear ();
1645 : : lower_patterns.reserve (other.lower_patterns.size ());
1646 : : for (const auto &e : other.lower_patterns)
1647 : : lower_patterns.push_back (e->clone_pattern ());
1648 : :
1649 : : upper_patterns.clear ();
1650 : : upper_patterns.reserve (other.upper_patterns.size ());
1651 : : for (const auto &e : other.upper_patterns)
1652 : : upper_patterns.push_back (e->clone_pattern ());
1653 : :
1654 : : return *this;
1655 : : }
1656 : :
1657 : : // move constructors
1658 : : SlicePatternItemsHasRest (SlicePatternItemsHasRest &&other) = default;
1659 : : SlicePatternItemsHasRest &operator= (SlicePatternItemsHasRest &&other)
1660 : : = default;
1661 : :
1662 : : std::string as_string () const override;
1663 : :
1664 : : void accept_vis (ASTVisitor &vis) override;
1665 : :
1666 : : // TODO: seems kinda dodgy. Think of better way.
1667 : 0 : std::vector<std::unique_ptr<Pattern>> &get_lower_patterns ()
1668 : : {
1669 : 0 : return lower_patterns;
1670 : : }
1671 : : const std::vector<std::unique_ptr<Pattern>> &get_lower_patterns () const
1672 : : {
1673 : : return lower_patterns;
1674 : : }
1675 : :
1676 : : // TODO: seems kinda dodgy. Think of better way.
1677 : 0 : std::vector<std::unique_ptr<Pattern>> &get_upper_patterns ()
1678 : : {
1679 : 0 : return upper_patterns;
1680 : : }
1681 : : const std::vector<std::unique_ptr<Pattern>> &get_upper_patterns () const
1682 : : {
1683 : : return upper_patterns;
1684 : : }
1685 : :
1686 : 0 : SlicePatternItemType get_pattern_type () const override
1687 : : {
1688 : 0 : return SlicePatternItemType::HAS_REST;
1689 : : }
1690 : :
1691 : : protected:
1692 : : /* Use covariance to implement clone function as returning this object rather
1693 : : * than base */
1694 : 0 : SlicePatternItemsHasRest *clone_slice_pattern_items_impl () const override
1695 : : {
1696 : 0 : return new SlicePatternItemsHasRest (*this);
1697 : : }
1698 : : };
1699 : :
1700 : : // AST node representing patterns that can match slices and arrays
1701 : : class SlicePattern : public Pattern
1702 : : {
1703 : : std::unique_ptr<SlicePatternItems> items;
1704 : : location_t locus;
1705 : : NodeId node_id;
1706 : :
1707 : : public:
1708 : : std::string as_string () const override;
1709 : :
1710 : 35 : SlicePattern (std::unique_ptr<SlicePatternItems> items, location_t locus)
1711 : 35 : : items (std::move (items)), locus (locus),
1712 : 35 : node_id (Analysis::Mappings::get ().get_next_node_id ())
1713 : 35 : {}
1714 : :
1715 : : // Copy constructor requires clone
1716 : 0 : SlicePattern (SlicePattern const &other) : locus (other.locus)
1717 : : {
1718 : : // guard to prevent null dereference
1719 : 0 : rust_assert (other.items != nullptr);
1720 : :
1721 : 0 : node_id = other.node_id;
1722 : 0 : items = other.items->clone_slice_pattern_items ();
1723 : 0 : }
1724 : :
1725 : : // Overloaded assignment operator to clone
1726 : : SlicePattern &operator= (SlicePattern const &other)
1727 : : {
1728 : : locus = other.locus;
1729 : : node_id = other.node_id;
1730 : :
1731 : : // guard to prevent null dereference
1732 : : rust_assert (other.items != nullptr);
1733 : :
1734 : : items = other.items->clone_slice_pattern_items ();
1735 : : return *this;
1736 : : }
1737 : :
1738 : : // move constructors
1739 : : SlicePattern (SlicePattern &&other) = default;
1740 : : SlicePattern &operator= (SlicePattern &&other) = default;
1741 : :
1742 : 64 : location_t get_locus () const override final { return locus; }
1743 : :
1744 : : void accept_vis (ASTVisitor &vis) override;
1745 : :
1746 : : // TODO: seems kinda dodgy. Think of better way.
1747 : 568 : SlicePatternItems &get_items ()
1748 : : {
1749 : 568 : rust_assert (items != nullptr);
1750 : 568 : return *items;
1751 : : }
1752 : :
1753 : 32 : NodeId get_node_id () const override { return node_id; }
1754 : :
1755 : 0 : Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Slice; }
1756 : :
1757 : : protected:
1758 : : /* Use covariance to implement clone function as returning this object rather
1759 : : * than base */
1760 : 0 : SlicePattern *clone_pattern_impl () const override
1761 : : {
1762 : 0 : return new SlicePattern (*this);
1763 : : }
1764 : : };
1765 : :
1766 : : // AST node for alternate patterns
1767 : : // joins together what are technically 'PatternNoTopAlt's
1768 : : class AltPattern : public Pattern
1769 : : {
1770 : : std::vector<std::unique_ptr<Pattern>> alts;
1771 : : location_t locus;
1772 : : NodeId node_id;
1773 : :
1774 : : public:
1775 : : std::string as_string () const override;
1776 : :
1777 : 190 : AltPattern (std::vector<std::unique_ptr<Pattern>> alts, location_t locus)
1778 : 190 : : alts (std::move (alts)), locus (locus),
1779 : 190 : node_id (Analysis::Mappings::get ().get_next_node_id ())
1780 : 190 : {}
1781 : :
1782 : : // Copy constructor with vector clone
1783 : 0 : AltPattern (AltPattern const &other) : locus (other.locus)
1784 : : {
1785 : 0 : node_id = other.node_id;
1786 : 0 : alts.reserve (other.alts.size ());
1787 : 0 : for (const auto &e : other.alts)
1788 : 0 : alts.push_back (e->clone_pattern ());
1789 : 0 : }
1790 : :
1791 : : // Overloaded assignment operator to vector clone
1792 : : AltPattern &operator= (AltPattern const &other)
1793 : : {
1794 : : locus = other.locus;
1795 : : node_id = other.node_id;
1796 : :
1797 : : alts.clear ();
1798 : : alts.reserve (other.alts.size ());
1799 : : for (const auto &e : other.alts)
1800 : : alts.push_back (e->clone_pattern ());
1801 : :
1802 : : return *this;
1803 : : }
1804 : :
1805 : : // move constructors
1806 : : AltPattern (AltPattern &&other) = default;
1807 : : AltPattern &operator= (AltPattern &&other) = default;
1808 : :
1809 : 294 : location_t get_locus () const override final { return locus; }
1810 : :
1811 : : void accept_vis (ASTVisitor &vis) override;
1812 : :
1813 : : // TODO: seems kinda dodgy. Think of better way.
1814 : 4436 : std::vector<std::unique_ptr<Pattern>> &get_alts () { return alts; }
1815 : : const std::vector<std::unique_ptr<Pattern>> &get_alts () const
1816 : : {
1817 : : return alts;
1818 : : }
1819 : :
1820 : 147 : NodeId get_node_id () const override { return node_id; }
1821 : :
1822 : 0 : Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Alt; }
1823 : :
1824 : : protected:
1825 : : /* Use covariance to implement clone function as returning this object rather
1826 : : * than base */
1827 : 0 : AltPattern *clone_pattern_impl () const override
1828 : : {
1829 : 0 : return new AltPattern (*this);
1830 : : }
1831 : : };
1832 : :
1833 : : // Moved definition to rust-path.h
1834 : : class Path;
1835 : :
1836 : : // Forward decls for paths (defined in rust-path.h)
1837 : : class PathInExpression;
1838 : : class QualifiedPathInExpression;
1839 : :
1840 : : // Replaced with forward decl - defined in rust-macro.h
1841 : : class MacroInvocation;
1842 : : } // namespace AST
1843 : : } // namespace Rust
1844 : :
1845 : : #endif
|