|
23 | 23 | } |
24 | 24 | } |
25 | 25 |
|
| 26 | +/// [`TemplateScope`] with no variables in scope. |
26 | 27 | #[derive(Clone, Default)] |
27 | 28 | pub struct EmptyTemplateScope; |
28 | 29 |
|
@@ -58,43 +59,45 @@ where |
58 | 59 | } |
59 | 60 | } |
60 | 61 |
|
| 62 | +/// Translates [`mir_ty::Ty`] to [`rty::Type`]. |
| 63 | +/// |
| 64 | +/// This struct implements a translation from Rust MIR types to Thrust types. |
| 65 | +/// Thrust types may contain refinement predicates which do not exist in MIR types, |
| 66 | +/// and [`TypeBuilder`] solely builds types with null refinement (true) in |
| 67 | +/// [`TypeBuilder::build`]. This also provides [`TypeBuilder::for_template`] to build |
| 68 | +/// refinement types by filling unknown predicates with templates with predicate variables. |
61 | 69 | #[derive(Clone)] |
62 | 70 | pub struct TypeBuilder<'tcx> { |
63 | 71 | tcx: mir_ty::TyCtxt<'tcx>, |
64 | | - type_param_mapping: HashMap<u32, rty::TypeParamIdx>, |
| 72 | + /// Maps index in [`mir_ty::ParamTy`] to [`rty::TypeParamIdx`]. |
| 73 | + /// These indices may differ because we skip lifetime parameters. |
| 74 | + /// See [`rty::TypeParamIdx`] for more details. |
| 75 | + param_idx_mapping: HashMap<u32, rty::TypeParamIdx>, |
65 | 76 | } |
66 | 77 |
|
67 | 78 | impl<'tcx> TypeBuilder<'tcx> { |
68 | 79 | pub fn new(tcx: mir_ty::TyCtxt<'tcx>, def_id: DefId) -> Self { |
69 | | - // The index of TyKind::ParamTy is based on the every generic parameters in |
70 | | - // the definition, including lifetimes. Given the following definition: |
71 | | - // |
72 | | - // struct X<'a, T> { f: &'a T } |
73 | | - // |
74 | | - // The type of field `f` is &T1 (not T0). However, in Thrust, we ignore lifetime |
75 | | - // parameters and the index of rty::ParamType is based on type parameters only. |
76 | | - // We're building a mapping from the original index to the new index here. |
77 | 80 | let generics = tcx.generics_of(def_id); |
78 | | - let mut type_param_mapping: HashMap<u32, rty::TypeParamIdx> = Default::default(); |
| 81 | + let mut param_idx_mapping: HashMap<u32, rty::TypeParamIdx> = Default::default(); |
79 | 82 | for i in 0..generics.count() { |
80 | 83 | let generic_param = generics.param_at(i, tcx); |
81 | 84 | match generic_param.kind { |
82 | 85 | mir_ty::GenericParamDefKind::Lifetime => {} |
83 | 86 | mir_ty::GenericParamDefKind::Type { .. } => { |
84 | | - type_param_mapping.insert(i as u32, type_param_mapping.len().into()); |
| 87 | + param_idx_mapping.insert(i as u32, param_idx_mapping.len().into()); |
85 | 88 | } |
86 | 89 | mir_ty::GenericParamDefKind::Const { .. } => unimplemented!(), |
87 | 90 | } |
88 | 91 | } |
89 | 92 | Self { |
90 | 93 | tcx, |
91 | | - type_param_mapping, |
| 94 | + param_idx_mapping, |
92 | 95 | } |
93 | 96 | } |
94 | 97 |
|
95 | 98 | fn translate_param_type(&self, ty: &mir_ty::ParamTy) -> rty::ParamType { |
96 | 99 | let index = *self |
97 | | - .type_param_mapping |
| 100 | + .param_idx_mapping |
98 | 101 | .get(&ty.index) |
99 | 102 | .expect("unknown type param idx"); |
100 | 103 | rty::ParamType::new(index) |
@@ -198,8 +201,16 @@ impl<'tcx> TypeBuilder<'tcx> { |
198 | 201 | } |
199 | 202 | } |
200 | 203 |
|
| 204 | +/// Translates [`mir_ty::Ty`] to [`rty::Type`] using templates for refinements. |
| 205 | +/// |
| 206 | +/// [`rty::Template`] is a refinement type in the form of `{ T | P(x1, ..., xn) }` where `P` is a |
| 207 | +/// predicate variable. When constructing a template, we need to know which variables can affect the |
| 208 | +/// predicate of the template (dependencies, `x1, ..., xn`), and they are provided by the |
| 209 | +/// [`TemplateScope`]. No variables are in scope by default and you can provide a scope using |
| 210 | +/// [`TemplateTypeBuilder::with_scope`]. |
201 | 211 | pub struct TemplateTypeBuilder<'tcx, 'a, R, S> { |
202 | 212 | inner: TypeBuilder<'tcx>, |
| 213 | + // XXX: this can't be simply `R` because monomorphization instantiates types recursively |
203 | 214 | registry: &'a mut R, |
204 | 215 | scope: S, |
205 | 216 | } |
@@ -310,6 +321,7 @@ where |
310 | 321 | } |
311 | 322 | } |
312 | 323 |
|
| 324 | +/// A builder for function template types. |
313 | 325 | pub struct FunctionTemplateTypeBuilder<'tcx, 'a, R> { |
314 | 326 | inner: TypeBuilder<'tcx>, |
315 | 327 | registry: &'a mut R, |
|
0 commit comments