| 1 |
#ifndef RUST_TYTY_VARIANCE_ANALYSIS_PRIVATE_H |
| 2 |
#define RUST_TYTY_VARIANCE_ANALYSIS_PRIVATE_H |
| 3 |
|
| 4 |
#include "rust-tyty-variance-analysis.h" |
| 5 |
|
| 6 |
#include "rust-tyty-visitor.h" |
| 7 |
|
| 8 |
namespace Rust { |
| 9 |
namespace TyTy { |
| 10 |
namespace VarianceAnalysis { |
| 11 |
|
| 12 |
using SolutionIndex = uint32_t; |
| 13 |
|
| 14 |
/** Term descibing variance relations. */ |
| 15 |
struct Term |
| 16 |
{ |
| 17 |
enum Kind : uint8_t |
| 18 |
{ |
| 19 |
CONST, |
| 20 |
REF, |
| 21 |
TRANSFORM, |
| 22 |
}; |
| 23 |
|
| 24 |
Kind kind; |
| 25 |
union |
| 26 |
{ |
| 27 |
struct |
| 28 |
{ |
| 29 |
Term *lhs; |
| 30 |
Term *rhs; |
| 31 |
} transform; |
| 32 |
SolutionIndex ref; |
| 33 |
Variance const_val; |
| 34 |
}; |
| 35 |
|
| 36 |
Term () {} |
| 37 |
|
| 38 |
Term (Variance variance) : kind (CONST), const_val (variance) {} |
| 39 |
|
| 40 |
WARN_UNUSED_RESULT bool is_const () const { return kind == CONST; } |
| 41 |
|
| 42 |
static Term make_ref (SolutionIndex index); |
| 43 |
|
| 44 |
static Term make_transform (Term lhs, Term rhs); |
| 45 |
}; |
| 46 |
|
| 47 |
/** Variance constraint of a type parameter. */ |
| 48 |
struct Constraint |
| 49 |
{ |
| 50 |
SolutionIndex target_index; |
| 51 |
Term *term; |
| 52 |
}; |
| 53 |
|
| 54 |
/** Abstract variance visitor context. */ |
| 55 |
template <typename VARIANCE> class VarianceVisitorCtx |
| 56 |
{ |
| 57 |
public: |
| 58 |
virtual ~VarianceVisitorCtx () = default; |
| 59 |
|
| 60 |
virtual void add_constraints_from_ty (BaseType *ty, VARIANCE variance) = 0; |
| 61 |
virtual void add_constraints_from_region (const Region ®ion, |
| 62 |
VARIANCE variance) |
| 63 |
= 0; |
| 64 |
void add_constraints_from_mutability (BaseType *type, Mutability mutability, |
| 65 |
VARIANCE variance) |
| 66 |
{ |
| 67 |
switch (mutability) |
| 68 |
{ |
| 69 |
case Mutability::Imm: |
| 70 |
return add_constraints_from_ty (type, variance); |
| 71 |
case Mutability::Mut: |
| 72 |
return add_constraints_from_ty (type, Variance::invariant ()); |
| 73 |
} |
| 74 |
} |
| 75 |
virtual void |
| 76 |
add_constraints_from_generic_args (HirId ref, SubstitutionRef &subst, |
| 77 |
VARIANCE variance, bool invariant_args) |
| 78 |
= 0; |
| 79 |
virtual void add_constrints_from_param (ParamType ¶m, VARIANCE variance) |
| 80 |
= 0; |
| 81 |
virtual VARIANCE contra (VARIANCE variance) = 0; |
| 82 |
}; |
| 83 |
|
| 84 |
template <typename VARIANCE> class VisitorBase final : public TyVisitor |
| 85 |
{ |
| 86 |
VarianceVisitorCtx<VARIANCE> &ctx; |
| 87 |
VARIANCE variance; |
| 88 |
|
| 89 |
public: |
| 90 |
VisitorBase (VarianceVisitorCtx<VARIANCE> &ctx, VARIANCE variance) |
| 91 |
: ctx (ctx), variance (variance) |
| 92 |
{} |
| 93 |
|
| 94 |
void visit (BoolType &type) override {} |
| 95 |
void visit (CharType &type) override {} |
| 96 |
void visit (IntType &type) override {} |
| 97 |
void visit (UintType &type) override {} |
| 98 |
void visit (FloatType &type) override {} |
| 99 |
void visit (USizeType &type) override {} |
| 100 |
void visit (ISizeType &type) override {} |
| 101 |
void visit (StrType &type) override {} |
| 102 |
void visit (NeverType &type) override {} |
| 103 |
|
| 104 |
void visit (ClosureType &type) override {} |
| 105 |
void visit (FnType &type) override |
| 106 |
{ |
| 107 |
for (auto ®ion : type.get_used_arguments ().get_regions ()) |
| 108 |
ctx.add_constraints_from_region (region, Variance::invariant ()); |
| 109 |
} |
| 110 |
|
| 111 |
void visit (ReferenceType &type) override |
| 112 |
{ |
| 113 |
ctx.add_constraints_from_region (type.get_region (), variance); |
| 114 |
ctx.add_constraints_from_mutability (type.get_base (), type.mutability (), |
| 115 |
variance); |
| 116 |
} |
| 117 |
void visit (ArrayType &type) override |
| 118 |
{ |
| 119 |
ctx.add_constraints_from_ty (type.get_element_type (), variance); |
| 120 |
} |
| 121 |
void visit (SliceType &type) override |
| 122 |
{ |
| 123 |
ctx.add_constraints_from_ty (type.get_element_type (), variance); |
| 124 |
} |
| 125 |
void visit (PointerType &type) override |
| 126 |
{ |
| 127 |
ctx.add_constraints_from_ty (type.get_base (), variance); |
| 128 |
ctx.add_constraints_from_mutability (type.get_base (), type.mutability (), |
| 129 |
variance); |
| 130 |
} |
| 131 |
void visit (TupleType &type) override |
| 132 |
{ |
| 133 |
for (auto &elem : type.get_fields ()) |
| 134 |
ctx.add_constraints_from_ty (elem.get_tyty (), variance); |
| 135 |
} |
| 136 |
void visit (ADTType &type) override |
| 137 |
{ |
| 138 |
ctx.add_constraints_from_generic_args (type.get_orig_ref (), type, variance, |
| 139 |
false); |
| 140 |
} |
| 141 |
void visit (ProjectionType &type) override |
| 142 |
{ |
| 143 |
ctx.add_constraints_from_generic_args (type.get_orig_ref (), type, variance, |
| 144 |
true); |
| 145 |
} |
| 146 |
void visit (ParamType &type) override |
| 147 |
{ |
| 148 |
ctx.add_constrints_from_param (type, variance); |
| 149 |
} |
| 150 |
void visit (FnPtr &type) override |
| 151 |
{ |
| 152 |
auto contra = ctx.contra (variance); |
| 153 |
|
| 154 |
for (auto ¶m : type.get_params ()) |
| 155 |
{ |
| 156 |
ctx.add_constraints_from_ty (param.get_tyty (), contra); |
| 157 |
} |
| 158 |
|
| 159 |
ctx.add_constraints_from_ty (type.get_return_type (), variance); |
| 160 |
} |
| 161 |
|
| 162 |
void visit (ErrorType &type) override {} |
| 163 |
|
| 164 |
void visit (PlaceholderType &type) override { rust_unreachable (); } |
| 165 |
void visit (InferType &type) override { rust_unreachable (); } |
| 166 |
|
| 167 |
void visit (DynamicObjectType &type) override |
| 168 |
{ |
| 169 |
// TODO |
| 170 |
} |
| 171 |
|
| 172 |
void visit (OpaqueType &type) override {} |
| 173 |
}; |
| 174 |
|
| 175 |
/** Per crate context for generic type variance analysis. */ |
| 176 |
class GenericTyPerCrateCtx |
| 177 |
{ |
| 178 |
public: // External API |
| 179 |
/** Add a type to context and process its variance constraints. */ |
| 180 |
void process_type (ADTType &ty); |
| 181 |
|
| 182 |
/** |
| 183 |
* Solve for all variance constraints and clear temporary data. |
| 184 |
* |
| 185 |
* Only keeps the results. |
| 186 |
*/ |
| 187 |
void solve (); |
| 188 |
|
| 189 |
/** Prints solution debug output. To be called after solve. */ |
| 190 |
void debug_print_solutions (); |
| 191 |
|
| 192 |
tl::optional<SolutionIndex> lookup_type_index (HirId orig_ref); |
| 193 |
|
| 194 |
public: // Module internal API |
| 195 |
/** Format term tree to string. */ |
| 196 |
WARN_UNUSED_RESULT std::string to_string (const Term &term) const; |
| 197 |
|
| 198 |
/** Formats as <type ident>`[`<param index>``]` */ |
| 199 |
WARN_UNUSED_RESULT std::string to_string (SolutionIndex index) const; |
| 200 |
|
| 201 |
/** Evaluate a variance relation expression (term tree). */ |
| 202 |
Variance evaluate (Term *term); |
| 203 |
|
| 204 |
std::vector<Variance> query_generic_variance (const ADTType &type); |
| 205 |
|
| 206 |
FreeRegions query_field_regions (const ADTType *parent, size_t variant_index, |
| 207 |
size_t field_index, |
| 208 |
const FreeRegions &parent_regions); |
| 209 |
|
| 210 |
std::vector<Region> query_type_regions (BaseType *base); |
| 211 |
|
| 212 |
public: // Data used by visitors. |
| 213 |
// This whole class is private, therfore members can be public. |
| 214 |
|
| 215 |
/** Current solutions. Initiated to bivariant. */ |
| 216 |
std::vector<Variance> solutions; |
| 217 |
|
| 218 |
/** Constrains on solutions. Iteratively applied until fixpoint. */ |
| 219 |
std::vector<Constraint> constraints; |
| 220 |
|
| 221 |
/** Maps TyTy::orig_ref to an index of first solution for this type. */ |
| 222 |
std::unordered_map<HirId, SolutionIndex> map_from_ty_orig_ref; |
| 223 |
}; |
| 224 |
|
| 225 |
/** Visitor context for generic type variance analysis used for processing of a |
| 226 |
* single type. */ |
| 227 |
class GenericTyVisitorCtx : VarianceVisitorCtx<Term> |
| 228 |
{ |
| 229 |
using Visitor = VisitorBase<Term>; |
| 230 |
|
| 231 |
public: |
| 232 |
explicit GenericTyVisitorCtx (GenericTyPerCrateCtx &ctx) : ctx (ctx) {} |
| 233 |
/** Entry point: Add a type to context and process its variance constraints. |
| 234 |
*/ |
| 235 |
void process_type (ADTType &ty); |
| 236 |
|
| 237 |
private: |
| 238 |
/** Resolve a type from a TyTy::ref. */ |
| 239 |
SolutionIndex lookup_or_add_type (HirId hir_id); |
| 240 |
|
| 241 |
/** Visit an inner type and add its constraints. */ |
| 242 |
void add_constraints_from_ty (BaseType *ty, Term variance) override; |
| 243 |
|
| 244 |
void add_constraint (SolutionIndex index, Term term); |
| 245 |
|
| 246 |
void add_constraints_from_region (const Region ®ion, Term term) override; |
| 247 |
|
| 248 |
void add_constraints_from_generic_args (HirId ref, SubstitutionRef &subst, |
| 249 |
Term variance, |
| 250 |
bool invariant_args) override; |
| 251 |
|
| 252 |
void add_constrints_from_param (ParamType &type, Term variance) override; |
| 253 |
|
| 254 |
/** Construct a term for type in contravaraint position. */ |
| 255 |
Term contra (Term variance) override; |
| 256 |
|
| 257 |
private: |
| 258 |
GenericTyPerCrateCtx &ctx; |
| 259 |
|
| 260 |
private: // Per type processing context |
| 261 |
/** Index of the solution first **lifetime param** for the current type. */ |
| 262 |
SolutionIndex first_lifetime = 0; |
| 263 |
|
| 264 |
/** Index of the solution first **type param** for the current type. */ |
| 265 |
SolutionIndex first_type = 0; |
| 266 |
|
| 267 |
/** Maps type param names to index among type params. */ |
| 268 |
std::vector<std::string> param_names; |
| 269 |
}; |
| 270 |
|
| 271 |
/** Visitor context for basic type variance analysis. */ |
| 272 |
class TyVisitorCtx : public VarianceVisitorCtx<Variance> |
| 273 |
{ |
| 274 |
public: |
| 275 |
using Visitor = VisitorBase<Variance>; |
| 276 |
|
| 277 |
TyVisitorCtx (GenericTyPerCrateCtx &ctx) : ctx (ctx) {} |
| 278 |
|
| 279 |
std::vector<Variance> collect_variances (BaseType &ty) |
| 280 |
{ |
| 281 |
add_constraints_from_ty (&ty, Variance::covariant ()); |
| 282 |
return variances; |
| 283 |
} |
| 284 |
|
| 285 |
std::vector<Region> collect_regions (BaseType &ty) |
| 286 |
{ |
| 287 |
add_constraints_from_ty (&ty, Variance::covariant ()); |
| 288 |
return regions; |
| 289 |
} |
| 290 |
|
| 291 |
void add_constraints_from_ty (BaseType *ty, Variance variance) override; |
| 292 |
void add_constraints_from_region (const Region ®ion, |
| 293 |
Variance variance) override; |
| 294 |
void add_constraints_from_generic_args (HirId ref, SubstitutionRef &subst, |
| 295 |
Variance variance, |
| 296 |
bool invariant_args) override; |
| 297 |
void add_constrints_from_param (ParamType ¶m, Variance variance) override |
| 298 |
{} |
| 299 |
Variance contra (Variance variance) override; |
| 300 |
|
| 301 |
private: |
| 302 |
GenericTyPerCrateCtx &ctx; |
| 303 |
std::vector<Variance> variances; |
| 304 |
std::vector<Region> regions; |
| 305 |
}; |
| 306 |
|
| 307 |
/** Extracts regions of a field from regions of parent ADT. */ |
| 308 |
class FieldVisitorCtx : public VarianceVisitorCtx<Variance> |
| 309 |
{ |
| 310 |
public: |
| 311 |
using Visitor = VisitorBase<Variance>; |
| 312 |
|
| 313 |
FreeRegions collect_regions (BaseType &ty); |
| 314 |
|
| 315 |
FieldVisitorCtx (GenericTyPerCrateCtx &ctx, const SubstitutionRef &subst, |
| 316 |
const FreeRegions &parent_regions) |
| 317 |
: ctx (ctx), subst (subst), parent_regions (parent_regions) |
| 318 |
{} |
| 319 |
|
| 320 |
void add_constraints_from_ty (BaseType *ty, Variance variance) override; |
| 321 |
void add_constraints_from_region (const Region ®ion, |
| 322 |
Variance variance) override; |
| 323 |
void add_constraints_from_generic_args (HirId ref, SubstitutionRef &subst, |
| 324 |
Variance variance, |
| 325 |
bool invariant_args) override{}; |
| 326 |
void add_constrints_from_param (ParamType ¶m, Variance variance) override; |
| 327 |
|
| 328 |
Variance contra (Variance variance) override |
| 329 |
{ |
| 330 |
return Variance::transform (variance, Variance::contravariant ()); |
| 331 |
} |
| 332 |
|
| 333 |
private: |
| 334 |
GenericTyPerCrateCtx &ctx; |
| 335 |
const SubstitutionRef &subst; |
| 336 |
FreeRegions regions; |
| 337 |
FreeRegions parent_regions; |
| 338 |
std::vector<size_t> type_param_ranges; |
| 339 |
}; |
| 340 |
|
| 341 |
} // namespace VarianceAnalysis |
| 342 |
|
| 343 |
} // namespace TyTy |
| 344 |
} // namespace Rust |
| 345 |
|
| 346 |
#endif // RUST_TYTY_VARIANCE_ANALYSIS_PRIVATE_H |