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