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 214846 : struct CoercionResult
39 : {
40 : std::vector<Adjustment> adjustments;
41 : TyTy::BaseType *tyty;
42 :
43 134801 : bool is_error ()
44 : {
45 134801 : return tyty == nullptr || tyty->get_kind () == TyTy::TypeKind::ERROR;
46 : }
47 :
48 39874 : 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 :
73 : static bool coerceable_mutability (Mutability from_mutbl,
74 : Mutability to_mutbl);
75 :
76 : void mismatched_mutability_error (location_t expr_locus, location_t lhs,
77 : location_t rhs);
78 : void object_unsafe_error (location_t expr_locus, location_t lhs,
79 : location_t rhs);
80 :
81 : protected:
82 : TypeCoercionRules (TyTy::BaseType *expected, location_t locus,
83 : bool emit_errors, bool allow_autoderef, bool try_flag,
84 : bool is_cast_site);
85 :
86 : bool select (TyTy::BaseType &autoderefed) override;
87 :
88 : bool do_coercion (TyTy::BaseType *receiver);
89 :
90 : private:
91 : // context info
92 : Analysis::Mappings &mappings;
93 : TypeCheckContext *context;
94 :
95 : // search
96 : TyTy::BaseType *expected;
97 : location_t locus;
98 :
99 : // mutable fields
100 : CoercionResult try_result;
101 : bool emit_errors;
102 : bool try_flag;
103 : bool is_cast_site;
104 : };
105 :
106 : } // namespace Resolver
107 : } // namespace Rust
108 :
109 : #endif // RUST_COERCION
|