Skip to content

Commit

Permalink
std lib with dummy fn
Browse files Browse the repository at this point in the history
  • Loading branch information
g-r-a-n-t committed Dec 29, 2021
1 parent 98967b5 commit 407609e
Show file tree
Hide file tree
Showing 40 changed files with 1,180 additions and 638 deletions.
51 changes: 50 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions crates/analyzer/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use crate::namespace::items::{
};
use crate::namespace::types;
use fe_common::Span;
use fe_parser::ast;
use fe_parser::node::Node;
use indexmap::map::IndexMap;
use smol_str::SmolStr;
use std::rc::Rc;
Expand Down Expand Up @@ -59,6 +57,12 @@ pub trait AnalyzerDb {
fn ingot_all_modules(&self, ingot: IngotId) -> Rc<Vec<ModuleId>>;
#[salsa::invoke(queries::ingots::ingot_main_module)]
fn ingot_main_module(&self, ingot: IngotId) -> Analysis<Option<ModuleId>>;
#[salsa::invoke(queries::ingots::ingot_lib_module)]
fn ingot_lib_module(&self, ingot: IngotId) -> Analysis<Option<ModuleId>>;
#[salsa::invoke(queries::ingots::ingot_root_module)]
fn ingot_root_module(&self, ingot: IngotId) -> Option<ModuleId>;
#[salsa::invoke(queries::ingots::ingot_root_sub_modules)]
fn ingot_root_sub_modules(&self, ingot: IngotId) -> Rc<IndexMap<SmolStr, ModuleId>>;

// Module
#[salsa::invoke(queries::module::module_all_items)]
Expand All @@ -74,16 +78,8 @@ pub trait AnalyzerDb {
&self,
module: ModuleId,
) -> Analysis<Rc<IndexMap<SmolStr, (Span, Item)>>>;
#[salsa::invoke(queries::module::module_resolve_use_tree)]
fn module_resolve_use_tree(
&self,
module: ModuleId,
tree: Node<ast::UseTree>,
) -> Analysis<Rc<IndexMap<SmolStr, (Span, Item)>>>;
#[salsa::invoke(queries::module::module_parent_module)]
fn module_parent_module(&self, module: ModuleId) -> Option<ModuleId>;
#[salsa::invoke(queries::module::module_adjacent_modules)]
fn module_adjacent_modules(&self, module: ModuleId) -> Rc<IndexMap<SmolStr, ModuleId>>;
#[salsa::invoke(queries::module::module_sub_modules)]
fn module_sub_modules(&self, module: ModuleId) -> Rc<IndexMap<SmolStr, ModuleId>>;

Expand Down
107 changes: 81 additions & 26 deletions crates/analyzer/src/db/queries/ingots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use crate::namespace::items::{IngotId, Module, ModuleContext, ModuleFileContent,
use crate::AnalyzerDb;
use fe_common::diagnostics::{Diagnostic, Severity};
use fe_parser::ast;
use indexmap::map::IndexMap;
use indexmap::set::IndexSet;
use smol_str::SmolStr;
use std::path::Path;
use std::rc::Rc;

Expand Down Expand Up @@ -42,25 +44,26 @@ pub fn ingot_all_modules(db: &dyn AnalyzerDb, ingot_id: IngotId) -> Rc<Vec<Modul
})
.collect::<IndexSet<_>>()
.into_iter()
.map(|dir| {
let module = Module {
name: dir
.file_name()
.expect("missing file name")
.to_str()
.expect("could not convert dir name to string")
.into(),
ast: ast::Module { body: vec![] },
context: ModuleContext::Ingot(ingot_id),
file_content: ModuleFileContent::Dir {
dir_path: dir
.filter_map(|dir| {
if let Some(file_name) = dir.file_name() {
let module = Module {
name: file_name
.to_str()
.expect("could not convert dir path to string")
.expect("could not convert dir name to string")
.into(),
},
};

db.intern_module(Rc::new(module))
ast: ast::Module { body: vec![] },
context: ModuleContext::Ingot(ingot_id),
file_content: ModuleFileContent::Dir {
dir_path: dir
.to_str()
.expect("could not convert dir path to string")
.into(),
},
};
Some(db.intern_module(Rc::new(module)))
} else {
None
}
})
.collect::<Vec<ModuleId>>();

Expand All @@ -72,15 +75,7 @@ pub fn ingot_main_module(db: &dyn AnalyzerDb, ingot_id: IngotId) -> Analysis<Opt
let main_id = ingot_id
.all_modules(db)
.iter()
.find(|module_id| {
module_id.name(db) == "main" && {
if let Some(parent_id) = module_id.parent_module(db) {
parent_id.name(db) == "src"
} else {
false
}
}
})
.find(|module_id| module_id.name(db) == "main")
.copied();

Analysis {
Expand All @@ -103,3 +98,63 @@ pub fn ingot_main_module(db: &dyn AnalyzerDb, ingot_id: IngotId) -> Analysis<Opt
}),
}
}

pub fn ingot_lib_module(db: &dyn AnalyzerDb, ingot_id: IngotId) -> Analysis<Option<ModuleId>> {
let lib_id = ingot_id
.all_modules(db)
.iter()
.find(|module_id| module_id.name(db) == "lib")
.copied();

Analysis {
value: lib_id,
diagnostics: Rc::new({
if lib_id.is_none() {
vec![Diagnostic {
severity: Severity::Error,
message: format!(
"The ingot named \"{}\" is missing a lib module. \
\nPlease add a `src/lib.fe` file to the base directory.",
ingot_id.name(db)
),
labels: vec![],
notes: vec![],
}]
} else {
vec![]
}
}),
}
}

pub fn ingot_root_module(db: &dyn AnalyzerDb, ingot_id: IngotId) -> Option<ModuleId> {
db.ingot_main_module(ingot_id)
.value
.or(db.ingot_lib_module(ingot_id).value)
}

pub fn ingot_root_sub_modules(
db: &dyn AnalyzerDb,
ingot_id: IngotId,
) -> Rc<IndexMap<SmolStr, ModuleId>> {
let modules = ingot_id
.all_modules(db)
.iter()
.filter(|module_id| {
if Some(**module_id) == ingot_id.root_module(db) {
false
} else {
let mut in_src = false;
if let Some(parent) = Path::new(&module_id.ingot_path(db).to_string()).parent() {
if let Some(name) = parent.file_name() {
in_src = name == "src"
}
}
in_src
}
})
.map(|module_id| (module_id.name(db), *module_id))
.collect();

Rc::new(modules)
}
Loading

0 comments on commit 407609e

Please sign in to comment.