Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions tasks/ast_tools/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use crate::{
#[derive(Default)]
pub struct AstCodegen {
files: Vec<PathBuf>,
passes: Vec<Box<dyn Runner<Output = (), Context = EarlyCtx>>>,
generators: Vec<Box<dyn Runner<Output = Output, Context = LateCtx>>>,
derives: Vec<Box<dyn Runner<Output = Vec<Output>, Context = LateCtx>>>,
passes: Vec<Box<dyn Runner<Context = EarlyCtx>>>,
generators: Vec<Box<dyn Runner<Context = LateCtx>>>,
derives: Vec<Box<dyn Runner<Context = LateCtx>>>,
}

pub struct AstCodegenResult {
Expand All @@ -29,10 +29,9 @@ pub struct AstCodegenResult {

pub trait Runner {
type Context;
type Output;
fn name(&self) -> &'static str;
fn file_path(&self) -> &'static str;
fn run(&mut self, ctx: &Self::Context) -> Result<Self::Output>;
fn run(&mut self, ctx: &Self::Context) -> Result<Vec<Output>>;
}

pub struct EarlyCtx {
Expand Down Expand Up @@ -115,7 +114,7 @@ impl AstCodegen {
#[must_use]
pub fn pass<P>(mut self, pass: P) -> Self
where
P: Pass + Runner<Output = (), Context = EarlyCtx> + 'static,
P: Pass + Runner<Context = EarlyCtx> + 'static,
{
self.passes.push(Box::new(pass));
self
Expand All @@ -124,7 +123,7 @@ impl AstCodegen {
#[must_use]
pub fn generate<G>(mut self, generator: G) -> Self
where
G: Generator + Runner<Output = Output, Context = LateCtx> + 'static,
G: Generator + Runner<Context = LateCtx> + 'static,
{
self.generators.push(Box::new(generator));
self
Expand All @@ -133,7 +132,7 @@ impl AstCodegen {
#[must_use]
pub fn derive<D>(mut self, derive: D) -> Self
where
D: Derive + Runner<Output = Vec<Output>, Context = LateCtx> + 'static,
D: Derive + Runner<Context = LateCtx> + 'static,
{
self.derives.push(Box::new(derive));
self
Expand Down Expand Up @@ -182,22 +181,26 @@ impl AstCodegen {
})
.flatten_ok();

let outputs = self.generators.into_iter().map(|mut runner| {
let name = runner.name();
log!("Generate {name}... ");
let result = runner.run(&ctx);
match result {
Ok(output) => {
logln!("Done!");
let generator_path = runner.file_path();
Ok(output.output(generator_path))
}
Err(err) => {
logln!("FAILED");
Err(err)
let outputs = self
.generators
.into_iter()
.map(|mut runner| {
let name = runner.name();
log!("Generate {name}... ");
let result = runner.run(&ctx);
match result {
Ok(outputs) => {
logln!("Done!");
let generator_path = runner.file_path();
Ok(outputs.into_iter().map(|output| output.output(generator_path)))
}
Err(err) => {
logln!("FAILED");
Err(err)
}
}
}
});
})
.flatten_ok();

let outputs = derives.chain(outputs).collect::<Result<Vec<_>>>()?;

Expand Down
1 change: 0 additions & 1 deletion tasks/ast_tools/src/derives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ macro_rules! define_derive {

impl $($lifetime)? Runner for $ident $($lifetime)? {
type Context = LateCtx;
type Output = Vec<Output>;

fn name(&self) -> &'static str {
stringify!($ident)
Expand Down
7 changes: 3 additions & 4 deletions tasks/ast_tools/src/generators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub trait Generator {

// Standard methods

fn output(&mut self, ctx: &LateCtx) -> Result<Output> {
Ok(self.generate(ctx))
fn output(&mut self, ctx: &LateCtx) -> Result<Vec<Output>> {
Ok(vec![self.generate(ctx)])
}
}

Expand All @@ -35,7 +35,6 @@ macro_rules! define_generator {

impl $($lifetime)? Runner for $ident $($lifetime)? {
type Context = LateCtx;
type Output = Output;

fn name(&self) -> &'static str {
stringify!($ident)
Expand All @@ -45,7 +44,7 @@ macro_rules! define_generator {
file!()
}

fn run(&mut self, ctx: &LateCtx) -> Result<Output> {
fn run(&mut self, ctx: &LateCtx) -> Result<Vec<Output>> {
self.output(ctx)
}
}
Expand Down
10 changes: 5 additions & 5 deletions tasks/ast_tools/src/passes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::VecDeque;

use crate::{codegen::EarlyCtx, rust_ast::AstType, Result};
use crate::{codegen::EarlyCtx, output::Output, rust_ast::AstType, Result};

mod calc_layout;
mod linker;
Expand All @@ -17,7 +17,7 @@ pub trait Pass {
// Standard methods

/// Run pass.
fn output(&mut self, ctx: &EarlyCtx) -> Result<()> {
fn output(&mut self, ctx: &EarlyCtx) -> Result<Vec<Output>> {
// We sort by `TypeId`, so we have the same ordering as it's written in Rust source
let mut unresolved = ctx.chronological_idents().collect::<VecDeque<_>>();

Expand All @@ -31,7 +31,7 @@ pub trait Pass {
unresolved.push_front(next);
}
}
Ok(())
Ok(vec![])
}
}

Expand All @@ -40,12 +40,12 @@ macro_rules! define_pass {
const _: () = {
use $crate::{
codegen::{EarlyCtx, Runner},
output::Output,
Result,
};

impl $($lifetime)? Runner for $ident $($lifetime)? {
type Context = EarlyCtx;
type Output = ();

fn name(&self) -> &'static str {
stringify!($ident)
Expand All @@ -55,7 +55,7 @@ macro_rules! define_pass {
file!()
}

fn run(&mut self, ctx: &Self::Context) -> Result<()> {
fn run(&mut self, ctx: &Self::Context) -> Result<Vec<Output>> {
self.output(ctx)
}
}
Expand Down