|
| 1 | +use std::{collections::HashMap, fmt::Display}; |
| 2 | + |
| 3 | +pub mod symbol; |
| 4 | + |
| 5 | +#[allow(non_camel_case_types)] |
| 6 | +#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Copy, PartialEq, Hash, Eq)] |
| 7 | +pub enum PrimitiveType { |
| 8 | + u8, |
| 9 | + u16, |
| 10 | + u32, |
| 11 | + i8, |
| 12 | + i16, |
| 13 | + i32, |
| 14 | + f32, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(serde::Serialize, serde::Deserialize, PartialEq, Clone, Hash, Eq)] |
| 18 | +pub struct Atype(String); |
| 19 | + |
| 20 | +impl Display for Atype { |
| 21 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 22 | + self.0.fmt(f) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +pub type Types = HashMap<Atype, TypeDef>; |
| 27 | + |
| 28 | +pub enum TypeDef { |
| 29 | + Struct { |
| 30 | + name: Atype, |
| 31 | + fields: Vec<symbol::Member>, |
| 32 | + }, |
| 33 | + Enum { |
| 34 | + name: Atype, |
| 35 | + discriminator_type: PrimitiveType, |
| 36 | + variants: Vec<EnumVariant>, |
| 37 | + }, |
| 38 | +} |
| 39 | + |
| 40 | +impl TypeDef { |
| 41 | + fn size_of(&self, types: &Types) -> u64 { |
| 42 | + match self { |
| 43 | + TypeDef::Struct { name, fields } => fields |
| 44 | + .last() |
| 45 | + .map(|x| { |
| 46 | + x.offset.unwrap().next_multiple_of(x.ty.align_of(&types)) + x.ty.size_of(&types) |
| 47 | + }) |
| 48 | + .unwrap_or(0), |
| 49 | + TypeDef::Enum { |
| 50 | + name, |
| 51 | + discriminator_type, |
| 52 | + variants, |
| 53 | + } => todo!(), |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + fn align_of(&self, _types: &Types) -> u64 { |
| 58 | + todo!() |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +pub struct EnumVariant { |
| 63 | + pub name: String, |
| 64 | + pub ty: PrimitiveType, |
| 65 | + pub expr: shunting::RPNExpr, |
| 66 | +} |
| 67 | + |
| 68 | +impl Atype { |
| 69 | + pub fn is_primitive(&self) -> bool { |
| 70 | + ["u8", "u16", "u32", "i8", "i16", "i32", "f32"].contains(&self.0.as_ref()) |
| 71 | + } |
| 72 | + |
| 73 | + pub fn size_of(&self, types: &Types) -> u64 { |
| 74 | + match PrimitiveType::try_from(self) { |
| 75 | + Ok(p) => p.size_of(), |
| 76 | + Err(()) => types.get(self).unwrap().size_of(types), |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + pub fn align_of(&self, types: &Types) -> u64 { |
| 81 | + match PrimitiveType::try_from(self) { |
| 82 | + Ok(p) => p.align_of(), |
| 83 | + Err(()) => types.get(self).unwrap().size_of(types), |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl TryFrom<&Atype> for PrimitiveType { |
| 89 | + type Error = (); |
| 90 | + |
| 91 | + fn try_from(t: &Atype) -> Result<PrimitiveType, Self::Error> { |
| 92 | + match t.0.as_str() { |
| 93 | + "u8" => Ok(PrimitiveType::u8), |
| 94 | + "u16" => Ok(PrimitiveType::u16), |
| 95 | + "u32" => Ok(PrimitiveType::u32), |
| 96 | + "i8" => Ok(PrimitiveType::i8), |
| 97 | + "i16" => Ok(PrimitiveType::i16), |
| 98 | + "i32" => Ok(PrimitiveType::i32), |
| 99 | + "f32" => Ok(PrimitiveType::f32), |
| 100 | + _ => Err(()), |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl PrimitiveType { |
| 106 | + pub fn size_of(&self) -> u64 { |
| 107 | + match self { |
| 108 | + PrimitiveType::u8 | PrimitiveType::i8 => 1, |
| 109 | + PrimitiveType::u16 | PrimitiveType::i16 => 2, |
| 110 | + PrimitiveType::u32 | PrimitiveType::i32 | PrimitiveType::f32 => 4, |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + pub fn align_of(&self) -> u64 { |
| 115 | + match self { |
| 116 | + PrimitiveType::u8 | PrimitiveType::i8 => 1, |
| 117 | + PrimitiveType::u16 | PrimitiveType::i16 => 2, |
| 118 | + PrimitiveType::u32 | PrimitiveType::i32 | PrimitiveType::f32 => 4, |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +impl Into<Atype> for &PrimitiveType { |
| 124 | + fn into(self) -> Atype { |
| 125 | + match self { |
| 126 | + PrimitiveType::u8 => Atype("u8".to_string()), |
| 127 | + PrimitiveType::u16 => Atype("u16".to_string()), |
| 128 | + PrimitiveType::u32 => Atype("u32".to_string()), |
| 129 | + PrimitiveType::i8 => Atype("i8".to_string()), |
| 130 | + PrimitiveType::i16 => Atype("i16".to_string()), |
| 131 | + PrimitiveType::i32 => Atype("i32".to_string()), |
| 132 | + PrimitiveType::f32 => Atype("f32".to_string()), |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments