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