|
| 1 | +//! Type queries (generics, fn sigs, discriminants, attrs). |
| 2 | +//! |
| 3 | +//! Wraps `tcx.generics_of()`, `tcx.predicates_of()`, `tcx.fn_sig()`, |
| 4 | +//! `tcx.optimized_mir()`, `tcx.def_kind()`, `tcx.type_of()`, |
| 5 | +//! `tcx.has_attr()`, `adt.discriminants(tcx)`, and `tcx.fn_abi_of_fn_ptr()`. |
| 6 | +
|
| 7 | +use super::middle; |
| 8 | +use super::middle::ty::{EarlyBinder, FnSig, GenericArgs, List, Ty, TypeFoldable, TypingEnv}; |
| 9 | +use super::rustc_internal::{self, internal}; |
| 10 | +use super::rustc_span; |
| 11 | +use super::stable_mir; |
| 12 | +use super::TyCtxt; |
| 13 | +use rustc_span::def_id::DefId; |
| 14 | + |
| 15 | +/// Collect generics/predicates chain for a DefId, walking parent scopes. |
| 16 | +pub fn generic_data(tcx: TyCtxt<'_>, id: DefId) -> Vec<(String, String)> { |
| 17 | + let mut v = Vec::new(); |
| 18 | + let mut next_id = Some(id); |
| 19 | + while let Some(curr_id) = next_id { |
| 20 | + let params = tcx.generics_of(curr_id); |
| 21 | + let preds = tcx.predicates_of(curr_id); |
| 22 | + if params.parent != preds.parent { |
| 23 | + panic!("Generics and predicates parent ids are distinct"); |
| 24 | + } |
| 25 | + v.push((format!("{:#?}", params), format!("{:#?}", preds))); |
| 26 | + next_id = params.parent; |
| 27 | + } |
| 28 | + v.reverse(); |
| 29 | + v |
| 30 | +} |
| 31 | + |
| 32 | +/// Unwrap an `EarlyBinder` in a default manner; panic on error. |
| 33 | +pub fn default_unwrap_early_binder<'tcx, T>( |
| 34 | + tcx: TyCtxt<'tcx>, |
| 35 | + id: DefId, |
| 36 | + v: EarlyBinder<'tcx, T>, |
| 37 | +) -> T |
| 38 | +where |
| 39 | + T: TypeFoldable<TyCtxt<'tcx>>, |
| 40 | +{ |
| 41 | + let v_copy = v.clone(); |
| 42 | + let body = tcx.optimized_mir(id); |
| 43 | + match tcx.try_instantiate_and_normalize_erasing_regions( |
| 44 | + GenericArgs::identity_for_item(tcx, id), |
| 45 | + body.typing_env(tcx), |
| 46 | + v, |
| 47 | + ) { |
| 48 | + Ok(res) => res, |
| 49 | + Err(err) => { |
| 50 | + println!("{:?}", err); |
| 51 | + v_copy.skip_binder() |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// Pretty-print a type, resolving FnDef signatures via `tcx.fn_sig()`. |
| 57 | +pub fn print_type<'tcx>(tcx: TyCtxt<'tcx>, id: DefId, ty: EarlyBinder<'tcx, Ty<'tcx>>) -> String { |
| 58 | + let kind: &middle::ty::TyKind = ty.skip_binder().kind(); |
| 59 | + if let middle::ty::TyKind::FnDef(fun_id, args) = kind { |
| 60 | + let sig0 = tcx.fn_sig(fun_id); |
| 61 | + let body = tcx.optimized_mir(id); |
| 62 | + let sig1 = match tcx.try_instantiate_and_normalize_erasing_regions( |
| 63 | + args, |
| 64 | + body.typing_env(tcx), |
| 65 | + sig0, |
| 66 | + ) { |
| 67 | + Ok(res) => res, |
| 68 | + Err(err) => { |
| 69 | + println!("{:?}", err); |
| 70 | + sig0.skip_binder() |
| 71 | + } |
| 72 | + }; |
| 73 | + let sig2: FnSig<'_> = tcx.instantiate_bound_regions_with_erased(sig1); |
| 74 | + format!("\nTyKind(FnDef): {:#?}", sig2) |
| 75 | + } else { |
| 76 | + let kind = default_unwrap_early_binder(tcx, id, ty); |
| 77 | + format!("\nTyKind: {:#?}", kind) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/// Query the def_kind, def_path, and type_of for a DefId (debug info). |
| 82 | +pub fn get_def_info(tcx: TyCtxt<'_>, id: DefId) -> (String, String, String) { |
| 83 | + ( |
| 84 | + format!("{:#?}", tcx.def_kind(id)), |
| 85 | + tcx.def_path_str(id), |
| 86 | + print_type(tcx, id, tcx.type_of(id)), |
| 87 | + ) |
| 88 | +} |
| 89 | + |
| 90 | +/// Check whether a CrateItem has a given attribute. |
| 91 | +pub fn has_attr( |
| 92 | + tcx: TyCtxt<'_>, |
| 93 | + item: &stable_mir::CrateItem, |
| 94 | + attr: rustc_span::symbol::Symbol, |
| 95 | +) -> bool { |
| 96 | + tcx.has_attr(rustc_internal::internal(tcx, item), attr) |
| 97 | +} |
| 98 | + |
| 99 | +/// Collect discriminant values for an ADT (enum) by going through internals. |
| 100 | +pub fn adt_discriminants(tcx: TyCtxt<'_>, adt_def: stable_mir::ty::AdtDef) -> Vec<u128> { |
| 101 | + let adt_internal = rustc_internal::internal(tcx, adt_def); |
| 102 | + adt_internal |
| 103 | + .discriminants(tcx) |
| 104 | + .map(|(_, discr)| discr.val) |
| 105 | + .collect() |
| 106 | +} |
| 107 | + |
| 108 | +/// Resolve the ABI of a function pointer type (via `tcx.fn_abi_of_fn_ptr`). |
| 109 | +pub fn fn_ptr_abi( |
| 110 | + tcx: TyCtxt<'_>, |
| 111 | + binder_stable: stable_mir::ty::PolyFnSig, |
| 112 | +) -> stable_mir::abi::FnAbi { |
| 113 | + let binder_internal = internal(tcx, binder_stable); |
| 114 | + rustc_internal::stable( |
| 115 | + tcx.fn_abi_of_fn_ptr( |
| 116 | + TypingEnv::fully_monomorphized().as_query_input((binder_internal, List::empty())), |
| 117 | + ) |
| 118 | + .unwrap(), |
| 119 | + ) |
| 120 | +} |
| 121 | + |
| 122 | +/// Convert a stable DefId to an internal DefId. |
| 123 | +pub fn internal_def_id(tcx: TyCtxt<'_>, id: stable_mir::DefId) -> DefId { |
| 124 | + rustc_internal::internal(tcx, id) |
| 125 | +} |
| 126 | + |
| 127 | +/// Get the stable crate ID for the local crate. |
| 128 | +pub fn local_crate_id(tcx: TyCtxt<'_>) -> u64 { |
| 129 | + tcx.stable_crate_id(rustc_span::def_id::LOCAL_CRATE) |
| 130 | + .as_u64() |
| 131 | +} |
0 commit comments