1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
22 #include "rust-autoderef.h"
23 #include "rust-hir-type-check.h"
28 class TypeCoercionRules
: protected AutoderefCycle
33 std::vector
<Adjustment
> adjustments
;
38 return tyty
== nullptr || tyty
->get_kind () == TyTy::TypeKind::ERROR
;
41 static CoercionResult
get_error () { return CoercionResult
{{}, nullptr}; }
44 static CoercionResult
Coerce (TyTy::BaseType
*receiver
,
45 TyTy::BaseType
*expected
, location_t locus
,
47 bool is_cast_site
= false);
49 static CoercionResult
TryCoerce (TyTy::BaseType
*receiver
,
50 TyTy::BaseType
*expected
, location_t locus
,
52 bool is_cast_site
= false);
54 CoercionResult
coerce_unsafe_ptr (TyTy::BaseType
*receiver
,
55 TyTy::PointerType
*expected
,
56 Mutability mutability
);
58 CoercionResult
coerce_borrowed_pointer (TyTy::BaseType
*receiver
,
59 TyTy::ReferenceType
*expected
,
60 Mutability mutability
);
62 CoercionResult
coerce_unsized (TyTy::BaseType
*receiver
,
63 TyTy::BaseType
*expected
, bool &unsafe_error
);
65 static bool coerceable_mutability (Mutability from_mutbl
,
68 void mismatched_mutability_error (location_t expr_locus
, location_t lhs
,
70 void object_unsafe_error (location_t expr_locus
, location_t lhs
,
74 TypeCoercionRules (TyTy::BaseType
*expected
, location_t locus
,
75 bool emit_errors
, bool allow_autoderef
, bool try_flag
,
78 bool select (TyTy::BaseType
&autoderefed
) override
;
80 bool do_coercion (TyTy::BaseType
*receiver
);
84 Analysis::Mappings
*mappings
;
85 TypeCheckContext
*context
;
88 TyTy::BaseType
*expected
;
92 CoercionResult try_result
;
98 } // namespace Resolver
101 #endif // RUST_COERCION