cilly/
lib.rs

1#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
2#![allow(clippy::module_name_repetitions)]
3pub mod field_desc;
4pub mod r#type;
5pub use r#type::*;
6pub type IString = Box<str>;
7pub mod dotnet_type;
8pub use dotnet_type::*;
9pub mod fn_sig;
10pub use fn_sig::*;
11pub mod access_modifier;
12pub mod asm;
13pub mod asm_exporter;
14pub mod basic_block;
15pub mod c_exporter;
16pub mod call_site;
17pub mod cil_iter;
18pub mod cil_iter_mut;
19pub mod cil_node;
20pub mod cil_root;
21pub mod cil_tree;
22pub mod entrypoint;
23pub mod ilasm_exporter;
24pub mod ilasm_op;
25pub mod method;
26pub mod static_field_desc;
27pub mod type_def;
28#[must_use]
29/// Returns the name of a fixed-size array
30pub fn arr_name(element_count: usize, element: &Type) -> IString {
31    let element_name = mangle(element);
32    format!("Arr{element_count}_{element_name}",).into()
33}
34/// Returns a mangled type name.
35/// # Panics
36/// Panics when a genetic managed array is used.
37pub fn mangle(tpe: &Type) -> std::borrow::Cow<'static, str> {
38    match tpe {
39        Type::Bool => "b".into(),
40        Type::Void => "v".into(),
41        Type::U8 => "u8".into(),
42        Type::U16 => "u16".into(),
43        Type::U32 => "u32".into(),
44        Type::U64 => "u64".into(),
45        Type::U128 => "u128".into(),
46        Type::USize => "us".into(),
47        Type::I8 => "i8".into(),
48        Type::I16 => "i16".into(),
49        Type::I32 => "i32".into(),
50        Type::I64 => "i64".into(),
51        Type::I128 => "i128".into(),
52        Type::ISize => "is".into(),
53        Type::F16 => "f16".into(),
54        Type::F32 => "f32".into(),
55        Type::F64 => "f64".into(),
56        Type::Ptr(inner) => format!("p{inner}", inner = mangle(inner)).into(),
57        Type::DotnetType(tpe) => {
58            assert!(
59                tpe.generics().is_empty(),
60                "Arrays of generic .NET types not supported yet"
61            );
62            tpe.name_path().replace('.', "_").into()
63        }
64        Type::ManagedArray { element, dims } => format!("a{}{}", dims, mangle(element)).into(),
65        Type::DotnetChar => "c".into(),
66        Type::GenericArg(_) => todo!("Can't mangle generic type arg"),
67        Type::FnDef(name) => format!("fn{}{}", name.len(), name).into(),
68        Type::Unresolved => "un".into(),
69        Type::DelegatePtr(sig) => format!(
70            "d{output}{input_count}{input_string}",
71            output = mangle(sig.output()),
72            input_count = sig.inputs().len(),
73            input_string = sig.inputs().iter().map(mangle).collect::<String>()
74        )
75        .into(),
76        Type::ManagedReference(inner) => format!("m{inner}", inner = mangle(inner)).into(),
77        Type::Foreign => "g".into(),
78        Type::CallGenericArg(_) => "l".into(),
79        Type::MethodGenericArg(_) => "h".into(),
80        //_ => todo!("Can't mangle type {tpe:?}"),
81    }
82}
83#[must_use]
84pub fn mem_checks() -> bool {
85    *crate::MEM_CHECKS
86}
87use lazy_static::lazy_static;
88lazy_static! {
89    #[doc = "Tells codegen to insert memory consistency checks after each call. If INSERT_MIR_DEBUG_COMMENTS is enabled, the consistency checks will be run also after each MIR statement."]pub static ref MEM_CHECKS:bool = {
90        std::env::vars().find_map(|(key,value)|if key == stringify!(MEM_CHECKS){
91            Some(value)
92        }else {
93            None
94        }).is_some_and(|value|match value.as_ref(){
95            "0"|"false"|"False"|"FALSE" => false,"1"|"true"|"True"|"TRUE" => true,_ => panic!("Boolean enviroment variable {} has invalid value {}",stringify!(MEM_CHECKS),value),
96        })
97    };
98}
99#[derive(Clone, Copy)]
100pub enum IlasmFlavour {
101    Clasic,
102    Modern,
103}