Skip to content

Commit

Permalink
misc: reworked compiler driver to always work with a project
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra committed Feb 5, 2021
1 parent b8ccc83 commit dc4a4a6
Show file tree
Hide file tree
Showing 33 changed files with 465 additions and 461 deletions.
5 changes: 4 additions & 1 deletion crates/mun_compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ keywords = ["game", "hot-reloading", "language", "mun", "scripting"]
categories = ["game-development", "mun"]

[dependencies]
rustc-hash = "1.1.0"
anyhow = "1.0.31"
mun_codegen = { version = "=0.2.0", path="../mun_codegen" }
mun_syntax = { version = "=0.2.0", path="../mun_syntax" }
hir = { version="=0.2.0", path="../mun_hir", package="mun_hir" }
vfs = { path = "../mun_vfs", package="mun_vfs" }
paths = {path="../mun_paths", package="mun_paths"}
mun_target = { version = "=0.2.0", path="../mun_target" }
mun_project = { version = "=0.1.0", path = "../mun_project" }
project = { path = "../mun_project", package="mun_project" }
mun_diagnostics = { version = "=0.1.0", path = "../mun_diagnostics" }
annotate-snippets = { version = "0.9.0", features = ["color"] }
unicode-segmentation = "1.6.0"
Expand All @@ -30,3 +32,4 @@ lockfile = "0.2.2"

[dev-dependencies]
insta = "0.16"
tempdir = "0.3.7"
26 changes: 9 additions & 17 deletions crates/mun_compiler/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::Config;
use hir::{salsa, HirDatabase, Upcast};
use mun_codegen::{CodeGenDatabase, CodeGenDatabaseStorage};
use mun_codegen::{CodeGenDatabase, CodeGenDatabaseStorage, OptimizationLevel};
use mun_target::spec::Target;

/// A compiler database is a salsa database that enables increment compilation.
/// A compiler database is a salsa database that enables increment compilation. Its a combination of
/// databases from different packages that together enable incremental compilation.
#[salsa::database(
hir::SourceDatabaseStorage,
hir::InternDatabaseStorage,
Expand Down Expand Up @@ -45,24 +46,15 @@ impl Upcast<dyn CodeGenDatabase> for CompilerDatabase {
}
}

impl CompilerDatabase {
/// Constructs a new database
pub fn new(config: &Config) -> Self {
let mut db = CompilerDatabase {
impl Default for CompilerDatabase {
fn default() -> Self {
let mut db = Self {
storage: Default::default(),
};

// Set the initial configuration
db.set_config(config);

db.set_target(Target::host_target().unwrap());
db.set_optimization_level(OptimizationLevel::Default);
db
}

/// Applies the given configuration to the database
pub fn set_config(&mut self, config: &Config) {
self.set_target(config.target.clone());
self.set_optimization_level(config.optimization_lvl);
}
}

impl salsa::Database for CompilerDatabase {}
40 changes: 33 additions & 7 deletions crates/mun_compiler/src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(test)]
mod tests {
use crate::{Config, DisplayColor, Driver, PathOrInline, RelativePathBuf};
use crate::{Config, DisplayColor, Driver};
use std::convert::TryFrom;
use std::io::Cursor;

/// Compile passed source code and return all compilation errors
Expand All @@ -10,12 +11,37 @@ mod tests {
..Config::default()
};

let input = PathOrInline::Inline {
rel_path: RelativePathBuf::from("main.mun"),
contents: source_code.to_owned(),
};

let (driver, _) = Driver::with_file(config, input).unwrap();
let package_dir = tempdir::TempDir::new("").unwrap();
std::fs::write(
package_dir.path().join(project::MANIFEST_FILENAME),
"[package]\nname=\"test\"\nversion=\"0.0.0\"",
)
.unwrap();

let source_dir = package_dir.path().join(project::SOURCE_DIR);
std::fs::create_dir_all(&source_dir).unwrap();
std::fs::write(
source_dir
.join(project::MODULE_FILE_STEM)
.with_extension(project::MODULE_FILE_EXTENSION),
source_code,
)
.unwrap();

let driver = Driver::from_manifest(
project::ProjectManifest::from_manifest_path(
paths::AbsPathBuf::try_from(
package_dir
.path()
.join(project::MANIFEST_FILENAME)
.to_path_buf(),
)
.unwrap(),
)
.unwrap(),
config,
)
.unwrap();

let mut compilation_errors = Vec::<u8>::new();

Expand Down
2 changes: 1 addition & 1 deletion crates/mun_compiler/src/diagnostics_snippets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,5 @@ fn emit_diagnostic(

// Build a display list and emit to the writer
let dl = DisplayList::from(snippet);
write!(writer, "{}", dl)
writeln!(writer, "{}", dl)
}
Loading

0 comments on commit dc4a4a6

Please sign in to comment.