Skip to content

Commit

Permalink
Finished ability for functions to be imported from modules in standar…
Browse files Browse the repository at this point in the history
…d modules folder
  • Loading branch information
IsaacShelton committed Oct 5, 2024
1 parent 121f3ee commit ca52e47
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 56 deletions.
3 changes: 2 additions & 1 deletion src/ast/structure/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::Type;
use crate::{name::Name, source_files::Source};
use derive_more::IsVariant;
use indexmap::IndexMap;

#[derive(Clone, Debug)]
Expand All @@ -10,7 +11,7 @@ pub struct Structure {
pub source: Source,
}

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, IsVariant)]
pub enum Privacy {
#[default]
Public,
Expand Down
4 changes: 2 additions & 2 deletions src/ast/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ 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 imported_folders: Vec<Box<str>>,
pub namespace_to_dependency: HashMap<String, String>,
pub dependency_to_module: HashMap<String, FsNodeId>,
}

impl Default for Settings {
Expand All @@ -124,8 +124,8 @@ impl Default for Settings {
debug_skip_merging_helper_exprs: false,
imported_namespaces: vec![],
assume_int_at_least_32_bits: true,
imported_folders: vec![],
namespace_to_dependency: HashMap::new(),
dependency_to_module: HashMap::new(),
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/c/parser/speculate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

macro_rules! speculate {
($input:expr, $expression:expr) => {{
$input.speculate();
Expand Down
6 changes: 4 additions & 2 deletions src/c/preprocessor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ mod stdc;
- __DATE__
- etc.
*/
pub use self::ast::{Define, DefineKind};
pub use self::pre_token::{PreToken, PreTokenKind};
pub use self::{
ast::{Define, DefineKind},
pre_token::{PreToken, PreTokenKind},
};
use self::{
error::PreprocessorError,
expand::expand_ast,
Expand Down
10 changes: 4 additions & 6 deletions src/inflow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ mod into_inflow_stream;
mod tools;
mod try_peek;

pub use self::inflow_end::InflowEnd;
pub use self::inflow_stream::InflowStream;
pub use self::into_inflow::IntoInflow;
pub use self::into_inflow_stream::IntoInflowStream;
pub use self::tools::InflowTools;
pub use self::try_peek::TryPeek;
pub use self::{
inflow_end::InflowEnd, inflow_stream::InflowStream, into_inflow::IntoInflow,
into_inflow_stream::IntoInflowStream, tools::InflowTools, try_peek::TryPeek,
};

pub trait Inflow<T>: InflowStream<Item = T> {
fn un_next(&mut self, item: Self::Item);
Expand Down
9 changes: 5 additions & 4 deletions src/interpreter/memory/write.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::super::{error::InterpreterError, size_of::size_of, value::Value};
use super::Memory;
use crate::interpreter::value::StructLiteral;
use crate::ir;
use super::{
super::{error::InterpreterError, size_of::size_of, value::Value},
Memory,
};
use crate::{interpreter::value::StructLiteral, ir};

impl Memory {
pub fn write(
Expand Down
5 changes: 0 additions & 5 deletions src/interpreter/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ 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 imported_folders: Vec<Box<str>>,
pub namespace_to_dependency: HashMap<String, String>,
}

Expand All @@ -54,7 +53,6 @@ impl Default for BuildSystemSyscallHandler {
debug_skip_merging_helper_exprs: false,
imported_namespaces: vec![],
assume_int_at_least_32_bits: true,
imported_folders: vec![],
namespace_to_dependency: HashMap::new(),
}
}
Expand Down Expand Up @@ -157,9 +155,6 @@ impl SyscallHandler for BuildSystemSyscallHandler {
let as_namespace = read_cstring(memory, &args[0]);
let dependency_name = read_cstring(memory, &args[1]);

self.imported_folders
.push(dependency_name.to_string().into());

self.namespace_to_dependency
.insert(as_namespace, dependency_name);

Expand Down
3 changes: 1 addition & 2 deletions src/llvm_backend/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ use llvm_sys::{
},
prelude::{LLVMBasicBlockRef, LLVMBuilderRef, LLVMTypeRef, LLVMValueRef},
};
use std::{ffi::CStr, ptr::null_mut};

pub use load::Volatility;
pub use phi_relocation::PhiRelocation;
use std::{ffi::CStr, ptr::null_mut};

pub struct Builder {
builder: LLVMBuilderRef,
Expand Down
3 changes: 1 addition & 2 deletions src/llvm_backend/functions/param_values/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod inalloca;
mod indirect;
mod value;

pub use self::value::ParamValue;
use super::params_mapping::ParamRange;
use crate::{
ir,
Expand All @@ -16,8 +17,6 @@ use crate::{
};
use llvm_sys::prelude::LLVMValueRef;

pub use self::value::ParamValue;

pub struct ParamValues {
values: Vec<ParamValue>,
}
Expand Down
4 changes: 2 additions & 2 deletions src/pragma_section/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
workspace::fs::Fs,
};
use indexmap::IndexMap;
use std::path::Path;
use std::{collections::HashMap, path::Path};

impl PragmaSection {
pub fn run(
Expand Down Expand Up @@ -112,8 +112,8 @@ impl PragmaSection {
debug_skip_merging_helper_exprs: user_settings.debug_skip_merging_helper_exprs,
imported_namespaces: user_settings.imported_namespaces,
assume_int_at_least_32_bits: user_settings.assume_int_at_least_32_bits,
imported_folders: user_settings.imported_folders,
namespace_to_dependency: user_settings.namespace_to_dependency,
dependency_to_module: HashMap::new(),
})
}
}
3 changes: 3 additions & 0 deletions src/resolve/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ use crate::{
resolve_stmts, resolve_type,
},
resolved::{self, Expr, ExprKind, FunctionRef, StructureRef, TypeKind, TypedExpr},
workspace::fs::FsNodeId,
};
use ast::FloatSize;
pub use basic_binary_operation::resolve_basic_binary_operator;
use indexmap::IndexMap;
use resolved::IntegerKnown;
use std::collections::HashMap;

pub struct ResolveExprCtx<'a, 'b> {
pub resolved_ast: &'b mut resolved::Ast<'a>,
Expand All @@ -56,6 +58,7 @@ pub struct ResolveExprCtx<'a, 'b> {
pub resolved_function_ref: resolved::FunctionRef,
pub helper_exprs: &'b IndexMap<ResolvedName, &'a ast::HelperExpr>,
pub settings: &'b Settings,
pub public: &'b HashMap<FsNodeId, HashMap<String, Vec<resolved::FunctionRef>>>,
}

impl<'a, 'b> ResolveExprCtx<'a, 'b> {
Expand Down
22 changes: 22 additions & 0 deletions src/resolve/function_search_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ impl FunctionSearchCtx {
};
}

let mut remote_matches = (!name.namespace.is_empty())
.then(|| {
ctx.settings
.namespace_to_dependency
.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())
.filter(|f| Self::fits(ctx, **f, arguments, source));

if let Some(found) = remote_matches.next() {
return if remote_matches.next().is_some() {
Err(FindFunctionError::Ambiguous)
} else {
Ok(*found)
};
}

if name.namespace.is_empty() {
let mut matches = self
.imported_namespaces
Expand Down
23 changes: 22 additions & 1 deletion src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use function_search_ctx::FunctionSearchCtx;
use indexmap::IndexMap;
use std::{
borrow::Borrow,
collections::{HashSet, VecDeque},
collections::{HashMap, HashSet, VecDeque},
};

enum Job {
Expand All @@ -46,6 +46,7 @@ struct ResolveCtx<'a> {
pub function_search_ctxs: IndexMap<FsNodeId, FunctionSearchCtx>,
pub global_search_ctxs: IndexMap<FsNodeId, GlobalSearchCtx>,
pub helper_exprs: IndexMap<ResolvedName, &'a ast::HelperExpr>,
pub public: HashMap<FsNodeId, HashMap<String, Vec<resolved::FunctionRef>>>,
}

impl<'a> ResolveCtx<'a> {
Expand All @@ -56,6 +57,7 @@ impl<'a> ResolveCtx<'a> {
function_search_ctxs: Default::default(),
global_search_ctxs: Default::default(),
helper_exprs,
public: HashMap::new(),
}
}
}
Expand Down Expand Up @@ -271,6 +273,24 @@ pub fn resolve<'a>(
ctx.jobs
.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 function_name = function
.name
.as_plain_str()
.expect("cannot make public symbol with existing namespace");

if public_of_module.get(function_name).is_none() {
public_of_module.insert(function_name.to_string(), vec![]);
}

let functions_of_name = public_of_module
.get_mut(function_name)
.expect("function list inserted");
functions_of_name.push(function_ref);
}

let settings = file.settings.map(|id| &ast_workspace.settings[id.0]);
let imported_namespaces = settings.map(|settings| &settings.imported_namespaces);

Expand Down Expand Up @@ -366,6 +386,7 @@ pub fn resolve<'a>(
resolved_function_ref,
helper_exprs: &ctx.helper_exprs,
settings,
public: &ctx.public,
};

resolve_stmts(&mut ctx, &ast_function.stmts)?
Expand Down
14 changes: 11 additions & 3 deletions src/workspace/compile/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
parser::Input,
pragma_section::PragmaSection,
show::{into_show, Show},
source_files::Source,
source_files::{Source, SourceFileKey},
text::{IntoText, IntoTextStream},
token::{Token, TokenKind},
workspace::fs::Fs,
Expand All @@ -19,7 +19,15 @@ pub fn compile_module_file<'a>(
compiler: &Compiler<'a>,
_fs: &Fs,
path: &Path,
) -> Result<(usize, Input<'a, impl Inflow<Token> + 'a>, Settings), Box<dyn Show + 'a>> {
) -> Result<
(
usize,
Input<'a, impl Inflow<Token> + 'a>,
Settings,
SourceFileKey,
),
Box<dyn Show + 'a>,
> {
let content = std::fs::read_to_string(path)
.map_err(ErrorDiagnostic::plain)
.map_err(into_show)?;
Expand Down Expand Up @@ -56,5 +64,5 @@ pub fn compile_module_file<'a>(
)));
};

Ok((content.len(), input, settings))
Ok((content.len(), input, settings, key))
}
23 changes: 18 additions & 5 deletions src/workspace/explore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ use super::{
use append_only_vec::AppendOnlyVec;
use ignore::{WalkBuilder, WalkState};
use path_absolutize::Absolutize;
use std::{ffi::OsStr, fs::FileType, path::Path, time::UNIX_EPOCH};
use std::{
ffi::OsStr,
fs::FileType,
path::Path,
sync::atomic::{self, AtomicBool},
time::UNIX_EPOCH,
};

pub struct ExploreResult {
pub normal_files: Vec<NormalFile>,
pub module_files: Vec<ModuleFile>,
}

pub fn explore(fs: &Fs, folder_path: &Path) -> ExploreResult {
pub fn explore(fs: &Fs, folder_path: &Path) -> Option<ExploreResult> {
let normal_files = AppendOnlyVec::new();
let module_files = AppendOnlyVec::new();

Expand All @@ -35,12 +41,19 @@ pub fn explore(fs: &Fs, folder_path: &Path) -> ExploreResult {
.hidden(true) // Ignore hidden files
.build_parallel();

let ok = AtomicBool::new(true);

walker.run(|| {
let normal_files = &normal_files;
let module_files = &module_files;
let ok = &ok;

Box::new(move |entry| {
let entry = entry.unwrap();
let Ok(entry) = entry else {
ok.store(false, atomic::Ordering::SeqCst);
return WalkState::Quit;
};

let basename = entry.file_name();
let is_file = entry.file_type().as_ref().map_or(false, FileType::is_file);

Expand Down Expand Up @@ -86,8 +99,8 @@ pub fn explore(fs: &Fs, folder_path: &Path) -> ExploreResult {
})
});

ExploreResult {
ok.load(atomic::Ordering::SeqCst).then(|| ExploreResult {
normal_files: normal_files.into_vec(),
module_files: module_files.into_vec(),
}
})
}
Loading

0 comments on commit ca52e47

Please sign in to comment.