1 #ifndef RUST_TYTY_VARIANCE_ANALYSIS_H
2 #define RUST_TYTY_VARIANCE_ANALYSIS_H
6 #include <rust-bir-free-region.h>
10 namespace VarianceAnalysis
{
13 class GenericTyPerCrateCtx
;
15 /** Per crate context for variance analysis. */
22 /** Add type to variance analysis context. */
23 void add_type_constraints (ADTType
&type
);
25 /** Solve all constraints and print debug output. */
28 /** Get variance of a type parameters. */
29 std::vector
<Variance
> query_generic_variance (const ADTType
&type
);
31 /** Get variance of a type body (members, fn parameters...). */
32 std::vector
<Variance
> query_type_variances (BaseType
*type
);
34 /** Get regions mentioned in a type. */
35 std::vector
<Region
> query_type_regions (BaseType
*type
);
36 std::vector
<size_t> query_field_regions (const ADTType
*parent
,
39 const FreeRegions
&parent_regions
);
42 std::unique_ptr
<GenericTyPerCrateCtx
> private_ctx
;
46 query_field_regions (const ADTType
*parent
, size_t variant_index
,
47 size_t field_index
, const FreeRegions
&parent_regions
);
49 /** Variance semilattice */
54 BIVARIANT
= 0, // 0b00
55 COVARIANT
= 1, // 0b01
56 CONTRAVARIANT
= 2, // 0b10
57 INVARIANT
= 3, // 0b11
60 static constexpr auto TOP
= BIVARIANT
;
61 static constexpr auto BOTTOM
= INVARIANT
;
63 constexpr Variance (Kind kind
) : kind (kind
) {}
66 constexpr Variance () : kind (TOP
) {}
68 constexpr bool is_bivariant () const { return kind
== BIVARIANT
; }
69 constexpr bool is_covariant () const { return kind
== COVARIANT
; }
70 constexpr bool is_contravariant () const { return kind
== CONTRAVARIANT
; }
71 constexpr bool is_invariant () const { return kind
== INVARIANT
; }
73 static constexpr Variance
bivariant () { return {BIVARIANT
}; }
74 static constexpr Variance
covariant () { return {COVARIANT
}; }
75 static constexpr Variance
contravariant () { return {CONTRAVARIANT
}; }
76 static constexpr Variance
invariant () { return {INVARIANT
}; }
78 WARN_UNUSED_RESULT Variance
reverse () const;
79 static WARN_UNUSED_RESULT Variance
join (Variance lhs
, Variance rhs
);
81 void join (Variance rhs
);
84 * Variance composition function.
86 * For `A<X>` and `B<X>` and the composition `A<B<X>>` the variance of
87 * `v(A<B<X>>, X)` is defined as:
89 * v(A<B<X>>, X) = v(A<X>, X).transform(v(B<X>, X))
92 static WARN_UNUSED_RESULT Variance
transform (Variance lhs
, Variance rhs
);
94 constexpr friend bool operator== (const Variance
&lhs
, const Variance
&rhs
)
96 return lhs
.kind
== rhs
.kind
;
98 constexpr friend bool operator!= (const Variance
&lhs
, const Variance
&rhs
)
100 return !(lhs
== rhs
);
103 WARN_UNUSED_RESULT
std::string
as_string () const;
106 } // namespace VarianceAnalysis
111 #endif // RUST_TYTY_VARIANCE_ANALYSIS_H