use crate::{
language::{parsed::Declaration, Visibility},
semantic_analysis::Namespace,
Engines,
};
use sway_error::handler::{ErrorEmitted, Handler};
use sway_types::{span::Span, Ident};
#[derive(Clone)]
pub struct SymbolCollectionContext {
pub(crate) namespace: Namespace,
}
impl SymbolCollectionContext {
pub fn new(namespace: Namespace) -> Self {
Self { namespace }
}
pub fn scoped<T>(
mut self,
engines: &Engines,
with_scoped_ctx: impl FnOnce(SymbolCollectionContext) -> Result<T, ErrorEmitted>,
) -> Result<T, ErrorEmitted> {
self.namespace
.module_mut(engines)
.write(engines, |m| m.push_new_lexical_scope());
let ret = with_scoped_ctx(self.clone());
self.namespace
.module_mut(engines)
.write(engines, |m| m.pop_lexical_scope());
ret
}
pub fn enter_submodule<T>(
&mut self,
engines: &Engines,
mod_name: Ident,
visibility: Visibility,
module_span: Span,
with_submod_ctx: impl FnOnce(&mut SymbolCollectionContext) -> T,
) -> T {
self.namespace
.push_new_submodule(engines, mod_name, visibility, module_span);
let ret = with_submod_ctx(self);
self.namespace.pop_submodule();
ret
}
pub(crate) fn insert_parsed_symbol(
&mut self,
_handler: &Handler,
engines: &Engines,
name: Ident,
item: Declaration,
) -> Result<(), ErrorEmitted> {
self.namespace.module_mut(engines).write(engines, |m| {
m.current_items_mut()
.insert_parsed_symbol(name.clone(), item.clone())
})
}
}