forked from Rust-GCC/gccrs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrust-tyty-variance-analysis-private.h
More file actions
360 lines (290 loc) · 10 KB
/
rust-tyty-variance-analysis-private.h
File metadata and controls
360 lines (290 loc) · 10 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#ifndef RUST_TYTY_VARIANCE_ANALYSIS_PRIVATE_H
#define RUST_TYTY_VARIANCE_ANALYSIS_PRIVATE_H
#include "rust-tyty-variance-analysis.h"
#include "rust-tyty-visitor.h"
namespace Rust {
namespace TyTy {
namespace VarianceAnalysis {
using SolutionIndex = uint32_t;
/** Term descibing variance relations. */
struct Term
{
enum Kind : uint8_t
{
CONST,
REF,
TRANSFORM,
};
Kind kind;
union
{
struct
{
Term *lhs;
Term *rhs;
} transform;
SolutionIndex ref;
Variance const_val;
};
Term () {}
Term (Variance variance) : kind (CONST), const_val (variance) {}
WARN_UNUSED_RESULT bool is_const () const { return kind == CONST; }
static Term make_ref (SolutionIndex index);
static Term make_transform (Term lhs, Term rhs);
};
/** Variance constraint of a type parameter. */
struct Constraint
{
Constraint (SolutionIndex target_index, Term *term)
: target_index (target_index), term (term)
{}
SolutionIndex target_index;
Term *term;
};
/** Abstract variance visitor context. */
template <typename VARIANCE> class VarianceVisitorCtx
{
public:
virtual ~VarianceVisitorCtx () = default;
virtual void add_constraints_from_ty (BaseType *ty, VARIANCE variance) = 0;
virtual void add_constraints_from_region (const Region ®ion,
VARIANCE variance)
= 0;
void add_constraints_from_mutability (BaseType *type, Mutability mutability,
VARIANCE variance)
{
switch (mutability)
{
case Mutability::Imm:
return add_constraints_from_ty (type, variance);
case Mutability::Mut:
return add_constraints_from_ty (type, Variance::invariant ());
}
}
virtual void
add_constraints_from_generic_args (HirId ref, SubstitutionRef &subst,
VARIANCE variance, bool invariant_args)
= 0;
virtual void add_constrints_from_param (ParamType ¶m, VARIANCE variance)
= 0;
virtual VARIANCE contra (VARIANCE variance) = 0;
};
template <typename VARIANCE> class VisitorBase final : public TyVisitor
{
VarianceVisitorCtx<VARIANCE> &ctx;
VARIANCE variance;
public:
VisitorBase (VarianceVisitorCtx<VARIANCE> &ctx, VARIANCE variance)
: ctx (ctx), variance (variance)
{}
void visit (BoolType &type) override {}
void visit (CharType &type) override {}
void visit (IntType &type) override {}
void visit (UintType &type) override {}
void visit (FloatType &type) override {}
void visit (USizeType &type) override {}
void visit (ISizeType &type) override {}
void visit (StrType &type) override {}
void visit (NeverType &type) override {}
void visit (ClosureType &type) override {}
void visit (FnType &type) override
{
for (auto ®ion : type.get_used_arguments ().get_regions ())
ctx.add_constraints_from_region (region, Variance::invariant ());
}
void visit (ReferenceType &type) override
{
ctx.add_constraints_from_region (type.get_region (), variance);
ctx.add_constraints_from_mutability (type.get_base (), type.mutability (),
variance);
}
void visit (ArrayType &type) override
{
ctx.add_constraints_from_ty (type.get_element_type (), variance);
}
void visit (SliceType &type) override
{
ctx.add_constraints_from_ty (type.get_element_type (), variance);
}
void visit (PointerType &type) override
{
ctx.add_constraints_from_ty (type.get_base (), variance);
ctx.add_constraints_from_mutability (type.get_base (), type.mutability (),
variance);
}
void visit (TupleType &type) override
{
for (auto &elem : type.get_fields ())
ctx.add_constraints_from_ty (elem.get_tyty (), variance);
}
void visit (ADTType &type) override
{
ctx.add_constraints_from_generic_args (type.get_orig_ref (), type, variance,
false);
}
void visit (ProjectionType &type) override
{
ctx.add_constraints_from_generic_args (type.get_orig_ref (), type, variance,
true);
}
void visit (ParamType &type) override
{
ctx.add_constrints_from_param (type, variance);
}
void visit (FnPtr &type) override
{
auto contra = ctx.contra (variance);
for (auto ¶m : type.get_params ())
{
ctx.add_constraints_from_ty (param.get_tyty (), contra);
}
ctx.add_constraints_from_ty (type.get_return_type (), variance);
}
void visit (ErrorType &type) override {}
void visit (TyTy::ConstExprType &type) override
{
// TODO: Traverse expression for variance
}
void visit (PlaceholderType &type) override { rust_unreachable (); }
void visit (InferType &type) override { rust_unreachable (); }
void visit (DynamicObjectType &type) override
{
// TODO
}
void visit (OpaqueType &type) override {}
void visit (TyTy::ConstParamType &) override {}
void visit (TyTy::ConstValueType &) override {}
void visit (TyTy::ConstInferType &) override {}
void visit (TyTy::ConstErrorType &) override {}
};
/** Per crate context for generic type variance analysis. */
class GenericTyPerCrateCtx
{
public: // External API
/** Add a type to context and process its variance constraints. */
void process_type (ADTType &ty);
/**
* Solve for all variance constraints and clear temporary data.
*
* Only keeps the results.
*/
void solve ();
/** Prints solution debug output. To be called after solve. */
void debug_print_solutions ();
tl::optional<SolutionIndex> lookup_type_index (HirId orig_ref);
public: // Module internal API
/** Format term tree to string. */
WARN_UNUSED_RESULT std::string to_string (const Term &term) const;
/** Formats as <type ident>`[`<param index>``]` */
WARN_UNUSED_RESULT std::string to_string (SolutionIndex index) const;
/** Evaluate a variance relation expression (term tree). */
Variance evaluate (Term *term);
std::vector<Variance> query_generic_variance (const ADTType &type);
FreeRegions query_field_regions (const ADTType *parent, size_t variant_index,
size_t field_index,
const FreeRegions &parent_regions);
std::vector<Region> query_type_regions (BaseType *base);
public: // Data used by visitors.
// This whole class is private, therfore members can be public.
/** Current solutions. Initiated to bivariant. */
std::vector<Variance> solutions;
/** Constrains on solutions. Iteratively applied until fixpoint. */
std::vector<Constraint> constraints;
/** Maps TyTy::orig_ref to an index of first solution for this type. */
std::unordered_map<HirId, SolutionIndex> map_from_ty_orig_ref;
};
/** Visitor context for generic type variance analysis used for processing of a
* single type. */
class GenericTyVisitorCtx : VarianceVisitorCtx<Term>
{
using Visitor = VisitorBase<Term>;
public:
explicit GenericTyVisitorCtx (GenericTyPerCrateCtx &ctx) : ctx (ctx) {}
/** Entry point: Add a type to context and process its variance constraints.
*/
void process_type (ADTType &ty);
private:
/** Resolve a type from a TyTy::ref. */
SolutionIndex lookup_or_add_type (HirId hir_id);
/** Visit an inner type and add its constraints. */
void add_constraints_from_ty (BaseType *ty, Term variance) override;
void add_constraint (SolutionIndex index, Term term);
void add_constraints_from_region (const Region ®ion, Term term) override;
void add_constraints_from_generic_args (HirId ref, SubstitutionRef &subst,
Term variance,
bool invariant_args) override;
void add_constrints_from_param (ParamType &type, Term variance) override;
/** Construct a term for type in contravaraint position. */
Term contra (Term variance) override;
private:
GenericTyPerCrateCtx &ctx;
private: // Per type processing context
/** Index of the solution first **lifetime param** for the current type. */
SolutionIndex first_lifetime = 0;
/** Index of the solution first **type param** for the current type. */
SolutionIndex first_type = 0;
/** Maps type param names to index among type params. */
std::vector<std::string> param_names;
};
/** Visitor context for basic type variance analysis. */
class TyVisitorCtx : public VarianceVisitorCtx<Variance>
{
public:
using Visitor = VisitorBase<Variance>;
TyVisitorCtx (GenericTyPerCrateCtx &ctx) : ctx (ctx) {}
std::vector<Variance> collect_variances (BaseType &ty)
{
add_constraints_from_ty (&ty, Variance::covariant ());
return variances;
}
std::vector<Region> collect_regions (BaseType &ty)
{
add_constraints_from_ty (&ty, Variance::covariant ());
return regions;
}
void add_constraints_from_ty (BaseType *ty, Variance variance) override;
void add_constraints_from_region (const Region ®ion,
Variance variance) override;
void add_constraints_from_generic_args (HirId ref, SubstitutionRef &subst,
Variance variance,
bool invariant_args) override;
void add_constrints_from_param (ParamType ¶m, Variance variance) override
{}
Variance contra (Variance variance) override;
private:
GenericTyPerCrateCtx &ctx;
std::vector<Variance> variances;
std::vector<Region> regions;
};
/** Extracts regions of a field from regions of parent ADT. */
class FieldVisitorCtx : public VarianceVisitorCtx<Variance>
{
public:
using Visitor = VisitorBase<Variance>;
FreeRegions collect_regions (BaseType &ty);
FieldVisitorCtx (GenericTyPerCrateCtx &ctx, const SubstitutionRef &subst,
const FreeRegions &parent_regions)
: ctx (ctx), subst (subst), parent_regions (parent_regions)
{}
void add_constraints_from_ty (BaseType *ty, Variance variance) override;
void add_constraints_from_region (const Region ®ion,
Variance variance) override;
void add_constraints_from_generic_args (HirId ref, SubstitutionRef &subst,
Variance variance,
bool invariant_args) override{};
void add_constrints_from_param (ParamType ¶m, Variance variance) override;
Variance contra (Variance variance) override
{
return Variance::transform (variance, Variance::contravariant ());
}
private:
GenericTyPerCrateCtx &ctx;
const SubstitutionRef &subst;
FreeRegions regions;
FreeRegions parent_regions;
std::vector<size_t> type_param_ranges;
};
} // namespace VarianceAnalysis
} // namespace TyTy
} // namespace Rust
#endif // RUST_TYTY_VARIANCE_ANALYSIS_PRIVATE_H