use super::Module;
use crate::utils::ptr_util::WeakPtr;
use std::fmt;
#[derive(Clone, Debug, Default)]
pub struct Scope {
pub parser_scope: String,
pub module: Option<WeakPtr<Module>>,
}
impl Scope {
pub fn push_scope(&mut self, scope: &str) {
if !self.parser_scope.is_empty() {
self.parser_scope.push_str("::");
}
self.parser_scope.push_str(scope);
}
pub fn pop_scope(&mut self) {
if let Some(last_scope_index) = self.parser_scope.rfind("::") {
#[cfg(debug_assertions)]
{
let module_scope = self.module.as_ref().map(|m| m.borrow().nested_module_identifier());
debug_assert!(self.parser_scope.len() > module_scope.map_or(0, str::len))
}
self.parser_scope.truncate(last_scope_index);
} else {
debug_assert!(self.module.is_none());
self.parser_scope.clear();
}
}
}
pub fn get_scoped_identifier(identifier: &str, scope: &str) -> String {
if scope.is_empty() {
identifier.to_owned()
} else {
scope.to_owned() + "::" + identifier
}
}
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Encoding {
Slice1,
#[default]
Slice2,
}
impl fmt::Display for Encoding {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match self {
Self::Slice1 => "Slice1",
Self::Slice2 => "Slice2",
})
}
}
pub type CompilationMode = Encoding;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TagFormat {
F1,
F2,
F4,
F8,
Size,
VSize,
FSize,
Class,
OptimizedVSize,
}
impl fmt::Display for TagFormat {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TagFormat::F1 => write!(f, "F1"),
TagFormat::F2 => write!(f, "F2"),
TagFormat::F4 => write!(f, "F4"),
TagFormat::F8 => write!(f, "F8"),
TagFormat::Size => write!(f, "Size"),
TagFormat::VSize => write!(f, "VSize"),
TagFormat::FSize => write!(f, "FSize"),
TagFormat::Class => write!(f, "Class"),
TagFormat::OptimizedVSize => write!(f, "OptimizedVSize"),
}
}
}