Skip to content

Commit

Permalink
Implementated data piping for implementation solver
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Dec 19, 2024
1 parent 74f8813 commit 37f5312
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 27 deletions.
5 changes: 4 additions & 1 deletion src/pragma_section/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
lower::lower,
parser::error::ParseErrorKind,
resolve::resolve,
resolved::Implementations,
show::{into_show, Show},
target::Target,
workspace::fs::Fs,
Expand Down Expand Up @@ -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)?;

Expand Down
13 changes: 9 additions & 4 deletions src/resolve/ctx.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
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<FuncJob>,
pub function_haystacks: IndexMap<FsNodeId, FunctionHaystack>,
pub public_functions: HashMap<FsNodeId, HashMap<String, Vec<resolved::FunctionRef>>>,
pub types_in_modules: HashMap<FsNodeId, HashMap<String, resolved::TypeDecl>>,
pub globals_in_modules: HashMap<FsNodeId, HashMap<String, resolved::GlobalVarDecl>>,
pub helper_exprs_in_modules: HashMap<FsNodeId, HashMap<String, resolved::HelperExprDecl>>,
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(),
public_functions: HashMap::new(),
types_in_modules: HashMap::new(),
globals_in_modules: HashMap::new(),
helper_exprs_in_modules: HashMap::new(),
implementations,
}
}
}
2 changes: 1 addition & 1 deletion src/resolve/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub struct ResolveExprCtx<'a, 'b> {
pub helper_exprs_in_modules: &'b HashMap<FsNodeId, HashMap<String, resolved::HelperExprDecl>>,
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> {
Expand Down
13 changes: 8 additions & 5 deletions src/resolve/function_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/resolve/global_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/resolve/helper_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

Expand Down
5 changes: 3 additions & 2 deletions src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,9 +37,10 @@ use type_definition::resolve_type_definitions;

pub fn resolve<'a>(
ast_workspace: &'a AstWorkspace,
implementations: &'a Implementations,
options: &BuildOptions,
) -> Result<resolved::Ast<'a>, 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);

Expand Down
2 changes: 1 addition & 1 deletion src/resolve/type_ctx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct ResolveTypeCtx<'a> {
file_fs_node_id: FsNodeId,
types_in_modules: &'a HashMap<FsNodeId, HashMap<String, resolved::TypeDecl>>,
used_aliases_stack: HashSet<ResolvedName>,
current_constraints: &'a CurrentConstraints,
current_constraints: &'a CurrentConstraints<'a>,
}

impl<'a> ResolveTypeCtx<'a> {
Expand Down
2 changes: 1 addition & 1 deletion src/resolve/type_definition/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions src/resolve/type_definition/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 26 additions & 5 deletions src/resolved/function.rs
Original file line number Diff line number Diff line change
@@ -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<String, HashSet<Constraint>>,
pub implementations: &'a Implementations,
}

#[derive(Clone, Debug, Default)]
pub struct Implementations {
targeting_trait: HashMap<TraitRef, ()>,
}

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 {
Expand Down Expand Up @@ -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,
Expand All @@ -48,7 +69,7 @@ pub struct Function {
pub source: Source,
pub abide_abi: bool,
pub tag: Option<Tag>,
pub constraints: CurrentConstraints,
pub constraints: CurrentConstraints<'a>,
}

#[derive(Clone, Debug, Default)]
Expand Down
2 changes: 1 addition & 1 deletion src/resolved/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ new_key_type! {
pub struct Ast<'a> {
pub source_files: &'a SourceFiles,
pub entry_point: Option<FunctionRef>,
pub functions: SlotMap<FunctionRef, Function>,
pub functions: SlotMap<FunctionRef, Function<'a>>,
pub structures: SlotMap<StructureRef, Structure>,
pub globals: SlotMap<GlobalVarRef, GlobalVar>,
pub enums: SlotMap<EnumRef, Enum>,
Expand Down
7 changes: 6 additions & 1 deletion src/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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)?;
Expand Down

0 comments on commit 37f5312

Please sign in to comment.