diff options
Diffstat (limited to 'crates/hir_ty/src/builder.rs')
-rw-r--r-- | crates/hir_ty/src/builder.rs | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/crates/hir_ty/src/builder.rs b/crates/hir_ty/src/builder.rs new file mode 100644 index 000000000..0bac31e4c --- /dev/null +++ b/crates/hir_ty/src/builder.rs | |||
@@ -0,0 +1,219 @@ | |||
1 | //! `TyBuilder`, a helper for building instances of `Ty` and related types. | ||
2 | |||
3 | use std::iter; | ||
4 | |||
5 | use chalk_ir::{ | ||
6 | cast::{Cast, CastTo, Caster}, | ||
7 | interner::HasInterner, | ||
8 | AdtId, BoundVar, DebruijnIndex, Safety, Scalar, | ||
9 | }; | ||
10 | use hir_def::{builtin_type::BuiltinType, GenericDefId, TraitId, TypeAliasId}; | ||
11 | use smallvec::SmallVec; | ||
12 | |||
13 | use crate::{ | ||
14 | db::HirDatabase, primitive, to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders, | ||
15 | CallableSig, FnPointer, FnSig, GenericArg, Interner, ProjectionTy, Substitution, TraitRef, Ty, | ||
16 | TyDefId, TyKind, TypeWalk, ValueTyDefId, | ||
17 | }; | ||
18 | |||
19 | /// This is a builder for `Ty` or anything that needs a `Substitution`. | ||
20 | pub struct TyBuilder<D> { | ||
21 | /// The `data` field is used to keep track of what we're building (e.g. an | ||
22 | /// ADT, a `TraitRef`, ...). | ||
23 | data: D, | ||
24 | vec: SmallVec<[GenericArg; 2]>, | ||
25 | param_count: usize, | ||
26 | } | ||
27 | |||
28 | impl<D> TyBuilder<D> { | ||
29 | fn new(data: D, param_count: usize) -> TyBuilder<D> { | ||
30 | TyBuilder { data, param_count, vec: SmallVec::with_capacity(param_count) } | ||
31 | } | ||
32 | |||
33 | fn build_internal(self) -> (D, Substitution) { | ||
34 | assert_eq!(self.vec.len(), self.param_count); | ||
35 | // FIXME: would be good to have a way to construct a chalk_ir::Substitution from the interned form | ||
36 | let subst = Substitution::intern(self.vec); | ||
37 | (self.data, subst) | ||
38 | } | ||
39 | |||
40 | pub fn push(mut self, arg: impl CastTo<GenericArg>) -> Self { | ||
41 | self.vec.push(arg.cast(&Interner)); | ||
42 | self | ||
43 | } | ||
44 | |||
45 | pub fn remaining(&self) -> usize { | ||
46 | self.param_count - self.vec.len() | ||
47 | } | ||
48 | |||
49 | pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self { | ||
50 | self.fill( | ||
51 | (starting_from..) | ||
52 | .map(|idx| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)), | ||
53 | ) | ||
54 | } | ||
55 | |||
56 | pub fn fill_with_unknown(self) -> Self { | ||
57 | self.fill(iter::repeat(TyKind::Unknown.intern(&Interner))) | ||
58 | } | ||
59 | |||
60 | pub fn fill(mut self, filler: impl Iterator<Item = impl CastTo<GenericArg>>) -> Self { | ||
61 | self.vec.extend(filler.take(self.remaining()).casted(&Interner)); | ||
62 | assert_eq!(self.remaining(), 0); | ||
63 | self | ||
64 | } | ||
65 | |||
66 | pub fn use_parent_substs(mut self, parent_substs: &Substitution) -> Self { | ||
67 | assert!(self.vec.is_empty()); | ||
68 | assert!(parent_substs.len(&Interner) <= self.param_count); | ||
69 | self.vec.extend(parent_substs.iter(&Interner).cloned()); | ||
70 | self | ||
71 | } | ||
72 | } | ||
73 | |||
74 | impl TyBuilder<()> { | ||
75 | pub fn unit() -> Ty { | ||
76 | TyKind::Tuple(0, Substitution::empty(&Interner)).intern(&Interner) | ||
77 | } | ||
78 | |||
79 | pub fn fn_ptr(sig: CallableSig) -> Ty { | ||
80 | TyKind::Function(FnPointer { | ||
81 | num_args: sig.params().len(), | ||
82 | sig: FnSig { abi: (), safety: Safety::Safe, variadic: sig.is_varargs }, | ||
83 | substs: Substitution::from_iter(&Interner, sig.params_and_return.iter().cloned()), | ||
84 | }) | ||
85 | .intern(&Interner) | ||
86 | } | ||
87 | |||
88 | pub fn builtin(builtin: BuiltinType) -> Ty { | ||
89 | match builtin { | ||
90 | BuiltinType::Char => TyKind::Scalar(Scalar::Char).intern(&Interner), | ||
91 | BuiltinType::Bool => TyKind::Scalar(Scalar::Bool).intern(&Interner), | ||
92 | BuiltinType::Str => TyKind::Str.intern(&Interner), | ||
93 | BuiltinType::Int(t) => { | ||
94 | TyKind::Scalar(Scalar::Int(primitive::int_ty_from_builtin(t))).intern(&Interner) | ||
95 | } | ||
96 | BuiltinType::Uint(t) => { | ||
97 | TyKind::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(t))).intern(&Interner) | ||
98 | } | ||
99 | BuiltinType::Float(t) => { | ||
100 | TyKind::Scalar(Scalar::Float(primitive::float_ty_from_builtin(t))).intern(&Interner) | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | |||
105 | pub fn type_params_subst(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> Substitution { | ||
106 | let params = generics(db.upcast(), def.into()); | ||
107 | params.type_params_subst(db) | ||
108 | } | ||
109 | |||
110 | pub fn subst_for_def(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> TyBuilder<()> { | ||
111 | let def = def.into(); | ||
112 | let params = generics(db.upcast(), def); | ||
113 | let param_count = params.len(); | ||
114 | TyBuilder::new((), param_count) | ||
115 | } | ||
116 | |||
117 | pub fn build(self) -> Substitution { | ||
118 | let ((), subst) = self.build_internal(); | ||
119 | subst | ||
120 | } | ||
121 | } | ||
122 | |||
123 | impl TyBuilder<hir_def::AdtId> { | ||
124 | pub fn adt(db: &dyn HirDatabase, adt: hir_def::AdtId) -> TyBuilder<hir_def::AdtId> { | ||
125 | let generics = generics(db.upcast(), adt.into()); | ||
126 | let param_count = generics.len(); | ||
127 | TyBuilder::new(adt, param_count) | ||
128 | } | ||
129 | |||
130 | pub fn fill_with_defaults( | ||
131 | mut self, | ||
132 | db: &dyn HirDatabase, | ||
133 | mut fallback: impl FnMut() -> Ty, | ||
134 | ) -> Self { | ||
135 | let defaults = db.generic_defaults(self.data.into()); | ||
136 | for default_ty in defaults.iter().skip(self.vec.len()) { | ||
137 | if default_ty.skip_binders().is_unknown() { | ||
138 | self.vec.push(fallback().cast(&Interner)); | ||
139 | } else { | ||
140 | // each default can depend on the previous parameters | ||
141 | let subst_so_far = Substitution::intern(self.vec.clone()); | ||
142 | self.vec.push(default_ty.clone().subst(&subst_so_far).cast(&Interner)); | ||
143 | } | ||
144 | } | ||
145 | self | ||
146 | } | ||
147 | |||
148 | pub fn build(self) -> Ty { | ||
149 | let (adt, subst) = self.build_internal(); | ||
150 | TyKind::Adt(AdtId(adt), subst).intern(&Interner) | ||
151 | } | ||
152 | } | ||
153 | |||
154 | pub struct Tuple(usize); | ||
155 | impl TyBuilder<Tuple> { | ||
156 | pub fn tuple(size: usize) -> TyBuilder<Tuple> { | ||
157 | TyBuilder::new(Tuple(size), size) | ||
158 | } | ||
159 | |||
160 | pub fn build(self) -> Ty { | ||
161 | let (Tuple(size), subst) = self.build_internal(); | ||
162 | TyKind::Tuple(size, subst).intern(&Interner) | ||
163 | } | ||
164 | } | ||
165 | |||
166 | impl TyBuilder<TraitId> { | ||
167 | pub fn trait_ref(db: &dyn HirDatabase, trait_id: TraitId) -> TyBuilder<TraitId> { | ||
168 | let generics = generics(db.upcast(), trait_id.into()); | ||
169 | let param_count = generics.len(); | ||
170 | TyBuilder::new(trait_id, param_count) | ||
171 | } | ||
172 | |||
173 | pub fn build(self) -> TraitRef { | ||
174 | let (trait_id, substitution) = self.build_internal(); | ||
175 | TraitRef { trait_id: to_chalk_trait_id(trait_id), substitution } | ||
176 | } | ||
177 | } | ||
178 | |||
179 | impl TyBuilder<TypeAliasId> { | ||
180 | pub fn assoc_type_projection( | ||
181 | db: &dyn HirDatabase, | ||
182 | type_alias: TypeAliasId, | ||
183 | ) -> TyBuilder<TypeAliasId> { | ||
184 | let generics = generics(db.upcast(), type_alias.into()); | ||
185 | let param_count = generics.len(); | ||
186 | TyBuilder::new(type_alias, param_count) | ||
187 | } | ||
188 | |||
189 | pub fn build(self) -> ProjectionTy { | ||
190 | let (type_alias, substitution) = self.build_internal(); | ||
191 | ProjectionTy { associated_ty_id: to_assoc_type_id(type_alias), substitution } | ||
192 | } | ||
193 | } | ||
194 | |||
195 | impl<T: TypeWalk + HasInterner<Interner = Interner>> TyBuilder<Binders<T>> { | ||
196 | fn subst_binders(b: Binders<T>) -> Self { | ||
197 | let param_count = b.num_binders; | ||
198 | TyBuilder::new(b, param_count) | ||
199 | } | ||
200 | |||
201 | pub fn build(self) -> T { | ||
202 | let (b, subst) = self.build_internal(); | ||
203 | b.subst(&subst) | ||
204 | } | ||
205 | } | ||
206 | |||
207 | impl TyBuilder<Binders<Ty>> { | ||
208 | pub fn def_ty(db: &dyn HirDatabase, def: TyDefId) -> TyBuilder<Binders<Ty>> { | ||
209 | TyBuilder::subst_binders(db.ty(def.into())) | ||
210 | } | ||
211 | |||
212 | pub fn impl_self_ty(db: &dyn HirDatabase, def: hir_def::ImplId) -> TyBuilder<Binders<Ty>> { | ||
213 | TyBuilder::subst_binders(db.impl_self_ty(def)) | ||
214 | } | ||
215 | |||
216 | pub fn value_ty(db: &dyn HirDatabase, def: ValueTyDefId) -> TyBuilder<Binders<Ty>> { | ||
217 | TyBuilder::subst_binders(db.value_ty(def)) | ||
218 | } | ||
219 | } | ||