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