Skip to content

Commit

Permalink
Revert "refactor(ast_codegen): declutter the main file."
Browse files Browse the repository at this point in the history
This reverts commit c92252f.
  • Loading branch information
rzvxa committed Aug 7, 2024
1 parent 0a24337 commit a3663f3
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 307 deletions.
156 changes: 0 additions & 156 deletions tasks/ast_codegen/src/codegen.rs

This file was deleted.

8 changes: 4 additions & 4 deletions tasks/ast_codegen/src/generators/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use quote::{format_ident, quote};
use syn::Type;

use crate::{
codegen::LateCtx,
output,
schema::{FieldDef, ToType, TypeDef},
Generator, GeneratorOutput,
Generator, GeneratorOutput, LateCtx,
};

use super::{define_generator, generated_header};
Expand All @@ -22,8 +21,9 @@ impl Generator for AssertLayouts {

fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
let (assertions_64, assertions_32) = ctx
.schema()
.into_iter()
.schema
.definitions
.iter()
.map(|def| {
let typ = def.to_type_elide();
assert_type(&typ, def)
Expand Down
8 changes: 4 additions & 4 deletions tasks/ast_codegen/src/generators/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ use quote::{format_ident, quote, ToTokens};
use syn::{parse_quote, Ident, Type};

use crate::{
codegen::LateCtx,
generators::generated_header,
output,
schema::{
EnumDef, FieldDef, GetIdent, InheritDef, StructDef, ToType, TypeDef, TypeName, VariantDef,
},
util::{TypeAnalysis, TypeWrapper},
Generator, GeneratorOutput,
Generator, GeneratorOutput, LateCtx,
};

use super::define_generator;
Expand All @@ -32,8 +31,9 @@ impl Generator for AstBuilderGenerator {

fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
let fns = ctx
.schema()
.into_iter()
.schema
.definitions
.iter()
.filter(|it| it.visitable())
.map(|it| generate_builder_fn(it, ctx))
.collect_vec();
Expand Down
7 changes: 3 additions & 4 deletions tasks/ast_codegen/src/generators/ast_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use quote::quote;
use syn::{parse_quote, Arm, Ident, Type, Variant};

use crate::{
codegen::LateCtx,
output,
schema::{GetIdent, ToType, TypeDef},
util::ToIdent,
Generator, GeneratorOutput,
Generator, GeneratorOutput, LateCtx,
};

use super::{define_generator, generated_header};
Expand Down Expand Up @@ -142,8 +141,8 @@ impl Generator for AstKindGenerator {

fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
let have_kinds: Vec<(Ident, Type)> = ctx
.schema()
.into_iter()
.schema.definitions
.iter()
.filter(|it| it.visitable())
.filter(
|maybe_kind| matches!(maybe_kind, kind @ (TypeDef::Enum(_) | TypeDef::Struct(_)) if kind.visitable())
Expand Down
8 changes: 4 additions & 4 deletions tasks/ast_codegen/src/generators/derive_clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use quote::{format_ident, quote};
use syn::Ident;

use crate::{
codegen::LateCtx,
output,
schema::{EnumDef, GetIdent, StructDef, TypeDef},
GeneratorOutput,
GeneratorOutput, LateCtx,
};

use super::{define_generator, generated_header, Generator};
Expand All @@ -23,8 +22,9 @@ impl Generator for DeriveCloneIn {

fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
let impls: Vec<TokenStream> = ctx
.schema()
.into_iter()
.schema
.definitions
.iter()
.filter(|def| def.generates_derive("CloneIn"))
.map(|def| match &def {
TypeDef::Enum(it) => derive_enum(it),
Expand Down
8 changes: 4 additions & 4 deletions tasks/ast_codegen/src/generators/derive_get_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use quote::{format_ident, quote};
use syn::{Generics, Ident, Type};

use crate::{
codegen::LateCtx,
output,
schema::{EnumDef, GetGenerics, StructDef, ToType, TypeDef},
util::ToIdent,
Generator, GeneratorOutput,
Generator, GeneratorOutput, LateCtx,
};

use super::{define_generator, generated_header};
Expand Down Expand Up @@ -89,8 +88,9 @@ fn derive<const MUT: bool>(ctx: &LateCtx) -> TokenStream {
)
};
let impls: Vec<TokenStream> = ctx
.schema()
.into_iter()
.schema
.definitions
.iter()
.filter(|def| def.visitable())
.map(|def| match &def {
TypeDef::Enum(it) => derive_enum(it),
Expand Down
84 changes: 4 additions & 80 deletions tasks/ast_codegen/src/generators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ macro_rules! generated_header {
}};
}

use std::path::PathBuf;

pub(crate) use generated_header;
pub(crate) use insert;

Expand All @@ -48,101 +46,27 @@ pub use ast_builder::AstBuilderGenerator;
pub use ast_kind::AstKindGenerator;
pub use derive_clone_in::DeriveCloneIn;
pub use derive_get_span::{DeriveGetSpan, DeriveGetSpanMut};
use proc_macro2::TokenStream;
pub use visit::{VisitGenerator, VisitMutGenerator};

use crate::codegen::LateCtx;
use crate::{GeneratorOutput, LateCtx};

pub trait Generator {
fn name(&self) -> &'static str;
fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput;
}

pub type GeneratedTokenStream = (/* output path */ PathBuf, TokenStream);
pub type GeneratedDataStream = (/* output path */ PathBuf, Vec<u8>);

// TODO: remove me
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub enum GeneratorOutput {
None,
Info(Vec<u8>),
Data(GeneratedDataStream),
Stream(GeneratedTokenStream),
}

// TODO: remove me
#[allow(dead_code)]
impl GeneratorOutput {
pub fn is_none(&self) -> bool {
matches!(self, Self::None)
}

pub fn expect_none(&self) {
assert!(self.is_none());
}

pub fn to_info(&self) -> &[u8] {
if let Self::Info(it) = self {
it
} else {
panic!();
}
}

pub fn to_data(&self) -> &GeneratedDataStream {
if let Self::Data(it) = self {
it
} else {
panic!();
}
}

pub fn to_stream(&self) -> &GeneratedTokenStream {
if let Self::Stream(it) = self {
it
} else {
panic!();
}
}

pub fn into_info(self) -> Vec<u8> {
if let Self::Info(it) = self {
it
} else {
panic!();
}
}

pub fn into_data(self) -> GeneratedDataStream {
if let Self::Data(it) = self {
it
} else {
panic!();
}
}

pub fn into_stream(self) -> GeneratedTokenStream {
if let Self::Stream(it) = self {
it
} else {
panic!();
}
}
}

macro_rules! define_generator {
($vis:vis struct $ident:ident $($lifetime:lifetime)? $($rest:tt)*) => {
$vis struct $ident $($lifetime)? $($rest)*
impl $($lifetime)? $crate::codegen::Runner for $ident $($lifetime)? {
type Context = $crate::codegen::LateCtx;
impl $($lifetime)? $crate::Runner for $ident $($lifetime)? {
type Context = $crate::LateCtx;
type Output = $crate::GeneratorOutput;

fn name(&self) -> &'static str {
$crate::Generator::name(self)
}

fn run(&mut self, ctx: &$crate::codegen::LateCtx) -> $crate::Result<Self::Output> {
fn run(&mut self, ctx: &$crate::LateCtx) -> $crate::Result<Self::Output> {
Ok(self.generate(ctx))
}
}
Expand Down
Loading

0 comments on commit a3663f3

Please sign in to comment.