use std::collections::{HashMap, HashSet};
use crate::semantic::{
AccessPath, FunctionBody,
metadata::{FunctionId, FunctionLayoutMetadata, TypeId, TypeLayoutMetadata},
};
#[derive(Debug, Clone)]
pub struct SemanticModel {
pub(crate) function_ids: HashMap<AccessPath, FunctionId>,
pub(crate) type_ids: HashMap<AccessPath, TypeId>,
pub(crate) function_layout_metas: Vec<FunctionLayoutMetadata>,
pub(crate) function_bodies: Vec<FunctionBody>,
pub(crate) exported_functions: HashSet<FunctionId>,
pub(crate) type_layout_metas: Vec<TypeLayoutMetadata>,
}
impl SemanticModel {
pub fn empty() -> Self {
Self {
function_ids: HashMap::new(),
type_ids: HashMap::new(),
function_layout_metas: Vec::new(),
function_bodies: Vec::new(),
exported_functions: HashSet::new(),
type_layout_metas: Vec::new(),
}
}
pub fn insert_function(
&mut self,
path: AccessPath,
meta: FunctionLayoutMetadata,
body: FunctionBody,
exported: bool,
) -> (
FunctionId,
Option<(FunctionLayoutMetadata, FunctionBody, bool)>,
) {
if let Some(function_id) = self.get_function_id(&path) {
let old_meta =
std::mem::replace(&mut self.function_layout_metas[function_id as usize], meta);
let old_body = std::mem::replace(&mut self.function_bodies[function_id as usize], body);
let old_exported = self.exported_functions.contains(&function_id);
if old_exported && !exported {
self.exported_functions.remove(&function_id);
} else if !old_exported && exported {
self.exported_functions.insert(function_id);
}
(function_id, Some((old_meta, old_body, old_exported)))
} else {
let function_id = self.function_layout_metas.len() as FunctionId;
self.function_layout_metas.push(meta);
self.function_bodies.push(body);
self.function_ids.insert(path, function_id);
if exported {
self.exported_functions.insert(function_id);
}
(function_id, None)
}
}
pub fn insert_type(
&mut self,
path: AccessPath,
meta: TypeLayoutMetadata,
) -> (TypeId, Option<TypeLayoutMetadata>) {
if let Some(type_id) = self.get_type_id(&path) {
let old_meta = std::mem::replace(&mut self.type_layout_metas[type_id as usize], meta);
(type_id, Some(old_meta))
} else {
let type_id = self.type_layout_metas.len() as TypeId;
self.type_layout_metas.push(meta);
self.type_ids.insert(path, type_id);
(type_id, None)
}
}
pub fn get_function_id(&self, path: &AccessPath) -> Option<FunctionId> {
self.function_ids.get(path).cloned()
}
pub fn get_type_id(&self, path: &AccessPath) -> Option<TypeId> {
self.type_ids.get(path).cloned()
}
pub fn get_function_layout_metadata_by_path(
&self,
path: &AccessPath,
) -> Option<&FunctionLayoutMetadata> {
self.function_ids
.get(path)
.map(|&id| &self.function_layout_metas[id as usize])
}
pub fn get_type_layout_metadata_by_path(
&self,
path: &AccessPath,
) -> Option<&TypeLayoutMetadata> {
self.type_ids
.get(path)
.map(|&id| &self.type_layout_metas[id as usize])
}
pub fn get_function_layout_metadata(&self, id: FunctionId) -> Option<&FunctionLayoutMetadata> {
self.function_layout_metas.get(id as usize)
}
pub fn get_function_body(&self, id: FunctionId) -> Option<&FunctionBody> {
self.function_bodies.get(id as usize)
}
pub fn get_type_layout_metadata(&self, id: TypeId) -> Option<&TypeLayoutMetadata> {
self.type_layout_metas.get(id as usize)
}
}