use std::fmt;
use wafel_data_access::MemoryLayout;
use wafel_data_type::{DataTypeRef, FloatType, IntType};
use crate::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DataType {
Void,
Int(IntType),
Float(FloatType),
Pointer,
Array,
Struct,
Union,
}
impl DataType {
pub fn is_void(&self) -> bool {
matches!(self, Self::Void)
}
pub fn is_int(&self) -> bool {
matches!(self, Self::Int(_))
}
pub fn is_float(&self) -> bool {
matches!(self, Self::Float(_))
}
pub fn is_pointer(&self) -> bool {
matches!(self, Self::Pointer)
}
pub fn is_array(&self) -> bool {
matches!(self, Self::Array)
}
pub fn is_struct(&self) -> bool {
matches!(self, Self::Struct)
}
pub fn is_union(&self) -> bool {
matches!(self, Self::Union)
}
}
impl fmt::Display for DataType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DataType::Void => write!(f, "void"),
DataType::Int(int_type) => write!(f, "{}", int_type),
DataType::Float(float_type) => write!(f, "{}", float_type),
DataType::Pointer => write!(f, "pointer"),
DataType::Array => write!(f, "array"),
DataType::Struct => write!(f, "struct"),
DataType::Union => write!(f, "union"),
}
}
}
pub(crate) fn simplified_data_type(
layout: &impl MemoryLayout,
data_type: &DataTypeRef,
) -> Result<DataType, Error> {
use wafel_data_type::DataType::*;
Ok(match data_type.as_ref() {
Void => DataType::Void,
Int(int_type) => DataType::Int(*int_type),
Float(float_type) => DataType::Float(*float_type),
Pointer { .. } => DataType::Pointer,
Array { .. } => DataType::Array,
Struct { .. } => DataType::Struct,
Union { .. } => DataType::Union,
Name(type_name) => {
simplified_data_type(layout, layout.data_layout().data_type(type_name)?)?
}
})
}