aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/typecheck/rust-substitution-mapper.h
blob: 13487cf002db0b3bdf4645106fea2d2e0162e24c (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
// Copyright (C) 2020-2024 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_SUBSTITUTION_MAPPER_H
#define RUST_SUBSTITUTION_MAPPER_H

#include "rust-tyty.h"
#include "rust-tyty-visitor.h"

namespace Rust {
namespace Resolver {

class SubstMapper : public TyTy::TyVisitor
{
public:
  static TyTy::BaseType *Resolve (TyTy::BaseType *base, location_t locus,
				  HIR::GenericArgs *generics = nullptr,
				  const std::vector<TyTy::Region> &regions
				  = {});

  static TyTy::BaseType *InferSubst (TyTy::BaseType *base, location_t locus);

  bool have_generic_args () const;

  void visit (TyTy::FnType &type) override;
  void visit (TyTy::ADTType &type) override;
  void visit (TyTy::PlaceholderType &type) override;
  void visit (TyTy::ProjectionType &type) override;

  // nothing to do for these
  void visit (TyTy::InferType &) override { rust_unreachable (); }
  void visit (TyTy::TupleType &) override { rust_unreachable (); }
  void visit (TyTy::FnPtr &) override { rust_unreachable (); }
  void visit (TyTy::ArrayType &) override { rust_unreachable (); }
  void visit (TyTy::SliceType &) override { rust_unreachable (); }
  void visit (TyTy::BoolType &) override { rust_unreachable (); }
  void visit (TyTy::IntType &) override { rust_unreachable (); }
  void visit (TyTy::UintType &) override { rust_unreachable (); }
  void visit (TyTy::FloatType &) override { rust_unreachable (); }
  void visit (TyTy::USizeType &) override { rust_unreachable (); }
  void visit (TyTy::ISizeType &) override { rust_unreachable (); }
  void visit (TyTy::ErrorType &) override { rust_unreachable (); }
  void visit (TyTy::CharType &) override { rust_unreachable (); }
  void visit (TyTy::ReferenceType &) override { rust_unreachable (); }
  void visit (TyTy::PointerType &) override { rust_unreachable (); }
  void visit (TyTy::ParamType &) override { rust_unreachable (); }
  void visit (TyTy::StrType &) override { rust_unreachable (); }
  void visit (TyTy::NeverType &) override { rust_unreachable (); }
  void visit (TyTy::DynamicObjectType &) override { rust_unreachable (); }
  void visit (TyTy::ClosureType &) override { rust_unreachable (); }

private:
  SubstMapper (HirId ref, HIR::GenericArgs *generics,
	       const std::vector<TyTy::Region> &regions, location_t locus);

  TyTy::BaseType *resolved;
  HIR::GenericArgs *generics;
  const std::vector<TyTy::Region> &regions;
  location_t locus;
};

class SubstMapperInternal : public TyTy::TyVisitor
{
public:
  static TyTy::BaseType *Resolve (TyTy::BaseType *base,
				  TyTy::SubstitutionArgumentMappings &mappings);

  static bool mappings_are_bound (TyTy::BaseType *ty,
				  TyTy::SubstitutionArgumentMappings &mappings);

  void visit (TyTy::FnType &type) override;
  void visit (TyTy::ADTType &type) override;
  void visit (TyTy::TupleType &type) override;
  void visit (TyTy::ReferenceType &type) override;
  void visit (TyTy::PointerType &type) override;
  void visit (TyTy::ParamType &type) override;
  void visit (TyTy::PlaceholderType &type) override;
  void visit (TyTy::ProjectionType &type) override;
  void visit (TyTy::ClosureType &type) override;
  void visit (TyTy::ArrayType &type) override;
  void visit (TyTy::SliceType &type) override;
  void visit (TyTy::InferType &type) override;
  void visit (TyTy::FnPtr &type) override;
  void visit (TyTy::BoolType &type) override;
  void visit (TyTy::IntType &type) override;
  void visit (TyTy::UintType &type) override;
  void visit (TyTy::FloatType &type) override;
  void visit (TyTy::USizeType &type) override;
  void visit (TyTy::ISizeType &type) override;
  void visit (TyTy::ErrorType &type) override;
  void visit (TyTy::CharType &type) override;
  void visit (TyTy::StrType &type) override;
  void visit (TyTy::NeverType &type) override;
  void visit (TyTy::DynamicObjectType &type) override;

private:
  SubstMapperInternal (HirId ref, TyTy::SubstitutionArgumentMappings &mappings);

  TyTy::BaseType *resolved;
  TyTy::SubstitutionArgumentMappings &mappings;
};

class SubstMapperFromExisting : public TyTy::TyVisitor
{
public:
  static TyTy::BaseType *Resolve (TyTy::BaseType *concrete,
				  TyTy::BaseType *receiver);

  void visit (TyTy::FnType &type) override;
  void visit (TyTy::ADTType &type) override;
  void visit (TyTy::ClosureType &type) override;

  void visit (TyTy::InferType &) override { rust_unreachable (); }
  void visit (TyTy::TupleType &) override { rust_unreachable (); }
  void visit (TyTy::FnPtr &) override { rust_unreachable (); }
  void visit (TyTy::ArrayType &) override { rust_unreachable (); }
  void visit (TyTy::SliceType &) override { rust_unreachable (); }
  void visit (TyTy::BoolType &) override { rust_unreachable (); }
  void visit (TyTy::IntType &) override { rust_unreachable (); }
  void visit (TyTy::UintType &) override { rust_unreachable (); }
  void visit (TyTy::FloatType &) override { rust_unreachable (); }
  void visit (TyTy::USizeType &) override { rust_unreachable (); }
  void visit (TyTy::ISizeType &) override { rust_unreachable (); }
  void visit (TyTy::ErrorType &) override { rust_unreachable (); }
  void visit (TyTy::CharType &) override { rust_unreachable (); }
  void visit (TyTy::ReferenceType &) override { rust_unreachable (); }
  void visit (TyTy::PointerType &) override { rust_unreachable (); }
  void visit (TyTy::ParamType &) override { rust_unreachable (); }
  void visit (TyTy::StrType &) override { rust_unreachable (); }
  void visit (TyTy::NeverType &) override { rust_unreachable (); }
  void visit (TyTy::PlaceholderType &) override { rust_unreachable (); }
  void visit (TyTy::ProjectionType &) override { rust_unreachable (); }
  void visit (TyTy::DynamicObjectType &) override { rust_unreachable (); }

private:
  SubstMapperFromExisting (TyTy::BaseType *concrete, TyTy::BaseType *receiver);

  TyTy::BaseType *concrete;
  TyTy::BaseType *receiver;
  TyTy::BaseType *resolved;
};

class GetUsedSubstArgs : public TyTy::TyConstVisitor
{
public:
  static TyTy::SubstitutionArgumentMappings From (const TyTy::BaseType *from);

  void visit (const TyTy::FnType &type) override;
  void visit (const TyTy::ADTType &type) override;
  void visit (const TyTy::ClosureType &type) override;

  void visit (const TyTy::InferType &) override {}
  void visit (const TyTy::TupleType &) override {}
  void visit (const TyTy::FnPtr &) override {}
  void visit (const TyTy::ArrayType &) override {}
  void visit (const TyTy::SliceType &) override {}
  void visit (const TyTy::BoolType &) override {}
  void visit (const TyTy::IntType &) override {}
  void visit (const TyTy::UintType &) override {}
  void visit (const TyTy::FloatType &) override {}
  void visit (const TyTy::USizeType &) override {}
  void visit (const TyTy::ISizeType &) override {}
  void visit (const TyTy::ErrorType &) override {}
  void visit (const TyTy::CharType &) override {}
  void visit (const TyTy::ReferenceType &) override {}
  void visit (const TyTy::PointerType &) override {}
  void visit (const TyTy::ParamType &) override {}
  void visit (const TyTy::StrType &) override {}
  void visit (const TyTy::NeverType &) override {}
  void visit (const TyTy::PlaceholderType &) override {}
  void visit (const TyTy::ProjectionType &) override {}
  void visit (const TyTy::DynamicObjectType &) override {}

private:
  GetUsedSubstArgs ();

  TyTy::SubstitutionArgumentMappings args;
};

} // namespace Resolver
} // namespace Rust

#endif // RUST_SUBSTITUTION_MAPPER_H