mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(ast_codegen): add derive_clone_in generator. (#4731)
Follow-on after #4276, related to #4284.
This commit is contained in:
parent
2e91ad6c4e
commit
ec0b4cbdaa
5 changed files with 152 additions and 1 deletions
7
crates/oxc_ast/src/generated/derive_clone_in.rs
Normal file
7
crates/oxc_ast/src/generated/derive_clone_in.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
// 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::*;
|
||||
|
||||
126
tasks/ast_codegen/src/generators/derive_clone_in.rs
Normal file
126
tasks/ast_codegen/src/generators/derive_clone_in.rs
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
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};
|
||||
endl!();
|
||||
use crate::ast::*;
|
||||
endl!();
|
||||
|
||||
#(#impls)*
|
||||
},
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn derive_enum(def: &EnumDef) -> TokenStream {
|
||||
let ty_ident = def.ident();
|
||||
let (alloc, body) = {
|
||||
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),*
|
||||
}
|
||||
},
|
||||
)
|
||||
};
|
||||
impl_clone_in(&ty_ident, def.has_lifetime, &alloc, &body)
|
||||
}
|
||||
|
||||
fn derive_struct(def: &StructDef) -> TokenStream {
|
||||
let ty_ident = def.ident();
|
||||
let (alloc, body) = {
|
||||
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 ))
|
||||
};
|
||||
impl_clone_in(&ty_ident, def.has_lifetime, &alloc, &body)
|
||||
}
|
||||
|
||||
fn impl_clone_in(
|
||||
ty_ident: &Ident,
|
||||
has_lifetime: bool,
|
||||
alloc: &Ident,
|
||||
body: &TokenStream,
|
||||
) -> TokenStream {
|
||||
if has_lifetime {
|
||||
quote! {
|
||||
endl!();
|
||||
impl <'old_alloc, 'new_alloc> CloneIn<'new_alloc> for #ty_ident<'old_alloc> {
|
||||
type Cloned = #ty_ident<'new_alloc>;
|
||||
fn clone_in(&self, #alloc: &'new_alloc Allocator) -> Self::Cloned {
|
||||
#body
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
endl!();
|
||||
impl <'alloc> CloneIn<'alloc> for #ty_ident {
|
||||
type Cloned = #ty_ident;
|
||||
fn clone_in(&self, #alloc: &'alloc Allocator) -> Self::Cloned {
|
||||
#body
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
mod assert_layouts;
|
||||
mod ast_builder;
|
||||
mod ast_kind;
|
||||
mod derive_clone_in;
|
||||
mod impl_get_span;
|
||||
mod visit;
|
||||
|
||||
|
|
@ -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};
|
||||
|
||||
|
|
|
|||
|
|
@ -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};
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)]
|
||||
|
|
@ -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)]
|
||||
|
|
|
|||
Loading…
Reference in a new issue