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