From 32f867b2baa3a72ccd31f20c43fac5ee082cf4a5 Mon Sep 17 00:00:00 2001 From: IsaacShelton Date: Sat, 5 Oct 2024 15:35:04 -0500 Subject: [PATCH] Added ability to import multiple dependencies into the same namespace --- src/ast/workspace/mod.rs | 2 +- src/interpreter/syscall_handler.rs | 6 ++++-- src/lower/mod.rs | 6 ++++-- src/resolve/function_search_ctx.rs | 14 ++++++++++---- src/resolve/mod.rs | 3 ++- src/workspace/fs.rs | 11 ++++++++++- src/workspace/mod.rs | 2 +- 7 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/ast/workspace/mod.rs b/src/ast/workspace/mod.rs index 315fad6c..fd5224b6 100644 --- a/src/ast/workspace/mod.rs +++ b/src/ast/workspace/mod.rs @@ -113,7 +113,7 @@ pub struct Settings { pub debug_skip_merging_helper_exprs: bool, pub imported_namespaces: Vec>, pub assume_int_at_least_32_bits: bool, - pub namespace_to_dependency: HashMap, + pub namespace_to_dependency: HashMap>, pub dependency_to_module: HashMap, } diff --git a/src/interpreter/syscall_handler.rs b/src/interpreter/syscall_handler.rs index 9ebc1300..57ba9e78 100644 --- a/src/interpreter/syscall_handler.rs +++ b/src/interpreter/syscall_handler.rs @@ -40,7 +40,7 @@ pub struct BuildSystemSyscallHandler { pub debug_skip_merging_helper_exprs: bool, pub imported_namespaces: Vec>, pub assume_int_at_least_32_bits: bool, - pub namespace_to_dependency: HashMap, + pub namespace_to_dependency: HashMap>, } impl Default for BuildSystemSyscallHandler { @@ -156,7 +156,9 @@ impl SyscallHandler for BuildSystemSyscallHandler { let dependency_name = read_cstring(memory, &args[1]); self.namespace_to_dependency - .insert(as_namespace, dependency_name); + .entry(as_namespace) + .or_default() + .push(dependency_name); #[allow(unreachable_code)] Value::Literal(ir::Literal::Void) diff --git a/src/lower/mod.rs b/src/lower/mod.rs index 057c1cfc..cf5817e6 100644 --- a/src/lower/mod.rs +++ b/src/lower/mod.rs @@ -197,6 +197,8 @@ fn lower_function( function.name.to_string() }; + let is_main = mangled_name == "main"; + ir_module.functions.insert( function_ref, ir::Function { @@ -205,8 +207,8 @@ fn lower_function( parameters, return_type, is_cstyle_variadic: function.parameters.is_cstyle_vararg, - is_foreign: true, - is_exposed: true, + is_foreign: function.is_foreign, + is_exposed: is_main, abide_abi: function.abide_abi && ir_module.target.arch().is_some(), }, ); diff --git a/src/resolve/function_search_ctx.rs b/src/resolve/function_search_ctx.rs index c4d7e81d..70f67ac4 100644 --- a/src/resolve/function_search_ctx.rs +++ b/src/resolve/function_search_ctx.rs @@ -61,11 +61,17 @@ impl FunctionSearchCtx { .get(name.namespace.as_ref()) }) .flatten() - .and_then(|dependency| ctx.settings.dependency_to_module.get(dependency)) - .and_then(|module_fs_node_id| ctx.public.get(module_fs_node_id)) - .and_then(|public| public.get(name.basename.as_ref())) .into_iter() - .flat_map(|f| f.iter()) + .flatten() + .flat_map(|dependency| { + ctx.settings + .dependency_to_module + .get(dependency) + .and_then(|module_fs_node_id| ctx.public.get(module_fs_node_id)) + .and_then(|public| public.get(name.basename.as_ref())) + .into_iter() + }) + .flatten() .filter(|f| Self::fits(ctx, **f, arguments, source)); if let Some(found) = remote_matches.next() { diff --git a/src/resolve/mod.rs b/src/resolve/mod.rs index 642bee25..bbb3b275 100644 --- a/src/resolve/mod.rs +++ b/src/resolve/mod.rs @@ -274,8 +274,9 @@ pub fn resolve<'a>( .push_back(Job::Regular(*real_file_id, function_i, function_ref)); if function.privacy.is_public() { - let public_of_module = ctx.public.entry(file_id).or_insert_with(|| HashMap::new()); + let public_of_module = ctx.public.entry(file_id).or_insert_with(HashMap::new); + // TODO: Add proper error message let function_name = function .name .as_plain_str() diff --git a/src/workspace/fs.rs b/src/workspace/fs.rs index 4ad599f7..a53358ff 100644 --- a/src/workspace/fs.rs +++ b/src/workspace/fs.rs @@ -33,6 +33,7 @@ impl Fs { last_modified_ms: 0.into(), parent: None, segment: OsString::new().into_boxed_os_str(), + filename: OsString::new().into(), }; // We assume that the root is at index 0 @@ -55,6 +56,7 @@ impl Fs { &normalized_path_segments(path), last_modified_ms.unwrap_or(0), Some(Self::ROOT), + OsStr::new(""), ) } @@ -97,6 +99,7 @@ pub struct FsNode { pub last_modified_ms: AtomicU64, pub parent: Option, pub segment: Box, + pub filename: Box, } impl FsNode { @@ -110,6 +113,7 @@ impl FsNode { components: &[&OsStr], last_modified_ms: u64, parent: Option, + parent_filename: &OsStr, ) -> Option { let Some((path_segment, rest)) = components.split_first() else { return None; @@ -127,6 +131,10 @@ impl FsNode { let make_value = |path_segment: &OsString| { let segment = path_segment.clone().into_boxed_os_str(); + let filename = Path::new(parent_filename) + .join(&*segment) + .into_os_string() + .into_boxed_os_str(); fs.new_node(FsNode { last_modified_ms: last_modified_ms.into(), @@ -137,6 +145,7 @@ impl FsNode { children: OnceMap::new(), parent, segment, + filename, }) }; @@ -148,7 +157,7 @@ impl FsNode { node.last_modified_ms .fetch_max(last_modified_ms, Ordering::Relaxed); - node.deep_insert(fs, rest, last_modified_ms, Some(*id)) + node.deep_insert(fs, rest, last_modified_ms, Some(*id), &node.filename) .unwrap_or(*id) }; diff --git a/src/workspace/mod.rs b/src/workspace/mod.rs index eb5e3fb0..ec777de2 100644 --- a/src/workspace/mod.rs +++ b/src/workspace/mod.rs @@ -131,7 +131,7 @@ pub fn compile_workspace(compiler: &mut Compiler, project_folder: &Path) { } }; - for (_namespace, folder) in settings.namespace_to_dependency.iter() { + for folder in settings.namespace_to_dependency.values().flatten() { let infrastructure = compiler .options .infrastructure