internpool helpers/ fixes

This commit is contained in:
Janis 2024-12-23 02:32:43 +01:00
parent d9e6d99286
commit 3be955aebf

View file

@ -43,6 +43,7 @@ impl Display for SimpleType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!( write!(
f, f,
"{}",
match self { match self {
SimpleType::F32 => "f32", SimpleType::F32 => "f32",
SimpleType::F64 => "f64", SimpleType::F64 => "f64",
@ -151,32 +152,11 @@ pub enum Key<'a> {
FalseValue, FalseValue,
} }
struct KeyDisplay<'a> { pub struct KeyDisplay<'a> {
ip: &'a InternPool, ip: &'a InternPool,
key: Key<'a>, key: Key<'a>,
} }
struct DisplayCommaSep<T: IntoIterator<Item = Display>> {
t: T,
}
impl<T: IntoIterator<Item = Display>> Display for DisplayCommaSep<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut iter = self.t.into_iter();
let Some(ref next) = iter.next() else {
return Ok(());
};
write!(f, "{next}")?;
for ref item in iter {
write!(f, ", {next}")?;
}
Ok(())
}
}
impl Display for KeyDisplay<'_> { impl Display for KeyDisplay<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.key { match self.key {
@ -188,8 +168,8 @@ impl Display for KeyDisplay<'_> {
Key::UInt64 { bits } => write!(f, "{bits}")?, Key::UInt64 { bits } => write!(f, "{bits}")?,
Key::F32 { bits } => write!(f, "{bits}")?, Key::F32 { bits } => write!(f, "{bits}")?,
Key::F64 { bits } => write!(f, "{bits}")?, Key::F64 { bits } => write!(f, "{bits}")?,
Key::PositiveInt { bigint } => write!(f, "{bigint}")?, Key::PositiveInt { ref bigint } => write!(f, "{bigint}")?,
Key::NegativeInt { bigint } => write!(f, "{bigint}")?, Key::NegativeInt { ref bigint } => write!(f, "{bigint}")?,
Key::UIntType { bit_width } => write!(f, "u{bit_width}")?, Key::UIntType { bit_width } => write!(f, "u{bit_width}")?,
Key::SIntType { bit_width } => write!(f, "i{bit_width}")?, Key::SIntType { bit_width } => write!(f, "i{bit_width}")?,
Key::SimpleType { ty } => write!(f, "{ty}")?, Key::SimpleType { ty } => write!(f, "{ty}")?,
@ -207,24 +187,31 @@ impl Display for KeyDisplay<'_> {
)?, )?,
Key::FunctionType { Key::FunctionType {
return_type, return_type,
parameters, ref parameters,
} => write!( } => {
f, write!(f, "fn (")?;
"fn ({}) -> {}",
DisplayCommaSep { let mut iter =
t: parameters.iter().map(|&ty| self.ip.display_key(ty)) parameters.iter().map(|&ty| self.ip.display_key(ty));
}, let Some(next) = iter.next() else {
self.ip.display_key(return_type) return Ok(());
)?, };
write!(f, "{next}")?;
for item in iter {
write!(f, ", {item}")?;
}
write!(f, ") -> {}", self.ip.display_key(return_type))?;
}
Key::StructType { Key::StructType {
decl, decl,
name, name,
packed, packed,
c_like, c_like,
fields, ref fields,
} => todo!(), } => todo!(),
Key::TrueValue => write!(f, "true"), Key::TrueValue => write!(f, "true")?,
Key::FalseValue => write!(f, "false"), Key::FalseValue => write!(f, "false")?,
} }
Ok(()) Ok(())
@ -803,10 +790,60 @@ impl InternPool {
} }
} }
pub fn from_ast1_type( pub fn insert_ast1_type(
&mut self, &mut self,
pointer_bits: u16, pointer_bits: u16,
ty: &crate::ast::Type, ty: &crate::ast::Type,
) -> Index {
match ty {
crate::ast::Type::Bool => self.get_bool_type(),
crate::ast::Type::ComptimeNumber => self.get_comptime_int_type(),
crate::ast::Type::Integer(i) => self.get_or_insert({
if i.signed {
Key::SIntType { bit_width: i.bits }
} else {
Key::UIntType { bit_width: i.bits }
}
}),
crate::ast::Type::Floating(crate::ast::FloatingType::Binary32) => {
self.get_f32_type()
}
crate::ast::Type::Floating(crate::ast::FloatingType::Binary64) => {
self.get_f64_type()
}
crate::ast::Type::Pointer { constness, pointee } => {
let pointee = self.insert_ast1_type(pointer_bits, &pointee);
self.get_or_insert(Key::PointerType {
pointee,
flags: PointerFlags::new(*constness, false, false),
})
}
crate::ast::Type::Fn {
parameter_types,
return_type,
} => {
let parameters = parameter_types
.iter()
.map(|ty| self.insert_ast1_type(pointer_bits, ty))
.collect::<Vec<_>>();
let return_type =
self.from_ast1_type(pointer_bits, &return_type);
self.get_or_insert(Key::FunctionType {
return_type,
parameters,
})
}
crate::ast::Type::Void => self.get_void_type(),
_ => {
todo!()
}
}
}
pub fn from_ast1_type(
&self,
pointer_bits: u16,
ty: &crate::ast::Type,
) -> Index { ) -> Index {
match ty { match ty {
crate::ast::Type::Bool => self.get_bool_type(), crate::ast::Type::Bool => self.get_bool_type(),
@ -826,7 +863,7 @@ impl InternPool {
} }
crate::ast::Type::Pointer { constness, pointee } => { crate::ast::Type::Pointer { constness, pointee } => {
let pointee = self.from_ast1_type(pointer_bits, &pointee); let pointee = self.from_ast1_type(pointer_bits, &pointee);
self.get_or_insert(Key::PointerType { self.get_assume_present(&Key::PointerType {
pointee, pointee,
flags: PointerFlags::new(*constness, false, false), flags: PointerFlags::new(*constness, false, false),
}) })
@ -841,7 +878,10 @@ impl InternPool {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let return_type = let return_type =
self.from_ast1_type(pointer_bits, &return_type); self.from_ast1_type(pointer_bits, &return_type);
self.get_function_type(return_type, parameters) self.get_assume_present(&Key::FunctionType {
return_type,
parameters,
})
} }
crate::ast::Type::Void => self.get_void_type(), crate::ast::Type::Void => self.get_void_type(),
_ => { _ => {
@ -1479,3 +1519,21 @@ impl InternPool {
}) })
} }
} }
impl InternPool {
pub fn is_function(&self, index: Index) -> bool {
matches!(self.get_key(index), Key::FunctionType { .. })
}
pub fn is_type(&self, index: Index) -> bool {
matches!(
self.get_key(index),
Key::FunctionType { .. }
| Key::ArrayType { .. }
| Key::PointerType { .. }
| Key::SimpleType { .. }
| Key::SIntType { .. }
| Key::UIntType { .. }
| Key::StructType { .. }
)
}
}