diff --git a/crates/cairo-lang-compiler/src/db.rs b/crates/cairo-lang-compiler/src/db.rs index 9ea7b152eb0..1be808ec563 100644 --- a/crates/cairo-lang-compiler/src/db.rs +++ b/crates/cairo-lang-compiler/src/db.rs @@ -5,8 +5,8 @@ use cairo_lang_defs::db::{DefsDatabase, DefsGroup}; use cairo_lang_defs::plugin::{InlineMacroExprPlugin, MacroPlugin}; use cairo_lang_filesystem::cfg::CfgSet; use cairo_lang_filesystem::db::{ - init_dev_corelib, init_files_group, AsFilesGroupMut, FilesDatabase, FilesGroup, FilesGroupEx, - CORELIB_CRATE_NAME, CORELIB_VERSION, + init_dev_corelib, init_files_group, AsFilesGroupMut, ExternalFiles, FilesDatabase, FilesGroup, + FilesGroupEx, CORELIB_CRATE_NAME, CORELIB_VERSION, }; use cairo_lang_filesystem::detect::detect_corelib; use cairo_lang_filesystem::flag::Flag; @@ -38,6 +38,7 @@ pub struct RootDatabase { storage: salsa::Storage, } impl salsa::Database for RootDatabase {} +impl ExternalFiles for RootDatabase {} impl salsa::ParallelDatabase for RootDatabase { fn snapshot(&self) -> salsa::Snapshot { salsa::Snapshot::new(RootDatabase { storage: self.storage.snapshot() }) diff --git a/crates/cairo-lang-compiler/src/diagnostics.rs b/crates/cairo-lang-compiler/src/diagnostics.rs index c7cc45ec203..ca16afabdca 100644 --- a/crates/cairo-lang-compiler/src/diagnostics.rs +++ b/crates/cairo-lang-compiler/src/diagnostics.rs @@ -130,6 +130,7 @@ impl<'a> DiagnosticsReporter<'a> { )) } FileLongId::Virtual(_) => panic!("Missing virtual file."), + FileLongId::External(_) => panic!("Missing external file."), } found_diagnostics = true; } diff --git a/crates/cairo-lang-defs/src/test.rs b/crates/cairo-lang-defs/src/test.rs index e27f4671673..09ad4be70b4 100644 --- a/crates/cairo-lang-defs/src/test.rs +++ b/crates/cairo-lang-defs/src/test.rs @@ -3,7 +3,8 @@ use std::sync::Arc; use cairo_lang_debug::debug::DebugWithDb; use cairo_lang_filesystem::db::{ - init_files_group, AsFilesGroupMut, CrateConfiguration, FilesDatabase, FilesGroup, FilesGroupEx, + init_files_group, AsFilesGroupMut, CrateConfiguration, ExternalFiles, FilesDatabase, + FilesGroup, FilesGroupEx, }; use cairo_lang_filesystem::ids::{CrateLongId, Directory, FileLongId}; use cairo_lang_parser::db::{ParserDatabase, ParserGroup}; @@ -30,6 +31,7 @@ pub struct DatabaseForTesting { storage: salsa::Storage, } impl salsa::Database for DatabaseForTesting {} +impl ExternalFiles for DatabaseForTesting {} impl Default for DatabaseForTesting { fn default() -> Self { let mut res = Self { storage: Default::default() }; diff --git a/crates/cairo-lang-filesystem/src/db.rs b/crates/cairo-lang-filesystem/src/db.rs index 08b9eede342..ea602884124 100644 --- a/crates/cairo-lang-filesystem/src/db.rs +++ b/crates/cairo-lang-filesystem/src/db.rs @@ -107,9 +107,27 @@ pub struct ExperimentalFeaturesConfig { pub coupons: bool, } +/// A trait for defining files external to the `filesystem` crate. +pub trait ExternalFiles { + /// Returns the content of an external file. + fn ext_file_content(&self, _external_id: u32) -> Option> { + None + } + + /// Returns the name of an external file. + fn ext_file_name(&self, _external_id: u32) -> String { + "".to_string() + } + + /// Returns the full path of an external file. + fn ext_file_full_path(&self, _external_id: u32) -> String { + "".to_string() + } +} + // Salsa database interface. #[salsa::query_group(FilesDatabase)] -pub trait FilesGroup { +pub trait FilesGroup: ExternalFiles { #[salsa::interned] fn intern_crate(&self, crt: CrateLongId) -> CrateId; #[salsa::interned] @@ -244,6 +262,7 @@ fn priv_raw_file_content(db: &dyn FilesGroup, file: FileId) -> Option> Err(_) => None, }, FileLongId::Virtual(virt) => Some(virt.content), + FileLongId::External(external_id) => db.ext_file_content(external_id), } } fn file_content(db: &dyn FilesGroup, file: FileId) -> Option> { diff --git a/crates/cairo-lang-filesystem/src/ids.rs b/crates/cairo-lang-filesystem/src/ids.rs index 8426b0a45ad..746099c59d3 100644 --- a/crates/cairo-lang-filesystem/src/ids.rs +++ b/crates/cairo-lang-filesystem/src/ids.rs @@ -62,6 +62,7 @@ impl FlagId { pub enum FileLongId { OnDisk(PathBuf), Virtual(VirtualFile), + External(u32), } /// Whether the file holds syntax for a module or for an expression. #[derive(Clone, Debug, Hash, PartialEq, Eq)] @@ -131,18 +132,21 @@ impl<'b> FileId { path.file_name().and_then(|x| x.to_str()).unwrap_or("").to_string() } FileLongId::Virtual(vf) => vf.name.to_string(), + FileLongId::External(external_id) => db.ext_file_name(external_id), } } pub fn full_path(self, db: &dyn FilesGroup) -> String { match self.lookup_intern(db) { FileLongId::OnDisk(path) => path.to_str().unwrap_or("").to_string(), FileLongId::Virtual(vf) => vf.full_path(db), + FileLongId::External(external_id) => db.ext_file_full_path(external_id), } } pub fn kind(self, db: &dyn FilesGroup) -> FileKind { match self.lookup_intern(db) { FileLongId::OnDisk(_) => FileKind::Module, FileLongId::Virtual(vf) => vf.kind.clone(), + FileLongId::External(_) => FileKind::Module, } } } diff --git a/crates/cairo-lang-filesystem/src/test_utils.rs b/crates/cairo-lang-filesystem/src/test_utils.rs index ca1eb9792ab..5b01bbdcdab 100644 --- a/crates/cairo-lang-filesystem/src/test_utils.rs +++ b/crates/cairo-lang-filesystem/src/test_utils.rs @@ -1,6 +1,6 @@ use cairo_lang_utils::Upcast; -use crate::db::{init_files_group, AsFilesGroupMut, FilesDatabase, FilesGroup}; +use crate::db::{init_files_group, AsFilesGroupMut, ExternalFiles, FilesDatabase, FilesGroup}; // Test salsa database. #[salsa::database(FilesDatabase)] @@ -8,6 +8,7 @@ pub struct FilesDatabaseForTesting { storage: salsa::Storage, } impl salsa::Database for FilesDatabaseForTesting {} +impl ExternalFiles for FilesDatabaseForTesting {} impl Default for FilesDatabaseForTesting { fn default() -> Self { let mut res = Self { storage: Default::default() }; diff --git a/crates/cairo-lang-formatter/src/test.rs b/crates/cairo-lang-formatter/src/test.rs index 4e96a679952..b8f598dc3f9 100644 --- a/crates/cairo-lang-formatter/src/test.rs +++ b/crates/cairo-lang-formatter/src/test.rs @@ -1,7 +1,7 @@ use std::fs; use std::path::PathBuf; -use cairo_lang_filesystem::db::{FilesDatabase, FilesGroup}; +use cairo_lang_filesystem::db::{ExternalFiles, FilesDatabase, FilesGroup}; use cairo_lang_parser::utils::{get_syntax_root_and_diagnostics_from_file, SimpleParserDatabase}; use cairo_lang_syntax::node::db::SyntaxDatabase; use cairo_lang_utils::Upcast; @@ -16,6 +16,7 @@ pub struct DatabaseImpl { storage: salsa::Storage, } impl salsa::Database for DatabaseImpl {} +impl ExternalFiles for DatabaseImpl {} impl Upcast for DatabaseImpl { fn upcast(&self) -> &(dyn FilesGroup + 'static) { self diff --git a/crates/cairo-lang-language-server/src/lang/db/mod.rs b/crates/cairo-lang-language-server/src/lang/db/mod.rs index 3412bdd4ba8..9fafd1c010a 100644 --- a/crates/cairo-lang-language-server/src/lang/db/mod.rs +++ b/crates/cairo-lang-language-server/src/lang/db/mod.rs @@ -1,7 +1,9 @@ use cairo_lang_defs::db::{DefsDatabase, DefsGroup}; use cairo_lang_doc::db::DocDatabase; use cairo_lang_filesystem::cfg::{Cfg, CfgSet}; -use cairo_lang_filesystem::db::{init_files_group, AsFilesGroupMut, FilesDatabase, FilesGroup}; +use cairo_lang_filesystem::db::{ + init_files_group, AsFilesGroupMut, ExternalFiles, FilesDatabase, FilesGroup, +}; use cairo_lang_lowering::db::{init_lowering_group, LoweringDatabase, LoweringGroup}; use cairo_lang_lowering::utils::InliningStrategy; use cairo_lang_parser::db::{ParserDatabase, ParserGroup}; @@ -71,6 +73,7 @@ impl AnalysisDatabase { } impl salsa::Database for AnalysisDatabase {} +impl ExternalFiles for AnalysisDatabase {} impl salsa::ParallelDatabase for AnalysisDatabase { fn snapshot(&self) -> salsa::Snapshot { diff --git a/crates/cairo-lang-language-server/src/lang/lsp/ls_proto_group.rs b/crates/cairo-lang-language-server/src/lang/lsp/ls_proto_group.rs index f35fe27d837..7d56d5d1fd0 100644 --- a/crates/cairo-lang-language-server/src/lang/lsp/ls_proto_group.rs +++ b/crates/cairo-lang-language-server/src/lang/lsp/ls_proto_group.rs @@ -52,6 +52,9 @@ pub trait LsProtoGroup: Upcast { url.path_segments_mut().unwrap().push(&format!("{}.cairo", virtual_file.name)); url } + FileLongId::External(id) => { + unimplemented!("External files are not supported yet: {id}") + } } } } diff --git a/crates/cairo-lang-lowering/src/test_utils.rs b/crates/cairo-lang-lowering/src/test_utils.rs index 316f614b28e..bcdccb36385 100644 --- a/crates/cairo-lang-lowering/src/test_utils.rs +++ b/crates/cairo-lang-lowering/src/test_utils.rs @@ -2,7 +2,7 @@ use std::sync::{LazyLock, Mutex}; use cairo_lang_defs::db::{DefsDatabase, DefsGroup}; use cairo_lang_filesystem::db::{ - init_dev_corelib, init_files_group, AsFilesGroupMut, FilesDatabase, FilesGroup, + init_dev_corelib, init_files_group, AsFilesGroupMut, ExternalFiles, FilesDatabase, FilesGroup, }; use cairo_lang_filesystem::detect::detect_corelib; use cairo_lang_parser::db::{ParserDatabase, ParserGroup}; @@ -26,6 +26,7 @@ pub struct LoweringDatabaseForTesting { storage: salsa::Storage, } impl salsa::Database for LoweringDatabaseForTesting {} +impl ExternalFiles for LoweringDatabaseForTesting {} impl salsa::ParallelDatabase for LoweringDatabaseForTesting { fn snapshot(&self) -> salsa::Snapshot { salsa::Snapshot::new(LoweringDatabaseForTesting { storage: self.storage.snapshot() }) diff --git a/crates/cairo-lang-parser/src/utils.rs b/crates/cairo-lang-parser/src/utils.rs index 522d0905f95..1c054c620e8 100644 --- a/crates/cairo-lang-parser/src/utils.rs +++ b/crates/cairo-lang-parser/src/utils.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use cairo_lang_diagnostics::{Diagnostics, DiagnosticsBuilder}; -use cairo_lang_filesystem::db::{init_files_group, FilesDatabase, FilesGroup}; +use cairo_lang_filesystem::db::{init_files_group, ExternalFiles, FilesDatabase, FilesGroup}; use cairo_lang_filesystem::ids::{FileId, FileKind, FileLongId, VirtualFile}; use cairo_lang_syntax::node::ast::SyntaxFile; use cairo_lang_syntax::node::db::{SyntaxDatabase, SyntaxGroup}; @@ -18,6 +18,7 @@ pub struct SimpleParserDatabase { storage: salsa::Storage, } impl salsa::Database for SimpleParserDatabase {} +impl ExternalFiles for SimpleParserDatabase {} impl Default for SimpleParserDatabase { fn default() -> Self { let mut res = Self { storage: Default::default() }; diff --git a/crates/cairo-lang-plugins/src/test.rs b/crates/cairo-lang-plugins/src/test.rs index fb85546eeca..85d7ff2a261 100644 --- a/crates/cairo-lang-plugins/src/test.rs +++ b/crates/cairo-lang-plugins/src/test.rs @@ -7,7 +7,8 @@ use cairo_lang_defs::plugin::{ }; use cairo_lang_filesystem::cfg::CfgSet; use cairo_lang_filesystem::db::{ - init_files_group, AsFilesGroupMut, CrateConfiguration, FilesDatabase, FilesGroup, FilesGroupEx, + init_files_group, AsFilesGroupMut, CrateConfiguration, ExternalFiles, FilesDatabase, + FilesGroup, FilesGroupEx, }; use cairo_lang_filesystem::ids::{CrateLongId, Directory, FileLongId}; use cairo_lang_parser::db::ParserDatabase; @@ -49,6 +50,7 @@ pub struct DatabaseForTesting { storage: salsa::Storage, } impl salsa::Database for DatabaseForTesting {} +impl ExternalFiles for DatabaseForTesting {} impl Default for DatabaseForTesting { fn default() -> Self { let mut res = Self { storage: Default::default() }; diff --git a/crates/cairo-lang-semantic/src/db.rs b/crates/cairo-lang-semantic/src/db.rs index 48dd38bedf9..04744d4543a 100644 --- a/crates/cairo-lang-semantic/src/db.rs +++ b/crates/cairo-lang-semantic/src/db.rs @@ -1627,7 +1627,9 @@ fn module_semantic_diagnostics( let path = match file_id.lookup_intern(db) { FileLongId::OnDisk(path) => path.display().to_string(), - FileLongId::Virtual(_) => panic!("Expected OnDisk file."), + FileLongId::Virtual(_) | FileLongId::External(_) => { + panic!("Expected OnDisk file.") + } }; let stable_location = diff --git a/crates/cairo-lang-semantic/src/test_utils.rs b/crates/cairo-lang-semantic/src/test_utils.rs index 2baaa01e52a..1a7cf997713 100644 --- a/crates/cairo-lang-semantic/src/test_utils.rs +++ b/crates/cairo-lang-semantic/src/test_utils.rs @@ -6,7 +6,7 @@ use cairo_lang_defs::ids::{FunctionWithBodyId, ModuleId, SubmoduleId, SubmoduleL use cairo_lang_diagnostics::{Diagnostics, DiagnosticsBuilder}; use cairo_lang_filesystem::db::{ init_dev_corelib, init_files_group, AsFilesGroupMut, CrateConfiguration, CrateSettings, - Edition, ExperimentalFeaturesConfig, FilesDatabase, FilesGroup, + Edition, ExperimentalFeaturesConfig, ExternalFiles, FilesDatabase, FilesGroup, }; use cairo_lang_filesystem::detect::detect_corelib; use cairo_lang_filesystem::ids::{ @@ -30,6 +30,7 @@ pub struct SemanticDatabaseForTesting { storage: salsa::Storage, } impl salsa::Database for SemanticDatabaseForTesting {} +impl ExternalFiles for SemanticDatabaseForTesting {} impl salsa::ParallelDatabase for SemanticDatabaseForTesting { fn snapshot(&self) -> salsa::Snapshot { salsa::Snapshot::new(SemanticDatabaseForTesting { storage: self.storage.snapshot() }) diff --git a/crates/cairo-lang-sierra-generator/src/test_utils.rs b/crates/cairo-lang-sierra-generator/src/test_utils.rs index 96024f05363..2fb0b989610 100644 --- a/crates/cairo-lang-sierra-generator/src/test_utils.rs +++ b/crates/cairo-lang-sierra-generator/src/test_utils.rs @@ -3,7 +3,8 @@ use std::sync::{Arc, LazyLock, Mutex}; use cairo_lang_defs::db::{DefsDatabase, DefsGroup}; use cairo_lang_defs::ids::ModuleId; use cairo_lang_filesystem::db::{ - init_dev_corelib, init_files_group, AsFilesGroupMut, FilesDatabase, FilesGroup, FilesGroupEx, + init_dev_corelib, init_files_group, AsFilesGroupMut, ExternalFiles, FilesDatabase, FilesGroup, + FilesGroupEx, }; use cairo_lang_filesystem::detect::detect_corelib; use cairo_lang_filesystem::flag::Flag; @@ -41,6 +42,7 @@ pub struct SierraGenDatabaseForTesting { storage: salsa::Storage, } impl salsa::Database for SierraGenDatabaseForTesting {} +impl ExternalFiles for SierraGenDatabaseForTesting {} impl salsa::ParallelDatabase for SierraGenDatabaseForTesting { fn snapshot(&self) -> salsa::Snapshot { salsa::Snapshot::new(SierraGenDatabaseForTesting { storage: self.storage.snapshot() }) diff --git a/crates/cairo-lang-syntax/src/node/test_utils.rs b/crates/cairo-lang-syntax/src/node/test_utils.rs index fb75a881cc0..3a5ee3bc289 100644 --- a/crates/cairo-lang-syntax/src/node/test_utils.rs +++ b/crates/cairo-lang-syntax/src/node/test_utils.rs @@ -1,4 +1,4 @@ -use cairo_lang_filesystem::db::{FilesDatabase, FilesGroup}; +use cairo_lang_filesystem::db::{ExternalFiles, FilesDatabase, FilesGroup}; use cairo_lang_utils::Upcast; use super::db::SyntaxDatabase; @@ -9,6 +9,7 @@ pub struct DatabaseForTesting { storage: salsa::Storage, } impl salsa::Database for DatabaseForTesting {} +impl ExternalFiles for DatabaseForTesting {} impl Upcast for DatabaseForTesting { fn upcast(&self) -> &(dyn FilesGroup + 'static) { self