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_TYTY
20 : #define RUST_TYTY
21 :
22 : #include "optional.h"
23 : #include "rust-hir-map.h"
24 : #include "rust-common.h"
25 : #include "rust-identifier.h"
26 : #include "rust-abi.h"
27 : #include "rust-tyty-util.h"
28 : #include "rust-tyty-subst.h"
29 : #include "rust-tyty-region.h"
30 : #include "rust-system.h"
31 : #include "rust-hir.h"
32 : #include "tree.h"
33 :
34 : namespace Rust {
35 :
36 : namespace Resolver {
37 : class TraitReference;
38 :
39 : class TraitItemReference;
40 :
41 : class AssociatedImplTrait;
42 : } // namespace Resolver
43 :
44 : namespace TyTy {
45 : class ClosureType;
46 : class FnPtr;
47 : class FnType;
48 : class CallableTypeInterface;
49 :
50 : // https://rustc-dev-guide.rust-lang.org/type-inference.html#inference-variables
51 : // https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.TyKind.html#variants
52 : enum TypeKind
53 : {
54 : INFER,
55 : ADT,
56 : STR,
57 : REF,
58 : POINTER,
59 : PARAM,
60 : CONST,
61 : ARRAY,
62 : SLICE,
63 : FNDEF,
64 : FNPTR,
65 : TUPLE,
66 : BOOL,
67 : CHAR,
68 : INT,
69 : UINT,
70 : FLOAT,
71 : USIZE,
72 : ISIZE,
73 : NEVER,
74 : PLACEHOLDER,
75 : PROJECTION,
76 : DYNAMIC,
77 : CLOSURE,
78 : OPAQUE,
79 : // there are more to add...
80 : ERROR
81 : };
82 :
83 : extern bool is_primitive_type_kind (TypeKind kind);
84 :
85 : class TypeKindFormat
86 : {
87 : public:
88 : static std::string to_string (TypeKind kind);
89 : };
90 :
91 : class TyVisitor;
92 : class TyConstVisitor;
93 : class BaseConstType;
94 :
95 : class TypeBoundPredicate : public SubstitutionRef
96 : {
97 : public:
98 : TypeBoundPredicate (const Resolver::TraitReference &trait_reference,
99 : BoundPolarity polarity, location_t locus);
100 :
101 : TypeBoundPredicate (DefId reference,
102 : std::vector<SubstitutionParamMapping> substitutions,
103 : BoundPolarity polarity, location_t locus);
104 :
105 : TypeBoundPredicate (const TypeBoundPredicate &other);
106 :
107 66258523 : virtual ~TypeBoundPredicate () {}
108 :
109 : TypeBoundPredicate &operator= (const TypeBoundPredicate &other);
110 :
111 : static TypeBoundPredicate error ();
112 :
113 : std::string as_string () const;
114 :
115 : std::string as_name () const;
116 :
117 : const Resolver::TraitReference *get () const;
118 :
119 7931 : location_t get_locus () const { return locus; }
120 :
121 : std::string get_name () const;
122 :
123 : // check that this is object-safe see:
124 : // https://doc.rust-lang.org/reference/items/traits.html#object-safety
125 : bool is_object_safe (bool emit_error, location_t locus) const;
126 :
127 : void apply_generic_arguments (HIR::GenericArgs *generic_args,
128 : bool has_associated_self, bool is_super_trait);
129 :
130 : void apply_argument_mappings (SubstitutionArgumentMappings &arguments,
131 : bool is_super_trait);
132 :
133 : bool contains_item (const std::string &search) const;
134 :
135 : tl::optional<TypeBoundPredicateItem>
136 : lookup_associated_item (const std::string &search) const;
137 :
138 : tl::optional<TypeBoundPredicateItem>
139 : lookup_associated_item (const Resolver::TraitItemReference *ref) const;
140 :
141 : // WARNING THIS WILL ALWAYS RETURN NULLPTR
142 : BaseType *
143 : handle_substitions (SubstitutionArgumentMappings &mappings) override final;
144 :
145 : bool is_error () const;
146 :
147 : bool requires_generic_args () const;
148 :
149 : bool contains_associated_types () const;
150 :
151 52457 : DefId get_id () const { return reference; }
152 :
153 13409 : BoundPolarity get_polarity () const { return polarity; }
154 :
155 : std::vector<TypeBoundPredicateItem> get_associated_type_items ();
156 :
157 : size_t get_num_associated_bindings () const override final;
158 :
159 : TypeBoundPredicateItem
160 : lookup_associated_type (const std::string &search) override final;
161 :
162 : bool is_equal (const TypeBoundPredicate &other) const;
163 :
164 : bool validate_type_implements_super_traits (TyTy::BaseType &self,
165 : HIR::Type &impl_type,
166 : HIR::Type &trait) const;
167 :
168 : bool validate_type_implements_this (TyTy::BaseType &self,
169 : HIR::Type &impl_type,
170 : HIR::Type &trait) const;
171 :
172 : private:
173 : struct mark_is_error
174 : {
175 : };
176 :
177 : TypeBoundPredicate (mark_is_error);
178 :
179 : void get_trait_hierachy (
180 : std::function<void (const Resolver::TraitReference &)> callback) const;
181 :
182 : DefId reference;
183 : location_t locus;
184 : bool error_flag;
185 : BoundPolarity polarity;
186 : std::vector<TyTy::TypeBoundPredicate> super_traits;
187 : };
188 :
189 98692 : class TypeBoundsMappings
190 : {
191 : protected:
192 : TypeBoundsMappings (std::vector<TypeBoundPredicate> specified_bounds);
193 :
194 : public:
195 : std::vector<TypeBoundPredicate> &get_specified_bounds ();
196 :
197 : const std::vector<TypeBoundPredicate> &get_specified_bounds () const;
198 :
199 : TypeBoundPredicate lookup_predicate (DefId id);
200 :
201 : size_t num_specified_bounds () const;
202 :
203 : std::string raw_bounds_as_string () const;
204 :
205 : std::string bounds_as_string () const;
206 :
207 : std::string raw_bounds_as_name () const;
208 :
209 : protected:
210 : void add_bound (TypeBoundPredicate predicate);
211 :
212 : std::vector<TypeBoundPredicate> specified_bounds;
213 : };
214 :
215 : class BaseType : public TypeBoundsMappings
216 : {
217 : public:
218 : virtual ~BaseType ();
219 :
220 : HirId get_ref () const;
221 : void set_ref (HirId id);
222 :
223 : HirId get_ty_ref () const;
224 : void set_ty_ref (HirId id);
225 :
226 : HirId get_orig_ref () const;
227 :
228 : virtual void accept_vis (TyVisitor &vis) = 0;
229 : virtual void accept_vis (TyConstVisitor &vis) const = 0;
230 :
231 : virtual std::string as_string () const = 0;
232 : virtual std::string get_name () const = 0;
233 :
234 : // Check value equality between two ty. Type inference rules are ignored. Two
235 : // ty are considered equal if they're of the same kind, and
236 : // 1. (For ADTs, arrays, tuples, refs) have the same underlying ty
237 : // 2. (For functions) have the same signature
238 : virtual bool is_equal (const BaseType &other) const;
239 :
240 : bool satisfies_bound (const TypeBoundPredicate &predicate, bool emit_error);
241 :
242 : bool bounds_compatible (BaseType &other, location_t locus, bool emit_error);
243 :
244 : void inherit_bounds (const BaseType &other);
245 :
246 : void inherit_bounds (
247 : const std::vector<TyTy::TypeBoundPredicate> &specified_bounds);
248 :
249 : // contains_infer checks if there is an inference variable inside the type
250 : const TyTy::BaseType *contains_infer () const;
251 :
252 : // is_unit returns whether this is just a unit-struct
253 : bool is_unit () const;
254 :
255 : // is_concrete returns true if the type is fully resolved to concrete
256 : // primitives
257 : bool is_concrete () const;
258 :
259 : // return the type-kind
260 : TypeKind get_kind () const;
261 :
262 : // monomorphized clone is a clone which destructures the types to get rid of
263 : // generics
264 : BaseType *monomorphized_clone () const;
265 :
266 : // get_combined_refs returns the chain of node refs involved in unification
267 : std::set<HirId> get_combined_refs () const;
268 :
269 : void append_reference (HirId id);
270 :
271 : std::string mappings_str () const;
272 :
273 : std::string debug_str () const;
274 :
275 : void debug () const;
276 :
277 : BaseType *get_root ();
278 :
279 : // This will get the monomorphized type from Params, Placeholders or
280 : // Projections if available or error
281 : BaseType *destructure ();
282 : const BaseType *destructure () const;
283 :
284 : const RustIdent &get_ident () const;
285 : location_t get_locus () const;
286 :
287 : bool has_substitutions_defined () const;
288 : bool needs_generic_substitutions () const;
289 : const SubstitutionArgumentMappings &get_subst_argument_mappings () const;
290 :
291 31649 : std::string mangle_string () const
292 : {
293 94947 : return TypeKindFormat::to_string (get_kind ()) + ":" + as_string () + ":"
294 126596 : + mappings_str () + ":" + bounds_as_string ();
295 : }
296 :
297 : /* Returns a pointer to a clone of this. The caller is responsible for
298 : * releasing the memory of the returned ty. */
299 : virtual BaseType *clone () const = 0;
300 :
301 : // Check if TyTy::BaseType is of a specific type.
302 155355898 : template <typename T> WARN_UNUSED_RESULT bool is () const
303 : {
304 : static_assert (std::is_base_of<BaseType, T>::value,
305 : "Can only safely cast to TyTy types.");
306 53652891 : return this->get_kind () == T::KIND;
307 : }
308 :
309 1038979 : template <typename T> T *as () const
310 : {
311 : static_assert (std::is_base_of<BaseType, T>::value,
312 : "Can only safely cast to TyTy types.");
313 1038979 : rust_assert (this->is<T> ());
314 1038979 : return static_cast<T *> (this);
315 : }
316 :
317 58626 : template <typename T> T *as ()
318 : {
319 : static_assert (std::is_base_of<BaseType, T>::value,
320 : "Can only safely cast to TyTy types.");
321 58626 : rust_assert (this->is<T> ());
322 58626 : return static_cast<T *> (this);
323 : }
324 :
325 : // Check if TyTy::BaseType is of a specific type and convert it to that type
326 : // if so.
327 : // Returns nullptr otherwise. Works as a dynamic_cast, but without compiler
328 : // RTTI.
329 88011822 : template <typename T> T *try_as () const
330 : {
331 : static_assert (std::is_base_of<BaseType, T>::value,
332 : "Can only safely cast to TyTy types.");
333 88011822 : if (!this->is<T> ())
334 : return nullptr;
335 :
336 : return static_cast<T *> (this);
337 : }
338 :
339 : // See above.
340 12644180 : template <typename T> T *try_as ()
341 : {
342 : static_assert (std::is_base_of<BaseType, T>::value,
343 : "Can only safely cast to TyTy types.");
344 12644180 : if (!this->is<T> ())
345 0 : return nullptr;
346 :
347 : return static_cast<T *> (this);
348 : }
349 :
350 : // Helper to get BaseConstType interface for CONST types
351 : // Overridden by const types that also inherit from BaseConstType
352 0 : virtual BaseConstType *as_const_type () { return nullptr; }
353 0 : virtual const BaseConstType *as_const_type () const { return nullptr; }
354 :
355 : protected:
356 : BaseType (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
357 : std::set<HirId> refs = std::set<HirId> ());
358 :
359 : BaseType (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
360 : std::vector<TypeBoundPredicate> specified_bounds,
361 : std::set<HirId> refs = std::set<HirId> ());
362 :
363 : TypeKind kind;
364 : HirId ref;
365 : HirId ty_ref;
366 : const HirId orig_ref;
367 : std::set<HirId> combined;
368 : RustIdent ident;
369 :
370 : Analysis::Mappings &mappings;
371 : };
372 :
373 : /** Unified interface for all function-like types. */
374 : class CallableTypeInterface : public BaseType
375 : {
376 : public:
377 57119 : explicit CallableTypeInterface (HirId ref, HirId ty_ref, TypeKind kind,
378 : RustIdent ident,
379 : std::set<HirId> refs = std::set<HirId> ())
380 57119 : : BaseType (ref, ty_ref, kind, ident, refs)
381 57119 : {}
382 :
383 : WARN_UNUSED_RESULT virtual size_t get_num_params () const = 0;
384 : WARN_UNUSED_RESULT virtual BaseType *get_param_type_at (size_t index) const
385 : = 0;
386 : WARN_UNUSED_RESULT virtual BaseType *get_return_type () const = 0;
387 : };
388 :
389 : class InferType : public BaseType
390 : {
391 : public:
392 : static constexpr auto KIND = TypeKind::INFER;
393 :
394 : enum InferTypeKind
395 : {
396 : GENERAL,
397 : INTEGRAL,
398 : FLOAT
399 : };
400 :
401 : struct TypeHint
402 : {
403 : enum SignedHint
404 : {
405 : SIGNED,
406 : UNSIGNED,
407 :
408 : UNKNOWN
409 : };
410 : enum SizeHint
411 : {
412 : S8,
413 : S16,
414 : S32,
415 : S64,
416 : S128,
417 : SUNKNOWN
418 : };
419 :
420 : TyTy::TypeKind kind;
421 : SignedHint shint;
422 : SizeHint szhint;
423 :
424 : static TypeHint Default ()
425 : {
426 : return TypeHint{TypeKind::ERROR, UNKNOWN, SUNKNOWN};
427 : }
428 : };
429 :
430 : InferType (HirId ref, InferTypeKind infer_kind, TypeHint hint,
431 : location_t locus, std::set<HirId> refs = std::set<HirId> ());
432 :
433 : InferType (HirId ref, HirId ty_ref, InferTypeKind infer_kind, TypeHint hint,
434 : location_t locus, std::set<HirId> refs = std::set<HirId> ());
435 :
436 : void accept_vis (TyVisitor &vis) override;
437 :
438 : void accept_vis (TyConstVisitor &vis) const override;
439 :
440 : std::string as_string () const override;
441 :
442 : BaseType *clone () const final override;
443 :
444 : InferTypeKind get_infer_kind () const;
445 :
446 : std::string get_name () const override final;
447 :
448 : bool default_type (BaseType **type) const;
449 :
450 : void apply_primitive_type_hint (const TyTy::BaseType &hint);
451 :
452 : private:
453 : InferTypeKind infer_kind;
454 : TypeHint default_hint;
455 : };
456 :
457 : class ErrorType : public BaseType
458 : {
459 : public:
460 : static constexpr auto KIND = TypeKind::ERROR;
461 :
462 : ErrorType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
463 :
464 : ErrorType (HirId ref, HirId ty_ref,
465 : std::set<HirId> refs = std::set<HirId> ());
466 :
467 : void accept_vis (TyVisitor &vis) override;
468 : void accept_vis (TyConstVisitor &vis) const override;
469 :
470 : std::string as_string () const override;
471 :
472 : BaseType *clone () const final override;
473 :
474 : std::string get_name () const override final;
475 : };
476 :
477 : class BaseGeneric : public BaseType
478 : {
479 : public:
480 : virtual std::string get_symbol () const = 0;
481 :
482 : virtual bool can_resolve () const = 0;
483 :
484 : virtual BaseType *resolve () const = 0;
485 :
486 : protected:
487 95110076 : BaseGeneric (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
488 : std::vector<TypeBoundPredicate> specified_bounds,
489 : std::set<HirId> refs = std::set<HirId> ())
490 95110076 : : BaseType (ref, ty_ref, kind, ident, specified_bounds, refs)
491 95110076 : {}
492 : };
493 :
494 : class ParamType : public BaseGeneric
495 : {
496 : public:
497 : static constexpr auto KIND = TypeKind::PARAM;
498 :
499 : ParamType (std::string symbol, location_t locus, HirId ref,
500 : std::vector<TypeBoundPredicate> specified_bounds,
501 : std::set<HirId> refs = std::set<HirId> ());
502 :
503 : ParamType (bool is_trait_self, std::string symbol, location_t locus,
504 : HirId ref, HirId ty_ref,
505 : std::vector<TypeBoundPredicate> specified_bounds,
506 : std::set<HirId> refs = std::set<HirId> ());
507 :
508 : void accept_vis (TyVisitor &vis) override;
509 : void accept_vis (TyConstVisitor &vis) const override;
510 :
511 : std::string as_string () const override;
512 :
513 : BaseType *clone () const final override;
514 :
515 : std::string get_symbol () const override final;
516 :
517 : bool can_resolve () const override final;
518 :
519 : BaseType *resolve () const override final;
520 :
521 : std::string get_name () const override final;
522 :
523 : bool is_equal (const BaseType &other) const override;
524 :
525 : ParamType *handle_substitions (SubstitutionArgumentMappings &mappings);
526 :
527 : void set_implicit_self_trait ();
528 : bool is_implicit_self_trait () const;
529 :
530 : private:
531 : bool is_trait_self;
532 : std::string symbol;
533 : };
534 :
535 3334 : class BaseConstType
536 : {
537 : public:
538 : static constexpr auto KIND = TypeKind::CONST;
539 :
540 : enum ConstKind
541 : {
542 : Decl,
543 : Value,
544 : Infer,
545 : Error
546 : };
547 :
548 : virtual ConstKind const_kind () const = 0;
549 :
550 2022 : BaseType *get_specified_type () const { return specified_type; }
551 :
552 : // Helper to get BaseType interface (all const types also inherit BaseType)
553 : // This must be implemented by concrete classes since BaseConstType doesn't
554 : // inherit from BaseType, but all concrete const types do.
555 : virtual BaseType *as_base_type () = 0;
556 : virtual const BaseType *as_base_type () const = 0;
557 :
558 : protected:
559 809 : BaseConstType (BaseType *type) : specified_type (type) {}
560 :
561 : BaseType *specified_type;
562 : };
563 :
564 : class ConstParamType : public BaseConstType, public BaseGeneric
565 : {
566 : public:
567 : ConstParamType (std::string symbol, location_t locus, BaseType *type,
568 : HirId ref, HirId ty_ref,
569 : std::set<HirId> refs = std::set<HirId> ());
570 :
571 : ConstKind const_kind () const override final;
572 :
573 : std::string get_symbol () const override final;
574 :
575 : bool can_resolve () const override final;
576 :
577 : BaseType *resolve () const override final;
578 :
579 : void accept_vis (TyVisitor &vis) override;
580 : void accept_vis (TyConstVisitor &vis) const override;
581 :
582 : std::string as_string () const override;
583 :
584 : BaseType *clone () const final override;
585 : std::string get_name () const override final;
586 :
587 : bool is_equal (const BaseType &other) const override;
588 :
589 : BaseType *handle_substitions (SubstitutionArgumentMappings &mappings);
590 :
591 117 : BaseType *as_base_type () override { return static_cast<BaseType *> (this); }
592 2635 : const BaseType *as_base_type () const override
593 : {
594 2635 : return static_cast<const BaseType *> (this);
595 : }
596 :
597 2253 : BaseConstType *as_const_type () override { return this; }
598 2510 : const BaseConstType *as_const_type () const override { return this; }
599 :
600 : private:
601 : std::string symbol;
602 : };
603 :
604 3334 : class ConstValueType : public BaseType, public BaseConstType
605 : {
606 : public:
607 : static constexpr auto KIND = TypeKind::CONST;
608 :
609 : ConstValueType (tree value, BaseType *type, HirId ref, HirId ty_ref,
610 : std::set<HirId> refs = std::set<HirId> ());
611 :
612 : ConstKind const_kind () const override final;
613 :
614 : void accept_vis (TyVisitor &vis) override;
615 : void accept_vis (TyConstVisitor &vis) const override;
616 :
617 : std::string as_string () const override;
618 :
619 : BaseType *clone () const final override;
620 : std::string get_name () const override final;
621 :
622 : bool is_equal (const BaseType &other) const override;
623 :
624 : tree get_value () const;
625 :
626 5478 : BaseType *as_base_type () override { return static_cast<BaseType *> (this); }
627 0 : const BaseType *as_base_type () const override
628 : {
629 0 : return static_cast<const BaseType *> (this);
630 : }
631 :
632 26484 : BaseConstType *as_const_type () override { return this; }
633 36782 : const BaseConstType *as_const_type () const override { return this; }
634 :
635 : private:
636 : tree folded_val;
637 : };
638 :
639 : class ConstInferType : public BaseType, public BaseConstType
640 : {
641 : public:
642 : static constexpr auto KIND = TypeKind::CONST;
643 :
644 : ConstInferType (BaseType *type, HirId ref, HirId ty_ref,
645 : std::set<HirId> refs = std::set<HirId> ());
646 :
647 : ConstKind const_kind () const override final;
648 :
649 : void accept_vis (TyVisitor &vis) override;
650 : void accept_vis (TyConstVisitor &vis) const override;
651 :
652 : std::string as_string () const override;
653 :
654 : BaseType *clone () const final override;
655 : std::string get_name () const override final;
656 :
657 : bool is_equal (const BaseType &other) const override;
658 :
659 12 : BaseType *as_base_type () override { return static_cast<BaseType *> (this); }
660 0 : const BaseType *as_base_type () const override
661 : {
662 0 : return static_cast<const BaseType *> (this);
663 : }
664 :
665 591 : BaseConstType *as_const_type () override { return this; }
666 78 : const BaseConstType *as_const_type () const override { return this; }
667 : };
668 :
669 : class ConstErrorType : public BaseType, public BaseConstType
670 : {
671 : public:
672 : static constexpr auto KIND = TypeKind::CONST;
673 :
674 : ConstErrorType (BaseType *type, HirId ref, HirId ty_ref,
675 : std::set<HirId> refs = std::set<HirId> ());
676 :
677 : ConstKind const_kind () const override final;
678 :
679 : void accept_vis (TyVisitor &vis) override;
680 : void accept_vis (TyConstVisitor &vis) const override;
681 :
682 : std::string as_string () const override;
683 :
684 : BaseType *clone () const final override;
685 : std::string get_name () const override final;
686 :
687 : bool is_equal (const BaseType &other) const override;
688 :
689 1 : BaseType *as_base_type () override { return static_cast<BaseType *> (this); }
690 0 : const BaseType *as_base_type () const override
691 : {
692 0 : return static_cast<const BaseType *> (this);
693 : }
694 :
695 0 : BaseConstType *as_const_type () override { return this; }
696 0 : const BaseConstType *as_const_type () const override { return this; }
697 : };
698 :
699 560 : class OpaqueType : public BaseType
700 : {
701 : public:
702 : static constexpr auto KIND = TypeKind::OPAQUE;
703 :
704 : OpaqueType (location_t locus, HirId ref,
705 : std::vector<TypeBoundPredicate> specified_bounds,
706 : std::set<HirId> refs = std::set<HirId> ());
707 :
708 : OpaqueType (location_t locus, HirId ref, HirId ty_ref,
709 : std::vector<TypeBoundPredicate> specified_bounds,
710 : std::set<HirId> refs = std::set<HirId> ());
711 :
712 : void accept_vis (TyVisitor &vis) override;
713 : void accept_vis (TyConstVisitor &vis) const override;
714 :
715 : std::string as_string () const override;
716 :
717 : BaseType *clone () const final override;
718 :
719 : bool can_resolve () const;
720 :
721 : BaseType *resolve () const;
722 :
723 : std::string get_name () const override final;
724 :
725 : bool is_equal (const BaseType &other) const override;
726 : };
727 :
728 : class StructFieldType
729 : {
730 : public:
731 : StructFieldType (HirId ref, std::string name, BaseType *ty, location_t locus);
732 :
733 : HirId get_ref () const;
734 :
735 : bool is_equal (const StructFieldType &other) const;
736 :
737 : std::string get_name () const;
738 :
739 : BaseType *get_field_type () const;
740 : void set_field_type (BaseType *fty);
741 :
742 : StructFieldType *clone () const;
743 : StructFieldType *monomorphized_clone () const;
744 :
745 : void debug () const;
746 : location_t get_locus () const;
747 : std::string as_string () const;
748 :
749 : private:
750 : HirId ref;
751 : std::string name;
752 : BaseType *ty;
753 : location_t locus;
754 : };
755 :
756 : class TupleType : public BaseType
757 : {
758 : public:
759 : static constexpr auto KIND = TypeKind::TUPLE;
760 :
761 : TupleType (HirId ref, location_t locus,
762 : std::vector<TyVar> fields = std::vector<TyVar> (),
763 : std::set<HirId> refs = std::set<HirId> ());
764 :
765 : TupleType (HirId ref, HirId ty_ref, location_t locus,
766 : std::vector<TyVar> fields = std::vector<TyVar> (),
767 : std::set<HirId> refs = std::set<HirId> ());
768 :
769 : static TupleType *get_unit_type ();
770 :
771 : void accept_vis (TyVisitor &vis) override;
772 : void accept_vis (TyConstVisitor &vis) const override;
773 :
774 : std::string as_string () const override;
775 :
776 : bool is_equal (const BaseType &other) const override;
777 :
778 : size_t num_fields () const;
779 :
780 : BaseType *get_field (size_t index) const;
781 :
782 : BaseType *clone () const final override;
783 :
784 : const std::vector<TyVar> &get_fields () const;
785 :
786 : std::string get_name () const override final;
787 :
788 : TupleType *handle_substitions (SubstitutionArgumentMappings &mappings);
789 :
790 : private:
791 : std::vector<TyVar> fields;
792 : };
793 :
794 58942 : class TypeBoundPredicateItem
795 : {
796 : public:
797 : TypeBoundPredicateItem (const TypeBoundPredicate parent,
798 : const Resolver::TraitItemReference *trait_item_ref);
799 :
800 : TypeBoundPredicateItem (const TypeBoundPredicateItem &other);
801 :
802 : TypeBoundPredicateItem &operator= (const TypeBoundPredicateItem &other);
803 :
804 : static TypeBoundPredicateItem error ();
805 :
806 : bool is_error () const;
807 :
808 : BaseType *get_tyty_for_receiver (const TyTy::BaseType *receiver);
809 :
810 : const Resolver::TraitItemReference *get_raw_item () const;
811 :
812 : bool needs_implementation () const;
813 :
814 : const TypeBoundPredicate *get_parent () const;
815 :
816 : location_t get_locus () const;
817 :
818 : private:
819 : TypeBoundPredicate parent;
820 : const Resolver::TraitItemReference *trait_item_ref;
821 : };
822 :
823 : // https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.VariantDef.html
824 : class VariantDef
825 : {
826 : public:
827 : enum VariantType
828 : {
829 : NUM,
830 : TUPLE,
831 : STRUCT,
832 : UNIT
833 : };
834 :
835 : static std::string variant_type_string (VariantType type);
836 :
837 : VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident,
838 : tl::optional<std::unique_ptr<HIR::Expr>> &&discriminant);
839 :
840 : VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident,
841 : VariantType type,
842 : tl::optional<std::unique_ptr<HIR::Expr>> &&discriminant,
843 : std::vector<StructFieldType *> fields);
844 :
845 : static VariantDef &get_error_node ();
846 : bool is_error () const;
847 :
848 : HirId get_id () const;
849 : DefId get_defid () const;
850 :
851 : VariantType get_variant_type () const;
852 : bool is_data_variant () const;
853 : bool is_dataless_variant () const;
854 :
855 : std::string get_identifier () const;
856 :
857 : size_t num_fields () const;
858 : StructFieldType *get_field_at_index (size_t index);
859 :
860 : std::vector<StructFieldType *> &get_fields ();
861 :
862 : bool lookup_field (const std::string &lookup, StructFieldType **field_lookup,
863 : size_t *index) const;
864 :
865 : bool has_discriminant () const;
866 :
867 : HIR::Expr &get_discriminant ();
868 : const HIR::Expr &get_discriminant () const;
869 :
870 : std::string as_string () const;
871 :
872 : bool is_equal (const VariantDef &other) const;
873 :
874 : VariantDef *clone () const;
875 :
876 : VariantDef *monomorphized_clone () const;
877 :
878 : const RustIdent &get_ident () const;
879 :
880 : private:
881 : HirId id;
882 : DefId defid;
883 : std::string identifier;
884 : RustIdent ident;
885 : VariantType type;
886 :
887 : // can either be a structure or a discriminant value
888 : tl::optional<std::unique_ptr<HIR::Expr>> discriminant;
889 :
890 : std::vector<StructFieldType *> fields;
891 : };
892 :
893 : class ADTType : public BaseType, public SubstitutionRef
894 : {
895 : public:
896 : static constexpr auto KIND = TypeKind::ADT;
897 :
898 : enum ADTKind
899 : {
900 : STRUCT_STRUCT,
901 : TUPLE_STRUCT,
902 : UNION,
903 : ENUM
904 : };
905 :
906 : enum ReprKind
907 : {
908 : RUST,
909 : C,
910 : INT,
911 : ALIGN,
912 : PACKED,
913 : // TRANSPARENT,
914 : // SIMD,
915 : // ...
916 : };
917 :
918 : // Representation options, specified via attributes e.g. #[repr(packed)]
919 104 : struct ReprOptions
920 : {
921 : ReprKind repr_kind = ReprKind::RUST;
922 :
923 : // For align and pack: 0 = unspecified. Nonzero = byte alignment.
924 : // It is an error for both to be nonzero, this should be caught when
925 : // parsing the #[repr] attribute.
926 : unsigned char align = 0;
927 : unsigned char pack = 0;
928 : BaseType *repr = nullptr;
929 : };
930 :
931 : ADTType (DefId id, HirId ref, std::string identifier, RustIdent ident,
932 : ADTKind adt_kind, std::vector<VariantDef *> variants,
933 : std::vector<SubstitutionParamMapping> subst_refs,
934 : SubstitutionArgumentMappings generic_arguments
935 : = SubstitutionArgumentMappings::error (),
936 : RegionConstraints region_constraints = RegionConstraints{},
937 : std::set<HirId> refs = std::set<HirId> ());
938 :
939 : ADTType (DefId id, HirId ref, HirId ty_ref, std::string identifier,
940 : RustIdent ident, ADTKind adt_kind,
941 : std::vector<VariantDef *> variants,
942 : std::vector<SubstitutionParamMapping> subst_refs,
943 : SubstitutionArgumentMappings generic_arguments
944 : = SubstitutionArgumentMappings::error (),
945 : RegionConstraints region_constraints = RegionConstraints{},
946 : std::set<HirId> refs = std::set<HirId> ());
947 :
948 : ADTType (DefId id, HirId ref, HirId ty_ref, std::string identifier,
949 : RustIdent ident, ADTKind adt_kind,
950 : std::vector<VariantDef *> variants,
951 : std::vector<SubstitutionParamMapping> subst_refs, ReprOptions repr,
952 : SubstitutionArgumentMappings generic_arguments
953 : = SubstitutionArgumentMappings::error (),
954 : RegionConstraints region_constraints = RegionConstraints{},
955 : std::set<HirId> refs = std::set<HirId> ());
956 :
957 326382 : ADTKind get_adt_kind () const { return adt_kind; }
958 :
959 198081 : ReprOptions get_repr_options () const { return repr; }
960 :
961 917 : bool is_struct_struct () const { return adt_kind == STRUCT_STRUCT; }
962 :
963 750 : bool is_tuple_struct () const { return adt_kind == TUPLE_STRUCT; }
964 :
965 19296 : bool is_union () const { return adt_kind == UNION; }
966 :
967 1014647 : bool is_enum () const { return adt_kind == ENUM; }
968 :
969 : void accept_vis (TyVisitor &vis) override;
970 :
971 : void accept_vis (TyConstVisitor &vis) const override;
972 :
973 : std::string as_string () const override;
974 :
975 : bool is_equal (const BaseType &other) const override;
976 :
977 243836 : std::string get_identifier () const { return identifier; }
978 :
979 2177000 : std::string get_name () const override final
980 : {
981 2177000 : return identifier + subst_as_string ();
982 : }
983 :
984 : DefId get_id () const;
985 :
986 : BaseType *clone () const final override;
987 :
988 543375 : size_t number_of_variants () const { return variants.size (); }
989 :
990 140903 : std::vector<VariantDef *> &get_variants () { return variants; }
991 :
992 1691064 : const std::vector<VariantDef *> &get_variants () const { return variants; }
993 :
994 6 : bool lookup_variant (const std::string &lookup,
995 : VariantDef **found_variant) const
996 : {
997 13 : for (auto &variant : variants)
998 : {
999 10 : if (variant->get_identifier ().compare (lookup) == 0)
1000 : {
1001 3 : *found_variant = variant;
1002 3 : return true;
1003 : }
1004 : }
1005 : return false;
1006 : }
1007 :
1008 8357 : bool lookup_variant_by_id (HirId id, VariantDef **found_variant,
1009 : int *index = nullptr) const
1010 : {
1011 8357 : int i = 0;
1012 15802 : for (auto &variant : variants)
1013 : {
1014 15802 : if (variant->get_id () == id)
1015 : {
1016 8357 : if (index != nullptr)
1017 4427 : *index = i;
1018 :
1019 8357 : *found_variant = variant;
1020 8357 : return true;
1021 : }
1022 7445 : i++;
1023 : }
1024 : return false;
1025 : }
1026 :
1027 : ADTType *
1028 : handle_substitions (SubstitutionArgumentMappings &mappings) override final;
1029 :
1030 : private:
1031 : DefId id;
1032 : std::string identifier;
1033 : std::vector<VariantDef *> variants;
1034 : ADTType::ADTKind adt_kind;
1035 : ReprOptions repr;
1036 : };
1037 :
1038 51879 : class FnParam
1039 : {
1040 : public:
1041 78877 : FnParam (std::unique_ptr<HIR::Pattern> pattern, BaseType *type)
1042 78877 : : pattern (std::move (pattern)), type (type)
1043 : {}
1044 :
1045 : FnParam (const FnParam &) = delete;
1046 51879 : FnParam (FnParam &&) = default;
1047 : FnParam &operator= (FnParam &&) = default;
1048 :
1049 16650 : HIR::Pattern &get_pattern () { return *pattern; }
1050 142886 : const HIR::Pattern &get_pattern () const { return *pattern; }
1051 :
1052 12761 : bool has_pattern () { return pattern != nullptr; }
1053 227840 : BaseType *get_type () const { return type; }
1054 13262 : void set_type (BaseType *new_type) { type = new_type; }
1055 :
1056 20345 : FnParam clone () const
1057 : {
1058 20345 : return FnParam (pattern->clone_pattern (), type->clone ());
1059 : }
1060 :
1061 0 : FnParam monomorphized_clone () const
1062 : {
1063 0 : return FnParam (pattern->clone_pattern (), type->monomorphized_clone ());
1064 : }
1065 :
1066 : private:
1067 : std::unique_ptr<HIR::Pattern> pattern;
1068 : BaseType *type;
1069 : };
1070 :
1071 : class FnType : public CallableTypeInterface, public SubstitutionRef
1072 : {
1073 : public:
1074 : static constexpr auto KIND = TypeKind::FNDEF;
1075 :
1076 : static const uint8_t FNTYPE_DEFAULT_FLAGS = 0x00;
1077 : static const uint8_t FNTYPE_IS_METHOD_FLAG = 0x01;
1078 : static const uint8_t FNTYPE_IS_EXTERN_FLAG = 0x02;
1079 : static const uint8_t FNTYPE_IS_VARADIC_FLAG = 0X04;
1080 : static const uint8_t FNTYPE_IS_SYN_CONST_FLAG = 0X08;
1081 :
1082 41029 : FnType (HirId ref, DefId id, std::string identifier, RustIdent ident,
1083 : uint8_t flags, ABI abi, std::vector<FnParam> params, BaseType *type,
1084 : std::vector<SubstitutionParamMapping> subst_refs,
1085 : SubstitutionArgumentMappings substitution_argument_mappings,
1086 : RegionConstraints region_constraints,
1087 : std::set<HirId> refs = std::set<HirId> ())
1088 41029 : : CallableTypeInterface (ref, ref, TypeKind::FNDEF, ident, refs),
1089 : SubstitutionRef (std::move (subst_refs), substitution_argument_mappings,
1090 : region_constraints),
1091 41029 : params (std::move (params)), type (type), flags (flags),
1092 82058 : identifier (identifier), id (id), abi (abi)
1093 : {
1094 41029 : LocalDefId local_def_id = id.localDefId;
1095 41029 : rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
1096 41029 : }
1097 :
1098 13817 : FnType (HirId ref, HirId ty_ref, DefId id, std::string identifier,
1099 : RustIdent ident, uint8_t flags, ABI abi, std::vector<FnParam> params,
1100 : BaseType *type, std::vector<SubstitutionParamMapping> subst_refs,
1101 : SubstitutionArgumentMappings substitution_argument_mappings,
1102 : RegionConstraints region_constraints,
1103 : std::set<HirId> refs = std::set<HirId> ())
1104 13817 : : CallableTypeInterface (ref, ty_ref, TypeKind::FNDEF, ident, refs),
1105 : SubstitutionRef (std::move (subst_refs), substitution_argument_mappings,
1106 : region_constraints),
1107 13817 : params (std::move (params)), type (type), flags (flags),
1108 27634 : identifier (identifier), id (id), abi (abi)
1109 : {
1110 13817 : LocalDefId local_def_id = id.localDefId;
1111 13817 : rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
1112 13817 : }
1113 :
1114 : FnType (const FnType &) = delete;
1115 : FnType (FnType &&) = default;
1116 :
1117 : void accept_vis (TyVisitor &vis) override;
1118 : void accept_vis (TyConstVisitor &vis) const override;
1119 :
1120 : std::string as_string () const override;
1121 :
1122 65381 : std::string get_name () const override final { return as_string (); }
1123 :
1124 69178 : std::string get_identifier () const { return identifier; }
1125 :
1126 : bool is_equal (const BaseType &other) const override;
1127 :
1128 98889 : size_t num_params () const { return params.size (); }
1129 :
1130 85930 : bool is_method () const
1131 : {
1132 85930 : if (num_params () == 0)
1133 : return false;
1134 :
1135 47658 : return (flags & FNTYPE_IS_METHOD_FLAG) != 0;
1136 : }
1137 :
1138 : bool is_extern () const { return (flags & FNTYPE_IS_EXTERN_FLAG) != 0; }
1139 :
1140 28574 : bool is_variadic () const { return (flags & FNTYPE_IS_VARADIC_FLAG) != 0; }
1141 :
1142 10737 : bool is_syn_constant () const
1143 : {
1144 10737 : return (flags & FNTYPE_IS_SYN_CONST_FLAG) != 0;
1145 : }
1146 :
1147 58726 : DefId get_id () const { return id; }
1148 :
1149 : // get the Self type for the method
1150 41214 : BaseType *get_self_type () const
1151 : {
1152 82428 : rust_assert (is_method ());
1153 82428 : return param_at (0).get_type ();
1154 : }
1155 :
1156 16042 : std::vector<FnParam> &get_params () { return params; }
1157 :
1158 27641 : const std::vector<FnParam> &get_params () const { return params; }
1159 :
1160 40473 : FnParam ¶m_at (size_t idx) { return params.at (idx); }
1161 :
1162 58216 : const FnParam ¶m_at (size_t idx) const { return params.at (idx); }
1163 :
1164 : BaseType *clone () const final override;
1165 :
1166 : FnType *
1167 : handle_substitions (SubstitutionArgumentMappings &mappings) override final;
1168 :
1169 32619 : ABI get_abi () const { return abi; }
1170 0 : uint8_t get_flags () const { return flags; }
1171 :
1172 1457 : WARN_UNUSED_RESULT size_t get_num_params () const override
1173 : {
1174 1457 : return params.size ();
1175 : }
1176 :
1177 22 : WARN_UNUSED_RESULT BaseType *get_param_type_at (size_t index) const override
1178 : {
1179 22 : return param_at (index).get_type ();
1180 : }
1181 :
1182 142260 : WARN_UNUSED_RESULT BaseType *get_return_type () const override
1183 : {
1184 142260 : return type;
1185 : }
1186 :
1187 : private:
1188 : std::vector<FnParam> params;
1189 : BaseType *type;
1190 : uint8_t flags;
1191 : std::string identifier;
1192 : DefId id;
1193 : ABI abi;
1194 : };
1195 :
1196 : class FnPtr : public CallableTypeInterface
1197 : {
1198 : public:
1199 : static constexpr auto KIND = TypeKind::FNPTR;
1200 :
1201 63 : FnPtr (HirId ref, location_t locus, std::vector<TyVar> params,
1202 : TyVar result_type, ABI abi, Unsafety unsafety,
1203 : std::set<HirId> refs = std::set<HirId> ())
1204 63 : : CallableTypeInterface (ref, ref, TypeKind::FNPTR,
1205 63 : {Resolver::CanonicalPath::create_empty (), locus},
1206 : refs),
1207 63 : params (std::move (params)), result_type (result_type), abi (abi),
1208 126 : unsafety (unsafety)
1209 63 : {}
1210 :
1211 1488 : FnPtr (HirId ref, HirId ty_ref, location_t locus, std::vector<TyVar> params,
1212 : TyVar result_type, ABI abi, Unsafety unsafety,
1213 : std::set<HirId> refs = std::set<HirId> ())
1214 1488 : : CallableTypeInterface (ref, ty_ref, TypeKind::FNPTR,
1215 1488 : {Resolver::CanonicalPath::create_empty (), locus},
1216 : refs),
1217 2976 : params (params), result_type (result_type), abi (abi), unsafety (unsafety)
1218 1488 : {}
1219 :
1220 9267 : std::string get_name () const override final { return as_string (); }
1221 :
1222 8 : WARN_UNUSED_RESULT size_t get_num_params () const override
1223 : {
1224 8 : return params.size ();
1225 : }
1226 :
1227 193 : WARN_UNUSED_RESULT BaseType *get_param_type_at (size_t index) const override
1228 : {
1229 193 : return params.at (index).get_tyty ();
1230 : }
1231 :
1232 14478 : WARN_UNUSED_RESULT BaseType *get_return_type () const override
1233 : {
1234 14478 : return result_type.get_tyty ();
1235 : }
1236 :
1237 0 : const TyVar &get_var_return_type () const { return result_type; }
1238 :
1239 446 : size_t num_params () const { return params.size (); }
1240 :
1241 : void accept_vis (TyVisitor &vis) override;
1242 : void accept_vis (TyConstVisitor &vis) const override;
1243 :
1244 : std::string as_string () const override;
1245 :
1246 : bool is_equal (const BaseType &other) const override;
1247 :
1248 : BaseType *clone () const final override;
1249 :
1250 20 : std::vector<TyVar> &get_params () { return params; }
1251 13759 : const std::vector<TyVar> &get_params () const { return params; }
1252 :
1253 9638 : ABI get_abi () const { return abi; }
1254 :
1255 11068 : Unsafety get_unsafety () const { return unsafety; }
1256 :
1257 : FnPtr *handle_substitions (SubstitutionArgumentMappings &mappings);
1258 :
1259 : private:
1260 : std::vector<TyVar> params;
1261 : TyVar result_type;
1262 : ABI abi;
1263 : Unsafety unsafety;
1264 : };
1265 :
1266 : class ClosureType : public CallableTypeInterface, public SubstitutionRef
1267 : {
1268 : public:
1269 : static constexpr auto KIND = TypeKind::CLOSURE;
1270 :
1271 65 : ClosureType (HirId ref, DefId id, RustIdent ident, TupleType *parameters,
1272 : TyVar result_type,
1273 : std::vector<SubstitutionParamMapping> subst_refs,
1274 : std::set<NodeId> captures,
1275 : std::set<HirId> refs = std::set<HirId> (),
1276 : std::vector<TypeBoundPredicate> specified_bounds
1277 : = std::vector<TypeBoundPredicate> ())
1278 65 : : CallableTypeInterface (ref, ref, TypeKind::CLOSURE, ident, refs),
1279 : SubstitutionRef (std::move (subst_refs),
1280 130 : SubstitutionArgumentMappings::error (),
1281 : {}), // TODO: check region constraints
1282 65 : parameters (parameters), result_type (std::move (result_type)), id (id),
1283 130 : captures (captures)
1284 : {
1285 65 : LocalDefId local_def_id = id.localDefId;
1286 65 : rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
1287 65 : inherit_bounds (specified_bounds);
1288 65 : }
1289 :
1290 657 : ClosureType (HirId ref, HirId ty_ref, RustIdent ident, DefId id,
1291 : TupleType *parameters, TyVar result_type,
1292 : std::vector<SubstitutionParamMapping> subst_refs,
1293 : std::set<NodeId> captures,
1294 : std::set<HirId> refs = std::set<HirId> (),
1295 : std::vector<TypeBoundPredicate> specified_bounds
1296 : = std::vector<TypeBoundPredicate> ())
1297 657 : : CallableTypeInterface (ref, ty_ref, TypeKind::CLOSURE, ident, refs),
1298 : SubstitutionRef (std::move (subst_refs),
1299 1314 : SubstitutionArgumentMappings::error (), {}), // TODO
1300 657 : parameters (parameters), result_type (std::move (result_type)), id (id),
1301 1314 : captures (captures)
1302 : {
1303 657 : LocalDefId local_def_id = id.localDefId;
1304 657 : rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
1305 657 : inherit_bounds (specified_bounds);
1306 657 : }
1307 :
1308 : void accept_vis (TyVisitor &vis) override;
1309 : void accept_vis (TyConstVisitor &vis) const override;
1310 :
1311 0 : WARN_UNUSED_RESULT size_t get_num_params () const override
1312 : {
1313 0 : return parameters->num_fields ();
1314 : }
1315 :
1316 0 : WARN_UNUSED_RESULT BaseType *get_param_type_at (size_t index) const override
1317 : {
1318 0 : return parameters->get_field (index);
1319 : }
1320 :
1321 0 : WARN_UNUSED_RESULT BaseType *get_return_type () const override
1322 : {
1323 0 : return result_type.get_tyty ();
1324 : }
1325 :
1326 : std::string as_string () const override;
1327 2393 : std::string get_name () const override final { return as_string (); }
1328 :
1329 : bool is_equal (const BaseType &other) const override;
1330 :
1331 : BaseType *clone () const final override;
1332 :
1333 : ClosureType *
1334 : handle_substitions (SubstitutionArgumentMappings &mappings) override final;
1335 :
1336 642 : TyTy::TupleType &get_parameters () const { return *parameters; }
1337 644 : TyTy::BaseType &get_result_type () const { return *result_type.get_tyty (); }
1338 :
1339 390 : DefId get_def_id () const { return id; }
1340 :
1341 365 : const std::set<NodeId> &get_captures () const { return captures; }
1342 :
1343 : private:
1344 : TyTy::TupleType *parameters;
1345 : TyVar result_type;
1346 : DefId id;
1347 : std::set<NodeId> captures;
1348 : };
1349 :
1350 4728 : class ArrayType : public BaseType
1351 : {
1352 : public:
1353 : static constexpr auto KIND = TypeKind::ARRAY;
1354 :
1355 1135 : ArrayType (HirId ref, location_t locus, TyVar capacity, TyVar base,
1356 : std::set<HirId> refs = std::set<HirId> ())
1357 1135 : : BaseType (ref, ref, TypeKind::ARRAY,
1358 1135 : {Resolver::CanonicalPath::create_empty (), locus}, refs),
1359 2270 : element_type (base), capacity (capacity)
1360 1135 : {}
1361 :
1362 2278 : ArrayType (HirId ref, HirId ty_ref, location_t locus, TyVar capacity,
1363 : TyVar base, std::set<HirId> refs = std::set<HirId> ())
1364 2278 : : BaseType (ref, ty_ref, TypeKind::ARRAY,
1365 2278 : {Resolver::CanonicalPath::create_empty (), locus}, refs),
1366 4556 : element_type (base), capacity (capacity)
1367 2278 : {}
1368 :
1369 : void accept_vis (TyVisitor &vis) override;
1370 : void accept_vis (TyConstVisitor &vis) const override;
1371 :
1372 : std::string as_string () const override;
1373 :
1374 54412 : std::string get_name () const override final { return as_string (); }
1375 :
1376 : bool is_equal (const BaseType &other) const override;
1377 :
1378 : BaseType *get_element_type () const;
1379 : const TyVar &get_var_element_type () const;
1380 :
1381 : BaseType *clone () const final override;
1382 :
1383 : BaseType *get_capacity () const;
1384 1 : const TyVar &get_capacity_var () const { return capacity; }
1385 :
1386 : ArrayType *handle_substitions (SubstitutionArgumentMappings &mappings);
1387 :
1388 : private:
1389 : TyVar element_type;
1390 : TyVar capacity;
1391 : };
1392 :
1393 7658 : class SliceType : public BaseType
1394 : {
1395 : public:
1396 : static constexpr auto KIND = TypeKind::SLICE;
1397 :
1398 1263 : SliceType (HirId ref, location_t locus, TyVar base,
1399 : std::set<HirId> refs = std::set<HirId> ())
1400 1263 : : BaseType (ref, ref, TypeKind::SLICE,
1401 1263 : {Resolver::CanonicalPath::create_empty (), locus}, refs),
1402 2526 : element_type (base)
1403 1263 : {}
1404 :
1405 57713 : SliceType (HirId ref, HirId ty_ref, location_t locus, TyVar base,
1406 : std::set<HirId> refs = std::set<HirId> ())
1407 57713 : : BaseType (ref, ty_ref, TypeKind::SLICE,
1408 57713 : {Resolver::CanonicalPath::create_empty (), locus}, refs),
1409 115426 : element_type (base)
1410 57713 : {}
1411 :
1412 : void accept_vis (TyVisitor &vis) override;
1413 : void accept_vis (TyConstVisitor &vis) const override;
1414 :
1415 : std::string as_string () const override;
1416 :
1417 57873 : std::string get_name () const override final { return as_string (); }
1418 :
1419 : bool is_equal (const BaseType &other) const override;
1420 :
1421 : BaseType *get_element_type () const;
1422 : const TyVar &get_var_element_type () const;
1423 :
1424 : BaseType *clone () const final override;
1425 :
1426 : SliceType *handle_substitions (SubstitutionArgumentMappings &mappings);
1427 :
1428 : private:
1429 : TyVar element_type;
1430 : };
1431 :
1432 : class BoolType : public BaseType
1433 : {
1434 : public:
1435 : static constexpr auto KIND = TypeKind::BOOL;
1436 :
1437 : BoolType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
1438 : BoolType (HirId ref, HirId ty_ref, std::set<HirId> refs = std::set<HirId> ());
1439 :
1440 : void accept_vis (TyVisitor &vis) override;
1441 : void accept_vis (TyConstVisitor &vis) const override;
1442 :
1443 : std::string as_string () const override;
1444 :
1445 : std::string get_name () const override final;
1446 :
1447 : BaseType *clone () const final override;
1448 : };
1449 :
1450 : class IntType : public BaseType
1451 : {
1452 : public:
1453 : enum IntKind
1454 : {
1455 : I8,
1456 : I16,
1457 : I32,
1458 : I64,
1459 : I128
1460 : };
1461 :
1462 : static constexpr auto KIND = TypeKind::INT;
1463 :
1464 : IntType (HirId ref, IntKind kind, std::set<HirId> refs = std::set<HirId> ());
1465 : IntType (HirId ref, HirId ty_ref, IntKind kind,
1466 : std::set<HirId> refs = std::set<HirId> ());
1467 :
1468 : void accept_vis (TyVisitor &vis) override;
1469 : void accept_vis (TyConstVisitor &vis) const override;
1470 :
1471 : std::string as_string () const override;
1472 :
1473 : std::string get_name () const override final;
1474 :
1475 : IntKind get_int_kind () const;
1476 :
1477 : BaseType *clone () const final override;
1478 :
1479 : bool is_equal (const BaseType &other) const override;
1480 :
1481 : private:
1482 : IntKind int_kind;
1483 : };
1484 :
1485 : class UintType : public BaseType
1486 : {
1487 : public:
1488 : static constexpr auto KIND = TypeKind::UINT;
1489 :
1490 : enum UintKind
1491 : {
1492 : U8,
1493 : U16,
1494 : U32,
1495 : U64,
1496 : U128
1497 : };
1498 :
1499 : UintType (HirId ref, UintKind kind,
1500 : std::set<HirId> refs = std::set<HirId> ());
1501 : UintType (HirId ref, HirId ty_ref, UintKind kind,
1502 : std::set<HirId> refs = std::set<HirId> ());
1503 :
1504 : void accept_vis (TyVisitor &vis) override;
1505 : void accept_vis (TyConstVisitor &vis) const override;
1506 :
1507 : std::string as_string () const override;
1508 :
1509 : std::string get_name () const override final;
1510 :
1511 : UintKind get_uint_kind () const;
1512 :
1513 : BaseType *clone () const final override;
1514 :
1515 : bool is_equal (const BaseType &other) const override;
1516 :
1517 : private:
1518 : UintKind uint_kind;
1519 : };
1520 :
1521 : class FloatType : public BaseType
1522 : {
1523 : public:
1524 : static constexpr auto KIND = TypeKind::FLOAT;
1525 :
1526 : enum FloatKind
1527 : {
1528 : F32,
1529 : F64
1530 : };
1531 :
1532 : FloatType (HirId ref, FloatKind kind,
1533 : std::set<HirId> refs = std::set<HirId> ());
1534 : FloatType (HirId ref, HirId ty_ref, FloatKind kind,
1535 : std::set<HirId> refs = std::set<HirId> ());
1536 :
1537 : void accept_vis (TyVisitor &vis) override;
1538 : void accept_vis (TyConstVisitor &vis) const override;
1539 :
1540 : std::string as_string () const override;
1541 : std::string get_name () const override final;
1542 :
1543 : FloatKind get_float_kind () const;
1544 :
1545 : BaseType *clone () const final override;
1546 :
1547 : bool is_equal (const BaseType &other) const override;
1548 :
1549 : private:
1550 : FloatKind float_kind;
1551 : };
1552 :
1553 : class USizeType : public BaseType
1554 : {
1555 : public:
1556 : static constexpr auto KIND = TypeKind::USIZE;
1557 :
1558 : USizeType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
1559 : USizeType (HirId ref, HirId ty_ref,
1560 : std::set<HirId> refs = std::set<HirId> ());
1561 :
1562 : void accept_vis (TyVisitor &vis) override;
1563 : void accept_vis (TyConstVisitor &vis) const override;
1564 :
1565 : std::string as_string () const override;
1566 : std::string get_name () const override final;
1567 :
1568 : BaseType *clone () const final override;
1569 : };
1570 :
1571 : class ISizeType : public BaseType
1572 : {
1573 : public:
1574 : static constexpr auto KIND = TypeKind::ISIZE;
1575 :
1576 : ISizeType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
1577 : ISizeType (HirId ref, HirId ty_ref,
1578 : std::set<HirId> refs = std::set<HirId> ());
1579 :
1580 : void accept_vis (TyVisitor &vis) override;
1581 : void accept_vis (TyConstVisitor &vis) const override;
1582 :
1583 : std::string as_string () const override;
1584 : std::string get_name () const override final;
1585 :
1586 : BaseType *clone () const final override;
1587 : };
1588 :
1589 : class CharType : public BaseType
1590 : {
1591 : public:
1592 : static constexpr auto KIND = TypeKind::CHAR;
1593 :
1594 : CharType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
1595 : CharType (HirId ref, HirId ty_ref, std::set<HirId> refs = std::set<HirId> ());
1596 :
1597 : void accept_vis (TyVisitor &vis) override;
1598 : void accept_vis (TyConstVisitor &vis) const override;
1599 :
1600 : std::string as_string () const override;
1601 : std::string get_name () const override final;
1602 :
1603 : BaseType *clone () const final override;
1604 : };
1605 :
1606 : class StrType : public BaseType
1607 : {
1608 : public:
1609 : static constexpr auto KIND = TypeKind::STR;
1610 :
1611 : StrType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
1612 : StrType (HirId ref, HirId ty_ref, std::set<HirId> refs = std::set<HirId> ());
1613 :
1614 : std::string get_name () const override final;
1615 :
1616 : void accept_vis (TyVisitor &vis) override;
1617 : void accept_vis (TyConstVisitor &vis) const override;
1618 :
1619 : std::string as_string () const override;
1620 :
1621 : bool is_equal (const BaseType &other) const override;
1622 :
1623 : BaseType *clone () const final override;
1624 : };
1625 :
1626 : class DynamicObjectType : public BaseType
1627 : {
1628 : public:
1629 : static constexpr auto KIND = TypeKind::DYNAMIC;
1630 :
1631 : DynamicObjectType (HirId ref, RustIdent ident,
1632 : std::vector<TypeBoundPredicate> specified_bounds,
1633 : std::set<HirId> refs = std::set<HirId> ());
1634 :
1635 : DynamicObjectType (HirId ref, HirId ty_ref, RustIdent ident,
1636 : std::vector<TypeBoundPredicate> specified_bounds,
1637 : std::set<HirId> refs = std::set<HirId> ());
1638 :
1639 : void accept_vis (TyVisitor &vis) override;
1640 : void accept_vis (TyConstVisitor &vis) const override;
1641 :
1642 : std::string as_string () const override;
1643 :
1644 : bool is_equal (const BaseType &other) const override;
1645 :
1646 : BaseType *clone () const final override;
1647 :
1648 : std::string get_name () const override final;
1649 :
1650 : // this returns a flat list of items including super trait bounds
1651 : const std::vector<
1652 : std::pair<const Resolver::TraitItemReference *, const TypeBoundPredicate *>>
1653 : get_object_items () const;
1654 : };
1655 :
1656 36356 : class ReferenceType : public BaseType
1657 : {
1658 : public:
1659 : static constexpr auto KIND = REF;
1660 :
1661 : ReferenceType (HirId ref, TyVar base, Mutability mut,
1662 : Region region = Region::make_anonymous (),
1663 : std::set<HirId> refs = std::set<HirId> ());
1664 : ReferenceType (HirId ref, HirId ty_ref, TyVar base, Mutability mut,
1665 : Region region = Region::make_anonymous (),
1666 : std::set<HirId> refs = std::set<HirId> ());
1667 :
1668 : BaseType *get_base () const;
1669 : const TyVar &get_var_element_type () const;
1670 :
1671 : void accept_vis (TyVisitor &vis) override;
1672 : void accept_vis (TyConstVisitor &vis) const override;
1673 :
1674 : std::string as_string () const override;
1675 :
1676 : std::string get_name () const override final;
1677 :
1678 : bool is_equal (const BaseType &other) const override;
1679 :
1680 : BaseType *clone () const final override;
1681 :
1682 : ReferenceType *handle_substitions (SubstitutionArgumentMappings &mappings);
1683 :
1684 : Mutability mutability () const;
1685 : bool is_mutable () const;
1686 :
1687 : WARN_UNUSED_RESULT Region get_region () const;
1688 : void set_region (Region region);
1689 :
1690 : bool is_dyn_object () const;
1691 : bool is_dyn_slice_type (const TyTy::SliceType **slice = nullptr) const;
1692 : bool is_dyn_str_type (const TyTy::StrType **str = nullptr) const;
1693 : bool is_dyn_obj_type (const TyTy::DynamicObjectType **dyn = nullptr) const;
1694 : bool is_dyn_cstr_type (const TyTy::ADTType **adt = nullptr) const;
1695 :
1696 : private:
1697 : TyVar base;
1698 : Mutability mut;
1699 : Region region;
1700 : };
1701 :
1702 32426 : class PointerType : public BaseType
1703 : {
1704 : public:
1705 : static constexpr auto KIND = TypeKind::POINTER;
1706 :
1707 : PointerType (HirId ref, TyVar base, Mutability mut,
1708 : std::set<HirId> refs = std::set<HirId> ());
1709 : PointerType (HirId ref, HirId ty_ref, TyVar base, Mutability mut,
1710 : std::set<HirId> refs = std::set<HirId> ());
1711 :
1712 : BaseType *get_base () const;
1713 : const TyVar &get_var_element_type () const;
1714 :
1715 : void accept_vis (TyVisitor &vis) override;
1716 : void accept_vis (TyConstVisitor &vis) const override;
1717 :
1718 : std::string as_string () const override;
1719 : std::string get_name () const override final;
1720 :
1721 : bool is_equal (const BaseType &other) const override;
1722 :
1723 : BaseType *clone () const final override;
1724 :
1725 : PointerType *handle_substitions (SubstitutionArgumentMappings &mappings);
1726 :
1727 : Mutability mutability () const;
1728 : bool is_mutable () const;
1729 : bool is_const () const;
1730 : bool is_dyn_object () const;
1731 : bool is_dyn_slice_type (const TyTy::SliceType **slice = nullptr) const;
1732 : bool is_dyn_str_type (const TyTy::StrType **str = nullptr) const;
1733 : bool is_dyn_obj_type (const TyTy::DynamicObjectType **dyn = nullptr) const;
1734 :
1735 : private:
1736 : TyVar base;
1737 : Mutability mut;
1738 : };
1739 :
1740 : // https://doc.rust-lang.org/std/primitive.never.html
1741 : //
1742 : // Since the `!` type is really complicated and it is even still unstable
1743 : // in rustc, only fairly limited support for this type is introduced here.
1744 : // Unification between `!` and ANY other type (including `<T?>`) is simply
1745 : // not allowed. If it is needed, it should be handled manually. For example,
1746 : // unifying `!` with other types is very necessary when resolving types of
1747 : // `if/else` expressions.
1748 : //
1749 : // See related discussion at https://github.com/Rust-GCC/gccrs/pull/364
1750 : class NeverType : public BaseType
1751 : {
1752 : public:
1753 : static constexpr auto KIND = TypeKind::NEVER;
1754 :
1755 : NeverType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
1756 :
1757 : NeverType (HirId ref, HirId ty_ref,
1758 : std::set<HirId> refs = std::set<HirId> ());
1759 :
1760 : void accept_vis (TyVisitor &vis) override;
1761 :
1762 : void accept_vis (TyConstVisitor &vis) const override;
1763 :
1764 : std::string as_string () const override;
1765 :
1766 : BaseType *clone () const final override;
1767 :
1768 : std::string get_name () const override final;
1769 : };
1770 :
1771 : // used at the type in associated types in traits
1772 : // see: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
1773 : class PlaceholderType : public BaseType
1774 : {
1775 : public:
1776 : static constexpr auto KIND = TypeKind::PLACEHOLDER;
1777 :
1778 : PlaceholderType (std::string symbol, DefId id, HirId ref,
1779 : std::set<HirId> refs = std::set<HirId> ());
1780 : PlaceholderType (std::string symbol, DefId id, HirId ref, HirId ty_ref,
1781 : std::set<HirId> refs = std::set<HirId> ());
1782 :
1783 : void accept_vis (TyVisitor &vis) override;
1784 : void accept_vis (TyConstVisitor &vis) const override;
1785 :
1786 : std::string as_string () const override;
1787 :
1788 : BaseType *clone () const final override;
1789 :
1790 : std::string get_name () const override final;
1791 :
1792 : std::string get_symbol () const;
1793 :
1794 : bool can_resolve () const;
1795 :
1796 : BaseType *resolve () const;
1797 :
1798 : bool is_equal (const BaseType &other) const override;
1799 :
1800 : DefId get_def_id () const;
1801 :
1802 : private:
1803 : std::string symbol;
1804 : DefId defId;
1805 : };
1806 :
1807 : class ProjectionType : public BaseType, public SubstitutionRef
1808 : {
1809 : public:
1810 : static constexpr auto KIND = TypeKind::PROJECTION;
1811 :
1812 : ProjectionType (HirId ref, BaseType *base,
1813 : const Resolver::TraitReference *trait, DefId item,
1814 : std::vector<SubstitutionParamMapping> subst_refs,
1815 : TyTy::BaseType *self,
1816 : SubstitutionArgumentMappings generic_arguments
1817 : = SubstitutionArgumentMappings::error (),
1818 : RegionConstraints region_constraints = {},
1819 : std::set<HirId> refs = std::set<HirId> (),
1820 : size_t num_trait_substitutions = 0);
1821 :
1822 : ProjectionType (HirId ref, HirId ty_ref, BaseType *base,
1823 : const Resolver::TraitReference *trait, DefId item,
1824 : std::vector<SubstitutionParamMapping> subst_refs,
1825 : TyTy::BaseType *self,
1826 : SubstitutionArgumentMappings generic_arguments
1827 : = SubstitutionArgumentMappings::error (),
1828 : RegionConstraints region_constraints = {},
1829 : std::set<HirId> refs = std::set<HirId> (),
1830 : size_t num_trait_substitutions = 0);
1831 :
1832 : void accept_vis (TyVisitor &vis) override;
1833 : void accept_vis (TyConstVisitor &vis) const override;
1834 :
1835 : bool is_trait_position () const;
1836 :
1837 : std::string as_string () const override;
1838 :
1839 : BaseType *clone () const final override;
1840 :
1841 : std::string get_name () const override final;
1842 :
1843 : const BaseType *get () const;
1844 : BaseType *get ();
1845 :
1846 : const BaseType *get_self () const;
1847 : BaseType *get_self ();
1848 126 : void set_self (BaseType *s) { self = s; }
1849 :
1850 : const Resolver::TraitReference *get_trait_ref () const;
1851 :
1852 : DefId get_item_defid () const;
1853 :
1854 : ProjectionType *
1855 : handle_substitions (SubstitutionArgumentMappings &mappings) override final;
1856 :
1857 0 : size_t get_outer_param_count () const override
1858 : {
1859 0 : return num_trait_substitutions;
1860 : }
1861 :
1862 : private:
1863 : BaseType *base;
1864 : const Resolver::TraitReference *trait;
1865 : DefId item;
1866 : TyTy::BaseType *self;
1867 : size_t num_trait_substitutions = 0;
1868 : };
1869 :
1870 : template <>
1871 : WARN_UNUSED_RESULT inline bool
1872 22 : BaseType::is<CallableTypeInterface> () const
1873 : {
1874 22 : auto kind = this->get_kind ();
1875 22 : return kind == FNPTR || kind == FNDEF || kind == CLOSURE;
1876 : }
1877 :
1878 : template <>
1879 : WARN_UNUSED_RESULT inline bool
1880 11 : BaseType::is<const CallableTypeInterface> () const
1881 : {
1882 11 : return this->is<CallableTypeInterface> ();
1883 : }
1884 :
1885 : template <>
1886 : WARN_UNUSED_RESULT inline bool
1887 17 : BaseType::is<SubstitutionRef> () const
1888 : {
1889 17 : auto kind = this->get_kind ();
1890 34 : return kind == FNPTR || kind == FNDEF || kind == CLOSURE || kind == ADT
1891 17 : || kind == PROJECTION;
1892 : }
1893 :
1894 : template <>
1895 : WARN_UNUSED_RESULT inline bool
1896 : BaseType::is<const SubstitutionRef> () const
1897 : {
1898 : return this->is<SubstitutionRef> ();
1899 : }
1900 :
1901 : template <>
1902 : WARN_UNUSED_RESULT inline SubstitutionRef *
1903 16 : BaseType::as<SubstitutionRef> ()
1904 : {
1905 16 : auto kind = this->get_kind ();
1906 16 : switch (kind)
1907 : {
1908 0 : case FNDEF:
1909 0 : return static_cast<FnType *> (this);
1910 0 : case CLOSURE:
1911 0 : return static_cast<ClosureType *> (this);
1912 16 : case ADT:
1913 16 : return static_cast<ADTType *> (this);
1914 0 : case PROJECTION:
1915 0 : return static_cast<ProjectionType *> (this);
1916 0 : default:
1917 0 : rust_unreachable ();
1918 : }
1919 : }
1920 :
1921 : template <>
1922 : WARN_UNUSED_RESULT inline const SubstitutionRef *
1923 5 : BaseType::as<const SubstitutionRef> () const
1924 : {
1925 5 : auto kind = this->get_kind ();
1926 5 : switch (kind)
1927 : {
1928 0 : case FNDEF:
1929 0 : return static_cast<const FnType *> (this);
1930 0 : case CLOSURE:
1931 0 : return static_cast<const ClosureType *> (this);
1932 5 : case ADT:
1933 5 : return static_cast<const ADTType *> (this);
1934 0 : case PROJECTION:
1935 0 : return static_cast<const ProjectionType *> (this);
1936 0 : default:
1937 0 : rust_unreachable ();
1938 : }
1939 : }
1940 :
1941 : template <>
1942 : WARN_UNUSED_RESULT inline SubstitutionRef *
1943 17 : BaseType::try_as<SubstitutionRef> ()
1944 : {
1945 17 : if (this->is<SubstitutionRef> ())
1946 : {
1947 16 : return this->as<SubstitutionRef> ();
1948 : }
1949 : return nullptr;
1950 : }
1951 :
1952 : template <>
1953 : WARN_UNUSED_RESULT inline const SubstitutionRef *
1954 : BaseType::try_as<const SubstitutionRef> () const
1955 : {
1956 : if (this->is<const SubstitutionRef> ())
1957 : {
1958 : return this->as<const SubstitutionRef> ();
1959 : }
1960 : return nullptr;
1961 : }
1962 :
1963 : WARN_UNUSED_RESULT tl::optional<BaseType *>
1964 : try_get_box_inner_type (BaseType *base);
1965 :
1966 : } // namespace TyTy
1967 : } // namespace Rust
1968 :
1969 : #endif // RUST_TYTY
|