Skip to content

Commit

Permalink
feat(ast_codegen): support for generate_derive marker. (#4728)
Browse files Browse the repository at this point in the history
Follow-on after #4276, related to #4284.
  • Loading branch information
rzvxa committed Aug 7, 2024
1 parent 6a36616 commit 2e91ad6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/oxc_ast_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn ast(_args: TokenStream, input: TokenStream) -> TokenStream {
/// Does not generate any code.
/// Only purpose is to allow using `#[scope]`, `#[visit]`, and other attrs in the AST node type defs.
/// These "marker" attributes are used in codegen.
#[proc_macro_derive(Ast, attributes(scope, visit, span, serde, tsify))]
#[proc_macro_derive(Ast, attributes(scope, visit, span, serde, tsify, generate_derive))]
pub fn ast_derive(_item: TokenStream) -> TokenStream {
TokenStream::new()
}
Expand Down
11 changes: 11 additions & 0 deletions tasks/ast_codegen/src/schema/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ impl TypeDef {
pub fn visitable(&self) -> bool {
with_either!(self, it => it.visitable)
}

pub fn generated_derives(&self) -> &Vec<String> {
with_either!(self, it => &it.generated_derives)
}

pub fn generates_derive(&self, derive: &str) -> bool {
let generated_derives = self.generated_derives();
generated_derives.iter().any(|it| it == derive)
}
}

#[derive(Debug, Serialize)]
Expand All @@ -41,6 +50,7 @@ pub struct StructDef {
pub size_32: usize,
pub align_32: usize,
pub offsets_32: Option<Vec<usize>>,
pub generated_derives: Vec<String>,
#[serde(skip)]
pub markers: OuterMarkers,
}
Expand All @@ -60,6 +70,7 @@ pub struct EnumDef {
pub size_32: usize,
pub align_32: usize,
pub offsets_32: Option<Vec<usize>>,
pub generated_derives: Vec<String>,
}

impl EnumDef {
Expand Down
21 changes: 21 additions & 0 deletions tasks/ast_codegen/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ fn lower_ast_enum(it @ rust::Enum { item, meta }: &rust::Enum, ctx: &crate::Earl
size_32,
align_32,
offsets_32,

generated_derives: parse_generate_derive(&item.attrs),
}
}

Expand Down Expand Up @@ -178,6 +180,8 @@ fn lower_ast_struct(
align_32,
offsets_32,
markers: parse_outer_markers(&item.attrs).unwrap(),

generated_derives: parse_generate_derive(&item.attrs),
}
}

Expand Down Expand Up @@ -264,6 +268,23 @@ fn get_docs(attrs: &[syn::Attribute]) -> Vec<String> {
.collect()
}

fn parse_generate_derive(attrs: &[syn::Attribute]) -> Vec<String> {
let mut derives = std::collections::HashSet::new();
for attr in attrs {
if !attr.path().is_ident("generate_derive") {
continue;
}

let args: syn::punctuated::Punctuated<syn::Ident, syn::Token![,]> =
attr.parse_args_with(syn::punctuated::Punctuated::parse_terminated).unwrap();

for arg in args {
derives.insert(arg.to_string());
}
}
Vec::from_iter(derives)
}

macro_rules! with_either {
($def:expr, $it:ident => $body:expr) => {
match $def {
Expand Down

0 comments on commit 2e91ad6

Please sign in to comment.