From 37f53129956836693c6a042f40e15ab7ed15ade5 Mon Sep 17 00:00:00 2001 From: IsaacShelton Date: Wed, 18 Dec 2024 21:59:42 -0600 Subject: [PATCH] Implementated data piping for implementation solver --- src/pragma_section/run.rs | 5 ++++- src/resolve/ctx.rs | 13 +++++++---- src/resolve/expr/mod.rs | 2 +- src/resolve/function_head.rs | 13 ++++++----- src/resolve/global_variable.rs | 2 +- src/resolve/helper_expr.rs | 1 + src/resolve/mod.rs | 5 +++-- src/resolve/type_ctx/mod.rs | 2 +- src/resolve/type_definition/prepare.rs | 2 +- src/resolve/type_definition/resolve.rs | 11 +++++---- src/resolved/function.rs | 31 +++++++++++++++++++++----- src/resolved/mod.rs | 2 +- src/workspace/mod.rs | 7 +++++- 13 files changed, 69 insertions(+), 27 deletions(-) diff --git a/src/pragma_section/run.rs b/src/pragma_section/run.rs index e4e693c1..1c0ee933 100644 --- a/src/pragma_section/run.rs +++ b/src/pragma_section/run.rs @@ -7,6 +7,7 @@ use crate::{ lower::lower, parser::error::ParseErrorKind, resolve::resolve, + resolved::Implementations, show::{into_show, Show}, target::Target, workspace::fs::Fs, @@ -47,7 +48,9 @@ impl PragmaSection { let files = IndexMap::from_iter(std::iter::once((fs_node_id, self.ast_file))); let workspace = AstWorkspace::new(fs, files, base_compiler.source_files, None); - let resolved_ast = resolve(&workspace, &compiler.options).map_err(into_show)?; + let implementations = Implementations::new(); + let resolved_ast = + resolve(&workspace, &implementations, &compiler.options).map_err(into_show)?; let ir_module = lower(&compiler.options, &resolved_ast).map_err(into_show)?; diff --git a/src/resolve/ctx.rs b/src/resolve/ctx.rs index 527963a8..580935f8 100644 --- a/src/resolve/ctx.rs +++ b/src/resolve/ctx.rs @@ -1,19 +1,23 @@ use super::{function_haystack::FunctionHaystack, job::FuncJob}; -use crate::{resolved, workspace::fs::FsNodeId}; +use crate::{ + resolved::{self, Implementations}, + workspace::fs::FsNodeId, +}; use indexmap::IndexMap; use std::collections::{HashMap, VecDeque}; -pub struct ResolveCtx { +pub struct ResolveCtx<'a> { pub jobs: VecDeque, pub function_haystacks: IndexMap, pub public_functions: HashMap>>, pub types_in_modules: HashMap>, pub globals_in_modules: HashMap>, pub helper_exprs_in_modules: HashMap>, + pub implementations: &'a Implementations, } -impl ResolveCtx { - pub fn new() -> Self { +impl<'a> ResolveCtx<'a> { + pub fn new(implementations: &'a Implementations) -> Self { Self { jobs: Default::default(), function_haystacks: Default::default(), @@ -21,6 +25,7 @@ impl ResolveCtx { types_in_modules: HashMap::new(), globals_in_modules: HashMap::new(), helper_exprs_in_modules: HashMap::new(), + implementations, } } } diff --git a/src/resolve/expr/mod.rs b/src/resolve/expr/mod.rs index 66f1e3ba..c1ceaf9f 100644 --- a/src/resolve/expr/mod.rs +++ b/src/resolve/expr/mod.rs @@ -59,7 +59,7 @@ pub struct ResolveExprCtx<'a, 'b> { pub helper_exprs_in_modules: &'b HashMap>, pub module_fs_node_id: FsNodeId, pub physical_fs_node_id: FsNodeId, - pub current_constraints: CurrentConstraints, + pub current_constraints: CurrentConstraints<'b>, } impl<'a, 'b> ResolveExprCtx<'a, 'b> { diff --git a/src/resolve/function_head.rs b/src/resolve/function_head.rs index af5b131e..f802ccf7 100644 --- a/src/resolve/function_head.rs +++ b/src/resolve/function_head.rs @@ -12,9 +12,9 @@ use crate::{ }; use std::collections::{HashMap, HashSet}; -pub fn create_function_heads( - ctx: &mut ResolveCtx, - resolved_ast: &mut resolved::Ast, +pub fn create_function_heads<'a>( + ctx: &mut ResolveCtx<'a>, + resolved_ast: &mut resolved::Ast<'a>, ast_workspace: &AstWorkspace, options: &BuildOptions, ) -> Result<(), ResolveError> { @@ -25,7 +25,7 @@ pub fn create_function_heads( for (function_i, function) in file.functions.iter().enumerate() { let name = ResolvedName::new(module_file_id, &function.name); - let pre_parameters_constraints = CurrentConstraints::default(); + let pre_parameters_constraints = CurrentConstraints::new_empty(ctx.implementations); let type_ctx = ResolveTypeCtx::new( &resolved_ast, @@ -75,7 +75,10 @@ pub fn create_function_heads( } }), is_generic, - constraints: CurrentConstraints { constraints }, + constraints: CurrentConstraints { + constraints, + implementations: ctx.implementations, + }, }); if function.privacy.is_public() { diff --git a/src/resolve/global_variable.rs b/src/resolve/global_variable.rs index 2a459f7d..8045dc71 100644 --- a/src/resolve/global_variable.rs +++ b/src/resolve/global_variable.rs @@ -10,7 +10,7 @@ pub fn resolve_global_variables( resolved_ast: &mut resolved::Ast, ast_workspace: &AstWorkspace, ) -> Result<(), ResolveError> { - let constraints = CurrentConstraints::default(); + let constraints = CurrentConstraints::new_empty(ctx.implementations); for (physical_file_id, file) in ast_workspace.files.iter() { let module_file_id = ast_workspace diff --git a/src/resolve/helper_expr.rs b/src/resolve/helper_expr.rs index c83ffb0a..c743cfee 100644 --- a/src/resolve/helper_expr.rs +++ b/src/resolve/helper_expr.rs @@ -45,6 +45,7 @@ pub fn resolve_helper_expressions( physical_fs_node_id: *physical_file_id, current_constraints: CurrentConstraints { constraints: Default::default(), + implementations: ctx.implementations, }, }; diff --git a/src/resolve/mod.rs b/src/resolve/mod.rs index c06983e9..7dbc2647 100644 --- a/src/resolve/mod.rs +++ b/src/resolve/mod.rs @@ -22,7 +22,7 @@ use self::error::ResolveError; use crate::{ ast::AstWorkspace, cli::BuildOptions, - resolved::{self}, + resolved::{self, Implementations}, }; use ctx::ResolveCtx; use function_body::resolve_function_bodies; @@ -37,9 +37,10 @@ use type_definition::resolve_type_definitions; pub fn resolve<'a>( ast_workspace: &'a AstWorkspace, + implementations: &'a Implementations, options: &BuildOptions, ) -> Result, ResolveError> { - let mut ctx = ResolveCtx::new(); + let mut ctx = ResolveCtx::new(implementations); let source_files = ast_workspace.source_files; let mut resolved_ast = resolved::Ast::new(source_files, &ast_workspace); diff --git a/src/resolve/type_ctx/mod.rs b/src/resolve/type_ctx/mod.rs index 48f0f072..6c4eb406 100644 --- a/src/resolve/type_ctx/mod.rs +++ b/src/resolve/type_ctx/mod.rs @@ -21,7 +21,7 @@ pub struct ResolveTypeCtx<'a> { file_fs_node_id: FsNodeId, types_in_modules: &'a HashMap>, used_aliases_stack: HashSet, - current_constraints: &'a CurrentConstraints, + current_constraints: &'a CurrentConstraints<'a>, } impl<'a> ResolveTypeCtx<'a> { diff --git a/src/resolve/type_definition/prepare.rs b/src/resolve/type_definition/prepare.rs index 0e634e6f..7baaa234 100644 --- a/src/resolve/type_definition/prepare.rs +++ b/src/resolve/type_definition/prepare.rs @@ -91,7 +91,7 @@ fn prepare_structure( let mut parameters = TypeParameters::default(); for (name, parameter) in structure.parameters.iter() { - let zero_current_constraints = CurrentConstraints::default(); + let zero_current_constraints = CurrentConstraints::new_empty(ctx.implementations); let constraints = resolve_constraints( &ResolveTypeCtx::new( resolved_ast, diff --git a/src/resolve/type_definition/resolve.rs b/src/resolve/type_definition/resolve.rs index ba4733fc..1589230c 100644 --- a/src/resolve/type_definition/resolve.rs +++ b/src/resolve/type_definition/resolve.rs @@ -80,7 +80,7 @@ fn resolve_structure( structure_ref: StructureRef, ) -> Result<(), ResolveError> { for (field_name, field) in structure.fields.iter() { - let pre_constraints = CurrentConstraints::default(); + let pre_constraints = CurrentConstraints::new_empty(ctx.implementations); let pre_type_ctx = ResolveTypeCtx::new( &resolved_ast, module_file_id, @@ -99,7 +99,10 @@ fn resolve_structure( ); } - let constraints = CurrentConstraints { constraints }; + let constraints = CurrentConstraints { + constraints, + implementations: ctx.implementations, + }; let type_ctx = ResolveTypeCtx::new( &resolved_ast, @@ -137,7 +140,7 @@ fn resolve_enum( definition: &ast::Enum, enum_ref: EnumRef, ) -> Result<(), ResolveError> { - let constraints = CurrentConstraints::default(); + let constraints = CurrentConstraints::new_empty(ctx.implementations); let type_ctx = ResolveTypeCtx::new( &resolved_ast, module_file_id, @@ -165,7 +168,7 @@ fn resolve_type_alias( definition: &ast::TypeAlias, type_alias_ref: TypeAliasRef, ) -> Result<(), ResolveError> { - let constraints = CurrentConstraints::default(); + let constraints = CurrentConstraints::new_empty(ctx.implementations); let type_ctx = ResolveTypeCtx::new( &resolved_ast, diff --git a/src/resolved/function.rs b/src/resolved/function.rs index 4b58b27d..d174bf37 100644 --- a/src/resolved/function.rs +++ b/src/resolved/function.rs @@ -1,12 +1,33 @@ use crate::{name::ResolvedName, resolved::*, source_files::Source, tag::Tag}; use std::{collections::HashSet, fmt::Display}; -#[derive(Clone, Debug, Default)] -pub struct CurrentConstraints { +#[derive(Clone, Debug)] +pub struct CurrentConstraints<'a> { pub constraints: HashMap>, + pub implementations: &'a Implementations, +} + +#[derive(Clone, Debug, Default)] +pub struct Implementations { + targeting_trait: HashMap, +} + +impl Implementations { + pub fn new() -> Self { + Self { + targeting_trait: Default::default(), + } + } } -impl CurrentConstraints { +impl<'a> CurrentConstraints<'a> { + pub fn new_empty(implementations: &'a Implementations) -> Self { + Self { + constraints: Default::default(), + implementations, + } + } + pub fn satisfies(&self, ty: &Type, constraint: &Constraint) -> bool { match constraint { Constraint::PrimitiveAdd => match &ty.kind { @@ -37,7 +58,7 @@ impl CurrentConstraints { } #[derive(Clone, Debug)] -pub struct Function { +pub struct Function<'a> { pub name: ResolvedName, pub parameters: Parameters, pub return_type: Type, @@ -48,7 +69,7 @@ pub struct Function { pub source: Source, pub abide_abi: bool, pub tag: Option, - pub constraints: CurrentConstraints, + pub constraints: CurrentConstraints<'a>, } #[derive(Clone, Debug, Default)] diff --git a/src/resolved/mod.rs b/src/resolved/mod.rs index f108b0c9..b4a9e65e 100644 --- a/src/resolved/mod.rs +++ b/src/resolved/mod.rs @@ -48,7 +48,7 @@ new_key_type! { pub struct Ast<'a> { pub source_files: &'a SourceFiles, pub entry_point: Option, - pub functions: SlotMap, + pub functions: SlotMap>, pub structures: SlotMap, pub globals: SlotMap, pub enums: SlotMap, diff --git a/src/workspace/mod.rs b/src/workspace/mod.rs index 2b23bb9d..92e6f093 100644 --- a/src/workspace/mod.rs +++ b/src/workspace/mod.rs @@ -22,6 +22,7 @@ use crate::{ interpreter_env::{run_build_system_interpreter, setup_build_system_interpreter_symbols}, lower::lower, resolve::resolve, + resolved::Implementations, unerror::unerror, workspace::export_and_link::export_and_link, }; @@ -77,7 +78,11 @@ pub fn compile_workspace( let workspace = AstWorkspace::new(fs, files, compiler.source_files, Some(module_folders)); // Resolve symbols and validate semantics for workspace - let resolved_ast = unerror(resolve(&workspace, &compiler.options), source_files)?; + let implementations = Implementations::new(); + let resolved_ast = unerror( + resolve(&workspace, &implementations, &compiler.options), + source_files, + )?; // Lower code to high level intermediate representation let ir_module = unerror(lower(&compiler.options, &resolved_ast), source_files)?;