use ridx_format::{IndexWriter, RawAssocTy, RawImpl, TyId, IMPL_FLAG_CONST, IMPL_FLAG_NEGATIVE, IMPL_FLAG_RESERVATION, IMPL_FLAG_TRAIT, IMPL_FLAG_UNSAFE};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_middle::ty;
use rustc_span::def_id::DefId;
use super::{build_raw_generic_args, TcxTypeLowerer};
impl<'tcx, 'a> TcxTypeLowerer<'tcx, 'a> {
pub(crate) fn record_impls(&mut self, impl_dids: &[DefId], writer: &mut IndexWriter) {
for &impl_did in impl_dids {
self.record_impl(impl_did, writer);
}
}
pub(crate) fn record_impl(&mut self, impl_did: DefId, writer: &mut IndexWriter) {
let DefKind::Impl { of_trait } = self.tcx.def_kind(impl_did) else {
return;
};
let def_index = self.local_def_indices.get(&impl_did).copied();
let self_ty = self.lower_ty(self.tcx.type_of(impl_did).skip_binder(), writer);
let (trait_def, owned_args, arg_exprs, flags) = if of_trait {
let hdr = self.tcx.impl_trait_header(impl_did);
let tr = hdr.trait_ref.skip_binder();
let t_def = self.lower_def(tr.def_id, writer);
let args_after_self = if tr.args.is_empty() { &[][..] } else { &tr.args[1..] };
let (oa, ex) = self.lower_generic_args(args_after_self, writer);
let mut fl = IMPL_FLAG_TRAIT;
match hdr.polarity {
ty::ImplPolarity::Positive => {}
ty::ImplPolarity::Negative => fl |= IMPL_FLAG_NEGATIVE,
ty::ImplPolarity::Reservation => fl |= IMPL_FLAG_RESERVATION,
}
if hdr.safety.is_unsafe() {
fl |= IMPL_FLAG_UNSAFE;
}
if matches!(hdr.constness, hir::Constness::Const { .. }) {
fl |= IMPL_FLAG_CONST;
}
(Some(t_def), oa, ex, fl)
} else {
(None, Vec::new(), Vec::new(), 0u8)
};
let raw_trait_args = build_raw_generic_args(&owned_args, &arg_exprs);
let item_dids = self.tcx.associated_item_def_ids(impl_did);
let mut owned_assocs: Vec<(Option<u32>, String, TyId)> = Vec::new();
let mut assoc_defs: Vec<u32> = Vec::new();
for &assoc_did in item_dids {
match self.tcx.def_kind(assoc_did) {
DefKind::AssocTy => {
let Some(name_sym) = self.tcx.opt_item_name(assoc_did) else {
continue;
};
if !self.tcx.defaultness(assoc_did).has_value() {
continue;
}
let rhs = self.lower_ty(self.tcx.type_of(assoc_did).skip_binder(), writer);
let a_def_idx = self.local_def_indices.get(&assoc_did).copied();
owned_assocs.push((a_def_idx, name_sym.to_string(), rhs));
}
DefKind::AssocFn | DefKind::AssocConst { .. } => {
if let Some(&d_idx) = self.local_def_indices.get(&assoc_did) {
assoc_defs.push(d_idx);
}
}
_ => {}
}
}
let raw_assocs: Vec<RawAssocTy<'_>> = owned_assocs.iter().map(|(def_index, name, ty)| RawAssocTy { def_index: *def_index, name: name.as_str(), ty: *ty }).collect();
let pending_idx = writer.add_impl(RawImpl { def_index, self_ty, trait_def, trait_args: &raw_trait_args, assoc_tys: &raw_assocs, assoc_defs: &assoc_defs, flags });
self.record_impl_generics_and_preds(impl_did, pending_idx, writer);
}
}