|
| 1 | +use std::collections::{HashMap, VecDeque}; |
| 2 | +use std::path::PathBuf; |
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use crate::error::{Error, ErrorCollector, RichError, Span}; |
| 6 | +use crate::parse::{self, ParseFromStrWithErrors}; |
| 7 | +use crate::resolution::{CanonPath, DependencyMap, SourceFile}; |
| 8 | + |
| 9 | +/// Represents a single, isolated file in the SimplicityHL project. |
| 10 | +/// In this architecture, a file and a module are the exact same thing. |
| 11 | +#[derive(Debug, Clone)] |
| 12 | +pub struct Module { |
| 13 | + pub source: SourceFile, |
| 14 | + /// The completely parsed program for this specific file. |
| 15 | + /// it contains all the functions, aliases, and imports defined inside the file. |
| 16 | + pub parsed_program: parse::Program, |
| 17 | +} |
| 18 | + |
| 19 | +/// The Dependency Graph itself. |
| 20 | +pub struct ProjectGraph { |
| 21 | + /// Arena Pattern: the data itself lives here. |
| 22 | + /// A flat vector guarantees that module data is stored contiguously in memory. |
| 23 | + #[expect(dead_code)] |
| 24 | + pub(self) modules: Vec<Module>, |
| 25 | + |
| 26 | + /// The configuration environment. |
| 27 | + /// Used to resolve external library dependencies and invoke their associated functions. |
| 28 | + pub dependency_map: Arc<DependencyMap>, |
| 29 | + |
| 30 | + /// Fast lookup: `CanonPath` -> Module ID. |
| 31 | + /// A reverse index mapping absolute file paths to their internal IDs. |
| 32 | + /// This solves the duplication problem, ensuring each file is only parsed once. |
| 33 | + pub lookup: HashMap<CanonPath, usize>, |
| 34 | + |
| 35 | + /// Fast lookup: Module ID -> `CanonPath`. |
| 36 | + /// A direct index mapping internal IDs back to their absolute file paths. |
| 37 | + /// This serves as the exact inverse of the `lookup` map. |
| 38 | + pub paths: Arc<[CanonPath]>, |
| 39 | + |
| 40 | + /// The Adjacency List: Defines the Directed acyclic Graph (DAG) of imports. |
| 41 | + /// |
| 42 | + /// The Key (`usize`) is the ID of a "Parent" module (the file doing the importing). |
| 43 | + /// The Value (`Vec<usize>`) is a list of IDs of the "Child" modules it relies on. |
| 44 | + /// |
| 45 | + /// Example: If `main.simf` (ID: 0) has `use lib::math;` (ID: 1) and `use lib::io;` (ID: 2), |
| 46 | + /// this map will contain: `{ 0: [1, 2] }`. |
| 47 | + pub dependencies: HashMap<usize, Vec<usize>>, |
| 48 | +} |
| 49 | + |
| 50 | +impl ProjectGraph { |
| 51 | + /// This helper cleanly encapsulates the process of loading source text, parsing it |
| 52 | + /// into an `parse::Program`, and combining them so the compiler can easily work with the file. |
| 53 | + /// If the file is missing or contains syntax errors, it logs the diagnostic to the |
| 54 | + /// `ErrorCollector` and safely returns `None`. |
| 55 | + fn parse_and_get_program( |
| 56 | + path: &CanonPath, |
| 57 | + importer_source: SourceFile, |
| 58 | + span: Span, |
| 59 | + handler: &mut ErrorCollector, |
| 60 | + ) -> Option<Module> { |
| 61 | + let Ok(content) = std::fs::read_to_string(path.as_path()) else { |
| 62 | + let err = RichError::new(Error::FileNotFound(PathBuf::from(path.as_path())), span) |
| 63 | + .with_source(importer_source.clone()); |
| 64 | + |
| 65 | + handler.push(err); |
| 66 | + return None; |
| 67 | + }; |
| 68 | + |
| 69 | + let dep_source_file = SourceFile::new(path.as_path(), Arc::from(content.clone())); |
| 70 | + |
| 71 | + parse::Program::parse_from_str_with_errors(&dep_source_file, handler).map( |
| 72 | + |parsed_program| Module { |
| 73 | + source: dep_source_file, |
| 74 | + parsed_program, |
| 75 | + }, |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + /// Initializes a new `ProjectGraph` by parsing the root program and discovering all dependencies. |
| 80 | + /// |
| 81 | + /// Performs a BFS to recursively parse `use` statements, |
| 82 | + /// building a DAG of the project's modules. |
| 83 | + /// |
| 84 | + /// # Arguments |
| 85 | + /// |
| 86 | + /// * `root_source` - The `SourceFile` representing the entry point of the project. |
| 87 | + /// * `dependency_map` - The context-aware mapping rules used to resolve external imports. |
| 88 | + /// * `root_program` - A reference to the already-parsed AST of the root file. |
| 89 | + /// * `handler` - The diagnostics collector used to record resolution and parsing errors. |
| 90 | + /// |
| 91 | + /// # Returns |
| 92 | + /// |
| 93 | + /// * `Ok(Some(Self))` - If the entire project graph was successfully resolved and parsed. |
| 94 | + /// * `Ok(None)` - If the graph traversal completed, but one or more modules contained |
| 95 | + /// errors (which have been safely logged into the `handler`). |
| 96 | + /// |
| 97 | + /// # Errors |
| 98 | + /// |
| 99 | + /// This function will return an `Err(String)` only for critical internal compiler errors |
| 100 | + /// (e.g., if a provided `SourceFile` is unexpectedly missing its underlying file path). |
| 101 | + pub fn new( |
| 102 | + root_source: SourceFile, |
| 103 | + dependency_map: Arc<DependencyMap>, |
| 104 | + root_program: &parse::Program, |
| 105 | + handler: &mut ErrorCollector, |
| 106 | + ) -> Result<Option<Self>, String> { |
| 107 | + let root_name = if let Some(root_name) = root_source.name() { |
| 108 | + CanonPath::canonicalize(root_name)? |
| 109 | + } else { |
| 110 | + return Err( |
| 111 | + "The root_source variable inside the ProjectGraph::new() function has no name" |
| 112 | + .to_string(), |
| 113 | + ); |
| 114 | + }; |
| 115 | + |
| 116 | + let mut modules: Vec<Module> = vec![Module { |
| 117 | + source: root_source, |
| 118 | + parsed_program: root_program.clone(), |
| 119 | + }]; |
| 120 | + |
| 121 | + let mut lookup: HashMap<CanonPath, usize> = HashMap::new(); |
| 122 | + let mut paths: Vec<CanonPath> = vec![root_name.clone()]; |
| 123 | + let mut dependencies: HashMap<usize, Vec<usize>> = HashMap::new(); |
| 124 | + |
| 125 | + let root_id = 0; |
| 126 | + lookup.insert(root_name, root_id); |
| 127 | + dependencies.insert(root_id, Vec::new()); |
| 128 | + |
| 129 | + // Implementation of the standard BFS algorithm with memoization and queue |
| 130 | + let mut queue = VecDeque::new(); |
| 131 | + queue.push_back(root_id); |
| 132 | + |
| 133 | + while let Some(curr_id) = queue.pop_front() { |
| 134 | + // We need this to report errors inside THIS file. |
| 135 | + let importer_source = modules[curr_id].source.clone(); |
| 136 | + let importer_source_name = if let Some(name) = importer_source.name() { |
| 137 | + CanonPath::canonicalize(name)? |
| 138 | + } else { |
| 139 | + return Err(format!( |
| 140 | + "The {:?} variable inside the ProjectGraph::new() function has no name", |
| 141 | + importer_source |
| 142 | + )); |
| 143 | + }; |
| 144 | + |
| 145 | + let current_program = &modules[curr_id].parsed_program; |
| 146 | + |
| 147 | + // Lists to separate valid logic from errors |
| 148 | + let mut valid_imports: Vec<(CanonPath, Span)> = Vec::new(); |
| 149 | + let mut resolution_errors: Vec<RichError> = Vec::new(); |
| 150 | + |
| 151 | + // PHASE 1: Resolve Imports |
| 152 | + for elem in current_program.items() { |
| 153 | + if let parse::Item::Use(use_decl) = elem { |
| 154 | + match dependency_map.resolve_path(importer_source_name.clone(), use_decl) { |
| 155 | + Ok(path) => valid_imports.push((path, *use_decl.span())), |
| 156 | + Err(err) => { |
| 157 | + resolution_errors.push(err.with_source(importer_source.clone())) |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // PHASE 2: Load and Parse Dependencies |
| 164 | + for (path, import_span) in valid_imports { |
| 165 | + if let Some(&existing_id) = lookup.get(&path) { |
| 166 | + let deps = dependencies.entry(curr_id).or_default(); |
| 167 | + if !deps.contains(&existing_id) { |
| 168 | + deps.push(existing_id); |
| 169 | + } |
| 170 | + continue; |
| 171 | + } |
| 172 | + |
| 173 | + let Some(module) = ProjectGraph::parse_and_get_program( |
| 174 | + &path, |
| 175 | + importer_source.clone(), |
| 176 | + import_span, |
| 177 | + handler, |
| 178 | + ) else { |
| 179 | + continue; |
| 180 | + }; |
| 181 | + |
| 182 | + let last_ind = modules.len(); |
| 183 | + modules.push(module); |
| 184 | + |
| 185 | + lookup.insert(path.clone(), last_ind); |
| 186 | + paths.push(path); |
| 187 | + dependencies.entry(curr_id).or_default().push(last_ind); |
| 188 | + |
| 189 | + queue.push_back(last_ind); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + Ok(if handler.has_errors() { |
| 194 | + None |
| 195 | + } else { |
| 196 | + Some(Self { |
| 197 | + modules, |
| 198 | + dependency_map, |
| 199 | + lookup, |
| 200 | + paths: paths.into(), |
| 201 | + dependencies, |
| 202 | + }) |
| 203 | + }) |
| 204 | + } |
| 205 | +} |
0 commit comments