use crate::{
language::{
parsed::ParseProgram,
ty::{self, TyProgram},
},
metadata::MetadataManager,
semantic_analysis::{
namespace::{self, Namespace},
TypeCheckContext,
},
BuildConfig, Engines,
};
use sway_error::handler::{ErrorEmitted, Handler};
use sway_ir::{Context, Module};
use super::{
TypeCheckAnalysis, TypeCheckAnalysisContext, TypeCheckFinalization,
TypeCheckFinalizationContext,
};
impl TyProgram {
pub fn type_check(
handler: &Handler,
engines: &Engines,
parsed: &ParseProgram,
initial_namespace: namespace::Module,
package_name: &str,
build_config: Option<&BuildConfig>,
) -> Result<Self, ErrorEmitted> {
let mut namespace = Namespace::init_root(initial_namespace);
let ctx = TypeCheckContext::from_root(&mut namespace, engines)
.with_kind(parsed.kind.clone())
.with_experimental_flags(build_config.map(|x| x.experimental));
let ParseProgram { root, kind } = parsed;
let modules_dep_graph = ty::TyModule::analyze(handler, root)?;
let module_eval_order = modules_dep_graph.compute_order(handler)?;
ty::TyModule::type_check(handler, ctx, root, module_eval_order).and_then(|root| {
let res = Self::validate_root(handler, engines, &root, kind.clone(), package_name);
res.map(|(kind, declarations, configurables)| Self {
kind,
root,
declarations,
configurables,
storage_slots: vec![],
logged_types: vec![],
messages_types: vec![],
})
})
}
pub(crate) fn get_typed_program_with_initialized_storage_slots(
self,
handler: &Handler,
engines: &Engines,
context: &mut Context,
md_mgr: &mut MetadataManager,
module: Module,
) -> Result<Self, ErrorEmitted> {
let decl_engine = engines.de();
match &self.kind {
ty::TyProgramKind::Contract { .. } => {
let storage_decl = self
.declarations
.iter()
.find(|decl| matches!(decl, ty::TyDecl::StorageDecl { .. }));
match storage_decl {
Some(ty::TyDecl::StorageDecl(ty::StorageDecl {
decl_id,
decl_span: _,
..
})) => {
let decl = decl_engine.get_storage(decl_id);
let mut storage_slots = decl.get_initialized_storage_slots(
handler, engines, context, md_mgr, module,
)?;
storage_slots.sort();
Ok(Self {
storage_slots,
..self
})
}
_ => Ok(Self {
storage_slots: vec![],
..self
}),
}
}
_ => Ok(Self {
storage_slots: vec![],
..self
}),
}
}
}
impl TypeCheckAnalysis for TyProgram {
fn type_check_analyze(
&self,
handler: &Handler,
ctx: &mut TypeCheckAnalysisContext,
) -> Result<(), ErrorEmitted> {
for node in self.root.all_nodes.iter() {
node.type_check_analyze(handler, ctx)?;
}
Ok(())
}
}
impl TypeCheckFinalization for TyProgram {
fn type_check_finalize(
&mut self,
handler: &Handler,
ctx: &mut TypeCheckFinalizationContext,
) -> Result<(), ErrorEmitted> {
handler.scope(|handler| {
for node in self.root.all_nodes.iter_mut() {
let _ = node.type_check_finalize(handler, ctx);
}
Ok(())
})
}
}