Skip to content

Commit d4412e2

Browse files
committed
Implemented symbol isolation for functions in C source files
1 parent 18c32b0 commit d4412e2

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

Diff for: src/resolve/func_haystack.rs

+33-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ use crate::{
1616
use itertools::Itertools;
1717
use std::collections::HashMap;
1818

19+
/// A function haystack for a module.
20+
/// Specific files can have additional restrictions based on
21+
/// `ctx.physical_fs_node_id`
1922
#[derive(Clone, Debug)]
2023
pub struct FuncHaystack {
2124
pub available: HashMap<ResolvedName, Vec<asg::FuncRef>>,
@@ -55,12 +58,25 @@ impl FuncHaystack {
5558
pub fn find_near_matches(&self, ctx: &ResolveExprCtx, name: &Name) -> Vec<String> {
5659
// TODO: Clean up this function
5760

61+
let isolate_from_module = ctx
62+
.asg
63+
.workspace
64+
.fs
65+
.get(ctx.physical_fs_node_id)
66+
.isolate_from_module;
67+
68+
let local_fs_node_id = if isolate_from_module {
69+
ctx.physical_fs_node_id
70+
} else {
71+
self.module_fs_node_id
72+
};
73+
5874
let local_matches = self
5975
.available
60-
.get(&ResolvedName::new(self.module_fs_node_id, name))
76+
.get(&ResolvedName::new(local_fs_node_id, name))
6177
.into_iter()
6278
.chain(
63-
(self.module_fs_node_id != ctx.physical_fs_node_id)
79+
(local_fs_node_id != ctx.physical_fs_node_id)
6480
.then(|| {
6581
self.available
6682
.get(&ResolvedName::new(ctx.physical_fs_node_id, name))
@@ -202,12 +218,25 @@ impl FuncHaystack {
202218
arguments: &[TypedExpr],
203219
source: Source,
204220
) -> Option<Result<Callee, FindFunctionError>> {
221+
let isolate_from_module = ctx
222+
.asg
223+
.workspace
224+
.fs
225+
.get(ctx.physical_fs_node_id)
226+
.isolate_from_module;
227+
228+
let local_fs_node_id = if isolate_from_module {
229+
ctx.physical_fs_node_id
230+
} else {
231+
self.module_fs_node_id
232+
};
233+
205234
let mut local_matches = self
206235
.available
207-
.get(&ResolvedName::new(self.module_fs_node_id, name))
236+
.get(&ResolvedName::new(local_fs_node_id, name))
208237
.into_iter()
209238
.chain(
210-
(self.module_fs_node_id != ctx.physical_fs_node_id)
239+
(local_fs_node_id != ctx.physical_fs_node_id)
211240
.then(|| {
212241
self.available
213242
.get(&ResolvedName::new(ctx.physical_fs_node_id, name))

Diff for: src/resolve/helper_expr.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
asg::{Asg, HelperExprDecl},
1010
ast::AstWorkspace,
1111
};
12+
use std::borrow::Cow;
1213

1314
pub fn resolve_helper_expressions(
1415
ctx: &mut ResolveCtx,
@@ -20,17 +21,18 @@ pub fn resolve_helper_expressions(
2021
let settings = &ast_workspace.settings[file.settings.unwrap_or_default().0];
2122

2223
// NOTE: This module should already have a function haystack
23-
let func_haystack = ctx
24-
.func_haystacks
25-
.get(&module_folder_id)
26-
.expect("function haystack to exist for file");
24+
let func_haystack = Cow::Borrowed(
25+
ctx.func_haystacks
26+
.get(&module_folder_id)
27+
.expect("function haystack to exist for file"),
28+
);
2729

2830
for helper_expr in file.helper_exprs.iter() {
2931
let value = {
3032
let variable_haystack = VariableHaystack::new();
3133
let mut ctx = ResolveExprCtx {
3234
asg,
33-
func_haystack,
35+
func_haystack: &func_haystack,
3436
variable_haystack,
3537
func_ref: None,
3638
settings,

Diff for: src/workspace/fs.rs

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl Fs {
3434
parent: None,
3535
segment: OsString::new().into_boxed_os_str(),
3636
filename: OsString::new().into(),
37+
isolate_from_module: false,
3738
};
3839

3940
// We assume that the root is at index 0
@@ -100,6 +101,7 @@ pub struct FsNode {
100101
pub parent: Option<FsNodeId>,
101102
pub segment: Box<OsStr>,
102103
pub filename: Box<OsStr>,
104+
pub isolate_from_module: bool,
103105
}
104106

105107
impl FsNode {
@@ -136,6 +138,10 @@ impl FsNode {
136138
.into_os_string()
137139
.into_boxed_os_str();
138140

141+
let is_c_source_file = Path::new(&segment)
142+
.extension()
143+
.map_or(false, |extension| extension == "c");
144+
139145
fs.new_node(FsNode {
140146
last_modified_ms: last_modified_ms.into(),
141147
node_type: rest
@@ -146,6 +152,7 @@ impl FsNode {
146152
parent,
147153
segment,
148154
filename,
155+
isolate_from_module: is_c_source_file,
149156
})
150157
};
151158

0 commit comments

Comments
 (0)