use crate::metadata::{token::Token, typesystem::CilPrimitiveKind};
pub mod names {
pub const OBJECT: &str = "System.Object";
pub const VALUE_TYPE: &str = "System.ValueType";
pub const ENUM: &str = "System.Enum";
pub const DELEGATE: &str = "System.Delegate";
pub const MULTICAST_DELEGATE: &str = "System.MulticastDelegate";
pub const STRING: &str = "System.String";
pub const VOID: &str = "System.Void";
pub const EXCEPTION: &str = "System.Exception";
pub const TYPE: &str = "System.Type";
pub const ARRAY: &str = "System.Array";
pub const ATTRIBUTE: &str = "System.Attribute";
}
pub mod members {
pub const CCTOR: &str = ".cctor";
pub const CTOR: &str = ".ctor";
pub const MODULE_TYPE: &str = "<Module>";
pub const PRIVATE_IMPL: &str = "<PrivateImplementationDetails>";
}
#[must_use]
pub fn primitive_token_to_typecode(token: Token) -> Option<i32> {
if token.value() & 0xFFFF_FF00 != 0xF000_0000 {
return None;
}
let element_byte = (token.value() & 0xFF) as u8;
CilPrimitiveKind::from_byte(element_byte).ok()?.typecode()
}
#[must_use]
pub fn fullname_to_typecode(fullname: &str) -> i32 {
match fullname {
"System.Boolean" => 3,
"System.Char" => 4,
"System.SByte" => 5,
"System.Byte" => 6,
"System.Int16" => 7,
"System.UInt16" => 8,
"System.Int32" => 9,
"System.UInt32" => 10,
"System.Int64" => 11,
"System.UInt64" => 12,
"System.Single" => 13,
"System.Double" => 14,
"System.DateTime" => 16,
"System.String" => 18,
_ => 1, }
}
#[must_use]
pub fn is_known_value_type(fullname: &str) -> bool {
matches!(
fullname,
"System.Boolean"
| "System.Char"
| "System.SByte"
| "System.Byte"
| "System.Int16"
| "System.UInt16"
| "System.Int32"
| "System.UInt32"
| "System.Int64"
| "System.UInt64"
| "System.Single"
| "System.Double"
| "System.IntPtr"
| "System.UIntPtr"
| "System.Decimal"
| "System.DateTime"
| "System.DateTimeOffset"
| "System.TimeSpan"
| "System.Guid"
| "System.TypedReference"
)
}
#[must_use]
pub fn system_name_to_primitive(name: &str) -> Option<CilPrimitiveKind> {
match name {
"Void" => Some(CilPrimitiveKind::Void),
"Boolean" => Some(CilPrimitiveKind::Boolean),
"Char" => Some(CilPrimitiveKind::Char),
"SByte" => Some(CilPrimitiveKind::I1),
"Byte" => Some(CilPrimitiveKind::U1),
"Int16" => Some(CilPrimitiveKind::I2),
"UInt16" => Some(CilPrimitiveKind::U2),
"Int32" => Some(CilPrimitiveKind::I4),
"UInt32" => Some(CilPrimitiveKind::U4),
"Int64" => Some(CilPrimitiveKind::I8),
"UInt64" => Some(CilPrimitiveKind::U8),
"Single" => Some(CilPrimitiveKind::R4),
"Double" => Some(CilPrimitiveKind::R8),
"IntPtr" => Some(CilPrimitiveKind::I),
"UIntPtr" => Some(CilPrimitiveKind::U),
"String" => Some(CilPrimitiveKind::String),
"Object" => Some(CilPrimitiveKind::Object),
_ => None,
}
}