Skip to content

Commit

Permalink
feat(ast, ast_macros): apply stable_repr to all #[ast] items.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Jul 28, 2024
1 parent ee94dae commit af72e1d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 26 deletions.
16 changes: 0 additions & 16 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -269,7 +268,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[serde(untagged)]
Expand Down Expand Up @@ -359,7 +357,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -436,7 +433,6 @@ pub struct TemplateElementValue<'a> {

/// <https://tc39.es/ecma262/#prod-MemberExpression>
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -597,7 +593,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -706,7 +701,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand All @@ -725,7 +719,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -780,7 +773,6 @@ macro_rules! match_simple_assignment_target {
pub use match_simple_assignment_target;

#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -846,7 +838,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -949,7 +940,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -979,7 +969,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -1050,7 +1039,6 @@ pub struct BlockStatement<'a> {

/// Declarations and the Variable Statement
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -1217,7 +1205,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -1251,7 +1238,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -2000,7 +1986,6 @@ pub struct StaticBlock<'a> {
/// export as namespace d; // TSNamespaceExportDeclaration
/// ```
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -2303,7 +2288,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_ast/src/ast/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down
6 changes: 0 additions & 6 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -190,7 +189,6 @@ pub enum TSLiteral<'a> {
/// This is the root-level type for TypeScript types, kind of like [`Expression`] is for
/// expressions.
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged, rename_all = "camelCase")]
Expand Down Expand Up @@ -482,7 +480,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged, rename_all = "camelCase")]
Expand Down Expand Up @@ -641,7 +638,6 @@ pub struct TSTypeReference<'a> {
/// IdentifierReference
/// NamespaceName . IdentifierReference
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -1034,7 +1030,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
Expand Down Expand Up @@ -1207,7 +1202,6 @@ inherit_variants! {
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged, rename_all = "camelCase")]
Expand Down
50 changes: 47 additions & 3 deletions crates/oxc_ast_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use proc_macro::TokenStream;
use proc_macro::{TokenStream, TokenTree};
use std::str::FromStr;

enum ItemKind {
Enum,
Struct,
Unknown,
}

/// Attach to AST node type (struct or enum), to signal to codegen to create visitor for this type.
///
/// Macro's role is not to generate code - it's purely a means to communicate information to the codegen.
Expand All @@ -16,9 +22,47 @@ use std::str::FromStr;
#[proc_macro_attribute]
#[allow(clippy::missing_panics_doc)]
pub fn ast(_args: TokenStream, input: TokenStream) -> TokenStream {
let mut stream = TokenStream::from_str("#[derive(::oxc_ast_macros::Ast)]").unwrap();
let mut input = input.into_iter();
let mut stream = TokenStream::new();
let mut output = TokenStream::from_str("#[derive(::oxc_ast_macros::Ast)]").unwrap();

let mut item_kind = ItemKind::Unknown;

while let Some(next) = input.next() {
if let TokenTree::Ident(ident) = &next {
match ident.to_string().as_str() {
"enum" => {
assert!(matches!(item_kind, ItemKind::Unknown));
item_kind = ItemKind::Enum;
stream.extend(Some(next));
break;
}
"struct" => {
assert!(matches!(item_kind, ItemKind::Unknown));
item_kind = ItemKind::Struct;
stream.extend(Some(next));
break;
}
_ => {}
}
}

stream.extend(Some(next));
}

// append the remained of the input tokens to the stream
stream.extend(input);
stream

let repr = match item_kind {
ItemKind::Enum => TokenStream::from_str("#[repr(C, u8)]").unwrap(),
// ItemKind::Struct => TokenStream::from_str("#[repr(C)]").unwrap(),
ItemKind::Struct => TokenStream::default(),
ItemKind::Unknown => unreachable!(),
};

output.extend(repr);
output.extend(stream);
output
}

/// Dummy derive macro for a non-existent trait `Ast`.
Expand Down

0 comments on commit af72e1d

Please sign in to comment.