use std::cmp::Ordering;
use std::fmt;
use std::hash::Hash;
use std::hash::Hasher;
use std::marker::PhantomData;
use delegate::delegate;
use merc_unsafety::ProtectionIndex;
use merc_unsafety::StablePointer;
use crate::Markable;
use crate::storage::Marker;
use crate::storage::SharedSymbol;
use crate::storage::THREAD_TERM_POOL;
pub trait Symb<'a, 'b> {
fn name(&'b self) -> &'a str;
fn arity(&self) -> usize;
fn copy(&'b self) -> SymbolRef<'a>;
fn index(&self) -> usize;
fn shared(&self) -> &SymbolIndex;
}
pub type SymbolIndex = StablePointer<SharedSymbol>;
#[repr(transparent)]
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct SymbolRef<'a> {
shared: SymbolIndex,
marker: PhantomData<&'a ()>,
}
#[cfg(not(debug_assertions))]
const _: () = assert!(std::mem::size_of::<SymbolRef>() == std::mem::size_of::<usize>());
#[cfg(not(debug_assertions))]
const _: () = assert!(std::mem::size_of::<Option<SymbolRef>>() == std::mem::size_of::<usize>());
impl<'a> SymbolRef<'a> {
pub fn protect(&self) -> Symbol {
THREAD_TERM_POOL.with(|tp| tp.protect_symbol(self))
}
pub unsafe fn from_index(index: &SymbolIndex) -> SymbolRef<'a> {
SymbolRef {
shared: unsafe { index.copy() },
marker: PhantomData,
}
}
}
impl SymbolRef<'_> {
pub(crate) fn from_symbol<'a, 'b, S: Symb<'a, 'b>>(symbol: &'b S) -> Self {
SymbolRef {
shared: unsafe { symbol.shared().copy() },
marker: PhantomData,
}
}
}
impl<'a> Symb<'a, '_> for SymbolRef<'a> {
fn name(&self) -> &'a str {
unsafe { std::mem::transmute(self.shared.deref().name()) }
}
fn arity(&self) -> usize {
unsafe { SharedSymbol::arity_of(self.shared.erased()) }
}
fn copy(&self) -> SymbolRef<'a> {
unsafe { SymbolRef::from_index(self.shared()) }
}
fn index(&self) -> usize {
self.shared.erased().as_ptr() as usize
}
fn shared(&self) -> &SymbolIndex {
&self.shared
}
}
impl Markable for SymbolRef<'_> {
fn mark(&self, marker: &mut Marker) {
marker.mark_symbol(self);
}
fn contains_term(&self, _term: &crate::aterm::ATermRef<'_>) -> bool {
false
}
fn contains_symbol(&self, symbol: &SymbolRef<'_>) -> bool {
self == symbol
}
fn len(&self) -> usize {
1
}
}
impl fmt::Display for SymbolRef<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}
impl fmt::Debug for SymbolRef<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}
pub struct Symbol {
symbol: SymbolRef<'static>,
root: ProtectionIndex,
}
impl Symbol {
pub fn new<N>(name: N, arity: usize) -> Symbol
where
N: AsRef<str>,
{
THREAD_TERM_POOL.with(|tp| tp.create_symbol(name, arity))
}
pub fn new_fmt(args: fmt::Arguments<'_>, arity: usize) -> Symbol {
THREAD_TERM_POOL.with(|tp| tp.create_symbol_fmt(args, arity))
}
pub fn new_unused_fmt(args: fmt::Arguments<'_>, arity: usize) -> Option<Symbol> {
THREAD_TERM_POOL.with(|tp| tp.create_unused_symbol_fmt(args, arity))
}
}
impl Symbol {
pub(crate) unsafe fn from_index(index: &SymbolIndex, root: ProtectionIndex) -> Symbol {
Self {
symbol: unsafe { SymbolRef::from_index(index) },
root,
}
}
pub fn root(&self) -> ProtectionIndex {
self.root
}
pub fn copy(&self) -> SymbolRef<'_> {
self.symbol.copy()
}
pub fn get(&self) -> &SymbolRef<'_> {
&self.symbol
}
}
impl<'a> Symb<'a, '_> for &'a Symbol {
delegate! {
to self.symbol {
fn name(&self) -> &'a str;
fn arity(&self) -> usize;
fn copy(&self) -> SymbolRef<'a>;
fn index(&self) -> usize;
fn shared(&self) -> &SymbolIndex;
}
}
}
impl<'a, 'b> Symb<'a, 'b> for Symbol
where
'b: 'a,
{
delegate! {
to self.symbol {
fn name(&self) -> &'a str;
fn arity(&self) -> usize;
fn copy(&self) -> SymbolRef<'a>;
fn index(&self) -> usize;
fn shared(&self) -> &SymbolIndex;
}
}
}
impl Drop for Symbol {
fn drop(&mut self) {
THREAD_TERM_POOL.with(|tp| {
tp.drop_symbol(self);
})
}
}
impl From<&SymbolRef<'_>> for Symbol {
fn from(value: &SymbolRef) -> Self {
value.protect()
}
}
impl Clone for Symbol {
fn clone(&self) -> Self {
self.copy().protect()
}
}
impl fmt::Display for Symbol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}
impl fmt::Debug for Symbol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}
impl Hash for Symbol {
fn hash<H: Hasher>(&self, state: &mut H) {
self.copy().hash(state)
}
}
impl PartialEq for Symbol {
fn eq(&self, other: &Self) -> bool {
self.copy().eq(&other.copy())
}
}
impl PartialEq<SymbolRef<'_>> for Symbol {
fn eq(&self, other: &SymbolRef<'_>) -> bool {
self.copy().eq(other)
}
}
impl PartialOrd for Symbol {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Symbol {
fn cmp(&self, other: &Self) -> Ordering {
self.copy().cmp(&other.copy())
}
}
impl Eq for Symbol {}