1use crate::ast::*;
3use crate::ids::IndexVec;
4use derive_generic_visitor::*;
5use index_vec::Idx;
6use itertools::Itertools;
7use std::borrow::Cow;
8use std::collections::HashSet;
9use std::convert::Infallible;
10use std::fmt::Debug;
11use std::iter::Iterator;
12use std::mem;
13
14impl TraitParam {
15 pub fn identity_tref(&self) -> TraitRef {
17 self.identity_tref_at_depth(DeBruijnId::zero())
18 }
19
20 pub fn identity_tref_at_depth(&self, depth: DeBruijnId) -> TraitRef {
22 TraitRef::new(
23 TraitRefKind::Clause(DeBruijnVar::bound(depth, self.clause_id)),
24 self.trait_.clone().move_under_binders(depth),
25 )
26 }
27}
28
29impl GenericParams {
30 pub fn empty() -> Self {
31 Self::default()
32 }
33
34 pub fn is_empty(&self) -> bool {
35 self.len() == 0
36 }
37 pub fn has_explicits(&self) -> bool {
39 !self.regions.is_empty() || !self.types.is_empty() || !self.const_generics.is_empty()
40 }
41 pub fn has_predicates(&self) -> bool {
44 !self.trait_clauses.is_empty()
45 || !self.types_outlive.is_empty()
46 || !self.regions_outlive.is_empty()
47 || !self.trait_type_constraints.is_empty()
48 }
49
50 pub fn check_consistency(&self) {
52 assert!(
54 self.trait_clauses
55 .iter()
56 .enumerate()
57 .all(|(i, c)| c.clause_id.index() == i)
58 );
59
60 let mut s = HashSet::new();
65 for r in &self.regions {
66 if let Some(name) = &r.name {
67 assert!(
68 !s.contains(name),
69 "Name \"{}\" reused for two different lifetimes",
70 name
71 );
72 s.insert(name);
73 }
74 }
75 }
76
77 pub fn len(&self) -> usize {
78 let GenericParams {
79 regions,
80 types,
81 const_generics,
82 trait_clauses,
83 regions_outlive,
84 types_outlive,
85 trait_type_constraints,
86 } = self;
87 regions.len()
88 + types.len()
89 + const_generics.len()
90 + trait_clauses.len()
91 + regions_outlive.len()
92 + types_outlive.len()
93 + trait_type_constraints.len()
94 }
95
96 pub fn identity_args(&self) -> GenericArgs {
100 self.identity_args_at_depth(DeBruijnId::zero())
101 }
102
103 pub fn identity_args_at_depth(&self, depth: DeBruijnId) -> GenericArgs {
105 GenericArgs {
106 regions: self
107 .regions
108 .map_ref_indexed(|id, _| Region::Var(DeBruijnVar::bound(depth, id))),
109 types: self
110 .types
111 .map_ref_indexed(|id, _| TyKind::TypeVar(DeBruijnVar::bound(depth, id)).into_ty()),
112 const_generics: self.const_generics.map_ref_indexed(|id, c| ConstantExpr {
113 ty: c.ty.clone(),
114 kind: ConstantExprKind::Var(DeBruijnVar::bound(depth, id)),
115 }),
116 trait_refs: self
117 .trait_clauses
118 .map_ref(|clause| clause.identity_tref_at_depth(depth)),
119 }
120 }
121
122 pub fn take_predicates_from(&mut self, other: GenericParams) {
125 assert!(!other.has_explicits());
126 let num_clauses = self.trait_clauses.len();
127 let GenericParams {
128 regions: _,
129 types: _,
130 const_generics: _,
131 trait_clauses,
132 regions_outlive,
133 types_outlive,
134 trait_type_constraints,
135 } = other;
136 self.trait_clauses
137 .extend(trait_clauses.into_iter().update(|clause| {
138 clause.clause_id += num_clauses;
139 }));
140 self.regions_outlive.extend(regions_outlive);
141 self.types_outlive.extend(types_outlive);
142 self.trait_type_constraints.extend(trait_type_constraints);
143 }
144
145 pub fn merge_predicates_from(&mut self, mut other: GenericParams) {
149 other.types.clear();
151 other.regions.clear();
152 other.const_generics.clear();
153 struct ShiftClausesVisitor(usize);
155 impl VarsVisitor for ShiftClausesVisitor {
156 fn visit_clause_var(&mut self, v: ClauseDbVar) -> Option<TraitRefKind> {
157 if let DeBruijnVar::Bound(DeBruijnId::ZERO, clause_id) = v {
158 Some(TraitRefKind::Clause(DeBruijnVar::Bound(
160 DeBruijnId::ZERO,
161 clause_id + self.0,
162 )))
163 } else {
164 None
165 }
166 }
167 }
168 let num_clauses = self.trait_clauses.len();
169 other.visit_vars(&mut ShiftClausesVisitor(num_clauses));
170 self.take_predicates_from(other);
171 }
172}
173
174impl<T> Binder<T> {
175 pub fn empty(kind: BinderKind, x: T) -> Self
177 where
178 T: TyVisitable,
179 {
180 Binder {
181 params: Default::default(),
182 skip_binder: x.move_under_binder(),
183 kind,
184 }
185 }
186 pub fn new(kind: BinderKind, params: GenericParams, skip_binder: T) -> Self {
187 Self {
188 params,
189 skip_binder,
190 kind,
191 }
192 }
193
194 pub fn binds_anything(&self) -> bool {
196 !self.params.is_empty()
197 }
198
199 pub fn get_if_binds_nothing(&self) -> Option<T>
202 where
203 T: TyVisitable + Clone,
204 {
205 self.params
206 .is_empty()
207 .then(|| self.skip_binder.clone().move_from_under_binder().unwrap())
208 }
209
210 pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Binder<U> {
211 Binder {
212 params: self.params,
213 skip_binder: f(self.skip_binder),
214 kind: self.kind.clone(),
215 }
216 }
217
218 pub fn map_ref<U>(&self, f: impl FnOnce(&T) -> U) -> Binder<U> {
219 Binder {
220 params: self.params.clone(),
221 skip_binder: f(&self.skip_binder),
222 kind: self.kind.clone(),
223 }
224 }
225
226 pub fn apply(self, args: &GenericArgs) -> T
229 where
230 T: TyVisitable,
231 {
232 self.skip_binder.substitute(args)
233 }
234}
235
236impl<T: AstVisitable> Binder<Binder<T>> {
237 pub fn flatten(self) -> Binder<T> {
239 #[derive(Visitor)]
240 struct FlattenVisitor<'a> {
241 shift_by: &'a GenericParams,
242 binder_depth: DeBruijnId,
243 }
244 impl VisitorWithBinderDepth for FlattenVisitor<'_> {
245 fn binder_depth_mut(&mut self) -> &mut DeBruijnId {
246 &mut self.binder_depth
247 }
248 }
249 impl VisitAstMut for FlattenVisitor<'_> {
250 fn visit<T: AstVisitable>(&mut self, x: &mut T) -> ControlFlow<Self::Break> {
251 VisitWithBinderDepth::new(self).visit(x)
252 }
253
254 fn enter_de_bruijn_id(&mut self, db_id: &mut DeBruijnId) {
255 if *db_id > self.binder_depth {
256 *db_id = db_id.decr();
261 }
262 }
263 fn enter_region(&mut self, x: &mut Region) {
264 if let Region::Var(var) = x
265 && let Some(id) = var.bound_at_depth_mut(self.binder_depth)
266 {
267 *id += self.shift_by.regions.len();
268 }
269 }
270 fn enter_ty_kind(&mut self, x: &mut TyKind) {
271 if let TyKind::TypeVar(var) = x
272 && let Some(id) = var.bound_at_depth_mut(self.binder_depth)
273 {
274 *id += self.shift_by.types.len();
275 }
276 }
277 fn enter_constant_expr(&mut self, x: &mut ConstantExpr) {
278 if let ConstantExprKind::Var(ref mut var) = x.kind
279 && let Some(id) = var.bound_at_depth_mut(self.binder_depth)
280 {
281 *id += self.shift_by.const_generics.len();
282 }
283 }
284 fn enter_trait_ref_kind(&mut self, x: &mut TraitRefKind) {
285 if let TraitRefKind::Clause(var) = x
286 && let Some(id) = var.bound_at_depth_mut(self.binder_depth)
287 {
288 *id += self.shift_by.trait_clauses.len();
289 }
290 }
291 }
292
293 let mut outer_params = self.params;
295
296 let mut bound_value = self.skip_binder.skip_binder;
300 let _ = bound_value.drive_mut(&mut FlattenVisitor {
301 shift_by: &outer_params,
302 binder_depth: Default::default(),
303 });
304
305 let mut inner_params = self.skip_binder.params;
308 let _ = inner_params.drive_mut(&mut FlattenVisitor {
309 shift_by: &outer_params,
310 binder_depth: Default::default(),
311 });
312 inner_params
313 .regions
314 .iter_mut()
315 .for_each(|v| v.index += outer_params.regions.len());
316 inner_params
317 .types
318 .iter_mut()
319 .for_each(|v| v.index += outer_params.types.len());
320 inner_params
321 .const_generics
322 .iter_mut()
323 .for_each(|v| v.index += outer_params.const_generics.len());
324 inner_params
325 .trait_clauses
326 .iter_mut()
327 .for_each(|v| v.clause_id += outer_params.trait_clauses.len());
328
329 let GenericParams {
330 regions,
331 types,
332 const_generics,
333 trait_clauses,
334 regions_outlive,
335 types_outlive,
336 trait_type_constraints,
337 } = &inner_params;
338 outer_params.regions.clone_extend_from_other(regions);
339 outer_params.types.clone_extend_from_other(types);
340 outer_params
341 .const_generics
342 .clone_extend_from_other(const_generics);
343 outer_params
344 .trait_clauses
345 .clone_extend_from_other(trait_clauses);
346 outer_params
347 .regions_outlive
348 .extend_from_slice(regions_outlive);
349 outer_params.types_outlive.extend_from_slice(types_outlive);
350 outer_params
351 .trait_type_constraints
352 .clone_extend_from_other(trait_type_constraints);
353
354 Binder {
355 params: outer_params,
356 skip_binder: bound_value,
357 kind: BinderKind::Other,
358 }
359 }
360}
361
362impl<T> RegionBinder<T> {
363 pub fn empty(x: T) -> Self
365 where
366 T: TyVisitable,
367 {
368 RegionBinder {
369 regions: Default::default(),
370 skip_binder: x.move_under_binder(),
371 }
372 }
373
374 pub fn map<U>(self, f: impl FnOnce(T) -> U) -> RegionBinder<U> {
375 RegionBinder {
376 regions: self.regions,
377 skip_binder: f(self.skip_binder),
378 }
379 }
380
381 pub fn map_ref<U>(&self, f: impl FnOnce(&T) -> U) -> RegionBinder<U> {
382 RegionBinder {
383 regions: self.regions.clone(),
384 skip_binder: f(&self.skip_binder),
385 }
386 }
387
388 pub fn apply(self, regions: IndexVec<RegionId, Region>) -> T
390 where
391 T: TyVisitable,
392 {
393 assert_eq!(regions.len(), self.regions.len());
394 let args = GenericArgs {
395 regions,
396 ..GenericArgs::empty()
397 };
398 self.skip_binder.substitute_inner_binder(&args)
399 }
400
401 pub fn erase(self) -> T
403 where
404 T: TyVisitable,
405 {
406 let regions = self.regions.map_ref_indexed(|_, _| Region::Erased);
407 self.apply(regions)
408 }
409}
410
411impl GenericArgs {
412 pub fn len(&self) -> usize {
413 let GenericArgs {
414 regions,
415 types,
416 const_generics,
417 trait_refs,
418 } = self;
419 regions.len() + types.len() + const_generics.len() + trait_refs.len()
420 }
421
422 pub fn is_empty(&self) -> bool {
423 self.len() == 0
424 }
425 pub fn has_explicits(&self) -> bool {
427 !self.regions.is_empty() || !self.types.is_empty() || !self.const_generics.is_empty()
428 }
429 pub fn has_implicits(&self) -> bool {
431 !self.trait_refs.is_empty()
432 }
433
434 pub fn empty() -> Self {
435 GenericArgs {
436 regions: Default::default(),
437 types: Default::default(),
438 const_generics: Default::default(),
439 trait_refs: Default::default(),
440 }
441 }
442
443 pub fn new(
444 regions: IndexVec<RegionId, Region>,
445 types: IndexVec<TypeVarId, Ty>,
446 const_generics: IndexVec<ConstGenericVarId, ConstantExpr>,
447 trait_refs: IndexVec<TraitClauseId, TraitRef>,
448 ) -> Self {
449 Self {
450 regions,
451 types,
452 const_generics,
453 trait_refs,
454 }
455 }
456 pub fn new_types(types: IndexVec<TypeVarId, Ty>) -> Self {
457 Self {
458 types,
459 ..Self::empty()
460 }
461 }
462
463 pub fn matches(&self, params: &GenericParams) -> bool {
466 params.regions.len() == self.regions.len()
467 && params.types.len() == self.types.len()
468 && params.const_generics.len() == self.const_generics.len()
469 && params.trait_clauses.len() == self.trait_refs.len()
470 }
471
472 pub fn pop_first_type_arg(&self) -> (Ty, Self) {
477 let mut generics = self.clone();
478 let mut it = mem::take(&mut generics.types).into_iter();
479 let ty = it.next().unwrap();
480 generics.types = it.collect();
481 (ty, generics)
482 }
483
484 pub fn concat(mut self, other: &Self) -> Self {
487 let Self {
488 regions,
489 types,
490 const_generics,
491 trait_refs,
492 } = other;
493 self.regions.clone_extend_from_other(regions);
494 self.types.clone_extend_from_other(types);
495 self.const_generics.clone_extend_from_other(const_generics);
496 self.trait_refs.clone_extend_from_other(trait_refs);
497 self
498 }
499}
500
501impl IntTy {
502 pub fn target_size(&self, ptr_size: ByteCount) -> usize {
505 match self {
506 IntTy::Isize => ptr_size as usize,
507 IntTy::I8 => size_of::<i8>(),
508 IntTy::I16 => size_of::<i16>(),
509 IntTy::I32 => size_of::<i32>(),
510 IntTy::I64 => size_of::<i64>(),
511 IntTy::I128 => size_of::<i128>(),
512 }
513 }
514}
515impl UIntTy {
516 pub fn target_size(&self, ptr_size: ByteCount) -> usize {
519 match self {
520 UIntTy::Usize => ptr_size as usize,
521 UIntTy::U8 => size_of::<u8>(),
522 UIntTy::U16 => size_of::<u16>(),
523 UIntTy::U32 => size_of::<u32>(),
524 UIntTy::U64 => size_of::<u64>(),
525 UIntTy::U128 => size_of::<u128>(),
526 }
527 }
528}
529impl FloatTy {
530 pub fn target_size(&self) -> usize {
533 match self {
534 FloatTy::F16 => size_of::<u16>(),
535 FloatTy::F32 => size_of::<u32>(),
536 FloatTy::F64 => size_of::<u64>(),
537 FloatTy::F128 => size_of::<u128>(),
538 }
539 }
540}
541
542impl IntegerTy {
543 pub fn to_unsigned(&self) -> Self {
544 match self {
545 IntegerTy::Signed(IntTy::Isize) => IntegerTy::Unsigned(UIntTy::Usize),
546 IntegerTy::Signed(IntTy::I8) => IntegerTy::Unsigned(UIntTy::U8),
547 IntegerTy::Signed(IntTy::I16) => IntegerTy::Unsigned(UIntTy::U16),
548 IntegerTy::Signed(IntTy::I32) => IntegerTy::Unsigned(UIntTy::U32),
549 IntegerTy::Signed(IntTy::I64) => IntegerTy::Unsigned(UIntTy::U64),
550 IntegerTy::Signed(IntTy::I128) => IntegerTy::Unsigned(UIntTy::U128),
551 _ => *self,
552 }
553 }
554
555 pub fn target_size(&self, ptr_size: ByteCount) -> usize {
558 match self {
559 IntegerTy::Signed(ty) => ty.target_size(ptr_size),
560 IntegerTy::Unsigned(ty) => ty.target_size(ptr_size),
561 }
562 }
563}
564
565impl LiteralTy {
566 pub fn to_integer_ty(&self) -> Option<IntegerTy> {
567 match self {
568 Self::Int(int_ty) => Some(IntegerTy::Signed(*int_ty)),
569 Self::UInt(uint_ty) => Some(IntegerTy::Unsigned(*uint_ty)),
570 _ => None,
571 }
572 }
573
574 pub fn target_size(&self, ptr_size: ByteCount) -> usize {
577 match self {
578 LiteralTy::Int(int_ty) => int_ty.target_size(ptr_size),
579 LiteralTy::UInt(uint_ty) => uint_ty.target_size(ptr_size),
580 LiteralTy::Float(float_ty) => float_ty.target_size(),
581 LiteralTy::Char => 4,
582 LiteralTy::Bool => 1,
583 }
584 }
585}
586
587impl From<LiteralTy> for Ty {
588 fn from(value: LiteralTy) -> Self {
589 TyKind::Literal(value).into_ty()
590 }
591}
592
593#[derive(Debug, Clone, Copy)]
599pub struct ItemBinder<ItemId, T> {
600 pub item_id: ItemId,
601 val: T,
602}
603
604impl<ItemId, T> ItemBinder<ItemId, T>
605where
606 ItemId: Debug + Copy + PartialEq,
607{
608 pub fn new(item_id: ItemId, val: T) -> Self {
609 Self { item_id, val }
610 }
611
612 pub fn as_ref(&self) -> ItemBinder<ItemId, &T> {
613 ItemBinder {
614 item_id: self.item_id,
615 val: &self.val,
616 }
617 }
618
619 pub fn map_bound<U>(self, f: impl FnOnce(T) -> U) -> ItemBinder<ItemId, U> {
620 ItemBinder {
621 item_id: self.item_id,
622 val: f(self.val),
623 }
624 }
625
626 fn assert_item_id(&self, item_id: ItemId) {
627 assert_eq!(
628 self.item_id, item_id,
629 "Trying to use item bound for {:?} as if it belonged to {:?}",
630 self.item_id, item_id
631 );
632 }
633
634 pub fn under_binder_of(self, item_id: ItemId) -> T {
637 self.assert_item_id(item_id);
638 self.val
639 }
640
641 pub fn substitute<OtherItem: Debug + Copy + PartialEq>(
645 self,
646 args: ItemBinder<OtherItem, &GenericArgs>,
647 ) -> ItemBinder<OtherItem, T>
648 where
649 ItemId: Into<ItemId>,
650 T: TyVisitable,
651 {
652 args.map_bound(|args| self.val.substitute(args))
653 }
654}
655
656#[derive(Debug, Clone, Copy, PartialEq, Eq)]
658pub struct CurrentItem;
659
660impl<T> ItemBinder<CurrentItem, T> {
661 pub fn under_current_binder(self) -> T {
662 self.val
663 }
664}
665
666impl Ty {
667 pub fn new(kind: TyKind) -> Self {
668 Ty(HashConsed::new(kind))
669 }
670
671 pub fn kind(&self) -> &TyKind {
672 self.0.inner()
673 }
674
675 pub fn with_kind_mut<R>(&mut self, f: impl FnOnce(&mut TyKind) -> R) -> R {
676 self.0.with_inner_mut(f)
677 }
678
679 pub fn mk_unit() -> Ty {
681 Self::mk_tuple(vec![])
682 }
683
684 pub fn mk_bool() -> Ty {
685 TyKind::Literal(LiteralTy::Bool).into()
686 }
687
688 pub fn mk_usize() -> Ty {
689 TyKind::Literal(LiteralTy::UInt(UIntTy::Usize)).into()
690 }
691
692 pub fn mk_tuple(tys: Vec<Ty>) -> Ty {
693 TyKind::Adt(TypeDeclRef {
694 id: TypeId::Tuple,
695 generics: Box::new(GenericArgs::new_types(tys.into())),
696 })
697 .into_ty()
698 }
699
700 pub fn mk_array(ty: Ty, len: ConstantExpr) -> Ty {
701 TyKind::Array(ty, Box::new(len)).into_ty()
702 }
703
704 pub fn mk_slice(ty: Ty) -> Ty {
705 TyKind::Slice(ty).into_ty()
706 }
707 pub fn is_unit(&self) -> bool {
709 match self.as_tuple() {
710 Some(tys) => tys.is_empty(),
711 None => false,
712 }
713 }
714
715 pub fn is_scalar(&self) -> bool {
717 match self.kind() {
718 TyKind::Literal(kind) => kind.is_int() || kind.is_uint(),
719 _ => false,
720 }
721 }
722
723 pub fn is_unsigned_scalar(&self) -> bool {
724 matches!(self.kind(), TyKind::Literal(LiteralTy::UInt(_)))
725 }
726
727 pub fn is_signed_scalar(&self) -> bool {
728 matches!(self.kind(), TyKind::Literal(LiteralTy::Int(_)))
729 }
730
731 pub fn is_str(&self) -> bool {
732 match self.kind() {
733 TyKind::Adt(ty_ref) if let TypeId::Builtin(BuiltinTy::Str) = ty_ref.id => true,
734 _ => false,
735 }
736 }
737
738 pub fn is_box(&self) -> bool {
740 match self.kind() {
741 TyKind::Adt(ty_ref) if let TypeId::Builtin(BuiltinTy::Box) = ty_ref.id => true,
742 _ => false,
743 }
744 }
745
746 pub fn as_box(&self) -> Option<&Ty> {
747 match self.kind() {
748 TyKind::Adt(ty_ref) if let TypeId::Builtin(BuiltinTy::Box) = ty_ref.id => {
749 Some(&ty_ref.generics.types[0])
750 }
751 _ => None,
752 }
753 }
754
755 pub fn get_ptr_metadata(&self, translated: &TranslatedCrate) -> PtrMetadata {
756 let ty_decls = &translated.type_decls;
757 match self.kind() {
758 TyKind::Adt(ty_ref) => {
759 match ty_ref.id {
763 TypeId::Adt(type_decl_id) => {
764 let Some(decl) = ty_decls.get(type_decl_id) else {
765 return PtrMetadata::InheritFrom(self.clone());
766 };
767 match decl.ptr_metadata.clone().substitute(&ty_ref.generics) {
768 PtrMetadata::InheritFrom(ty) => ty.get_ptr_metadata(translated),
770 meta => meta,
772 }
773 }
774 TypeId::Tuple => {
776 match ty_ref.generics.types.iter().last() {
777 None => PtrMetadata::None,
779 Some(ty) => ty.get_ptr_metadata(translated),
781 }
782 }
783 TypeId::Builtin(BuiltinTy::Box) => PtrMetadata::None,
785 TypeId::Builtin(BuiltinTy::Str) => PtrMetadata::Length,
787 }
788 }
789 TyKind::DynTrait(pred) => match pred.vtable_ref(translated) {
790 Some(vtable) => PtrMetadata::VTable(vtable),
791 None => PtrMetadata::InheritFrom(self.clone()),
792 },
793 TyKind::Slice(..) => PtrMetadata::Length,
795 TyKind::TraitType(..) | TyKind::TypeVar(_) => PtrMetadata::InheritFrom(self.clone()),
796 TyKind::Literal(_)
797 | TyKind::Never
798 | TyKind::Ref(..)
799 | TyKind::RawPtr(..)
800 | TyKind::FnPtr(..)
801 | TyKind::FnDef(..)
802 | TyKind::Array(..)
803 | TyKind::Error(_) => PtrMetadata::None,
804 TyKind::PtrMetadata(_) => PtrMetadata::None,
806 }
807 }
808
809 pub fn as_ref_or_ptr(&self) -> Option<&Ty> {
810 match self.kind() {
811 TyKind::RawPtr(ty, _) | TyKind::Ref(_, ty, _) => Some(ty),
812 _ => None,
813 }
814 }
815
816 pub fn as_array_or_slice(&self) -> Option<&Ty> {
817 match self.kind() {
818 TyKind::Slice(ty) | TyKind::Array(ty, _) => Some(ty),
819 _ => None,
820 }
821 }
822
823 pub fn as_tuple(&self) -> Option<&IndexVec<TypeVarId, Ty>> {
824 match self.kind() {
825 TyKind::Adt(ty_ref) if let TypeId::Tuple = ty_ref.id => Some(&ty_ref.generics.types),
826 _ => None,
827 }
828 }
829
830 pub fn as_adt(&self) -> Option<&TypeDeclRef> {
831 self.kind().as_adt()
832 }
833}
834
835impl TyKind {
836 pub fn into_ty(self) -> Ty {
837 Ty::new(self)
838 }
839}
840
841impl From<TyKind> for Ty {
842 fn from(kind: TyKind) -> Ty {
843 kind.into_ty()
844 }
845}
846
847impl std::ops::Deref for Ty {
849 type Target = TyKind;
850
851 fn deref(&self) -> &Self::Target {
852 self.kind()
853 }
854}
855unsafe impl std::ops::DerefPure for Ty {}
857
858impl TypeDeclRef {
859 pub fn new(id: TypeId, generics: GenericArgs) -> Self {
860 Self {
861 id,
862 generics: Box::new(generics),
863 }
864 }
865}
866
867impl TraitDeclRef {
868 pub fn self_ty<'a>(&'a self, krate: &'a TranslatedCrate) -> Option<&'a Ty> {
869 match self.generics.types.iter().next() {
870 Some(ty) => Some(ty),
871 None => {
873 let name = krate.item_name(self.id)?;
874 let args = name.name.last()?.as_monomorphized()?;
875 args.types.iter().next()
876 }
877 }
878 }
879}
880
881impl TraitRef {
882 pub fn new(kind: TraitRefKind, trait_decl_ref: PolyTraitDeclRef) -> Self {
883 TraitRefContents {
884 kind,
885 trait_decl_ref,
886 }
887 .intern()
888 }
889
890 pub fn new_builtin(
891 trait_id: TraitDeclId,
892 ty: Ty,
893 parents: IndexVec<TraitClauseId, TraitRef>,
894 builtin_data: BuiltinImplData,
895 ) -> Self {
896 let trait_decl_ref = RegionBinder::empty(TraitDeclRef {
897 id: trait_id,
898 generics: Box::new(GenericArgs::new_types([ty].into())),
899 });
900 Self::new(
901 TraitRefKind::BuiltinOrAuto {
902 builtin_data,
903 parent_trait_refs: parents,
904 types: Default::default(),
905 },
906 trait_decl_ref,
907 )
908 }
909
910 pub fn trait_id(&self) -> TraitDeclId {
911 self.trait_decl_ref.skip_binder.id
912 }
913
914 pub fn with_contents_mut<R>(&mut self, f: impl FnOnce(&mut TraitRefContents) -> R) -> R {
917 self.0.with_inner_mut(f)
918 }
919}
920impl TraitRefContents {
921 pub fn intern(self) -> TraitRef {
922 TraitRef(HashConsed::new(self))
923 }
924}
925
926impl std::ops::Deref for TraitRef {
927 type Target = TraitRefContents;
928 fn deref(&self) -> &Self::Target {
929 &self.0
930 }
931}
932
933impl BuiltinImplData {
934 pub fn as_closure_kind(&self) -> Option<ClosureKind> {
935 match self {
936 BuiltinImplData::FnOnce => Some(ClosureKind::FnOnce),
937 BuiltinImplData::FnMut => Some(ClosureKind::FnMut),
938 BuiltinImplData::Fn => Some(ClosureKind::Fn),
939 _ => None,
940 }
941 }
942}
943
944impl PtrMetadata {
945 pub fn into_type(self) -> Ty {
946 match self {
947 PtrMetadata::None => Ty::mk_unit(),
948 PtrMetadata::Length => Ty::mk_usize(),
949 PtrMetadata::VTable(type_decl_ref) => Ty::new(TyKind::Ref(
950 Region::Static,
951 Ty::new(TyKind::Adt(type_decl_ref)),
952 RefKind::Shared,
953 )),
954 PtrMetadata::InheritFrom(ty) => Ty::new(TyKind::PtrMetadata(ty)),
955 }
956 }
957}
958
959impl Field {
960 pub fn renamed_name(&self) -> Option<&str> {
962 self.attr_info.rename.as_deref().or(self.name.as_deref())
963 }
964
965 pub fn is_opaque(&self) -> bool {
967 self.attr_info
968 .attributes
969 .iter()
970 .any(|attr| attr.is_opaque())
971 }
972}
973
974impl Variant {
975 pub fn renamed_name(&self) -> &str {
978 self.attr_info
979 .rename
980 .as_deref()
981 .unwrap_or(self.name.as_ref())
982 }
983
984 pub fn is_opaque(&self) -> bool {
986 self.attr_info
987 .attributes
988 .iter()
989 .any(|attr| attr.is_opaque())
990 }
991}
992
993impl DynPredicate {
994 pub fn vtable_ref(&self, translated: &TranslatedCrate) -> Option<TypeDeclRef> {
996 let dyn_ty = TyKind::DynTrait(self.clone()).into_ty();
997 let relevant_tref = self.binder.params.trait_clauses[0]
1000 .trait_
1001 .clone()
1002 .erase()
1003 .substitute(&GenericArgs::new_types([dyn_ty].into_iter().collect()));
1004
1005 let trait_decl = translated.trait_decls.get(relevant_tref.id)?;
1007 let vtable_ref = trait_decl
1008 .vtable
1009 .clone()?
1010 .substitute_with_self(&relevant_tref.generics, &TraitRefKind::Dyn);
1011 Some(vtable_ref)
1012 }
1013}
1014
1015impl RefKind {
1016 pub fn mutable(x: bool) -> Self {
1017 if x { Self::Mut } else { Self::Shared }
1018 }
1019}
1020
1021pub trait VarsVisitor {
1026 fn visit_erased_region(&mut self) -> Option<Region> {
1027 None
1028 }
1029 fn visit_region_var(&mut self, _v: RegionDbVar) -> Option<Region> {
1030 None
1031 }
1032 fn visit_type_var(&mut self, _v: TypeDbVar) -> Option<Ty> {
1033 None
1034 }
1035 fn visit_const_generic_var(&mut self, _v: ConstGenericDbVar) -> Option<ConstantExprKind> {
1036 None
1037 }
1038 fn visit_clause_var(&mut self, _v: ClauseDbVar) -> Option<TraitRefKind> {
1039 None
1040 }
1041 fn visit_self_clause(&mut self) -> Option<TraitRefKind> {
1042 None
1043 }
1044}
1045
1046#[derive(Visitor)]
1049pub(crate) struct SubstVisitor<'a> {
1050 generics: &'a GenericArgs,
1051 self_ref: Option<&'a TraitRefKind>,
1052 explicits_only: bool,
1054 had_error: bool,
1055}
1056impl<'a> SubstVisitor<'a> {
1057 pub(crate) fn new(
1058 generics: &'a GenericArgs,
1059 self_ref: Option<&'a TraitRefKind>,
1060 explicits_only: bool,
1061 ) -> Self {
1062 Self {
1063 generics,
1064 self_ref,
1065 explicits_only,
1066 had_error: false,
1067 }
1068 }
1069
1070 pub fn visit<T: TyVisitable>(mut self, mut x: T) -> Result<T, GenericsMismatch> {
1071 x.visit_vars(&mut self);
1072 if self.had_error {
1073 Err(GenericsMismatch)
1074 } else {
1075 Ok(x)
1076 }
1077 }
1078
1079 fn process_var<Id, T>(
1081 &mut self,
1082 var: DeBruijnVar<Id>,
1083 get: impl Fn(Id) -> Option<&'a T>,
1084 ) -> Option<T>
1085 where
1086 Id: Copy,
1087 T: Clone + TyVisitable,
1088 DeBruijnVar<Id>: Into<T>,
1089 {
1090 match var {
1091 DeBruijnVar::Bound(dbid, varid) => {
1092 Some(if let Some(dbid) = dbid.sub(DeBruijnId::one()) {
1093 DeBruijnVar::Bound(dbid, varid).into()
1095 } else {
1096 match get(varid) {
1097 Some(v) => v.clone(),
1098 None => {
1099 self.had_error = true;
1100 return None;
1101 }
1102 }
1103 })
1104 }
1105 DeBruijnVar::Free(..) => None,
1106 }
1107 }
1108}
1109impl VarsVisitor for SubstVisitor<'_> {
1110 fn visit_region_var(&mut self, v: RegionDbVar) -> Option<Region> {
1111 self.process_var(v, |id| self.generics.regions.get(id))
1112 }
1113 fn visit_type_var(&mut self, v: TypeDbVar) -> Option<Ty> {
1114 self.process_var(v, |id| self.generics.types.get(id))
1115 }
1116 fn visit_const_generic_var(&mut self, v: ConstGenericDbVar) -> Option<ConstantExprKind> {
1117 self.process_var(v, |id| {
1118 self.generics.const_generics.get(id).map(|c| &c.kind)
1119 })
1120 }
1121 fn visit_clause_var(&mut self, v: ClauseDbVar) -> Option<TraitRefKind> {
1122 if self.explicits_only {
1123 None
1124 } else {
1125 self.process_var(v, |id| Some(&self.generics.trait_refs.get(id)?.kind))
1126 }
1127 }
1128 fn visit_self_clause(&mut self) -> Option<TraitRefKind> {
1129 Some(self.self_ref.cloned().expect(
1130 "used `substitute` on an item coming from a trait; \
1131 use `substitute_with_self` or `substitute_inner_binder` instead.",
1132 ))
1133 }
1134}
1135
1136#[derive(Debug)]
1137pub struct GenericsMismatch;
1138
1139pub trait TyVisitable: Sized + AstVisitable {
1141 fn visit_vars(&mut self, v: &mut impl VarsVisitor) {
1145 #[derive(Visitor)]
1146 struct Wrap<'v, V> {
1147 v: &'v mut V,
1148 depth: DeBruijnId,
1149 }
1150 impl<V> VisitorWithBinderDepth for Wrap<'_, V> {
1151 fn binder_depth_mut(&mut self) -> &mut DeBruijnId {
1152 &mut self.depth
1153 }
1154 }
1155 impl<V: VarsVisitor> VisitAstMut for Wrap<'_, V> {
1156 fn visit<T: AstVisitable>(&mut self, x: &mut T) -> ControlFlow<Self::Break> {
1157 VisitWithBinderDepth::new(self).visit(x)
1158 }
1159
1160 fn exit_region(&mut self, r: &mut Region) {
1161 match r {
1162 Region::Var(var)
1163 if let Some(var) = var.move_out_from_depth(self.depth)
1164 && let Some(new_r) = self.v.visit_region_var(var) =>
1165 {
1166 *r = new_r.move_under_binders(self.depth);
1167 }
1168 Region::Erased | Region::Body(..)
1169 if let Some(new_r) = self.v.visit_erased_region() =>
1170 {
1171 *r = new_r.move_under_binders(self.depth);
1172 }
1173 _ => (),
1174 }
1175 }
1176 fn exit_ty(&mut self, ty: &mut Ty) {
1177 if let TyKind::TypeVar(var) = ty.kind()
1178 && let Some(var) = var.move_out_from_depth(self.depth)
1179 && let Some(new_ty) = self.v.visit_type_var(var)
1180 {
1181 *ty = new_ty.move_under_binders(self.depth);
1182 }
1183 }
1184 fn exit_constant_expr(&mut self, ce: &mut ConstantExpr) {
1185 if let ConstantExprKind::Var(var) = &mut ce.kind
1186 && let Some(var) = var.move_out_from_depth(self.depth)
1187 && let Some(new_cg) = self.v.visit_const_generic_var(var)
1188 {
1189 ce.kind = new_cg.move_under_binders(self.depth);
1190 }
1191 }
1192 fn exit_trait_ref_kind(&mut self, kind: &mut TraitRefKind) {
1193 match kind {
1194 TraitRefKind::SelfId => {
1195 if let Some(new_kind) = self.v.visit_self_clause() {
1196 *kind = new_kind.move_under_binders(self.depth);
1197 }
1198 }
1199 TraitRefKind::Clause(var) => {
1200 if let Some(var) = var.move_out_from_depth(self.depth)
1201 && let Some(new_kind) = self.v.visit_clause_var(var)
1202 {
1203 *kind = new_kind.move_under_binders(self.depth);
1204 }
1205 }
1206 _ => {}
1207 }
1208 }
1209 }
1210 Wrap {
1211 v,
1212 depth: DeBruijnId::zero(),
1213 }
1214 .visit(self);
1215 }
1216
1217 fn substitute(self, generics: &GenericArgs) -> Self {
1221 SubstVisitor::new(generics, None, false)
1222 .visit(self)
1223 .unwrap()
1224 }
1225 fn substitute_inner_binder(self, generics: &GenericArgs) -> Self {
1228 self.substitute_with_self(generics, &TraitRefKind::SelfId)
1229 }
1230 fn substitute_explicits(self, generics: &GenericArgs) -> Self {
1232 SubstVisitor::new(generics, None, true).visit(self).unwrap()
1233 }
1234 fn substitute_with_self(self, generics: &GenericArgs, self_ref: &TraitRefKind) -> Self {
1236 self.try_substitute_with_self(generics, self_ref).unwrap()
1237 }
1238 fn substitute_with_tref(self, tref: &TraitRef) -> Self {
1240 let pred = tref.trait_decl_ref.clone().erase();
1241 self.substitute_with_self(&pred.generics, &tref.kind)
1242 }
1243
1244 fn try_substitute(self, generics: &GenericArgs) -> Result<Self, GenericsMismatch> {
1245 SubstVisitor::new(generics, None, false).visit(self)
1246 }
1247 fn try_substitute_with_self(
1248 self,
1249 generics: &GenericArgs,
1250 self_ref: &TraitRefKind,
1251 ) -> Result<Self, GenericsMismatch> {
1252 SubstVisitor::new(generics, Some(self_ref), false).visit(self)
1253 }
1254
1255 fn move_under_binder(self) -> Self {
1257 self.move_under_binders(DeBruijnId::one())
1258 }
1259
1260 fn move_under_binders(mut self, depth: DeBruijnId) -> Self {
1262 if !depth.is_zero() {
1263 let Continue(()) = self.visit_db_id::<Infallible>(|id| {
1264 *id = id.plus(depth);
1265 Continue(())
1266 });
1267 }
1268 self
1269 }
1270
1271 fn move_from_under_binder(self) -> Option<Self> {
1273 self.move_from_under_binders(DeBruijnId::one())
1274 }
1275
1276 fn move_from_under_binders(mut self, depth: DeBruijnId) -> Option<Self> {
1279 self.visit_db_id::<()>(|id| match id.sub(depth) {
1280 Some(sub) => {
1281 *id = sub;
1282 Continue(())
1283 }
1284 None => Break(()),
1285 })
1286 .is_continue()
1287 .then_some(self)
1288 }
1289
1290 fn visit_db_id<B>(
1294 &mut self,
1295 f: impl FnMut(&mut DeBruijnId) -> ControlFlow<B>,
1296 ) -> ControlFlow<B> {
1297 struct Wrap<F> {
1298 f: F,
1299 depth: DeBruijnId,
1300 }
1301 impl<B, F> Visitor for Wrap<F>
1302 where
1303 F: FnMut(&mut DeBruijnId) -> ControlFlow<B>,
1304 {
1305 type Break = B;
1306 }
1307 impl<B, F> VisitAstMut for Wrap<F>
1308 where
1309 F: FnMut(&mut DeBruijnId) -> ControlFlow<B>,
1310 {
1311 fn enter_region_binder<T: AstVisitable>(&mut self, _: &mut RegionBinder<T>) {
1312 self.depth = self.depth.incr()
1313 }
1314 fn exit_region_binder<T: AstVisitable>(&mut self, _: &mut RegionBinder<T>) {
1315 self.depth = self.depth.decr()
1316 }
1317 fn enter_binder<T: AstVisitable>(&mut self, _: &mut Binder<T>) {
1318 self.depth = self.depth.incr()
1319 }
1320 fn exit_binder<T: AstVisitable>(&mut self, _: &mut Binder<T>) {
1321 self.depth = self.depth.decr()
1322 }
1323
1324 fn visit_de_bruijn_id(&mut self, x: &mut DeBruijnId) -> ControlFlow<Self::Break> {
1325 if let Some(mut shifted) = x.sub(self.depth) {
1326 (self.f)(&mut shifted)?;
1327 *x = shifted.plus(self.depth)
1328 }
1329 Continue(())
1330 }
1331 }
1332 self.drive_mut(&mut Wrap {
1333 f,
1334 depth: DeBruijnId::zero(),
1335 })
1336 }
1337
1338 fn replace_erased_regions(mut self, f: impl FnMut() -> Region) -> Self {
1341 #[derive(Visitor)]
1342 struct RefreshErasedRegions<F>(F);
1343 impl<F: FnMut() -> Region> VarsVisitor for RefreshErasedRegions<F> {
1344 fn visit_erased_region(&mut self) -> Option<Region> {
1345 Some((self.0)())
1346 }
1347 }
1348 self.visit_vars(&mut RefreshErasedRegions(f));
1349 self
1350 }
1351}
1352
1353#[derive(Debug, Clone)]
1356pub struct Substituted<'a, T> {
1357 pub val: &'a T,
1358 pub generics: Cow<'a, GenericArgs>,
1359 pub trait_self: Option<&'a TraitRefKind>,
1360}
1361
1362impl<'a, T> Substituted<'a, T> {
1363 pub fn new(val: &'a T, generics: &'a GenericArgs) -> Self {
1364 Self {
1365 val,
1366 generics: Cow::Borrowed(generics),
1367 trait_self: None,
1368 }
1369 }
1370 pub fn new_for_trait(
1371 val: &'a T,
1372 generics: &'a GenericArgs,
1373 trait_self: &'a TraitRefKind,
1374 ) -> Self {
1375 Self {
1376 val,
1377 generics: Cow::Borrowed(generics),
1378 trait_self: Some(trait_self),
1379 }
1380 }
1381 pub fn new_for_trait_ref(val: &'a T, tref: &'a TraitRef) -> Self {
1382 Self {
1383 val,
1384 generics: Cow::Owned(*tref.trait_decl_ref.clone().erase().generics),
1385 trait_self: Some(&tref.kind),
1386 }
1387 }
1388
1389 pub fn rebind<U>(&self, val: &'a U) -> Substituted<'a, U> {
1390 Substituted {
1391 val,
1392 generics: self.generics.clone(),
1393 trait_self: self.trait_self,
1394 }
1395 }
1396
1397 pub fn substitute(&self) -> T
1398 where
1399 T: TyVisitable + Clone,
1400 {
1401 self.try_substitute().unwrap()
1402 }
1403 pub fn try_substitute(&self) -> Result<T, GenericsMismatch>
1404 where
1405 T: TyVisitable + Clone,
1406 {
1407 match self.trait_self {
1408 None => self.val.clone().try_substitute(&self.generics),
1409 Some(trait_self) => self
1410 .val
1411 .clone()
1412 .try_substitute_with_self(&self.generics, trait_self),
1413 }
1414 }
1415
1416 pub fn iter<Item: 'a>(&self) -> impl Iterator<Item = Substituted<'a, Item>>
1417 where
1418 &'a T: IntoIterator<Item = &'a Item>,
1419 {
1420 self.val.into_iter().map(move |x| self.rebind(x))
1421 }
1422}
1423
1424impl TypeDecl {
1425 pub fn get_variant_from_tag(
1431 &self,
1432 target: &TargetTriple,
1433 tag: ScalarValue,
1434 ) -> Option<VariantId> {
1435 let layout = self.layout.get(target)?;
1436 if layout.uninhabited {
1437 return None;
1438 };
1439 let discr_layout = layout.discriminant_layout.as_ref()?;
1440
1441 let variant_for_tag =
1442 layout
1443 .variant_layouts
1444 .iter_enumerated()
1445 .find_map(|(id, variant_layout)| {
1446 if variant_layout.tag == Some(tag) {
1447 Some(id)
1448 } else {
1449 None
1450 }
1451 });
1452
1453 match &discr_layout.encoding {
1454 TagEncoding::Direct => {
1455 assert_eq!(tag.get_integer_ty(), discr_layout.tag_ty);
1456 variant_for_tag
1457 }
1458 TagEncoding::Niche { untagged_variant } => variant_for_tag.or(Some(*untagged_variant)),
1459 }
1460 }
1461
1462 pub fn is_c_repr(&self) -> bool {
1463 self.repr
1464 .as_ref()
1465 .is_some_and(|repr| repr.repr_algo == ReprAlgorithm::C)
1466 }
1467
1468 pub fn get_field_by_name(
1469 &self,
1470 variant: Option<VariantId>,
1471 field_name: &str,
1472 ) -> Option<(FieldId, &Field)> {
1473 let fields = match &self.kind {
1474 TypeDeclKind::Struct(fields) | TypeDeclKind::Union(fields) => fields,
1475 TypeDeclKind::Enum(variants) => &variants[variant.unwrap()].fields,
1476 _ => return None,
1477 };
1478 fields
1479 .iter_enumerated()
1480 .find(|(_, field)| field.name.as_deref() == Some(field_name))
1481 }
1482}
1483
1484impl Layout {
1485 pub fn is_variant_uninhabited(&self, variant_id: VariantId) -> bool {
1486 if let Some(v) = self.variant_layouts.get(variant_id) {
1487 v.uninhabited
1488 } else {
1489 false
1490 }
1491 }
1492}
1493
1494impl ReprOptions {
1495 pub fn guarantees_fixed_field_order(&self) -> bool {
1505 self.repr_algo == ReprAlgorithm::C || self.explicit_discr_type
1506 }
1507}
1508
1509impl<T: AstVisitable> TyVisitable for T {}
1510
1511impl Eq for TraitParam {}
1512
1513pub trait HasIdxVecOf<Id: Idx>: std::ops::Index<Id, Output: Sized> {
1514 fn get_idx_vec(&self) -> &IndexVec<Id, Self::Output>;
1515 fn get_idx_vec_mut(&mut self) -> &mut IndexVec<Id, Self::Output>;
1516}
1517
1518macro_rules! mk_index_impls {
1520 ($ty:ident.$field:ident[$idx:ty]: $output:ty) => {
1521 impl std::ops::Index<$idx> for $ty {
1522 type Output = $output;
1523 fn index(&self, index: $idx) -> &Self::Output {
1524 &self.$field[index]
1525 }
1526 }
1527 impl std::ops::IndexMut<$idx> for $ty {
1528 fn index_mut(&mut self, index: $idx) -> &mut Self::Output {
1529 &mut self.$field[index]
1530 }
1531 }
1532 impl HasIdxVecOf<$idx> for $ty {
1533 fn get_idx_vec(&self) -> &IndexVec<$idx, Self::Output> {
1534 &self.$field
1535 }
1536 fn get_idx_vec_mut(&mut self) -> &mut IndexVec<$idx, Self::Output> {
1537 &mut self.$field
1538 }
1539 }
1540 };
1541}
1542mk_index_impls!(GenericArgs.regions[RegionId]: Region);
1543mk_index_impls!(GenericArgs.types[TypeVarId]: Ty);
1544mk_index_impls!(GenericArgs.const_generics[ConstGenericVarId]: ConstantExpr);
1545mk_index_impls!(GenericArgs.trait_refs[TraitClauseId]: TraitRef);
1546mk_index_impls!(GenericParams.regions[RegionId]: RegionParam);
1547mk_index_impls!(GenericParams.types[TypeVarId]: TypeParam);
1548mk_index_impls!(GenericParams.const_generics[ConstGenericVarId]: ConstGenericParam);
1549mk_index_impls!(GenericParams.trait_clauses[TraitClauseId]: TraitParam);