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_COERCION
20 : #define RUST_COERCION
21 :
22 : #include "rust-autoderef.h"
23 : #include "rust-hir-type-check.h"
24 : #include "expected.h"
25 :
26 : namespace Rust {
27 : namespace Resolver {
28 :
29 : class TypeCoercionRules : protected AutoderefCycle
30 : {
31 : public:
32 : enum class CoerceUnsizedError
33 : {
34 : Regular,
35 : Unsafe
36 : };
37 :
38 221809 : struct CoercionResult
39 : {
40 : std::vector<Adjustment> adjustments;
41 : TyTy::BaseType *tyty;
42 :
43 117633 : bool is_error ()
44 : {
45 117633 : return tyty == nullptr || tyty->get_kind () == TyTy::TypeKind::ERROR;
46 : }
47 :
48 21586 : static CoercionResult get_error () { return CoercionResult{{}, nullptr}; }
49 : };
50 :
51 : static CoercionResult Coerce (TyTy::BaseType *receiver,
52 : TyTy::BaseType *expected, location_t locus,
53 : bool allow_autoderef,
54 : bool is_cast_site = false);
55 :
56 : static CoercionResult TryCoerce (TyTy::BaseType *receiver,
57 : TyTy::BaseType *expected, location_t locus,
58 : bool allow_autoderef,
59 : bool is_cast_site = false,
60 : bool allow_never_coercion = true);
61 :
62 : CoercionResult coerce_never (TyTy::BaseType *receiver);
63 : CoercionResult coerce_unsafe_ptr (TyTy::BaseType *receiver,
64 : TyTy::PointerType *expected,
65 : Mutability mutability);
66 :
67 : CoercionResult coerce_borrowed_pointer (TyTy::BaseType *receiver,
68 : TyTy::ReferenceType *expected,
69 : Mutability mutability);
70 :
71 : tl::expected<CoercionResult, CoerceUnsizedError>
72 : coerce_unsized (TyTy::BaseType *receiver, TyTy::BaseType *expected,
73 : bool is_inner = false);
74 :
75 : static bool coerceable_mutability (Mutability from_mutbl,
76 : Mutability to_mutbl);
77 :
78 : void mismatched_mutability_error (location_t expr_locus, location_t lhs,
79 : location_t rhs);
80 : void object_unsafe_error (location_t expr_locus, location_t lhs,
81 : location_t rhs);
82 :
83 : protected:
84 : TypeCoercionRules (TyTy::BaseType *expected, location_t locus,
85 : bool emit_errors, bool allow_autoderef, bool try_flag,
86 : bool is_cast_site, bool allow_never_coercion = true);
87 :
88 : bool select (TyTy::BaseType &autoderefed) override;
89 :
90 : bool do_coercion (TyTy::BaseType *receiver);
91 :
92 : struct CoercionSetup
93 : {
94 : TyTy::BaseType *ty_a;
95 : TyTy::BaseType *ty_b;
96 : bool needs_reborrow;
97 : Mutability expected_mutability;
98 : bool unwrapped_pointer;
99 : };
100 : tl::expected<CoercionSetup, CoerceUnsizedError>
101 : unwrap_ptrs_and_refs (TyTy::BaseType *source, TyTy::BaseType *target);
102 : tl::expected<TyTy::BaseType *, CoerceUnsizedError>
103 : coerce_unsized_array_to_slice (TyTy::BaseType *a, TyTy::BaseType *b);
104 : tl::expected<TyTy::BaseType *, CoerceUnsizedError>
105 : coerce_unsized_dyn (TyTy::BaseType *a, TyTy::BaseType *b);
106 : tl::expected<TyTy::BaseType *, CoerceUnsizedError>
107 : coerce_unsized_adt (TyTy::BaseType *a, TyTy::BaseType *b,
108 : bool needs_reborrow);
109 : TyTy::BaseType *apply_reborrow_adjustment (TyTy::BaseType *source,
110 : TyTy::BaseType *target,
111 : TyTy::BaseType *result,
112 : Mutability expected_mutability);
113 :
114 : private:
115 : // context info
116 : Analysis::Mappings &mappings;
117 : TypeCheckContext *context;
118 :
119 : // search
120 : TyTy::BaseType *expected;
121 : location_t locus;
122 :
123 : // mutable fields
124 : CoercionResult try_result;
125 : bool emit_errors;
126 : bool try_flag;
127 : bool is_cast_site;
128 : bool allow_never_coercion;
129 : };
130 :
131 : } // namespace Resolver
132 : } // namespace Rust
133 :
134 : #endif // RUST_COERCION
|