Debian Codegraph @

gcc/rust/typecheck/rust-tyty.h

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