Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #ifndef RUST_HIR_PATH_H
20 : #define RUST_HIR_PATH_H
21 :
22 : #include "rust-hir-map.h"
23 : #include "rust-hir-simple-path.h"
24 : #include "rust-hir-type-no-bounds.h"
25 : #include "rust-hir-pattern-abstract.h"
26 : #include "rust-hir-expr-abstract.h"
27 :
28 : namespace Rust {
29 : namespace HIR {
30 :
31 : // The "identifier" (not generic args) aspect of each path expression segment
32 3279403 : class PathIdentSegment
33 : {
34 : std::string segment_name;
35 :
36 : // TODO: should this have location info stored?
37 :
38 : // only allow identifiers, "super", "self", "Self", "crate", or "$crate"
39 : public:
40 2740383 : PathIdentSegment (std::string segment_name)
41 2723854 : : segment_name (std::move (segment_name))
42 : {}
43 :
44 11022664 : PathIdentSegment (const PathIdentSegment &other)
45 21283641 : : segment_name (other.segment_name)
46 : {}
47 :
48 0 : PathIdentSegment &operator= (PathIdentSegment const &other)
49 : {
50 0 : segment_name = other.segment_name;
51 0 : return *this;
52 : }
53 :
54 : // Creates an error PathIdentSegment.
55 16529 : static PathIdentSegment create_error () { return PathIdentSegment (""); }
56 :
57 : // Returns whether PathIdentSegment is in an error state.
58 22632 : bool is_error () const { return segment_name.empty (); }
59 :
60 2004237 : std::string to_string () const { return segment_name; }
61 : };
62 :
63 : // A binding of an identifier to a type used in generic arguments in paths
64 : class GenericArgsBinding
65 : {
66 : public:
67 : enum class Kind
68 : {
69 : Equality,
70 : Constraint
71 : };
72 :
73 : private:
74 : Identifier identifier;
75 : std::unique_ptr<Type> type;
76 :
77 : location_t locus;
78 : Kind kind;
79 :
80 : public:
81 : // Returns whether binding is in an error state.
82 : bool is_error () const
83 : {
84 : return type == nullptr;
85 : // and also identifier is empty, but cheaper computation
86 : }
87 :
88 : // Creates an error state generic args binding.
89 : static GenericArgsBinding create_error ()
90 : {
91 : return GenericArgsBinding ({""}, nullptr);
92 : }
93 :
94 : // Pointer type for type in constructor to enable polymorphism
95 : GenericArgsBinding (Identifier ident, std::unique_ptr<Type> type_ptr,
96 : location_t locus = UNDEF_LOCATION,
97 : Kind kind = Kind::Equality);
98 :
99 : // Copy constructor has to deep copy the type as it is a unique pointer
100 : GenericArgsBinding (GenericArgsBinding const &other);
101 :
102 : // default destructor
103 12429 : ~GenericArgsBinding () = default;
104 :
105 : // Overload assignment operator to deep copy the pointed-to type
106 : GenericArgsBinding &operator= (GenericArgsBinding const &other);
107 :
108 : // move constructors
109 2868 : GenericArgsBinding (GenericArgsBinding &&other) = default;
110 : GenericArgsBinding &operator= (GenericArgsBinding &&other) = default;
111 :
112 : std::string to_string () const;
113 :
114 : Identifier &get_identifier () { return identifier; }
115 : const Identifier &get_identifier () const { return identifier; }
116 :
117 2154 : Type &get_type ()
118 : {
119 2154 : rust_assert (type);
120 2154 : return *type;
121 : }
122 : const Type &get_type () const
123 : {
124 : rust_assert (type);
125 : return *type;
126 : }
127 :
128 3 : location_t get_locus () const { return locus; }
129 2064 : Kind get_kind () const { return kind; }
130 : };
131 :
132 799 : class ConstGenericArg
133 : {
134 : // FIXME: Do we need to disambiguate or no? We should be able to disambiguate
135 : // at name-resolution, hence no need for ambiguities here
136 :
137 : public:
138 : ConstGenericArg (std::unique_ptr<Expr> expression, location_t locus);
139 :
140 : ConstGenericArg (const ConstGenericArg &other);
141 :
142 : ConstGenericArg operator= (const ConstGenericArg &other);
143 :
144 371 : std::unique_ptr<Expr> &get_expression () { return expression; }
145 :
146 0 : location_t get_locus () const { return locus; }
147 :
148 : private:
149 : std::unique_ptr<Expr> expression;
150 : location_t locus;
151 : };
152 :
153 : class GenericArgs
154 : {
155 : std::vector<Lifetime> lifetime_args;
156 : std::vector<std::unique_ptr<Type>> type_args;
157 : std::vector<GenericArgsBinding> binding_args;
158 : std::vector<ConstGenericArg> const_args;
159 : location_t locus;
160 :
161 : public:
162 : // Returns true if there are any generic arguments
163 178245 : bool has_generic_args () const
164 : {
165 308773 : return !(lifetime_args.empty () && type_args.empty ()
166 131639 : && binding_args.empty () && const_args.empty ());
167 : }
168 :
169 : GenericArgs (std::vector<Lifetime> lifetime_args,
170 : std::vector<std::unique_ptr<Type>> type_args,
171 : std::vector<GenericArgsBinding> binding_args,
172 : std::vector<ConstGenericArg> const_args, location_t locus);
173 :
174 : // copy constructor with vector clone
175 : GenericArgs (GenericArgs const &other);
176 :
177 8434724 : ~GenericArgs () = default;
178 :
179 : // overloaded assignment operator to vector clone
180 : GenericArgs &operator= (GenericArgs const &other);
181 :
182 : // move constructors
183 3020356 : GenericArgs (GenericArgs &&other) = default;
184 88300 : GenericArgs &operator= (GenericArgs &&other) = default;
185 :
186 : // Creates an empty GenericArgs (no arguments)
187 2038813 : static GenericArgs create_empty (location_t locus = UNDEF_LOCATION)
188 : {
189 2038813 : return GenericArgs ({}, {}, {}, {}, locus);
190 : }
191 :
192 : bool is_empty () const;
193 :
194 : std::string to_string () const;
195 :
196 88872 : std::vector<Lifetime> &get_lifetime_args () { return lifetime_args; }
197 : const std::vector<Lifetime> &get_lifetime_args () const
198 : {
199 113806 : return lifetime_args;
200 : }
201 :
202 117327 : std::vector<std::unique_ptr<Type>> &get_type_args () { return type_args; }
203 :
204 95271 : std::vector<GenericArgsBinding> &get_binding_args () { return binding_args; }
205 :
206 202667 : std::vector<ConstGenericArg> &get_const_args () { return const_args; }
207 :
208 208261 : location_t get_locus () const { return locus; }
209 : };
210 :
211 : /* A segment of a path in expression, including an identifier aspect and maybe
212 : * generic args */
213 6004308 : class PathExprSegment
214 : {
215 : private:
216 : Analysis::NodeMapping mappings;
217 : PathIdentSegment segment_name;
218 : GenericArgs generic_args;
219 : location_t locus;
220 :
221 : public:
222 : PathExprSegment (Analysis::NodeMapping mappings,
223 : PathIdentSegment segment_name, location_t locus,
224 : GenericArgs generic_args);
225 :
226 : PathExprSegment (PathExprSegment const &other);
227 :
228 : PathExprSegment &operator= (PathExprSegment const &other);
229 :
230 : // move constructors
231 1890018 : PathExprSegment (PathExprSegment &&other) = default;
232 : PathExprSegment &operator= (PathExprSegment &&other) = default;
233 :
234 : std::string to_string () const;
235 :
236 731723 : location_t get_locus () const { return locus; }
237 :
238 59314 : PathIdentSegment &get_segment () { return segment_name; }
239 50908 : const PathIdentSegment &get_segment () const { return segment_name; }
240 :
241 : GenericArgs &get_generic_args () { return generic_args; }
242 :
243 870281 : const Analysis::NodeMapping &get_mappings () const { return mappings; }
244 :
245 132296 : bool has_generic_args () const { return generic_args.has_generic_args (); }
246 : };
247 :
248 : // HIR node representing a pattern that involves a "path" - abstract base class
249 : class PathPattern : public Pattern, public RawPath<PathExprSegment>
250 : {
251 : public:
252 : enum class Kind
253 : {
254 : Segmented,
255 : LangItem
256 : };
257 :
258 : private:
259 : tl::optional<LangItem::Kind> lang_item;
260 : Kind kind;
261 :
262 : protected:
263 1638733 : PathPattern (RawPath<PathExprSegment> path)
264 1638733 : : RawPath (std::move (path)), lang_item (tl::nullopt),
265 1638733 : kind (Kind::Segmented)
266 1638733 : {}
267 :
268 13365 : PathPattern (LangItem::Kind lang_item)
269 13365 : : RawPath ({}, false), lang_item (lang_item), kind (Kind::LangItem)
270 13365 : {}
271 : bool is_refutable (const TyTy::BaseType &scrutinee) const override;
272 0 : bool is_refutable () const override
273 : {
274 : // Needs to be called with the other overload
275 0 : rust_unreachable ();
276 : }
277 :
278 : // Returns whether path has segments.
279 62 : bool has_segments () const
280 : {
281 62 : rust_assert (kind == Kind::Segmented);
282 62 : return !segments.empty ();
283 : }
284 :
285 : /* Converts path segments to their equivalent SimplePath segments if possible,
286 : * and creates a SimplePath from them. */
287 : AST::SimplePath
288 : convert_to_simple_path (bool with_opening_scope_resolution) const;
289 :
290 : public:
291 : /* Returns whether the path is a single segment (excluding qualified path
292 : * initial as segment). */
293 0 : bool is_single_segment () const
294 : {
295 0 : rust_assert (kind == Kind::Segmented);
296 0 : return segments.size () == 1;
297 : }
298 :
299 : std::string to_string () const override;
300 :
301 : void iterate_path_segments (std::function<bool (PathExprSegment &)> cb);
302 :
303 136577 : size_t get_num_segments () const
304 : {
305 136577 : rust_assert (kind == Kind::Segmented);
306 136577 : return segments.size ();
307 : }
308 :
309 138605 : std::vector<PathExprSegment> &get_segments ()
310 : {
311 138605 : rust_assert (kind == Kind::Segmented);
312 138605 : return segments;
313 : }
314 :
315 : const std::vector<PathExprSegment> &get_segments () const
316 : {
317 : rust_assert (kind == Kind::Segmented);
318 : return segments;
319 : }
320 :
321 : PathExprSegment &get_root_seg ()
322 : {
323 : rust_assert (kind == Kind::Segmented);
324 : return segments.at (0);
325 : }
326 :
327 50915 : const PathExprSegment &get_final_segment () const
328 : {
329 50915 : rust_assert (kind == Kind::Segmented);
330 50915 : return segments.back ();
331 : }
332 :
333 529 : LangItem::Kind get_lang_item () const
334 : {
335 529 : rust_assert (kind == Kind::LangItem);
336 :
337 529 : return *lang_item;
338 : }
339 :
340 811 : PatternType get_pattern_type () const override final
341 : {
342 811 : return PatternType::PATH;
343 : }
344 :
345 216836 : bool is_lang_item () const { return kind == Kind::LangItem; }
346 :
347 0 : Kind get_path_kind () const { return kind; }
348 : };
349 :
350 : /* HIR node representing a path-in-expression pattern (path that allows generic
351 : * arguments) */
352 : class PathInExpression : public PathPattern, public PathExpr
353 : {
354 : location_t locus;
355 :
356 : public:
357 : std::string to_string () const override;
358 :
359 : // Constructor
360 : PathInExpression (Analysis::NodeMapping mappings,
361 : RawPath<PathExprSegment> path,
362 : location_t locus = UNDEF_LOCATION,
363 : std::vector<AST::Attribute> outer_attrs
364 : = std::vector<AST::Attribute> ());
365 :
366 : // lang-item Constructor
367 : PathInExpression (Analysis::NodeMapping mappings, LangItem::Kind kind,
368 : location_t locus = UNDEF_LOCATION,
369 : std::vector<AST::Attribute> outer_attrs
370 : = std::vector<AST::Attribute> ());
371 :
372 : // Creates an error state path in expression.
373 : static PathInExpression create_error ()
374 : {
375 : return PathInExpression (Analysis::NodeMapping::get_error (), {});
376 : }
377 :
378 : // Returns whether path in expression is in an error state.
379 : bool is_error () const { return !has_segments (); }
380 :
381 : /* Converts PathInExpression to SimplePath if possible (i.e. no generic
382 : * arguments). Otherwise returns an empty SimplePath. */
383 62 : AST::SimplePath as_simple_path () const
384 : {
385 : /* delegate to parent class as can't access segments. however,
386 : * QualifiedPathInExpression conversion to simple path wouldn't make sense,
387 : * so the method in the parent class should be protected, not public. Have
388 : * to pass in opening scope resolution as parent class has no access to it.
389 : */
390 62 : return convert_to_simple_path (has_opening_scope_resolution ());
391 : }
392 :
393 1834572 : location_t get_locus () const override final { return locus; }
394 :
395 : void accept_vis (HIRFullVisitor &vis) override;
396 : void accept_vis (HIRExpressionVisitor &vis) override;
397 : void accept_vis (HIRPatternVisitor &vis) override;
398 :
399 : bool is_self () const;
400 :
401 226970 : const Analysis::NodeMapping &get_mappings () const override final
402 : {
403 226970 : return mappings;
404 : }
405 :
406 : protected:
407 : /* Use covariance to implement clone function as returning this object rather
408 : * than base */
409 8376 : PathInExpression *clone_pattern_impl () const override
410 : {
411 8376 : return new PathInExpression (*this);
412 : }
413 :
414 : /* Use covariance to implement clone function as returning this object rather
415 : * than base */
416 2494 : PathInExpression *clone_expr_without_block_impl () const override
417 : {
418 2494 : return new PathInExpression (*this);
419 : }
420 : };
421 :
422 : /* Base class for segments used in type paths - not abstract (represents an
423 : * ident-only segment) */
424 : class TypePathSegment
425 : {
426 : public:
427 : enum SegmentType
428 : {
429 : REG,
430 : GENERIC,
431 : FUNCTION
432 : };
433 :
434 : private:
435 : Analysis::NodeMapping mappings;
436 : tl::optional<PathIdentSegment> ident_segment;
437 : tl::optional<LangItem::Kind> lang_item;
438 : location_t locus;
439 :
440 : protected:
441 : bool has_separating_scope_resolution;
442 : SegmentType type;
443 :
444 : public:
445 : // Clone function implementation - not pure virtual as overrided by subclasses
446 419138 : virtual TypePathSegment *clone_type_path_segment_impl () const
447 : {
448 419138 : return new TypePathSegment (*this);
449 : }
450 :
451 : public:
452 747894 : virtual ~TypePathSegment () {}
453 :
454 292034 : virtual SegmentType get_type () const { return SegmentType::REG; }
455 :
456 : // Unique pointer custom clone function
457 487639 : std::unique_ptr<TypePathSegment> clone_type_path_segment () const
458 : {
459 487639 : return std::unique_ptr<TypePathSegment> (clone_type_path_segment_impl ());
460 : }
461 :
462 : TypePathSegment (Analysis::NodeMapping mappings,
463 : PathIdentSegment ident_segment,
464 : bool has_separating_scope_resolution, location_t locus);
465 :
466 : TypePathSegment (Analysis::NodeMapping mappings, LangItem::Kind lang_item,
467 : location_t locus);
468 :
469 : TypePathSegment (Analysis::NodeMapping mappings, std::string segment_name,
470 : bool has_separating_scope_resolution, location_t locus);
471 :
472 507996 : virtual std::string to_string () const
473 : {
474 507996 : if (ident_segment)
475 507996 : return ident_segment->to_string ();
476 :
477 0 : return LangItem::PrettyString (*lang_item);
478 : }
479 :
480 : /* Returns whether the type path segment is in an error state. May be virtual
481 : * in future. */
482 3 : bool is_error () const
483 : {
484 3 : rust_assert (ident_segment);
485 3 : return ident_segment->is_error ();
486 : }
487 :
488 : /* Returns whether segment is identifier only (as opposed to generic args or
489 : * function). Overriden in derived classes with other segments. */
490 225521 : virtual bool is_ident_only () const { return true; }
491 :
492 6250 : location_t get_locus () const { return locus; }
493 :
494 : // not pure virtual as class not abstract
495 : virtual void accept_vis (HIRFullVisitor &vis);
496 :
497 255658 : const Analysis::NodeMapping &get_mappings () const { return mappings; }
498 :
499 19817 : const PathIdentSegment &get_ident_segment () const
500 : {
501 19817 : rust_assert (ident_segment);
502 19817 : return *ident_segment;
503 : }
504 :
505 50 : const LangItem::Kind &get_lang_item () const
506 : {
507 50 : rust_assert (lang_item);
508 50 : return *lang_item;
509 : }
510 :
511 252803 : bool is_generic_segment () const
512 : {
513 252803 : return get_type () == SegmentType::GENERIC;
514 : }
515 :
516 253574 : bool is_lang_item () const { return lang_item.has_value (); }
517 : };
518 :
519 : // Segment used in type path with generic args
520 : class TypePathSegmentGeneric : public TypePathSegment
521 : {
522 : GenericArgs generic_args;
523 :
524 : public:
525 45949 : bool has_generic_args () const { return generic_args.has_generic_args (); }
526 :
527 25432 : bool is_ident_only () const override { return false; }
528 :
529 : // Constructor with PathIdentSegment and GenericArgs
530 : TypePathSegmentGeneric (Analysis::NodeMapping mappings,
531 : PathIdentSegment ident_segment,
532 : bool has_separating_scope_resolution,
533 : GenericArgs generic_args, location_t locus);
534 :
535 : TypePathSegmentGeneric (Analysis::NodeMapping mappings,
536 : LangItem::Kind lang_item, GenericArgs generic_args,
537 : location_t locus);
538 :
539 : // Constructor from segment name and all args
540 : TypePathSegmentGeneric (Analysis::NodeMapping mappings,
541 : std::string segment_name,
542 : bool has_separating_scope_resolution,
543 : std::vector<Lifetime> lifetime_args,
544 : std::vector<std::unique_ptr<Type>> type_args,
545 : std::vector<GenericArgsBinding> binding_args,
546 : std::vector<ConstGenericArg> const_args,
547 : location_t locus);
548 :
549 : std::string to_string () const override;
550 :
551 : void accept_vis (HIRFullVisitor &vis) override;
552 :
553 25438 : GenericArgs &get_generic_args () { return generic_args; }
554 :
555 68755 : virtual SegmentType get_type () const override final
556 : {
557 68755 : return SegmentType::GENERIC;
558 : }
559 :
560 : // Use covariance to override base class method
561 63781 : TypePathSegmentGeneric *clone_type_path_segment_impl () const override
562 : {
563 63781 : return new TypePathSegmentGeneric (*this);
564 : }
565 : };
566 :
567 : // A function as represented in a type path
568 : class TypePathFunction
569 : {
570 : std::vector<std::unique_ptr<Type>> inputs;
571 : std::unique_ptr<Type> return_type;
572 :
573 : public:
574 : // Returns whether the return type of the function has been specified.
575 7026 : bool has_return_type () const { return return_type != nullptr; }
576 :
577 : // Returns whether the function has inputs.
578 36 : bool has_inputs () const { return !inputs.empty (); }
579 :
580 : // Constructor
581 : TypePathFunction (std::vector<std::unique_ptr<Type>> inputs,
582 : std::unique_ptr<Type> type);
583 :
584 : // Copy constructor with clone
585 : TypePathFunction (TypePathFunction const &other);
586 :
587 4147 : ~TypePathFunction () = default;
588 :
589 : // Overloaded assignment operator to clone type
590 : TypePathFunction &operator= (TypePathFunction const &other);
591 :
592 : // move constructors
593 8294 : TypePathFunction (TypePathFunction &&other) = default;
594 : TypePathFunction &operator= (TypePathFunction &&other) = default;
595 :
596 : std::string to_string () const;
597 :
598 : const std::vector<std::unique_ptr<Type>> &get_params () const
599 : {
600 : return inputs;
601 : };
602 2115 : std::vector<std::unique_ptr<Type>> &get_params () { return inputs; };
603 :
604 4713 : const Type &get_return_type () const { return *return_type; };
605 1066 : Type &get_return_type () { return *return_type; };
606 : };
607 :
608 : // Segment used in type path with a function argument
609 : class TypePathSegmentFunction : public TypePathSegment
610 : {
611 : TypePathFunction function_path;
612 :
613 : public:
614 : // Constructor with PathIdentSegment and TypePathFn
615 : TypePathSegmentFunction (Analysis::NodeMapping mappings,
616 : PathIdentSegment ident_segment,
617 : bool has_separating_scope_resolution,
618 : TypePathFunction function_path, location_t locus);
619 :
620 : // Constructor with segment name and TypePathFn
621 : TypePathSegmentFunction (Analysis::NodeMapping mappings,
622 : std::string segment_name,
623 : bool has_separating_scope_resolution,
624 : TypePathFunction function_path, location_t locus);
625 :
626 : std::string to_string () const override;
627 :
628 0 : bool is_ident_only () const override { return false; }
629 :
630 : void accept_vis (HIRFullVisitor &vis) override;
631 :
632 2080 : SegmentType get_type () const override final { return SegmentType::FUNCTION; }
633 :
634 2116 : TypePathFunction &get_function_path () { return function_path; }
635 :
636 : // Use covariance to override base class method
637 4910 : TypePathSegmentFunction *clone_type_path_segment_impl () const override
638 : {
639 4910 : return new TypePathSegmentFunction (*this);
640 : }
641 : };
642 :
643 : // Path used inside types
644 42007 : class TypePath : public TypeNoBounds,
645 : public RawPath<std::unique_ptr<TypePathSegment>>
646 : {
647 : protected:
648 : /* Use covariance to implement clone function as returning this object rather
649 : * than base */
650 419943 : TypePath *clone_type_impl () const override { return new TypePath (*this); }
651 :
652 : /* Use covariance to implement clone function as returning this object rather
653 : * than base */
654 0 : TypePath *clone_type_no_bounds_impl () const override
655 : {
656 0 : return new TypePath (*this);
657 : }
658 :
659 : public:
660 : // Returns whether the TypePath is in an invalid state.
661 : bool is_error () const { return segments.empty (); }
662 :
663 : // Creates an error state TypePath.
664 : static TypePath create_error ()
665 : {
666 : return TypePath (Analysis::NodeMapping::get_error (),
667 : RawPath<std::unique_ptr<TypePathSegment>>{{}, false},
668 : UNDEF_LOCATION);
669 : }
670 :
671 : // Constructor
672 : TypePath (Analysis::NodeMapping mappings,
673 : RawPath<std::unique_ptr<TypePathSegment>> path, location_t locus);
674 :
675 : // Copy constructor with vector clone
676 : TypePath (TypePath const &other);
677 :
678 : // Overloaded assignment operator with clone
679 : TypePath &operator= (TypePath const &other);
680 :
681 : // move constructors
682 42007 : TypePath (TypePath &&other) = default;
683 : TypePath &operator= (TypePath &&other) = default;
684 :
685 : std::string to_string () const override;
686 :
687 : /* Converts TypePath to SimplePath if possible (i.e. no generic or function
688 : * arguments). Otherwise returns an empty SimplePath. */
689 : AST::SimplePath as_simple_path () const;
690 :
691 : // Creates a trait bound with a clone of this type path as its only element.
692 : std::unique_ptr<TraitBound> to_trait_bound (bool in_parens) const override;
693 :
694 : void accept_vis (HIRFullVisitor &vis) override;
695 : void accept_vis (HIRTypeVisitor &vis) override;
696 :
697 750366 : size_t get_num_segments () const { return segments.size (); }
698 :
699 256199 : std::vector<std::unique_ptr<TypePathSegment>> &get_segments ()
700 : {
701 314796 : return segments;
702 : }
703 :
704 92917 : TypePathSegment &get_final_segment () { return *segments.back (); }
705 : };
706 :
707 : class QualifiedPathType
708 : {
709 : std::unique_ptr<Type> type;
710 : std::unique_ptr<TypePath> trait;
711 : location_t locus;
712 : Analysis::NodeMapping mappings;
713 :
714 : public:
715 : // Constructor
716 : QualifiedPathType (Analysis::NodeMapping mappings, std::unique_ptr<Type> type,
717 : std::unique_ptr<TypePath> trait, location_t locus);
718 :
719 : // Copy constructor uses custom deep copy for Type to preserve polymorphism
720 : QualifiedPathType (QualifiedPathType const &other);
721 :
722 : // default destructor
723 71380 : ~QualifiedPathType () = default;
724 :
725 : // overload assignment operator to use custom clone method
726 : QualifiedPathType &operator= (QualifiedPathType const &other);
727 :
728 : // move constructor
729 53160 : QualifiedPathType (QualifiedPathType &&other) = default;
730 : QualifiedPathType &operator= (QualifiedPathType &&other) = default;
731 :
732 : // Returns whether the qualified path type has a rebind as clause.
733 36086 : bool has_as_clause () const { return trait != nullptr; }
734 :
735 : std::string to_string () const;
736 :
737 : location_t get_locus () const { return locus; }
738 :
739 0 : Analysis::NodeMapping get_mappings () const { return mappings; }
740 :
741 0 : bool has_type () { return type != nullptr; }
742 0 : bool has_trait () { return trait != nullptr; }
743 :
744 34884 : Type &get_type ()
745 : {
746 34884 : rust_assert (type);
747 34884 : return *type;
748 : }
749 :
750 34721 : TypePath &get_trait ()
751 : {
752 34721 : rust_assert (trait);
753 34721 : return *trait;
754 : }
755 :
756 : bool trait_has_generic_args () const;
757 :
758 : GenericArgs &get_trait_generic_args ();
759 : };
760 :
761 : /* HIR node representing a qualified path-in-expression pattern (path that
762 : * allows specifying trait functions) */
763 : class QualifiedPathInExpression : public PathPattern, public PathExpr
764 : {
765 : QualifiedPathType path_type;
766 : location_t locus;
767 :
768 : public:
769 : std::string to_string () const override;
770 :
771 : QualifiedPathInExpression (Analysis::NodeMapping mappings,
772 : QualifiedPathType qual_path_type,
773 : RawPath<PathExprSegment> path,
774 : location_t locus = UNDEF_LOCATION,
775 : std::vector<AST::Attribute> outer_attrs
776 : = std::vector<AST::Attribute> ());
777 :
778 : // lang-item constructor
779 : QualifiedPathInExpression (Analysis::NodeMapping mappings,
780 : QualifiedPathType qual_path_type,
781 : LangItem::Kind lang_item,
782 : location_t locus = UNDEF_LOCATION,
783 : std::vector<AST::Attribute> outer_attrs
784 : = std::vector<AST::Attribute> ());
785 :
786 984 : location_t get_locus () const override final { return locus; }
787 :
788 : void accept_vis (HIRFullVisitor &vis) override;
789 : void accept_vis (HIRExpressionVisitor &vis) override;
790 : void accept_vis (HIRPatternVisitor &vis) override;
791 :
792 205 : QualifiedPathType &get_path_type () { return path_type; }
793 :
794 237 : location_t get_locus () { return locus; }
795 :
796 360 : const Analysis::NodeMapping &get_mappings () const override final
797 : {
798 360 : return mappings;
799 : }
800 :
801 : protected:
802 : /* Use covariance to implement clone function as returning this object rather
803 : * than base */
804 0 : QualifiedPathInExpression *clone_pattern_impl () const override
805 : {
806 0 : return new QualifiedPathInExpression (*this);
807 : }
808 :
809 : /* Use covariance to implement clone function as returning this object rather
810 : * than base */
811 0 : QualifiedPathInExpression *clone_expr_without_block_impl () const override
812 : {
813 0 : return new QualifiedPathInExpression (*this);
814 : }
815 : };
816 :
817 : /* Represents a qualified path in a type; used for disambiguating trait function
818 : * calls */
819 : class QualifiedPathInType : public TypeNoBounds
820 : {
821 : QualifiedPathType path_type;
822 : std::unique_ptr<TypePathSegment> associated_segment;
823 : std::vector<std::unique_ptr<TypePathSegment>> segments;
824 :
825 : protected:
826 : /* Use covariance to implement clone function as returning this object rather
827 : * than base */
828 190 : QualifiedPathInType *clone_type_impl () const override
829 : {
830 190 : return new QualifiedPathInType (*this);
831 : }
832 :
833 : /* Use covariance to implement clone function as returning this object rather
834 : * than base */
835 0 : QualifiedPathInType *clone_type_no_bounds_impl () const override
836 : {
837 0 : return new QualifiedPathInType (*this);
838 : }
839 :
840 : public:
841 : QualifiedPathInType (
842 : Analysis::NodeMapping mappings, QualifiedPathType qual_path_type,
843 : std::unique_ptr<TypePathSegment> associated_segment,
844 : std::vector<std::unique_ptr<TypePathSegment>> path_segments,
845 : location_t locus = UNDEF_LOCATION);
846 :
847 : // Copy constructor with vector clone
848 : QualifiedPathInType (QualifiedPathInType const &other);
849 :
850 : // Overloaded assignment operator with vector clone
851 : QualifiedPathInType &operator= (QualifiedPathInType const &other);
852 :
853 : // move constructors
854 : QualifiedPathInType (QualifiedPathInType &&other) = default;
855 : QualifiedPathInType &operator= (QualifiedPathInType &&other) = default;
856 :
857 : std::string to_string () const override;
858 :
859 : void accept_vis (HIRFullVisitor &vis) override;
860 : void accept_vis (HIRTypeVisitor &vis) override;
861 :
862 17407 : QualifiedPathType &get_path_type () { return path_type; }
863 :
864 17406 : TypePathSegment &get_associated_segment () { return *associated_segment; }
865 :
866 1 : std::vector<std::unique_ptr<TypePathSegment>> &get_segments ()
867 : {
868 17364 : return segments;
869 : }
870 : };
871 :
872 : } // namespace HIR
873 : } // namespace Rust
874 :
875 : #endif
|