Daily bump.
[official-gcc.git] / gcc / rust / typecheck / rust-autoderef.h
blob30472d23c2ff520d61714dee4266ca7ddcd66cee
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_AUTODEREF
20 #define RUST_AUTODEREF
22 #include "rust-tyty.h"
24 namespace Rust {
25 namespace Resolver {
27 class Adjustment
29 public:
30 enum AdjustmentType
32 ERROR,
34 IMM_REF,
35 MUT_REF,
36 DEREF,
37 DEREF_MUT,
38 INDIRECTION,
39 UNSIZE,
42 // ctor for all adjustments except derefs
43 Adjustment (AdjustmentType type, TyTy::BaseType *actual,
44 TyTy::BaseType *expected)
45 : Adjustment (type, actual, expected, nullptr, AdjustmentType::ERROR)
48 static Adjustment get_op_overload_deref_adjustment (
49 AdjustmentType type, TyTy::BaseType *actual, TyTy::BaseType *expected,
50 TyTy::FnType *fn, Adjustment::AdjustmentType requires_ref_adjustment)
52 rust_assert (type == DEREF || type == DEREF_MUT);
53 return Adjustment (type, actual, expected, fn, requires_ref_adjustment);
56 AdjustmentType get_type () const { return type; }
58 TyTy::BaseType *get_actual () const { return actual; }
59 TyTy::BaseType *get_expected () const { return expected; }
61 std::string as_string () const
63 return Adjustment::type_string (get_type ()) + "->"
64 + get_expected ()->debug_str ();
67 static std::string type_string (AdjustmentType type)
69 switch (type)
71 case AdjustmentType::ERROR:
72 return "ERROR";
73 case AdjustmentType::IMM_REF:
74 return "IMM_REF";
75 case AdjustmentType::MUT_REF:
76 return "MUT_REF";
77 case AdjustmentType::DEREF:
78 return "DEREF";
79 case AdjustmentType::DEREF_MUT:
80 return "DEREF_MUT";
81 case AdjustmentType::INDIRECTION:
82 return "INDIRECTION";
83 case AdjustmentType::UNSIZE:
84 return "UNSIZE";
86 rust_unreachable ();
87 return "";
90 static Adjustment get_error () { return Adjustment{ERROR, nullptr, nullptr}; }
92 bool is_error () const { return type == ERROR; }
94 bool is_deref_adjustment () const { return type == DEREF; }
96 bool is_deref_mut_adjustment () const { return type == DEREF_MUT; }
98 bool has_operator_overload () const { return deref_operator_fn != nullptr; }
100 TyTy::FnType *get_deref_operator_fn () const { return deref_operator_fn; }
102 AdjustmentType get_deref_adjustment_type () const
104 return requires_ref_adjustment;
107 private:
108 Adjustment (AdjustmentType type, TyTy::BaseType *actual,
109 TyTy::BaseType *expected, TyTy::FnType *deref_operator_fn,
110 Adjustment::AdjustmentType requires_ref_adjustment)
111 : type (type), actual (actual), expected (expected),
112 deref_operator_fn (deref_operator_fn),
113 requires_ref_adjustment (requires_ref_adjustment)
116 AdjustmentType type;
117 TyTy::BaseType *actual;
118 TyTy::BaseType *expected;
120 // - only used for deref operator_overloads
122 // the fn that we are calling
123 TyTy::FnType *deref_operator_fn;
124 // operator overloads can requre a reference
125 Adjustment::AdjustmentType requires_ref_adjustment;
128 class Adjuster
130 public:
131 Adjuster (const TyTy::BaseType *ty) : base (ty) {}
133 TyTy::BaseType *adjust_type (const std::vector<Adjustment> &adjustments);
135 static Adjustment try_deref_type (TyTy::BaseType *ty,
136 LangItem::Kind deref_lang_item);
138 static Adjustment try_raw_deref_type (TyTy::BaseType *ty);
140 static Adjustment try_unsize_type (TyTy::BaseType *ty);
142 private:
143 const TyTy::BaseType *base;
146 class AutoderefCycle
148 protected:
149 AutoderefCycle (bool autoderef_flag);
151 virtual ~AutoderefCycle ();
153 virtual bool select (TyTy::BaseType &autoderefed) = 0;
155 // optional: this is a chance to hook in to grab predicate items on the raw
156 // type
157 virtual void try_hook (const TyTy::BaseType &);
159 virtual bool cycle (TyTy::BaseType *receiver);
161 bool try_autoderefed (TyTy::BaseType *r);
163 bool autoderef_flag;
164 std::vector<Adjustment> adjustments;
167 } // namespace Resolver
168 } // namespace Rust
170 #endif // RUST_AUTODEREF