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_UNIFY
20 : #define RUST_UNIFY
21 :
22 : #include "rust-tyty-util.h"
23 : #include "rust-hir-type-check.h"
24 :
25 : namespace Rust {
26 : namespace Resolver {
27 :
28 : class UnifyRules
29 : {
30 : public:
31 : struct InferenceSite
32 : {
33 85007 : InferenceSite (HirId pref, HirId ptyref, TyTy::BaseGeneric *param,
34 : TyTy::BaseType *infer)
35 85007 : : pref (pref), ptyref (ptyref), param (param), infer (infer)
36 : {}
37 :
38 : HirId pref;
39 : HirId ptyref;
40 : TyTy::BaseGeneric *param;
41 : TyTy::BaseType *infer;
42 : };
43 : struct CommitSite
44 : {
45 2213200 : CommitSite (TyTy::BaseType *lhs, TyTy::BaseType *rhs,
46 : TyTy::BaseType *resolved)
47 2213200 : : lhs (lhs), rhs (rhs), resolved (resolved)
48 : {}
49 :
50 : TyTy::BaseType *lhs;
51 : TyTy::BaseType *rhs;
52 : TyTy::BaseType *resolved;
53 : };
54 :
55 : static TyTy::BaseType *Resolve (TyTy::TyWithLocation lhs,
56 : TyTy::TyWithLocation rhs, location_t locus,
57 : bool commit_flag, bool emit_error, bool infer,
58 : bool check_bounds,
59 : std::vector<CommitSite> &commits,
60 : std::vector<InferenceSite> &infers);
61 :
62 : static void commit (TyTy::BaseType *base, TyTy::BaseType *other,
63 : TyTy::BaseType *resolved);
64 :
65 : protected:
66 : TyTy::BaseType *expect_inference_variable (TyTy::InferType *ltype,
67 : TyTy::BaseType *rtype);
68 : TyTy::BaseType *expect_adt (TyTy::ADTType *ltype, TyTy::BaseType *rtype);
69 : TyTy::BaseType *expect_str (TyTy::StrType *ltype, TyTy::BaseType *rtype);
70 : TyTy::BaseType *expect_reference (TyTy::ReferenceType *ltype,
71 : TyTy::BaseType *rtype);
72 : TyTy::BaseType *expect_pointer (TyTy::PointerType *ltype,
73 : TyTy::BaseType *rtype);
74 : TyTy::BaseType *expect_param (TyTy::ParamType *ltype, TyTy::BaseType *rtype);
75 : TyTy::BaseType *expect_array (TyTy::ArrayType *ltype, TyTy::BaseType *rtype);
76 : TyTy::BaseType *expect_slice (TyTy::SliceType *ltype, TyTy::BaseType *rtype);
77 : TyTy::BaseType *expect_fndef (TyTy::FnType *ltype, TyTy::BaseType *rtype);
78 : TyTy::BaseType *expect_fnptr (TyTy::FnPtr *ltype, TyTy::BaseType *rtype);
79 : TyTy::BaseType *expect_tuple (TyTy::TupleType *ltype, TyTy::BaseType *rtype);
80 : TyTy::BaseType *expect_bool (TyTy::BoolType *ltype, TyTy::BaseType *rtype);
81 : TyTy::BaseType *expect_char (TyTy::CharType *ltype, TyTy::BaseType *rtype);
82 : TyTy::BaseType *expect_int (TyTy::IntType *ltype, TyTy::BaseType *rtype);
83 : TyTy::BaseType *expect_uint (TyTy::UintType *ltype, TyTy::BaseType *rtype);
84 : TyTy::BaseType *expect_float (TyTy::FloatType *ltype, TyTy::BaseType *rtype);
85 : TyTy::BaseType *expect_isize (TyTy::ISizeType *ltype, TyTy::BaseType *rtype);
86 : TyTy::BaseType *expect_usize (TyTy::USizeType *ltype, TyTy::BaseType *rtype);
87 : TyTy::BaseType *expect_never (TyTy::NeverType *ltype, TyTy::BaseType *rtype);
88 : TyTy::BaseType *expect_placeholder (TyTy::PlaceholderType *ltype,
89 : TyTy::BaseType *rtype);
90 : TyTy::BaseType *expect_projection (TyTy::ProjectionType *ltype,
91 : TyTy::BaseType *rtype);
92 : TyTy::BaseType *expect_dyn (TyTy::DynamicObjectType *ltype,
93 : TyTy::BaseType *rtype);
94 : TyTy::BaseType *expect_closure (TyTy::ClosureType *ltype,
95 : TyTy::BaseType *rtype);
96 : TyTy::BaseType *expect_opaque (TyTy::OpaqueType *ltype,
97 : TyTy::BaseType *rtype);
98 : TyTy::BaseType *expect_const (TyTy::BaseConstType *ltype,
99 : TyTy::BaseType *rtype);
100 :
101 : private:
102 : UnifyRules (TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs,
103 : location_t locus, bool commit_flag, bool emit_error, bool infer,
104 : bool check_bounds, std::vector<CommitSite> &commits,
105 : std::vector<InferenceSite> &infers);
106 :
107 : TyTy::BaseType *resolve_subtype (TyTy::TyWithLocation lhs,
108 : TyTy::TyWithLocation rhs);
109 :
110 : void emit_type_mismatch () const;
111 : void emit_abi_mismatch (const TyTy::FnType &expected,
112 : const TyTy::FnType &got) const;
113 :
114 : TyTy::BaseType *go ();
115 :
116 : TyTy::BaseType *get_base ();
117 : TyTy::BaseType *get_other ();
118 :
119 : TyTy::TyWithLocation lhs;
120 : TyTy::TyWithLocation rhs;
121 : location_t locus;
122 : bool commit_flag;
123 : bool emit_error;
124 : bool infer_flag;
125 : bool check_bounds_flag;
126 : std::vector<CommitSite> &commits;
127 : std::vector<InferenceSite> &infers;
128 :
129 : Analysis::Mappings &mappings;
130 : TypeCheckContext &context;
131 : };
132 :
133 : } // namespace Resolver
134 : } // namespace Rust
135 :
136 : #endif // RUST_UNIFY
|