|
| 1 | +use itertools::Itertools; |
| 2 | +use proc_macro2::TokenStream; |
| 3 | +use quote::{format_ident, quote}; |
| 4 | +use syn::Ident; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + output, |
| 8 | + schema::{EnumDef, GetIdent, StructDef, TypeDef}, |
| 9 | + GeneratorOutput, LateCtx, |
| 10 | +}; |
| 11 | + |
| 12 | +use super::{define_generator, generated_header, Generator}; |
| 13 | + |
| 14 | +define_generator! { |
| 15 | + pub struct DeriveCloneIn; |
| 16 | +} |
| 17 | + |
| 18 | +impl Generator for DeriveCloneIn { |
| 19 | + fn name(&self) -> &'static str { |
| 20 | + stringify!(DeriveCloneIn) |
| 21 | + } |
| 22 | + |
| 23 | + fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput { |
| 24 | + let impls: Vec<TokenStream> = ctx |
| 25 | + .schema |
| 26 | + .definitions |
| 27 | + .iter() |
| 28 | + .filter(|def| def.generates_derive("CloneIn")) |
| 29 | + .map(|def| match &def { |
| 30 | + TypeDef::Enum(it) => derive_enum(it), |
| 31 | + TypeDef::Struct(it) => derive_struct(it), |
| 32 | + }) |
| 33 | + .collect(); |
| 34 | + |
| 35 | + let header = generated_header!(); |
| 36 | + |
| 37 | + GeneratorOutput::Stream(( |
| 38 | + output(crate::AST_CRATE, "derive_clone_in.rs"), |
| 39 | + quote! { |
| 40 | + #header |
| 41 | + |
| 42 | + use oxc_allocator::{Allocator, CloneIn}; |
| 43 | + endl!(); |
| 44 | + use crate::ast::*; |
| 45 | + endl!(); |
| 46 | + |
| 47 | + #(#impls)* |
| 48 | + }, |
| 49 | + )) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +fn derive_enum(def: &EnumDef) -> TokenStream { |
| 54 | + let ty_ident = def.ident(); |
| 55 | + let (alloc, body) = { |
| 56 | + let mut used_alloc = false; |
| 57 | + let matches = def |
| 58 | + .all_variants() |
| 59 | + .map(|var| { |
| 60 | + let ident = var.ident(); |
| 61 | + if var.is_unit() { |
| 62 | + quote!(Self :: #ident => Self :: Cloned :: #ident) |
| 63 | + } else { |
| 64 | + used_alloc = true; |
| 65 | + quote!(Self :: #ident(it) => Self :: Cloned :: #ident(it.clone_in(alloc))) |
| 66 | + } |
| 67 | + }) |
| 68 | + .collect_vec(); |
| 69 | + let alloc_ident = if used_alloc { format_ident!("alloc") } else { format_ident!("_") }; |
| 70 | + ( |
| 71 | + alloc_ident, |
| 72 | + quote! { |
| 73 | + match self { |
| 74 | + #(#matches),* |
| 75 | + } |
| 76 | + }, |
| 77 | + ) |
| 78 | + }; |
| 79 | + impl_clone_in(&ty_ident, def.has_lifetime, alloc, body) |
| 80 | +} |
| 81 | + |
| 82 | +fn derive_struct(def: &StructDef) -> TokenStream { |
| 83 | + let ty_ident = def.ident(); |
| 84 | + let (alloc, body) = { |
| 85 | + let (alloc_ident, body) = if def.fields.is_empty() { |
| 86 | + (format_ident!("_"), TokenStream::default()) |
| 87 | + } else { |
| 88 | + let fields = def.fields.iter().map(|field| { |
| 89 | + let ident = field.ident(); |
| 90 | + quote!(#ident: self.#ident.clone_in(alloc)) |
| 91 | + }); |
| 92 | + (format_ident!("alloc"), quote!({ #(#fields),* })) |
| 93 | + }; |
| 94 | + (alloc_ident, quote!( #ty_ident #body )) |
| 95 | + }; |
| 96 | + impl_clone_in(&ty_ident, def.has_lifetime, alloc, body) |
| 97 | +} |
| 98 | + |
| 99 | +fn impl_clone_in( |
| 100 | + ty_ident: &Ident, |
| 101 | + has_lifetime: bool, |
| 102 | + alloc: Ident, |
| 103 | + body: TokenStream, |
| 104 | +) -> TokenStream { |
| 105 | + if has_lifetime { |
| 106 | + quote! { |
| 107 | + endl!(); |
| 108 | + impl <'old_alloc, 'new_alloc> CloneIn<'new_alloc> for #ty_ident<'old_alloc> { |
| 109 | + type Cloned = #ty_ident<'new_alloc>; |
| 110 | + fn clone_in(&self, #alloc: &'new_alloc Allocator) -> Self::Cloned { |
| 111 | + #body |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } else { |
| 116 | + quote! { |
| 117 | + endl!(); |
| 118 | + impl <'alloc> CloneIn<'alloc> for #ty_ident { |
| 119 | + type Cloned = #ty_ident; |
| 120 | + fn clone_in(&self, #alloc: &'alloc Allocator) -> Self::Cloned { |
| 121 | + #body |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments