deimos-lang/dmc-lib/src/ir/ir_class.rs

63 lines
1.6 KiB
Rust

use crate::ast::fqn_util::fqn_parts_to_string;
use crate::type_info::TypeInfo;
use dvm_lib::vm::class::{Class, Field};
use dvm_lib::vm::type_info::TypeInfo as VmTypeInfo;
use std::rc::Rc;
pub struct IrClass {
declared_name: Rc<str>,
fqn: Rc<str>,
fields: Vec<IrField>,
}
impl IrClass {
pub fn new(declared_name: Rc<str>, fqn: Rc<str>, fields: Vec<IrField>) -> Self {
Self {
declared_name,
fqn,
fields,
}
}
pub fn to_vm_class(&self) -> Class {
Class::new(
self.declared_name.clone(),
self.fqn.clone(),
self.fields.iter().map(IrField::to_vm_field).collect(),
)
}
}
pub struct IrField {
debug_name: Rc<str>,
field_index: usize,
type_info: TypeInfo,
}
impl IrField {
pub fn new(debug_name: Rc<str>, field_index: usize, type_info: TypeInfo) -> Self {
Self {
debug_name,
field_index,
type_info,
}
}
pub fn to_vm_field(&self) -> Field {
Field::new(
self.debug_name.clone(),
self.field_index,
match &self.type_info {
TypeInfo::Integer => VmTypeInfo::Int,
TypeInfo::Double => VmTypeInfo::Double,
TypeInfo::String => VmTypeInfo::String,
TypeInfo::ClassInstance(class_symbol) => {
VmTypeInfo::ClassInstance(fqn_parts_to_string(class_symbol.fqn_parts()).into())
}
TypeInfo::GenericType(_) => VmTypeInfo::Any,
_ => panic!(),
},
)
}
}