Skip to main content

syn_sem/semantic/entry/
context.rs

1use crate::{
2    semantic::{eval, infer},
3    SharedLock, SharedLockGuard,
4};
5use any_intern::{DroplessInterner, Interned};
6use logic_eval::Intern;
7use logic_eval_util::symbol::SymbolTable;
8use std::{
9    fmt::{self, Display},
10    result::Result as StdResult,
11};
12
13// Self-referential type
14#[derive(Debug)]
15pub struct GlobalCx<'gcx> {
16    pub interner: DroplessInterner,
17    config: SharedLock<Config>,
18    lasting_symbols: SharedLock<LastingSymbols<'gcx>>,
19}
20
21impl<'gcx> GlobalCx<'gcx> {
22    pub fn configure(&self, config: Config) {
23        *self.config.lock().unwrap() = config;
24    }
25
26    pub fn get_config(&self) -> SharedLockGuard<'_, Config> {
27        self.config.lock().unwrap()
28    }
29
30    pub(crate) fn lasting_symbols(&'gcx self) -> SharedLockGuard<'gcx, LastingSymbols<'gcx>> {
31        self.lasting_symbols.lock().unwrap()
32    }
33}
34
35impl Default for GlobalCx<'_> {
36    fn default() -> Self {
37        Self {
38            interner: DroplessInterner::new(),
39            config: SharedLock::new(Config::default()),
40            lasting_symbols: SharedLock::new(LastingSymbols::default()),
41        }
42    }
43}
44
45impl<'gcx> Intern for GlobalCx<'gcx> {
46    type InternedStr<'a>
47        = any_intern::Interned<'a, str>
48    where
49        Self: 'a;
50
51    fn intern_formatted_str<T: Display + ?Sized>(
52        &self,
53        value: &T,
54        upper_size: usize,
55    ) -> StdResult<Self::InternedStr<'_>, fmt::Error> {
56        self.interner.intern_formatted_str(value, upper_size)
57    }
58
59    fn intern_str(&self, text: &str) -> Self::InternedStr<'_> {
60        self.interner.intern(text)
61    }
62}
63
64#[derive(Debug, Clone)]
65pub struct Config {
66    pub load: ConfigLoad,
67}
68
69impl Default for Config {
70    fn default() -> Self {
71        Self {
72            load: ConfigLoad::all(),
73        }
74    }
75}
76
77bitflags::bitflags! {
78    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
79    pub struct ConfigLoad: u32 {
80        const CORE = 1 << 0;
81        const STD = 1 << 1;
82    }
83}
84
85/// Symbols may live across task boundary.
86///
87/// Therefore, symbols in this type can be added within one task and consumed within another task.
88#[derive(Debug, Default)]
89pub(crate) struct LastingSymbols<'gcx> {
90    pub(crate) infer_type_symbols: SymbolTable<Interned<'gcx, str>, infer::Type<'gcx>>,
91    pub(crate) eval_value_symbols: SymbolTable<Interned<'gcx, str>, eval::Value<'gcx>>,
92}