Daily bump.
[official-gcc.git] / gcc / rust / typecheck / rust-coercion.h
blob780fafd3baa80f8c16d959cca482a5f268e66dd6
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
8 // version.
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.
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/>.
19 #ifndef RUST_COERCION
20 #define RUST_COERCION
22 #include "rust-autoderef.h"
23 #include "rust-hir-type-check.h"
25 namespace Rust {
26 namespace Resolver {
28 class TypeCoercionRules : protected AutoderefCycle
30 public:
31 struct CoercionResult
33 std::vector<Adjustment> adjustments;
34 TyTy::BaseType *tyty;
36 bool is_error ()
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,
46 bool allow_autoderef,
47 bool is_cast_site = false);
49 static CoercionResult TryCoerce (TyTy::BaseType *receiver,
50 TyTy::BaseType *expected, location_t locus,
51 bool allow_autoderef,
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,
66 Mutability to_mutbl);
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);
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);
78 bool select (TyTy::BaseType &autoderefed) override;
80 bool do_coercion (TyTy::BaseType *receiver);
82 private:
83 // context info
84 Analysis::Mappings *mappings;
85 TypeCheckContext *context;
87 // search
88 TyTy::BaseType *expected;
89 location_t locus;
91 // mutable fields
92 CoercionResult try_result;
93 bool emit_errors;
94 bool try_flag;
95 bool is_cast_site;
98 } // namespace Resolver
99 } // namespace Rust
101 #endif // RUST_COERCION