Skip to content

Commit 576221f

Browse files
committed
Refactored AST workspaces to use arenas
1 parent 1e5897f commit 576221f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1511
-167
lines changed

Cargo.lock

+24-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ members = [
4141
"src/components/build_c_token",
4242
"src/support/look_ahead",
4343
"src/support/repeating_last",
44-
"src/support/backend",
44+
"src/support/backend", "src/components/job", "src/support/arena",
4545
]
4646

4747
[workspace.dependencies]
@@ -57,13 +57,9 @@ memo-map = "0.3.2"
5757
indexmap = "2.2.5"
5858
itertools = "0.13.0"
5959
lazy_format = "2.0.3"
60-
bit-vec = "0.8.0"
61-
thin-vec = "0.2.13"
62-
walkdir = "2.5.0"
6360
once_map = "0.4.18"
6461
num = "0.4.3"
6562
num-derive = "0.4.2"
66-
bitvec = "1.0.1"
6763
derivative = "2.2.0"
6864
ignore = "0.4.22"
6965
humansize = "2.1.3"

src/components/build_asg/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7+
arena = { version = "0.1.0", path = "../../support/arena" }
78
asg = { version = "0.1.0", path = "../../representations/asg" }
89
ast = { version = "0.1.0", path = "../../representations/ast" }
910
ast_workspace = { version = "0.1.0", path = "../../representations/ast_workspace" }

src/components/build_asg/src/helper_expr.rs src/components/build_asg/src/expr_alias.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use super::{ctx::ResolveCtx, error::ResolveError};
22
use asg::HelperExprDecl;
33
use ast_workspace::AstWorkspace;
44

5-
pub fn resolve_helper_expressions(
5+
pub fn resolve_expr_aliases(
66
ctx: &mut ResolveCtx,
77
ast_workspace: &AstWorkspace,
88
) -> Result<(), ResolveError> {
99
for (physical_file_id, file) in ast_workspace.files.iter() {
10-
let module_folder_id = ast_workspace.get_owning_module_or_self(*physical_file_id);
10+
let module_folder_id = ast_workspace.get_owning_module_or_self(physical_file_id);
1111

12-
for helper_expr in file.helper_exprs.iter() {
12+
for helper_expr in ast_workspace.view(file).expr_aliases {
1313
let helper_exprs = ctx
1414
.helper_exprs_in_modules
1515
.entry(module_folder_id)

src/components/build_asg/src/func_body.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,7 @@ pub fn resolve_func_bodies(
2121
FuncJob::Regular(physical_file_id, ast_func_index, func_ref) => {
2222
let module_folder_id = ast_workspace.get_owning_module_or_self(physical_file_id);
2323

24-
let ast_file = ast_workspace
25-
.files
26-
.get(&physical_file_id)
27-
.expect("file referenced by job to exist");
28-
29-
let ast_function = ast_file
30-
.funcs
31-
.get(ast_func_index)
32-
.expect("function referenced by job to exist");
24+
let ast_function = &ast_workspace.all_funcs[ast_func_index];
3325

3426
resolve_func_body(
3527
ctx,
@@ -49,15 +41,7 @@ pub fn resolve_func_bodies(
4941
) => {
5042
let module_folder_id = ast_workspace.get_owning_module_or_self(physical_file_id);
5143

52-
let ast_file = ast_workspace
53-
.files
54-
.get(&physical_file_id)
55-
.expect("file referenced by job to exist");
56-
57-
let ast_function = ast_file
58-
.impls
59-
.get(ast_impl_index)
60-
.expect("referenced impl to exist")
44+
let ast_function = ast_workspace.all_impls[ast_impl_index]
6145
.body
6246
.get(ast_impl_function_index)
6347
.expect("referenced impl function to exist");
@@ -103,10 +87,10 @@ fn resolve_func_body(
10387

10488
let file = ast_workspace
10589
.files
106-
.get(&physical_file_id)
90+
.get(physical_file_id)
10791
.expect("referenced file exists");
10892

109-
let settings = &ast_workspace.settings[file.settings.unwrap_or_default().0];
93+
let settings = &ast_workspace.settings[file.settings.unwrap_or(ast_workspace.default_settings)];
11094

11195
let resolved_stmts = resolve_stmts(
11296
&mut ResolveExprCtx {

src/components/build_asg/src/func_head.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,23 @@ pub fn create_func_heads<'a>(
2222
ast_workspace: &AstWorkspace,
2323
options: &BuildOptions,
2424
) -> Result<(), ResolveError> {
25-
for (physical_file_id, file) in ast_workspace.files.iter() {
26-
let module_folder_id = ast_workspace.get_owning_module_or_self(*physical_file_id);
25+
for (physical_file_id, ast_file) in ast_workspace.files.iter() {
26+
let module_folder_id = ast_workspace.get_owning_module_or_self(physical_file_id);
2727

28-
create_impl_heads(ctx, asg, options, module_folder_id, *physical_file_id, file)?;
28+
create_impl_heads(
29+
ctx,
30+
asg,
31+
options,
32+
module_folder_id,
33+
physical_file_id,
34+
ast_file,
35+
)?;
36+
37+
for func_id in ast_file.funcs.iter() {
38+
let func = &ast_workspace.all_funcs[func_id];
2939

30-
for (func_i, func) in file.funcs.iter().enumerate() {
3140
let name = if func.head.privacy.is_private() {
32-
ResolvedName::new(*physical_file_id, &Name::plain(&func.head.name))
41+
ResolvedName::new(physical_file_id, &Name::plain(&func.head.name))
3342
} else {
3443
ResolvedName::new(module_folder_id, &Name::plain(&func.head.name))
3544
};
@@ -41,7 +50,7 @@ pub fn create_func_heads<'a>(
4150
name.clone(),
4251
&func.head,
4352
module_folder_id,
44-
*physical_file_id,
53+
physical_file_id,
4554
)?;
4655

4756
if func.head.privacy.is_public() {
@@ -53,8 +62,10 @@ pub fn create_func_heads<'a>(
5362
.push(func_ref);
5463
}
5564

56-
let settings = file.settings.map(|id| &ast_workspace.settings[id.0]);
57-
let imported_namespaces = settings.map(|settings| &settings.imported_namespaces);
65+
let imported_namespaces = ast_workspace
66+
.view(ast_file)
67+
.settings
68+
.map(|settings| &settings.imported_namespaces);
5869

5970
let func_haystack = ctx.func_haystacks.get_or_insert_with(module_folder_id, || {
6071
FuncHaystack::new(
@@ -72,7 +83,7 @@ pub fn create_func_heads<'a>(
7283
.or_insert_with(|| vec![func_ref]);
7384

7485
ctx.jobs
75-
.push_back(FuncJob::Regular(*physical_file_id, func_i, func_ref));
86+
.push_back(FuncJob::Regular(physical_file_id, func_id, func_ref));
7687
}
7788
}
7889

src/components/build_asg/src/global_variable.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ pub fn resolve_global_variables(
1313
ast_workspace: &AstWorkspace,
1414
) -> Result<(), ResolveError> {
1515
for (physical_file_id, file) in ast_workspace.files.iter() {
16-
let module_folder_id = ast_workspace.get_owning_module_or_self(*physical_file_id);
16+
let module_folder_id = ast_workspace.get_owning_module_or_self(physical_file_id);
1717

18-
for global in file.global_variables.iter() {
18+
for global in ast_workspace.view(file).globals.iter() {
1919
let type_ctx = ResolveTypeCtx::new(
2020
&asg,
2121
module_folder_id,
22-
*physical_file_id,
22+
physical_file_id,
2323
&ctx.types_in_modules,
2424
);
2525

@@ -35,7 +35,7 @@ pub fn resolve_global_variables(
3535
});
3636

3737
let fs_node_id = if global.privacy.is_private() {
38-
*physical_file_id
38+
physical_file_id
3939
} else {
4040
module_folder_id
4141
};

src/components/build_asg/src/impl_head/mod.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use super::{
55
};
66
use crate::{error::ResolveErrorKind, type_ctx::ResolveTypeCtx};
77
use asg::{Asg, Func, GenericTraitRef, ImplDecl, ImplRef, ResolvedName, TraitFunc, Type};
8-
use ast::{AstFile, Name};
8+
use ast::Name;
9+
use ast_workspace::AstFile;
910
use attributes::Privacy;
1011
use compiler::BuildOptions;
1112
use for_alls::ForAlls;
@@ -20,9 +21,10 @@ pub fn create_impl_heads(
2021
options: &BuildOptions,
2122
module_folder_id: FsNodeId,
2223
physical_file_id: FsNodeId,
23-
file: &AstFile,
24+
ast_file: &AstFile,
2425
) -> Result<(), ResolveError> {
25-
for (impl_i, ast_impl) in file.impls.iter().enumerate() {
26+
for impl_id in ast_file.impls.iter() {
27+
let ast_impl = &asg.workspace.all_impls[impl_id];
2628
let impl_ref = create_impl_head(ctx, asg, module_folder_id, physical_file_id, ast_impl)?;
2729

2830
for (func_i, func) in ast_impl.body.iter().enumerate() {
@@ -61,8 +63,12 @@ pub fn create_impl_heads(
6163

6264
ensure_satisfies_trait_func(ctx, asg, expected, trait_func, impl_func)?;
6365

64-
ctx.jobs
65-
.push_back(FuncJob::Impling(physical_file_id, impl_i, func_i, func_ref));
66+
ctx.jobs.push_back(FuncJob::Impling(
67+
physical_file_id,
68+
impl_id,
69+
func_i,
70+
func_ref,
71+
));
6672

6773
if asg
6874
.impls

src/components/build_asg/src/job.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use arena::Idx;
12
use asg::{EnumRef, StructRef, TraitRef, TypeAliasRef};
3+
use ast_workspace::{FuncId, ImplId};
24
use fs_tree::FsNodeId;
35

46
#[derive(Clone, Debug)]
57
pub enum FuncJob {
6-
Regular(FsNodeId, usize, asg::FuncRef),
7-
Impling(FsNodeId, usize, usize, asg::FuncRef),
8+
Regular(FsNodeId, Idx<FuncId, ast::Func>, asg::FuncRef),
9+
Impling(FsNodeId, Idx<ImplId, ast::Impl>, usize, asg::FuncRef),
810
}
911

1012
#[derive(Clone, Debug)]

src/components/build_asg/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ mod ctx;
55
mod destination;
66
mod error;
77
mod expr;
8+
mod expr_alias;
89
mod func_body;
910
mod func_haystack;
1011
mod func_head;
1112
mod global_variable;
12-
mod helper_expr;
1313
mod impl_head;
1414
mod initialized;
1515
mod job;
@@ -26,10 +26,10 @@ use ast_workspace::AstWorkspace;
2626
use compiler::BuildOptions;
2727
use ctx::ResolveCtx;
2828
use error::ResolveError;
29+
use expr_alias::resolve_expr_aliases;
2930
use func_body::resolve_func_bodies;
3031
use func_head::create_func_heads;
3132
use global_variable::resolve_global_variables;
32-
use helper_expr::resolve_helper_expressions;
3333
use initialized::Initialized;
3434
pub use polymorph::*;
3535
pub use stmt::resolve_stmts;
@@ -48,7 +48,7 @@ pub fn resolve<'a>(
4848
resolve_type_definitions(&mut ctx, &mut asg, workspace)?;
4949
resolve_global_variables(&mut ctx, &mut asg, workspace)?;
5050
create_func_heads(&mut ctx, &mut asg, workspace, options)?;
51-
resolve_helper_expressions(&mut ctx, workspace)?;
51+
resolve_expr_aliases(&mut ctx, workspace)?;
5252
resolve_func_bodies(&mut ctx, &mut asg, workspace)?;
5353

5454
Ok(asg)

src/components/build_asg/src/type_ctx/find.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ impl<'a> ResolveTypeCtx<'a> {
1616
.asg
1717
.workspace
1818
.files
19-
.get(&self.file_fs_node_id)
19+
.get(self.file_fs_node_id)
2020
.unwrap()
2121
.settings
22-
.expect("valid settings id")
23-
.0];
22+
.expect("valid settings id")];
2423

2524
let decl = name
2625
.as_plain_str()

src/components/build_asg/src/type_definition/prepare.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ pub fn prepare_type_jobs(
1717
asg: &mut Asg,
1818
ast_workspace: &AstWorkspace,
1919
) -> Result<Vec<TypeJob>, ResolveError> {
20-
let mut type_jobs = Vec::with_capacity(ast_workspace.files.len());
20+
let mut type_jobs = Vec::new();
2121

2222
for (physical_file_id, file) in ast_workspace.files.iter() {
23-
let physical_file_id = *physical_file_id;
23+
let file = ast_workspace.view(file);
24+
let physical_file_id = physical_file_id;
2425
let module_fs_node_id = ast_workspace.get_owning_module_or_self(physical_file_id);
2526

2627
let mut job = TypeJob {

src/components/build_asg/src/type_definition/resolve.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ pub fn resolve_type_jobs(
1919
type_jobs: &[TypeJob],
2020
) -> Result<(), ResolveError> {
2121
for job in type_jobs.iter() {
22-
let file = ast_workspace
23-
.files
24-
.get(&job.physical_file_id)
25-
.expect("valid ast file");
22+
let file = ast_workspace.view(
23+
ast_workspace
24+
.files
25+
.get(job.physical_file_id)
26+
.expect("valid ast file"),
27+
);
2628

2729
let module_folder_id = ast_workspace.get_owning_module_or_self(job.physical_file_id);
2830

0 commit comments

Comments
 (0)