Skip to content

Commit

Permalink
feat(ast_codegen): add derive_clone_in generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Aug 7, 2024
1 parent c4abb28 commit c497446
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 1 deletion.
6 changes: 6 additions & 0 deletions crates/oxc_ast/src/generated/derive_clone_in.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Auto-generated code, DO NOT EDIT DIRECTLY!
// To edit this generated file you have to edit `tasks/ast_codegen/src/generators/derive_clone_in.rs`

use oxc_allocator::{Allocator, CloneIn};
use crate::ast::*;

121 changes: 121 additions & 0 deletions tasks/ast_codegen/src/generators/derive_clone_in.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use itertools::Itertools;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::Ident;

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

use super::{define_generator, generated_header, Generator};

define_generator! {
pub struct DeriveCloneIn;
}

impl Generator for DeriveCloneIn {
fn name(&self) -> &'static str {
stringify!(DeriveCloneIn)
}

fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
let impls: Vec<TokenStream> = ctx
.schema
.definitions
.iter()
.filter(|def| def.generates_derive("CloneIn"))
.map(|def| match &def {
TypeDef::Enum(it) => derive_enum(it),
TypeDef::Struct(it) => derive_struct(it),
})
.collect();

let header = generated_header!();

GeneratorOutput::Stream((
output(crate::AST_CRATE, "derive_clone_in.rs"),
quote! {
#header

use oxc_allocator::{Allocator, CloneIn};
use crate::ast::*;
endl!();

#(#impls)*
},
))
}
}

fn derive_enum(def: &EnumDef) -> TokenStream {
let ty_ident = def.ident();
impl_clone_in(&ty_ident, def.has_lifetime, || {
let mut used_alloc = false;
let matches = def
.all_variants()
.map(|var| {
let ident = var.ident();
if var.is_unit() {
quote!(Self :: #ident => Self :: Cloned :: #ident)
} else {
used_alloc = true;
quote!(Self :: #ident(it) => Self :: Cloned :: #ident(it.clone_in(alloc)))
}
})
.collect_vec();
let alloc_ident = if used_alloc { format_ident!("alloc") } else { format_ident!("_") };
(
alloc_ident,
quote! {
match self {
#(#matches),*
}
},
)
})
}

fn derive_struct(def: &StructDef) -> TokenStream {
let ty_ident = def.ident();
impl_clone_in(&ty_ident, def.has_lifetime, || {
let (alloc_ident, body) = if def.fields.is_empty() {
(format_ident!("_"), TokenStream::default())
} else {
let fields = def.fields.iter().map(|field| {
let ident = field.ident();
quote!(#ident: self.#ident.clone_in(alloc))
});
(format_ident!("alloc"), quote!({ #(#fields),* }))
};
(
alloc_ident,
quote! {
#ty_ident #body
},
)
})
}

fn impl_clone_in<F>(ty_ident: &Ident, has_lifetime: bool, body: F) -> TokenStream
where
F: FnOnce() -> (/* allocator name */ Ident, TokenStream),
{
let (alloc, body) = body();
let (old_lifetime, new_lifetime) = if has_lifetime {
(quote!(<'old_alloc>), quote!(<'new_alloc>))
} else {
Default::default()
};

quote! {
impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for #ty_ident #old_lifetime {
type Cloned = #ty_ident #new_lifetime;

fn clone_in(&self, #alloc: &'new_alloc Allocator) -> Self::Cloned {
#body
}
}
}
}
2 changes: 2 additions & 0 deletions tasks/ast_codegen/src/generators/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod assert_layouts;
mod ast_builder;
mod ast_kind;
mod derive_clone_in;
mod impl_get_span;
mod visit;

Expand Down Expand Up @@ -43,6 +44,7 @@ pub(crate) use insert;
pub use assert_layouts::AssertLayouts;
pub use ast_builder::AstBuilderGenerator;
pub use ast_kind::AstKindGenerator;
pub use derive_clone_in::DeriveCloneIn;
pub use impl_get_span::ImplGetSpanGenerator;
pub use visit::{VisitGenerator, VisitMutGenerator};

Expand Down
3 changes: 2 additions & 1 deletion tasks/ast_codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod util;

use fmt::{cargo_fmt, pprint};
use generators::{
AssertLayouts, AstBuilderGenerator, AstKindGenerator, Generator, VisitGenerator,
AssertLayouts, AstBuilderGenerator, AstKindGenerator, DeriveCloneIn, Generator, VisitGenerator,
VisitMutGenerator,
};
use passes::{CalcLayout, Linker, Pass};
Expand Down Expand Up @@ -297,6 +297,7 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
.gen(AssertLayouts)
.gen(AstKindGenerator)
.gen(AstBuilderGenerator)
.gen(DeriveCloneIn)
.gen(ImplGetSpanGenerator)
.gen(VisitGenerator)
.gen(VisitMutGenerator)
Expand Down
15 changes: 15 additions & 0 deletions tasks/ast_codegen/src/schema/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ impl EnumDef {
pub fn all_variants(&self) -> impl Iterator<Item = &VariantDef> {
self.variants.iter().chain(self.inherits.iter().flat_map(|it| it.variants.iter()))
}

/// Are all the variants in this enum unit?
/// Example:
/// ```
/// enum E { A, B, C, D }
///
/// ```
///
pub fn is_unit(&self) -> bool {
self.all_variants().all(VariantDef::is_unit)
}
}

#[derive(Debug, Serialize)]
Expand All @@ -93,6 +104,10 @@ impl VariantDef {
pub fn ident(&self) -> syn::Ident {
self.name.to_ident()
}

pub fn is_unit(&self) -> bool {
self.fields.is_empty()
}
}

#[derive(Debug, Serialize)]
Expand Down

0 comments on commit c497446

Please sign in to comment.