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