Branch data Line data Source code
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_COERCION
20 : : #define RUST_COERCION
21 : :
22 : : #include "rust-autoderef.h"
23 : : #include "rust-hir-type-check.h"
24 : :
25 : : namespace Rust {
26 : : namespace Resolver {
27 : :
28 : : class TypeCoercionRules : protected AutoderefCycle
29 : : {
30 : : public:
31 : 124076 : struct CoercionResult
32 : : {
33 : : std::vector<Adjustment> adjustments;
34 : : TyTy::BaseType *tyty;
35 : :
36 : 79640 : bool is_error ()
37 : : {
38 : 79640 : return tyty == nullptr || tyty->get_kind () == TyTy::TypeKind::ERROR;
39 : : }
40 : :
41 : 38140 : static CoercionResult get_error () { return CoercionResult{{}, nullptr}; }
42 : : };
43 : :
44 : : static CoercionResult Coerce (TyTy::BaseType *receiver,
45 : : TyTy::BaseType *expected, location_t locus,
46 : : bool allow_autoderef,
47 : : bool is_cast_site = false);
48 : :
49 : : static CoercionResult TryCoerce (TyTy::BaseType *receiver,
50 : : TyTy::BaseType *expected, location_t locus,
51 : : bool allow_autoderef,
52 : : bool is_cast_site = false);
53 : :
54 : : CoercionResult coerce_unsafe_ptr (TyTy::BaseType *receiver,
55 : : TyTy::PointerType *expected,
56 : : Mutability mutability);
57 : :
58 : : CoercionResult coerce_borrowed_pointer (TyTy::BaseType *receiver,
59 : : TyTy::ReferenceType *expected,
60 : : Mutability mutability);
61 : :
62 : : CoercionResult coerce_unsized (TyTy::BaseType *receiver,
63 : : TyTy::BaseType *expected, bool &unsafe_error);
64 : :
65 : : static bool coerceable_mutability (Mutability from_mutbl,
66 : : Mutability to_mutbl);
67 : :
68 : : void mismatched_mutability_error (location_t expr_locus, location_t lhs,
69 : : location_t rhs);
70 : : void object_unsafe_error (location_t expr_locus, location_t lhs,
71 : : location_t rhs);
72 : :
73 : : protected:
74 : : TypeCoercionRules (TyTy::BaseType *expected, location_t locus,
75 : : bool emit_errors, bool allow_autoderef, bool try_flag,
76 : : bool is_cast_site);
77 : :
78 : : bool select (TyTy::BaseType &autoderefed) override;
79 : :
80 : : bool do_coercion (TyTy::BaseType *receiver);
81 : :
82 : : private:
83 : : // context info
84 : : Analysis::Mappings *mappings;
85 : : TypeCheckContext *context;
86 : :
87 : : // search
88 : : TyTy::BaseType *expected;
89 : : location_t locus;
90 : :
91 : : // mutable fields
92 : : CoercionResult try_result;
93 : : bool emit_errors;
94 : : bool try_flag;
95 : : bool is_cast_site;
96 : : };
97 : :
98 : : } // namespace Resolver
99 : : } // namespace Rust
100 : :
101 : : #endif // RUST_COERCION
|