gruel_air/sema/
file_paths.rs

1//! File path management for multi-file compilation.
2//!
3//! This module handles mapping FileIds to source file paths, which is needed
4//! for module resolution and relative imports.
5
6use std::collections::HashMap;
7
8use gruel_span::FileId;
9
10use super::Sema;
11
12impl<'a> Sema<'a> {
13    /// Set file paths for module resolution in multi-file compilation.
14    ///
15    /// This maps FileIds to their corresponding source file paths,
16    /// enabling relative import resolution during @import.
17    pub fn set_file_paths(&mut self, file_paths: HashMap<FileId, String>) {
18        self.file_paths = file_paths;
19    }
20
21    /// Get the source file path for a span.
22    ///
23    /// Looks up the file path using the span's file_id.
24    pub(crate) fn get_source_path(&self, span: gruel_span::Span) -> Option<&str> {
25        self.file_paths.get(&span.file_id).map(|s| s.as_str())
26    }
27
28    /// Get the file path for a given FileId.
29    pub(crate) fn get_file_path(&self, file_id: FileId) -> Option<&str> {
30        self.file_paths.get(&file_id).map(|s| s.as_str())
31    }
32
33    /// Check if the compilation involves imports (multi-file compilation).
34    ///
35    /// When imports are present, lazy analysis is used to only analyze
36    /// functions reachable from main(). For single-file compilation,
37    /// eager analysis is used for backwards compatibility.
38    pub(crate) fn has_imports(&self) -> bool {
39        !self.module_registry.is_empty()
40    }
41}