1 // Copyright (C) 2020-2023 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 static TyTy::BaseType *Resolve (TyTy::TyWithLocation lhs,
32 TyTy::TyWithLocation rhs, Location locus,
33 bool commit_flag, bool emit_error);
34
35 protected:
36 TyTy::BaseType *expect_inference_variable (TyTy::InferType *ltype,
37 TyTy::BaseType *rtype);
38 TyTy::BaseType *expect_adt (TyTy::ADTType *ltype, TyTy::BaseType *rtype);
39 TyTy::BaseType *expect_str (TyTy::StrType *ltype, TyTy::BaseType *rtype);
40 TyTy::BaseType *expect_reference (TyTy::ReferenceType *ltype,
41 TyTy::BaseType *rtype);
42 TyTy::BaseType *expect_pointer (TyTy::PointerType *ltype,
43 TyTy::BaseType *rtype);
44 TyTy::BaseType *expect_param (TyTy::ParamType *ltype, TyTy::BaseType *rtype);
45 TyTy::BaseType *expect_array (TyTy::ArrayType *ltype, TyTy::BaseType *rtype);
46 TyTy::BaseType *expect_slice (TyTy::SliceType *ltype, TyTy::BaseType *rtype);
47 TyTy::BaseType *expect_fndef (TyTy::FnType *ltype, TyTy::BaseType *rtype);
48 TyTy::BaseType *expect_fnptr (TyTy::FnPtr *ltype, TyTy::BaseType *rtype);
49 TyTy::BaseType *expect_tuple (TyTy::TupleType *ltype, TyTy::BaseType *rtype);
50 TyTy::BaseType *expect_bool (TyTy::BoolType *ltype, TyTy::BaseType *rtype);
51 TyTy::BaseType *expect_char (TyTy::CharType *ltype, TyTy::BaseType *rtype);
52 TyTy::BaseType *expect_int (TyTy::IntType *ltype, TyTy::BaseType *rtype);
53 TyTy::BaseType *expect_uint (TyTy::UintType *ltype, TyTy::BaseType *rtype);
54 TyTy::BaseType *expect_float (TyTy::FloatType *ltype, TyTy::BaseType *rtype);
55 TyTy::BaseType *expect_isize (TyTy::ISizeType *ltype, TyTy::BaseType *rtype);
56 TyTy::BaseType *expect_usize (TyTy::USizeType *ltype, TyTy::BaseType *rtype);
57 TyTy::BaseType *expect_never (TyTy::NeverType *ltype, TyTy::BaseType *rtype);
58 TyTy::BaseType *expect_placeholder (TyTy::PlaceholderType *ltype,
59 TyTy::BaseType *rtype);
60 TyTy::BaseType *expect_projection (TyTy::ProjectionType *ltype,
61 TyTy::BaseType *rtype);
62 TyTy::BaseType *expect_dyn (TyTy::DynamicObjectType *ltype,
63 TyTy::BaseType *rtype);
64 TyTy::BaseType *expect_closure (TyTy::ClosureType *ltype,
65 TyTy::BaseType *rtype);
66
67 private:
68 UnifyRules (TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs,
69 Location locus, bool commit_flag, bool emit_error);
70
71 void emit_type_mismatch () const;
72 void commit (TyTy::BaseType *resolved);
73 TyTy::BaseType *go ();
74
75 TyTy::BaseType *get_base ();
76 TyTy::BaseType *get_other ();
77
78 TyTy::TyWithLocation lhs;
79 TyTy::TyWithLocation rhs;
80 Location locus;
81 bool commit_flag;
82 bool emit_error;
83
84 Analysis::Mappings &mappings;
85 TypeCheckContext &context;
86 };
87
88 } // namespace Resolver
89 } // namespace Rust
90
91 #endif // RUST_UNIFY