Skip to main content

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 rustc_hash::FxHashMap as HashMap;
7
8use gruel_util::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_util::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
34/// Path-based predicate: the file lives inside the top-level `prelude/`
35/// directory. Used by the `@lang(...)` privilege check (Phase 1+).
36pub fn is_prelude_path(path: &str) -> bool {
37    // Match either the embedded virtual prefix `prelude/` (no leading
38    // path component) or any path with a `/prelude/` segment (on-disk
39    // workspace paths). Reject paths whose prelude segment sits under
40    // `std/prelude/` — that layout was retired in ADR-0079, and a
41    // residual `std/prelude/` directory is not the privileged one.
42    if path.contains("std/prelude/") || path.contains("std\\prelude\\") {
43        return false;
44    }
45    path.starts_with("prelude/")
46        || path.starts_with("prelude\\")
47        || path.contains("/prelude/")
48        || path.contains("\\prelude\\")
49}