Skip to content

Commit

Permalink
fix: remove tight coupling between pipeline and session
Browse files Browse the repository at this point in the history
  • Loading branch information
junlarsen committed Feb 9, 2025
1 parent e13b2ce commit f46851d
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 26 deletions.
13 changes: 7 additions & 6 deletions compiler/eight-diagnostics/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use miette::{Diagnostic, NamedSource, Report};
use std::borrow::Cow;
use std::cmp::min;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Mutex;

#[derive(Debug)]
Expand All @@ -27,20 +28,20 @@ impl DiagnosticSource {
///
/// Diagnostics can either recoverable, unrecoverable (but still allow compilation to continue in
/// order to collect more diagnostics), or fatal (the compiler should exit immediately).
pub struct DiagnosticContext<'src> {
pub struct DiagnosticContext {
diagnostics: Mutex<Vec<Report>>,
src: &'src DiagnosticSource,
src: Rc<DiagnosticSource>,
}

impl<'src> DiagnosticContext<'src> {
impl DiagnosticContext {
/// Create a new diagnostic context.
///
/// The `max_diagnostic_count` parameter specifies the maximum number of diagnostics that can
/// be collected.
pub fn new(src: &'src DiagnosticSource, max_diagnostic_count: usize) -> Self {
pub fn new(src: Rc<DiagnosticSource>, max_diagnostic_count: usize) -> Self {
Self {
diagnostics: Mutex::new(Vec::with_capacity(min(max_diagnostic_count, 16))),
src,
diagnostics: Mutex::new(Vec::with_capacity(min(max_diagnostic_count, 16))),
}
}

Expand Down Expand Up @@ -75,7 +76,7 @@ impl<'src> DiagnosticContext<'src> {
.unwrap_or_else(|_| ice!("failed to acquire diagnostics lock"));

for report in diagnostics.drain(..) {
let (source_name, source) = match &self.src {
let (source_name, source) = match self.src.as_ref() {
DiagnosticSource::File(path, source) => (path.to_string_lossy(), source),
DiagnosticSource::Stdin(source) => (Cow::Borrowed("stdin"), source),
};
Expand Down
4 changes: 3 additions & 1 deletion compiler/eight-driver/src/bin/eightc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use eight_driver::pipeline::{
execute_compilation_pipeline, Pipeline, PipelineError, PipelineOptions, TerminationStep,
};
use eight_driver::query::{EmitQuery, QueryError};
use eight_middle::context::CompileSession;
use std::io::Read;
use std::path::PathBuf;

Expand Down Expand Up @@ -99,7 +100,8 @@ fn main() -> miette::Result<()> {
};

let options = args.try_into()?;
let pipeline = Pipeline::new(options, &source);
let session = CompileSession::new(source);
let pipeline = Pipeline::new(options, &session);
match execute_compilation_pipeline(&pipeline) {
Err(PipelineError::StopToken(msg)) => {
eprintln!("eightc: early termination due to: {}", msg);
Expand Down
2 changes: 1 addition & 1 deletion compiler/eight-driver/src/operations/ast_lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl<'c> PipelinePass<'c, AstTranslationUnit<'c>, HirModule<'c>> for AstLowerPas
pipeline: &'c Pipeline<'c>,
input: AstTranslationUnit<'c>,
) -> Result<HirModule<'c>, PipelineError> {
let mut lowering_pass = AstLoweringPass::new(&pipeline.session);
let mut lowering_pass = AstLoweringPass::new(pipeline.session);
let module = lowering_pass.visit_translation_unit(&input)?;
Ok(module)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/eight-driver/src/operations/hir_lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl<'c> PipelinePass<'c, HirModule<'c>, MirModule<'c>> for HirLowerPass {
pipeline: &'c Pipeline<'c>,
input: HirModule<'c>,
) -> Result<MirModule<'c>, PipelineError> {
let mut lowering_pass = HirModuleLoweringPass::new(&pipeline.session);
let mut lowering_pass = HirModuleLoweringPass::new(pipeline.session);
let module = lowering_pass.visit_module(&input)?;
Ok(module)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/eight-driver/src/operations/hir_simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl<'c> PipelinePass<'c, HirModule<'c>, HirModule<'c>> for HirSimplifyPass {
mut input: HirModule<'c>,
) -> Result<HirModule<'c>, PipelineError> {
let mut lowering_pass =
eight_middle::passes::hir_simplify_pass::HirSimplifyPass::new(&pipeline.session);
eight_middle::passes::hir_simplify_pass::HirSimplifyPass::new(pipeline.session);
lowering_pass.visit_module(&mut input);
Ok(input)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/eight-driver/src/operations/hir_type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl<'c> PipelinePass<'c, HirModule<'c>, HirModule<'c>> for HirTypeCheckPass {
pipeline: &'c Pipeline<'c>,
mut input: HirModule<'c>,
) -> Result<HirModule<'c>, PipelineError> {
let mut typing_context = TypingContext::new(&pipeline.session, input.signature);
let mut typing_context = TypingContext::new(pipeline.session, input.signature);
HirModuleTypeCheckerPass::visit(&mut input, &mut typing_context)?;
Ok(input)
}
Expand Down
12 changes: 5 additions & 7 deletions compiler/eight-driver/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,25 @@ pub struct PipelineOptions {
/// A compilation pipeline for the compiler.
pub struct Pipeline<'session> {
pub(crate) opts: PipelineOptions,
pub(crate) session: ManuallyDrop<CompileSession<'session>>,
pub(crate) session: &'session CompileSession<'session>,
pub(crate) ast_arena: ManuallyDrop<AstArena<'session>>,
source: &'session DiagnosticSource,
}

impl<'session> Pipeline<'session> {
pub fn new(opts: PipelineOptions, source: &'session DiagnosticSource) -> Self {
pub fn new(opts: PipelineOptions, session: &'session CompileSession<'session>) -> Self {
Self {
opts,
source,
session: ManuallyDrop::new(CompileSession::new(source)),
session,
ast_arena: ManuallyDrop::new(AstArena::default()),
}
}

/// Get the source for the pipeline.
pub fn source(&self) -> &DiagnosticSource {
self.source
self.session.src()
}

pub fn dcx(&self) -> &DiagnosticContext<'session> {
pub fn dcx(&self) -> &DiagnosticContext {
self.session.dcx()
}

Expand Down
23 changes: 15 additions & 8 deletions compiler/eight-middle/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ use std::rc::Rc;

/// A shared context for the middle-end and backend components.
pub struct CompileSession<'session> {
dcx: DiagnosticContext<'session>,
src: Rc<DiagnosticSource>,
dcx: DiagnosticContext,
allocator: Rc<Bump>,
strings: StringInterner<'session>,
mir_types: TypedInterner<'session, MirTyId, MirTy<'session>>,
hir_types: TypedInterner<'session, HirTyId, HirTy<'session>>,
}

impl<'be> CompileSession<'be> {
pub fn new(source: &'be DiagnosticSource) -> Self {
let dcx = DiagnosticContext::new(source, 16);
impl<'session> CompileSession<'session> {
pub fn new(source: DiagnosticSource) -> Self {
let src = Rc::new(source);
let dcx = DiagnosticContext::new(src.clone(), 16);
let alloc = Rc::new(Bump::new());
Self {
src,
dcx,
strings: StringInterner::new(alloc.clone()),
mir_types: TypedInterner::new(alloc.clone()),
Expand All @@ -34,25 +37,29 @@ impl<'be> CompileSession<'be> {
}

/// Get a reference to the diagnostic context.
pub fn dcx(&self) -> &DiagnosticContext<'be> {
pub fn dcx(&self) -> &DiagnosticContext {
&self.dcx
}

pub fn src(&self) -> &DiagnosticSource {
&self.src
}

/// Allocate a value into the arena.
///
/// This can be used to allocate things that should be dropped automatically when the compiler
/// exits, and that are fine being non-owning references.
pub fn arena_alloc<T>(&'be self, v: T) -> &'be T {
pub fn arena_alloc<T>(&'session self, v: T) -> &'session T {
self.allocator.alloc(v)
}

/// Intern a string into the context.
pub fn intern_str<T: AsRef<str>>(&'be self, name: T) -> &'be str {
pub fn intern_str<T: AsRef<str>>(&'session self, name: T) -> &'session str {
self.strings.get(name.as_ref())
}

/// Intern something that can be converted to a string into the context.
pub fn intern_as_str<T: ToString>(&'be self, name: T) -> &'be str {
pub fn intern_as_str<T: ToString>(&'session self, name: T) -> &'session str {
self.intern_str(name.to_string())
}
}
Expand Down

0 comments on commit f46851d

Please sign in to comment.