use rustc_hash::FxHashMap;
use slotmap::{DefaultKey, SlotMap};
use sway_features::ExperimentalFeatures;
use sway_types::SourceEngine;
use crate::{
block::BlockContent,
function::FunctionContent,
local_var::LocalVarContent,
metadata::Metadatum,
module::{Kind, ModuleContent, ModuleIterator},
value::ValueContent,
Type, TypeContent,
};
pub struct Context<'eng> {
pub source_engine: &'eng SourceEngine,
pub(crate) modules: SlotMap<DefaultKey, ModuleContent>,
pub(crate) functions: SlotMap<DefaultKey, FunctionContent>,
pub(crate) blocks: SlotMap<DefaultKey, BlockContent>,
pub(crate) values: SlotMap<DefaultKey, ValueContent>,
pub(crate) local_vars: SlotMap<DefaultKey, LocalVarContent>,
pub(crate) types: SlotMap<DefaultKey, TypeContent>,
pub(crate) type_map: FxHashMap<TypeContent, Type>,
pub(crate) metadata: SlotMap<DefaultKey, Metadatum>,
pub program_kind: Kind,
next_unique_sym_tag: u64,
pub experimental: ExperimentalFeatures,
}
impl<'eng> Context<'eng> {
pub fn new(source_engine: &'eng SourceEngine, experimental: ExperimentalFeatures) -> Self {
let mut def = Self {
source_engine,
modules: Default::default(),
functions: Default::default(),
blocks: Default::default(),
values: Default::default(),
local_vars: Default::default(),
types: Default::default(),
type_map: Default::default(),
metadata: Default::default(),
next_unique_sym_tag: Default::default(),
program_kind: Kind::Contract,
experimental,
};
Type::create_basic_types(&mut def);
def
}
pub fn source_engine(&self) -> &'eng SourceEngine {
self.source_engine
}
pub fn module_iter(&self) -> ModuleIterator {
ModuleIterator::new(self)
}
pub fn get_unique_name(&mut self) -> String {
format!("anon_{}", self.get_unique_id())
}
pub fn get_unique_id(&mut self) -> u64 {
let sym = self.next_unique_sym_tag;
self.next_unique_sym_tag += 1;
sym
}
}
use std::fmt::{Display, Error, Formatter};
impl Display for Context<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
write!(f, "{}", crate::printer::to_string(self))
}
}
impl From<Context<'_>> for String {
fn from(context: Context) -> Self {
crate::printer::to_string(&context)
}
}