| 1 |
// Copyright (C) 2020-2025 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 |
HirId pref; |
| 34 |
HirId ptyref; |
| 35 |
TyTy::ParamType *param; |
| 36 |
TyTy::InferType *infer; |
| 37 |
}; |
| 38 |
struct CommitSite |
| 39 |
{ |
| 40 |
TyTy::BaseType *lhs; |
| 41 |
TyTy::BaseType *rhs; |
| 42 |
TyTy::BaseType *resolved; |
| 43 |
}; |
| 44 |
|
| 45 |
static TyTy::BaseType *Resolve (TyTy::TyWithLocation lhs, |
| 46 |
TyTy::TyWithLocation rhs, location_t locus, |
| 47 |
bool commit_flag, bool emit_error, bool infer, |
| 48 |
std::vector<CommitSite> &commits, |
| 49 |
std::vector<InferenceSite> &infers); |
| 50 |
|
| 51 |
static void commit (TyTy::BaseType *base, TyTy::BaseType *other, |
| 52 |
TyTy::BaseType *resolved); |
| 53 |
|
| 54 |
protected: |
| 55 |
TyTy::BaseType *expect_inference_variable (TyTy::InferType *ltype, |
| 56 |
TyTy::BaseType *rtype); |
| 57 |
TyTy::BaseType *expect_adt (TyTy::ADTType *ltype, TyTy::BaseType *rtype); |
| 58 |
TyTy::BaseType *expect_str (TyTy::StrType *ltype, TyTy::BaseType *rtype); |
| 59 |
TyTy::BaseType *expect_reference (TyTy::ReferenceType *ltype, |
| 60 |
TyTy::BaseType *rtype); |
| 61 |
TyTy::BaseType *expect_pointer (TyTy::PointerType *ltype, |
| 62 |
TyTy::BaseType *rtype); |
| 63 |
TyTy::BaseType *expect_param (TyTy::ParamType *ltype, TyTy::BaseType *rtype); |
| 64 |
TyTy::BaseType *expect_array (TyTy::ArrayType *ltype, TyTy::BaseType *rtype); |
| 65 |
TyTy::BaseType *expect_slice (TyTy::SliceType *ltype, TyTy::BaseType *rtype); |
| 66 |
TyTy::BaseType *expect_fndef (TyTy::FnType *ltype, TyTy::BaseType *rtype); |
| 67 |
TyTy::BaseType *expect_fnptr (TyTy::FnPtr *ltype, TyTy::BaseType *rtype); |
| 68 |
TyTy::BaseType *expect_tuple (TyTy::TupleType *ltype, TyTy::BaseType *rtype); |
| 69 |
TyTy::BaseType *expect_bool (TyTy::BoolType *ltype, TyTy::BaseType *rtype); |
| 70 |
TyTy::BaseType *expect_char (TyTy::CharType *ltype, TyTy::BaseType *rtype); |
| 71 |
TyTy::BaseType *expect_int (TyTy::IntType *ltype, TyTy::BaseType *rtype); |
| 72 |
TyTy::BaseType *expect_uint (TyTy::UintType *ltype, TyTy::BaseType *rtype); |
| 73 |
TyTy::BaseType *expect_float (TyTy::FloatType *ltype, TyTy::BaseType *rtype); |
| 74 |
TyTy::BaseType *expect_isize (TyTy::ISizeType *ltype, TyTy::BaseType *rtype); |
| 75 |
TyTy::BaseType *expect_usize (TyTy::USizeType *ltype, TyTy::BaseType *rtype); |
| 76 |
TyTy::BaseType *expect_never (TyTy::NeverType *ltype, TyTy::BaseType *rtype); |
| 77 |
TyTy::BaseType *expect_placeholder (TyTy::PlaceholderType *ltype, |
| 78 |
TyTy::BaseType *rtype); |
| 79 |
TyTy::BaseType *expect_projection (TyTy::ProjectionType *ltype, |
| 80 |
TyTy::BaseType *rtype); |
| 81 |
TyTy::BaseType *expect_dyn (TyTy::DynamicObjectType *ltype, |
| 82 |
TyTy::BaseType *rtype); |
| 83 |
TyTy::BaseType *expect_closure (TyTy::ClosureType *ltype, |
| 84 |
TyTy::BaseType *rtype); |
| 85 |
TyTy::BaseType *expect_opaque (TyTy::OpaqueType *ltype, |
| 86 |
TyTy::BaseType *rtype); |
| 87 |
|
| 88 |
private: |
| 89 |
UnifyRules (TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs, |
| 90 |
location_t locus, bool commit_flag, bool emit_error, bool infer, |
| 91 |
std::vector<CommitSite> &commits, |
| 92 |
std::vector<InferenceSite> &infers); |
| 93 |
|
| 94 |
void emit_type_mismatch () const; |
| 95 |
void emit_abi_mismatch (const TyTy::FnType &expected, |
| 96 |
const TyTy::FnType &got) const; |
| 97 |
|
| 98 |
TyTy::BaseType *go (); |
| 99 |
|
| 100 |
TyTy::BaseType *get_base (); |
| 101 |
TyTy::BaseType *get_other (); |
| 102 |
|
| 103 |
TyTy::TyWithLocation lhs; |
| 104 |
TyTy::TyWithLocation rhs; |
| 105 |
location_t locus; |
| 106 |
bool commit_flag; |
| 107 |
bool emit_error; |
| 108 |
bool infer_flag; |
| 109 |
std::vector<CommitSite> &commits; |
| 110 |
std::vector<InferenceSite> &infers; |
| 111 |
|
| 112 |
Analysis::Mappings &mappings; |
| 113 |
TypeCheckContext &context; |
| 114 |
}; |
| 115 |
|
| 116 |
} // namespace Resolver |
| 117 |
} // namespace Rust |
| 118 |
|
| 119 |
#endif // RUST_UNIFY |