Skip to content

Commit

Permalink
Added ability to import multiple dependencies into the same namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Oct 5, 2024
1 parent ca52e47 commit 32f867b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/ast/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub struct Settings {
pub debug_skip_merging_helper_exprs: bool,
pub imported_namespaces: Vec<Box<str>>,
pub assume_int_at_least_32_bits: bool,
pub namespace_to_dependency: HashMap<String, String>,
pub namespace_to_dependency: HashMap<String, Vec<String>>,
pub dependency_to_module: HashMap<String, FsNodeId>,
}

Expand Down
6 changes: 4 additions & 2 deletions src/interpreter/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct BuildSystemSyscallHandler {
pub debug_skip_merging_helper_exprs: bool,
pub imported_namespaces: Vec<Box<str>>,
pub assume_int_at_least_32_bits: bool,
pub namespace_to_dependency: HashMap<String, String>,
pub namespace_to_dependency: HashMap<String, Vec<String>>,
}

impl Default for BuildSystemSyscallHandler {
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions src/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
},
);
Expand Down
14 changes: 10 additions & 4 deletions src/resolve/function_search_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 10 additions & 1 deletion src/workspace/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -55,6 +56,7 @@ impl Fs {
&normalized_path_segments(path),
last_modified_ms.unwrap_or(0),
Some(Self::ROOT),
OsStr::new(""),
)
}

Expand Down Expand Up @@ -97,6 +99,7 @@ pub struct FsNode {
pub last_modified_ms: AtomicU64,
pub parent: Option<FsNodeId>,
pub segment: Box<OsStr>,
pub filename: Box<OsStr>,
}

impl FsNode {
Expand All @@ -110,6 +113,7 @@ impl FsNode {
components: &[&OsStr],
last_modified_ms: u64,
parent: Option<FsNodeId>,
parent_filename: &OsStr,
) -> Option<FsNodeId> {
let Some((path_segment, rest)) = components.split_first() else {
return None;
Expand All @@ -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(),
Expand All @@ -137,6 +145,7 @@ impl FsNode {
children: OnceMap::new(),
parent,
segment,
filename,
})
};

Expand All @@ -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)
};

Expand Down
2 changes: 1 addition & 1 deletion src/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 32f867b

Please sign in to comment.