summaryrefslogtreecommitdiff
path: root/gcc/rust/typecheck/rust-autoderef.h
blob: 5f05c0489d1e75ed0779b810ca9fc44d121ecf66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (C) 2020-2023 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#ifndef RUST_AUTODEREF
#define RUST_AUTODEREF

#include "rust-tyty.h"

namespace Rust {
namespace Resolver {

class Adjustment
{
public:
  enum AdjustmentType
  {
    ERROR,

    IMM_REF,
    MUT_REF,
    DEREF,
    DEREF_MUT,
    INDIRECTION,
    UNSIZE,
  };

  // ctor for all adjustments except derefs
  Adjustment (AdjustmentType type, const TyTy::BaseType *actual,
	      const TyTy::BaseType *expected)
    : Adjustment (type, actual, expected, nullptr, nullptr,
		  AdjustmentType::ERROR)
  {}

  static Adjustment get_op_overload_deref_adjustment (
    AdjustmentType type, const TyTy::BaseType *actual,
    const TyTy::BaseType *expected, TyTy::FnType *fn, HIR::ImplItem *deref_item,
    Adjustment::AdjustmentType requires_ref_adjustment)
  {
    rust_assert (type == DEREF || type == DEREF_MUT);
    return Adjustment (type, actual, expected, fn, deref_item,
		       requires_ref_adjustment);
  }

  AdjustmentType get_type () const { return type; }

  const TyTy::BaseType *get_actual () const { return actual; }
  const TyTy::BaseType *get_expected () const { return expected; }

  std::string as_string () const
  {
    return Adjustment::type_string (get_type ()) + "->"
	   + get_expected ()->debug_str ();
  }

  static std::string type_string (AdjustmentType type)
  {
    switch (type)
      {
      case AdjustmentType::ERROR:
	return "ERROR";
      case AdjustmentType::IMM_REF:
	return "IMM_REF";
      case AdjustmentType::MUT_REF:
	return "MUT_REF";
      case AdjustmentType::DEREF:
	return "DEREF";
      case AdjustmentType::DEREF_MUT:
	return "DEREF_MUT";
      case AdjustmentType::INDIRECTION:
	return "INDIRECTION";
      case AdjustmentType::UNSIZE:
	return "UNSIZE";
      }
    gcc_unreachable ();
    return "";
  }

  static Adjustment get_error () { return Adjustment{ERROR, nullptr, nullptr}; }

  bool is_error () const { return type == ERROR; }

  bool is_deref_adjustment () const { return type == DEREF; }

  bool is_deref_mut_adjustment () const { return type == DEREF_MUT; }

  bool has_operator_overload () const { return deref_operator_fn != nullptr; }

  TyTy::FnType *get_deref_operator_fn () const { return deref_operator_fn; }

  AdjustmentType get_deref_adjustment_type () const
  {
    return requires_ref_adjustment;
  }

  HIR::ImplItem *get_deref_hir_item () const { return deref_item; }

private:
  Adjustment (AdjustmentType type, const TyTy::BaseType *actual,
	      const TyTy::BaseType *expected, TyTy::FnType *deref_operator_fn,
	      HIR::ImplItem *deref_item,
	      Adjustment::AdjustmentType requires_ref_adjustment)
    : type (type), actual (actual), expected (expected),
      deref_operator_fn (deref_operator_fn), deref_item (deref_item),
      requires_ref_adjustment (requires_ref_adjustment)
  {}

  AdjustmentType type;
  const TyTy::BaseType *actual;
  const TyTy::BaseType *expected;

  // - only used for deref operator_overloads
  //
  // the fn that we are calling
  TyTy::FnType *deref_operator_fn;
  HIR::ImplItem *deref_item;
  // operator overloads can requre a reference
  Adjustment::AdjustmentType requires_ref_adjustment;
};

class Adjuster
{
public:
  Adjuster (const TyTy::BaseType *ty) : base (ty) {}

  TyTy::BaseType *adjust_type (const std::vector<Adjustment> &adjustments);

  static Adjustment
  try_deref_type (const TyTy::BaseType *ty,
		  Analysis::RustLangItem::ItemType deref_lang_item);

  static Adjustment try_raw_deref_type (const TyTy::BaseType *ty);

  static Adjustment try_unsize_type (const TyTy::BaseType *ty);

private:
  const TyTy::BaseType *base;
};

class AutoderefCycle
{
protected:
  AutoderefCycle (bool autoderef_flag);

  virtual ~AutoderefCycle ();

  virtual bool select (const TyTy::BaseType &autoderefed) = 0;

  // optional: this is a chance to hook in to grab predicate items on the raw
  // type
  virtual void try_hook (const TyTy::BaseType &);

  virtual bool cycle (const TyTy::BaseType *receiver);

  bool try_autoderefed (const TyTy::BaseType *r);

  bool autoderef_flag;
  std::vector<Adjustment> adjustments;
};

} // namespace Resolver
} // namespace Rust

#endif // RUST_AUTODEREF