summaryrefslogtreecommitdiff
path: root/gcc/rust/typecheck/rust-hir-trait-reference.h
blob: d20b2952e5b2b67ff1b7db127a364a62c0bae91f (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright (C) 2021-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_HIR_TRAIT_REF_H
#define RUST_HIR_TRAIT_REF_H

#include "rust-hir-full.h"
#include "rust-hir-type-check-util.h"
#include "rust-tyty-visitor.h"

namespace Rust {
namespace Resolver {

// Data Objects for the associated trait items in a structure we can work with
// https://doc.rust-lang.org/edition-guide/rust-2018/trait-system/associated-constants.html
class TypeCheckContext;
class TraitItemReference
{
public:
  enum TraitItemType
  {
    FN,
    CONST,
    TYPE,
    ERROR
  };

  TraitItemReference (std::string identifier, bool optional, TraitItemType type,
		      HIR::TraitItem *hir_trait_item, TyTy::BaseType *self,
		      std::vector<TyTy::SubstitutionParamMapping> substitutions,
		      Location locus);

  TraitItemReference (TraitItemReference const &other);

  TraitItemReference &operator= (TraitItemReference const &other);

  static TraitItemReference error ()
  {
    return TraitItemReference ("", false, ERROR, nullptr, nullptr, {},
			       Location ());
  }

  static TraitItemReference &error_node ()
  {
    static TraitItemReference error = TraitItemReference::error ();
    return error;
  }

  bool is_error () const;

  std::string as_string () const;

  static std::string trait_item_type_as_string (TraitItemType ty)
  {
    switch (ty)
      {
      case FN:
	return "FN";
      case CONST:
	return "CONST";
      case TYPE:
	return "TYPE";
      case ERROR:
	return "ERROR";
      }
    return "ERROR";
  }

  bool is_optional () const;

  std::string get_identifier () const;

  TraitItemType get_trait_item_type () const;

  HIR::TraitItem *get_hir_trait_item () const;

  Location get_locus () const;

  const Analysis::NodeMapping get_mappings () const;

  TyTy::BaseType *get_tyty () const;

  Analysis::NodeMapping get_parent_trait_mappings () const;

  // this is called when the trait is completed resolution and gives the items
  // a chance to run their specific type resolution passes. If we call their
  // resolution on construction it can lead to a case where the trait being
  // resolved recursively trying to resolve the trait itself infinitely since
  // the trait will not be stored in its own map yet
  void on_resolved ();

  void associated_type_set (TyTy::BaseType *ty) const;

  void associated_type_reset (bool only_projections) const;

  bool is_object_safe () const;

private:
  TyTy::ErrorType *get_error () const;

  TyTy::BaseType *get_type_from_typealias (/*const*/
					   HIR::TraitItemType &type) const;

  TyTy::BaseType *
  get_type_from_constant (/*const*/ HIR::TraitItemConst &constant) const;

  TyTy::BaseType *get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const;

  bool is_item_resolved () const;
  void resolve_item (HIR::TraitItemType &type);
  void resolve_item (HIR::TraitItemConst &constant);
  void resolve_item (HIR::TraitItemFunc &func);

  std::string identifier;
  bool optional_flag;
  TraitItemType type;
  HIR::TraitItem *hir_trait_item;
  std::vector<TyTy::SubstitutionParamMapping> inherited_substitutions;
  Location locus;

  TyTy::BaseType
    *self; // this is the implict Self TypeParam required for methods
  Resolver::TypeCheckContext *context;
};

// this wraps up the HIR::Trait so we can do analysis on it

class TraitReference
{
public:
  TraitReference (const HIR::Trait *hir_trait_ref,
		  std::vector<TraitItemReference> item_refs,
		  std::vector<const TraitReference *> super_traits,
		  std::vector<TyTy::SubstitutionParamMapping> substs);

  TraitReference (TraitReference const &other);

  TraitReference &operator= (TraitReference const &other);

  TraitReference (TraitReference &&other) = default;
  TraitReference &operator= (TraitReference &&other) = default;

  static TraitReference error ()
  {
    return TraitReference (nullptr, {}, {}, {});
  }

  bool is_error () const;

  static TraitReference &error_node ()
  {
    static TraitReference trait_error_node = TraitReference::error ();
    return trait_error_node;
  }

  Location get_locus () const;

  std::string get_name () const;

  std::string as_string () const;

  const HIR::Trait *get_hir_trait_ref () const;

  const Analysis::NodeMapping &get_mappings () const;

  DefId get_defid () const;

  bool lookup_hir_trait_item (const HIR::TraitItem &item,
			      TraitItemReference **ref);

  bool lookup_trait_item (const std::string &ident, TraitItemReference **ref);

  bool lookup_trait_item_by_type (const std::string &ident,
				  TraitItemReference::TraitItemType type,
				  TraitItemReference **ref);

  bool lookup_trait_item_by_type (const std::string &ident,
				  TraitItemReference::TraitItemType type,
				  const TraitItemReference **ref) const;

  bool lookup_hir_trait_item (const HIR::TraitItem &item,
			      const TraitItemReference **ref) const;

  bool lookup_trait_item (const std::string &ident,
			  const TraitItemReference **ref) const;

  const TraitItemReference *
  lookup_trait_item (const std::string &ident,
		     TraitItemReference::TraitItemType type) const;

  size_t size () const;

  const std::vector<TraitItemReference> &get_trait_items () const;

  void get_trait_items_and_supers (
    std::vector<const TraitItemReference *> &result) const;

  void on_resolved ();

  void clear_associated_types () const;

  void clear_associated_type_projections () const;

  bool is_equal (const TraitReference &other) const;

  const std::vector<const TraitReference *> get_super_traits () const;

  bool is_object_safe (bool emit_error, Location locus) const;

  bool trait_has_generics () const;

  std::vector<TyTy::SubstitutionParamMapping> get_trait_substs () const;

  bool satisfies_bound (const TraitReference &reference) const;

private:
  const HIR::Trait *hir_trait_ref;
  std::vector<TraitItemReference> item_refs;
  std::vector<const TraitReference *> super_traits;
  std::vector<TyTy::SubstitutionParamMapping> trait_substs;
};

class AssociatedImplTrait
{
public:
  AssociatedImplTrait (TraitReference *trait, HIR::ImplBlock *impl,
		       TyTy::BaseType *self,
		       Resolver::TypeCheckContext *context);

  TraitReference *get_trait ();

  HIR::ImplBlock *get_impl_block ();

  TyTy::BaseType *get_self ();
  const TyTy::BaseType *get_self () const;

  TyTy::BaseType *
  setup_associated_types (const TyTy::BaseType *self,
			  const TyTy::TypeBoundPredicate &bound);

  void reset_associated_types ();

private:
  TraitReference *trait;
  HIR::ImplBlock *impl;
  TyTy::BaseType *self;
  Resolver::TypeCheckContext *context;
};

} // namespace Resolver
} // namespace Rust

#endif // RUST_HIR_TRAIT_REF_H