Debian Codegraph @

gcc/rust/ast/rust-path.h

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2
3 // This file is part of GCC.
4
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
8 // version.
9
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 // for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
18
19 #ifndef RUST_AST_PATH_H
20 #define RUST_AST_PATH_H
21 /* "Path" (identifier within namespaces, essentially) handling. Required include
22 * for virtually all AST-related functionality. */
23
24 #include "optional.h"
25 #include "rust-ast.h"
26 #include "rust-hir-map.h"
27 #include "rust-mapping-common.h"
28 #include "rust-system.h"
29 #include "system.h"
30
31 namespace Rust {
32 namespace AST {
33
34 // The "identifier" (not generic args) aspect of each path expression segment
35 class PathIdentSegment
36 {
37 std::string segment_name;
38 location_t locus;
39
40 // only allow identifiers, "super", "self", "Self", "crate", or "$crate"
41 public:
42 PathIdentSegment (std::string segment_name, location_t locus)
43 : segment_name (std::move (segment_name)), locus (locus)
44 {}
45
46 // Creates an error PathIdentSegment.
47 static PathIdentSegment create_error ()
48 {
49 return PathIdentSegment ("", UNDEF_LOCATION);
50 }
51
52 // Returns whether PathIdentSegment is in an error state.
53 bool is_error () const { return segment_name.empty (); }
54
55 std::string as_string () const { return segment_name; }
56
57 location_t get_locus () const { return locus; }
58
59 bool is_super_path_seg () const
60 {
61 return as_string ().compare ("super") == 0;
62 }
63 bool is_crate_path_seg () const
64 {
65 return as_string ().compare ("crate") == 0;
66 }
67 bool is_lower_self_seg () const { return as_string ().compare ("self") == 0; }
68 bool is_big_self_seg () const { return as_string ().compare ("Self") == 0; }
69 };
70
71 // A binding of an identifier to a type used in generic arguments in paths
72 struct GenericArgsBinding
73 {
74 private:
75 Identifier identifier;
76 std::unique_ptr<Type> type;
77 location_t locus;
78
79 public:
80 // Returns whether binding is in an error state.
81 bool is_error () const
82 {
83 return type == nullptr;
84 // and also identifier is empty, but cheaper computation
85 }
86
87 // Creates an error state generic args binding.
88 static GenericArgsBinding create_error ()
89 {
90 return GenericArgsBinding ({""}, nullptr);
91 }
92
93 // Pointer type for type in constructor to enable polymorphism
94 GenericArgsBinding (Identifier ident, std::unique_ptr<Type> type_ptr,
95 location_t locus = UNDEF_LOCATION)
96 : identifier (std::move (ident)), type (std::move (type_ptr)), locus (locus)
97 {}
98
99 // Copy constructor has to deep copy the type as it is a unique pointer
100 GenericArgsBinding (GenericArgsBinding const &other)
101 : identifier (other.identifier), locus (other.locus)
102 {
103 // guard to protect from null pointer dereference
104 if (other.type != nullptr)
105 type = other.type->clone_type ();
106 }
107
108 // default destructor
109 ~GenericArgsBinding () = default;
110
111 // Overload assignment operator to deep copy the pointed-to type
112 GenericArgsBinding &operator= (GenericArgsBinding const &other)
113 {
114 identifier = other.identifier;
115 locus = other.locus;
116
117 // guard to protect from null pointer dereference
118 if (other.type != nullptr)
119 type = other.type->clone_type ();
120 else
121 type = nullptr;
122
123 return *this;
124 }
125
126 // move constructors
127 GenericArgsBinding (GenericArgsBinding &&other) = default;
128 GenericArgsBinding &operator= (GenericArgsBinding &&other) = default;
129
130 std::string as_string () const;
131
132 // TODO: is this better? Or is a "vis_pattern" better?
133 Type &get_type ()
134 {
135 rust_assert (type != nullptr);
136 return *type;
137 }
138
139 std::unique_ptr<Type> &get_type_ptr ()
140 {
141 rust_assert (type != nullptr);
142 return type;
143 }
144
145 location_t get_locus () const { return locus; }
146
147 Identifier get_identifier () const { return identifier; }
148 };
149
150 /* Class representing a const generic application */
151 class GenericArg
152 {
153 public:
154 /**
155 * const generic arguments cannot always be differentiated with generic type
156 * arguments during parsing, e.g:
157 * ```rust
158 * let a: Foo<N>;
159 * ```
160 *
161 * Is N a type? A constant defined elsewhere? The parser cannot know, and must
162 * not draw any conclusions. We must wait until later passes of the compiler
163 * to decide whether this refers to a constant item or a type.
164 *
165 * On the other hand, simple expressions like literals or block expressions
166 * will always be constant expressions: There is no ambiguity at all.
167 */
168 enum class Kind
169 {
170 Error,
171 Const, // A const value
172 Type, // A type argument (not discernable during parsing)
173 Either, // Either a type or a const value, cleared up during resolving
174 };
175
176 static GenericArg create_error ()
177 {
178 return GenericArg (nullptr, nullptr, {""}, Kind::Error, UNDEF_LOCATION);
179 }
180
181 static GenericArg create_const (std::unique_ptr<Expr> expression)
182 {
183 auto locus = expression->get_locus ();
184 return GenericArg (std::move (expression), nullptr, {""}, Kind::Const,
185 locus);
186 }
187
188 static GenericArg create_type (std::unique_ptr<Type> type)
189 {
190 auto locus = type->get_locus ();
191 return GenericArg (nullptr, std::move (type), {""}, Kind::Type, locus);
192 }
193
194 static GenericArg create_ambiguous (Identifier path, location_t locus)
195 {
196 return GenericArg (nullptr, nullptr, std::move (path), Kind::Either, locus);
197 }
198
199 GenericArg (const GenericArg &other)
200 : path (other.path), kind (other.kind), locus (other.locus)
201 {
202 if (other.expression)
203 expression = other.expression->clone_expr ();
204 if (other.type)
205 type = other.type->clone_type ();
206 }
207
208 GenericArg operator= (const GenericArg &other)
209 {
210 kind = other.kind;
211 path = other.path;
212 locus = other.locus;
213
214 if (other.expression)
215 expression = other.expression->clone_expr ();
216 if (other.type)
217 type = other.type->clone_type ();
218
219 return *this;
220 }
221
222 GenericArg (GenericArg &&other) = default;
223 GenericArg &operator= (GenericArg &&other) = default;
224
225 bool is_error () const { return kind == Kind::Error; }
226
227 Kind get_kind () const { return kind; }
228 location_t get_locus () const { return locus; }
229
230 void accept_vis (AST::ASTVisitor &visitor)
231 {
232 switch (get_kind ())
233 {
234 case Kind::Const:
235 get_expression ().accept_vis (visitor);
236 break;
237 case Kind::Type:
238 get_type ().accept_vis (visitor);
239 break;
240 case Kind::Either:
241 break;
242 case Kind::Error:
243 rust_unreachable ();
244 }
245 }
246
247 Expr &get_expression ()
248 {
249 rust_assert (kind == Kind::Const);
250
251 return *expression;
252 }
253
254 std::unique_ptr<Expr> &get_expression_ptr ()
255 {
256 rust_assert (kind == Kind::Const);
257
258 return expression;
259 }
260
261 Type &get_type ()
262 {
263 rust_assert (kind == Kind::Type);
264
265 return *type;
266 }
267
268 std::unique_ptr<Type> &get_type_ptr ()
269 {
270 rust_assert (kind == Kind::Type);
271
272 return type;
273 }
274
275 const std::string get_path () const
276 {
277 rust_assert (kind == Kind::Either);
278
279 return path.as_string ();
280 }
281
282 std::string as_string () const
283 {
284 switch (get_kind ())
285 {
286 case Kind::Error:
287 rust_unreachable ();
288 case Kind::Either:
289 return "Ambiguous: " + path.as_string ();
290 case Kind::Const:
291 return "Const: { " + expression->as_string () + " }";
292 case Kind::Type:
293 return "Type: " + type->as_string ();
294 }
295
296 return "";
297 }
298
299 /**
300 * Disambiguate an ambiguous generic argument to a const generic argument,
301 * unequivocally
302 */
303 GenericArg disambiguate_to_const () const;
304
305 /**
306 * Disambiguate an ambiguous generic argument to a type argument,
307 * unequivocally
308 */
309 GenericArg disambiguate_to_type () const;
310
311 private:
312 GenericArg (std::unique_ptr<Expr> expression, std::unique_ptr<Type> type,
313 Identifier path, Kind kind, location_t locus)
314 : expression (std::move (expression)), type (std::move (type)),
315 path (std::move (path)), kind (kind), locus (locus)
316 {}
317
318 /**
319 * Expression associated with a `Clear` const generic application
320 * A null pointer here is allowed in the case that the const argument is
321 * ambiguous.
322 */
323 std::unique_ptr<Expr> expression;
324
325 /**
326 * If the argument ends up being a type argument instead. A null pointer will
327 * be present here until the resolving phase.
328 */
329 std::unique_ptr<Type> type;
330
331 /**
332 * Optional path which cannot be differentiated between a constant item and
333 * a type. Only used for ambiguous const generic arguments, otherwise
334 * empty.
335 */
336 Identifier path;
337
338 /* Which kind of const generic application are we dealing with */
339 Kind kind;
340
341 location_t locus;
342 };
343
344 /**
345 * Representation of const generic parameters
346 */
347 class ConstGenericParam : public GenericParam
348 {
349 /* Name of the parameter */
350 Identifier name;
351
352 /* Mandatory type of the const parameter - a null pointer is an error */
353 std::unique_ptr<AST::Type> type;
354
355 /**
356 * Default value for the const generic parameter
357 */
358 GenericArg default_value;
359
360 AST::AttrVec outer_attrs;
361 location_t locus;
362
363 public:
364 ConstGenericParam (Identifier name, std::unique_ptr<AST::Type> type,
365 GenericArg default_value, AST::AttrVec outer_attrs,
366 location_t locus)
367 : name (name), type (std::move (type)),
368 default_value (std::move (default_value)), outer_attrs (outer_attrs),
369 locus (locus)
370 {}
371
372 ConstGenericParam (const ConstGenericParam &other)
373 : GenericParam (), name (other.name), type (other.type->clone_type ()),
374 default_value (other.default_value), outer_attrs (other.outer_attrs),
375 locus (other.locus)
376 {}
377
378 bool has_type () const { return type != nullptr; }
379 bool has_default_value () const { return !default_value.is_error (); }
380
381 const Identifier &get_name () const { return name; }
382
383 AST::AttrVec &get_outer_attrs () { return outer_attrs; }
384
385 AST::Type &get_type ()
386 {
387 rust_assert (has_type ());
388
389 return *type;
390 }
391
392 GenericArg &get_default_value ()
393 {
394 rust_assert (has_default_value ());
395
396 return default_value;
397 }
398
399 const GenericArg &get_default_value () const
400 {
401 rust_assert (has_default_value ());
402
403 return default_value;
404 }
405
406 std::string as_string () const override;
407
408 void accept_vis (ASTVisitor &vis) override;
409
410 location_t get_locus () const override final { return locus; }
411
412 Kind get_kind () const override final { return Kind::Const; }
413
414 protected:
415 /* Use covariance to implement clone function as returning this object rather
416 * than base */
417 ConstGenericParam *clone_generic_param_impl () const override
418 {
419 return new ConstGenericParam (*this);
420 }
421 };
422
423 // Generic arguments allowed in each path expression segment - inline?
424 struct GenericArgs
425 {
426 std::vector<Lifetime> lifetime_args;
427 std::vector<GenericArg> generic_args;
428 std::vector<GenericArgsBinding> binding_args;
429 location_t locus;
430
431 public:
432 // Returns true if there are any generic arguments
433 bool has_generic_args () const
434 {
435 return !(lifetime_args.empty () && generic_args.empty ()
436 && binding_args.empty ());
437 }
438
439 GenericArgs (std::vector<Lifetime> lifetime_args,
440 std::vector<GenericArg> generic_args,
441 std::vector<GenericArgsBinding> binding_args,
442 location_t locus = UNDEF_LOCATION)
443 : lifetime_args (std::move (lifetime_args)),
444 generic_args (std::move (generic_args)),
445 binding_args (std::move (binding_args)), locus (locus)
446 {}
447
448 // copy constructor with vector clone
449 GenericArgs (GenericArgs const &other)
450 : lifetime_args (other.lifetime_args), binding_args (other.binding_args),
451 locus (other.locus)
452 {
453 generic_args.clear ();
454 generic_args.reserve (other.generic_args.size ());
455 for (const auto &arg : other.generic_args)
456 {
457 generic_args.push_back (GenericArg (arg));
458 }
459 }
460
461 ~GenericArgs () = default;
462
463 // overloaded assignment operator to vector clone
464 GenericArgs &operator= (GenericArgs const &other)
465 {
466 lifetime_args = other.lifetime_args;
467 binding_args = other.binding_args;
468 locus = other.locus;
469
470 generic_args.clear ();
471 generic_args.reserve (other.generic_args.size ());
472 for (const auto &arg : other.generic_args)
473 {
474 generic_args.push_back (GenericArg (arg));
475 }
476
477 return *this;
478 }
479
480 // move constructors
481 GenericArgs (GenericArgs &&other) = default;
482 GenericArgs &operator= (GenericArgs &&other) = default;
483
484 // Creates an empty GenericArgs (no arguments)
485 static GenericArgs create_empty () { return GenericArgs ({}, {}, {}); }
486
487 std::string as_string () const;
488
489 std::vector<GenericArg> &get_generic_args () { return generic_args; }
490
491 std::vector<GenericArgsBinding> &get_binding_args () { return binding_args; }
492
493 const std::vector<GenericArgsBinding> &get_binding_args () const
494 {
495 return binding_args;
496 }
497
498 std::vector<Lifetime> &get_lifetime_args () { return lifetime_args; };
499
500 const std::vector<Lifetime> &get_lifetime_args () const
501 {
502 return lifetime_args;
503 };
504
505 location_t get_locus () const { return locus; }
506 };
507
508 /* A segment of a path in expression, including an identifier aspect and maybe
509 * generic args */
510 class PathExprSegment
511 { // or should this extend PathIdentSegment?
512 private:
513 PathIdentSegment segment_name;
514 GenericArgs generic_args;
515 location_t locus;
516 NodeId node_id;
517
518 public:
519 // Returns true if there are any generic arguments
520 bool has_generic_args () const { return generic_args.has_generic_args (); }
521
522 // Constructor for segment (from IdentSegment and GenericArgs)
523 PathExprSegment (PathIdentSegment segment_name, location_t locus,
524 GenericArgs generic_args = GenericArgs::create_empty ())
525 : segment_name (std::move (segment_name)),
526 generic_args (std::move (generic_args)), locus (locus),
527 node_id (Analysis::Mappings::get ().get_next_node_id ())
528 {}
529
530 /* Constructor for segment with generic arguments (from segment name and all
531 * args) */
532 PathExprSegment (std::string segment_name, location_t locus,
533 std::vector<Lifetime> lifetime_args = {},
534 std::vector<GenericArg> generic_args = {},
535 std::vector<GenericArgsBinding> binding_args = {})
536 : segment_name (PathIdentSegment (std::move (segment_name), locus)),
537 generic_args (GenericArgs (std::move (lifetime_args),
538 std::move (generic_args),
539 std::move (binding_args))),
540 locus (locus), node_id (Analysis::Mappings::get ().get_next_node_id ())
541 {}
542
543 // Returns whether path expression segment is in an error state.
544 bool is_error () const { return segment_name.is_error (); }
545
546 // Creates an error-state path expression segment.
547 static PathExprSegment create_error ()
548 {
549 return PathExprSegment (PathIdentSegment::create_error (), UNDEF_LOCATION);
550 }
551
552 std::string as_string () const;
553
554 location_t get_locus () const { return locus; }
555
556 // TODO: is this better? Or is a "vis_pattern" better?
557 GenericArgs &get_generic_args ()
558 {
559 rust_assert (has_generic_args ());
560 return generic_args;
561 }
562
563 PathIdentSegment &get_ident_segment () { return segment_name; }
564 const PathIdentSegment &get_ident_segment () const { return segment_name; }
565
566 NodeId get_node_id () const { return node_id; }
567
568 bool is_super_path_seg () const
569 {
570 return !has_generic_args () && get_ident_segment ().is_super_path_seg ();
571 }
572
573 bool is_crate_path_seg () const
574 {
575 return !has_generic_args () && get_ident_segment ().is_crate_path_seg ();
576 }
577
578 bool is_lower_self_seg () const
579 {
580 return !has_generic_args () && get_ident_segment ().is_lower_self_seg ();
581 }
582 };
583
584 // AST node representing a pattern that involves a "path" - abstract base
585 // class
586 class Path : public Pattern
587 {
588 public:
589 enum class Kind
590 {
591 LangItem,
592 Regular,
593 };
594
595 Path (std::vector<PathExprSegment> segments)
596 : segments (std::move (segments)), lang_item (tl::nullopt),
597 kind (Kind::Regular)
598 {}
599
600 Path (LangItem::Kind lang_item)
601 : segments ({}), lang_item (lang_item), kind (Kind::LangItem)
602 {}
603
604 // Returns whether path has segments.
605 bool has_segments () const
606 {
607 rust_assert (kind == Kind::Regular);
608 return !segments.empty ();
609 }
610
611 /* Converts path segments to their equivalent SimplePath segments if
612 * possible, and creates a SimplePath from them. */
613 SimplePath convert_to_simple_path (bool with_opening_scope_resolution) const;
614
615 /* Returns whether the path is a single segment (excluding qualified path
616 * initial as segment). */
617 bool is_single_segment () const
618 {
619 rust_assert (kind == Kind::Regular);
620 return segments.size () == 1;
621 }
622
623 std::string as_string () const override;
624
625 bool is_lang_item () const { return kind == Kind::LangItem; }
626
627 // TODO: this seems kinda dodgy
628 std::vector<PathExprSegment> &get_segments ()
629 {
630 rust_assert (kind == Kind::Regular);
631 return segments;
632 }
633 const std::vector<PathExprSegment> &get_segments () const
634 {
635 rust_assert (kind == Kind::Regular);
636 return segments;
637 }
638
639 LangItem::Kind get_lang_item () const
640 {
641 rust_assert (kind == Kind::LangItem);
642 return *lang_item;
643 }
644
645 Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Path; }
646 Path::Kind get_path_kind () { return kind; }
647
648 protected:
649 std::vector<PathExprSegment> segments;
650 tl::optional<LangItem::Kind> lang_item;
651
652 Path::Kind kind;
653 };
654
655 /* AST node representing a path-in-expression pattern (path that allows
656 * generic arguments) */
657 class PathInExpression : public Path, public ExprWithoutBlock
658 {
659 std::vector<Attribute> outer_attrs;
660 bool has_opening_scope_resolution;
661 location_t locus;
662 NodeId _node_id;
663
664 bool marked_for_strip;
665
666 public:
667 std::string as_string () const override;
668
669 // Constructor
670 PathInExpression (std::vector<PathExprSegment> path_segments,
671 std::vector<Attribute> outer_attrs, location_t locus,
672 bool has_opening_scope_resolution = false)
673 : Path (std::move (path_segments)), outer_attrs (std::move (outer_attrs)),
674 has_opening_scope_resolution (has_opening_scope_resolution),
675 locus (locus), _node_id (Analysis::Mappings::get ().get_next_node_id ()),
676 marked_for_strip (false)
677 {}
678
679 PathInExpression (LangItem::Kind lang_item,
680 std::vector<Attribute> outer_attrs, location_t locus)
681 : Path (lang_item), outer_attrs (std::move (outer_attrs)),
682 has_opening_scope_resolution (false), locus (locus),
683 _node_id (Analysis::Mappings::get ().get_next_node_id ()),
684 marked_for_strip (false)
685 {}
686
687 // Creates an error state path in expression.
688 static PathInExpression create_error ()
689 {
690 return PathInExpression (std::vector<PathExprSegment> (), {},
691 UNDEF_LOCATION);
692 }
693
694 // Returns whether path in expression is in an error state.
695 bool is_error () const { return !has_segments (); }
696
697 /* Converts PathInExpression to SimplePath if possible (i.e. no generic
698 * arguments). Otherwise returns an empty SimplePath. */
699 SimplePath as_simple_path () const
700 {
701 /* delegate to parent class as can't access segments. however,
702 * QualifiedPathInExpression conversion to simple path wouldn't make
703 * sense, so the method in the parent class should be protected, not
704 * public. Have to pass in opening scope resolution as parent class has no
705 * access to it.
706 */
707 return convert_to_simple_path (has_opening_scope_resolution);
708 }
709
710 location_t get_locus () const override final { return locus; }
711
712 void accept_vis (ASTVisitor &vis) override;
713
714 void mark_for_strip () override { marked_for_strip = true; }
715 bool is_marked_for_strip () const override { return marked_for_strip; }
716
717 bool opening_scope_resolution () const
718 {
719 return has_opening_scope_resolution;
720 }
721
722 NodeId get_node_id () const override { return _node_id; }
723
724 const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
725 std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; }
726
727 void set_outer_attrs (std::vector<Attribute> new_attrs) override
728 {
729 outer_attrs = std::move (new_attrs);
730 }
731
732 PathExprSegment &get_final_segment () { return get_segments ().back (); }
733 const PathExprSegment &get_final_segment () const
734 {
735 return get_segments ().back ();
736 }
737
738 Expr::Kind get_expr_kind () const override
739 {
740 return Expr::Kind::PathInExpression;
741 }
742
743 protected:
744 /* Use covariance to implement clone function as returning this object
745 * rather than base */
746 PathInExpression *clone_pattern_impl () const final override
747 {
748 return clone_path_in_expression_impl ();
749 }
750
751 /* Use covariance to implement clone function as returning this object
752 * rather than base */
753 PathInExpression *clone_expr_without_block_impl () const final override
754 {
755 return clone_path_in_expression_impl ();
756 }
757
758 /*virtual*/ PathInExpression *clone_path_in_expression_impl () const
759 {
760 return new PathInExpression (*this);
761 }
762 };
763
764 /* Base class for segments used in type paths - not abstract (represents an
765 * ident-only segment) */
766 class TypePathSegment
767 {
768 public:
769 enum SegmentType
770 {
771 REG,
772 GENERIC,
773 FUNCTION
774 };
775
776 private:
777 tl::optional<LangItem::Kind> lang_item;
778 tl::optional<PathIdentSegment> ident_segment;
779 location_t locus;
780
781 protected:
782 /* This is protected because it is only really used by derived classes, not
783 * the base. */
784 bool has_separating_scope_resolution;
785 NodeId node_id;
786
787 public:
788 // Clone function implementation - not pure virtual as overrided by
789 // subclasses
790 virtual TypePathSegment *clone_type_path_segment_impl () const
791 {
792 return new TypePathSegment (*this);
793 }
794
795 public:
796 virtual ~TypePathSegment () {}
797
798 virtual SegmentType get_type () const { return SegmentType::REG; }
799
800 // Unique pointer custom clone function
801 std::unique_ptr<TypePathSegment> clone_type_path_segment () const
802 {
803 return std::unique_ptr<TypePathSegment> (clone_type_path_segment_impl ());
804 }
805
806 TypePathSegment (PathIdentSegment ident_segment,
807 bool has_separating_scope_resolution, location_t locus)
808 : lang_item (tl::nullopt), ident_segment (std::move (ident_segment)),
809 locus (locus),
810 has_separating_scope_resolution (has_separating_scope_resolution),
811 node_id (Analysis::Mappings::get ().get_next_node_id ())
812 {}
813
814 TypePathSegment (LangItem::Kind lang_item, location_t locus)
815 : lang_item (lang_item), ident_segment (tl::nullopt), locus (locus),
816 has_separating_scope_resolution (false),
817 node_id (Analysis::Mappings::get ().get_next_node_id ())
818 {}
819
820 TypePathSegment (std::string segment_name,
821 bool has_separating_scope_resolution, location_t locus)
822 : lang_item (tl::nullopt),
823 ident_segment (PathIdentSegment (std::move (segment_name), locus)),
824 locus (locus),
825 has_separating_scope_resolution (has_separating_scope_resolution),
826 node_id (Analysis::Mappings::get ().get_next_node_id ())
827 {}
828
829 TypePathSegment (TypePathSegment const &other)
830 : lang_item (other.lang_item), ident_segment (other.ident_segment),
831 locus (other.locus),
832 has_separating_scope_resolution (other.has_separating_scope_resolution),
833 node_id (other.node_id)
834 {}
835
836 TypePathSegment &operator= (TypePathSegment const &other)
837 {
838 ident_segment = other.ident_segment;
839 lang_item = other.lang_item;
840 locus = other.locus;
841 has_separating_scope_resolution = other.has_separating_scope_resolution;
842 node_id = other.node_id;
843
844 return *this;
845 }
846
847 TypePathSegment (TypePathSegment &&other) = default;
848 TypePathSegment &operator= (TypePathSegment &&other) = default;
849
850 virtual std::string as_string () const
851 {
852 if (lang_item.has_value ())
853 return LangItem::PrettyString (*lang_item);
854
855 return ident_segment->as_string ();
856 }
857
858 /* Returns whether the type path segment is in an error state. May be
859 * virtual in future. */
860 bool is_error () const
861 {
862 rust_assert (ident_segment);
863 return ident_segment->is_error ();
864 }
865
866 /* Returns whether segment is identifier only (as opposed to generic args or
867 * function). Overridden in derived classes with other segments. */
868 virtual bool is_ident_only () const { return true; }
869
870 bool is_lang_item () const { return lang_item.has_value (); }
871
872 location_t get_locus () const { return locus; }
873
874 // not pure virtual as class not abstract
875 virtual void accept_vis (ASTVisitor &vis);
876
877 bool get_separating_scope_resolution () const
878 {
879 return has_separating_scope_resolution;
880 }
881
882 PathIdentSegment &get_ident_segment ()
883 {
884 rust_assert (!is_lang_item ());
885 return *ident_segment;
886 };
887
888 const PathIdentSegment &get_ident_segment () const
889 {
890 rust_assert (!is_lang_item ());
891 return *ident_segment;
892 };
893
894 LangItem::Kind get_lang_item () const
895 {
896 rust_assert (is_lang_item ());
897 return *lang_item;
898 }
899
900 NodeId get_node_id () const { return node_id; }
901
902 bool is_crate_path_seg () const
903 {
904 return get_ident_segment ().is_crate_path_seg ();
905 }
906 bool is_super_path_seg () const
907 {
908 return get_ident_segment ().is_super_path_seg ();
909 }
910 bool is_big_self_seg () const
911 {
912 return get_ident_segment ().is_big_self_seg ();
913 }
914 bool is_lower_self_seg () const
915 {
916 return get_ident_segment ().is_lower_self_seg ();
917 }
918 };
919
920 // Segment used in type path with generic args
921 class TypePathSegmentGeneric : public TypePathSegment
922 {
923 GenericArgs generic_args;
924
925 public:
926 SegmentType get_type () const override { return SegmentType::GENERIC; }
927
928 bool has_generic_args () const { return generic_args.has_generic_args (); }
929
930 bool is_ident_only () const override { return false; }
931
932 // Constructor with PathIdentSegment and GenericArgs
933 TypePathSegmentGeneric (PathIdentSegment ident_segment,
934 bool has_separating_scope_resolution,
935 GenericArgs generic_args, location_t locus)
936 : TypePathSegment (std::move (ident_segment),
937 has_separating_scope_resolution, locus),
938 generic_args (std::move (generic_args))
939 {}
940
941 TypePathSegmentGeneric (LangItem::Kind lang_item, GenericArgs generic_args,
942 location_t locus)
943 : TypePathSegment (lang_item, locus),
944 generic_args (std::move (generic_args))
945 {}
946
947 // Constructor from segment name and all args
948 TypePathSegmentGeneric (std::string segment_name,
949 bool has_separating_scope_resolution,
950 std::vector<Lifetime> lifetime_args,
951 std::vector<GenericArg> generic_args,
952 std::vector<GenericArgsBinding> binding_args,
953 location_t locus)
954 : TypePathSegment (std::move (segment_name),
955 has_separating_scope_resolution, locus),
956 generic_args (GenericArgs (std::move (lifetime_args),
957 std::move (generic_args),
958 std::move (binding_args)))
959 {}
960
961 // Copy constructor with vector clone
962 TypePathSegmentGeneric (TypePathSegmentGeneric const &other)
963 : TypePathSegment (other), generic_args (other.generic_args)
964 {}
965
966 // Overloaded assignment operator with vector clone
967 TypePathSegmentGeneric &operator= (TypePathSegmentGeneric const &other)
968 {
969 generic_args = other.generic_args;
970
971 return *this;
972 }
973
974 // move constructors
975 TypePathSegmentGeneric (TypePathSegmentGeneric &&other) = default;
976 TypePathSegmentGeneric &operator= (TypePathSegmentGeneric &&other) = default;
977
978 std::string as_string () const override;
979
980 void accept_vis (ASTVisitor &vis) override;
981
982 // TODO: is this better? Or is a "vis_pattern" better?
983 GenericArgs &get_generic_args ()
984 {
985 rust_assert (has_generic_args ());
986 return generic_args;
987 }
988
989 // Use covariance to override base class method
990 TypePathSegmentGeneric *clone_type_path_segment_impl () const override
991 {
992 return new TypePathSegmentGeneric (*this);
993 }
994 };
995
996 // A function as represented in a type path
997 struct TypePathFunction
998 {
999 private:
1000 // TODO: remove
1001 /*bool has_inputs;
1002 TypePathFnInputs inputs;*/
1003 // inlined from TypePathFnInputs
1004 std::vector<std::unique_ptr<Type>> inputs;
1005
1006 // bool has_type;
1007 std::unique_ptr<Type> return_type;
1008
1009 // FIXME: think of better way to mark as invalid than taking up storage
1010 bool is_invalid;
1011
1012 location_t locus;
1013
1014 protected:
1015 // Constructor only used to create invalid type path functions.
1016 TypePathFunction (bool is_invalid, location_t locus)
1017 : is_invalid (is_invalid), locus (locus)
1018 {}
1019
1020 public:
1021 // Returns whether the return type of the function has been specified.
1022 bool has_return_type () const { return return_type != nullptr; }
1023
1024 // Returns whether the function has inputs.
1025 bool has_inputs () const { return !inputs.empty (); }
1026
1027 // Returns whether function is in an error state.
1028 bool is_error () const { return is_invalid; }
1029
1030 // Creates an error state function.
1031 static TypePathFunction create_error ()
1032 {
1033 return TypePathFunction (true, UNDEF_LOCATION);
1034 }
1035
1036 // Constructor
1037 TypePathFunction (std::vector<std::unique_ptr<Type>> inputs, location_t locus,
1038 std::unique_ptr<Type> type = nullptr)
1039 : inputs (std::move (inputs)), return_type (std::move (type)),
1040 is_invalid (false), locus (locus)
1041 {}
1042
1043 // Copy constructor with clone
1044 TypePathFunction (TypePathFunction const &other)
1045 : is_invalid (other.is_invalid)
1046 {
1047 // guard to protect from null pointer dereference
1048 if (other.return_type != nullptr)
1049 return_type = other.return_type->clone_type ();
1050
1051 inputs.reserve (other.inputs.size ());
1052 for (const auto &e : other.inputs)
1053 inputs.push_back (e->clone_type ());
1054 }
1055
1056 ~TypePathFunction () = default;
1057
1058 // Overloaded assignment operator to clone type
1059 TypePathFunction &operator= (TypePathFunction const &other)
1060 {
1061 is_invalid = other.is_invalid;
1062
1063 // guard to protect from null pointer dereference
1064 if (other.return_type != nullptr)
1065 return_type = other.return_type->clone_type ();
1066 else
1067 return_type = nullptr;
1068
1069 inputs.reserve (other.inputs.size ());
1070 for (const auto &e : other.inputs)
1071 inputs.push_back (e->clone_type ());
1072
1073 return *this;
1074 }
1075
1076 // move constructors
1077 TypePathFunction (TypePathFunction &&other) = default;
1078 TypePathFunction &operator= (TypePathFunction &&other) = default;
1079
1080 std::string as_string () const;
1081
1082 // TODO: this mutable getter seems really dodgy. Think up better way.
1083 const std::vector<std::unique_ptr<Type>> &get_params () const
1084 {
1085 return inputs;
1086 }
1087 std::vector<std::unique_ptr<Type>> &get_params () { return inputs; }
1088
1089 // TODO: is this better? Or is a "vis_pattern" better?
1090 Type &get_return_type ()
1091 {
1092 rust_assert (has_return_type ());
1093 return *return_type;
1094 }
1095
1096 std::unique_ptr<Type> &get_return_type_ptr ()
1097 {
1098 rust_assert (has_return_type ());
1099 return return_type;
1100 }
1101
1102 location_t get_locus () const { return locus; }
1103 };
1104
1105 // Segment used in type path with a function argument
1106 class TypePathSegmentFunction : public TypePathSegment
1107 {
1108 TypePathFunction function_path;
1109
1110 public:
1111 SegmentType get_type () const override { return SegmentType::FUNCTION; }
1112
1113 // Constructor with PathIdentSegment and TypePathFn
1114 TypePathSegmentFunction (PathIdentSegment ident_segment,
1115 bool has_separating_scope_resolution,
1116 TypePathFunction function_path, location_t locus)
1117 : TypePathSegment (std::move (ident_segment),
1118 has_separating_scope_resolution, locus),
1119 function_path (std::move (function_path))
1120 {}
1121
1122 // Constructor with segment name and TypePathFn
1123 TypePathSegmentFunction (std::string segment_name,
1124 bool has_separating_scope_resolution,
1125 TypePathFunction function_path, location_t locus)
1126 : TypePathSegment (std::move (segment_name),
1127 has_separating_scope_resolution, locus),
1128 function_path (std::move (function_path))
1129 {}
1130
1131 std::string as_string () const override;
1132
1133 bool is_ident_only () const override { return false; }
1134
1135 void accept_vis (ASTVisitor &vis) override;
1136
1137 // TODO: is this better? Or is a "vis_pattern" better?
1138 TypePathFunction &get_type_path_function ()
1139 {
1140 rust_assert (!function_path.is_error ());
1141 return function_path;
1142 }
1143
1144 // Use covariance to override base class method
1145 TypePathSegmentFunction *clone_type_path_segment_impl () const override
1146 {
1147 return new TypePathSegmentFunction (*this);
1148 }
1149 };
1150
1151 class TypePath : public TypeNoBounds
1152 {
1153 bool has_opening_scope_resolution;
1154 std::vector<std::unique_ptr<TypePathSegment>> segments;
1155 location_t locus;
1156
1157 protected:
1158 /* Use covariance to implement clone function as returning this object
1159 * rather than base */
1160 TypePath *clone_type_no_bounds_impl () const override
1161 {
1162 return new TypePath (*this);
1163 }
1164
1165 public:
1166 /* Returns whether the TypePath has an opening scope resolution operator
1167 * (i.e. is global path or crate-relative path, not module-relative) */
1168 bool has_opening_scope_resolution_op () const
1169 {
1170 return has_opening_scope_resolution;
1171 }
1172
1173 // Returns whether the TypePath is in an invalid state.
1174 bool is_error () const { return segments.empty (); }
1175
1176 // Creates an error state TypePath.
1177 static TypePath create_error ()
1178 {
1179 return TypePath (std::vector<std::unique_ptr<TypePathSegment>> (),
1180 UNDEF_LOCATION);
1181 }
1182
1183 // Constructor
1184 TypePath (std::vector<std::unique_ptr<TypePathSegment>> segments,
1185 location_t locus, bool has_opening_scope_resolution = false)
1186 : TypeNoBounds (),
1187 has_opening_scope_resolution (has_opening_scope_resolution),
1188 segments (std::move (segments)), locus (locus)
1189 {}
1190
1191 TypePath (LangItem::Kind lang_item,
1192 std::vector<std::unique_ptr<TypePathSegment>> segments,
1193 location_t locus, bool has_opening_scope_resolution = false)
1194 : TypeNoBounds (),
1195 has_opening_scope_resolution (has_opening_scope_resolution),
1196 segments (std::move (segments)), locus (locus)
1197 {}
1198
1199 // Copy constructor with vector clone
1200 TypePath (TypePath const &other)
1201 : has_opening_scope_resolution (other.has_opening_scope_resolution),
1202 locus (other.locus)
1203 {
1204 node_id = other.node_id;
1205 segments.reserve (other.segments.size ());
1206 for (const auto &e : other.segments)
1207 segments.push_back (e->clone_type_path_segment ());
1208 }
1209
1210 // Overloaded assignment operator with clone
1211 TypePath &operator= (TypePath const &other)
1212 {
1213 node_id = other.node_id;
1214 has_opening_scope_resolution = other.has_opening_scope_resolution;
1215 locus = other.locus;
1216
1217 segments.reserve (other.segments.size ());
1218 for (const auto &e : other.segments)
1219 segments.push_back (e->clone_type_path_segment ());
1220
1221 return *this;
1222 }
1223
1224 // move constructors
1225 TypePath (TypePath &&other) = default;
1226 TypePath &operator= (TypePath &&other) = default;
1227
1228 std::string as_string () const override;
1229
1230 /* Converts TypePath to SimplePath if possible (i.e. no generic or function
1231 * arguments). Otherwise returns an empty SimplePath. */
1232 SimplePath as_simple_path () const;
1233
1234 // Creates a trait bound with a clone of this type path as its only element.
1235 TraitBound *to_trait_bound (bool in_parens) const override;
1236
1237 location_t get_locus () const override final { return locus; }
1238 NodeId get_node_id () const { return node_id; }
1239
1240 void mark_for_strip () override {}
1241 bool is_marked_for_strip () const override { return false; }
1242
1243 void accept_vis (ASTVisitor &vis) override;
1244
1245 // TODO: this seems kinda dodgy
1246 std::vector<std::unique_ptr<TypePathSegment>> &get_segments ()
1247 {
1248 return segments;
1249 }
1250 const std::vector<std::unique_ptr<TypePathSegment>> &get_segments () const
1251 {
1252 return segments;
1253 }
1254
1255 size_t get_num_segments () const { return segments.size (); }
1256 };
1257
1258 struct QualifiedPathType
1259 {
1260 private:
1261 std::unique_ptr<Type> type_to_invoke_on;
1262 TypePath trait_path;
1263 location_t locus;
1264 NodeId node_id;
1265
1266 public:
1267 // Constructor
1268 QualifiedPathType (std::unique_ptr<Type> invoke_on_type,
1269 location_t locus = UNDEF_LOCATION,
1270 TypePath trait_path = TypePath::create_error ())
1271 : type_to_invoke_on (std::move (invoke_on_type)), trait_path (trait_path),
1272 locus (locus), node_id (Analysis::Mappings::get ().get_next_node_id ())
1273 {}
1274
1275 // Copy constructor uses custom deep copy for Type to preserve polymorphism
1276 QualifiedPathType (QualifiedPathType const &other)
1277 : trait_path (other.trait_path), locus (other.locus)
1278 {
1279 node_id = other.node_id;
1280 // guard to prevent null dereference
1281 if (other.type_to_invoke_on != nullptr)
1282 type_to_invoke_on = other.type_to_invoke_on->clone_type ();
1283 }
1284
1285 // default destructor
1286 ~QualifiedPathType () = default;
1287
1288 // overload assignment operator to use custom clone method
1289 QualifiedPathType &operator= (QualifiedPathType const &other)
1290 {
1291 node_id = other.node_id;
1292 trait_path = other.trait_path;
1293 locus = other.locus;
1294
1295 // guard to prevent null dereference
1296 if (other.type_to_invoke_on != nullptr)
1297 type_to_invoke_on = other.type_to_invoke_on->clone_type ();
1298 else
1299 type_to_invoke_on = nullptr;
1300
1301 return *this;
1302 }
1303
1304 // move constructor
1305 QualifiedPathType (QualifiedPathType &&other) = default;
1306 QualifiedPathType &operator= (QualifiedPathType &&other) = default;
1307
1308 // Returns whether the qualified path type has a rebind as clause.
1309 bool has_as_clause () const { return !trait_path.is_error (); }
1310
1311 // Returns whether the qualified path type is in an error state.
1312 bool is_error () const { return type_to_invoke_on == nullptr; }
1313
1314 // Creates an error state qualified path type.
1315 static QualifiedPathType create_error ()
1316 {
1317 return QualifiedPathType (nullptr);
1318 }
1319
1320 std::string as_string () const;
1321
1322 location_t get_locus () const { return locus; }
1323
1324 // TODO: is this better? Or is a "vis_pattern" better?
1325 Type &get_type ()
1326 {
1327 rust_assert (type_to_invoke_on != nullptr);
1328 return *type_to_invoke_on;
1329 }
1330
1331 std::unique_ptr<Type> &get_type_ptr ()
1332 {
1333 rust_assert (type_to_invoke_on != nullptr);
1334 return type_to_invoke_on;
1335 }
1336
1337 // TODO: is this better? Or is a "vis_pattern" better?
1338 TypePath &get_as_type_path ()
1339 {
1340 rust_assert (has_as_clause ());
1341 return trait_path;
1342 }
1343
1344 NodeId get_node_id () const { return node_id; }
1345 };
1346
1347 /* AST node representing a qualified path-in-expression pattern (path that
1348 * allows specifying trait functions) */
1349 class QualifiedPathInExpression : public Path, public ExprWithoutBlock
1350 {
1351 std::vector<Attribute> outer_attrs;
1352 QualifiedPathType path_type;
1353 location_t locus;
1354 NodeId _node_id;
1355
1356 public:
1357 std::string as_string () const override;
1358
1359 QualifiedPathInExpression (QualifiedPathType qual_path_type,
1360 std::vector<PathExprSegment> path_segments,
1361 std::vector<Attribute> outer_attrs,
1362 location_t locus)
1363 : Path (std::move (path_segments)), outer_attrs (std::move (outer_attrs)),
1364 path_type (std::move (qual_path_type)), locus (locus),
1365 _node_id (Analysis::Mappings::get ().get_next_node_id ())
1366 {}
1367
1368 /* TODO: maybe make a shortcut constructor that has QualifiedPathType
1369 * elements as params */
1370
1371 // Returns whether qualified path in expression is in an error state.
1372 bool is_error () const { return path_type.is_error (); }
1373
1374 // Creates an error qualified path in expression.
1375 static QualifiedPathInExpression create_error ()
1376 {
1377 return QualifiedPathInExpression (QualifiedPathType::create_error (), {},
1378 {}, UNDEF_LOCATION);
1379 }
1380
1381 location_t get_locus () const override final { return locus; }
1382
1383 void accept_vis (ASTVisitor &vis) override;
1384
1385 // Invalid if path_type is error, so base stripping on that.
1386 void mark_for_strip () override
1387 {
1388 path_type = QualifiedPathType::create_error ();
1389 }
1390 bool is_marked_for_strip () const override { return is_error (); }
1391
1392 // TODO: is this better? Or is a "vis_pattern" better?
1393 QualifiedPathType &get_qualified_path_type ()
1394 {
1395 rust_assert (!path_type.is_error ());
1396 return path_type;
1397 }
1398
1399 const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
1400 std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; }
1401
1402 void set_outer_attrs (std::vector<Attribute> new_attrs) override
1403 {
1404 outer_attrs = std::move (new_attrs);
1405 }
1406
1407 NodeId get_node_id () const override { return _node_id; }
1408
1409 Expr::Kind get_expr_kind () const override
1410 {
1411 return Expr::Kind::QualifiedPathInExpression;
1412 }
1413
1414 protected:
1415 /* Use covariance to implement clone function as returning this object
1416 * rather than base */
1417 QualifiedPathInExpression *clone_pattern_impl () const final override
1418 {
1419 return clone_qual_path_in_expression_impl ();
1420 }
1421
1422 /* Use covariance to implement clone function as returning this object
1423 * rather than base */
1424 QualifiedPathInExpression *
1425 clone_expr_without_block_impl () const final override
1426 {
1427 return clone_qual_path_in_expression_impl ();
1428 }
1429
1430 /*virtual*/ QualifiedPathInExpression *
1431 clone_qual_path_in_expression_impl () const
1432 {
1433 return new QualifiedPathInExpression (*this);
1434 }
1435 };
1436
1437 /* Represents a qualified path in a type; used for disambiguating trait
1438 * function calls */
1439 class QualifiedPathInType : public TypeNoBounds
1440 {
1441 QualifiedPathType path_type;
1442 std::unique_ptr<TypePathSegment> associated_segment;
1443 std::vector<std::unique_ptr<TypePathSegment>> segments;
1444 location_t locus;
1445
1446 protected:
1447 /* Use covariance to implement clone function as returning this object
1448 * rather than base */
1449 QualifiedPathInType *clone_type_no_bounds_impl () const override
1450 {
1451 return new QualifiedPathInType (*this);
1452 }
1453
1454 public:
1455 QualifiedPathInType (
1456 QualifiedPathType qual_path_type,
1457 std::unique_ptr<TypePathSegment> associated_segment,
1458 std::vector<std::unique_ptr<TypePathSegment>> path_segments,
1459 location_t locus)
1460 : path_type (std::move (qual_path_type)),
1461 associated_segment (std::move (associated_segment)),
1462 segments (std::move (path_segments)), locus (locus)
1463 {}
1464
1465 // Copy constructor with vector clone
1466 QualifiedPathInType (QualifiedPathInType const &other)
1467 : path_type (other.path_type), locus (other.locus)
1468 {
1469 auto seg = other.associated_segment->clone_type_path_segment_impl ();
1470 associated_segment = std::unique_ptr<TypePathSegment> (seg);
1471
1472 segments.reserve (other.segments.size ());
1473 for (const auto &e : other.segments)
1474 segments.push_back (e->clone_type_path_segment ());
1475 }
1476
1477 // Overloaded assignment operator with vector clone
1478 QualifiedPathInType &operator= (QualifiedPathInType const &other)
1479 {
1480 auto seg = other.associated_segment->clone_type_path_segment_impl ();
1481 associated_segment = std::unique_ptr<TypePathSegment> (seg);
1482
1483 path_type = other.path_type;
1484 locus = other.locus;
1485
1486 segments.reserve (other.segments.size ());
1487 for (const auto &e : other.segments)
1488 segments.push_back (e->clone_type_path_segment ());
1489
1490 return *this;
1491 }
1492
1493 // move constructors
1494 QualifiedPathInType (QualifiedPathInType &&other) = default;
1495 QualifiedPathInType &operator= (QualifiedPathInType &&other) = default;
1496
1497 // Returns whether qualified path in type is in an error state.
1498 bool is_error () const { return path_type.is_error (); }
1499
1500 // Creates an error state qualified path in type.
1501 static QualifiedPathInType create_error ()
1502 {
1503 return QualifiedPathInType (
1504 QualifiedPathType::create_error (), nullptr,
1505 std::vector<std::unique_ptr<TypePathSegment>> (), UNDEF_LOCATION);
1506 }
1507
1508 std::string as_string () const override;
1509
1510 void accept_vis (ASTVisitor &vis) override;
1511
1512 // TODO: is this better? Or is a "vis_pattern" better?
1513 QualifiedPathType &get_qualified_path_type ()
1514 {
1515 rust_assert (!path_type.is_error ());
1516 return path_type;
1517 }
1518
1519 std::unique_ptr<TypePathSegment> &get_associated_segment ()
1520 {
1521 return associated_segment;
1522 }
1523
1524 // TODO: this seems kinda dodgy
1525 std::vector<std::unique_ptr<TypePathSegment>> &get_segments ()
1526 {
1527 return segments;
1528 }
1529 const std::vector<std::unique_ptr<TypePathSegment>> &get_segments () const
1530 {
1531 return segments;
1532 }
1533
1534 location_t get_locus () const override final { return locus; }
1535 };
1536 } // namespace AST
1537 } // namespace Rust
1538
1539 #endif