Skip to content

Commit 407609e

Browse files
committed
std lib with dummy fn
1 parent 98967b5 commit 407609e

40 files changed

Lines changed: 1180 additions & 638 deletions

Cargo.lock

Lines changed: 50 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/analyzer/src/db.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use crate::namespace::items::{
66
};
77
use crate::namespace::types;
88
use fe_common::Span;
9-
use fe_parser::ast;
10-
use fe_parser::node::Node;
119
use indexmap::map::IndexMap;
1210
use smol_str::SmolStr;
1311
use std::rc::Rc;
@@ -59,6 +57,12 @@ pub trait AnalyzerDb {
5957
fn ingot_all_modules(&self, ingot: IngotId) -> Rc<Vec<ModuleId>>;
6058
#[salsa::invoke(queries::ingots::ingot_main_module)]
6159
fn ingot_main_module(&self, ingot: IngotId) -> Analysis<Option<ModuleId>>;
60+
#[salsa::invoke(queries::ingots::ingot_lib_module)]
61+
fn ingot_lib_module(&self, ingot: IngotId) -> Analysis<Option<ModuleId>>;
62+
#[salsa::invoke(queries::ingots::ingot_root_module)]
63+
fn ingot_root_module(&self, ingot: IngotId) -> Option<ModuleId>;
64+
#[salsa::invoke(queries::ingots::ingot_root_sub_modules)]
65+
fn ingot_root_sub_modules(&self, ingot: IngotId) -> Rc<IndexMap<SmolStr, ModuleId>>;
6266

6367
// Module
6468
#[salsa::invoke(queries::module::module_all_items)]
@@ -74,16 +78,8 @@ pub trait AnalyzerDb {
7478
&self,
7579
module: ModuleId,
7680
) -> Analysis<Rc<IndexMap<SmolStr, (Span, Item)>>>;
77-
#[salsa::invoke(queries::module::module_resolve_use_tree)]
78-
fn module_resolve_use_tree(
79-
&self,
80-
module: ModuleId,
81-
tree: Node<ast::UseTree>,
82-
) -> Analysis<Rc<IndexMap<SmolStr, (Span, Item)>>>;
8381
#[salsa::invoke(queries::module::module_parent_module)]
8482
fn module_parent_module(&self, module: ModuleId) -> Option<ModuleId>;
85-
#[salsa::invoke(queries::module::module_adjacent_modules)]
86-
fn module_adjacent_modules(&self, module: ModuleId) -> Rc<IndexMap<SmolStr, ModuleId>>;
8783
#[salsa::invoke(queries::module::module_sub_modules)]
8884
fn module_sub_modules(&self, module: ModuleId) -> Rc<IndexMap<SmolStr, ModuleId>>;
8985

crates/analyzer/src/db/queries/ingots.rs

Lines changed: 81 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use crate::namespace::items::{IngotId, Module, ModuleContext, ModuleFileContent,
33
use crate::AnalyzerDb;
44
use fe_common::diagnostics::{Diagnostic, Severity};
55
use fe_parser::ast;
6+
use indexmap::map::IndexMap;
67
use indexmap::set::IndexSet;
8+
use smol_str::SmolStr;
79
use std::path::Path;
810
use std::rc::Rc;
911

@@ -42,25 +44,26 @@ pub fn ingot_all_modules(db: &dyn AnalyzerDb, ingot_id: IngotId) -> Rc<Vec<Modul
4244
})
4345
.collect::<IndexSet<_>>()
4446
.into_iter()
45-
.map(|dir| {
46-
let module = Module {
47-
name: dir
48-
.file_name()
49-
.expect("missing file name")
50-
.to_str()
51-
.expect("could not convert dir name to string")
52-
.into(),
53-
ast: ast::Module { body: vec![] },
54-
context: ModuleContext::Ingot(ingot_id),
55-
file_content: ModuleFileContent::Dir {
56-
dir_path: dir
47+
.filter_map(|dir| {
48+
if let Some(file_name) = dir.file_name() {
49+
let module = Module {
50+
name: file_name
5751
.to_str()
58-
.expect("could not convert dir path to string")
52+
.expect("could not convert dir name to string")
5953
.into(),
60-
},
61-
};
62-
63-
db.intern_module(Rc::new(module))
54+
ast: ast::Module { body: vec![] },
55+
context: ModuleContext::Ingot(ingot_id),
56+
file_content: ModuleFileContent::Dir {
57+
dir_path: dir
58+
.to_str()
59+
.expect("could not convert dir path to string")
60+
.into(),
61+
},
62+
};
63+
Some(db.intern_module(Rc::new(module)))
64+
} else {
65+
None
66+
}
6467
})
6568
.collect::<Vec<ModuleId>>();
6669

@@ -72,15 +75,7 @@ pub fn ingot_main_module(db: &dyn AnalyzerDb, ingot_id: IngotId) -> Analysis<Opt
7275
let main_id = ingot_id
7376
.all_modules(db)
7477
.iter()
75-
.find(|module_id| {
76-
module_id.name(db) == "main" && {
77-
if let Some(parent_id) = module_id.parent_module(db) {
78-
parent_id.name(db) == "src"
79-
} else {
80-
false
81-
}
82-
}
83-
})
78+
.find(|module_id| module_id.name(db) == "main")
8479
.copied();
8580

8681
Analysis {
@@ -103,3 +98,63 @@ pub fn ingot_main_module(db: &dyn AnalyzerDb, ingot_id: IngotId) -> Analysis<Opt
10398
}),
10499
}
105100
}
101+
102+
pub fn ingot_lib_module(db: &dyn AnalyzerDb, ingot_id: IngotId) -> Analysis<Option<ModuleId>> {
103+
let lib_id = ingot_id
104+
.all_modules(db)
105+
.iter()
106+
.find(|module_id| module_id.name(db) == "lib")
107+
.copied();
108+
109+
Analysis {
110+
value: lib_id,
111+
diagnostics: Rc::new({
112+
if lib_id.is_none() {
113+
vec![Diagnostic {
114+
severity: Severity::Error,
115+
message: format!(
116+
"The ingot named \"{}\" is missing a lib module. \
117+
\nPlease add a `src/lib.fe` file to the base directory.",
118+
ingot_id.name(db)
119+
),
120+
labels: vec![],
121+
notes: vec![],
122+
}]
123+
} else {
124+
vec![]
125+
}
126+
}),
127+
}
128+
}
129+
130+
pub fn ingot_root_module(db: &dyn AnalyzerDb, ingot_id: IngotId) -> Option<ModuleId> {
131+
db.ingot_main_module(ingot_id)
132+
.value
133+
.or(db.ingot_lib_module(ingot_id).value)
134+
}
135+
136+
pub fn ingot_root_sub_modules(
137+
db: &dyn AnalyzerDb,
138+
ingot_id: IngotId,
139+
) -> Rc<IndexMap<SmolStr, ModuleId>> {
140+
let modules = ingot_id
141+
.all_modules(db)
142+
.iter()
143+
.filter(|module_id| {
144+
if Some(**module_id) == ingot_id.root_module(db) {
145+
false
146+
} else {
147+
let mut in_src = false;
148+
if let Some(parent) = Path::new(&module_id.ingot_path(db).to_string()).parent() {
149+
if let Some(name) = parent.file_name() {
150+
in_src = name == "src"
151+
}
152+
}
153+
in_src
154+
}
155+
})
156+
.map(|module_id| (module_id.name(db), *module_id))
157+
.collect();
158+
159+
Rc::new(modules)
160+
}

0 commit comments

Comments
 (0)