From fadd54f14d9e1ff082b405d572b8276fb98fcbd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Matos?= Date: Mon, 13 Nov 2023 17:49:56 +0000 Subject: [PATCH] Remove unused monomorphization code. (#5266) ## Description This removes monomorphization code that was added previously by Emily, but never got finished and is thus unused currently. ## Checklist - [ ] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [ ] I have requested a review from the relevant team or maintainers. --- sway-core/src/decl_engine/template.rs | 1 + sway-core/src/lib.rs | 1 - sway-core/src/monomorphize/constraint.rs | 170 ---------------- .../src/monomorphize/gather/code_block.rs | 15 -- sway-core/src/monomorphize/gather/context.rs | 121 ----------- .../src/monomorphize/gather/declaration.rs | 64 ------ .../src/monomorphize/gather/expression.rs | 98 --------- sway-core/src/monomorphize/gather/mod.rs | 30 --- sway-core/src/monomorphize/gather/module.rs | 33 --- sway-core/src/monomorphize/gather/node.rs | 17 -- .../src/monomorphize/instruct/code_block.rs | 15 -- .../src/monomorphize/instruct/context.rs | 124 ----------- .../src/monomorphize/instruct/context2.rs | 131 ------------ .../src/monomorphize/instruct/declaration.rs | 55 ----- .../src/monomorphize/instruct/expression.rs | 98 --------- sway-core/src/monomorphize/instruct/mod.rs | 30 --- sway-core/src/monomorphize/instruct/module.rs | 31 --- sway-core/src/monomorphize/instruct/node.rs | 17 -- sway-core/src/monomorphize/instructions.rs | 72 ------- sway-core/src/monomorphize/mod.rs | 32 --- sway-core/src/monomorphize/priv_prelude.rs | 30 --- .../monomorphize/solve/instruction_result.rs | 37 ---- .../monomorphize/solve/iteration_report.rs | 6 - sway-core/src/monomorphize/solve/mod.rs | 15 -- sway-core/src/monomorphize/solve/solver.rs | 192 ------------------ 25 files changed, 1 insertion(+), 1434 deletions(-) delete mode 100644 sway-core/src/monomorphize/constraint.rs delete mode 100644 sway-core/src/monomorphize/gather/code_block.rs delete mode 100644 sway-core/src/monomorphize/gather/context.rs delete mode 100644 sway-core/src/monomorphize/gather/declaration.rs delete mode 100644 sway-core/src/monomorphize/gather/expression.rs delete mode 100644 sway-core/src/monomorphize/gather/mod.rs delete mode 100644 sway-core/src/monomorphize/gather/module.rs delete mode 100644 sway-core/src/monomorphize/gather/node.rs delete mode 100644 sway-core/src/monomorphize/instruct/code_block.rs delete mode 100644 sway-core/src/monomorphize/instruct/context.rs delete mode 100644 sway-core/src/monomorphize/instruct/context2.rs delete mode 100644 sway-core/src/monomorphize/instruct/declaration.rs delete mode 100644 sway-core/src/monomorphize/instruct/expression.rs delete mode 100644 sway-core/src/monomorphize/instruct/mod.rs delete mode 100644 sway-core/src/monomorphize/instruct/module.rs delete mode 100644 sway-core/src/monomorphize/instruct/node.rs delete mode 100644 sway-core/src/monomorphize/instructions.rs delete mode 100644 sway-core/src/monomorphize/mod.rs delete mode 100644 sway-core/src/monomorphize/priv_prelude.rs delete mode 100644 sway-core/src/monomorphize/solve/instruction_result.rs delete mode 100644 sway-core/src/monomorphize/solve/iteration_report.rs delete mode 100644 sway-core/src/monomorphize/solve/mod.rs delete mode 100644 sway-core/src/monomorphize/solve/solver.rs diff --git a/sway-core/src/decl_engine/template.rs b/sway-core/src/decl_engine/template.rs index 892bfd0c5e9..c4778b1f75c 100644 --- a/sway-core/src/decl_engine/template.rs +++ b/sway-core/src/decl_engine/template.rs @@ -20,6 +20,7 @@ where Template(value) } + #[allow(dead_code)] pub(crate) fn inner(&self) -> &T { &self.0 } diff --git a/sway-core/src/lib.rs b/sway-core/src/lib.rs index 63dd0fa655b..64d1288b3b6 100644 --- a/sway-core/src/lib.rs +++ b/sway-core/src/lib.rs @@ -15,7 +15,6 @@ pub mod decl_engine; pub mod ir_generation; pub mod language; mod metadata; -mod monomorphize; pub mod query_engine; pub mod semantic_analysis; pub mod source_map; diff --git a/sway-core/src/monomorphize/constraint.rs b/sway-core/src/monomorphize/constraint.rs deleted file mode 100644 index 5fdaa692b32..00000000000 --- a/sway-core/src/monomorphize/constraint.rs +++ /dev/null @@ -1,170 +0,0 @@ -use std::{ - cmp::Ordering, - hash::{Hash, Hasher}, -}; - -use sway_types::Ident; - -use crate::{ - decl_engine::DeclId, - engine_threading::*, - language::{ty, CallPath}, - type_system::*, -}; - -#[derive(Debug, Clone)] -pub(crate) enum Constraint { - /// Type use. - Ty(TypeId), - /// Function call. - FnCall { - call_path: CallPath, - decl_id: DeclId, - subst_list: SubstList, - arguments: Vec, - }, -} - -impl Constraint { - fn discriminant_value(&self) -> u8 { - match self { - Constraint::Ty(_) => 0, - Constraint::FnCall { .. } => 1, - } - } -} - -impl From<&TypeId> for Constraint { - fn from(value: &TypeId) -> Self { - Constraint::Ty(*value) - } -} - -impl From for Constraint { - fn from(value: TypeId) -> Self { - Constraint::Ty(value) - } -} - -impl From<&ty::TyExpressionVariant> for Constraint { - fn from(value: &ty::TyExpressionVariant) -> Self { - match value { - ty::TyExpressionVariant::FunctionApplication { - call_path, - arguments, - fn_ref, - .. - } => Constraint::FnCall { - call_path: call_path.clone(), - decl_id: *fn_ref.id(), - subst_list: fn_ref.subst_list().clone(), - arguments: args_helper(arguments), - }, - _ => unimplemented!(), - } - } -} - -fn args_helper(args: &[(Ident, ty::TyExpression)]) -> Vec { - args.iter().map(|(_, exp)| exp.return_type).collect() -} - -impl EqWithEngines for Constraint {} -impl PartialEqWithEngines for Constraint { - fn eq(&self, other: &Self, engines: &Engines) -> bool { - let type_engine = engines.te(); - match (self, other) { - (Constraint::Ty(l), Constraint::Ty(r)) => { - type_engine.get(*l).eq(&type_engine.get(*r), engines) - } - ( - Constraint::FnCall { - call_path: lcp, - decl_id: ldi, - subst_list: lsl, - arguments: la, - }, - Constraint::FnCall { - call_path: rcp, - decl_id: rdi, - subst_list: rsl, - arguments: ra, - }, - ) => { - lcp == rcp - && ldi == rdi - && lsl.eq(rsl, engines) - && la.len() == ra.len() - && la - .iter() - .zip(ra.iter()) - .map(|(l, r)| type_engine.get(*l).eq(&type_engine.get(*r), engines)) - .all(|b| b) - } - _ => false, - } - } -} - -impl HashWithEngines for Constraint { - fn hash(&self, state: &mut H, engines: &Engines) { - let type_engine = engines.te(); - match self { - Constraint::Ty(type_id) => { - state.write_u8(self.discriminant_value()); - type_engine.get(*type_id).hash(state, engines); - } - Constraint::FnCall { - call_path, - decl_id, - subst_list, - arguments, - } => { - state.write_u8(self.discriminant_value()); - call_path.hash(state); - decl_id.hash(state); - subst_list.hash(state, engines); - arguments - .iter() - .for_each(|arg| type_engine.get(*arg).hash(state, engines)); - } - } - } -} - -impl OrdWithEngines for Constraint { - fn cmp(&self, other: &Self, engines: &Engines) -> Ordering { - let type_engine = engines.te(); - match (self, other) { - (Constraint::Ty(l), Constraint::Ty(r)) => { - type_engine.get(*l).cmp(&type_engine.get(*r), engines) - } - ( - Constraint::FnCall { - call_path: lcp, - decl_id: ldi, - subst_list: lsl, - arguments: la, - }, - Constraint::FnCall { - call_path: rcp, - decl_id: rdi, - subst_list: rsl, - arguments: ra, - }, - ) => lcp - .cmp(rcp) - .then_with(|| ldi.cmp(rdi)) - .then_with(|| lsl.cmp(rsl, engines)) - .then_with(|| la.len().cmp(&ra.len())) - .then_with(|| { - la.iter() - .zip(ra.iter()) - .fold(Ordering::Equal, |acc, (l, r)| { - acc.then_with(|| type_engine.get(*l).cmp(&type_engine.get(*r), engines)) - }) - }), - (l, r) => l.discriminant_value().cmp(&r.discriminant_value()), - } - } -} diff --git a/sway-core/src/monomorphize/gather/code_block.rs b/sway-core/src/monomorphize/gather/code_block.rs deleted file mode 100644 index 262347d128c..00000000000 --- a/sway-core/src/monomorphize/gather/code_block.rs +++ /dev/null @@ -1,15 +0,0 @@ -use sway_error::handler::{ErrorEmitted, Handler}; - -use crate::{language::ty, monomorphize::priv_prelude::*}; - -pub(crate) fn gather_from_code_block( - mut ctx: GatherContext, - handler: &Handler, - body: &ty::TyCodeBlock, -) -> Result<(), ErrorEmitted> { - body.contents - .iter() - .try_for_each(|node| gather_from_node(ctx.by_ref(), handler, &node.content))?; - - Ok(()) -} diff --git a/sway-core/src/monomorphize/gather/context.rs b/sway-core/src/monomorphize/gather/context.rs deleted file mode 100644 index cfcc0da60f3..00000000000 --- a/sway-core/src/monomorphize/gather/context.rs +++ /dev/null @@ -1,121 +0,0 @@ -use hashbrown::{hash_map::RawEntryMut, HashMap}; -use std::sync::RwLock; - -use crate::{engine_threading::*, monomorphize::priv_prelude::*, namespace}; - -/// Contextual state tracked and accumulated throughout gathering the trait -/// constraints. -pub(crate) struct GatherContext<'a> { - /// The namespace context accumulated throughout type-checking. - pub(crate) namespace: &'a GatherNamespace<'a>, - - pub(crate) engines: &'a Engines, - - /// The list of constraints. - /// NOTE: This only needs to be a [HashSet][hashbrown::HashSet], but there - /// isn't the right method implement on that data type for what we need, so - /// instead we use a dummy [HashMap][hashbrown::HashMap]. - constraints: &'a RwLock>, -} - -impl<'a> GatherContext<'a> { - /// Initialize a context at the top-level of a module with its namespace. - pub(crate) fn from_root( - root_namespace: &'a GatherNamespace<'a>, - engines: &'a Engines, - constraints: &'a RwLock>, - ) -> GatherContext<'a> { - Self::from_module_namespace(root_namespace, engines, constraints) - } - - fn from_module_namespace( - namespace: &'a GatherNamespace<'a>, - engines: &'a Engines, - constraints: &'a RwLock>, - ) -> Self { - Self { - namespace, - engines, - constraints, - } - } - - /// Create a new context that mutably borrows the inner [Namespace] with a - /// lifetime bound by `self`. - pub(crate) fn by_ref(&mut self) -> GatherContext<'_> { - GatherContext { - namespace: self.namespace, - engines: self.engines, - constraints: self.constraints, - } - } - - /// Scope the [Context] with the given [Namespace]. - pub(crate) fn scoped(self, namespace: &'a GatherNamespace<'a>) -> GatherContext<'a> { - GatherContext { - namespace, - engines: self.engines, - constraints: self.constraints, - } - } - - pub(crate) fn add_constraint(&self, constraint: Constraint) { - let mut constraints = self.constraints.write().unwrap(); - let hash_builder = constraints.hasher().clone(); - let constraint_hash = make_hasher(&hash_builder, self.engines)(&constraint); - let raw_entry = constraints - .raw_entry_mut() - .from_hash(constraint_hash, |x| x.eq(&constraint, self.engines)); - if let RawEntryMut::Vacant(v) = raw_entry { - v.insert_with_hasher( - constraint_hash, - constraint, - 0, - make_hasher(&hash_builder, self.engines), - ); - } - } -} - -/// The set of items that represent the namespace context passed throughout -/// gathering the trait constraints. -#[derive(Debug)] -pub(crate) struct GatherNamespace<'a> { - /// The `root` of the project namespace. - pub(crate) root: &'a namespace::Module, - - /// An absolute path from the `root` that represents the current module - /// being gathered. - pub(crate) mod_path: PathBuf, -} - -impl<'a> GatherNamespace<'a> { - /// Initialize the namespace at its root from the given initial namespace. - pub(crate) fn init_root(root: &'a namespace::Module) -> GatherNamespace<'a> { - let mod_path = vec![]; - Self { root, mod_path } - } - - pub(crate) fn new_with_module(&self, module: &namespace::Module) -> GatherNamespace<'_> { - let mut mod_path = self.mod_path.clone(); - if let Some(name) = &module.name { - mod_path.push(name.clone()); - } - GatherNamespace { - root: self.root, - mod_path, - } - } - - /// Access to the current [Module], i.e. the module at the inner `mod_path`. - pub(crate) fn module(&self) -> &namespace::Module { - &self.root[&self.mod_path] - } -} - -impl<'a> std::ops::Deref for GatherNamespace<'a> { - type Target = namespace::Module; - fn deref(&self) -> &Self::Target { - self.module() - } -} diff --git a/sway-core/src/monomorphize/gather/declaration.rs b/sway-core/src/monomorphize/gather/declaration.rs deleted file mode 100644 index a8a51e93977..00000000000 --- a/sway-core/src/monomorphize/gather/declaration.rs +++ /dev/null @@ -1,64 +0,0 @@ -use sway_error::handler::{ErrorEmitted, Handler}; - -use crate::{decl_engine::DeclId, language::ty, monomorphize::priv_prelude::*, SubstList}; - -pub(crate) fn gather_from_decl( - ctx: GatherContext, - handler: &Handler, - decl: &ty::TyDecl, -) -> Result<(), ErrorEmitted> { - match decl { - ty::TyDecl::VariableDecl(decl) => { - gather_from_exp(ctx, handler, &decl.body)?; - } - ty::TyDecl::ConstantDecl(_) => todo!(), - ty::TyDecl::FunctionDecl(ty::FunctionDecl { - decl_id, - subst_list, - .. - }) => { - gather_from_fn_decl(ctx, handler, decl_id, subst_list.inner())?; - } - ty::TyDecl::TraitDecl(_) => todo!(), - ty::TyDecl::StructDecl(_) => todo!(), - ty::TyDecl::EnumDecl(_) => todo!(), - ty::TyDecl::EnumVariantDecl(_) => todo!(), - ty::TyDecl::ImplTrait(_) => todo!(), - ty::TyDecl::AbiDecl(_) => todo!(), - ty::TyDecl::GenericTypeForFunctionScope(_) => todo!(), - ty::TyDecl::StorageDecl(_) => todo!(), - ty::TyDecl::ErrorRecovery(_, _) => {} - ty::TyDecl::TypeAliasDecl(_) => todo!(), - ty::TyDecl::TraitTypeDecl(_) => todo!(), - } - - Ok(()) -} - -fn gather_from_fn_decl( - mut ctx: GatherContext, - handler: &Handler, - decl_id: &DeclId, - subst_list: &SubstList, -) -> Result<(), ErrorEmitted> { - let decl = ctx.engines.de().get_function(decl_id); - - if !subst_list.is_empty() { - unimplemented!("{}", decl.name); - } - - let ty::TyFunctionDecl { - body, - parameters, - return_type, - .. - } = decl; - - parameters.iter().for_each(|param| { - ctx.add_constraint(param.type_argument.type_id.into()); - }); - ctx.add_constraint(return_type.type_id.into()); - gather_from_code_block(ctx.by_ref(), handler, &body)?; - - Ok(()) -} diff --git a/sway-core/src/monomorphize/gather/expression.rs b/sway-core/src/monomorphize/gather/expression.rs deleted file mode 100644 index 3076149e764..00000000000 --- a/sway-core/src/monomorphize/gather/expression.rs +++ /dev/null @@ -1,98 +0,0 @@ -use sway_error::handler::{ErrorEmitted, Handler}; - -use crate::{language::ty, monomorphize::priv_prelude::*, type_system::*}; - -pub(crate) fn gather_from_exp( - ctx: GatherContext, - handler: &Handler, - exp: &ty::TyExpression, -) -> Result<(), ErrorEmitted> { - gather_from_exp_inner(ctx, handler, &exp.expression, exp.return_type) -} - -pub(crate) fn gather_from_exp_inner( - mut ctx: GatherContext, - handler: &Handler, - exp: &ty::TyExpressionVariant, - return_type: TypeId, -) -> Result<(), ErrorEmitted> { - ctx.add_constraint(return_type.into()); - match exp { - ty::TyExpressionVariant::FunctionApplication { - arguments, - contract_call_params, - .. - } => { - arguments - .iter() - .try_for_each(|(_, arg)| gather_from_exp(ctx.by_ref(), handler, arg))?; - contract_call_params - .iter() - .try_for_each(|(_, arg)| gather_from_exp(ctx.by_ref(), handler, arg))?; - ctx.add_constraint(exp.into()); - } - ty::TyExpressionVariant::LazyOperator { lhs, rhs, .. } => { - gather_from_exp(ctx.by_ref(), handler, lhs)?; - gather_from_exp(ctx.by_ref(), handler, rhs)?; - } - ty::TyExpressionVariant::ConstantExpression { .. } => { - // NOTE: may need to do something here later - } - ty::TyExpressionVariant::VariableExpression { .. } => { - // NOTE: may need to do something here later - } - ty::TyExpressionVariant::Tuple { fields } => { - fields - .iter() - .try_for_each(|field| gather_from_exp(ctx.by_ref(), handler, field))?; - } - ty::TyExpressionVariant::Array { - contents: _, - elem_type: _, - } => { - todo!(); - // contents - // .iter() - // .try_for_each(|elem| gather_from_exp(ctx.by_ref(), handler, elem))?; - } - ty::TyExpressionVariant::ArrayIndex { - prefix: _, - index: _, - } => { - todo!(); - // gather_from_exp(ctx.by_ref(), handler, prefix)?; - // gather_from_exp(ctx.by_ref(), handler, index)?; - } - ty::TyExpressionVariant::StructExpression { .. } => todo!(), - ty::TyExpressionVariant::CodeBlock(block) => { - gather_from_code_block(ctx, handler, block)?; - } - ty::TyExpressionVariant::IfExp { .. } => todo!(), - ty::TyExpressionVariant::MatchExp { .. } => todo!(), - ty::TyExpressionVariant::AsmExpression { .. } => todo!(), - ty::TyExpressionVariant::StructFieldAccess { .. } => todo!(), - ty::TyExpressionVariant::TupleElemAccess { prefix, .. } => { - gather_from_exp(ctx, handler, prefix)?; - } - ty::TyExpressionVariant::EnumInstantiation { .. } => todo!(), - ty::TyExpressionVariant::AbiCast { .. } => todo!(), - ty::TyExpressionVariant::StorageAccess(_) => todo!(), - ty::TyExpressionVariant::IntrinsicFunction(_) => todo!(), - ty::TyExpressionVariant::AbiName(_) => todo!(), - ty::TyExpressionVariant::EnumTag { exp } => { - gather_from_exp(ctx.by_ref(), handler, exp)?; - } - ty::TyExpressionVariant::UnsafeDowncast { .. } => todo!(), - ty::TyExpressionVariant::WhileLoop { .. } => todo!(), - ty::TyExpressionVariant::Reassignment(_) => todo!(), - ty::TyExpressionVariant::Return(exp) => { - gather_from_exp(ctx.by_ref(), handler, exp)?; - } - ty::TyExpressionVariant::Literal(_) => {} - ty::TyExpressionVariant::Break => {} - ty::TyExpressionVariant::Continue => {} - ty::TyExpressionVariant::FunctionParameter => {} - } - - Ok(()) -} diff --git a/sway-core/src/monomorphize/gather/mod.rs b/sway-core/src/monomorphize/gather/mod.rs deleted file mode 100644 index 76c84032204..00000000000 --- a/sway-core/src/monomorphize/gather/mod.rs +++ /dev/null @@ -1,30 +0,0 @@ -//! This module gathers a list of [Constraint]s from a typed AST. - -pub(crate) mod code_block; -pub(crate) mod context; -pub(crate) mod declaration; -pub(crate) mod expression; -pub(crate) mod module; -pub(crate) mod node; - -use std::sync::RwLock; - -use hashbrown::HashMap; -use sway_error::handler::{ErrorEmitted, Handler}; - -use crate::{language::ty, monomorphize::priv_prelude::*, Engines}; - -/// Gathers [Constraint]s from a typed AST. -pub(super) fn gather_constraints( - engines: &Engines, - handler: &Handler, - module: &ty::TyModule, -) -> Result, ErrorEmitted> { - let root_namespace = GatherNamespace::init_root(&module.namespace); - let constraints = RwLock::new(HashMap::new()); - let ctx = GatherContext::from_root(&root_namespace, engines, &constraints); - - gather_from_root(ctx, handler, module)?; - - Ok(constraints.into_inner().unwrap().into_keys()) -} diff --git a/sway-core/src/monomorphize/gather/module.rs b/sway-core/src/monomorphize/gather/module.rs deleted file mode 100644 index 8572e97b528..00000000000 --- a/sway-core/src/monomorphize/gather/module.rs +++ /dev/null @@ -1,33 +0,0 @@ -use sway_error::handler::{ErrorEmitted, Handler}; - -use crate::{language::ty, monomorphize::priv_prelude::*}; - -pub(crate) fn gather_from_root( - mut ctx: GatherContext, - handler: &Handler, - module: &ty::TyModule, -) -> Result<(), ErrorEmitted> { - for (_, submod) in module.submodules_recursive() { - gather_from_module(ctx.by_ref(), handler, &submod.module)?; - } - for node in module.all_nodes.iter() { - gather_from_node(ctx.by_ref(), handler, &node.content)?; - } - Ok(()) -} - -pub(crate) fn gather_from_module( - ctx: GatherContext, - handler: &Handler, - module: &ty::TyModule, -) -> Result<(), ErrorEmitted> { - let module_namespace = ctx.namespace.new_with_module(&module.namespace); - let mut ctx = ctx.scoped(&module_namespace); - for (_, submod) in module.submodules_recursive() { - gather_from_module(ctx.by_ref(), handler, &submod.module)?; - } - for node in module.all_nodes.iter() { - gather_from_node(ctx.by_ref(), handler, &node.content)?; - } - Ok(()) -} diff --git a/sway-core/src/monomorphize/gather/node.rs b/sway-core/src/monomorphize/gather/node.rs deleted file mode 100644 index 530a4adf21f..00000000000 --- a/sway-core/src/monomorphize/gather/node.rs +++ /dev/null @@ -1,17 +0,0 @@ -use sway_error::handler::{ErrorEmitted, Handler}; - -use crate::{language::ty, monomorphize::priv_prelude::*}; - -pub(crate) fn gather_from_node( - ctx: GatherContext, - handler: &Handler, - node: &ty::TyAstNodeContent, -) -> Result<(), ErrorEmitted> { - match node { - ty::TyAstNodeContent::Declaration(decl) => gather_from_decl(ctx, handler, decl), - ty::TyAstNodeContent::Expression(exp) => gather_from_exp(ctx, handler, exp), - ty::TyAstNodeContent::ImplicitReturnExpression(exp) => gather_from_exp(ctx, handler, exp), - ty::TyAstNodeContent::SideEffect(_) => Ok(()), - ty::TyAstNodeContent::Error(_, _) => Ok(()), - } -} diff --git a/sway-core/src/monomorphize/instruct/code_block.rs b/sway-core/src/monomorphize/instruct/code_block.rs deleted file mode 100644 index 53a1b1230c9..00000000000 --- a/sway-core/src/monomorphize/instruct/code_block.rs +++ /dev/null @@ -1,15 +0,0 @@ -use sway_error::handler::{ErrorEmitted, Handler}; - -use crate::{language::ty, monomorphize::priv_prelude::*}; - -pub(crate) fn instruct_code_block( - mut ctx: InstructContext, - handler: &Handler, - body: &ty::TyCodeBlock, -) -> Result<(), ErrorEmitted> { - body.contents - .iter() - .try_for_each(|node| instruct_node(ctx.by_ref(), handler, &node.content))?; - - Ok(()) -} diff --git a/sway-core/src/monomorphize/instruct/context.rs b/sway-core/src/monomorphize/instruct/context.rs deleted file mode 100644 index cca6a464214..00000000000 --- a/sway-core/src/monomorphize/instruct/context.rs +++ /dev/null @@ -1,124 +0,0 @@ -use std::sync::RwLock; - -use hashbrown::HashMap; - -use crate::{decl_engine::*, language::ty, monomorphize::priv_prelude::*, Engines}; - -/// Contextual state tracked and accumulated throughout applying the -/// monomorphization instructions. -pub(crate) struct InstructContext<'a> { - pub(crate) engines: &'a Engines, - /// All of the instructions, sorted. - instructions: &'a RwLock, -} - -impl<'a> InstructContext<'a> { - /// Initialize a context at the top-level of a module with its namespace. - pub(crate) fn from_root( - engines: &'a Engines, - instructions: &'a RwLock, - ) -> Self { - Self::from_module_namespace(engines, instructions) - } - - fn from_module_namespace( - engines: &'a Engines, - instructions: &'a RwLock, - ) -> Self { - Self { - engines, - instructions, - } - } - - /// Create a new context that mutably borrows the inner [Namespace] with a - /// lifetime bound by `self`. - pub(crate) fn by_ref(&mut self) -> InstructContext<'_> { - InstructContext { - engines: self.engines, - instructions: self.instructions, - } - } - - /// Scope the [InstructContext] with the given [Namespace]. - pub(crate) fn scoped(self) -> InstructContext<'a> { - InstructContext { - engines: self.engines, - instructions: self.instructions, - } - } -} - -type FnMap = HashMap, Vec>; -type TraitMap = HashMap, Vec>; -type ImplTraitMap = HashMap, Vec>; -type StructMap = HashMap, Vec>; -type EnumMap = HashMap, Vec>; - -pub(crate) struct InstructionItems { - /// A map of [TyFunctionDecl](ty::TyFunctionDecl) [DeclId]s to be - /// monomorphized. - fn_map: FnMap, - - /// A map of [TyTraitDecl](ty::TyTraitDecl) [DeclId]s to be monomorphized. - trait_map: TraitMap, - - /// A map of [TyImplTrait](ty::TyImplTrait) [DeclId]s to be monomorphized. - impl_trait_map: ImplTraitMap, - - /// A map of [TyStructDecl](ty::TyStructDecl) [DeclId]s to be monomorphized. - struct_map: StructMap, - - /// A map of [TyEnumDecl](ty::TyEnumDecl) [DeclId]s to be monomorphized. - enum_map: EnumMap, - - /// The list of instructions not included in any of the previous fields. - instructions: Vec, -} - -impl InstructionItems { - pub(crate) fn new(instructions: Vec) -> InstructionItems { - let mut fn_map: FnMap = HashMap::new(); - let mut trait_map: TraitMap = HashMap::new(); - let mut impl_trait_map: ImplTraitMap = HashMap::new(); - let mut struct_map: StructMap = HashMap::new(); - let mut enum_map: EnumMap = HashMap::new(); - let mut leftovers = vec![]; - for instruction in instructions.into_iter() { - match &instruction { - Instruction::FnDecl(decl_id, _) => { - let v = fn_map.entry(*decl_id).or_default(); - v.push(instruction); - } - Instruction::TraitDecl(decl_id, _) => { - let v = trait_map.entry(*decl_id).or_default(); - v.push(instruction); - } - Instruction::ImplTrait(decl_id, _) => { - let v = impl_trait_map.entry(*decl_id).or_default(); - v.push(instruction); - } - Instruction::StructDecl(decl_id, _) => { - let v = struct_map.entry(*decl_id).or_default(); - v.push(instruction); - } - Instruction::EnumDecl(decl_id, _) => { - let v = enum_map.entry(*decl_id).or_default(); - v.push(instruction); - } - _ => { - leftovers.push(instruction); - } - } - } - - InstructionItems { - fn_map, - trait_map, - impl_trait_map, - struct_map, - enum_map, - instructions: leftovers, - } - } -} diff --git a/sway-core/src/monomorphize/instruct/context2.rs b/sway-core/src/monomorphize/instruct/context2.rs deleted file mode 100644 index f05e4296501..00000000000 --- a/sway-core/src/monomorphize/instruct/context2.rs +++ /dev/null @@ -1,131 +0,0 @@ -use crate::{decl_engine::*, monomorphize::priv_prelude::*, namespace, Engines, TypeEngine}; - -/// Contextual state tracked and accumulated throughout applying the -/// monomorphization instructions. -pub(crate) struct InstructContext<'a, 'b: 'a> { - /// The namespace context accumulated throughout applying the - /// monomorphization instructions. - pub(crate) namespace: &'a mut InstructNamespace<'b>, - - /// The type engine storing types. - pub(crate) type_engine: &'a TypeEngine, - - /// The declaration engine holds declarations. - pub(crate) decl_engine: &'a DeclEngine, - - /// The list of instructions. - instructions: &'a [Instruction], -} - -impl<'a, 'b> InstructContext<'a, 'b> { - /// Initialize a context at the top-level of a module with its namespace. - pub(crate) fn from_root( - root_namespace: &'a mut InstructNamespace<'b>, - engines: Engines<'a>, - instructions: &'a [Instruction], - ) -> Self { - Self::from_module_namespace(root_namespace, engines, instructions) - } - - fn from_module_namespace( - namespace: &'a mut InstructNamespace<'b>, - engines: Engines<'a>, - instructions: &'a [Instruction], - ) -> Self { - let (type_engine, decl_engine) = engines.unwrap(); - Self { - namespace, - type_engine, - decl_engine, - instructions, - } - } - - /// Create a new context that mutably borrows the inner [Namespace] with a - /// lifetime bound by `self`. - pub(crate) fn by_ref(&mut self) -> InstructContext<'_, 'b> { - InstructContext { - namespace: self.namespace, - type_engine: self.type_engine, - decl_engine: self.decl_engine, - instructions: self.instructions, - } - } - - /// Scope the [InstructContext] with the given [Namespace]. - pub(crate) fn scoped( - self, - namespace: &'a mut InstructNamespace<'b>, - ) -> InstructContext<'a, 'b> { - InstructContext { - namespace, - type_engine: self.type_engine, - decl_engine: self.decl_engine, - instructions: self.instructions, - } - } -} - -/// The set of items that represent the namespace context passed throughout -/// applying the monomorphization instructions. -pub(crate) struct InstructNamespace<'a> { - /// An absolute path from the `root` that represents the current module - /// for which we are applying instructions. - pub(crate) mod_path: PathBuf, - - /// The `root` of the project namespace. - pub(crate) root: &'a mut namespace::Module, -} - -impl<'a> InstructNamespace<'a> { - /// Initialize the namespace at its root from the given initial namespace. - pub(crate) fn init_root(root: &'a mut namespace::Module) -> Self { - let mod_path = vec![]; - Self { root, mod_path } - } - - pub(crate) fn new_with_module( - &mut self, - module: &mut namespace::Module, - ) -> InstructNamespace<'_> { - let mut mod_path = self.mod_path.clone(); - if let Some(name) = &module.name { - mod_path.push(name.clone()); - } - InstructNamespace { - root: self.root, - mod_path, - } - } - - // /// A reference to the path of the module where constraints are currently - // /// being gathered. - // pub(crate) fn mod_path(&self) -> &Path { - // &self.mod_path - // } - - /// Access to the current [namespace::Module], i.e. the module at the inner - /// `mod_path`. - pub(crate) fn module(&self) -> &namespace::Module { - &self.root[&self.mod_path] - } - - /// Mutable access to the current [namespace::Module], i.e. the module at - /// the inner `mod_path`. - pub(crate) fn module_mut(&mut self) -> &mut namespace::Module { - &mut self.root[&self.mod_path] - } -} - -impl<'a> std::ops::Deref for InstructNamespace<'a> { - type Target = namespace::Module; - fn deref(&self) -> &Self::Target { - self.module() - } -} - -impl<'a> std::ops::DerefMut for InstructNamespace<'a> { - fn deref_mut(&mut self) -> &mut Self::Target { - self.module_mut() - } -} diff --git a/sway-core/src/monomorphize/instruct/declaration.rs b/sway-core/src/monomorphize/instruct/declaration.rs deleted file mode 100644 index e4da9989278..00000000000 --- a/sway-core/src/monomorphize/instruct/declaration.rs +++ /dev/null @@ -1,55 +0,0 @@ -use sway_error::handler::{ErrorEmitted, Handler}; - -use crate::{decl_engine::DeclId, language::ty, monomorphize::priv_prelude::*, SubstList}; - -pub(crate) fn instruct_decl( - ctx: InstructContext, - handler: &Handler, - decl: &ty::TyDecl, -) -> Result<(), ErrorEmitted> { - match decl { - ty::TyDecl::VariableDecl(decl) => { - instruct_exp(ctx, handler, &decl.body)?; - } - ty::TyDecl::ConstantDecl(_) => todo!(), - ty::TyDecl::FunctionDecl(ty::FunctionDecl { - decl_id, - subst_list, - .. - }) => { - instruct_fn_decl(ctx, handler, decl_id, subst_list.inner())?; - } - ty::TyDecl::TraitDecl(_) => todo!(), - ty::TyDecl::StructDecl(_) => todo!(), - ty::TyDecl::EnumDecl(_) => todo!(), - ty::TyDecl::EnumVariantDecl(_) => todo!(), - ty::TyDecl::ImplTrait(_) => todo!(), - ty::TyDecl::AbiDecl(_) => todo!(), - ty::TyDecl::GenericTypeForFunctionScope(_) => todo!(), - ty::TyDecl::StorageDecl(_) => todo!(), - ty::TyDecl::ErrorRecovery(_, _) => {} - ty::TyDecl::TypeAliasDecl(_) => todo!(), - ty::TyDecl::TraitTypeDecl(_) => todo!(), - } - Ok(()) -} - -fn instruct_fn_decl( - mut ctx: InstructContext, - handler: &Handler, - decl_id: &DeclId, - subst_list: &SubstList, -) -> Result<(), ErrorEmitted> { - let decl = ctx.engines.de().get_function(decl_id); - - if !subst_list.is_empty() { - unimplemented!("{}", decl.name); - } - - let ty::TyFunctionDecl { body, .. } = decl; - - // NOTE: todo here - instruct_code_block(ctx.by_ref(), handler, &body)?; - - Ok(()) -} diff --git a/sway-core/src/monomorphize/instruct/expression.rs b/sway-core/src/monomorphize/instruct/expression.rs deleted file mode 100644 index efbd198cffd..00000000000 --- a/sway-core/src/monomorphize/instruct/expression.rs +++ /dev/null @@ -1,98 +0,0 @@ -use sway_error::handler::{ErrorEmitted, Handler}; - -use crate::{language::ty, monomorphize::priv_prelude::*, type_system::*}; - -pub(crate) fn instruct_exp( - ctx: InstructContext, - handler: &Handler, - exp: &ty::TyExpression, -) -> Result<(), ErrorEmitted> { - instruct_exp_inner(ctx, handler, &exp.expression, exp.return_type) -} - -pub(crate) fn instruct_exp_inner( - mut ctx: InstructContext, - handler: &Handler, - exp: &ty::TyExpressionVariant, - _return_type: TypeId, -) -> Result<(), ErrorEmitted> { - // NOTE: todo here - match exp { - ty::TyExpressionVariant::FunctionApplication { - arguments, - contract_call_params, - .. - } => { - arguments - .iter() - .try_for_each(|(_, arg)| instruct_exp(ctx.by_ref(), handler, arg))?; - contract_call_params - .iter() - .try_for_each(|(_, arg)| instruct_exp(ctx.by_ref(), handler, arg))?; - // NOTE: todo here - } - ty::TyExpressionVariant::LazyOperator { lhs, rhs, .. } => { - instruct_exp(ctx.by_ref(), handler, lhs)?; - instruct_exp(ctx.by_ref(), handler, rhs)?; - } - ty::TyExpressionVariant::ConstantExpression { .. } => { - // NOTE: may need to do something here later - } - ty::TyExpressionVariant::VariableExpression { .. } => { - // NOTE: may need to do something here later - } - ty::TyExpressionVariant::Tuple { fields } => { - fields - .iter() - .try_for_each(|field| instruct_exp(ctx.by_ref(), handler, field))?; - } - ty::TyExpressionVariant::Array { - contents: _, - elem_type: _, - } => { - todo!(); - // contents - // .iter() - // .try_for_each(|elem| instruct_exp(ctx.by_ref(), handler, elem))?; - } - ty::TyExpressionVariant::ArrayIndex { - prefix: _, - index: _, - } => { - todo!(); - // instruct_exp(ctx.by_ref(), handler, prefix)?; - // instruct_exp(ctx.by_ref(), handler, index)?; - } - ty::TyExpressionVariant::StructExpression { .. } => todo!(), - ty::TyExpressionVariant::CodeBlock(block) => { - instruct_code_block(ctx, handler, block)?; - } - ty::TyExpressionVariant::IfExp { .. } => todo!(), - ty::TyExpressionVariant::MatchExp { .. } => todo!(), - ty::TyExpressionVariant::AsmExpression { .. } => todo!(), - ty::TyExpressionVariant::StructFieldAccess { .. } => todo!(), - ty::TyExpressionVariant::TupleElemAccess { prefix, .. } => { - instruct_exp(ctx, handler, prefix)?; - } - ty::TyExpressionVariant::EnumInstantiation { .. } => todo!(), - ty::TyExpressionVariant::AbiCast { .. } => todo!(), - ty::TyExpressionVariant::StorageAccess(_) => todo!(), - ty::TyExpressionVariant::IntrinsicFunction(_) => todo!(), - ty::TyExpressionVariant::AbiName(_) => todo!(), - ty::TyExpressionVariant::EnumTag { exp } => { - instruct_exp(ctx.by_ref(), handler, exp)?; - } - ty::TyExpressionVariant::UnsafeDowncast { .. } => todo!(), - ty::TyExpressionVariant::WhileLoop { .. } => todo!(), - ty::TyExpressionVariant::Reassignment(_) => todo!(), - ty::TyExpressionVariant::Return(exp) => { - instruct_exp(ctx.by_ref(), handler, exp)?; - } - ty::TyExpressionVariant::Literal(_) => {} - ty::TyExpressionVariant::Break => {} - ty::TyExpressionVariant::Continue => {} - ty::TyExpressionVariant::FunctionParameter => {} - } - - Ok(()) -} diff --git a/sway-core/src/monomorphize/instruct/mod.rs b/sway-core/src/monomorphize/instruct/mod.rs deleted file mode 100644 index abd6df81aff..00000000000 --- a/sway-core/src/monomorphize/instruct/mod.rs +++ /dev/null @@ -1,30 +0,0 @@ -//! This module applies a list of [Instruction]s to a typed AST to -//! monomorphize it. - -pub(crate) mod code_block; -pub(crate) mod context; -pub(crate) mod declaration; -pub(crate) mod expression; -pub(crate) mod module; -pub(crate) mod node; - -use std::sync::RwLock; - -use sway_error::handler::{ErrorEmitted, Handler}; - -use crate::{language::ty, monomorphize::priv_prelude::*, Engines}; - -/// Uses [Instruction]s to monomorphize a typed AST. -pub(crate) fn apply_instructions( - engines: &Engines, - handler: &Handler, - instructions: Vec, - module: &mut ty::TyModule, -) -> Result<(), ErrorEmitted> { - let instructions = RwLock::new(InstructionItems::new(instructions)); - let ctx = InstructContext::from_root(engines, &instructions); - - instruct_root(ctx, handler, module)?; - - Ok(()) -} diff --git a/sway-core/src/monomorphize/instruct/module.rs b/sway-core/src/monomorphize/instruct/module.rs deleted file mode 100644 index ed0415c796e..00000000000 --- a/sway-core/src/monomorphize/instruct/module.rs +++ /dev/null @@ -1,31 +0,0 @@ -use sway_error::handler::{ErrorEmitted, Handler}; - -use crate::{language::ty, monomorphize::priv_prelude::*}; - -pub(crate) fn instruct_root( - mut ctx: InstructContext, - handler: &Handler, - module: &mut ty::TyModule, -) -> Result<(), ErrorEmitted> { - for (_, submod) in module.submodules.iter_mut() { - instruct_module(ctx.by_ref(), handler, &mut submod.module)?; - } - for node in module.all_nodes.iter() { - instruct_node(ctx.by_ref(), handler, &node.content)?; - } - Ok(()) -} - -pub(crate) fn instruct_module( - mut ctx: InstructContext, - handler: &Handler, - module: &mut ty::TyModule, -) -> Result<(), ErrorEmitted> { - for (_, submod) in module.submodules.iter_mut() { - instruct_module(ctx.by_ref(), handler, &mut submod.module)?; - } - for node in module.all_nodes.iter() { - instruct_node(ctx.by_ref(), handler, &node.content)?; - } - Ok(()) -} diff --git a/sway-core/src/monomorphize/instruct/node.rs b/sway-core/src/monomorphize/instruct/node.rs deleted file mode 100644 index 9c444462af1..00000000000 --- a/sway-core/src/monomorphize/instruct/node.rs +++ /dev/null @@ -1,17 +0,0 @@ -use sway_error::handler::{ErrorEmitted, Handler}; - -use crate::{language::ty, monomorphize::priv_prelude::*}; - -pub(crate) fn instruct_node( - ctx: InstructContext, - handler: &Handler, - node: &ty::TyAstNodeContent, -) -> Result<(), ErrorEmitted> { - match node { - ty::TyAstNodeContent::Declaration(decl) => instruct_decl(ctx, handler, decl), - ty::TyAstNodeContent::Expression(exp) => instruct_exp(ctx, handler, exp), - ty::TyAstNodeContent::ImplicitReturnExpression(exp) => instruct_exp(ctx, handler, exp), - ty::TyAstNodeContent::SideEffect(_) => Ok(()), - ty::TyAstNodeContent::Error(_, _) => Ok(()), - } -} diff --git a/sway-core/src/monomorphize/instructions.rs b/sway-core/src/monomorphize/instructions.rs deleted file mode 100644 index 7a0a65017a4..00000000000 --- a/sway-core/src/monomorphize/instructions.rs +++ /dev/null @@ -1,72 +0,0 @@ -use std::{cmp::Ordering, hash::Hasher}; - -use crate::{decl_engine::DeclId, engine_threading::*, language::ty, type_system::*}; - -#[derive(Debug)] -pub(crate) enum Instruction { - Type(TypeId, SubstList), - FnDecl(DeclId, SubstList), - TraitDecl(DeclId, SubstList), - ImplTrait(DeclId, SubstList), - StructDecl(DeclId, SubstList), - EnumDecl(DeclId, SubstList), -} - -impl Instruction { - fn discriminant_value(&self) -> u8 { - use Instruction::*; - match self { - Type(_, _) => 0, - FnDecl(_, _) => 1, - TraitDecl(_, _) => 2, - ImplTrait(_, _) => 3, - StructDecl(_, _) => 4, - EnumDecl(_, _) => 5, - } - } -} - -impl EqWithEngines for Instruction {} -impl PartialEqWithEngines for Instruction { - fn eq(&self, other: &Self, _engines: &Engines) -> bool { - use Instruction::*; - match (self, other) { - (Type(_, _), Type(_, _)) => todo!(), - (FnDecl(_, _), FnDecl(_, _)) => todo!(), - (TraitDecl(_, _), TraitDecl(_, _)) => todo!(), - (ImplTrait(_, _), ImplTrait(_, _)) => todo!(), - (StructDecl(_, _), StructDecl(_, _)) => todo!(), - (EnumDecl(_, _), EnumDecl(_, _)) => todo!(), - (l, r) => l.discriminant_value() == r.discriminant_value(), - } - } -} - -impl HashWithEngines for Instruction { - fn hash(&self, _state: &mut H, _engines: &Engines) { - use Instruction::*; - match self { - Type(_, _) => todo!(), - FnDecl(_, _) => todo!(), - TraitDecl(_, _) => todo!(), - ImplTrait(_, _) => todo!(), - StructDecl(_, _) => todo!(), - EnumDecl(_, _) => todo!(), - } - } -} - -impl OrdWithEngines for Instruction { - fn cmp(&self, other: &Self, _engines: &Engines) -> Ordering { - use Instruction::*; - match (self, other) { - (Type(_, _), Type(_, _)) => todo!(), - (FnDecl(_, _), FnDecl(_, _)) => todo!(), - (TraitDecl(_, _), TraitDecl(_, _)) => todo!(), - (ImplTrait(_, _), ImplTrait(_, _)) => todo!(), - (StructDecl(_, _), StructDecl(_, _)) => todo!(), - (EnumDecl(_, _), EnumDecl(_, _)) => todo!(), - (l, r) => l.discriminant_value().cmp(&r.discriminant_value()), - } - } -} diff --git a/sway-core/src/monomorphize/mod.rs b/sway-core/src/monomorphize/mod.rs deleted file mode 100644 index 86d47f78a25..00000000000 --- a/sway-core/src/monomorphize/mod.rs +++ /dev/null @@ -1,32 +0,0 @@ -#![allow(dead_code)] - -mod constraint; -mod gather; -mod instruct; -mod instructions; -mod priv_prelude; -mod solve; - -use crate::{engine_threading::*, language::ty}; - -use priv_prelude::*; -use sway_error::handler::{ErrorEmitted, Handler}; - -pub(super) fn monomorphize( - handler: &Handler, - engines: &Engines, - module: &mut ty::TyModule, -) -> Result<(), ErrorEmitted> { - // Gather the constraints from the typed AST. - let constraints = gather_constraints(engines, handler, module)?; - - // Solve the constraints and get back instructions from the solver. - let mut solver = Solver::new(engines); - solver.solve(handler, constraints)?; - let instructions = solver.into_instructions(); - - // Use the new instructions to monomorphize the AST. - apply_instructions(engines, handler, instructions, module)?; - - Ok(()) -} diff --git a/sway-core/src/monomorphize/priv_prelude.rs b/sway-core/src/monomorphize/priv_prelude.rs deleted file mode 100644 index ce303f93b90..00000000000 --- a/sway-core/src/monomorphize/priv_prelude.rs +++ /dev/null @@ -1,30 +0,0 @@ -use sway_types::Ident; - -pub(super) type PathBuf = Vec; - -pub(super) use super::{ - constraint::*, - gather::{ - code_block::gather_from_code_block, - context::{GatherContext, GatherNamespace}, - declaration::gather_from_decl, - expression::gather_from_exp, - gather_constraints, - module::gather_from_root, - node::gather_from_node, - }, - instruct::{ - apply_instructions, - code_block::instruct_code_block, - context::{InstructContext, InstructionItems}, - declaration::instruct_decl, - expression::instruct_exp, - module::instruct_root, - node::instruct_node, - }, - instructions::Instruction, - solve::{ - instruction_result::InstructionResult, iteration_report::IterationReport, solver::Solver, - ConstraintPQ, ConstraintWrapper, - }, -}; diff --git a/sway-core/src/monomorphize/solve/instruction_result.rs b/sway-core/src/monomorphize/solve/instruction_result.rs deleted file mode 100644 index cf2ae8cb8fb..00000000000 --- a/sway-core/src/monomorphize/solve/instruction_result.rs +++ /dev/null @@ -1,37 +0,0 @@ -use crate::monomorphize::priv_prelude::*; - -pub(crate) enum InstructionResult { - NewInstructions(Vec), - NoInstruction, - RedoConstraint, -} - -impl InstructionResult { - pub(crate) fn from_instructions(instructions: Vec) -> InstructionResult { - if instructions.is_empty() { - InstructionResult::NoInstruction - } else { - InstructionResult::NewInstructions(instructions) - } - } -} - -impl FromIterator for InstructionResult { - fn from_iter>(iter: I) -> Self { - let mut instructions = vec![]; - - for elem in iter { - match elem { - InstructionResult::NewInstructions(new_instructions) => { - instructions.extend(new_instructions); - } - InstructionResult::NoInstruction => {} - InstructionResult::RedoConstraint => { - return InstructionResult::RedoConstraint; - } - } - } - - InstructionResult::from_instructions(instructions) - } -} diff --git a/sway-core/src/monomorphize/solve/iteration_report.rs b/sway-core/src/monomorphize/solve/iteration_report.rs deleted file mode 100644 index 889970cdf5b..00000000000 --- a/sway-core/src/monomorphize/solve/iteration_report.rs +++ /dev/null @@ -1,6 +0,0 @@ -use crate::monomorphize::priv_prelude::*; - -pub(crate) struct IterationReport<'a> { - pub(super) new_constraints: ConstraintPQ<'a>, - pub(super) instructions: Vec, -} diff --git a/sway-core/src/monomorphize/solve/mod.rs b/sway-core/src/monomorphize/solve/mod.rs deleted file mode 100644 index a03e22f0a41..00000000000 --- a/sway-core/src/monomorphize/solve/mod.rs +++ /dev/null @@ -1,15 +0,0 @@ -//! This module takes a list of [Constraint]s, solves them (if they are -//! solvable), and outputs a list of [Instruction]s to apply to a typed AST. - -pub(crate) mod instruction_result; -pub(crate) mod iteration_report; -pub(crate) mod solver; - -use std::collections::BinaryHeap; - -use crate::{engine_threading::*, monomorphize::priv_prelude::*}; - -/// Priority queue sorting the constraints. -// https://dev.to/timclicks/creating-a-priority-queue-with-a-custom-sort-order-using-a-binary-heap-in-rust-3oab -pub(crate) type ConstraintWrapper<'a> = WithEngines<'a, Constraint>; -pub(crate) type ConstraintPQ<'a> = BinaryHeap>; diff --git a/sway-core/src/monomorphize/solve/solver.rs b/sway-core/src/monomorphize/solve/solver.rs deleted file mode 100644 index 9468e5d7ae1..00000000000 --- a/sway-core/src/monomorphize/solve/solver.rs +++ /dev/null @@ -1,192 +0,0 @@ -use sway_error::handler::{ErrorEmitted, Handler}; - -use crate::{ - decl_engine::*, engine_threading::*, language::ty, monomorphize::priv_prelude::*, - type_system::*, -}; - -/// Contextual state tracked and accumulated throughout solving [Constraint]s. -pub(crate) struct Solver<'a> { - engines: &'a Engines, - - /// The instructions returned by the [Solver]. - instructions: Vec, -} - -impl<'a> Solver<'a> { - /// Creates a new [Solver]. - pub(crate) fn new(engines: &'a Engines) -> Solver<'a> { - Solver { - engines, - instructions: vec![], - } - } - - /// Takes a [Solver] and returns the list of [Instruction]s from that - /// [Solver]. - pub(crate) fn into_instructions(self) -> Vec { - self.instructions - } - - /// Solves a set of constraints with a given [Solver]. - pub(crate) fn solve(&mut self, handler: &Handler, constraints: T) -> Result<(), ErrorEmitted> - where - T: IntoIterator, - { - let mut constraints: ConstraintPQ = constraints - .into_iter() - .map(|c| self.wrap_constraint(c)) - .collect(); - - // for constraint in constraints.iter() { - // println!("{:#?}", constraint); - // } - - let mut iterations = 0; - let mut instructions = vec![]; - - while iterations < 100 && !constraints.is_empty() { - let report = self.helper( - handler, - constraints - .into_sorted_vec() - .into_iter() - .map(|c| c.thing) - .collect(), - )?; - - constraints = report.new_constraints; - instructions.extend(report.instructions); - - iterations += 1; - } - - self.instructions.extend(instructions); - - Ok(()) - } - - fn helper( - &self, - handler: &Handler, - constraints: Vec, - ) -> Result { - let mut new_constraints = ConstraintPQ::new(); - let mut instructions = vec![]; - - for constraint in constraints.into_iter() { - let instruction_res = match &constraint { - Constraint::Ty(type_id) => self.helper_ty_use(*type_id)?, - Constraint::FnCall { - decl_id, - subst_list, - arguments, - .. - } => { - self.helper_fn_call(handler, *decl_id, subst_list.clone(), arguments.clone())? - } - }; - match instruction_res { - InstructionResult::NewInstructions(instruction_res) => { - instructions.extend(instruction_res); - } - InstructionResult::NoInstruction => {} - InstructionResult::RedoConstraint => { - new_constraints.push(self.wrap_constraint(constraint)); - } - } - } - - let report = IterationReport { - new_constraints, - instructions, - }; - - Ok(report) - } - - fn helper_ty_use(&self, type_id: TypeId) -> Result { - let mut instructions = vec![]; - - match self.engines.te().get(type_id) { - TypeInfo::Unknown => todo!(), - TypeInfo::UnknownGeneric { .. } => todo!(), - TypeInfo::Placeholder(_) => todo!(), - TypeInfo::TypeParam(_) => todo!(), - TypeInfo::Enum { .. } => todo!(), - TypeInfo::Struct { .. } => todo!(), - TypeInfo::Tuple(elems) => { - let res: InstructionResult = elems - .into_iter() - .map(|type_arg| self.helper_ty_use(type_arg.type_id)) - .collect::>()?; - match res { - InstructionResult::NewInstructions(new_instructions) => { - instructions.extend(new_instructions); - } - InstructionResult::NoInstruction => {} - InstructionResult::RedoConstraint => { - return Ok(InstructionResult::RedoConstraint); - } - } - } - TypeInfo::ContractCaller { .. } => todo!(), - TypeInfo::Custom { .. } => todo!(), - TypeInfo::Numeric => todo!(), - TypeInfo::ErrorRecovery(_) => todo!(), - TypeInfo::Array(_, _) => todo!(), - TypeInfo::Storage { .. } => todo!(), - TypeInfo::Alias { .. } => todo!(), - TypeInfo::StringArray(_) - | TypeInfo::StringSlice - | TypeInfo::UnsignedInteger(_) - | TypeInfo::Boolean - | TypeInfo::B256 - | TypeInfo::Contract - | TypeInfo::RawUntypedPtr - | TypeInfo::RawUntypedSlice - | TypeInfo::Ptr(..) - | TypeInfo::Slice(..) - | TypeInfo::TraitType { .. } => {} - } - - Ok(InstructionResult::from_instructions(instructions)) - } - - fn helper_fn_call( - &self, - _handler: &Handler, - decl_id: DeclId, - subst_list: SubstList, - _arguments: Vec, - ) -> Result { - let mut instructions = vec![]; - - let res: InstructionResult = subst_list - .iter() - .map(|type_param| self.helper_ty_use(type_param.type_id)) - .collect::>()?; - match res { - InstructionResult::NewInstructions(new_instructions) => { - instructions.extend(new_instructions); - } - InstructionResult::NoInstruction => {} - InstructionResult::RedoConstraint => { - return Ok(InstructionResult::RedoConstraint); - } - } - - if !subst_list.is_empty() { - instructions.push(Instruction::FnDecl(decl_id, subst_list)); - } - - Ok(InstructionResult::from_instructions(instructions)) - } - - fn wrap_constraint(&self, constraint: Constraint) -> ConstraintWrapper { - WithEngines { - thing: constraint, - engines: self.engines, - } - } -}