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 224702 : struct CoercionResult
39 : {
40 : std::vector<Adjustment> adjustments;
41 : TyTy::BaseType *tyty;
42 :
43 138809 : bool is_error ()
44 : {
45 138809 : return tyty == nullptr || tyty->get_kind () == TyTy::TypeKind::ERROR;
46 : }
47 :
48 40058 : 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 :
61 : CoercionResult coerce_never (TyTy::BaseType *receiver);
62 : CoercionResult coerce_unsafe_ptr (TyTy::BaseType *receiver,
63 : TyTy::PointerType *expected,
64 : Mutability mutability);
65 :
66 : CoercionResult coerce_borrowed_pointer (TyTy::BaseType *receiver,
67 : TyTy::ReferenceType *expected,
68 : Mutability mutability);
69 :
70 : tl::expected<CoercionResult, CoerceUnsizedError>
71 : coerce_unsized (TyTy::BaseType *receiver, TyTy::BaseType *expected,
72 : bool is_inner = false);
73 :
74 : static bool coerceable_mutability (Mutability from_mutbl,
75 : Mutability to_mutbl);
76 :
77 : void mismatched_mutability_error (location_t expr_locus, location_t lhs,
78 : location_t rhs);
79 : void object_unsafe_error (location_t expr_locus, location_t lhs,
80 : location_t rhs);
81 :
82 : protected:
83 : TypeCoercionRules (TyTy::BaseType *expected, location_t locus,
84 : bool emit_errors, bool allow_autoderef, bool try_flag,
85 : bool is_cast_site);
86 :
87 : bool select (TyTy::BaseType &autoderefed) override;
88 :
89 : bool do_coercion (TyTy::BaseType *receiver);
90 :
91 : struct CoercionSetup
92 : {
93 : TyTy::BaseType *ty_a;
94 : TyTy::BaseType *ty_b;
95 : bool needs_reborrow;
96 : Mutability expected_mutability;
97 : bool unwrapped_pointer;
98 : };
99 : tl::expected<CoercionSetup, CoerceUnsizedError>
100 : unwrap_ptrs_and_refs (TyTy::BaseType *source, TyTy::BaseType *target);
101 : tl::expected<TyTy::BaseType *, CoerceUnsizedError>
102 : coerce_unsized_array_to_slice (TyTy::BaseType *a, TyTy::BaseType *b);
103 : tl::expected<TyTy::BaseType *, CoerceUnsizedError>
104 : coerce_unsized_dyn (TyTy::BaseType *a, TyTy::BaseType *b);
105 : tl::expected<TyTy::BaseType *, CoerceUnsizedError>
106 : coerce_unsized_adt (TyTy::BaseType *a, TyTy::BaseType *b,
107 : bool needs_reborrow);
108 : TyTy::BaseType *apply_reborrow_adjustment (TyTy::BaseType *source,
109 : TyTy::BaseType *target,
110 : TyTy::BaseType *result,
111 : Mutability expected_mutability);
112 :
113 : private:
114 : // context info
115 : Analysis::Mappings &mappings;
116 : TypeCheckContext *context;
117 :
118 : // search
119 : TyTy::BaseType *expected;
120 : location_t locus;
121 :
122 : // mutable fields
123 : CoercionResult try_result;
124 : bool emit_errors;
125 : bool try_flag;
126 : bool is_cast_site;
127 : };
128 :
129 : } // namespace Resolver
130 : } // namespace Rust
131 :
132 : #endif // RUST_COERCION
|