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