|
| 1 | +use std::io; |
| 2 | + |
| 3 | +use rustc_middle::ty::TyCtxt; |
| 4 | +use stable_mir::{ |
| 5 | + ty::{RigidTy, TyKind}, |
| 6 | + CrateItem, mir::Mutability, |
| 7 | +}; |
| 8 | + |
| 9 | + |
| 10 | +use super::{run, RustcInternal}; |
| 11 | + |
| 12 | +pub fn write_smir_pretty<'tcx>(tcx: TyCtxt<'tcx>, w: &mut dyn io::Write) -> io::Result<()> { |
| 13 | + run(tcx, || { |
| 14 | + let items = stable_mir::all_local_items(); |
| 15 | + items.iter().for_each(|item| { |
| 16 | + // Because we can't return a Result from a closure, we have to unwrap here. |
| 17 | + writeln!(w, "{}", function_name(*item,tcx)).unwrap(); |
| 18 | + writeln!(w, "{}", function_body(*item,tcx)).unwrap(); |
| 19 | + }) |
| 20 | + }); |
| 21 | + Ok(()) |
| 22 | +} |
| 23 | + |
| 24 | +pub fn function_name(item: CrateItem,tcx: TyCtxt<'_>) -> String { |
| 25 | + let mut name = String::new(); |
| 26 | + let body = item.body(); |
| 27 | + name.push_str("fn "); |
| 28 | + name.push_str(item.name().as_str()); |
| 29 | + if body.arg_locals().is_empty() { |
| 30 | + name.push_str("()"); |
| 31 | + }else{ |
| 32 | + name.push_str("("); |
| 33 | + } |
| 34 | + body.arg_locals().iter().for_each(|local| { |
| 35 | + name.push_str(format!("_{}: ",local.local).as_str()); |
| 36 | + name.push_str(&pretty_ty(local.ty.kind(), tcx)); |
| 37 | + }); |
| 38 | + if !body.arg_locals().is_empty() { |
| 39 | + name.push_str(")"); |
| 40 | + } |
| 41 | + let return_local = body.ret_local(); |
| 42 | + name.push_str(" -> "); |
| 43 | + name.push_str(&pretty_ty(return_local.ty.kind(), tcx)); |
| 44 | + name.push_str(" {"); |
| 45 | + name |
| 46 | +} |
| 47 | + |
| 48 | +pub fn function_body(item: CrateItem,_tcx: TyCtxt<'_>) -> String { |
| 49 | + let mut body_str = String::new(); |
| 50 | + let body = item.body(); |
| 51 | + body.inner_locals().iter().for_each(|local| { |
| 52 | + body_str.push_str(" "); |
| 53 | + body_str.push_str(format!("let {}",ret_mutability(&local.mutability)).as_str()); |
| 54 | + body_str.push_str(format!("_{}: ",local.local).as_str()); |
| 55 | + body_str.push_str(format!("{}",pretty_ty(local.ty.kind(), _tcx)).as_str()); |
| 56 | + body_str.push_str(";\n"); |
| 57 | + |
| 58 | + }); |
| 59 | + body_str.push_str("}"); |
| 60 | + body_str |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +pub fn ret_mutability(mutability: &Mutability) -> String { |
| 65 | + match mutability { |
| 66 | + Mutability::Not => "".to_string(), |
| 67 | + Mutability::Mut => "mut ".to_string(), |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +pub fn pretty_ty<'tcx>(ty: TyKind,tcx: TyCtxt<'tcx>) -> String { |
| 72 | + let mut pretty = String::new(); |
| 73 | + pretty.push_str(""); |
| 74 | + match ty { |
| 75 | + TyKind::RigidTy(rigid_ty) => match rigid_ty { |
| 76 | + RigidTy::Bool => "bool".to_string(), |
| 77 | + RigidTy::Char => "char".to_string(), |
| 78 | + RigidTy::Int(i) => match i { |
| 79 | + stable_mir::ty::IntTy::Isize => "isize".to_string(), |
| 80 | + stable_mir::ty::IntTy::I8 => "i8".to_string(), |
| 81 | + stable_mir::ty::IntTy::I16 => "i16".to_string(), |
| 82 | + stable_mir::ty::IntTy::I32 => "i32".to_string(), |
| 83 | + stable_mir::ty::IntTy::I64 => "i64".to_string(), |
| 84 | + stable_mir::ty::IntTy::I128 => "i128".to_string(), |
| 85 | + }, |
| 86 | + RigidTy::Uint(u) => match u { |
| 87 | + stable_mir::ty::UintTy::Usize => "usize".to_string(), |
| 88 | + stable_mir::ty::UintTy::U8 => "u8".to_string(), |
| 89 | + stable_mir::ty::UintTy::U16 => "u16".to_string(), |
| 90 | + stable_mir::ty::UintTy::U32 => "u32".to_string(), |
| 91 | + stable_mir::ty::UintTy::U64 => "u64".to_string(), |
| 92 | + stable_mir::ty::UintTy::U128 => "u128".to_string(), |
| 93 | + }, |
| 94 | + RigidTy::Float(f) => match f { |
| 95 | + stable_mir::ty::FloatTy::F32 => "f32".to_string(), |
| 96 | + stable_mir::ty::FloatTy::F64 => "f64".to_string(), |
| 97 | + }, |
| 98 | + RigidTy::Adt(def, _) => format!("{:#?}", tcx.type_of(def.0.internal_via_tls()).instantiate_identity()), |
| 99 | + RigidTy::Foreign(_) => format!("{:#?}", rigid_ty), |
| 100 | + RigidTy::Str => "str".to_string(), |
| 101 | + RigidTy::Array(_ty, len) => { |
| 102 | + format!("[{};{:#?}]", 1,len.internal_via_tls())}, |
| 103 | + RigidTy::Slice(ty) => pretty_ty(ty.kind(),tcx), |
| 104 | + RigidTy::RawPtr(_, _) => format!("{:#?}", rigid_ty), |
| 105 | + RigidTy::Ref(_, ty, _) => pretty_ty(ty.kind(),tcx), |
| 106 | + RigidTy::FnDef(_, _) => format!("{:#?}", rigid_ty), |
| 107 | + RigidTy::FnPtr(_) => format!("{:#?}", rigid_ty), |
| 108 | + RigidTy::Closure(_, _) => format!("{:#?}", rigid_ty), |
| 109 | + RigidTy::Coroutine(_, _, _) => format!("{:#?}", rigid_ty), |
| 110 | + RigidTy::Dynamic(_, _, _) => format!("{:#?}", rigid_ty), |
| 111 | + RigidTy::Never => "!".to_string(), |
| 112 | + RigidTy::Tuple(tuple) => { |
| 113 | + if tuple.is_empty(){ |
| 114 | + "()".to_string() |
| 115 | + }else { |
| 116 | + let mut tuple_str = String::new(); |
| 117 | + tuple_str.push_str("("); |
| 118 | + tuple.iter().enumerate().for_each(|(i,ty)| { |
| 119 | + tuple_str.push_str(&pretty_ty(ty.kind(),tcx)); |
| 120 | + if i != tuple.len() - 1 { |
| 121 | + tuple_str.push_str(", "); |
| 122 | + } |
| 123 | + }); |
| 124 | + tuple_str.push_str(")"); |
| 125 | + tuple_str |
| 126 | + } |
| 127 | + }, |
| 128 | + }, |
| 129 | + TyKind::Alias(_, _) => format!("{:#?}", ty), |
| 130 | + TyKind::Param(_) => format!("{:#?}", ty), |
| 131 | + TyKind::Bound(_, _) => format!("{:#?}", ty), |
| 132 | + } |
| 133 | +} |
0 commit comments