Skip to content

Commit

Permalink
Added external files.
Browse files Browse the repository at this point in the history
commit-id:dd8a62c8
  • Loading branch information
orizi committed Aug 19, 2024
1 parent de9af0a commit 039fad4
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 14 deletions.
5 changes: 3 additions & 2 deletions crates/cairo-lang-compiler/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,6 +38,7 @@ pub struct RootDatabase {
storage: salsa::Storage<RootDatabase>,
}
impl salsa::Database for RootDatabase {}
impl ExternalFiles for RootDatabase {}
impl salsa::ParallelDatabase for RootDatabase {
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
salsa::Snapshot::new(RootDatabase { storage: self.storage.snapshot() })
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-compiler/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl<'a> DiagnosticsReporter<'a> {
))
}
FileLongId::Virtual(_) => panic!("Missing virtual file."),
FileLongId::External(_) => panic!("Missing external file."),
}
found_diagnostics = true;
}
Expand Down
4 changes: 3 additions & 1 deletion crates/cairo-lang-defs/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -30,6 +31,7 @@ pub struct DatabaseForTesting {
storage: salsa::Storage<DatabaseForTesting>,
}
impl salsa::Database for DatabaseForTesting {}
impl ExternalFiles for DatabaseForTesting {}
impl Default for DatabaseForTesting {
fn default() -> Self {
let mut res = Self { storage: Default::default() };
Expand Down
21 changes: 20 additions & 1 deletion crates/cairo-lang-filesystem/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<str>> {
None
}

/// Returns the name of an external file.
fn ext_file_name(&self, _external_id: u32) -> String {
"<external>".to_string()
}

/// Returns the full path of an external file.
fn ext_file_full_path(&self, _external_id: u32) -> String {
"<external>".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]
Expand Down Expand Up @@ -244,6 +262,7 @@ fn priv_raw_file_content(db: &dyn FilesGroup, file: FileId) -> Option<Arc<str>>
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<Arc<str>> {
Expand Down
4 changes: 4 additions & 0 deletions crates/cairo-lang-filesystem/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -131,18 +132,21 @@ impl<'b> FileId {
path.file_name().and_then(|x| x.to_str()).unwrap_or("<unknown>").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("<unknown>").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,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/cairo-lang-filesystem/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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)]
pub struct FilesDatabaseForTesting {
storage: salsa::Storage<FilesDatabaseForTesting>,
}
impl salsa::Database for FilesDatabaseForTesting {}
impl ExternalFiles for FilesDatabaseForTesting {}
impl Default for FilesDatabaseForTesting {
fn default() -> Self {
let mut res = Self { storage: Default::default() };
Expand Down
3 changes: 2 additions & 1 deletion crates/cairo-lang-formatter/src/test.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +16,7 @@ pub struct DatabaseImpl {
storage: salsa::Storage<DatabaseImpl>,
}
impl salsa::Database for DatabaseImpl {}
impl ExternalFiles for DatabaseImpl {}
impl Upcast<dyn FilesGroup> for DatabaseImpl {
fn upcast(&self) -> &(dyn FilesGroup + 'static) {
self
Expand Down
5 changes: 4 additions & 1 deletion crates/cairo-lang-language-server/src/lang/db/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<Self> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ pub trait LsProtoGroup: Upcast<dyn FilesGroup> {
url.path_segments_mut().unwrap().push(&format!("{}.cairo", virtual_file.name));
url
}
FileLongId::External(id) => {
unimplemented!("External files are not supported yet: {id}")
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/cairo-lang-lowering/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -26,6 +26,7 @@ pub struct LoweringDatabaseForTesting {
storage: salsa::Storage<LoweringDatabaseForTesting>,
}
impl salsa::Database for LoweringDatabaseForTesting {}
impl ExternalFiles for LoweringDatabaseForTesting {}
impl salsa::ParallelDatabase for LoweringDatabaseForTesting {
fn snapshot(&self) -> salsa::Snapshot<LoweringDatabaseForTesting> {
salsa::Snapshot::new(LoweringDatabaseForTesting { storage: self.storage.snapshot() })
Expand Down
3 changes: 2 additions & 1 deletion crates/cairo-lang-parser/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -18,6 +18,7 @@ pub struct SimpleParserDatabase {
storage: salsa::Storage<SimpleParserDatabase>,
}
impl salsa::Database for SimpleParserDatabase {}
impl ExternalFiles for SimpleParserDatabase {}
impl Default for SimpleParserDatabase {
fn default() -> Self {
let mut res = Self { storage: Default::default() };
Expand Down
4 changes: 3 additions & 1 deletion crates/cairo-lang-plugins/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,6 +50,7 @@ pub struct DatabaseForTesting {
storage: salsa::Storage<DatabaseForTesting>,
}
impl salsa::Database for DatabaseForTesting {}
impl ExternalFiles for DatabaseForTesting {}
impl Default for DatabaseForTesting {
fn default() -> Self {
let mut res = Self { storage: Default::default() };
Expand Down
4 changes: 3 additions & 1 deletion crates/cairo-lang-semantic/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
3 changes: 2 additions & 1 deletion crates/cairo-lang-semantic/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -30,6 +30,7 @@ pub struct SemanticDatabaseForTesting {
storage: salsa::Storage<SemanticDatabaseForTesting>,
}
impl salsa::Database for SemanticDatabaseForTesting {}
impl ExternalFiles for SemanticDatabaseForTesting {}
impl salsa::ParallelDatabase for SemanticDatabaseForTesting {
fn snapshot(&self) -> salsa::Snapshot<SemanticDatabaseForTesting> {
salsa::Snapshot::new(SemanticDatabaseForTesting { storage: self.storage.snapshot() })
Expand Down
4 changes: 3 additions & 1 deletion crates/cairo-lang-sierra-generator/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +42,7 @@ pub struct SierraGenDatabaseForTesting {
storage: salsa::Storage<SierraGenDatabaseForTesting>,
}
impl salsa::Database for SierraGenDatabaseForTesting {}
impl ExternalFiles for SierraGenDatabaseForTesting {}
impl salsa::ParallelDatabase for SierraGenDatabaseForTesting {
fn snapshot(&self) -> salsa::Snapshot<SierraGenDatabaseForTesting> {
salsa::Snapshot::new(SierraGenDatabaseForTesting { storage: self.storage.snapshot() })
Expand Down
3 changes: 2 additions & 1 deletion crates/cairo-lang-syntax/src/node/test_utils.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,6 +9,7 @@ pub struct DatabaseForTesting {
storage: salsa::Storage<DatabaseForTesting>,
}
impl salsa::Database for DatabaseForTesting {}
impl ExternalFiles for DatabaseForTesting {}
impl Upcast<dyn FilesGroup> for DatabaseForTesting {
fn upcast(&self) -> &(dyn FilesGroup + 'static) {
self
Expand Down

0 comments on commit 039fad4

Please sign in to comment.